Developer Documentation
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 #pragma once
43 
44 #include <ACG/GL/GLState.hh>
45 #include <ACG/Scenegraph/DrawModes.hh>
46 #include <ACG/Scenegraph/BaseNode.hh>
47 #include <ACG/GL/VertexDeclaration.hh>
48 
49 #include "VolumeMeshDrawModesContainer.hh"
50 
51 #include <OpenVolumeMesh/Attribs/StatusAttrib.hh>
52 #include <OpenVolumeMesh/Attribs/NormalAttrib.hh>
53 #include <OpenVolumeMesh/Attribs/ColorAttrib.hh>
54 #include <OpenVolumeMesh/Attribs/TexCoordAttrib.hh>
55 #include <ACG/Geometry/Types/PlaneType.hh>
56 
60 template<class VolumeMesh>
62 {
63  // typedefs for easy access
70 
71  typedef typename VolumeMesh::PointT Vertex;
75 
77 
78 public:
79 
80  enum PrimitiveMode
81  {
82  PM_NONE,
83  PM_CELLS,
84  PM_FACES,
85  PM_FACES_ON_CELLS,
86  PM_HALFFACES,
87  PM_HALFFACES_ON_CELLS,
88  PM_EDGES,
89  PM_IRREGULAR_EDGES,
90  PM_EDGES_ON_CELLS,
91  PM_HALFEDGES,
92  PM_VERTICES,
93  PM_VERTICES_ON_CELLS
94  };
95 
96  enum NormalMode
97  {
98  NM_NONE,
99  NM_FACE,
100  NM_HALFFACE,
101  NM_VERTEX
102  };
103 
104  enum ColorMode
105  {
106  CM_NO_COLORS,
107  CM_CELL,
108  CM_FACE,
109  CM_HALFFACE,
110  CM_EDGE,
111  CM_HALFEDGE,
112  CM_VERTEX,
113  CM_PICK
114  };
115 
116  enum TexCoordMode
117  {
118  TCM_NONE,
119  TCM_SINGLE_2D
120  };
121 
122  VolumeMeshBufferManager(const VolumeMesh &mesh_, OpenVolumeMesh::StatusAttrib& statusAttrib_,
126  virtual ~VolumeMeshBufferManager(){ free(); }
127 
128 
130  void setScale(double _scale) { mScale = _scale; invalidate();}
131 
133  double getScale() const { return mScale; }
134 
135 
137  unsigned char getStride() { return mVertexSize; }
138 
140  unsigned char getColorOffset() { return mColorOffset; }
141 
143  unsigned char getNormalOffset() { return mNormalOffset; }
144 
146  unsigned char getTexCoordOffset() { return mTexCoordOffset; }
147 
148 
150  const ACG::VertexDeclaration* getVertexDeclaration() { return &mVertexDeclaration; }
151 
152 
154  GLuint getBuffer();
155 
157  GLuint getPickBuffer(ACG::GLState &_state, unsigned int _offset);
158 
159 
161  void invalidate();
162 
164  void invalidateGeometry();
165 
167  void invalidateColors();
168 
170  void invalidateNormals();
171 
173  void invalidateTexCoords();
174 
175 
177  void free();
178 
179 
181  unsigned int getNumOfVertices();
182 
183 
185  void setDefaultColor(ACG::Vec4f _defaultColor);
186 
187 
190 
191 
193  void disableColors() { mColorMode = CM_NO_COLORS; }
194 
196  void enablePerCellColors() { mColorMode = CM_CELL; }
197 
199  void enablePerFaceColors() { mColorMode = CM_FACE; }
200 
202  void enablePerHalffaceColors() { mColorMode = CM_HALFFACE; }
203 
205  void enablePerEdgeColors() { mColorMode = CM_EDGE; }
206 
208  void enablePerHalfedgeColors() { mColorMode = CM_HALFEDGE; }
209 
211  void enablePerVertexColors() { mColorMode = CM_VERTEX; }
212 
214  void enablePickColors() { mColorMode = CM_PICK; }
215 
216 
218  void disableNormals() { mNormalMode = NM_NONE; }
219 
221  void enablePerFaceNormals() { mNormalMode = NM_FACE; }
222 
224  void enablePerHalffaceNormals() { mNormalMode = NM_HALFFACE; }
225 
227  void enablePerVertexNormals() { mNormalMode = NM_VERTEX; }
228 
229 
231  void disablePrimitives() { mPrimitiveMode = PM_NONE; }
232 
234  void enableCellPrimitives() { mPrimitiveMode = PM_CELLS; }
235 
237  void enableFacePrimitives() { mPrimitiveMode = PM_FACES; }
238 
240  void enableFaceOnCellPrimitives() { mPrimitiveMode = PM_FACES_ON_CELLS; }
241 
243  void enableHalffacePrimitives() { mPrimitiveMode = PM_HALFFACES; }
244 
246  void enableHalffaceOnCellPrimitives() { mPrimitiveMode = PM_HALFFACES_ON_CELLS; }
247 
249  void enableEdgePrimitives() { mPrimitiveMode = PM_EDGES; }
250 
252  void enableIrregularEdgePrimitives() { mPrimitiveMode = PM_IRREGULAR_EDGES; }
253 
255  void enableEdgeOnCellPrimitives() { mPrimitiveMode = PM_EDGES_ON_CELLS; }
256 
258  void enableHalfedgePrimitives() { mPrimitiveMode = PM_HALFEDGES; }
259 
261  void enableVertexPrimitives() { mPrimitiveMode = PM_VERTICES; }
262 
264  void enableVertexOnCellPrimitives() { mPrimitiveMode = PM_VERTICES_ON_CELLS; }
265 
266 
268  void disableTextures() { mTexCoordMode = TCM_NONE; }
269 
271  void enableTextureSingle() { mTexCoordMode = TCM_SINGLE_2D; }
272 
273 
275  bool selectionOnly() { return mSkipUnselected; }
276 
278  void setSelectionOnly(bool _selectionOnly) { mSkipUnselected = _selectionOnly; }
279 
280 
282  void clearCutPlanes();
283 
285  void addCutPlane(const ACG::Geometry::Plane& _p);
286 
288  void addCutPlane(const ACG::Vec3d& _p, const ACG::Vec3d& _n, const ACG::Vec3d& _xsize, const ACG::Vec3d& _ysize);
289 
290 
292  void setBoundaryOnly(bool _boundaryOnly) { mBoundaryOnly = _boundaryOnly; }
293 
294 private:
296  bool is_inside(const ACG::Vec3d& _p);
297 
299  bool is_inside(const VertexHandle& _vh);
300 
302  bool is_inside(const HalfEdgeHandle& _heh);
303 
305  bool is_inside(const EdgeHandle& _eh);
306 
308  bool is_inside(const HalfFaceHandle& _hfh);
309 
311  bool is_inside(const FaceHandle& _fh);
312 
314  bool is_inside(const CellHandle& _ch);
315 
318 
319 
321  bool optionsChanged();
322 
324  void saveOptions();
325 
326 
328  void addFloatToBuffer( float _value, unsigned char *&_buffer );
329 
331  void addUCharToBuffer( unsigned char _value, unsigned char *&_buffer );
332 
334  void addPositionToBuffer(ACG::Vec3d _position, unsigned char* _buffer , unsigned int _offset);
335 
337  void addColorToBuffer(ACG::Vec4uc _color, unsigned char* _buffer, unsigned int _offset);
338 
340  void addColorToBuffer(ACG::Vec4f _color, unsigned char* _buffer, unsigned int _offset);
341 
343  void addNormalToBuffer(ACG::Vec3d _normal, unsigned char *_buffer, unsigned int _offset);
344 
346  void addTexCoordToBuffer(ACG::Vec2f _texCoord, unsigned char* _buffer, unsigned int _offset);
347 
348 
351 
352 
354  void buildVertexBuffer(unsigned char* _buffer);
355 
357  void buildNormalBuffer(unsigned char* _buffer);
358 
360  void buildColorBuffer(unsigned char* _buffer);
361 
363  void buildTexCoordBuffer(unsigned char* _buffer);
364 
366  void buildPickColorBuffer(ACG::GLState &_state, unsigned int _offset, unsigned char* _buffer);
367 
368 
370  ACG::Vec4f getValenceColorCode(unsigned int _valence, bool _inner) const;
371 
372 
374  void countNumOfVertices();
375 
376 
379 
382 
385 
388 
390  void calculateCOGs();
391 
393  bool positionsNeedRebuild();
394 
396  bool colorsNeedRebuild();
397 
399  bool texCoordsNeedRebuild();
400 
402  bool normalsNeedRebuild();
403 
404 private:
405  ACG::Vec4f mDefaultColor;
406 
407  const VolumeMesh& mMesh;
408  OpenVolumeMesh::StatusAttrib& mStatusAttrib;
412 
413  int mNumOfVertices;
414  int mCurrentNumOfVertices;
415 
416  unsigned int mVertexSize;
417  ACG::VertexDeclaration mVertexDeclaration;
418 
419  int mColorOffset;
420  int mNormalOffset;
421 
422  double mScale;
423 
424  GLuint mBuffer;
425 
426  // local picking offset ?
427  unsigned int mCurrentPickOffset;
428 
429  // global picking offset
430  unsigned int mGlobalPickOffset;
431 
432  bool mInvalidated;
433  bool mGeometryChanged;
434  bool mNormalsChanged;
435  bool mColorsChanged;
436  bool mTexCoordsChanged;
437 
438  //draw modes
439  VolumeMeshDrawModesContainer mDrawModes;
440 
441  //options
442  PrimitiveMode mPrimitiveMode;
443  NormalMode mNormalMode;
444  ColorMode mColorMode;
445 
446  bool mSkipUnselected;
447  bool mShowIrregularInnerEdges;
448  bool mShowIrregularOuterValence2Edges;
449  bool mSkipRegularEdges;
450  bool mBoundaryOnly;
451 
452  PrimitiveMode mCurrentPrimitiveMode;
453  NormalMode mCurrentNormalMode;
454  ColorMode mCurrentColorMode;
455 
456  bool mCurrentSkipUnselected;
457  bool mCurrentShowIrregularInnerEdges;
458  bool mCurrentShowIrregularOuterValence2Edges;
459  bool mCurrentSkipRegularEdges;
460  bool mCurrentBoundaryOnly;
461 
462  unsigned char mCurrentVertexSize;
463  unsigned char mCurrentNormalOffset;
464  unsigned char mCurrentColorOffset;
465 
466  std::vector<ACG::Geometry::Plane> cut_planes_;
467 
468  std::vector<ACG::Vec3d> mCogs;
469  bool mCogsValid;
470  std::vector<bool> mCellInsideness;
471  bool mCellInsidenessValid;
472 
473  TexCoordMode mTexCoordMode;
474  TexCoordMode mCurrentTexCoordMode;
475  unsigned char mTexCoordOffset;
476  unsigned char mCurrentTexCoordOffset;
477 
478 };
479 
480 //=============================================================================
481 #if defined(INCLUDE_TEMPLATES) && !defined(VOLUMEMESHBUFFERMANAGERT_CC)
482 #include "VolumeMeshBufferManagerT_impl.hh"
483 #endif
484 //=============================================================================
void saveOptions()
State that the current buffer was built with the current options.
void enableEdgePrimitives()
Enables edge primitves.
void enableHalffacePrimitives()
Enables halfface primitves.
Class to define the vertex input layout.
unsigned char getNormalOffset()
Returns the normal&#39;s offset within the buffer.
void addPositionToBuffer(ACG::Vec3d _position, unsigned char *_buffer, unsigned int _offset)
Adds a position to the buffer.
void calculateCellInsideness()
Calculates for all cells whether they are inside w.r.t. all cut planes.
void enablePerCellColors()
Enables per cell colors.
bool is_inside(const ACG::Vec3d &_p)
Tests whether the given point is inside w.r.t. all cut planes.
void setScale(double _scale)
Sets the scale used to shrink cells.
ACG::Vec4f getValenceColorCode(unsigned int _valence, bool _inner) const
Returns a color code for irregular edges.
GLuint getPickBuffer(ACG::GLState &_state, unsigned int _offset)
Returns the name of the pick buffer.
void buildTexCoordBuffer(unsigned char *_buffer)
Adds texture coordinates to the buffer.
void disableColors()
Disables colors.
void enablePerFaceNormals()
Enables per face normals.
void enableHalfedgePrimitives()
Enables per halfedge primitives.
void invalidateTexCoords()
Invalidates texture coordinates.
void setSelectionOnly(bool _selectionOnly)
Set whether only selected primitves should be added to the buffer.
void addNormalToBuffer(ACG::Vec3d _normal, unsigned char *_buffer, unsigned int _offset)
Adds a normal to the buffer.
void clearCutPlanes()
Removes all cut planes.
void setDefaultColor(ACG::Vec4f _defaultColor)
Sets the default color.
unsigned char getTexCoordOffset()
Returns the texcoord&#39;s offset within the buffer.
void invalidate()
Invalidates the buffer.
void enablePerHalffaceNormals()
Enables per halfface normals.
This class creates buffers that can be used to render open volume meshs.
void addFloatToBuffer(float _value, unsigned char *&_buffer)
Adds a float to the buffer.
void enablePerVertexNormals()
Enables per vertex normals.
const ACG::VertexDeclaration * getVertexDeclaration()
Returns a VertexDeclaration for the vertices stored in the buffer.
bool texCoordsNeedRebuild()
Checks whether texture coordinates need to be rebuild.
unsigned int getNumOfVertices()
Returns the number of vertices stored in the buffer.
void disableTextures()
Disables textures.
unsigned char getColorOffset()
Returns the color&#39;s offset within the buffer.
void free()
Deletes the buffers on the GPU.
void enableEdgeOnCellPrimitives()
Enables edge primitives which are drawn on the cells.
void buildVertexBuffer(unsigned char *_buffer)
Adds all vertices to the buffer.
void disableNormals()
Disables normals.
bool colorsNeedRebuild()
Checks whether colors need to be rebuild.
void enablePerEdgeColors()
Enables per edge colors.
void enablePerHalfedgeColors()
Enables per halfedge colors.
void enableVertexPrimitives()
Enables vertex primitives.
double getScale() const
Return the scale used to shrink cells.
void enableFacePrimitives()
Enables face primitives.
void addUCharToBuffer(unsigned char _value, unsigned char *&_buffer)
Adds an unsigned char to the buffer.
void enableVertexOnCellPrimitives()
Enables vertex primitives that are drawn on the cells.
void disablePrimitives()
Disables primitives (nothing will be drawn)
void enablePerHalffaceColors()
Enables per halfface colors.
bool optionsChanged()
Tests whether the options were changed since the last time building the buffer.
void buildColorBuffer(unsigned char *_buffer)
Adds all colors to the buffer.
void addTexCoordToBuffer(ACG::Vec2f _texCoord, unsigned char *_buffer, unsigned int _offset)
Adds a texture coordnate to the buffer.
void enablePerFaceColors()
Enables per face colors.
void invalidateNormals()
Invalidates normals.
bool selectionOnly()
Checks whether only selected primitives should be added to the buffer.
void setBoundaryOnly(bool _boundaryOnly)
Sets whether only the boundary primitves should be rendered.
void setOptionsFromDrawMode(ACG::SceneGraph::DrawModes::DrawMode _drawMode)
Configures the buffer manager&#39;s options from a DrawMode.
void countNumOfVertices()
Counts the number of vertices that need to be stored in the buffer.
void addColorToBuffer(ACG::Vec4uc _color, unsigned char *_buffer, unsigned int _offset)
Adds a color to the buffer.
void enableTextureSingle()
Enables textures.
void enableHalffaceOnCellPrimitives()
Enables halfface primitives which are drawn on the cells.
This class provides easy access to DrawModes supported by OpenVolumeMesh.
bool positionsNeedRebuild()
Checks whether positions need to be rebuild.
void invalidateGeometry()
Invalidates geometry.
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 buildNormalBuffer(unsigned char *_buffer)
Adds all normals to the buffer.
bool normalsNeedRebuild()
Checks whether normals need to be rebuild.
unsigned char getStride()
Returns the vertex stride.
ACG::Vec3d getCOG(OpenVolumeMesh::CellHandle _ch)
Returns the center of gravity of the given cell.
void enableIrregularEdgePrimitives()
Enables edge primitives for irregular edges.
void addCutPlane(const ACG::Geometry::Plane &_p)
Adds a cut plane.
void enablePickColors()
Enables picking colors.
void calculateVertexDeclaration()
Constructs a VertexDeclaration, the size and the offsets for the vertices stored in the buffer...
void enableCellPrimitives()
Enables cell primitives.
void invalidateColors()
Invalidates colors.
void enableFaceOnCellPrimitives()
Enables face primitives which are drawn on the cells.
void enablePerVertexColors()
Enables per vertex colors.
GLuint getBuffer()
Returns the name of the buffer.
void calculateCOGs()
Calculates the center of gravity for all cells.
void buildPickColorBuffer(ACG::GLState &_state, unsigned int _offset, unsigned char *_buffer)
Adds all picking colors to the buffer.