]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/placement.cxx
Zap SGLocation.
[simgear.git] / simgear / scene / model / placement.cxx
1 // placement.cxx - manage the placment of a 3D model.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #ifdef HAVE_CONFIG_H
7 #include <simgear_config.h>
8 #endif
9
10 #include <simgear/compiler.h>
11
12 #include <simgear/scene/util/SGSceneUserData.hxx>
13
14 #include "placementtrans.hxx"
15
16 #include "placement.hxx"
17
18
19 \f
20 ////////////////////////////////////////////////////////////////////////
21 // Implementation of SGModelPlacement.
22 ////////////////////////////////////////////////////////////////////////
23
24 SGModelPlacement::SGModelPlacement () :
25     _position(SGGeod::fromRad(0, 0)),
26     _roll_deg(0),
27     _pitch_deg(0),
28     _heading_deg(0),
29     _selector(new osg::Switch),
30     _transform(new SGPlacementTransform)
31 {
32 }
33
34 SGModelPlacement::~SGModelPlacement ()
35 {
36 }
37
38 void
39 SGModelPlacement::init( osg::Node * model )
40 {
41   if (model != 0) {
42       _transform->addChild(model);
43   }
44   _selector->addChild(_transform.get());
45   _selector->setValue(0, 1);
46 }
47
48 void
49 SGModelPlacement::update()
50 {
51   // The cartesian position
52   SGVec3d position = SGVec3d::fromGeod(_position);
53
54   // The orientation, composed from the horizontal local orientation and the
55   // orientation wrt the horizontal local frame
56   SGQuatd orient = SGQuatd::fromLonLat(_position);
57   orient *= SGQuatd::fromAngleAxisDeg(180, SGVec3d(0, 1, 0));
58   orient *= SGQuatd::fromYawPitchRollDeg(-_heading_deg, _pitch_deg, -_roll_deg);
59   SGMatrixd rotation(inverse(orient));
60   _transform->setTransform(position, rotation);
61 }
62
63 bool
64 SGModelPlacement::getVisible () const
65 {
66   return _selector->getValue(0);
67 }
68
69 void
70 SGModelPlacement::setVisible (bool visible)
71 {
72   _selector->setValue(0, visible);
73 }
74
75 void
76 SGModelPlacement::setLongitudeDeg (double lon_deg)
77 {
78   _position.setLongitudeDeg(lon_deg);
79 }
80
81 void
82 SGModelPlacement::setLatitudeDeg (double lat_deg)
83 {
84   _position.setLatitudeDeg(lat_deg);
85 }
86
87 void
88 SGModelPlacement::setElevationFt (double elev_ft)
89 {
90   _position.setElevationFt(elev_ft);
91 }
92
93 void
94 SGModelPlacement::setPosition (double lon_deg, double lat_deg, double elev_ft)
95 {
96   _position = SGGeod::fromDegFt(lon_deg, lat_deg, elev_ft);
97 }
98
99 void
100 SGModelPlacement::setPosition(const SGGeod& position)
101 {
102   _position = position;
103 }
104
105 void
106 SGModelPlacement::setRollDeg (double roll_deg)
107 {
108   _roll_deg = roll_deg;
109 }
110
111 void
112 SGModelPlacement::setPitchDeg (double pitch_deg)
113 {
114   _pitch_deg = pitch_deg;
115 }
116
117 void
118 SGModelPlacement::setHeadingDeg (double heading_deg)
119 {
120   _heading_deg = heading_deg;
121 }
122
123 void
124 SGModelPlacement::setOrientation (double roll_deg, double pitch_deg,
125                                   double heading_deg)
126 {
127   _roll_deg = roll_deg;
128   _pitch_deg = pitch_deg;
129   _heading_deg = heading_deg;
130 }
131
132 void
133 SGModelPlacement::setOrientation (const SGQuatd& orientation)
134 {
135   orientation.getEulerDeg(_heading_deg, _pitch_deg, _roll_deg);
136 }
137
138 void
139 SGModelPlacement::setBodyLinearVelocity(const SGVec3d& linear)
140 {
141   SGSceneUserData* userData;
142   userData = SGSceneUserData::getOrCreateSceneUserData(_transform);
143   SGSceneUserData::Velocity* vel = userData->getOrCreateVelocity();
144   SGQuatd orientation = SGQuatd::fromAngleAxisDeg(180, SGVec3d(0, 1, 0));
145   vel->linear = orientation.backTransform(linear);
146 }
147
148 void
149 SGModelPlacement::setBodyAngularVelocity(const SGVec3d& angular)
150 {
151   SGSceneUserData* userData;
152   userData = SGSceneUserData::getOrCreateSceneUserData(_transform);
153   SGSceneUserData::Velocity* vel = userData->getOrCreateVelocity();
154   SGQuatd orientation = SGQuatd::fromAngleAxisDeg(180, SGVec3d(0, 1, 0));
155   vel->angular = orientation.backTransform(angular);
156 }
157
158 // end of model.cxx