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
31SO_KIT_SOURCE(scene3D::SceneObject)
32
33void
35{
36 SO_KIT_INIT_CLASS(SceneObject, SoBaseKit, "BaseKit");
37}
38
39bool scene3D::SceneObject::isInitialized = false;
40
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
88scene3D::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
101SbVec3f
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
120void
122{
123 this->transformNode->translation.setValue(translation);
124
125 //Reset manipulator
126 if (this->hasManipulator())
127 {
128 this->manipNode->setToDefaults();
129 }
130}
131
132SbRotation
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
152void
154{
155 this->transformNode->rotation.setValue(rotation);
156
157 //Reset manipulator
158 if (this->hasManipulator())
159 {
160 this->manipNode->setToDefaults();
161 }
162}
163
164void
166{
167 this->switchNode->whichChild = show ? 1 : 0;
168}
169
170void
171scene3D::SceneObject::addManipulator(SoTransformManip* manip)
172{
173 this->manipNode = manip;
174 this->setPart("transformManip", manipNode);
175}
176
177void
178scene3D::SceneObject::applyManipulator()
179{
180 if (this->hasManipulator())
181 {
182 transformNode->combineRight(manipNode);
183 this->setPart("transformManip", NULL);
184 this->manipNode = nullptr;
185 }
186}
187
188bool
189scene3D::SceneObject::hasManipulator()
190{
191 return this->manipNode != nullptr;
192}
193
194std::string
196{
197 return this->objectId;
198}
199
200void
202{
203 this->objectId = objectId;
204}
205
206std::string
208{
209 return classId;
210}
211
212void
214{
215 this->classId = classId;
216}
217
218std::string
220{
221 return collection;
222}
223
224void
226{
227 this->collection = collection;
228}
229
230std::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
272void
273scene3D::SceneObject::pushHistory()
274{
275 this->lastRotation = this->getRotation();
276 this->lastTranslation = this->getTranslation();
277}
278
279SbRotation
280scene3D::SceneObject::getHistoryRotation()
281{
282 return this->lastRotation;
283}
284
285SbVec3f
286scene3D::SceneObject::getHistoryTranslation()
287{
288 return lastTranslation;
289}
290
291void
292scene3D::SceneObject::trackThisTransformation(SoTransform* transformation)
293{
294 this->manipNode->rotation.connectFrom(&transformation->rotation);
295 this->manipNode->translation.connectFrom(&transformation->translation);
296}
297
298void
299scene3D::SceneObject::untrackTransformations()
300{
301 this->manipNode->rotation.disconnect();
302 this->manipNode->translation.disconnect();
303}
304
305bool
307{
308 return !isManipulated;
309}
void setTranslation(SbVec3f translation)
Sets the Translation of the SceneObject.
std::string getObjectId() const
Returns the ObjectId of the SceneObject.
SbVec3f getTranslation()
Returns the Translation of the SceneObject.
std::string getClassId() const
Returns the ClassId of the SceneObject.
std::string getCollection() const
Returns the Collection of the SceneObject.
SceneObject(std::string objectId, std::string classId, std::string collection, SoSeparator *geometry, SoSeparator *collision)
std::map< std::string, std::string > getAllAttributes()
Returns all Attributes of the SceneObject as Map.
void showCollisionMesh(bool show)
Decides, whether CollisionMesh is beeing shown.
void setObjectId(std::string &objectId)
Sets the ObjectId of the SceneObject.
void setRotation(SbRotation rotation)
Sets the Rotation of the SceneObject.
bool isMutable()
Returns if you are allowed to change this object.
void setClassId(std::string &classId)
Sets the ClassId of the SceneObject.
static void initClass()
Initializes Class.
SbRotation getRotation()
Returns the Rotation of the SceneObject.
void setCollection(std::string &collection)
Sets the Collection of the SceneObject.
This file offers overloads of toIce() and fromIce() functions for STL container types.