marchingcube.h 10.2 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef MARCHINGCUBE_H
#define MARCHINGCUBE_H

#include "Algo/MC/image.h"
#include "Algo/MC/buffer.h"
#include "Algo/MC/tables.h"

Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
32
#include "Geometry/vector_gen.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
33 34 35 36 37 38 39

namespace CGoGN
{

namespace Algo
{

40 41 42
namespace MC
{

Pierre Kraemer's avatar
Pierre Kraemer committed
43 44 45 46 47 48 49 50 51 52
/**
 * Marching Cube
 *
 * @param DataType the type of voxel image
 * @param Windowing the windowing class which allow to distinguish inside from outside
 */
template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
class MarchingCube
{
protected:
53 54 55
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::MAP L_MAP ;
	typedef Dart L_DART ;
Pierre Kraemer's avatar
Pierre Kraemer committed
56

57 58 59 60 61 62 63
#ifdef MC_WIDTH_EDGE_Z_EMBEDED
	unsigned int m_zbound;
	unsigned int m_sliceGroup;
	EdgeAttribute<unsigned long long>* m_zslice;
	unsigned int m_currentZSlice;
#endif

Pierre Kraemer's avatar
Pierre Kraemer committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	/**
	* voxel image
	*/
	Image<DataType>* m_Image;

	/**
	 *  the windowing class that define inside from outside
	 */
	Windowing<DataType> m_windowFunc;

	/**
	* Pointer on buffer
	*/
	Buffer<L_DART>* m_Buffer;

	/**
	* map to store the mesh
	*/
	L_MAP* m_map;

84
	VertexAttribute<VEC3>& m_positions;
Pierre Kraemer's avatar
Pierre Kraemer committed
85 86 87 88

	/**
	* Origin of image
	*/
89
	VEC3 m_fOrigin;
Pierre Kraemer's avatar
Pierre Kraemer committed
90 91 92 93

	/**
	* scale of image
	*/
94
	VEC3 m_fScal;
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97 98 99 100

	/**
	* compute the index of a cube:\n
	* The index is a eight bit index, one bit for each vertex of the cube.\n
	* If the vertex is inside the objet (val < isovalue) the bit is set to 1, else to 0
	*/
101
	unsigned char computeIndex(const DataType* const _ucData) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
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

	/**
	 * tag boundary to b removed or not
	 */
	bool m_brem;

	/**
	* @name Creation of face
	* 8 different cases -> 8 functions
	*
	* create the faces of the current cube from index and coordinates of cubes in the eight different cases:
	* -# Z=0 Y=0 X=0
	* -# Z=0 Y=0 X=1..
	* -# Z=0 Y=1.. X=0
	* -# Z=0 Y=1.. X=1..
	* -# Z=1.. Y=0 X=0
	* -# Z=1.. Y=0 X=1..
	* -# Z=1.. Y=1.. X=0
	* -# Z=1.. Y=1.. X=1..
	*/
	//@{

	/**
	* create the faces of the current cube from index and coordinates
	* of cube.
	**************************************************************
	* @param 	vox pointer on current voxel
	* @param   _lX  coordinate X of the cube
	* @param   _lY  coordinate Y of the cube
	* @param   _lZ  coordinate Z of the cube
	* @param   tag  the boundary tag (NOT USE FOR THE MOMENT)
	*/
134 135 136 137 138 139 140 141
	void createFaces_1(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag);
	void createFaces_2(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag);
	void createFaces_3(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag);
	void createFaces_4(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag);
	void createFaces_5(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag);
	void createFaces_6(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag);
	void createFaces_7(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag);
	void createFaces_8(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag);
Pierre Kraemer's avatar
Pierre Kraemer committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
//@}

	/**
	* create the faces of the current cube from index, coordinates, list of vertex and  face mask
	*
	* @param   _ucCubeIndex the index of the cube
	* @param   _lX  coordinate X of the cube
	* @param   _lY  coordinate Y of the cube
	* @param   _lZ  coordinate Z of the cube
	* @param   _lVertTable list of index of created vertices
	* @param   _ucMask the mask which describes in whose neighbours cube we have to search neighbours faces
	* @param   curv curvature of face (NOT USE FOR THE MOMENT)
	* @param   tag the boundary tag value (NOT USE FOR THE MOMENT)
	*
	* @todo use the member (of struct HalfCube) number of faces instead of fill with -1
	*/
158
	void createLocalFaces(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int const *_lVertTable, const unsigned short _ucMask, float curv, unsigned char tag);
Pierre Kraemer's avatar
Pierre Kraemer committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

	/**
	* @name create vertices on edges of cube
	* 12 edges -> 12 functions
	*/
//@{
	/**
	* create the faces of the current cube from index, coordinates, list of vertex and  face mask
	*
	* @param   _ucCubeIndex the index of the cube
	* @param   _lX  coordinate X of the cube
	* @param   _lY  coordinate Y of the cube
	* @param   _lZ  coordinate Z of the cube
	* @param   lVertTable list of index of created vertices
	* @param   vPos the position in "real world"
	*
	*/
176 177 178 179 180 181 182 183 184 185 186 187
	void createPointEdge0(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge1(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge2(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge3(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge4(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge5(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge6(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge7(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge8(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge9(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge10(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
	void createPointEdge11(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int* const lVertTable, const typename PFP::VEC3& vPos);
Pierre Kraemer's avatar
Pierre Kraemer committed
188 189 190 191 192 193 194 195
//@}

	/**
	 * modify position of vertex to obtain real world coordinate
	 * @param _P initial position
	 * @param _dec shifting due to interpolation in the cube
	 * @return new position
	 */
196
	typename PFP::VEC3 recalPoint(const VEC3& _P, const VEC3& _dec ) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
197 198 199 200 201

	void setNeighbourSimple(L_DART d1, L_DART d2);

	void setNeighbour(L_DART d1, L_DART d2);

202
	L_DART createTriEmb(unsigned int e1, unsigned int e2, unsigned int e3);
Pierre Kraemer's avatar
Pierre Kraemer committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

public:
	/**
	* constructor from filename
	* @param _cName name of file to open
	*/
	MarchingCube(const char* _cName);

	/**
	* constructor from image
	* @param img voxel image
	* @param wind the windowing class (for inside/outside distinguish)
	* @param boundRemoved true is bound is going to be removed
	*/
	MarchingCube(Image<DataType>* img, Windowing<DataType> wind, bool boundRemoved);

	/**
	* constructor from filename
	* @param img voxel image
	* @param map ptr to the map use to store the mesh
	* @param idPos id of attribute position
	* @param wind the windowing class (for inside/outside distinguish)
	* @param boundRemoved true is bound is going to be removed
	*/
227
	MarchingCube(Image<DataType>* img, L_MAP* map, VertexAttribute<VEC3>& position, Windowing<DataType> wind, bool boundRemoved);
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

	/**
	* destructor
	*/
	~MarchingCube();

	/**
	* delete the mesh
	*/
	void deleteMesh();

	/**
	* simple version of Marching Cubes algorithm
	*/
	void simpleMeshing();

	/**
	 * get pointer on result mesh after processing
	 * @return the mesh
	 */
248
	L_MAP* getMesh() const { return m_map; }
Pierre Kraemer's avatar
Pierre Kraemer committed
249 250 251 252

	/**
	 * get the image
	 */
253
	Image<DataType>* getImg() { return m_Image; }
Pierre Kraemer's avatar
Pierre Kraemer committed
254 255 256 257

	/**
	 * Get the lower corner of bounding AABB
	 */
258
	Geom::Vec3f boundMin() const { return m_Image->boundMin(); }
Pierre Kraemer's avatar
Pierre Kraemer committed
259 260 261 262

	/**
	 * Get the upper corner of bounding AABB
	 */
263
	Geom::Vec3f boundMax() const { return m_Image->boundMax(); }
Pierre Kraemer's avatar
Pierre Kraemer committed
264

265
	void removeFacesOfBoundary(VertexAttribute<unsigned char>& boundVertices, unsigned int frameWidth);
Sylvain Thery's avatar
Sylvain Thery committed
266

Sylvain Thery's avatar
Sylvain Thery committed
267
	void recalPoints(const Geom::Vec3f& origin);
268 269 270 271 272 273


	#ifdef MC_WIDTH_EDGE_Z_EMBEDED
	void setZSliceAttrib(EdgeAttribute<unsigned long long>* zsatt, unsigned int zbound, unsigned int nbZone);
	#endif

Pierre Kraemer's avatar
Pierre Kraemer committed
274 275
};

276 277 278
} // namespace MC

} // namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
279

280
} // namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
281

Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
282
#include "Algo/MC/marchingcube.hpp"
Pierre Kraemer's avatar
Pierre Kraemer committed
283
#endif