VolumeMeshBufferManager.hh 17.1 KB
Newer Older
1
/*===========================================================================*\
2 3
 *                                                                           *
 *                              OpenFlipper                                  *
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 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.   *
 *                                                                           *
 * 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.               *
 *                                                                           *
 * 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.              *
39
 *                                                                           *
40 41
\*===========================================================================*/

42
#pragma once
43 44 45 46 47 48 49 50 51 52 53 54

#include <ACG/GL/GLState.hh>
#include <ACG/Scenegraph/DrawModes.hh>
#include <ACG/Scenegraph/BaseNode.hh>
#include <ACG/GL/VertexDeclaration.hh>

#include "VolumeMeshDrawModesContainer.hh"

#include <OpenVolumeMesh/Attribs/StatusAttrib.hh>
#include <OpenVolumeMesh/Attribs/NormalAttrib.hh>
#include <OpenVolumeMesh/Attribs/ColorAttrib.hh>
#include <OpenVolumeMesh/Attribs/TexCoordAttrib.hh>
55
#include <ACG/Geometry/Types/PlaneType.hh>
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

/*! \class VolumeMeshBufferManager
 *  \brief This class creates buffers that can be used to render open volume meshs.
 */
template<class VolumeMesh>
class VolumeMeshBufferManager
{
    // typedefs for easy access
    typedef OpenVolumeMesh::VertexHandle VertexHandle;
    typedef OpenVolumeMesh::EdgeHandle EdgeHandle;
    typedef OpenVolumeMesh::HalfEdgeHandle HalfEdgeHandle;
    typedef OpenVolumeMesh::FaceHandle FaceHandle;
    typedef OpenVolumeMesh::HalfFaceHandle HalfFaceHandle;
    typedef OpenVolumeMesh::CellHandle CellHandle;

    typedef typename VolumeMesh::PointT Vertex;
    typedef OpenVolumeMesh::OpenVolumeMeshEdge Edge;
    typedef OpenVolumeMesh::OpenVolumeMeshFace Face;
    typedef OpenVolumeMesh::OpenVolumeMeshCell Cell;

76 77
    using Plane = ACG::Geometry::Plane;

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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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 203 204 205 206 207 208 209 210 211 212 213 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 255 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
public:

    enum PrimitiveMode
    {
        PM_NONE,
        PM_CELLS,
        PM_FACES,
        PM_FACES_ON_CELLS,
        PM_HALFFACES,
        PM_HALFFACES_ON_CELLS,
        PM_EDGES,
        PM_IRREGULAR_EDGES,
        PM_EDGES_ON_CELLS,
        PM_HALFEDGES,
        PM_VERTICES,
        PM_VERTICES_ON_CELLS
    };

    enum NormalMode
    {
        NM_NONE,
        NM_FACE,
        NM_HALFFACE,
        NM_VERTEX
    };

    enum ColorMode
    {
        CM_NO_COLORS,
        CM_CELL,
        CM_FACE,
        CM_HALFFACE,
        CM_EDGE,
        CM_HALFEDGE,
        CM_VERTEX,
        CM_PICK
    };

    enum TexCoordMode
    {
        TCM_NONE,
        TCM_SINGLE_2D
    };

    VolumeMeshBufferManager(const VolumeMesh &mesh_, OpenVolumeMesh::StatusAttrib& statusAttrib_,
                                                     OpenVolumeMesh::ColorAttrib<ACG::Vec4f>& colorAttrib_,
                                                     OpenVolumeMesh::NormalAttrib<VolumeMesh>& normalAttrib_,
                                                     OpenVolumeMesh::TexCoordAttrib<ACG::Vec2f>& texcoordAttrib_);
    virtual ~VolumeMeshBufferManager(){ free(); }


    /// Sets the scale used to shrink cells.
    void setScale(double _scale) { mScale = _scale; invalidate();}

    /// Return the scale used to shrink cells.
    double getScale() const     { return mScale;  }


    /// Returns the vertex stride
    unsigned char getStride()       { return mVertexSize; }

    /// Returns the color's offset within the buffer
    unsigned char getColorOffset()  { return mColorOffset;  }

    /// Returns the normal's offset within the buffer
    unsigned char getNormalOffset() { return mNormalOffset; }

    /// Returns the texcoord's offset within the buffer
    unsigned char getTexCoordOffset() { return mTexCoordOffset; }


    /// Returns a VertexDeclaration for the vertices stored in the buffer
    const ACG::VertexDeclaration* getVertexDeclaration() { return &mVertexDeclaration; }


    /// Returns the name of the buffer
    GLuint getBuffer();

    /// Returns the name of the pick buffer
    GLuint getPickBuffer(ACG::GLState &_state, unsigned int _offset);


    /// Invalidates the buffer
    void invalidate();

    /// Invalidates geometry
    void invalidateGeometry();

    /// Invalidates colors
    void invalidateColors();

    /// Invalidates normals
    void invalidateNormals();

    /// Invalidates texture coordinates
    void invalidateTexCoords();


    /// Deletes the buffers on the GPU
    void free();


    /// Returns the number of vertices stored in the buffer
    unsigned int getNumOfVertices();


    /// Sets the default color
    void setDefaultColor(ACG::Vec4f _defaultColor);


    /// Configures the buffer manager's options from a DrawMode
    void setOptionsFromDrawMode(ACG::SceneGraph::DrawModes::DrawMode _drawMode);


    /// Disables colors
    void disableColors()            { mColorMode  = CM_NO_COLORS;     }

    /// Enables per cell colors
    void enablePerCellColors()      { mColorMode  = CM_CELL;     }

    /// Enables per face colors
    void enablePerFaceColors()      { mColorMode  = CM_FACE;     }

    /// Enables per halfface colors
    void enablePerHalffaceColors()  { mColorMode  = CM_HALFFACE; }

    /// Enables per edge colors
    void enablePerEdgeColors()      { mColorMode  = CM_EDGE;     }

    /// Enables per halfedge colors
    void enablePerHalfedgeColors()  { mColorMode  = CM_HALFEDGE; }

    /// Enables per vertex colors
    void enablePerVertexColors()    { mColorMode  = CM_VERTEX;   }

    /// Enables picking colors
    void enablePickColors()         { mColorMode  = CM_PICK;     }


    /// Disables normals
    void disableNormals()           { mNormalMode = NM_NONE;     }

    /// Enables per face normals
    void enablePerFaceNormals()     { mNormalMode = NM_FACE;     }

    /// Enables per halfface normals
    void enablePerHalffaceNormals() { mNormalMode = NM_HALFFACE; }

    /// Enables per vertex normals
    void enablePerVertexNormals()   { mNormalMode = NM_VERTEX;   }


    /// Disables primitives (nothing will be drawn)
    void disablePrimitives()              { mPrimitiveMode = PM_NONE;               }

    /// Enables cell primitives
    void enableCellPrimitives()           { mPrimitiveMode = PM_CELLS;              }

    /// Enables face primitives
    void enableFacePrimitives()           { mPrimitiveMode = PM_FACES;              }

    /// Enables face primitives which are drawn on the cells
    void enableFaceOnCellPrimitives()     { mPrimitiveMode = PM_FACES_ON_CELLS;     }

    /// Enables halfface primitves
    void enableHalffacePrimitives()       { mPrimitiveMode = PM_HALFFACES;          }

    /// Enables halfface primitives which are drawn on the cells
    void enableHalffaceOnCellPrimitives() { mPrimitiveMode = PM_HALFFACES_ON_CELLS; }

    /// Enables edge primitves
    void enableEdgePrimitives()           { mPrimitiveMode = PM_EDGES;              }

    /// Enables edge primitives for irregular edges
    void enableIrregularEdgePrimitives()  { mPrimitiveMode = PM_IRREGULAR_EDGES;    }

    /// Enables edge primitives which are drawn on the cells
    void enableEdgeOnCellPrimitives()     { mPrimitiveMode = PM_EDGES_ON_CELLS;     }

    /// Enables per halfedge primitives
    void enableHalfedgePrimitives()       { mPrimitiveMode = PM_HALFEDGES;          }

    /// Enables vertex primitives
    void enableVertexPrimitives()         { mPrimitiveMode = PM_VERTICES;           }

    /// Enables vertex primitives that are drawn on the cells
    void enableVertexOnCellPrimitives()   { mPrimitiveMode = PM_VERTICES_ON_CELLS;  }


    /// Disables textures
    void disableTextures()                { mTexCoordMode = TCM_NONE;       }

    /// Enables textures
    void enableTextureSingle()            { mTexCoordMode = TCM_SINGLE_2D;  }


    /// Checks whether only selected primitives should be added to the buffer
    bool selectionOnly()                       { return mSkipUnselected;            }

    /// Set whether only selected primitves should be added to the buffer
    void setSelectionOnly(bool _selectionOnly) { mSkipUnselected = _selectionOnly;  }


    /// Removes all cut planes
    void clearCutPlanes();

    /// Adds a cut plane
285
    void addCutPlane(const ACG::Geometry::Plane& _p);
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

    /// Adds a cut plane
    void addCutPlane(const ACG::Vec3d& _p, const ACG::Vec3d& _n, const ACG::Vec3d& _xsize, const ACG::Vec3d& _ysize);


    /// Sets whether only the boundary primitves should be rendered
    void setBoundaryOnly(bool _boundaryOnly) { mBoundaryOnly = _boundaryOnly; }

private:
    /// Tests whether the given point is inside w.r.t. all cut planes
    bool is_inside(const ACG::Vec3d& _p);

    /// Tests whether the given vertex is inside w.r.t. all cut planes
    bool is_inside(const VertexHandle& _vh);

    /// Tests whether the given halfedge is inside w.r.t. all cut planes
    bool is_inside(const HalfEdgeHandle& _heh);

    /// Tests whether the given edge is inside w.r.t. all cut planes
    bool is_inside(const EdgeHandle& _eh);

    /// Tests whether the given halfface is inside w.r.t. all cut planes
    bool is_inside(const HalfFaceHandle& _hfh);

    /// Tests whether the given face is inside w.r.t. all cut planes
    bool is_inside(const FaceHandle& _fh);

    /// Tests whether the given cell is inside w.r.t. all cut planes
Martin Heistermann's avatar
Martin Heistermann committed
314
    bool is_inside(const CellHandle& _ch);
315 316 317 318

    /// Calculates for all cells whether they are inside w.r.t. all cut planes
    void calculateCellInsideness();

319 320 321 322 323
    bool should_render(const VertexHandle& _vh);
    bool should_render(const HalfEdgeHandle& _heh);
    bool should_render(const EdgeHandle& _eh);
    bool should_render(const HalfFaceHandle& _hfh);
    bool should_render(const FaceHandle& _fh);
324 325
    bool should_render(const CellHandle& _ch);

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 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

    /// Tests whether the options were changed since the last time building the buffer
    bool optionsChanged();

    /// State that the current buffer was built with the current options
    void saveOptions();


    /// Adds a float to the buffer
    void addFloatToBuffer( float _value, unsigned char *&_buffer );

    /// Adds an unsigned char to the buffer
    void addUCharToBuffer( unsigned char _value, unsigned char *&_buffer );

    /// Adds a position to the buffer
    void addPositionToBuffer(ACG::Vec3d _position, unsigned char* _buffer , unsigned int _offset);

    /// Adds a color to the buffer
    void addColorToBuffer(ACG::Vec4uc _color, unsigned char* _buffer, unsigned int _offset);

    /// Adds a color to the buffer
    void addColorToBuffer(ACG::Vec4f _color, unsigned char* _buffer, unsigned int _offset);

    /// Adds a normal to the buffer
    void addNormalToBuffer(ACG::Vec3d _normal, unsigned char *_buffer, unsigned int _offset);

    /// Adds a texture coordnate to the buffer
    void addTexCoordToBuffer(ACG::Vec2f _texCoord, unsigned char* _buffer, unsigned int _offset);


    /// Constructs a VertexDeclaration, the size and the offsets for the vertices stored in the buffer
    void calculateVertexDeclaration();


    /// Adds all vertices to the buffer
    void buildVertexBuffer(unsigned char* _buffer);

    /// Adds all normals to the buffer
    void buildNormalBuffer(unsigned char* _buffer);

    /// Adds all colors to the buffer
    void buildColorBuffer(unsigned char* _buffer);

    /// Adds texture coordinates to the buffer
    void buildTexCoordBuffer(unsigned char* _buffer);

    /// Adds all picking colors to the buffer
    void buildPickColorBuffer(ACG::GLState &_state, unsigned int _offset, unsigned char* _buffer);


    /// Returns a color code for irregular edges
    ACG::Vec4f getValenceColorCode(unsigned int _valence, bool _inner) const;


    /// Counts the number of vertices that need to be stored in the buffer
    void countNumOfVertices();


    /// Returns the number of cells that are incident to the given face and also inside w.r.t. all cut planes
    int getNumOfIncidentCells(OpenVolumeMesh::FaceHandle _fh);

    /// Returns the number of cells that are incident to the given edge and also inside w.r.t. all cut planes
    int getNumOfIncidentCells(OpenVolumeMesh::EdgeHandle _eh);

    /// Returns the number of cells that are incident to the given vertex and also inside w.r.t. all cut planes
    int getNumOfIncidentCells(OpenVolumeMesh::VertexHandle _vh);

    /// Returns the center of gravity of the given cell
    ACG::Vec3d getCOG(OpenVolumeMesh::CellHandle _ch);

    /// Calculates the center of gravity for all cells
    void calculateCOGs();

    /// Checks whether positions need to be rebuild
    bool positionsNeedRebuild();

    /// Checks whether colors need to be rebuild
    bool colorsNeedRebuild();

    /// Checks whether texture coordinates need to be rebuild
    bool texCoordsNeedRebuild();

    /// Checks whether normals need to be rebuild
    bool normalsNeedRebuild();

private:
    ACG::Vec4f mDefaultColor;

    const VolumeMesh& mMesh;
    OpenVolumeMesh::StatusAttrib& mStatusAttrib;
    OpenVolumeMesh::ColorAttrib<ACG::Vec4f>& mColorAttrib;
    OpenVolumeMesh::NormalAttrib<VolumeMesh>& mNormalAttrib;
    OpenVolumeMesh::TexCoordAttrib<ACG::Vec2f>& mTexcoordAttrib;

    int mNumOfVertices;
    int mCurrentNumOfVertices;

    unsigned int mVertexSize;
    ACG::VertexDeclaration mVertexDeclaration;

    int mColorOffset;
    int mNormalOffset;

    double mScale;

    GLuint mBuffer;

    // local picking offset ?
    unsigned int mCurrentPickOffset;

    // global picking offset
    unsigned int mGlobalPickOffset;

    bool mInvalidated;
    bool mGeometryChanged;
    bool mNormalsChanged;
    bool mColorsChanged;
    bool mTexCoordsChanged;

    //draw modes
    VolumeMeshDrawModesContainer mDrawModes;

    //options
    PrimitiveMode mPrimitiveMode;
    NormalMode mNormalMode;
    ColorMode mColorMode;

    bool mSkipUnselected;
    bool mShowIrregularInnerEdges;
    bool mShowIrregularOuterValence2Edges;
    bool mSkipRegularEdges;
    bool mBoundaryOnly;

    PrimitiveMode mCurrentPrimitiveMode;
    NormalMode mCurrentNormalMode;
    ColorMode mCurrentColorMode;

    bool mCurrentSkipUnselected;
    bool mCurrentShowIrregularInnerEdges;
    bool mCurrentShowIrregularOuterValence2Edges;
    bool mCurrentSkipRegularEdges;
    bool mCurrentBoundaryOnly;

    unsigned char mCurrentVertexSize;
    unsigned char mCurrentNormalOffset;
    unsigned char mCurrentColorOffset;

473
    std::vector<ACG::Geometry::Plane> cut_planes_;
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

    std::vector<ACG::Vec3d> mCogs;
    bool mCogsValid;
    std::vector<bool> mCellInsideness;
    bool mCellInsidenessValid;

    TexCoordMode mTexCoordMode;
    TexCoordMode mCurrentTexCoordMode;
    unsigned char mTexCoordOffset;
    unsigned char mCurrentTexCoordOffset;

};

//=============================================================================
#if defined(INCLUDE_TEMPLATES) && !defined(VOLUMEMESHBUFFERMANAGERT_CC)
489
#include "VolumeMeshBufferManagerT_impl.hh"
490 491
#endif
//=============================================================================