]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGTranslateTransform.cxx
Merge branch 'timoore/aptsign' into next
[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 SGTranslateTransform::SGTranslateTransform() :
33   _axis(0, 0, 0),
34   _value(0)
35 {
36   setReferenceFrame(RELATIVE_RF);
37 }
38
39 SGTranslateTransform::SGTranslateTransform(const SGTranslateTransform& trans,
40                                            const osg::CopyOp& copyop) :
41   osg::Transform(trans, copyop),
42   _axis(trans._axis),
43   _value(trans._value)
44 {
45 }
46
47 bool
48 SGTranslateTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
49                                                 osg::NodeVisitor* nv) const 
50 {
51   if (_referenceFrame == RELATIVE_RF) {
52     matrix.preMultTranslate(toOsg(_value*_axis));
53   } else {
54     matrix.setTrans(toOsg(_value*_axis));
55   }
56   return true;
57 }
58
59 bool
60 SGTranslateTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
61                                                 osg::NodeVisitor* nv) const
62 {
63   if (_referenceFrame == RELATIVE_RF) {
64     matrix.postMultTranslate(toOsg(-_value*_axis));
65   } else {
66     matrix.setTrans(toOsg(-_value*_axis));
67   }
68   return true;
69 }
70
71 osg::BoundingSphere
72 SGTranslateTransform::computeBound() const
73 {
74   osg::BoundingSphere bs = osg::Group::computeBound();
75   bs._center += toOsg(_axis)*_value;
76   return bs;
77 }
78
79 namespace {
80
81 bool TranslateTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
82 {
83     SGTranslateTransform& trans = static_cast<SGTranslateTransform&>(obj);
84
85     if (fr[0].matchWord("axis")) {
86         ++fr;
87         osg::Vec3d axis;
88         if (fr.readSequence(axis))
89             fr += 3;
90         else
91             return false;
92         trans.setAxis(toSG(axis));
93     }
94     if (fr[0].matchWord("value")) {
95         ++fr;
96         double value;
97         if (fr[0].getFloat(value))
98             ++fr;
99         else
100             return false;
101         trans.setValue(value);
102     }
103     return true;
104 }
105
106 bool TranslateTransform_writeLocalData(const osg::Object& obj,
107                                        osgDB::Output& fw)
108 {
109     const SGTranslateTransform& trans
110         = static_cast<const SGTranslateTransform&>(obj);
111     const SGVec3d& axis = trans.getAxis();
112     double value = trans.getValue();
113     
114     fw.indent() << "axis ";
115     for (int i = 0; i < 3; i++)
116         fw << axis(i) << " ";
117     fw << std::endl;
118     fw.indent() << "value " << value << std::endl;
119     return true;
120 }
121
122 }
123
124 osgDB::RegisterDotOsgWrapperProxy g_SGTranslateTransformProxy
125 (
126     new SGTranslateTransform,
127     "SGTranslateTransform",
128     "Object Node Transform SGTranslateTransform Group",
129     &TranslateTransform_readLocalData,
130     &TranslateTransform_writeLocalData
131 );