Scene.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include <Inventor/nodes/SoPerspectiveCamera.h>
25 #include <Inventor/nodes/SoPickStyle.h>
26 #include <Inventor/nodes/SoCone.h>
27 #include <Inventor/nodes/SoLineSet.h>
28 #include <Inventor/nodes/SoVertexProperty.h>
29 #include <Inventor/nodes/SoDrawStyle.h>
30 #include <Inventor/nodes/SoMaterial.h>
31 #include "Scene.h"
32 
33 
35 {
37 
38  scene->sceneObjectManager.reset(new SceneObjectManager(scene));
39  scene->sceneGroupManager.reset(new SceneGroupManager);
40  scene->sceneSelectionManager.reset(new SceneSelectionManager(scene));
41  scene->sceneManipulatorManager.reset(new SceneManipulatorManager(scene));
42  scene->scenePreviewGenerator.reset(new PreviewGenerator);
43 
44  return scene;
45 }
46 
47 scene3D::Scene::Scene(controller::ControllerPtr controller) : viewerMode(false), controller(controller)
48 {
49  selectionRootNode = new SoSelection;
50  selectionRootNode->setName("selectionRootNode");
51  selectionRootNode->ref();
52 
53  //Add gridfloor root and make it unpickable
54  SoPickStyle* unpickable = new SoPickStyle();
55  unpickable->style = SoPickStyle::UNPICKABLE;
56  SoSeparator* gridFloorRoot = new SoSeparator;
57  SoPickStyle* pickable = new SoPickStyle();
58  pickable->style = SoPickStyle::SHAPE;
59 
60  selectionRootNode->addChild(unpickable);
61  selectionRootNode->addChild(gridFloorRoot);
62  selectionRootNode->addChild(pickable);
63 
64  /*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65  Here we draw our grid floor. To change the way the grid floor is drawn change the following constants.
66  GRIDSIZE: How many lines to display for each direction of each axis.
67  E. g.: 2 would lead to 2 lines in negative and positive direction respectively, plus indicator for axis.
68  5 lines in sum.
69  GRIDUNIT: Factor for coin units, e.g. 2 means between 2 lines there is space for 2 coin units.
70  GRIDSUBSIZE: How many subunits to display between the larger lines. So 5 would lead to 3 lines between 2 large lines for 5
71  subunits.
72  GRIDPATTERN: Pattern for sublines. Bitpattern, where 1 is line and 0 is no line.
73  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
74  const int GRIDSIZE = 5;
75  const int GRIDUNIT = 5;
76  const int GRIDSUBSIZE = 5;
77  const unsigned short GRIDPATTERN = 0xCCCC;
78 
79  //Red material for X axis
80  SoMaterial* red = new SoMaterial;
81  red->diffuseColor = SbColor(1, 0, 0);
82  //Green material for Y axis
83  SoMaterial* green = new SoMaterial;
84  green->diffuseColor = SbColor(0, 1, 0);
85  //Blue material for Z axis
86  SoMaterial* blue = new SoMaterial;
87  blue->diffuseColor = SbColor(0, 0, 1);
88 
89  //Small lines
90  SoDrawStyle* thinStyle = new SoDrawStyle;
91  thinStyle->lineWidth = 1;
92  thinStyle->linePattern = GRIDPATTERN;
93  //Larger lines
94  SoDrawStyle* largeStyle = new SoDrawStyle;
95  largeStyle->lineWidth = 2;
96 
97  //Our set of vertices for the grid lines
98  SoVertexProperty* vertices = new SoVertexProperty;
99  SoVertexProperty* subVertices = new SoVertexProperty;
100  SoVertexProperty* xVertices = new SoVertexProperty;
101  SoVertexProperty* yVertices = new SoVertexProperty;
102  SoVertexProperty* zVertices = new SoVertexProperty;
103 
104  //Definition of which vertex belongs to which line and should be connected
105  SoLineSet* lines = new SoLineSet;
106  SoLineSet* subLines = new SoLineSet;
107  SoLineSet* xLines = new SoLineSet;
108  SoLineSet* yLines = new SoLineSet;
109  SoLineSet* zLines = new SoLineSet;
110 
111  //Add 2 vertices for X axis
112  xVertices->vertex.set1Value(0, GRIDSIZE * GRIDUNIT, 0, 0);
113  xVertices->vertex.set1Value(1, -(GRIDSIZE * GRIDUNIT), 0, 0);
114  //Connect them to a line by adding '2' to numVertices at the correct position
115  xLines->numVertices.set1Value(0, 2);
116 
117  //Y axis
118  yVertices->vertex.set1Value(0, 0, GRIDSIZE * GRIDUNIT, 0);
119  yVertices->vertex.set1Value(1, 0, -(GRIDSIZE * GRIDUNIT), 0);
120  yLines->numVertices.set1Value(0, 2);
121 
122  //Z axis
123  zVertices->vertex.set1Value(0, 0, 0, GRIDSIZE * GRIDUNIT);
124  zVertices->vertex.set1Value(1, 0, 0, -(GRIDSIZE * GRIDUNIT));
125  zLines->numVertices.set1Value(0, 2);
126 
127  //Counters to keep track of vertex index
128  int verticescounter = 0;
129  int subverticescounter = 0;
130 
131  //Draw all lines parallel to the X and Y axis and all sublines, excepted axis itself
132  for (int i = -(GRIDSIZE * GRIDUNIT); i < GRIDUNIT * (GRIDSIZE + 1); i += GRIDUNIT)
133  {
134  if (i != 0)
135  {
136  vertices->vertex.set1Value(verticescounter++, GRIDSIZE * GRIDUNIT, i, 0);
137  vertices->vertex.set1Value(verticescounter++, -(GRIDSIZE * GRIDUNIT), i, 0);
138  lines->numVertices.set1Value((verticescounter - 1) / 2, 2);
139 
140  vertices->vertex.set1Value(verticescounter++, i, GRIDSIZE * GRIDUNIT, 0);
141  vertices->vertex.set1Value(verticescounter++, i, -(GRIDSIZE * GRIDUNIT), 0);
142  lines->numVertices.set1Value((verticescounter - 1) / 2, 2);
143  }
144 
145  //If this is not the last line to draw, draw sublines
146  if (i < GRIDUNIT * GRIDSIZE)
147  {
148  float delta = (float)GRIDUNIT / (float)GRIDSUBSIZE;
149 
150  for (int n = 1; n < GRIDSUBSIZE; n++)
151  {
152  subVertices->vertex.set1Value(subverticescounter++, GRIDSIZE * GRIDUNIT, (float)i + (float)n * delta, 0);
153  subVertices->vertex.set1Value(subverticescounter++, -(GRIDSIZE * GRIDUNIT), (float)i + (float)n * delta, 0);
154  subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
155 
156  subVertices->vertex.set1Value(subverticescounter++, (float)i + (float)n * delta, GRIDSIZE * GRIDUNIT, 0);
157  subVertices->vertex.set1Value(subverticescounter++, (float)i + (float)n * delta, -(GRIDSIZE * GRIDUNIT), 0);
158  subLines->numVertices.set1Value((subverticescounter - 1) / 2, 2);
159  }
160  }
161  }
162 
163 
164  lines->vertexProperty.setValue(vertices);
165  subLines->vertexProperty.setValue(subVertices);
166  xLines->vertexProperty.setValue(xVertices);
167  yLines->vertexProperty.setValue(yVertices);
168  zLines->vertexProperty.setValue(zVertices);
169 
170  gridFloorRoot->addChild(thinStyle);
171  gridFloorRoot->addChild(subLines);
172  gridFloorRoot->addChild(largeStyle);
173  gridFloorRoot->addChild(lines);
174  gridFloorRoot->addChild(red);
175  gridFloorRoot->addChild(xLines);
176  gridFloorRoot->addChild(green);
177  gridFloorRoot->addChild(yLines);
178  gridFloorRoot->addChild(blue);
179  gridFloorRoot->addChild(zLines);
180 
181  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
182  //Gridfloor done
183 
184  objectRootNode = new SoSeparator;
185  objectRootNode->setName("objectRootNode");
186  selectionRootNode->addChild(objectRootNode);
187 }
188 
189 scene3D::Scene::~Scene()
190 {
191  selectionRootNode->unref();
192 }
193 
194 
195 void scene3D::Scene::makeCameraViewAll(SoCamera* camera, const SbViewportRegion& region)
196 {
197  camera->viewAll(this->objectRootNode, region);
198 }
199 
200 SoSeparator* scene3D::Scene::registerCamera(SoCamera* camera)
201 {
202  SoSeparator* cameraRoot = new SoSeparator;
203  cameraRoot->addChild(camera);
204  cameraRoot->addChild(selectionRootNode);
205 
206  return cameraRoot;
207 }
208 
209 scene3D::SceneObjectManagerPtr scene3D::Scene::getObjectManager()
210 {
211  return sceneObjectManager;
212 }
213 
214 scene3D::SceneGroupManagerPtr scene3D::Scene::getGroupManager()
215 {
216  return sceneGroupManager;
217 }
218 
219 scene3D::SceneSelectionManagerPtr scene3D::Scene::getSelectionManager()
220 {
221  return sceneSelectionManager;
222 }
223 
224 scene3D::SceneManipulatorManagerPtr scene3D::Scene::getManipulatorManager()
225 {
226  return sceneManipulatorManager;
227 }
228 
229 scene3D::PreviewGeneratorPtr scene3D::Scene::getPreviewGenerator()
230 {
231  return scenePreviewGenerator;
232 }
233 
234 controller::ControllerWeakPtr scene3D::Scene::getController()
235 {
236  return controller;
237 }
238 
239 void scene3D::Scene::enterEditorMode()
240 {
241  if (this->viewerMode)
242  {
243  this->selectionRootNode->policy = SoSelection::SHIFT;
244  this->sceneSelectionManager->deselectAll();
245  this->sceneSelectionManager->restoreHistory();
246  this->sceneSelectionManager->setCreateOperations(true);
247  this->viewerMode = false;
248  }
249 }
250 
251 void scene3D::Scene::enterViewerMode()
252 {
253  if (!this->viewerMode)
254  {
255  this->sceneSelectionManager->setCreateOperations(false);
256  this->selectionRootNode->policy = SoSelection::SINGLE;
257  scene3D::SceneObjectPtr activeObject = this->sceneSelectionManager->getLastSelected();
258  this->sceneSelectionManager->storeHistory();
259  this->sceneSelectionManager->deselectAll();
260 
261  if (activeObject)
262  {
263  this->sceneSelectionManager->addToSelection(activeObject);
264  }
265 
266  this->viewerMode = true;
267  }
268 }
scene3D::Scene::SceneManipulatorManager
friend class SceneManipulatorManager
Definition: Scene.h:68
scene3D::Scene
Definition: Scene.h:61
scene3D::Scene::SceneObjectManager
friend class SceneObjectManager
Definition: Scene.h:64
scene3D::Scene::create
static ScenePtr create(controller::ControllerPtr controller)
Creates a new instance of the Scene.
Definition: Scene.cpp:34
scene3D::ScenePtr
std::shared_ptr< Scene > ScenePtr
Definition: PointerDefinitions.h:36
controller
Definition: AddOperation.h:39
controller::ControllerPtr
std::shared_ptr< Controller > ControllerPtr
Definition: ClassDefinitions.h:41
scene3D::SceneGroupManager
Definition: SceneGroupManager.h:34
scene3D::PreviewGenerator
Definition: PreviewGenerator.h:34
Scene.h
scene3D::Scene::SceneSelectionManager
friend class SceneSelectionManager
Definition: Scene.h:66