SceneObject.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/SoTransform.h>
25 #include "SceneObject.h"
26 #include "Scene.h"
27 #include <boost/format.hpp>
28 
29 SO_KIT_SOURCE(scene3D::SceneObject)
30 
31 void scene3D::SceneObject::initClass()
32 {
33  SO_KIT_INIT_CLASS(SceneObject, SoBaseKit, "BaseKit");
34 }
35 
36 bool scene3D::SceneObject::isInitialized = false;
37 
38 scene3D::SceneObject::SceneObject(std::string objectId, std::string classId, std::string collection,
39  SoSeparator* geometry, SoSeparator* collision) : classId(classId), collection(collection), manipNode(nullptr), isManipulated(false)
40 {
41 
42  if (!SceneObject::isInitialized)
43  {
45  SceneObject::isInitialized = true;
46  }
47 
48  //Initializer for our BaseKit
49  SO_KIT_CONSTRUCTOR(SceneObject);
50  SO_KIT_ADD_CATALOG_ENTRY(root, SoSeparator, false, this, "", TRUE);
51  SO_KIT_ADD_CATALOG_ENTRY(transformManip, SoTransformManip,
52  TRUE, root, "", TRUE);
53  SO_KIT_ADD_CATALOG_ENTRY(transform, SoTransform,
54  TRUE, root, "", TRUE);
55  SO_KIT_ADD_CATALOG_ENTRY(modelswitch, SoSwitch,
56  TRUE, root, "", TRUE);
57  SO_KIT_ADD_CATALOG_ENTRY(visual, SoSeparator,
58  TRUE, modelswitch, "", TRUE);
59  SO_KIT_ADD_CATALOG_ENTRY(collision, SoSeparator,
60  TRUE, modelswitch, "", TRUE);
61  SO_KIT_INIT_INSTANCE();
62 
63  //Use given Ids as our node name and object class
64  this->setObjectId(objectId);
65 
66  //Initialize root node
67  this->setPart("root", new SoSeparator);
68 
69  //Add an empty transform node, manipulator is still null and not displayed
70  this->transformNode = new SoTransform;
71  this->setPart("transform", this->transformNode);
72 
73  //Add switch for visual and collision model
74  this->switchNode = new SoSwitch;
75  this->setPart("modelswitch", this->switchNode);
76  //Set switch to show visual model
77  this->switchNode->whichChild = 0;
78 
79  //Insert visual model
80  this->setPart("visual", geometry);
81 
82  //Insert collision model
83  this->setPart("collision", collision);
84 }
85 
86 scene3D::SceneObject::SceneObject() : isManipulated(false)
87 {
88  //Initializer for our BaseKit
89  SO_KIT_CONSTRUCTOR(SceneObject);
90  SO_KIT_ADD_CATALOG_ENTRY(root, SoSeparator, false, this, "", TRUE);
91  SO_KIT_ADD_CATALOG_ENTRY(transform, SoTransform,
92  TRUE, this, nullptr, TRUE);
93  SO_KIT_ADD_CATALOG_ENTRY(transformManip, SoTransformManip,
94  TRUE, root, "", TRUE);
95  SO_KIT_ADD_CATALOG_ENTRY(modelswitch, SoSwitch,
96  TRUE, this, nullptr, TRUE);
97  SO_KIT_ADD_CATALOG_ENTRY(visual, SoSeparator,
98  TRUE, this, modelswitch, TRUE);
99  SO_KIT_ADD_CATALOG_ENTRY(collision, SoSeparator,
100  TRUE, this, modelswitch, TRUE);
101  SO_KIT_INIT_INSTANCE();
102 }
103 
105 {
106  if (this->hasManipulator())
107  {
108  SoTransform* tempTransform = new SoTransform;
109  tempTransform->ref();
110 
111  tempTransform->combineRight(transformNode);
112  tempTransform->combineRight(manipNode);
113 
114  SbVec3f result = tempTransform->translation.getValue();
115  tempTransform->unref();
116  return result;
117  }
118 
119  return this->transformNode->translation.getValue();
120 }
121 
122 void scene3D::SceneObject::setTranslation(SbVec3f translation)
123 {
124  this->transformNode->translation.setValue(translation);
125 
126  //Reset manipulator
127  if (this->hasManipulator())
128  {
129  this->manipNode->setToDefaults();
130  }
131 }
132 
134 {
135  if (this->hasManipulator())
136  {
137  SoTransform* tempTransform = new SoTransform;
138  tempTransform->ref();
139 
140  tempTransform->combineRight(transformNode);
141  tempTransform->combineRight(manipNode);
142 
143 
144  SbRotation result = tempTransform->rotation.getValue();
145  tempTransform->unref();
146  return result;
147  }
148 
149  return this->transformNode->rotation.getValue();
150 }
151 
152 void scene3D::SceneObject::setRotation(SbRotation rotation)
153 {
154  this->transformNode->rotation.setValue(rotation);
155 
156  //Reset manipulator
157  if (this->hasManipulator())
158  {
159  this->manipNode->setToDefaults();
160  }
161 }
162 
164 {
165  this->switchNode->whichChild = show ? 1 : 0;
166 }
167 
168 void scene3D::SceneObject::addManipulator(SoTransformManip* manip)
169 {
170  this->manipNode = manip;
171  this->setPart("transformManip", manipNode);
172 }
173 
174 void scene3D::SceneObject::applyManipulator()
175 {
176  if (this->hasManipulator())
177  {
178  transformNode->combineRight(manipNode);
179  this->setPart("transformManip", NULL);
180  this->manipNode = nullptr;
181  }
182 }
183 
184 bool scene3D::SceneObject::hasManipulator()
185 {
186  return this->manipNode != nullptr;
187 }
188 
190 {
191  return this->objectId;
192 }
193 
194 void scene3D::SceneObject::setObjectId(std::string& objectId)
195 {
196  this->objectId = objectId;
197 }
198 
200 {
201  return classId;
202 }
203 
204 void scene3D::SceneObject::setClassId(std::string& classId)
205 {
206  this->classId = classId;
207 }
208 
210 {
211  return collection;
212 }
213 
214 void scene3D::SceneObject::setCollection(std::string& collection)
215 {
216  this->collection = collection;
217 }
218 
219 std::map<std::string, std::string> scene3D::SceneObject::getAllAttributes()
220 {
221  std::map<std::string, std::string> allAttributes;
222 
223  //Translation
224  float x, y, z;
225  this->getTranslation().getValue(x, y, z);
226  allAttributes.insert(std::pair<std::string, std::string>("Translation X", std::to_string(x)));
227  allAttributes.insert(std::pair<std::string, std::string>("Translation Y", std::to_string(y)));
228  allAttributes.insert(std::pair<std::string, std::string>("Translation Z", std::to_string(z)));
229 
230  //Rotation
231  float pw, px, py, pz;
232  this->getRotation().getValue(px, py, pz, pw);
233  allAttributes.insert(std::pair<std::string, std::string>("Rotation X", std::to_string(px)));
234  allAttributes.insert(std::pair<std::string, std::string>("Rotation Y", std::to_string(py)));
235  allAttributes.insert(std::pair<std::string, std::string>("Rotation Z", std::to_string(pz)));
236  allAttributes.insert(std::pair<std::string, std::string>("Angle", std::to_string(pw)));
237 
238  //Matrix
239  SbMatrix matrix;
240  matrix.setRotate(this->getRotation());
241  matrix.setTranslate(this->getTranslation());
242  std::string out;
243 
244  for (int i = 0; i < 4; i++)
245  {
246  out.append(boost::str(boost::format("%3.2g %3.2g %3.2g %3.2g") %
247  matrix[i][0] % matrix[i][1] %
248  matrix[i][2] % matrix[i][3]));
249 
250  if (i < 3)
251  {
252  out.append("\n");
253  }
254  }
255 
256  allAttributes.insert(std::pair<std::string, std::string>("Matrix", out));
257 
258  return allAttributes;
259 }
260 void scene3D::SceneObject::pushHistory()
261 {
262  this->lastRotation = this->getRotation();
263  this->lastTranslation = this->getTranslation();
264 }
265 
266 SbRotation scene3D::SceneObject::getHistoryRotation()
267 {
268  return this->lastRotation;
269 }
270 
271 SbVec3f scene3D::SceneObject::getHistoryTranslation()
272 {
273  return lastTranslation;
274 }
275 
276 void scene3D::SceneObject::trackThisTransformation(SoTransform* transformation)
277 {
278  this->manipNode->rotation.connectFrom(&transformation->rotation);
279  this->manipNode->translation.connectFrom(&transformation->translation);
280 }
281 
282 void scene3D::SceneObject::untrackTransformations()
283 {
284  this->manipNode->rotation.disconnect();
285  this->manipNode->translation.disconnect();
286 }
287 
289 {
290  return !isManipulated;
291 }
scene3D::SceneObject::setTranslation
void setTranslation(SbVec3f translation)
Sets the Translation of the SceneObject.
Definition: SceneObject.cpp:122
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
scene3D::SceneObject::showCollisionMesh
void showCollisionMesh(bool show)
Decides, whether CollisionMesh is beeing shown.
Definition: SceneObject.cpp:163
scene3D::SceneObject::getRotation
SbRotation getRotation()
Returns the Rotation of the SceneObject.
Definition: SceneObject.cpp:133
scene3D::SceneObject::setCollection
void setCollection(std::string &collection)
Sets the Collection of the SceneObject.
Definition: SceneObject.cpp:214
scene3D::SceneObject::setObjectId
void setObjectId(std::string &objectId)
Sets the ObjectId of the SceneObject.
Definition: SceneObject.cpp:194
scene3D::SceneObject::SceneObject
SceneObject(std::string objectId, std::string classId, std::string collection, SoSeparator *geometry, SoSeparator *collision)
Definition: SceneObject.cpp:38
scene3D::SceneObject
Definition: SceneObject.h:43
scene3D::SceneObject::getCollection
std::string getCollection() const
Returns the Collection of the SceneObject.
Definition: SceneObject.cpp:209
scene3D::SceneObject::setClassId
void setClassId(std::string &classId)
Sets the ClassId of the SceneObject.
Definition: SceneObject.cpp:204
Scene.h
scene3D::SceneObject::getTranslation
SbVec3f getTranslation()
Returns the Translation of the SceneObject.
Definition: SceneObject.cpp:104
SceneObject.h
scene3D::SceneObject::isMutable
bool isMutable()
Returns if you are allowed to change this object.
Definition: SceneObject.cpp:288
scene3D::SceneObject::getClassId
std::string getClassId() const
Returns the ClassId of the SceneObject.
Definition: SceneObject.cpp:199
scene3D::SceneObject::setRotation
void setRotation(SbRotation rotation)
Sets the Rotation of the SceneObject.
Definition: SceneObject.cpp:152
scene3D
Definition: ManipulatorMode.h:26
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:315
scene3D::SceneObject::getAllAttributes
std::map< std::string, std::string > getAllAttributes()
Returns all Attributes of the SceneObject as Map.
Definition: SceneObject.cpp:219
scene3D::SceneObject::initClass
static void initClass()
Initializes Class.
Definition: SceneObject.cpp:31
scene3D::SceneObject::getObjectId
std::string getObjectId() const
Returns the ObjectId of the SceneObject.
Definition: SceneObject.cpp:189