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