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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

namespace CGoGN
{

namespace Algo
{

namespace MC {
/**
 * 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:
	typedef typename PFP::MAP L_MAP;
	typedef Dart L_DART;

	/**
	* 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;

	typename PFP::TVEC3 m_positions;

	/**
	* Origin of image
	*/
	typename PFP::VEC3 m_fOrigin;

	/**
	* scale of image
	*/
	typename PFP::VEC3 m_fScal;

	/**
	* 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
	*/
91
	unsigned char computeIndex(const DataType* const _ucData) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
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

	/**
	 * 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)
	*/
124 125 126 127 128 129 130 131
	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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
//@}

	/**
	* 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
	*/
148
	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
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

	/**
	* @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"
	*
	*/
166 167 168 169 170 171 172 173 174 175 176 177
	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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
//@}

	/**
	 * 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
	 */
	typename PFP::VEC3 recalPoint( const typename PFP::VEC3& _P,  const typename PFP::VEC3& _dec ) const;

	void setNeighbourSimple(L_DART d1, L_DART d2);

	void setNeighbour(L_DART d1, L_DART d2);

	L_DART createTriEmb(unsigned int e1, unsigned int e2, unsigned int e3) {
		L_DART d = m_map->newFace(3);
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
194 195 196 197
		//TODO change this which work only with 2-maps
		m_map->setDartEmbedding(VERTEX,d,e1); d = m_map->phi1(d);
		m_map->setDartEmbedding(VERTEX,d,e2); d = m_map->phi1(d);
		m_map->setDartEmbedding(VERTEX,d,e3); d = m_map->phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		return d;
	}

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
	*/
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
224
	MarchingCube(Image<DataType>* img, L_MAP* map, const typename PFP::TVEC3& position,  Windowing<DataType> wind, bool boundRemoved);
Pierre Kraemer's avatar
Pierre Kraemer committed
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

	/**
	* 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
	 */
	L_MAP* getMesh() const { return m_map;}

	/**
	 * get the image
	 */
	Image<DataType>* getImg() {return m_Image;}

	/**
	 * Get the lower corner of bounding AABB
	 */
	Geom::Vec3f boundMin() const {return m_Image->boundMin();}

	/**
	 * Get the upper corner of bounding AABB
	 */
	Geom::Vec3f boundMax() const {return m_Image->boundMax();}

Sylvain Thery's avatar
Sylvain Thery committed
262 263
	void removeFacesOfBoundary(AttributeHandler<unsigned char>& boundVertices, unsigned int frameWidth);

Sylvain Thery's avatar
Sylvain Thery committed
264
	void recalPoints(const Geom::Vec3f& origin);
Pierre Kraemer's avatar
Pierre Kraemer committed
265 266 267 268 269 270 271
};


} // end namespace
} // end namespace
} // end namespace

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