3 #include "VsMovableItem.h"
6 #include "VsMotorizedJoint.h"
7 #include "VsRigidBody.h"
10 #include "VsClassFactory.h"
11 #include "VsSimulator.h"
12 #include "VsOsgUserData.h"
13 #include "VsOsgUserDataVisitor.h"
16 #include "VsMouseSpring.h"
18 #include "VsCameraManipulator.h"
19 #include "VsDragger.h"
30 VsMovableItem::VsMovableItem()
32 m_bCullBackfaces =
false;
33 m_eTextureMode = GL_TEXTURE_2D;
38 m_lpParentVsMI = NULL;
44 VsMovableItem::~VsMovableItem()
56 void VsMovableItem::SetThisPointers()
58 m_lpThisAB =
dynamic_cast<AnimatBase *
>(
this);
60 THROW_TEXT_ERROR(Vs_Err_lThisPointerNotDefined, Vs_Err_strThisPointerNotDefined,
"m_lpThisAB");
62 m_lpThisMI =
dynamic_cast<MovableItem *
>(
this);
64 THROW_TEXT_ERROR(Vs_Err_lThisPointerNotDefined, Vs_Err_strThisPointerNotDefined,
"m_lpThisMI, " + m_lpThisAB->Name());
68 THROW_TEXT_ERROR(Vs_Err_lThisPointerNotDefined, Vs_Err_strThisPointerNotDefined,
"m_lpThisVsMI, " + m_lpThisAB->Name());
70 m_lpThisMI->PhysicsMovableItem(
this);
73 std::string VsMovableItem::Physics_ID()
76 return m_lpThisAB->ID();
81 #pragma region Selection-Code
83 void VsMovableItem::Physics_Selected(
bool bValue,
bool bSelectMultiple)
85 if(m_osgNodeGroup.valid() && m_osgDragger.valid() && m_osgSelectedGroup.valid())
87 bool bIsReceptiveFieldMode = (m_lpThisAB->GetSimulator()->VisualSelectionMode() & RECEPTIVE_FIELD_SELECTION_MODE);
90 bool bNodeFound = m_osgNodeGroup->containsNode(m_osgSelectedGroup.get());
91 if(bValue && !bNodeFound)
93 m_osgNodeGroup->addChild(m_osgSelectedGroup.get());
94 if(!bIsReceptiveFieldMode)
95 m_osgDragger->AddToScene();
100 else if(!bValue && bNodeFound)
102 m_osgNodeGroup->removeChild(m_osgSelectedGroup.get());
103 m_osgDragger->RemoveFromScene();
104 HideSelectedVertex();
109 void VsMovableItem::CreateSelectedGraphics(std::string strName)
111 m_osgSelectedGroup =
new osg::Group();
112 m_osgSelectedGroup->setName(strName +
"_SelectedGroup");
113 m_osgSelectedGroup->addChild(m_osgNode.get());
119 osg::StateSet* stateset =
new osg::StateSet;
120 osg::PolygonOffset* polyoffset =
new osg::PolygonOffset;
121 polyoffset->setFactor(-1.0f);
122 polyoffset->setUnits(-1.0f);
123 osg::PolygonMode* polymode =
new osg::PolygonMode;
124 polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
125 stateset->setAttributeAndModes(polyoffset,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
126 stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
128 osg::Material* material =
new osg::Material;
129 stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
130 stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
132 stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
134 m_osgSelectedGroup->setStateSet(stateset);
136 CreateDragger(strName);
137 CreateSelectedVertex(strName);
140 void VsMovableItem::CreateDragger(std::string strName)
142 std::string strVers = osgGetSOVersion();
144 if(m_lpThisAB->GetSimulator())
146 if(GetVsSimulator()->OsgCmdMgr())
148 if(m_osgDragger.valid())
149 m_osgDragger.release();
151 m_osgDragger =
new VsDragger(
this, m_lpThisMI->AllowTranslateDragX(), m_lpThisMI->AllowTranslateDragY(), m_lpThisMI->AllowTranslateDragX(),
152 m_lpThisMI->AllowRotateDragX(), m_lpThisMI->AllowRotateDragY(), m_lpThisMI->AllowRotateDragZ(),
153 m_lpThisMI->UserDefinedDraggerRadius());
154 m_osgDragger->setName(strName +
"_Dragger");
156 m_osgDragger->setupDefaultGeometry();
158 GetVsSimulator()->OsgCmdMgr()->connect(*m_osgDragger, *m_osgMT);
167 void VsMovableItem::CreateSelectedVertex(std::string strName)
169 if(!m_osgSelVertexNode.valid())
171 m_osgSelVertexNode =
new osg::Geode();
172 m_osgSelVertexNode->setName(strName +
"SelVertex");
173 float fltRadius = m_lpThisAB->GetSimulator()->RecFieldSelRadius();
174 osg::ShapeDrawable *osgDraw =
new osg::ShapeDrawable(
new osg::Sphere(osg::Vec3(0, 0, 0), fltRadius));
175 osgDraw->setColor(osg::Vec4(0, 1, 0, 0));
176 m_osgSelVertexNode->addDrawable(osgDraw);
179 if(!m_osgSelVertexMT.valid())
181 m_osgSelVertexMT =
new osg::MatrixTransform();
185 osgMT.makeIdentity();
186 m_osgSelVertexMT->setMatrix(osgMT);
188 m_osgSelVertexMT->addChild(m_osgSelVertexNode.get());
192 void VsMovableItem::DeleteSelectedVertex()
194 HideSelectedVertex();
196 if(m_osgSelVertexNode.valid()) m_osgSelVertexNode.release();
197 if(m_osgSelVertexMT.valid()) m_osgSelVertexMT.release();
202 float *VsMovableItem::Physics_GetDataPointer(
const std::string &strDataType) {
return NULL;}
204 void VsMovableItem::LocalMatrix(osg::Matrix osgLocalMT)
206 m_osgLocalMatrix = osgLocalMT;
207 m_osgFinalMatrix = osgLocalMT;
211 void VsMovableItem::GeometryRotationMatrix(osg::Matrix osgGeometryMT)
221 void VsMovableItem::AttachedPartMovedOrRotated(std::string strID)
223 Physics_ResetGraphicsAndPhysics();
226 void VsMovableItem::CreateGraphicsGeometry() {}
228 void VsMovableItem::CreatePhysicsGeometry() {}
230 void VsMovableItem::ResizePhysicsGeometry() {}
232 void VsMovableItem::CreateGeometry()
234 CreateGraphicsGeometry();
235 m_osgGeometry->setName(m_lpThisAB->Name() +
"_Geometry");
237 osg::Geode *osgGroup =
new osg::Geode;
238 osgGroup->addDrawable(m_osgGeometry.get());
239 osgGroup->setName(m_lpThisAB->Name() +
"_Node");
249 m_osgNode = osgGroup;
251 CreatePhysicsGeometry();
254 void VsMovableItem::SetupGraphics()
256 m_osgParent = ParentOSG();
258 if(m_osgParent.valid())
262 SetColor(*m_lpThisMI->Ambient(), *m_lpThisMI->Diffuse(), *m_lpThisMI->Specular(), m_lpThisMI->Shininess());
263 SetTexture(m_lpThisMI->Texture());
265 SetVisible(m_lpThisMI->IsVisible());
268 m_osgParent->addChild(m_osgRoot.get());
271 Physics_UpdateAbsolutePosition();
276 osgVisitor->traverse(*m_osgMT);
280 void VsMovableItem::DeleteGraphics()
282 if(m_osgParent.valid() && m_osgRoot.valid())
284 if(m_osgParent->containsNode(m_osgRoot.get()))
285 m_osgParent->removeChild(m_osgRoot.get());
288 if(m_osgSelVertexNode.valid()) m_osgSelVertexNode.release();
289 if(m_osgSelVertexMT.valid()) m_osgSelVertexMT.release();
291 if(m_osgCull.valid()) m_osgCull.release();
292 if(m_osgTexture.valid()) m_osgTexture.release();
293 if(m_osgStateSet.valid()) m_osgStateSet.release();
294 if(m_osgMaterial.valid()) m_osgMaterial.release();
296 if(m_osgGeometry.valid()) m_osgGeometry.release();
297 if(m_osgNode.valid()) m_osgNode.release();
298 if(m_osgSelectedGroup.valid()) m_osgSelectedGroup.release();
299 if(m_osgNodeGroup.valid()) m_osgNodeGroup.release();
301 if(m_osgMT.valid()) m_osgMT.release();
302 if(m_osgRoot.valid()) m_osgRoot.release();
303 if(m_osgParent.valid()) m_osgParent.release();
308 return m_lpParentVsMI;
311 osg::Matrix VsMovableItem::GetWorldMatrix()
313 return m_osgWorldMatrix;
316 osg::Matrix VsMovableItem::GetParentWorldMatrix()
319 return m_lpParentVsMI->GetWorldMatrix();
321 osg::Matrix osgMatrix;
322 osgMatrix.makeIdentity();
326 void VsMovableItem::UpdateWorldMatrix()
328 osg::Matrix osgParentMatrix = GetParentWorldMatrix();
331 m_osgWorldMatrix = m_osgFinalMatrix * osgParentMatrix;
334 CStdFPoint VsMovableItem::GetOSGWorldCoords()
337 osg::Vec3 vCoord = m_osgWorldMatrix.getTrans();
338 CStdFPoint vPoint(vCoord[0], vCoord[1], vCoord[2]);
343 osg::Matrix VsMovableItem::GetOSGWorldMatrix(
bool bUpdate)
348 return m_osgWorldMatrix;
351 bool VsMovableItem::Physics_CalculateLocalPosForWorldPos(
float fltWorldX,
float fltWorldY,
float fltWorldZ, CStdFPoint &vLocalPos)
357 fltWorldX *= m_lpThisAB->GetSimulator()->InverseDistanceUnits();
358 fltWorldY *= m_lpThisAB->GetSimulator()->InverseDistanceUnits();
359 fltWorldZ *= m_lpThisAB->GetSimulator()->InverseDistanceUnits();
361 CStdFPoint vPos(fltWorldX, fltWorldY, fltWorldZ), vRot(0, 0, 0);
362 osg::Matrix osgWorldPos = SetupMatrix(vPos, vRot);
365 osg::Matrix osgInverse = osg::Matrix::inverse(lpParent->GetWorldMatrix());
367 osg::Matrix osgCalc = osgWorldPos * osgInverse;
369 osg::Vec3 vCoord = osgCalc.getTrans();
370 vLocalPos.Set(vCoord[0] * m_lpThisAB->GetSimulator()->DistanceUnits(),
371 vCoord[1] * m_lpThisAB->GetSimulator()->DistanceUnits(),
372 vCoord[2] * m_lpThisAB->GetSimulator()->DistanceUnits());
416 void VsMovableItem::WorldToBodyCoords(VxReal3 vWorld, StdVector3 &vLocalPos)
418 osg::Vec3f vWorldPos;
421 vLocalPos[0] = vWorld[0]; vLocalPos[1] = vWorld[1]; vLocalPos[2] = vWorld[2];
422 vWorldPos[0] = vWorld[0]; vWorldPos[1] = vWorld[1]; vWorldPos[2] = vWorld[2];
424 if(m_osgNode.valid())
426 osg::NodePathList paths = m_osgNode->getParentalNodePaths();
427 osg::Matrix worldToLocal = osg::computeWorldToLocal(paths.at(0));
428 vLocal = vWorldPos * worldToLocal;
431 vLocalPos[0] = vLocal[0]; vLocalPos[1] = vLocal[1]; vLocalPos[2] = vLocal[2];
434 osg::MatrixTransform* VsMovableItem::GetMatrixTransform()
436 return m_osgMT.get();
459 return m_osgMT.get();
462 void VsMovableItem::UpdatePositionAndRotationFromMatrix()
464 UpdatePositionAndRotationFromMatrix(m_osgMT->getMatrix());
467 void VsMovableItem::UpdatePositionAndRotationFromMatrix(osg::Matrix osgMT)
473 osg::Vec3 vL = osgMT.getTrans();
474 CStdFPoint vLocal(vL.x(), vL.y(), vL.z());
475 vLocal.ClearNearZero();
476 m_lpThisMI->Position(vLocal,
false,
true,
false);
480 VxOSG::copyOsgMatrix_to_VxReal44(osgMT, vxTM);
481 Vx::VxTransform vTrans(vxTM);
483 vTrans.getRotationEulerAngles(vEuler);
484 CStdFPoint vRot(vEuler[0], vEuler[1] ,vEuler[2]);
485 vRot.ClearNearZero();
486 m_lpThisMI->Rotation(vRot,
true,
false);
488 if(m_osgDragger.valid())
489 m_osgDragger->SetupMatrix();
492 osg::Matrix osgTest = SetupMatrix(vLocal, vRot);
493 if(!OsgMatricesEqual(osgTest, m_osgLocalMatrix))
494 THROW_ERROR(Vs_Err_lUpdateMatricesDoNotMatch, Vs_Err_strUpdateMatricesDoNotMatch);
497 void VsMovableItem::Physics_UpdateMatrix()
501 LocalMatrix(SetupMatrix(m_lpThisMI->Position(), m_lpThisMI->Rotation()));
502 m_osgMT->setMatrix(m_osgLocalMatrix);
504 if(m_osgDragger.valid())
505 m_osgDragger->SetupMatrix();
507 Physics_UpdateAbsolutePosition();
511 void VsMovableItem::Physics_UpdateAbsolutePosition()
514 CStdFPoint vPos = VsMovableItem::GetOSGWorldCoords();
515 vPos.ClearNearZero();
516 m_lpThisMI->AbsolutePosition(vPos.x, vPos.y, vPos.z);
519 void VsMovableItem::BuildLocalMatrix()
522 BuildLocalMatrix(m_lpThisMI->Position(), m_lpThisMI->Rotation(), m_lpThisAB->Name());
525 void VsMovableItem::BuildLocalMatrix(CStdFPoint localPos, CStdFPoint localRot, std::string strName)
529 m_osgMT =
new osgManipulator::Selection;
530 m_osgMT->setName(strName +
"_MT");
533 if(!m_osgRoot.valid())
535 m_osgRoot =
new osg::Group;
536 m_osgRoot->setName(strName +
"_Root");
539 if(!m_osgRoot->containsNode(m_osgMT.get()))
540 m_osgRoot->addChild(m_osgMT.get());
542 LocalMatrix(SetupMatrix(localPos, localRot));
545 m_osgMT->setMatrix(m_osgLocalMatrix);
549 if(!m_osgNodeGroup.valid() && m_osgNode.valid())
551 m_osgNodeGroup =
new osg::Group();
552 m_osgNodeGroup->addChild(m_osgNode.get());
553 m_osgNodeGroup->setName(strName +
"_NodeGroup");
555 m_osgMT->addChild(m_osgNodeGroup.get());
557 CreateSelectedGraphics(strName);
561 void VsMovableItem::Physics_LoadLocalTransformMatrix(CStdXml &oXml)
563 osg::Matrix osgMT = LoadMatrix(oXml,
"LocalMatrix");
564 UpdatePositionAndRotationFromMatrix(osgMT);
567 void VsMovableItem::Physics_SaveLocalTransformMatrix(CStdXml &oXml)
569 SaveMatrix(oXml,
"LocalMatrix", m_osgMT->getMatrix());
572 std::string VsMovableItem::Physics_GetLocalTransformMatrixString()
575 return SaveMatrixString(m_osgMT->getMatrix());
577 return SaveMatrixString(osg::Matrixf::identity());
580 void VsMovableItem::Physics_ResizeDragHandler(
float fltRadius)
582 bool bInScene =
false;
583 if(m_osgDragger.valid() && m_osgDragger->IsInScene())
585 m_osgDragger->RemoveFromScene();
589 CreateDragger(m_lpThisAB->Name());
592 m_osgDragger->AddToScene();
595 void VsMovableItem::Physics_ResetGraphicsAndPhysics()
602 void VsMovableItem::Physics_PositionChanged()
604 Physics_UpdateMatrix();
607 void VsMovableItem::Physics_RotationChanged()
609 Physics_UpdateMatrix();
612 BoundingBox VsMovableItem::Physics_GetBoundingBox()
617 osg::Geode *osgGroup =
dynamic_cast<osg::Geode *
>(m_osgNode.get());
620 bb = osgGroup->getBoundingBox();
621 abb.Set(bb.xMin(), bb.yMin(), bb.zMin(), bb.xMax(), bb.yMax(), bb.zMax());
623 else if(m_osgNode.valid())
625 osg::BoundingSphere osgBound = m_osgNode->getBound();
626 abb.Set(-osgBound.radius(), -osgBound.radius(), -osgBound.radius(), osgBound.radius(), osgBound.radius(), osgBound.radius());
630 abb.Set(-0.5, -0.5, -0.5, 0.5, 0.5, 0.5);
636 float VsMovableItem::Physics_GetBoundingRadius()
638 BoundingBox bb = Physics_GetBoundingBox();
639 return bb.MaxDimension();
650 void VsMovableItem::SetTexture(std::string strTexture)
652 if(m_osgNode.valid())
656 std::string strFile = AnimatSim::GetFilePath(m_lpThisAB->GetSimulator()->ProjectPath(), strTexture);
657 osg::ref_ptr<osg::Image> image = osgDB::readImageFile(strFile);
659 THROW_PARAM_ERROR(Vs_Err_lTextureLoad, Vs_Err_strTextureLoad,
"Image File", strFile);
661 osg::StateSet* state = m_osgNode->getOrCreateStateSet();
662 m_osgTexture =
new osg::Texture2D(image.get());
663 m_osgTexture->setDataVariance(osg::Object::DYNAMIC);
665 m_osgTexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
666 m_osgTexture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
668 state->setTextureAttributeAndModes(0, m_osgTexture.get());
669 state->setTextureMode(0, m_eTextureMode, osg::StateAttribute::ON);
670 state->setMode(GL_BLEND,osg::StateAttribute::ON);
674 else if(m_osgTexture.valid())
676 m_osgTexture.release();
677 osg::StateSet* state = m_osgNode->getOrCreateStateSet();
678 state->setTextureAttributeAndModes(0, NULL);
679 state->setTextureMode(0, m_eTextureMode, osg::StateAttribute::OFF);
684 void VsMovableItem::Physics_CollectData()
687 Physics_UpdateAbsolutePosition();
693 void VsMovableItem::Physics_ResetSimulation()
700 Physics_UpdateAbsolutePosition();
701 m_lpThisMI->ReportRotation(m_lpThisMI->Rotation());
706 void VsMovableItem::SetCulling()
712 if(!m_osgCull.valid())
714 m_osgCull =
new osg::CullFace();
715 m_osgCull->setMode(osg::CullFace::BACK);
717 osg::StateSet* ss = m_osgMT->getOrCreateStateSet();
718 ss->setAttributeAndModes(m_osgCull.get(), osg::StateAttribute::ON);
720 else if(m_osgCull.valid())
722 osg::StateSet* ss = m_osgMT->getOrCreateStateSet();
723 ss->setAttributeAndModes(m_osgCull.get(), osg::StateAttribute::OFF);
728 void VsMovableItem::ShowSelectedVertex() {}
730 void VsMovableItem::HideSelectedVertex() {}
732 void VsMovableItem::SetAlpha()
734 switch (m_lpThisAB->GetSimulator()->VisualSelectionMode())
736 case GRAPHICS_SELECTION_MODE:
737 m_lpThisMI->Alpha(m_lpThisMI->GraphicsAlpha());
738 HideSelectedVertex();
741 case COLLISION_SELECTION_MODE:
742 m_lpThisMI->Alpha(m_lpThisMI->CollisionsAlpha());
743 HideSelectedVertex();
746 case JOINT_SELECTION_MODE:
747 m_lpThisMI->Alpha(m_lpThisMI->JointsAlpha());
748 HideSelectedVertex();
751 case RECEPTIVE_FIELD_SELECTION_MODE:
752 m_lpThisMI->Alpha(m_lpThisMI->ReceptiveFieldsAlpha());
753 ShowSelectedVertex();
756 case SIMULATION_SELECTION_MODE:
757 m_lpThisMI->Alpha(m_lpThisMI->SimulationAlpha());
758 HideSelectedVertex();
762 m_lpThisMI->Alpha(m_lpThisMI->GraphicsAlpha());
763 HideSelectedVertex();
767 if(m_osgMaterial.valid() && m_osgStateSet.valid())
768 SetMaterialAlpha(m_osgMaterial.get(), m_osgStateSet.get(), m_lpThisMI->Alpha());
771 void VsMovableItem::SetMaterialAlpha(osg::Material *osgMat, osg::StateSet *ss,
float fltAlpha)
773 osgMat->setAlpha(osg::Material::FRONT_AND_BACK, fltAlpha);
776 ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
778 ss->setRenderingHint(osg::StateSet::OPAQUE_BIN);
781 void VsMovableItem::SetColor(CStdColor &vAmbient, CStdColor &vDiffuse, CStdColor &vSpecular,
float fltShininess)
783 if(m_osgNode.valid())
788 m_osgMaterial =
new osg::Material();
791 m_osgStateSet = m_osgNode->getOrCreateStateSet();
794 m_osgMaterial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(vAmbient[0], vAmbient[1], vAmbient[2], 1));
795 m_osgMaterial->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(vDiffuse[0], vDiffuse[1], vDiffuse[2], vDiffuse[3]));
796 m_osgMaterial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(vSpecular[0], vSpecular[1], vSpecular[2], 1));
797 m_osgMaterial->setShininess(osg::Material::FRONT_AND_BACK, fltShininess);
798 m_osgStateSet->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
802 m_osgStateSet->setAttribute(m_osgMaterial.get(), osg::StateAttribute::ON);
806 void VsMovableItem::SetVisible(osg::Node *osgNode,
bool bVisible)
811 osgNode->setNodeMask(0x1);
813 osgNode->setNodeMask(0x0);
817 void VsMovableItem::SetVisible(
bool bVisible)
819 SetVisible(m_osgNode.get(), bVisible);
822 void VsMovableItem::CreateItem()
824 m_lpThisAB->Initialize();
829 void VsMovableItem::EndGripDrag()
831 this->UpdatePositionAndRotationFromMatrix();
907 void VsMovableItem::Physics_OrientNewPart(
float fltXPos,
float fltYPos,
float fltZPos,
float fltXNorm,
float fltYNorm,
float fltZNorm)
910 if(!m_lpThisMI || !m_lpThisMI->Parent())
913 CStdFPoint vParentPos = m_lpThisMI->Parent()->AbsolutePosition();
914 osg::Vec3 vParent(vParentPos.x, vParentPos.y, vParentPos.z);
916 osg::Vec3 vClickPos(fltXPos, fltYPos, fltZPos), vClickNormal(fltXNorm, fltYNorm, fltZNorm);
919 float fltRadius = Physics_GetBoundingRadius();
922 osg::Vec3 vWorldPos = vClickPos + (vClickNormal*fltRadius);
925 osg::Vec3 vLocalPos = vWorldPos - vParent;
928 m_lpThisMI->Position(vLocalPos[0], vLocalPos[1], vLocalPos[2],
false,
true,
true);
float m_fltNullReport
This is used to report back 0 from GetDataPointer for items that are not supported in vortex...
virtual osg::MatrixTransform * GetCameraMatrixTransform()
Gets the matrix transform used by the camera for the mouse spring.
Declares the vortex organism class.
osg::ref_ptr< osg::MatrixTransform > m_osgGeometryRotationMT
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
Declares the vortex Light class.
Declares the vortex structure class.