TriangleBSPCoreT.hh 7.01 KB
Newer Older
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
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 43 44 45 46 47 48
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision: 13338 $                                                       *
*   $LastChangedBy: moebius $                                                *
*   $Date: 2012-01-12 10:15:45 +0100 (Thu, 12 Jan 2012) $                     *
*                                                                            *
\*===========================================================================*/
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65




//=============================================================================
//
//  CLASS TriangleBSPCoreT
//
//=============================================================================

#ifndef TRIANGLEBSPCORET_HH
#define TRIANGLEBSPCORET_HH


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


66
#include <vector>
67 68
#include <ACG/Geometry/Types/PlaneT.hh>
#include <OpenMesh/Core/Geometry/VectorT.hh>
69 70

#include "TriangleBSPT.hh"
71 72 73 74 75 76 77 78 79 80 81 82 83


//== CLASS DEFINITION =========================================================


template <class BSPTraits>
class TriangleBSPCoreT
{
public: //---------------------------------------------------------------------

  typedef BSPTraits                      Traits;
  typedef typename BSPTraits::Point      Point;
  typedef typename BSPTraits::Handle     Handle;
84
  typedef typename BSPTraits::Node	     Node;
85 86 87 88 89 90 91 92 93 94 95
  typedef typename Point::value_type     Scalar;
  typedef ACG::Geometry::PlaneT<Scalar>  Plane;
  typedef std::vector<Handle>            Handles;
  typedef typename Handles::iterator     HandleIter;


public: //---------------------------------------------------------------------


  /** Constructor: need traits that define the types and 
      give us the points by traits_.point(PointHandle) */
96
  TriangleBSPCoreT(const BSPTraits& _traits) : traits_(_traits), root_(0), nodes(0), n_triangles(0) {}
97 98

  /// Destructor
99 100 101
  ~TriangleBSPCoreT() {
      delete root_;
  }
102 103 104


  /// Reserve memory for _n entries
Jan Möbius's avatar
Jan Möbius committed
105
  void reserve(size_t _n) { handles_.reserve(_n); }
106
  /// Add a handle to the BSP
107 108 109 110 111 112 113 114 115 116 117
  void push_back(Handle _h)     { handles_.push_back(_h); ++n_triangles; }

  /**
   * @return size() == 0
   */
  bool empty()     { return n_triangles == 0; }

  /**
   * @return The number of triangles contained in the tree.
   */
  size_t size()     { return n_triangles; }
118 119 120 121

  /** Build the tree.
   *
   * @param _max_handles Maximum number of vertices per leaf.
122
   * @param _max_depth   Maximum depth.
123
   */
124
  void build(unsigned int _max_handles, unsigned int _max_depth);
125

126 127 128 129 130 131
    /** \brief Create a PolyMesh object that visualizes the bounding boxes of the BSP tree
     *
     * @param _object     The output mesh which the tree will be written into
     * @param _max_depth  The maximal depth that will be visualized
     */
  template <typename MeshT>
Jan Möbius's avatar
cleanup  
Jan Möbius committed
132 133 134 135 136
  void visualizeTree(MeshT *_object, int _max_depth)
  {
    root_->visualizeTree(_object, _max_depth-1);
    _object->update_normals();
  }
137

138 139 140 141 142 143 144
private:
  /*
   * Noncopyable because of root_.
   */
  TriangleBSPCoreT(const TriangleBSPCoreT &rhs);
  TriangleBSPCoreT &operator=(const TriangleBSPCoreT &rhs);

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

private: //---------------------------------------------------------------------


  // Recursive part of build()
  void _build(Node*        _node,
	      unsigned int _max_handles, 
	      unsigned int _depth);




protected: //-------------------------------------------------------------------


  BSPTraits  traits_;
  Handles    handles_;
  Node*      root_;
163
  int	       nodes, n_triangles;
164
  
165 166 167 168 169 170 171 172 173 174 175
};


//=============================================================================
#if defined(OM_INCLUDE_TEMPLATES) && !defined(TRIANGLEBSPCORET_C)
#  define TRIANGLEBSPCORET_TEMPLATES
#  include "TriangleBSPCoreT.cc"
#endif
//=============================================================================
#endif // TRIANGLEBSPCORET_HH defined
//=============================================================================