Developer Documentation
CoordinateSystemNode.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 
43 
44 
45 
46 
47 //=============================================================================
48 //
49 // CLASS CoordsysNode - IMPLEMENTATION
50 //
51 //=============================================================================
52 
53 //== INCLUDES =================================================================
54 
55 #include <ObjectTypes/Coordsys/CoordinateSystemNode.hh>
56 
57 
58 
59 //== NAMESPACES ===============================================================
60 
61 namespace ACG {
62 namespace SceneGraph {
63 
64 
65 //== IMPLEMENTATION ==========================================================
66 
67 
69  std::string _name)
70  : BaseNode(_parent, _name),
71  coordsysSize_(1.0),
72  slices_(10),
73  stacks_(10),
74  loops_(10)
75 {
77  sphere_ = new GLSphere(slices_, stacks_);
78  cone_ = new GLCone(slices_, stacks_, 1.0f, 1.0f, false, true);
79  cylinder_ = new GLCylinder(slices_, stacks_, 1.0f, false, false);
80  disk_ = new GLDisk(slices_, loops_, 0.1f, 1.0f);
81 }
82 
83 //----------------------------------------------------------------------------
84 
86  if (sphere_)
87  delete sphere_;
88 
89  if (cone_)
90  delete cone_;
91 
92  if (cylinder_)
93  delete cylinder_;
94 
95  if (disk_)
96  delete disk_;
97 }
98 
99 //----------------------------------------------------------------------------
100 
101 void
103 boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
104 {
105  //TODO!!
106  Vec3d topLeft = position_ + coordsysSize_ * ACG::Vec3d(1.0,1.0,1.0);
107 
108  _bbMin.minimize( topLeft );
109  _bbMin.minimize( position_ );
110 
111  _bbMax.maximize( topLeft );
112  _bbMax.maximize( position_ );
113 
114 }
115 
116 
117 //----------------------------------------------------------------------------
118 
119 
123 {
125 }
126 
127 
128 //----------------------------------------------------------------------------
129 
130 void
131 CoordinateSystemNode::
132 drawCoordsys( GLState& _state) {
133 
134  double topRadius = 0.1 * coordsysSize_;
135  double arrowLength = 0.4 * coordsysSize_;
136  double bodyRadius = 0.04 * coordsysSize_;
137  double bodyLength = 0.6 * coordsysSize_;
138  double sphereRadius = 0.1 * coordsysSize_;
139 
140  // Origin
141  glColor3f(0.5, 0.5, 0.5);
142  sphere_->draw(_state, sphereRadius);
143 
144  // X-Axis
145  glColor3f(1.0, 0.0, 0.0);
146  _state.push_modelview_matrix ();
147  _state.rotate (-90, 0, 1, 0);
148  _state.translate ( 0, 0, -bodyLength );
149  cylinder_->setBottomRadius(bodyRadius);
150  cylinder_->setTopRadius(bodyRadius);
151  cylinder_->draw(_state, bodyLength);
152  disk_->setInnerRadius(0.0f);
153  disk_->setOuterRadius(topRadius);
154  disk_->draw(_state);
155  _state.translate ( 0, 0, -arrowLength );
156  cone_->setBottomRadius(0.0f);
157  cone_->setTopRadius(topRadius);
158  cone_->draw(_state, arrowLength);
159  _state.pop_modelview_matrix ();
160 
161  // Y-Axis
162  glColor3f(0.0, 1.0, 0.0);
163  _state.push_modelview_matrix ();
164  _state.rotate (90, 1, 0, 0);
165  _state.translate ( 0, 0, -bodyLength );
166  cylinder_->draw(_state, bodyLength);
167  disk_->draw(_state);
168  _state.translate ( 0, 0, -arrowLength );
169  cone_->draw(_state, arrowLength);
170  _state.pop_modelview_matrix ();
171 
172  // Z-Axis
173  glColor3f(0.0, 0.0, 1.0);
174  _state.push_modelview_matrix ();
175  _state.rotate (180, 0, 1, 0);
176  _state.translate ( 0, 0, -bodyLength );
177  cylinder_->draw(_state, bodyLength);
178  disk_->draw(_state);
179  _state.translate ( 0, 0, -arrowLength );
180  cone_->draw(_state, arrowLength);
181  _state.pop_modelview_matrix ();
182 }
183 
184 //============================================================================
185 
186 
187 void
188 CoordinateSystemNode::drawCoordsysPick( GLState& _state) {
189 
190  double topRadius = 0.1 * coordsysSize_;
191  double arrowLength = 0.4 * coordsysSize_;
192  double bodyRadius = 0.04 * coordsysSize_;
193  double bodyLength = 0.6 * coordsysSize_;
194  double sphereRadius = 0.1 * coordsysSize_;
195 
196  // Origin
197  _state.pick_set_name (1);
198  sphere_->draw(_state, sphereRadius);
199 
200  // X-Axis
201  _state.pick_set_name (2);
202  _state.push_modelview_matrix ();
203  _state.rotate (-90, 0, 1, 0);
204  _state.translate ( 0, 0, -bodyLength );
205  cylinder_->setBottomRadius(bodyRadius);
206  cylinder_->setTopRadius(bodyRadius);
207  cylinder_->draw(_state, bodyLength);
208  disk_->setInnerRadius(0.0f);
209  disk_->setOuterRadius(topRadius);
210  disk_->draw(_state);
211  _state.translate ( 0, 0, -arrowLength );
212  cone_->setBottomRadius(0.0f);
213  cone_->setTopRadius(topRadius);
214  cone_->draw(_state, arrowLength);
215  _state.pop_modelview_matrix ();
216 
217  // Y-Axis
218  _state.pick_set_name (3);
219  _state.push_modelview_matrix ();
220  _state.rotate (90, 1, 0, 0);
221  _state.translate ( 0, 0, -bodyLength );
222  cylinder_->draw(_state, bodyLength);
223  disk_->draw(_state);
224  _state.translate ( 0, 0, -arrowLength );
225  cone_->draw(_state, arrowLength);
226  _state.pop_modelview_matrix ();
227 
228  // Z-Axis
229  _state.pick_set_name (4);
230  _state.push_modelview_matrix ();
231  _state.rotate (180, 0, 1, 0);
232  _state.translate ( 0, 0, -bodyLength );
233  cylinder_->draw(_state, bodyLength);
234  disk_->draw(_state);
235  _state.translate ( 0, 0, -arrowLength );
236  cone_->draw(_state, arrowLength);
237  _state.pop_modelview_matrix ();
238 }
239 
240 
241 //============================================================================
242 
243 
244 void
246 draw(GLState& _state , const DrawModes::DrawMode& /*_drawMode*/)
247 {
248  glPushAttrib(GL_ENABLE_BIT);
249 
250  // Push Modelview-Matrix
251  _state.push_modelview_matrix();
252 
253  Vec4f lastBaseColor = _state.base_color();
254 
255  glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
256  ACG::GLState::enable(GL_COLOR_MATERIAL);
257  ACG::GLState::enable(GL_LIGHTING);
258  ACG::GLState::shadeModel(GL_SMOOTH);
259 
260  // Translate to right position
261  _state.translate(position_);
262 
263  // Apply rotation matrix
264  GLMatrixd modelview = _state.modelview();
265  modelview *= rotation_;
266  _state.set_modelview(modelview);
267 
268  // Draw coordsys
269  drawCoordsys(_state);
270 
271  glColor4fv(lastBaseColor.data());
272 
273  // Reload old configuration
274  _state.pop_modelview_matrix();
275 
276  glPopAttrib();
277 }
278 
279 
280 void
282 position(const Vec3d& _pos)
283 {
284  position_ = _pos;
285 }
286 
287 
288 Vec3d
291  return position_;
292 }
293 
295 {
296  rotation_ = _rotation;
297 }
298 
300 {
301  return rotation_;
302 }
303 
304 
305 void
307 size(const double _size) {
308  coordsysSize_ = _size;
309 }
310 
311 double
313 size() {
314  return coordsysSize_;
315 }
316 
317 
318 void
320 {
321 
322 
323  if (_target == PICK_ANYTHING) {
324 
325  _state.pick_set_maximum(5);
326  _state.pick_set_name(0);
327 
328  // Push Modelview-Matrix
329  _state.push_modelview_matrix();
330 
331  // Translate to right position
332  _state.translate(position_);
333 
334  // Apply rotation matrix
335  GLMatrixd modelview = _state.modelview();
336  modelview *= rotation_;
337  _state.set_modelview(modelview);
338 
339  // Koordinatensystem zeichnen
340  drawCoordsysPick(_state);
341 
342  // Reload old configuration
343  _state.pop_modelview_matrix();
344 
345  }
346 }
347 
348 
349 //=============================================================================
350 } // namespace SceneGraph
351 } // namespace ACG
352 //=============================================================================
void pop_modelview_matrix()
pop modelview matrix
Definition: GLState.cc:1026
CoordinateSystemNode(BaseNode *_parent=0, std::string _name="<TextNode>")
Namespace providing different geometric functions concerning angles.
const Vec4f & base_color() const
get base color (used when lighting is off)
Definition: GLState.hh:926
double coordsysSize_
Size of the coordsys.
void identity()
setup an identity matrix
Matrix4x4d rotation()
Get current rotation of the coordinate system;.
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: Vector11T.hh:568
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax) override
update bounding box
DrawMode SOLID_SMOOTH_SHADED
draw smooth shaded (Gouraud shaded) faces (requires halfedge normals)
Definition: DrawModes.cc:82
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode) override
draw Coordsys
Scalar * data()
access to Scalar array
Definition: Vector11T.hh:200
PickTarget
What target to use for picking.
Definition: PickTarget.hh:73
void push_modelview_matrix()
push modelview matrix
Definition: GLState.cc:1010
void set_modelview(const GLMatrixd &_m)
set modelview
Definition: GLState.hh:728
Vec3d position_
3d position of the coordsys origin
Vec3d position()
Get current position of the coordinate system;.
pick any of the prior targets (should be implemented for all nodes)
Definition: PickTarget.hh:84
ACG::SceneGraph::DrawModes::DrawMode availableDrawModes() const override
return available draw modes
bool pick_set_maximum(size_t _idx)
Set the maximal number of primitives/components of your object.
Definition: GLState.cc:1051
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: Vector11T.hh:540
void pick(GLState &_state, PickTarget _target) override
draw Coordsys for object picking
void rotate(double _angle, double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
rotate around axis (_x, _y, _z) by _angle
Definition: GLState.cc:564
void pick_set_name(size_t _idx)
sets the current name/color (like glLoadName(_idx))
Definition: GLState.cc:1061
const GLMatrixd & modelview() const
get modelview matrix
Definition: GLState.hh:791
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
Matrix4x4d rotation_
Orientation of coordsys.
static void shadeModel(GLenum _mode)
replaces glShadeModel, supports locking
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
Definition: GLState.cc:533
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:121