Skip to content

Commit e1ecf77

Browse files
committed
ovis: correct and simplify coordinate system conversions
by keeping Camera oriented in OpenCV conventions instead of doing the conversion for each SceneNode.
1 parent bae6838 commit e1ecf77

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

modules/ovis/src/ovis.cpp

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ static const char* RENDERSYSTEM_NAME = "OpenGL 3+ Rendering Subsystem";
2323
static std::vector<String> _extraResourceLocations;
2424

2525
// convert from OpenCV to Ogre coordinates:
26-
// rotation by 180° around x axis
27-
static Matrix3 toOGRE = Matrix3(1, 0, 0, 0, -1, 0, 0, 0, -1);
26+
static Quaternion toOGRE(Degree(180), Vector3::UNIT_X);
2827
static Vector2 toOGRE_SS = Vector2(1, -1);
2928

3029
WindowScene::~WindowScene() {}
@@ -48,15 +47,12 @@ void _createTexture(const String& name, Mat image)
4847
texMgr.loadImage(name, RESOURCEGROUP_NAME, im);
4948
}
5049

51-
static void _convertRT(InputArray rot, InputArray tvec, Quaternion& q, Vector3& t,
52-
bool invert = false, bool init = false)
50+
static void _convertRT(InputArray rot, InputArray tvec, Quaternion& q, Vector3& t, bool invert = false)
5351
{
5452
CV_Assert(rot.empty() || rot.rows() == 3 || rot.size() == Size(3, 3),
5553
tvec.empty() || tvec.rows() == 3);
5654

57-
// make sure the entity is oriented by the OpenCV coordinate conventions
58-
// when initialised
59-
q = init ? Quaternion(toOGRE) : Quaternion::IDENTITY;
55+
q = Quaternion::IDENTITY;
6056
t = Vector3::ZERO;
6157

6258
if (!rot.empty())
@@ -74,7 +70,7 @@ static void _convertRT(InputArray rot, InputArray tvec, Quaternion& q, Vector3&
7470

7571
Matrix3 R;
7672
_R.copyTo(Mat_<Real>(3, 3, R[0]));
77-
q = Quaternion(toOGRE * R);
73+
q = Quaternion(R);
7874

7975
if (invert)
8076
{
@@ -85,7 +81,6 @@ static void _convertRT(InputArray rot, InputArray tvec, Quaternion& q, Vector3&
8581
if (!tvec.empty())
8682
{
8783
tvec.copyTo(Mat_<Real>(3, 1, t.ptr()));
88-
t = toOGRE * t;
8984

9085
if(invert)
9186
{
@@ -281,6 +276,7 @@ class WindowSceneImpl : public WindowScene
281276
cam->setNearClipDistance(0.5);
282277
cam->setAutoAspectRatio(true);
283278
camNode = sceneMgr->getRootSceneNode()->createChildSceneNode();
279+
camNode->setOrientation(toOGRE);
284280
camNode->attachObject(cam);
285281

286282
if (flags & SCENE_INTERACTIVE)
@@ -356,7 +352,7 @@ class WindowSceneImpl : public WindowScene
356352

357353
Quaternion q;
358354
Vector3 t;
359-
_convertRT(rot, tvec, q, t, false, true);
355+
_convertRT(rot, tvec, q, t);
360356
SceneNode* node = sceneMgr->getRootSceneNode()->createChildSceneNode(t, q);
361357
node->attachObject(ent);
362358
}
@@ -387,7 +383,7 @@ class WindowSceneImpl : public WindowScene
387383

388384
Quaternion q;
389385
Vector3 t;
390-
_convertRT(rot, tvec, q, t, false, true);
386+
_convertRT(rot, tvec, q, t);
391387
SceneNode* node = sceneMgr->getRootSceneNode()->createChildSceneNode(t, q);
392388
node->attachObject(cam);
393389

@@ -410,7 +406,7 @@ class WindowSceneImpl : public WindowScene
410406

411407
Quaternion q;
412408
Vector3 t;
413-
_convertRT(rot, tvec, q, t, false, true);
409+
_convertRT(rot, tvec, q, t);
414410
SceneNode* node = sceneMgr->getRootSceneNode()->createChildSceneNode(t, q);
415411
node->attachObject(light);
416412
}
@@ -430,7 +426,7 @@ class WindowSceneImpl : public WindowScene
430426
SceneNode& node = _getSceneNode(sceneMgr, name);
431427
Quaternion q;
432428
Vector3 t;
433-
_convertRT(rot, tvec, q, t, invert, true);
429+
_convertRT(rot, tvec, q, t, invert);
434430
node.setOrientation(q);
435431
node.setPosition(t);
436432
}
@@ -546,33 +542,27 @@ class WindowSceneImpl : public WindowScene
546542
up = toOGRE * up;
547543
}
548544

549-
Camera* cam = sceneMgr->getCamera(title);
550-
cam->getParentSceneNode()->setFixedYawAxis(useFixed, up);
545+
camNode->setFixedYawAxis(useFixed, up);
551546
}
552547

553548
void setCameraPose(InputArray tvec, InputArray rot, bool invert)
554549
{
555-
Camera* cam = sceneMgr->getCamera(title);
556-
557-
SceneNode* node = cam->getParentSceneNode();
558550
Quaternion q;
559551
Vector3 t;
560-
_convertRT(rot, tvec, q, t, invert, true);
552+
_convertRT(rot, tvec, q, t, invert);
561553

562554
if (!rot.empty())
563-
node->setOrientation(q);
555+
camNode->setOrientation(q*toOGRE);
564556

565557
if (!tvec.empty())
566-
node->setPosition(t);
558+
camNode->setPosition(t);
567559
}
568560

569561
void getCameraPose(OutputArray R, OutputArray tvec, bool invert)
570562
{
571-
Camera* cam = sceneMgr->getCamera(title);
572-
SceneNode* node = cam->getParentSceneNode();
573-
574563
Matrix3 _R;
575-
node->getOrientation().ToRotationMatrix(_R);
564+
// toOGRE.Inverse() == toOGRE
565+
(camNode->getOrientation()*toOGRE).ToRotationMatrix(_R);
576566

577567
if (invert)
578568
{
@@ -581,20 +571,18 @@ class WindowSceneImpl : public WindowScene
581571

582572
if (tvec.needed())
583573
{
584-
Vector3 _tvec = node->getPosition();
574+
Vector3 _tvec = camNode->getPosition();
585575

586576
if (invert)
587577
{
588578
_tvec = _R * -_tvec;
589579
}
590580

591-
_tvec = toOGRE.Transpose() * _tvec;
592581
Mat_<Real>(3, 1, _tvec.ptr()).copyTo(tvec);
593582
}
594583

595584
if (R.needed())
596585
{
597-
_R = toOGRE.Transpose() * _R;
598586
Mat_<Real>(3, 3, _R[0]).copyTo(R);
599587
}
600588
}
@@ -607,18 +595,16 @@ class WindowSceneImpl : public WindowScene
607595

608596
void setCameraLookAt(const String& target, InputArray offset)
609597
{
610-
SceneNode* cam = sceneMgr->getCamera(title)->getParentSceneNode();
611598
SceneNode* tgt = sceneMgr->getEntity(target)->getParentSceneNode();
612599

613600
Vector3 _offset = Vector3::ZERO;
614601

615602
if (!offset.empty())
616603
{
617604
offset.copyTo(Mat_<Real>(3, 1, _offset.ptr()));
618-
_offset = toOGRE * _offset;
619605
}
620606

621-
cam->lookAt(tgt->_getDerivedPosition() + _offset, Ogre::Node::TS_WORLD);
607+
camNode->lookAt(tgt->_getDerivedPosition() + _offset, Ogre::Node::TS_WORLD);
622608
}
623609
};
624610

0 commit comments

Comments
 (0)