CoordsysNode.cc 13.9 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  OpenFlipper is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  OpenFlipper is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
Jan Möbius's avatar
Jan Möbius committed
26 27 28
//   $Revision$
//   $Author$
//   $Date$
Jan Möbius's avatar
 
Jan Möbius committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
//
//=============================================================================




//=============================================================================
//
//  CLASS CoordsysNode - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================

#include "CoordsysNode.hh"
#include "../GL/gl.hh"
#include <iostream>
#include <math.h>


//== NAMESPACES ===============================================================

namespace ACG {
namespace SceneGraph {


//== IMPLEMENTATION ==========================================================


void
CoordsysNode::
boundingBox(Vec3f& _bbMin, Vec3f& _bbMax)
{
	//_bbMin.minimize( Vect3f  )
}


//----------------------------------------------------------------------------


unsigned int
CoordsysNode::
availableDrawModes() const
{
  return ( DrawModes::POINTS |
	        DrawModes::POINTS_SHADED |
	        DrawModes::POINTS_COLORED );
}


//----------------------------------------------------------------------------

void
CoordsysNode::
drawCoordsys( GLState&  _state) {

  double topRadius = 0.01;
  double arrowLength = 0.04;
  double bodyRadius = 0.004;
  double bodyLength = 0.06;
  int slices = 10;
  int stacks = 10;
  int loops = 10;
  double sphereRadius = 0.01;

  GLUquadricObj *quadric = gluNewQuadric();

  // Origin
  glColor3f(0.5, 0.5, 0.5);
  gluSphere( quadric, sphereRadius, slices, stacks );

  // X-Axis
  glColor3f(1.0, 0.0, 0.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
102 103 104
  _state.push_modelview_matrix ();
  _state.rotate (-90, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
Jan Möbius's avatar
 
Jan Möbius committed
105 106
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
107
  _state.translate ( 0, 0, -arrowLength );
Jan Möbius's avatar
 
Jan Möbius committed
108
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
109
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
110 111 112

  // Y-Axis
  glColor3f(0.0, 1.0, 0.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
113 114 115
  _state.push_modelview_matrix ();
  _state.rotate (90, 1, 0, 0);
  _state.translate ( 0, 0, -bodyLength );
Jan Möbius's avatar
 
Jan Möbius committed
116 117
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
118
  _state.translate ( 0, 0, -arrowLength );
Jan Möbius's avatar
 
Jan Möbius committed
119
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
120
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
121 122 123

  // Z-Axis
  glColor3f(0.0, 0.0, 1.0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
124 125 126
  _state.push_modelview_matrix ();
  _state.rotate (180, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
Jan Möbius's avatar
 
Jan Möbius committed
127 128
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
129
  _state.translate ( 0, 0, -arrowLength );
Jan Möbius's avatar
 
Jan Möbius committed
130
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
131
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
132 133 134 135 136 137 138 139 140

  gluDeleteQuadric(quadric);
}

//============================================================================

void
CoordsysNode::drawCoordsysPick( GLState&  _state) {

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
141 142 143 144 145 146 147 148
  double topRadius = 0.01;
  double arrowLength = 0.04;
  double bodyRadius = 0.004;
  double bodyLength = 0.06;
  int slices = 10;
  int stacks = 10;
  int loops = 10;
  double sphereRadius = 0.01;
Jan Möbius's avatar
 
Jan Möbius committed
149

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
150
  GLUquadricObj *quadric = gluNewQuadric();
Jan Möbius's avatar
 
Jan Möbius committed
151 152

  // Origin
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
153 154
  _state.pick_set_name (1);
  gluSphere( quadric, sphereRadius, slices, stacks );
Jan Möbius's avatar
 
Jan Möbius committed
155 156

  // X-Axis
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
157 158 159 160 161 162 163 164 165
  _state.pick_set_name (2);
  _state.push_modelview_matrix ();
  _state.rotate (-90, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
  _state.translate ( 0, 0, -arrowLength );
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
166 167

  // Y-Axis
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
168 169 170 171 172 173 174 175 176
  _state.pick_set_name (3);
  _state.push_modelview_matrix ();
  _state.rotate (90, 1, 0, 0);
  _state.translate ( 0, 0, -bodyLength );
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
  _state.translate ( 0, 0, -arrowLength );
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
  _state.pop_modelview_matrix ();
Jan Möbius's avatar
 
Jan Möbius committed
177 178

  // Z-Axis
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
179 180 181 182 183 184 185 186 187 188 189 190
  _state.pick_set_name (4);
  _state.push_modelview_matrix ();
  _state.rotate (180, 0, 1, 0);
  _state.translate ( 0, 0, -bodyLength );
  gluCylinder( quadric, bodyRadius, bodyRadius, bodyLength, slices, stacks );
  gluDisk( quadric, 0, topRadius, slices, loops );
  _state.translate ( 0, 0, -arrowLength );
  gluCylinder( quadric, 0, topRadius, arrowLength, slices, stacks );
  _state.pop_modelview_matrix ();


  gluDeleteQuadric(quadric);
Jan Möbius's avatar
 
Jan Möbius committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

}


//============================================================================


void
CoordsysNode::
draw(GLState&  _state  , unsigned int /*_drawMode*/)
{
  // Push Modelview-Matrix
  _state.push_modelview_matrix();

  Vec4f lastBaseColor = _state.base_color();

  glPushAttrib( GL_LIGHTING_BIT ); // STACK_ATTRIBUTES <- LIGHTING_ATTRIBUTE
  glEnable(GL_LIGHTING);
  glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
  glEnable(GL_COLOR_MATERIAL);
  glShadeModel(GL_SMOOTH);

  // Init state - changes when mode_ != POSITION
  Vec3d pos3D(0.0,0.0,0.0);

  if ( mode_ == SCREENPOS ) {

    int left, bottom, width, height;
    _state.get_viewport(left, bottom, width, height);

    // Projection reset
    _state.push_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
223 224
    _state.reset_projection();
    _state.perspective(45.0, _state.aspect(), 0.8, 20.0);
Jan Möbius's avatar
 
Jan Möbius committed
225 226 227 228 229 230

    float posx = left + width - 30.0 ;
    float posy = bottom + height - 30.0 ;

    Vec3d screenposCenterPoint( posx , posy , 0.0);

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
231 232
    _state.push_modelview_matrix();
    _state.reset_modelview();
Jan Möbius's avatar
 
Jan Möbius committed
233

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
234 235
    pos3D = _state.unproject (Vec3d (posx, posy, 0.5));
    _state.pop_modelview_matrix();
Jan Möbius's avatar
 
Jan Möbius committed
236

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
237 238
    // reset scene translation
    GLMatrixd modelview = _state.modelview();
Jan Möbius's avatar
 
Jan Möbius committed
239

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
240 241 242
    modelview(0,3) = 0.0;
    modelview(1,3) = 0.0;
    modelview(2,3) = 0.0;
Jan Möbius's avatar
 
Jan Möbius committed
243

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
244 245
    _state.set_modelview (modelview);
    _state.translate (pos3D[0], pos3D[1], pos3D[2]-0.3, MULT_FROM_LEFT);
Jan Möbius's avatar
 
Jan Möbius committed
246

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
247 248 249
    // clear depth buffer behind coordsys node
    drawClearArea(_state, false);

Jan Möbius's avatar
 
Jan Möbius committed
250 251 252 253 254
    // Koordinatensystem zeichnen
    drawCoordsys(_state);

    // Projection reload
    _state.pop_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
255

Jan Möbius's avatar
 
Jan Möbius committed
256 257 258

  } else if (mode_ == POSITION) { /* mode_ == POSITION */

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
259 260 261 262 263 264 265
    GLMatrixd modelview = _state.modelview();

    modelview(0,3) = 0.0;
    modelview(1,3) = 0.0;
    modelview(2,3) = 0.0;

    _state.set_modelview (modelview);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
266 267 268

    // clear depth buffer behind coordsys node
    drawClearArea(_state, false);
Jan Möbius's avatar
 
Jan Möbius committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    drawCoordsys(_state);

  }

  glPopAttrib();

  glColor4fv(lastBaseColor.data());

  // Reload old configuration
  _state.pop_modelview_matrix();
}


void
CoordsysNode::
setMode(const CoordsysMode _mode)
{
  mode_ = _mode;
}

void
CoordsysNode::
setPosition(const Vec3f& _pos)
{
  pos3f_ = _pos;
}

CoordsysNode::CoordsysMode
CoordsysNode::
getMode() const
{
  return mode_;
}

void
CoordsysNode::pick(GLState& _state, PickTarget _target)
{
Jan Möbius's avatar
Jan Möbius committed
306 307
  if (_target == PICK_ANYTHING) {

Jan Möbius's avatar
 
Jan Möbius committed
308
    // Push Modelview-Matrix
Jan Möbius's avatar
Jan Möbius committed
309
    _state.push_modelview_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
310
    _state.pick_set_maximum (5);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
311
    _state.pick_set_name (0);
Jan Möbius's avatar
 
Jan Möbius committed
312 313

    // Init state - changes when mode_ != POSITION
Jan Möbius's avatar
Jan Möbius committed
314
    Vec3d pos3D(0.0,0.0,0.0);
Jan Möbius's avatar
 
Jan Möbius committed
315

Jan Möbius's avatar
Jan Möbius committed
316
    if ( mode_ == SCREENPOS ) {
Jan Möbius's avatar
 
Jan Möbius committed
317

Jan Möbius's avatar
Jan Möbius committed
318 319
      int left, bottom, width, height;
      _state.get_viewport(left, bottom, width, height);
Jan Möbius's avatar
 
Jan Möbius committed
320

Jan Möbius's avatar
Jan Möbius committed
321 322
      // Projection reset
      _state.push_projection_matrix();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
323 324
      _state.reset_projection();
      _state.perspective(45.0, _state.aspect(), 0.8, 20.0);
Jan Möbius's avatar
 
Jan Möbius committed
325

Jan Möbius's avatar
Jan Möbius committed
326 327
      float posx = left + width - 30.0 ;
      float posy = bottom + height - 30.0 ;
Jan Möbius's avatar
 
Jan Möbius committed
328

Jan Möbius's avatar
Jan Möbius committed
329
      Vec3d screenposCenterPoint( posx , posy , 0.0);
Jan Möbius's avatar
 
Jan Möbius committed
330

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
331 332
      _state.push_modelview_matrix();
      _state.reset_modelview();
Jan Möbius's avatar
 
Jan Möbius committed
333

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
334 335
      pos3D = _state.unproject (Vec3d (posx, posy, 0.5));
      _state.pop_modelview_matrix();
Jan Möbius's avatar
 
Jan Möbius committed
336

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
337 338
      // reset scene translation
      GLMatrixd modelview = _state.modelview();
Jan Möbius's avatar
 
Jan Möbius committed
339

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
340 341 342
      modelview(0,3) = 0.0;
      modelview(1,3) = 0.0;
      modelview(2,3) = 0.0;
Jan Möbius's avatar
 
Jan Möbius committed
343

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
344 345
      _state.set_modelview (modelview);
      _state.translate (pos3D[0], pos3D[1], pos3D[2]-0.3, MULT_FROM_LEFT);
Jan Möbius's avatar
 
Jan Möbius committed
346

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
347 348 349
      // clear depth buffer behind coordsys node
      drawClearArea(_state, true);

Jan Möbius's avatar
Jan Möbius committed
350 351 352 353 354 355
      // Koordinatensystem zeichnen
      drawCoordsysPick(_state);

      // Projection reload
      _state.pop_projection_matrix();

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
356

Jan Möbius's avatar
Jan Möbius committed
357 358
    } else if (mode_ == POSITION) { /* mode_ == POSITION */

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
359 360 361 362 363 364
      GLMatrixd modelview = _state.modelview();

      modelview(0,3) = 0.0;
      modelview(1,3) = 0.0;
      modelview(2,3) = 0.0;

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
365 366
      // clear depth buffer behind coordsys node
      drawClearArea(_state, true);
Jan Möbius's avatar
Jan Möbius committed
367
      drawCoordsysPick(_state);
Jan Möbius's avatar
 
Jan Möbius committed
368
    }
Jan Möbius's avatar
Jan Möbius committed
369 370 371 372
    // Reload old configuration
    _state.pop_modelview_matrix();

  }
Jan Möbius's avatar
 
Jan Möbius committed
373 374
}

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
//----------------------------------------------------------------------------

void CoordsysNode::drawClearArea(GLState&  _state, bool _pick)
{
  std::vector<Vec2f> points;
  Vec2f center;
  float radius;

  int left, bottom, width, height;
  _state.get_viewport(left, bottom, width, height);

  GLUquadricObj *quadric = gluNewQuadric();

  // respect sphere radius
  Vec3d proj = _state.project (Vec3d (-0.01, -0.01, -0.01));
  points.push_back (Vec2f (proj[0], proj[1]));

  proj = _state.project (Vec3d (0.1, 0.0, 0.0));
  points.push_back (Vec2f (proj[0], proj[1]));

  proj = _state.project (Vec3d (0.0, 0.1, 0.0));
  points.push_back (Vec2f (proj[0], proj[1]));

  proj = _state.project (Vec3d (0.0, 0.0, 0.1));
  points.push_back (Vec2f (proj[0], proj[1]));


  // get bounding circle of projected 4 points of the coord node
  boundingCircle(points, center, radius);

  _state.push_projection_matrix();
  _state.reset_projection();
  _state.ortho (left, left + width, bottom, bottom + height, 0.0, 1.0);

  _state.push_modelview_matrix();
  _state.reset_modelview();
  glDepthFunc (GL_ALWAYS);
  _state.translate (center[0], center[1], -1.0);

  if (_pick)
    _state.pick_set_name (0);
  else
    glColorMask(false, false, false, false);

  // 10% more to ensure everything is in
  gluDisk( quadric, 0, radius * 1.1, 10, 10 );

  glDepthFunc (GL_LESS);
  _state.pop_modelview_matrix();
  _state.pop_projection_matrix();

  if (!_pick)
    glColorMask(true, true, true, true);

  gluDeleteQuadric(quadric);
}

//----------------------------------------------------------------------------

void CoordsysNode::boundingCircle(std::vector<Vec2f> &_in, Vec2f &_center, float &_radius)
{
  if (_in.size () == 0)
    return;
  if (_in.size () < 2)
  {
    _center = _in[0];
    _radius = 0.0f;
    return;
  }
  bool found = false;

  // try all circumcircles of all possible lines
  for (unsigned int i = 0; i < _in.size () - 1; i++)
    for (unsigned int j = i + 1; j < _in.size (); j++)
    {
      Vec2f cen = (_in[i] + _in[j]) * 0.5;
      float rad = (_in[i] - cen).length ();
      bool allin = true;

      for (unsigned int k = 0; k < _in.size (); k++)
        if (k != i && k != j && (_in[k] - cen).length () > rad)
        {
          allin = false;
          break;
        }

      if (!allin)
        continue;

      if (found)
      {
        if (rad < _radius)
        {
          _center = cen;
          _radius = rad;
        }
      }
      else
      {
        found = true;
        _center = cen;
        _radius = rad;
      }
    }

  if (found)
    return;

  // try all circumcircles of all possible triangles
  for (unsigned int i = 0; i < _in.size () - 2; i++)
    for (unsigned int j = i + 1; j < _in.size () - 1; j++)
      for (unsigned int k = j + 1; k < _in.size (); k++)
      {
        float v = ((_in[k][0]-_in[j][0])*((_in[i][0]*_in[i][0])+(_in[i][1]*_in[i][1]))) +
                  ((_in[i][0]-_in[k][0])*((_in[j][0]*_in[j][0])+(_in[j][1]*_in[j][1]))) +
                  ((_in[j][0]-_in[i][0])*((_in[k][0]*_in[k][0])+(_in[k][1]*_in[k][1])));
        float u = ((_in[j][1]-_in[k][1])*((_in[i][0]*_in[i][0])+(_in[i][1]*_in[i][1]))) +
                  ((_in[k][1]-_in[i][1])*((_in[j][0]*_in[j][0])+(_in[j][1]*_in[j][1]))) +
                  ((_in[i][1]-_in[j][1])*((_in[k][0]*_in[k][0])+(_in[k][1]*_in[k][1])));
        float d = (_in[i][0]*_in[j][1])+(_in[j][0]*_in[k][1])+(_in[k][0]*_in[i][1]) -
                  (_in[i][0]*_in[k][1])-(_in[j][0]*_in[i][1])-(_in[k][0]*_in[j][1]);
        Vec2f cen(0.5 * (u/d), 0.5 * (v/d));
        float rad = (_in[i] - cen).length ();
        bool allin = true;

        for (unsigned int l = 0; l < _in.size (); l++)
          if (l != i && l != j && l != k && (_in[l] - cen).length () > rad)
          {
            allin = false;
            break;
          }

        if (!allin)
          continue;

        if (found)
        {
          if (rad < _radius)
          {
            _center = cen;
            _radius = rad;
          }
        }
        else
        {
          found = true;
          _center = cen;
          _radius = rad;
        }
      }
}

Jan Möbius's avatar
 
Jan Möbius committed
527 528 529 530
//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================