]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGTranslateTransform.cxx
OSG Reader and Writer for BTG files
[simgear.git] / simgear / scene / model / SGTranslateTransform.cxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2006-2007 Mathias Froehlich 
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18  * MA 02110-1301, USA.
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include <simgear_config.h>
24 #endif
25
26 #include <osgDB/Registry>
27 #include <osgDB/Input>
28 #include <osgDB/Output>
29
30 #include "SGTranslateTransform.hxx"
31
32 static inline void
33 set_translation (osg::Matrix &matrix, double position_m, const SGVec3d &axis)
34 {
35   SGVec3d xyz = axis * position_m;
36   matrix.makeIdentity();
37   matrix(3, 0) = xyz[0];
38   matrix(3, 1) = xyz[1];
39   matrix(3, 2) = xyz[2];
40 }
41
42 SGTranslateTransform::SGTranslateTransform() :
43   _axis(0, 0, 0),
44   _value(0)
45 {
46   setReferenceFrame(RELATIVE_RF);
47 }
48
49 SGTranslateTransform::SGTranslateTransform(const SGTranslateTransform& trans,
50                                            const osg::CopyOp& copyop) :
51   osg::Transform(trans, copyop),
52   _axis(trans._axis),
53   _value(trans._value)
54 {
55 }
56
57 bool
58 SGTranslateTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
59                                                 osg::NodeVisitor* nv) const 
60 {
61   if (_referenceFrame == RELATIVE_RF) {
62     osg::Matrix tmp;
63     set_translation(tmp, _value, _axis);
64     matrix.preMult(tmp);
65   } else {
66     osg::Matrix tmp;
67     set_translation(tmp, _value, _axis);
68     matrix = tmp;
69   }
70   return true;
71 }
72
73 bool
74 SGTranslateTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
75                                                 osg::NodeVisitor* nv) const
76 {
77   if (_referenceFrame == RELATIVE_RF) {
78     osg::Matrix tmp;
79     set_translation(tmp, -_value, _axis);
80     matrix.postMult(tmp);
81   } else {
82     osg::Matrix tmp;
83     set_translation(tmp, -_value, _axis);
84     matrix = tmp;
85   }
86   return true;
87 }
88
89 osg::BoundingSphere
90 SGTranslateTransform::computeBound() const
91 {
92   osg::BoundingSphere bs = osg::Group::computeBound();
93   bs._center += _axis.osg()*_value;
94   return bs;
95 }
96
97 namespace {
98
99 bool TranslateTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
100 {
101     SGTranslateTransform& trans = static_cast<SGTranslateTransform&>(trans);
102
103     if (fr[0].matchWord("axis")) {
104         ++fr;
105         SGVec3d axis;
106         if (fr.readSequence(axis.osg()))
107             fr += 3;
108         else
109             return false;
110         trans.setAxis(axis);
111     }
112     if (fr[0].matchWord("value")) {
113         ++fr;
114         double value;
115         if (fr[0].getFloat(value))
116             ++fr;
117         else
118             return false;
119         trans.setValue(value);
120     }
121     return true;
122 }
123
124 bool TranslateTransform_writeLocalData(const osg::Object& obj,
125                                        osgDB::Output& fw)
126 {
127     const SGTranslateTransform& trans
128         = static_cast<const SGTranslateTransform&>(obj);
129     const SGVec3d& axis = trans.getAxis();
130     double value = trans.getValue();
131     
132     fw.indent() << "axis ";
133     for (int i = 0; i < 3; i++)
134         fw << axis(i) << " ";
135     fw << std::endl;
136     fw.indent() << "value " << value << std::endl;
137     return true;
138 }
139
140 }
141
142 osgDB::RegisterDotOsgWrapperProxy g_SGTranslateTransformProxy
143 (
144     new SGTranslateTransform,
145     "SGTranslateTransform",
146     "Object Node Transform SGTranslateTransform Group",
147     &TranslateTransform_readLocalData,
148     &TranslateTransform_writeLocalData
149 );