SoOpenMeshNodeT_impl.hh 10.8 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
2 3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo  
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6 7
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
8
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
9 10 11
 *---------------------------------------------------------------------------*
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
12
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
13 14 15
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
16
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
17 18
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
19
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
20 21 22
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
23
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
24 25 26
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
27
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
28 29 30 31 32 33 34 35 36 37 38
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39 40
 *                                                                           *
 * ========================================================================= */
41

42

Jan Möbius's avatar
Jan Möbius committed
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

//=============================================================================
//
//  Class SoOpenMeshNodeT - implementation
//
//=============================================================================


#define OPENMESH_SOOPENMESHNODE_CC

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

#include <GL/gl.h>
#include <Inventor/SbBox.h>
#include <Inventor/SoPrimitiveVertex.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/misc/SoState.h>
#include <Inventor/elements/SoLightModelElement.h>
#include <Inventor/bundles/SoMaterialBundle.h>

#include "SoOpenMeshNodeT.hh"
// Attention must be included after SoOpenMeshNodeT.hh
// as it redefines several macros!
#include "SoOpenMeshSupport.hh"

#include <OpenMesh/Core/Math/VectorT.hh>


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


namespace OpenMesh {


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


// Helper functions: draw vertices
inline void glVertex(const OpenMesh::Vec3f& _v)  { glVertex3fv(_v); }
inline void glVertex(const OpenMesh::Vec3d& _v)  { glVertex3dv(_v); }

// Helper functions: draw normal
inline void glNormal(const OpenMesh::Vec3f& _n)  { glNormal3fv(_n); }
inline void glNormal(const OpenMesh::Vec3d& _n)  { glNormal3dv(_n); }

// Helper function: convert Vec to SbVec3f
template <class Vec> inline SbVec3f sbvec3f(const Vec& _v) { 
  return SbVec3f(_v[0], _v[1], _v[2]); 
}


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


template <class Mesh>
void
SoOpenMeshNodeT<Mesh>::initClass()
{
  SO_NODE_INIT_CLASS(SoOpenMeshNodeT<Mesh>, SoShape, "Shape");
}


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


template <class Mesh>
SoOpenMeshNodeT<Mesh>::SoOpenMeshNodeT(const Mesh* _mesh) :
  mesh_(_mesh)
{
  SO_NODE_CONSTRUCTOR(SoOpenMeshNodeT<Mesh>);
}


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


template <class Mesh>
void
SoOpenMeshNodeT<Mesh>::GLRender(SoGLRenderAction *action)
{
  if (mesh_ && shouldGLRender(action))
  {
    SoState*  state = action->getState();
    SbBool send_normals = (SoLightModelElement::get(state) !=
			   SoLightModelElement::BASE_COLOR);

    SoMaterialBundle mb(action);
    mb.sendFirst();

    drawFaces(send_normals);
  }
}


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


template<class Mesh>
void
SoOpenMeshNodeT<Mesh>::
drawFaces(bool _send_normals, OpenMesh::GenProg::Bool2Type<true>)
{
  typename Mesh::ConstFaceIter    f_it(mesh_->faces_begin()), 
                                  f_end(mesh_->faces_end());

  typename Mesh::ConstFaceVertexIter fv_it;


  if (_send_normals)
  {
    glBegin(GL_TRIANGLES);
    for (; f_it!=f_end; ++f_it)
    {
      glNormal(mesh_->normal(f_it));
Jan Möbius's avatar
Jan Möbius committed
157
      fv_it = mesh_->cfv_iter(*f_it);
Jan Möbius's avatar
Jan Möbius committed
158 159 160 161 162 163 164 165 166 167 168 169 170
      glVertex(mesh_->point(fv_it));
      ++fv_it;
      glVertex(mesh_->point(fv_it));
      ++fv_it;
      glVertex(mesh_->point(fv_it));
    }
    glEnd();
  }
  else 
  {
    glBegin(GL_TRIANGLES);
    for (; f_it!=f_end; ++f_it)
    {
Jan Möbius's avatar
Jan Möbius committed
171
      fv_it = mesh_->cfv_iter(*f_it);
Jan Möbius's avatar
Jan Möbius committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
      glVertex(mesh_->point(fv_it));
      ++fv_it;
      glVertex(mesh_->point(fv_it));
      ++fv_it;
      glVertex(mesh_->point(fv_it));
    }
    glEnd();
  }
}


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


template<class Mesh>
void
SoOpenMeshNodeT<Mesh>::
drawFaces(bool _send_normals, OpenMesh::GenProg::Bool2Type<false>)
{
  typename Mesh::ConstFaceIter    f_it(mesh_->faces_begin()), 
                                  f_end(mesh_->faces_end());

  typename Mesh::ConstFaceVertexIter fv_it;


  if (_send_normals)
  {
    for (; f_it!=f_end; ++f_it)
    {
      glBegin(GL_POLYGON);
      glNormal(mesh_->normal(f_it));
Jan Möbius's avatar
Jan Möbius committed
203
      for (fv_it=mesh_->cfv_iter(*f_it); fv_it.is_valid() ++fv_it)
Jan Möbius's avatar
Jan Möbius committed
204 205 206 207 208 209 210 211 212
	glVertex(mesh_->point(fv_it));
      glEnd();
    }
  }
  else
  {
    for (; f_it!=f_end; ++f_it)
    {
      glBegin(GL_POLYGON);
Jan Möbius's avatar
Jan Möbius committed
213
      for (fv_it=mesh_->cfv_iter(*f_it); fv_it.is_valid() ++fv_it)
Jan Möbius's avatar
Jan Möbius committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	glVertex(mesh_->point(fv_it));
      glEnd();
    }
  }
}


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


template <class Mesh>
void
SoOpenMeshNodeT<Mesh>::generatePrimitives(SoAction* _action)
{
  if (mesh_)
    genPrimitives(_action);
}


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


template<class Mesh>
void
SoOpenMeshNodeT<Mesh>::
genPrimitives(SoAction* _action, OpenMesh::GenProg::Bool2Type<true>)
{
  typename Mesh::ConstFaceIter    f_it(mesh_->faces_begin()), 
                                  f_end(mesh_->faces_end());

  typename Mesh::ConstFaceVertexIter fv_it;


  SoPrimitiveVertex pv;

  beginShape(_action, TRIANGLES);

  for (; f_it!=f_end; ++f_it)
  {
    pv.setNormal(sbvec3f(mesh_->normal(f_it)));
			 
Jan Möbius's avatar
Jan Möbius committed
255
    fv_it = mesh_->cfv_iter(*f_it);
Jan Möbius's avatar
Jan Möbius committed
256 257 258 259 260 261 262 263 264 265 266 267 268 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

    pv.setPoint(sbvec3f(mesh_->point(fv_it)));
    shapeVertex(&pv);
    ++fv_it;

    pv.setPoint(sbvec3f(mesh_->point(fv_it)));
    shapeVertex(&pv);
    ++fv_it;

    pv.setPoint(sbvec3f(mesh_->point(fv_it)));
    shapeVertex(&pv);
  }

  endShape();
}


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


template<class Mesh>
void
SoOpenMeshNodeT<Mesh>::
genPrimitives(SoAction* _action, OpenMesh::GenProg::Bool2Type<false>)
{
  typename Mesh::ConstFaceIter    f_it(mesh_->faces_begin()), 
                                  f_end(mesh_->faces_end());

  typename Mesh::ConstFaceVertexIter fv_it;

  SoPrimitiveVertex pv;


  for (; f_it!=f_end; ++f_it)
  {
    beginShape(_action, POLYGON);

    pv.setNormal(sbvec3f(mesh_->normal(f_it)));

Jan Möbius's avatar
Jan Möbius committed
295
    for (fv_it=mesh_->cfv_iter(*f_it); fv_it.is_valid() ++fv_it)
Jan Möbius's avatar
Jan Möbius committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    {
      pv.setPoint(sbvec3f(mesh_->point(fv_it)));
      shapeVertex(&pv);
    }

    endShape();
  }
}


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


template <class Mesh>
void
SoOpenMeshNodeT<Mesh>::computeBBox(SoAction *action,
				   SbBox3f &box,
				   SbVec3f &center)
{
  if (mesh_ && mesh_->n_vertices())
  {
    typename Mesh::ConstVertexIter  vIt(mesh_->vertices_begin());
    typename Mesh::ConstVertexIter  vEnd(mesh_->vertices_end());

    typename Mesh::Point min(mesh_->point(vIt));
    typename Mesh::Point max(mesh_->point(vIt));
                         

    for (++vIt; vIt != vEnd; ++vIt)
    {
      max.maximize(mesh_->point(vIt));
      min.minimize(mesh_->point(vIt));
    }

    box.setBounds(SbVec3f(min[0],min[1],min[2]),
		  SbVec3f(max[0],max[1],max[2]));
  }
  
  else  box.setBounds(SbVec3f(0,0,0), SbVec3f(0,0,0));
}


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

#if 0
/* As we are using templates, we cannot use the predefined macro
   SO_NODE_SOURCE to automatically generate the following piece of
   code. Ugly, but necessary.  
*/

template <class Mesh>
SoType SoOpenMeshNodeT<Mesh>::classTypeId;

template <class Mesh>
SoFieldData *SoOpenMeshNodeT<Mesh>::fieldData;

template <class Mesh>
const SoFieldData **SoOpenMeshNodeT<Mesh>::parentFieldData;

template <class Mesh>
SbBool SoOpenMeshNodeT<Mesh>::firstInstance = TRUE;

template <class Mesh> 
SoType SoOpenMeshNodeT<Mesh>::getTypeId() const {
  return classTypeId; 
}

template <class Mesh> 
const SoFieldData* 
SoOpenMeshNodeT<Mesh>::getFieldData() const {
  SO__NODE_CHECK_CONSTRUCT("SoOpenMeshNodeT");
  return fieldData;
}

template <class Mesh> 
void* SoOpenMeshNodeT<Mesh>::createInstance() {
  return (void *)(new SoOpenMeshNodeT<Mesh>);
}

#else

SO_NODE_SOURCE(SoOpenMeshNodeT<Mesh>);

#endif

//=============================================================================
} // namespace OpenMesh
//=============================================================================