Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
VolumeMeshBufferManager.hh
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 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #ifndef VOLUME_MESH_BUFFER_MANAGER_HH
51 #define VOLUME_MESH_BUFFER_MANAGER_HH
52 
53 #include <ACG/GL/GLState.hh>
54 #include <ACG/Scenegraph/DrawModes.hh>
55 #include <ACG/Scenegraph/BaseNode.hh>
56 #include <ACG/GL/VertexDeclaration.hh>
57 
58 #include "VolumeMeshDrawModesContainer.hh"
59 
60 #include <OpenVolumeMesh/Attribs/StatusAttrib.hh>
61 #include <OpenVolumeMesh/Attribs/NormalAttrib.hh>
62 #include <OpenVolumeMesh/Attribs/ColorAttrib.hh>
63 #include <OpenVolumeMesh/Attribs/TexCoordAttrib.hh>
64 
68 template<class VolumeMesh>
70 {
71  // typedefs for easy access
78 
79  typedef typename VolumeMesh::PointT Vertex;
83 
84 public:
85 
86  enum PrimitiveMode
87  {
88  PM_NONE,
89  PM_CELLS,
90  PM_FACES,
91  PM_FACES_ON_CELLS,
92  PM_HALFFACES,
93  PM_HALFFACES_ON_CELLS,
94  PM_EDGES,
95  PM_IRREGULAR_EDGES,
96  PM_EDGES_ON_CELLS,
97  PM_HALFEDGES,
98  PM_VERTICES,
99  PM_VERTICES_ON_CELLS
100  };
101 
102  enum NormalMode
103  {
104  NM_NONE,
105  NM_FACE,
106  NM_HALFFACE,
107  NM_VERTEX
108  };
109 
110  enum ColorMode
111  {
112  CM_NO_COLORS,
113  CM_CELL,
114  CM_FACE,
115  CM_HALFFACE,
116  CM_EDGE,
117  CM_HALFEDGE,
118  CM_VERTEX,
119  CM_PICK
120  };
121 
122  enum TexCoordMode
123  {
124  TCM_NONE,
125  TCM_SINGLE_2D
126  };
127 
128  class Plane {
129  public:
130  Plane(const ACG::Vec3d& _p, const ACG::Vec3d& _n, const ACG::Vec3d& _x, const ACG::Vec3d& _y) :
131  position(_p), normal(_n), xdirection(_x), ydirection(_y) {
132  }
133 
134  // position
135  ACG::Vec3d position;
136  // normal
137  ACG::Vec3d normal;
138  // xdirection scaled by 1.0/width -> [0,1] is inrange
139  ACG::Vec3d xdirection;
140  // ydirection scaled by 1.0/height -> [0,1] is inrange
141  ACG::Vec3d ydirection;
142  };
143 
144  VolumeMeshBufferManager(const VolumeMesh &mesh_, OpenVolumeMesh::StatusAttrib& statusAttrib_,
148  virtual ~VolumeMeshBufferManager(){ free(); }
149 
150 
152  void setScale(double _scale) { mScale = _scale; invalidate();}
153 
155  double getScale() const { return mScale; }
156 
157 
159  unsigned char getStride() { return mVertexSize; }
160 
162  unsigned char getColorOffset() { return mColorOffset; }
163 
165  unsigned char getNormalOffset() { return mNormalOffset; }
166 
168  unsigned char getTexCoordOffset() { return mTexCoordOffset; }
169 
170 
172  const ACG::VertexDeclaration* getVertexDeclaration() { return &mVertexDeclaration; }
173 
174 
176  GLuint getBuffer();
177 
179  GLuint getPickBuffer(ACG::GLState &_state, unsigned int _offset);
180 
181 
183  void invalidate();
184 
186  void invalidateGeometry();
187 
189  void invalidateColors();
190 
192  void invalidateNormals();
193 
195  void invalidateTexCoords();
196 
197 
199  void free();
200 
201 
203  unsigned int getNumOfVertices();
204 
205 
207  void setDefaultColor(ACG::Vec4f _defaultColor);
208 
209 
212 
213 
215  void disableColors() { mColorMode = CM_NO_COLORS; }
216 
218  void enablePerCellColors() { mColorMode = CM_CELL; }
219 
221  void enablePerFaceColors() { mColorMode = CM_FACE; }
222 
224  void enablePerHalffaceColors() { mColorMode = CM_HALFFACE; }
225 
227  void enablePerEdgeColors() { mColorMode = CM_EDGE; }
228 
230  void enablePerHalfedgeColors() { mColorMode = CM_HALFEDGE; }
231 
233  void enablePerVertexColors() { mColorMode = CM_VERTEX; }
234 
236  void enablePickColors() { mColorMode = CM_PICK; }
237 
238 
240  void disableNormals() { mNormalMode = NM_NONE; }
241 
243  void enablePerFaceNormals() { mNormalMode = NM_FACE; }
244 
246  void enablePerHalffaceNormals() { mNormalMode = NM_HALFFACE; }
247 
249  void enablePerVertexNormals() { mNormalMode = NM_VERTEX; }
250 
251 
253  void disablePrimitives() { mPrimitiveMode = PM_NONE; }
254 
256  void enableCellPrimitives() { mPrimitiveMode = PM_CELLS; }
257 
259  void enableFacePrimitives() { mPrimitiveMode = PM_FACES; }
260 
262  void enableFaceOnCellPrimitives() { mPrimitiveMode = PM_FACES_ON_CELLS; }
263 
265  void enableHalffacePrimitives() { mPrimitiveMode = PM_HALFFACES; }
266 
268  void enableHalffaceOnCellPrimitives() { mPrimitiveMode = PM_HALFFACES_ON_CELLS; }
269 
271  void enableEdgePrimitives() { mPrimitiveMode = PM_EDGES; }
272 
274  void enableIrregularEdgePrimitives() { mPrimitiveMode = PM_IRREGULAR_EDGES; }
275 
277  void enableEdgeOnCellPrimitives() { mPrimitiveMode = PM_EDGES_ON_CELLS; }
278 
280  void enableHalfedgePrimitives() { mPrimitiveMode = PM_HALFEDGES; }
281 
283  void enableVertexPrimitives() { mPrimitiveMode = PM_VERTICES; }
284 
286  void enableVertexOnCellPrimitives() { mPrimitiveMode = PM_VERTICES_ON_CELLS; }
287 
288 
290  void disableTextures() { mTexCoordMode = TCM_NONE; }
291 
293  void enableTextureSingle() { mTexCoordMode = TCM_SINGLE_2D; }
294 
295 
297  bool selectionOnly() { return mSkipUnselected; }
298 
300  void setSelectionOnly(bool _selectionOnly) { mSkipUnselected = _selectionOnly; }
301 
302 
304  void clearCutPlanes();
305 
308 
310  void addCutPlane(const ACG::Vec3d& _p, const ACG::Vec3d& _n, const ACG::Vec3d& _xsize, const ACG::Vec3d& _ysize);
311 
312 
314  void setBoundaryOnly(bool _boundaryOnly) { mBoundaryOnly = _boundaryOnly; }
315 
316 private:
318  bool is_inside(const ACG::Vec3d& _p);
319 
321  bool is_inside(const VertexHandle& _vh);
322 
324  bool is_inside(const HalfEdgeHandle& _heh);
325 
327  bool is_inside(const EdgeHandle& _eh);
328 
330  bool is_inside(const HalfFaceHandle& _hfh);
331 
333  bool is_inside(const FaceHandle& _fh);
334 
336  bool is_inside(const CellHandle& _ch);
337 
340 
341 
343  bool optionsChanged();
344 
346  void saveOptions();
347 
348 
350  void addFloatToBuffer( float _value, unsigned char *&_buffer );
351 
353  void addUCharToBuffer( unsigned char _value, unsigned char *&_buffer );
354 
356  void addPositionToBuffer(ACG::Vec3d _position, unsigned char* _buffer , unsigned int _offset);
357 
359  void addColorToBuffer(ACG::Vec4uc _color, unsigned char* _buffer, unsigned int _offset);
360 
362  void addColorToBuffer(ACG::Vec4f _color, unsigned char* _buffer, unsigned int _offset);
363 
365  void addNormalToBuffer(ACG::Vec3d _normal, unsigned char *_buffer, unsigned int _offset);
366 
368  void addTexCoordToBuffer(ACG::Vec2f _texCoord, unsigned char* _buffer, unsigned int _offset);
369 
370 
373 
374 
376  void buildVertexBuffer(unsigned char* _buffer);
377 
379  void buildNormalBuffer(unsigned char* _buffer);
380 
382  void buildColorBuffer(unsigned char* _buffer);
383 
385  void buildTexCoordBuffer(unsigned char* _buffer);
386 
388  void buildPickColorBuffer(ACG::GLState &_state, unsigned int _offset, unsigned char* _buffer);
389 
390 
392  ACG::Vec4f getValenceColorCode(unsigned int _valence, bool _inner) const;
393 
394 
396  void countNumOfVertices();
397 
398 
401 
404 
407 
410 
412  void calculateCOGs();
413 
415  bool positionsNeedRebuild();
416 
418  bool colorsNeedRebuild();
419 
421  bool texCoordsNeedRebuild();
422 
424  bool normalsNeedRebuild();
425 
426 private:
427  ACG::Vec4f mDefaultColor;
428 
429  const VolumeMesh& mMesh;
430  OpenVolumeMesh::StatusAttrib& mStatusAttrib;
434 
435  int mNumOfVertices;
436  int mCurrentNumOfVertices;
437 
438  unsigned int mVertexSize;
439  ACG::VertexDeclaration mVertexDeclaration;
440 
441  int mColorOffset;
442  int mNormalOffset;
443 
444  double mScale;
445 
446  GLuint mBuffer;
447 
448  // local picking offset ?
449  unsigned int mCurrentPickOffset;
450 
451  // global picking offset
452  unsigned int mGlobalPickOffset;
453 
454  bool mInvalidated;
455  bool mGeometryChanged;
456  bool mNormalsChanged;
457  bool mColorsChanged;
458  bool mTexCoordsChanged;
459 
460  //draw modes
461  VolumeMeshDrawModesContainer mDrawModes;
462 
463  //options
464  PrimitiveMode mPrimitiveMode;
465  NormalMode mNormalMode;
466  ColorMode mColorMode;
467 
468  bool mSkipUnselected;
469  bool mShowIrregularInnerEdges;
470  bool mShowIrregularOuterValence2Edges;
471  bool mSkipRegularEdges;
472  bool mBoundaryOnly;
473 
474  PrimitiveMode mCurrentPrimitiveMode;
475  NormalMode mCurrentNormalMode;
476  ColorMode mCurrentColorMode;
477 
478  bool mCurrentSkipUnselected;
479  bool mCurrentShowIrregularInnerEdges;
480  bool mCurrentShowIrregularOuterValence2Edges;
481  bool mCurrentSkipRegularEdges;
482  bool mCurrentBoundaryOnly;
483 
484  unsigned char mCurrentVertexSize;
485  unsigned char mCurrentNormalOffset;
486  unsigned char mCurrentColorOffset;
487 
488  std::vector<Plane> cut_planes_;
489 
490  std::vector<ACG::Vec3d> mCogs;
491  bool mCogsValid;
492  std::vector<bool> mCellInsideness;
493  bool mCellInsidenessValid;
494 
495  TexCoordMode mTexCoordMode;
496  TexCoordMode mCurrentTexCoordMode;
497  unsigned char mTexCoordOffset;
498  unsigned char mCurrentTexCoordOffset;
499 
500 };
501 
502 //=============================================================================
503 #if defined(INCLUDE_TEMPLATES) && !defined(VOLUMEMESHBUFFERMANAGERT_CC)
504 #include "VolumeMeshBufferManagerT.cc"
505 #endif
506 //=============================================================================
507 
508 #endif // VOLUME_MESH_BUFFER_MANAGER_HH
ACG::Vec4f getValenceColorCode(unsigned int _valence, bool _inner) const
Returns a color code for irregular edges.
void setBoundaryOnly(bool _boundaryOnly)
Sets whether only the boundary primitves should be rendered.
void enableIrregularEdgePrimitives()
Enables edge primitives for irregular edges.
void setOptionsFromDrawMode(ACG::SceneGraph::DrawModes::DrawMode _drawMode)
Configures the buffer manager's options from a DrawMode.
void calculateCOGs()
Calculates the center of gravity for all cells.
void enableEdgeOnCellPrimitives()
Enables edge primitives which are drawn on the cells.
void enablePerHalffaceNormals()
Enables per halfface normals.
void enablePerVertexNormals()
Enables per vertex normals.
void invalidate()
Invalidates the buffer.
void clearCutPlanes()
Removes all cut planes.
const ACG::VertexDeclaration * getVertexDeclaration()
Returns a VertexDeclaration for the vertices stored in the buffer.
void enableVertexPrimitives()
Enables vertex primitives.
void invalidateColors()
Invalidates colors.
void invalidateNormals()
Invalidates normals.
void buildColorBuffer(unsigned char *_buffer)
Adds all colors to the buffer.
bool texCoordsNeedRebuild()
Checks whether texture coordinates need to be rebuild.
void setScale(double _scale)
Sets the scale used to shrink cells.
void free()
Deletes the buffers on the GPU.
void addPositionToBuffer(ACG::Vec3d _position, unsigned char *_buffer, unsigned int _offset)
Adds a position to the buffer.
void disablePrimitives()
Disables primitives (nothing will be drawn)
void enableEdgePrimitives()
Enables edge primitves.
void addUCharToBuffer(unsigned char _value, unsigned char *&_buffer)
Adds an unsigned char to the buffer.
bool positionsNeedRebuild()
Checks whether positions need to be rebuild.
void enablePerFaceNormals()
Enables per face normals.
This class creates buffers that can be used to render open volume meshs.
void addCutPlane(const typename VolumeMeshBufferManager< VolumeMesh >::Plane &_p)
Adds a cut plane.
GLuint getBuffer()
Returns the name of the buffer.
void enablePickColors()
Enables picking colors.
void enablePerFaceColors()
Enables per face colors.
void enableCellPrimitives()
Enables cell primitives.
void enableFacePrimitives()
Enables face primitives.
int getNumOfIncidentCells(OpenVolumeMesh::FaceHandle _fh)
Returns the number of cells that are incident to the given face and also inside w.r.t. all cut planes.
void buildPickColorBuffer(ACG::GLState &_state, unsigned int _offset, unsigned char *_buffer)
Adds all picking colors to the buffer.
void setDefaultColor(ACG::Vec4f _defaultColor)
Sets the default color.
void enableVertexOnCellPrimitives()
Enables vertex primitives that are drawn on the cells.
void enableHalfedgePrimitives()
Enables per halfedge primitives.
GLuint getPickBuffer(ACG::GLState &_state, unsigned int _offset)
Returns the name of the pick buffer.
unsigned char getTexCoordOffset()
Returns the texcoord's offset within the buffer.
void enablePerVertexColors()
Enables per vertex colors.
void calculateVertexDeclaration()
Constructs a VertexDeclaration, the size and the offsets for the vertices stored in the buffer...
void addNormalToBuffer(ACG::Vec3d _normal, unsigned char *_buffer, unsigned int _offset)
Adds a normal to the buffer.
void enablePerCellColors()
Enables per cell colors.
void addColorToBuffer(ACG::Vec4uc _color, unsigned char *_buffer, unsigned int _offset)
Adds a color to the buffer.
void buildVertexBuffer(unsigned char *_buffer)
Adds all vertices to the buffer.
void saveOptions()
State that the current buffer was built with the current options.
void enablePerHalffaceColors()
Enables per halfface colors.
void countNumOfVertices()
Counts the number of vertices that need to be stored in the buffer.
void calculateCellInsideness()
Calculates for all cells whether they are inside w.r.t. all cut planes.
bool optionsChanged()
Tests whether the options were changed since the last time building the buffer.
void enableHalffacePrimitives()
Enables halfface primitves.
void buildTexCoordBuffer(unsigned char *_buffer)
Adds texture coordinates to the buffer.
void addTexCoordToBuffer(ACG::Vec2f _texCoord, unsigned char *_buffer, unsigned int _offset)
Adds a texture coordnate to the buffer.
unsigned char getStride()
Returns the vertex stride.
void disableTextures()
Disables textures.
unsigned int getNumOfVertices()
Returns the number of vertices stored in the buffer.
unsigned char getColorOffset()
Returns the color's offset within the buffer.
void setSelectionOnly(bool _selectionOnly)
Set whether only selected primitves should be added to the buffer.
Class to define the vertex input layout.
void invalidateTexCoords()
Invalidates texture coordinates.
void enableHalffaceOnCellPrimitives()
Enables halfface primitives which are drawn on the cells.
void addFloatToBuffer(float _value, unsigned char *&_buffer)
Adds a float to the buffer.
void invalidateGeometry()
Invalidates geometry.
void enablePerHalfedgeColors()
Enables per halfedge colors.
bool selectionOnly()
Checks whether only selected primitives should be added to the buffer.
void disableColors()
Disables colors.
bool colorsNeedRebuild()
Checks whether colors need to be rebuild.
void disableNormals()
Disables normals.
void enableFaceOnCellPrimitives()
Enables face primitives which are drawn on the cells.
void enablePerEdgeColors()
Enables per edge colors.
bool normalsNeedRebuild()
Checks whether normals need to be rebuild.
void enableTextureSingle()
Enables textures.
double getScale() const
Return the scale used to shrink cells.
bool is_inside(const ACG::Vec3d &_p)
Tests whether the given point is inside w.r.t. all cut planes.
unsigned char getNormalOffset()
Returns the normal's offset within the buffer.
This class provides easy access to DrawModes supported by OpenVolumeMesh.
void buildNormalBuffer(unsigned char *_buffer)
Adds all normals to the buffer.
ACG::Vec3d getCOG(OpenVolumeMesh::CellHandle _ch)
Returns the center of gravity of the given cell.