1 // placementtrans.hxx -- class for carrying transforms for placing models in the world
3 // Written by Mathias Froehlich, started April 2005.
5 // Copyright (C) 2005 Mathias Froehlich
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # include <simgear_config.h>
27 # error This library requires C++
30 #include <osgDB/Registry>
31 #include <osgDB/Input>
32 #include <osgDB/Output>
34 #include <simgear/compiler.h>
35 #include <simgear/constants.h>
37 #include "placementtrans.hxx"
39 SGPlacementTransform::SGPlacementTransform(void) :
40 _placement_offset(0, 0, 0),
41 _rotation(SGQuatd::unit())
45 SGPlacementTransform::SGPlacementTransform(const SGPlacementTransform& trans,
46 const osg::CopyOp& copyop):
47 osg::Transform(trans, copyop),
48 _placement_offset(trans._placement_offset),
49 _rotation(trans._rotation)
54 SGPlacementTransform::~SGPlacementTransform(void)
59 SGPlacementTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
60 osg::NodeVisitor*) const
62 if (_referenceFrame == RELATIVE_RF) {
63 matrix.preMultTranslate(_placement_offset.osg());
64 matrix.preMultRotate(_rotation.osg());
66 matrix.makeRotate(_rotation.osg());
67 matrix.postMultTranslate(_placement_offset.osg());
73 SGPlacementTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
74 osg::NodeVisitor*) const
76 if (_referenceFrame == RELATIVE_RF) {
77 matrix.postMultTranslate(-_placement_offset.osg());
78 matrix.postMultRotate(inverse(_rotation).osg());
80 matrix.makeRotate(inverse(_rotation).osg());
81 matrix.preMultTranslate(-_placement_offset.osg());
86 // Functions to read / write SGPlacementTrans from / to a .osg file,
87 // mostly for debugging purposes.
91 bool PlacementTrans_readLocalData(osg::Object& obj, osgDB::Input& fr)
93 SGPlacementTransform& trans = static_cast<SGPlacementTransform&>(obj);
94 SGQuatd rotation = SGQuatd::unit();
95 SGVec3d placementOffset(0, 0, 0);
97 if (fr[0].matchWord("rotation")) {
100 if (fr.readSequence(vec4)) {
101 rotation = SGQuatd(vec4[0], vec4[1], vec4[2], vec4[3]);
106 if (fr[0].matchWord("placement")) {
108 if (fr.readSequence(placementOffset.osg()))
113 trans.setTransform(placementOffset, rotation);
117 bool PlacementTrans_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
119 const SGPlacementTransform& trans
120 = static_cast<const SGPlacementTransform&>(obj);
121 const SGQuatd& rotation = trans.getRotation();
122 const SGVec3d& placement = trans.getGlobalPos();
124 fw.indent() << "rotation ";
125 for (int i = 0; i < 4; i++) {
126 fw << rotation(i) << " ";
129 int prec = fw.precision();
131 fw.indent() << "placement ";
132 for (int i = 0; i < 3; i++) {
133 fw << placement(i) << " ";
141 osgDB::RegisterDotOsgWrapperProxy g_SGPlacementTransProxy
143 new SGPlacementTransform,
144 "SGPlacementTransform",
145 "Object Node Transform SGPlacementTransform Group",
146 &PlacementTrans_readLocalData,
147 &PlacementTrans_writeLocalData