Developer Documentation
GridNode.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  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS GridNode - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 //== INCLUDES =================================================================
60 
61 
62 #include "GridNode.hh"
63 #include "SceneGraph.hh"
64 #include <cstdio>
65 
66 //== NAMESPACES ===============================================================
67 
68 
69 namespace ACG {
70 namespace SceneGraph {
71 
72 
73 //== IMPLEMENTATION ==========================================================
74 
75 
77 GridNode(BaseNode* _parent, const std::string& _name)
78  : MaterialNode(_parent, _name),
79  horizontalLines_(7),
80  verticalLines_(7),
81  maxRefinement_(49),
82  minRefinementDistance_(10),
83  gridSize_(10.0),
84  autoResize_(false),
85  orientation_(XZ_PLANE)
86 {
87  baseLineColor_ = Vec3f(0.5f, 0.5f, 0.5f);
88  midLineColor_ = Vec3f(0.3f, 0.3f, 0.3f);
89 }
90 
91 
92 //-----------------------------------------------------------------------------
93 
94 
97 {
98  return ( DrawModes::WIREFRAME |
100 }
101 
102 
103 //-----------------------------------------------------------------------------
104 
105 
106 void
108 {
109  // If we do not autoresize, we set our bounding box here
110  // otherwise we set the size of the grid according to the
111  // real scene geometry from the state
112 // if ( !autoResize_ ) {
113 
114  // Only return a bounding box, if something will be drawn
115  if ( orientation_ != NONE) {
116  bb_min_ = Vec3f(-0.5*gridSize_, 0.0, -0.5*gridSize_);
117  bb_max_ = Vec3f( 0.5*gridSize_, 0.0, 0.5*gridSize_);
118 
119  _bbMin.minimize(bb_min_);
120  _bbMax.maximize(bb_max_);
121  }
122 
123 }
124 
125 
126 //----------------------------------------------------------------
127 
128 void
129 GridNode::pick(GLState& /*_state*/, PickTarget /*_target*/)
130 {
131 
132 
133 }
134 
135 //-----------------------------------------------------------------------------
136 
137 
138 void
139 GridNode::draw(GLState& _state , const DrawModes::DrawMode& /* _drawMode */ )
140 {
141 
142  glPushAttrib( GL_LIGHTING_BIT ); // STACK_ATTRIBUTES <- LIGHTING_ATTRIBUTE
143  ACG::GLState::disable(GL_LIGHTING);
144 
145  glPushAttrib( GL_DEPTH_TEST );
146  ACG::GLState::enable( GL_DEPTH_TEST );
147 
148  glLineWidth(0.1f);
149 
150  ACG::Vec3d direction1,direction2;
151 
152  // For all orientations :
153  for ( unsigned int i = 0 ; i < 3 ; ++i ) {
154 
155  Vec3d viewDirection(0.0, 1.0, 0.0); // = _state.viewing_direction();
156  Vec3d eye = _state.eye();
157 
158  //first we calculate the hitpoint of the camera direction with the grid
159  GLMatrixd modelview = _state.modelview();
160 
161  // Distance from eye point to the plane
162  double distance = 0.0;
163 
164  if ( i == 0 && ((orientation_ ) & XZ_PLANE)) {
165  direction1 = ACG::Vec3f( 1.0 , 0.0 , 0.0 );
166  direction2 = ACG::Vec3f( 0.0 , 0.0 , 1.0 );
167  distance = fabs( eye | ACG::Vec3d(0.0,1.0,0.0 ) );
168  } else if ( i == 1 && (orientation_ & XY_PLANE)) {
169  direction1 = ACG::Vec3f( 1.0 , 0.0 , 0.0 );
170  direction2 = ACG::Vec3f( 0.0 , 1.0 , 0.0 );
171  distance = fabs( eye | ACG::Vec3d(0.0,0.0,1.0 ) );
172  } else if ( i == 2 && (orientation_ & YZ_PLANE)) {
173  direction1 = ACG::Vec3f( 0.0 , 1.0 , 0.0 );
174  direction2 = ACG::Vec3f( 0.0 , 0.0 , 1.0 );
175  distance = fabs( eye | ACG::Vec3d(1.0,0.0,0.0 ) );
176  } else {
177  continue;
178  }
179 
180  // Remember original Modelview Matrix
181  _state.push_modelview_matrix();
182 
183  // The factor is means the following
184  // If the viewer is farther away than the minRefinementDistance_, the factor is 0 and no refinement will take place
185  // If the viewer goes closer to the grid, the grid will be refined.
186  // Block negative distances (grid behind us)
187  int factor = floor( minRefinementDistance_ / distance) - 1;
188 
189  int vertical = verticalLines_;
190  int horizontal = horizontalLines_;
191 
192  if (factor > 20){
193  vertical = maxRefinement_;
194  horizontal = maxRefinement_;
195 
196  } else if (factor > 0){
197  // 'factor' times split the grid (split every cell into 4 new cells)
198  int rest = 0;
199 
200  for (int i=0; i < factor; i++)
201  rest -= floor( pow(double(2.0), i));
202 
203  vertical = vertical * floor( pow(double(2.0),factor)) + rest;
204  horizontal = horizontal * floor( pow(double(2.0),factor)) + rest;
205 
206  vertical = std::min(vertical, maxRefinement_ );
207  horizontal = std::min(vertical, maxRefinement_ );
208  }
209 
210 
211 
212 // if ( autoResize_ ) {
213 //
214 // // Get the current scene size from the glstate
215 // ACG::Vec3d bb_min,bb_max;
216 // _state.get_bounding_box(bb_min,bb_max);
217 //
218 // double radius = std::max( std::max( fabs(bb_max | direction1) , fabs(bb_min | direction1) ),
219 // std::max( fabs(bb_max | direction2) , fabs(bb_min | direction2) ) ) * 2.0;
220 //
221 // // compute the minimal scene radius from the bounding box
222 // // double radius = std::min( fabs(bb_max[0]-bb_min[0]) * 2,fabs(bb_max[2]-bb_min[2]) * 2);
223 //
224 // // set grid size to the given radius if we autoadjust to the scene size
225 // if ( radius > 10.0 )
226 // gridSize_ = radius;
227 //
228 //
229 // /*
230 // _state.set_line_width(3);
231 // glBegin(GL_LINES);
232 // glVertex(bb_min);
233 // glVertex(bb_max);
234 // glEnd();*/
235 //
236 // // // update the bounding box
237 // // bb_min_ = Vec3f(-0.5*gridSize_, 0.0, -0.5*gridSize_);
238 // // bb_max_ = Vec3f( 0.5*gridSize_, 0.0, 0.5*gridSize_);
239 //
240 // std::cerr << "Draw " << bb_min << " " << bb_max << std::endl;
241 //
242 //
243 //
244 // }
245 
246  //now start drawing
247  _state.translate( direction1 * -0.5 * gridSize_ + direction2 * -0.5 * gridSize_ );
248 
249  glBegin(GL_LINES);
250 
251  //red line (x-axis)
252  glColor3f( 0.7f, 0.0f, 0.0f );
253  glVertex( direction2 * gridSize_ * 0.5 );
254  glVertex( direction1 * gridSize_ + direction2 * gridSize_ * 0.5 );
255 
256  //blue line (z-axis)
257  glColor3f( 0.0f, 0.0f, 0.7f );
258  glVertex( direction1 * gridSize_ * 0.5 );
259  glVertex( direction1 * 0.5 * gridSize_ + direction2 * gridSize_);
260 
261  //remaining vertical lines
262  for ( int i = 0 ; i < vertical ; ++i ) {
263 
264  //first draw a baseLine
265  glColor3fv( &baseLineColor_[0] );
266 
267  double big = gridSize_ / (vertical-1) * i;
268  double next = gridSize_ / (vertical-1) * (i+1);
269 
270  glVertex( direction1 * big );
271  glVertex( direction1 * big + direction2 * gridSize_ );
272 
273  if ( i+1 < vertical)
274  for (int j=1; j < 10; j++){
275 
276  //then draw 9 darker lines in between
277  glColor3fv( &midLineColor_[0] );
278 
279  double smallPos = big + (next - big) / 10.0 * j;
280  glVertex( direction1 * smallPos);
281  glVertex( direction1 * smallPos + direction2 * gridSize_);
282  }
283  }
284 
285  //remaining horizontal lines
286  for ( int i = 0 ; i < horizontal; ++i ) {
287 
288  //first draw a baseline
289  glColor3fv( &baseLineColor_[0] );
290 
291  double big = gridSize_ / (vertical-1) * i;
292  double next = gridSize_ / (vertical-1) * (i+1);
293 
294  glVertex( direction2 * gridSize_ / (horizontal-1) * i);
295  glVertex( direction1 * gridSize_ + direction2 * gridSize_ / (horizontal-1) * i);
296 
297  if ( i+1 < vertical)
298  for (int j=1; j < 10; j++){
299 
300  //then draw 9 darker lines in between
301  glColor3fv( &midLineColor_[0] );
302 
303  double smallPos = big + (next - big) / 10.0 * j;
304  glVertex( direction2 * smallPos );
305  glVertex( direction1 * gridSize_ + direction2 * smallPos );
306  }
307  }
308  glEnd();
309 
310  _state.pop_modelview_matrix();
311 
312  }
313 
314  glLineWidth(1.0);
315 
316  glPopAttrib( ); // ->Depth test
317  glPopAttrib( ); // ->Lighting
318 }
319 
320 //-----------------------------------------------------------------------------
321 
322 void
323 GridNode::gridSize(float _size){
324  gridSize_ = _size;
325 
326  bb_min_ = Vec3f(-0.5*gridSize_, 0.0, -0.5*gridSize_);
327  bb_max_ = Vec3f( 0.5*gridSize_, 0.0, 0.5*gridSize_);
328 }
329 
330 //-----------------------------------------------------------------------------
331 
332 float
334  return gridSize_;
335 }
336 
337 //-----------------------------------------------------------------------------
338 
339 void
340 GridNode::minRefinementDistance( double _distance ) {
341  minRefinementDistance_ = _distance;
342 }
343 
344 //-----------------------------------------------------------------------------
345 
346 double
348 {
349  return minRefinementDistance_;
350 }
351 
352 //-----------------------------------------------------------------------------
353 
354 void GridNode::setOrientation( unsigned int _orientation)
355 {
356  orientation_ = _orientation;
357 }
358 
359 //-----------------------------------------------------------------------------
360 
361 void GridNode::autoResize(bool _auto)
362 {
363  autoResize_ = _auto;
364 }
365 
366 
367 //=============================================================================
368 } // namespace SceneGraph
369 } // namespace ACG
370 //=============================================================================
Vec3d eye() const
get eye point
Definition: GLState.cc:882
void setOrientation(unsigned int _orientation)
Set the plane orientation.
Definition: GridNode.cc:354
DrawMode SOLID_FLAT_SHADED
draw flat shaded faces (requires face normals)
Definition: DrawModes.cc:87
DrawMode WIREFRAME
draw wireframe
Definition: DrawModes.cc:84
float gridSize_
dimensions of the grid
Definition: GridNode.hh:161
void push_modelview_matrix()
push modelview matrix
Definition: GLState.cc:1006
Vec3d bb_min_
bounding box
Definition: GridNode.hh:164
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode)
drawing the primitive
Definition: GridNode.cc:139
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax)
update bounding box
Definition: GridNode.cc:107
void glVertex(const Vec2i &_v)
Wrapper: glVertex for Vec2i.
Definition: gl.hh:97
PickTarget
What target to use for picking.
Definition: BaseNode.hh:99
GridNode(BaseNode *_parent=0, const std::string &_name="<GridNode>")
Default constructor.
Definition: GridNode.cc:77
float gridSize()
Get GridSize.
Definition: GridNode.cc:333
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: Vector11T.hh:562
static void enable(GLenum _cap)
replaces glEnable, but supports locking
VectorT< float, 3 > Vec3f
Definition: VectorT.hh:125
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
Definition: GLState.cc:531
static void disable(GLenum _cap)
replaces glDisable, but supports locking
unsigned int orientation_
Contains all orientations to draw.
Definition: GridNode.hh:174
ACG::SceneGraph::DrawModes::DrawMode availableDrawModes() const
return available draw modes
Definition: GridNode.cc:96
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
void pop_modelview_matrix()
pop modelview matrix
Definition: GLState.cc:1022
void pick(GLState &_state, PickTarget _target)
don&#39;t pick me
Definition: GridNode.cc:129
double minRefinementDistance()
returns the minimal refinement distance
Definition: GridNode.cc:347
const GLMatrixd & modelview() const
get modelview matrix
Definition: GLState.hh:794
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: Vector11T.hh:534
Vec3f baseLineColor_
colors for the grid
Definition: GridNode.hh:168
int horizontalLines_
initial number of baseLines
Definition: GridNode.hh:150