24 #include <Inventor/nodes/SoTransform.h>
27 #include <boost/format.hpp>
33 SO_KIT_INIT_CLASS(
SceneObject, SoBaseKit,
"BaseKit");
36 bool scene3D::SceneObject::isInitialized =
false;
39 SoSeparator* geometry, SoSeparator* collision) : classId(classId), collection(collection), manipNode(nullptr), isManipulated(false)
42 if (!SceneObject::isInitialized)
45 SceneObject::isInitialized =
true;
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();
67 this->setPart(
"root",
new SoSeparator);
70 this->transformNode =
new SoTransform;
71 this->setPart(
"transform", this->transformNode);
74 this->switchNode =
new SoSwitch;
75 this->setPart(
"modelswitch", this->switchNode);
77 this->switchNode->whichChild = 0;
80 this->setPart(
"visual", geometry);
83 this->setPart(
"collision", collision);
86 scene3D::SceneObject::SceneObject() : isManipulated(false)
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();
106 if (this->hasManipulator())
108 SoTransform* tempTransform =
new SoTransform;
109 tempTransform->ref();
111 tempTransform->combineRight(transformNode);
112 tempTransform->combineRight(manipNode);
114 SbVec3f result = tempTransform->translation.getValue();
115 tempTransform->unref();
119 return this->transformNode->translation.getValue();
124 this->transformNode->translation.setValue(translation);
127 if (this->hasManipulator())
129 this->manipNode->setToDefaults();
135 if (this->hasManipulator())
137 SoTransform* tempTransform =
new SoTransform;
138 tempTransform->ref();
140 tempTransform->combineRight(transformNode);
141 tempTransform->combineRight(manipNode);
144 SbRotation result = tempTransform->rotation.getValue();
145 tempTransform->unref();
149 return this->transformNode->rotation.getValue();
154 this->transformNode->rotation.setValue(rotation);
157 if (this->hasManipulator())
159 this->manipNode->setToDefaults();
165 this->switchNode->whichChild = show ? 1 : 0;
168 void scene3D::SceneObject::addManipulator(SoTransformManip* manip)
170 this->manipNode = manip;
171 this->setPart(
"transformManip", manipNode);
174 void scene3D::SceneObject::applyManipulator()
176 if (this->hasManipulator())
178 transformNode->combineRight(manipNode);
179 this->setPart(
"transformManip", NULL);
180 this->manipNode =
nullptr;
184 bool scene3D::SceneObject::hasManipulator()
186 return this->manipNode !=
nullptr;
191 return this->objectId;
196 this->objectId = objectId;
206 this->classId = classId;
216 this->collection = collection;
221 std::map<std::string, std::string> allAttributes;
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)));
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)));
240 matrix.setRotate(this->getRotation());
241 matrix.setTranslate(this->getTranslation());
244 for (
int i = 0; i < 4; i++)
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]));
256 allAttributes.insert(std::pair<std::string, std::string>(
"Matrix", out));
258 return allAttributes;
260 void scene3D::SceneObject::pushHistory()
262 this->lastRotation = this->getRotation();
263 this->lastTranslation = this->getTranslation();
266 SbRotation scene3D::SceneObject::getHistoryRotation()
268 return this->lastRotation;
271 SbVec3f scene3D::SceneObject::getHistoryTranslation()
273 return lastTranslation;
276 void scene3D::SceneObject::trackThisTransformation(SoTransform* transformation)
278 this->manipNode->rotation.connectFrom(&transformation->rotation);
279 this->manipNode->translation.connectFrom(&transformation->translation);
282 void scene3D::SceneObject::untrackTransformations()
284 this->manipNode->rotation.disconnect();
285 this->manipNode->translation.disconnect();
290 return !isManipulated;