Skip to content

Commit 51b16a8

Browse files
committed
Added a LookAt-method for entities and also a method to get the pose of an entity.
1 parent 3830f18 commit 51b16a8

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

modules/ovis/include/opencv2/ovis.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ class CV_EXPORTS_W WindowScene {
165165
CV_WRAP virtual void setEntityPose(const String& name, InputArray tvec = noArray(),
166166
InputArray rot = noArray(), bool invert = false) = 0;
167167

168+
/**
169+
* Retrieves the current pose of an entity
170+
* @param name entity name
171+
* @param R 3x3 rotation matrix
172+
* @param tvec translation vector
173+
* @param invert return the inverted pose
174+
*/
175+
CV_WRAP virtual void getEntityPose(const String& name, OutputArray R = noArray(), OutputArray tvec = noArray(),
176+
bool invert = false) = 0;
177+
168178
/**
169179
* get a list of available entity animations
170180
* @param name entity name
@@ -236,6 +246,15 @@ class CV_EXPORTS_W WindowScene {
236246
*/
237247
CV_WRAP virtual void setCameraLookAt(const String& target, InputArray offset = noArray()) = 0;
238248

249+
/**
250+
* convenience method to orient an entity to a specific entity.
251+
* If target is an empty string the entity looks at the given offset point
252+
* @param origin entity to make look at
253+
* @param target name of target entity
254+
* @param offset offset from entity centre
255+
*/
256+
CV_WRAP virtual void setEntityLookAt(const String& origin, const String& target, InputArray offset = noArray()) = 0;
257+
239258
/**
240259
* Retrieves the current camera pose
241260
* @param R 3x3 rotation matrix

modules/ovis/src/ovis.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,36 @@ class WindowSceneImpl : public WindowScene
578578
node.setPosition(t);
579579
}
580580

581+
void getEntityPose(const String& name ,OutputArray R, OutputArray tvec, bool invert) CV_OVERRIDE
582+
{
583+
SceneNode* node = sceneMgr->getEntity(name)->getParentSceneNode();
584+
Matrix3 _R;
585+
// toOGRE.Inverse() == toOGRE
586+
(node->getOrientation()*toOGRE).ToRotationMatrix(_R);
587+
588+
if (invert)
589+
{
590+
_R = _R.Transpose();
591+
}
592+
593+
if (tvec.needed())
594+
{
595+
Vector3 _tvec = node->getPosition();
596+
597+
if (invert)
598+
{
599+
_tvec = _R * -_tvec;
600+
}
601+
602+
Mat_<Real>(3, 1, _tvec.ptr()).copyTo(tvec);
603+
}
604+
605+
if (R.needed())
606+
{
607+
Mat_<Real>(3, 3, _R[0]).copyTo(R);
608+
}
609+
}
610+
581611
void getEntityAnimations(const String& name, std::vector<String>& out) CV_OVERRIDE
582612
{
583613
SceneNode& node = _getSceneNode(sceneMgr, name);
@@ -659,7 +689,7 @@ class WindowSceneImpl : public WindowScene
659689
Entity* ent = dynamic_cast<Entity*>(node.getAttachedObject(name));
660690
CV_Assert(ent && "invalid entity");
661691

662-
ent->getSkeleton()->setBlendMode(static_cast<Ogre::SkeletonAnimationBlendMode>(value[0]));
692+
ent->getSkeleton()->setBlendMode(static_cast<Ogre::SkeletonAnimationBlendMode>(int(value[0])));
663693
break;
664694
}
665695
default:
@@ -847,6 +877,26 @@ class WindowSceneImpl : public WindowScene
847877

848878
camNode->lookAt(tgt->_getDerivedPosition() + _offset, Ogre::Node::TS_WORLD);
849879
}
880+
881+
void setEntityLookAt(const String& origin, const String& target, InputArray offset) CV_OVERRIDE
882+
{
883+
SceneNode* orig = sceneMgr->getEntity(origin)->getParentSceneNode();
884+
885+
Vector3 _offset = Vector3::ZERO;
886+
887+
if (!offset.empty())
888+
{
889+
offset.copyTo(Mat_<Real>(3, 1, _offset.ptr()));
890+
}
891+
892+
if(target.compare("") != 0){
893+
SceneNode* tgt = sceneMgr->getEntity(target)->getParentSceneNode();
894+
orig->lookAt(tgt->_getDerivedPosition() + _offset, Ogre::Node::TS_WORLD, Ogre::Vector3::UNIT_Z);
895+
}else{
896+
orig->lookAt(_offset, Ogre::Node::TS_WORLD, Ogre::Vector3::UNIT_Z);
897+
}
898+
}
899+
850900
};
851901

852902
CV_EXPORTS_W void addResourceLocation(const String& path)

0 commit comments

Comments
 (0)