marchingcube.hpp 40.1 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
25 26
#include "Algo/MC/windowing.h"
#include "Topology/generic/dartmarker.h"
27
#include <vector>
Pierre Kraemer's avatar
Pierre Kraemer committed
28 29 30 31 32 33 34

namespace CGoGN
{

namespace Algo
{

35 36 37
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
38 39 40 41 42 43 44 45 46 47 48
namespace MC
{
template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
MarchingCube<DataType, Windowing, PFP>::MarchingCube(const char* _cName)
{
	m_Image = new Image<DataType>();

	m_Image->loadInr(_cName); // voxel sizes initialized with (1.0,1.0,1.0)
	m_Buffer = NULL;
	m_map = NULL;

49 50
	m_fOrigin = VEC3(0.0,0.0,0.0);
	m_fScal = VEC3(1.0,1.0,1.0);
Pierre Kraemer's avatar
Pierre Kraemer committed
51

52 53 54 55
	#ifdef MC_WIDTH_EDGE_Z_EMBEDED
		m_currentZSlice = 0;
		m_zslice = NULL;
	#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
56 57 58 59 60 61 62 63
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
MarchingCube<DataType, Windowing, PFP>::MarchingCube(Image<DataType>* img, Windowing<DataType> wind, bool boundRemoved):
	m_Image(img),
	m_windowFunc(wind),
	m_Buffer(NULL),
	m_map(NULL),
64 65
	m_fOrigin(VEC3(0.0,0.0,0.0)),
	m_fScal(VEC3(1.0,1.0,1.0)),
Pierre Kraemer's avatar
Pierre Kraemer committed
66
	m_brem(boundRemoved)
67 68 69 70 71 72
{
	#ifdef MC_WIDTH_EDGE_Z_EMBEDED
		m_currentZSlice = 0;
		m_zslice = NULL;
	#endif
}
Pierre Kraemer's avatar
Pierre Kraemer committed
73 74

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
75
MarchingCube<DataType, Windowing, PFP>::MarchingCube(Image<DataType>* img, L_MAP* map, VertexAttribute<VEC3, L_MAP_IMPL>& position, Windowing<DataType> wind, bool boundRemoved):
Pierre Kraemer's avatar
Pierre Kraemer committed
76 77 78 79
	m_Image(img),
	m_windowFunc(wind),
	m_Buffer(NULL),
	m_map(map),
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
80
	m_positions(position),
81 82
	m_fOrigin(VEC3(0.0,0.0,0.0)),
	m_fScal(VEC3(1.0,1.0,1.0)),
Pierre Kraemer's avatar
Pierre Kraemer committed
83
	m_brem(boundRemoved)
84 85 86 87 88 89
{
	#ifdef MC_WIDTH_EDGE_Z_EMBEDED
		m_currentZSlice = 0;
		m_zslice = NULL;
	#endif
}
Pierre Kraemer's avatar
Pierre Kraemer committed
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


template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
MarchingCube<DataType, Windowing, PFP>::~MarchingCube()
{
//	if (m_Image != NULL)
//	{
//		delete m_Image;
//	}
//
	if (m_Buffer != NULL)
	{
		delete m_Buffer;
		m_Buffer = NULL;
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
void MarchingCube<DataType, Windowing, PFP>::deleteMesh()
{
	if (m_map != NULL)
	{
		delete m_map;
		m_map = NULL;
	}
}

117 118 119
template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
Dart  MarchingCube<DataType, Windowing, PFP>::createTriEmb(unsigned int e1, unsigned int e2, unsigned int e3)
{
120
	L_DART d = m_map->newFace(3,false);
121 122 123 124 125

	unsigned int vemb = e1;

	auto fsetemb = [&] (Dart d) { m_map->template setDartEmbedding<VERTEX>(d, vemb); };

Pierre Kraemer's avatar
Pierre Kraemer committed
126
	m_map->template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
Sylvain Thery's avatar
Sylvain Thery committed
127
	d = m_map->phi1(d);
128
	vemb = e2;
Pierre Kraemer's avatar
Pierre Kraemer committed
129
	m_map->template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
Sylvain Thery's avatar
Sylvain Thery committed
130
	d = m_map->phi1(d);
131
	vemb = e3;
Pierre Kraemer's avatar
Pierre Kraemer committed
132
	m_map->template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
Sylvain Thery's avatar
Sylvain Thery committed
133
	d = m_map->phi1(d);
134 135 136 137

	return d;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
138 139 140 141
template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
void MarchingCube<DataType, Windowing, PFP>::simpleMeshing()
{
	// create the mesh if needed
142
	if (m_map == NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156
	{
		m_map = new L_MAP();
	}

	// create the buffer
	if (m_Buffer != NULL)
	{
		delete m_Buffer;
	}

	m_Buffer = new Buffer<L_DART>(m_Image->getWidthX(),m_Image->getWidthY());

	// compute value to transform points directly to final system coordinate

157
	m_fOrigin   =  VEC3((float)(m_Image->getOrigin()[0]),(float)(m_Image->getOrigin()[1]),(float)(m_Image->getOrigin()[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
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

	m_fScal[0] = m_Image->getVoxSizeX();
	m_fScal[1] = m_Image->getVoxSizeY();
	m_fScal[2] = m_Image->getVoxSizeZ();

	// get access to data (pointer + size)

	DataType* ucData = m_Image->getData();

//	DataType* ucDa = ucData;

	int lTx = m_Image->getWidthX();
	int lTy = m_Image->getWidthY();
	int lTz = m_Image->getWidthZ();


	int lTxm = lTx - 1 ;
	int lTym = lTy - 1;
	int lTzm = lTz - 1;

	int lZ,lY,lX;

	lX = 0 ;
	lY = 0 ;
	lZ = 0 ;
	ucData = m_Image->getVoxelPtr(lX,lY,lZ);

	createFaces_1(ucData++,lX++,lY,lZ,1);  // TAG

	while (lX < lTxm)
	{
		createFaces_2(ucData++,lX++,lY,lZ,1);   // TAG
	}
	lY++;

	while (lY < lTym)   // 2nd and others rows  lY = 1..
	{
		lX = 0;
		ucData = m_Image->getVoxelPtr(lX,lY,lZ);

		createFaces_3(ucData++,lX++,lY,lZ,1); // TAG
		while (lX < lTxm)
		{
			createFaces_4(ucData++,lX++,lY,lZ,1);  // TAG
		}
		lY++;
	}
	lZ++;
206 207 208 209
	#ifdef MC_WIDTH_EDGE_Z_EMBEDED
		m_currentZSlice++;
	#endif

Pierre Kraemer's avatar
Pierre Kraemer committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	m_Buffer->nextSlice();

// middles slices

	while (lZ < lTzm)
	{
		lY = 0;
		lX = 0;

		ucData = m_Image->getVoxelPtr(lX,lY,lZ);

		createFaces_5(ucData++,lX++,lY,lZ,4);  // TAG
		while (lX < lTxm)
		{
			createFaces_6(ucData++,lX++,lY,lZ,4);   // TAG
		}
		lY++;

		while (lY<lTym)   // 2nd and others rows  lY = 1..
		{
			lX = 0;
			ucData = m_Image->getVoxelPtr(lX,lY,lZ);

			createFaces_7(ucData++,lX++,lY,lZ,16); // TAG
Sylvain Thery's avatar
Sylvain Thery committed
234
			while (lX < lTxm-1)
Pierre Kraemer's avatar
Pierre Kraemer committed
235 236 237
			{
				createFaces_8(ucData++,lX++,lY,lZ,0);
			}
Sylvain Thery's avatar
Sylvain Thery committed
238
			createFaces_8(ucData++,lX,lY,lZ,32);   //TAG
Pierre Kraemer's avatar
Pierre Kraemer committed
239 240 241 242
			lY++;
		}

		lZ++;
243 244 245
		#ifdef MC_WIDTH_EDGE_Z_EMBEDED
			m_currentZSlice++;
		#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
246 247 248
		m_Buffer->nextSlice();
	}

Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
249
	CGoGNout << "Taille 2-carte:"<<m_map->getNbDarts()<<" brins"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
250 251 252 253 254 255 256 257
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
unsigned char MarchingCube<DataType, Windowing, PFP>::computeIndex(const DataType* const _ucData) const
{
	unsigned char ucCubeIndex = 0;
	const DataType* ucDataLocal = _ucData;

258 259
	int lTx = m_Image->getWidthX();
	int lTxy = m_Image->getWidthXY();
Pierre Kraemer's avatar
Pierre Kraemer committed
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 285 286 287 288

	if ( m_windowFunc.inside(*ucDataLocal) )
		ucCubeIndex = 1; // point 0
	ucDataLocal ++;
	if ( m_windowFunc.inside(*ucDataLocal) )
		ucCubeIndex += 2; // point 1
	ucDataLocal += lTx;
	if ( m_windowFunc.inside(*ucDataLocal) )
		ucCubeIndex += 4; // point 2
	ucDataLocal --;
	if ( m_windowFunc.inside(*ucDataLocal) )
		ucCubeIndex += 8; // point 3
	ucDataLocal += lTxy;
	if ( m_windowFunc.inside(*ucDataLocal) )
		ucCubeIndex += 128; // point 7
	ucDataLocal -= lTx;
	if ( m_windowFunc.inside(*ucDataLocal) )
		ucCubeIndex += 16; // point 4
	ucDataLocal ++;
	if ( m_windowFunc.inside(*ucDataLocal) )
		ucCubeIndex += 32; // point 5
	ucDataLocal += lTx;
	if ( m_windowFunc.inside(*ucDataLocal) )
		ucCubeIndex += 64; // point 6

	return ucCubeIndex;
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
289
typename PFP::VEC3 MarchingCube<DataType, Windowing, PFP>::recalPoint(const VEC3& _P, const VEC3& _dec ) const
Pierre Kraemer's avatar
Pierre Kraemer committed
290
{
291
	VEC3 point = _P + _dec ;
292 293 294 295 296
//	point[0] = point[0] * m_fScal[0];
//	point[1] = point[1] * m_fScal[1];
//	point[2] = point[2] * m_fScal[2];
//
//	point += m_fOrigin;
Pierre Kraemer's avatar
Pierre Kraemer committed
297 298 299 300
	return 	point;
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
301
void MarchingCube<DataType, Windowing, PFP>::createPointEdge0(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ,  unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
302 303 304 305 306
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 1)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX,_lY,_lZ), m_Image->getVoxel(_lX+1,_lY,_lZ) );

307
//		VEC3 newPoint = recalPoint(vPos,VEC3(interp, 0., 0.));
Pierre Kraemer's avatar
Pierre Kraemer committed
308
//		lVertTable[0] = L_EMB::create(newPoint);
309
		lVertTable[0] = m_map->template newCell<VERTEX>();
310
		m_positions[lVertTable[0]] = recalPoint(vPos,VEC3(interp, 0., 0.));
Pierre Kraemer's avatar
Pierre Kraemer committed
311 312 313 314 315
		m_Buffer->setPointEdge0(_lX, _lY,lVertTable[0]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
316
void MarchingCube<DataType, Windowing, PFP>::createPointEdge1(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ,  unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
317 318 319 320 321
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 2)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX,_lY,_lZ), m_Image->getVoxel(_lX,_lY+1,_lZ) );

322
//		VEC3 newPoint = recalPoint(vPos,VEC3(1.,interp, 0.));
Pierre Kraemer's avatar
Pierre Kraemer committed
323
//		lVertTable[1] = L_EMB::create(newPoint);
324
		lVertTable[1] = m_map->template newCell<VERTEX>();
325
		m_positions[lVertTable[1]] = recalPoint(vPos,VEC3(1.,interp, 0.));
Pierre Kraemer's avatar
Pierre Kraemer committed
326 327 328 329 330
		m_Buffer->setPointEdge1(_lX, _lY,lVertTable[1]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
331
void MarchingCube<DataType, Windowing, PFP>::createPointEdge2(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ,  unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
332 333 334 335 336
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 4)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX-1,_lY,_lZ), m_Image->getVoxel(_lX,_lY,_lZ) );

337
//		VEC3 newPoint = recalPoint(vPos,VEC3(interp, 1., 0.));
Pierre Kraemer's avatar
Pierre Kraemer committed
338
//		lVertTable[2] = L_EMB::create(newPoint);
339
		lVertTable[2] = m_map->template newCell<VERTEX>();
340
		m_positions[lVertTable[2]] = recalPoint(vPos,VEC3(interp, 1., 0.));
Pierre Kraemer's avatar
Pierre Kraemer committed
341 342 343 344 345
		m_Buffer->setPointEdge2(_lX, _lY,lVertTable[2]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
346
void MarchingCube<DataType, Windowing, PFP>::createPointEdge3(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
347 348 349 350 351
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 8)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX,_lY-1,_lZ), m_Image->getVoxel(_lX,_lY,_lZ) );

352
//		VEC3 newPoint = recalPoint(vPos,VEC3(0., interp, 0.));
Pierre Kraemer's avatar
Pierre Kraemer committed
353
//		lVertTable[3] = L_EMB::create(newPoint);
354
		lVertTable[3] = m_map->template newCell<VERTEX>();
355
		m_positions[lVertTable[3]] = recalPoint(vPos,VEC3(0., interp, 0.));
Pierre Kraemer's avatar
Pierre Kraemer committed
356 357 358 359 360
		m_Buffer->setPointEdge3(_lX, _lY,lVertTable[3]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
361
void MarchingCube<DataType, Windowing, PFP>::createPointEdge4(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
362 363 364 365 366
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 16)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX,_lY,_lZ), m_Image->getVoxel(_lX+1,_lY,_lZ) );

367
//		VEC3 newPoint = recalPoint(vPos,VEC3(interp, 0., 1.));
Pierre Kraemer's avatar
Pierre Kraemer committed
368
//		lVertTable[4] = L_EMB::create(newPoint);
369
		lVertTable[4] = m_map->template newCell<VERTEX>();
370
		m_positions[lVertTable[4]] = recalPoint(vPos,VEC3(interp, 0., 1.));
Pierre Kraemer's avatar
Pierre Kraemer committed
371 372 373 374 375
		m_Buffer->setPointEdge4(_lX, _lY,lVertTable[4]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
376
void MarchingCube<DataType, Windowing, PFP>::createPointEdge5(const unsigned char _ucCubeIndex,  const int _lX, const int _lY, const int _lZ, unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
377 378 379 380 381
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 32)
 	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX,_lY,_lZ), m_Image->getVoxel(_lX,_lY+1,_lZ) );

382
//		VEC3 newPoint = recalPoint(vPos,VEC3(1., interp, 1.));
Pierre Kraemer's avatar
Pierre Kraemer committed
383
//		lVertTable[5] = L_EMB::create(newPoint);
384
		lVertTable[5] = m_map->template newCell<VERTEX>();
385
		m_positions[lVertTable[5]] = recalPoint(vPos,VEC3(1., interp, 1.));
Pierre Kraemer's avatar
Pierre Kraemer committed
386 387 388 389 390
		m_Buffer->setPointEdge5(_lX, _lY,lVertTable[5]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
391
void MarchingCube<DataType, Windowing, PFP>::createPointEdge6(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
392 393 394 395 396
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 64)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX-1,_lY,_lZ), m_Image->getVoxel(_lX,_lY,_lZ) );

397
//		VEC3 newPoint = recalPoint(vPos,VEC3(interp, 1., 1.));
Pierre Kraemer's avatar
Pierre Kraemer committed
398
//		lVertTable[6] = L_EMB::create(newPoint);
399
		lVertTable[6] = m_map->template newCell<VERTEX>();
400
		m_positions[lVertTable[6]] = recalPoint(vPos,VEC3(interp, 1., 1.));
Pierre Kraemer's avatar
Pierre Kraemer committed
401 402 403 404 405
		m_Buffer->setPointEdge6(_lX, _lY,lVertTable[6]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
406
void MarchingCube<DataType, Windowing, PFP>::createPointEdge7(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
407 408 409 410 411
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 128)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX,_lY-1,_lZ), m_Image->getVoxel(_lX,_lY,_lZ) );

412
//		VEC3 newPoint = recalPoint(vPos,VEC3(0., interp, 1.));
Pierre Kraemer's avatar
Pierre Kraemer committed
413
//		lVertTable[7] = L_EMB::create(newPoint);
414
		lVertTable[7] = m_map->template newCell<VERTEX>();
415
		m_positions[lVertTable[7]] = recalPoint(vPos,VEC3(0., interp, 1.));
Pierre Kraemer's avatar
Pierre Kraemer committed
416 417 418 419 420
		m_Buffer->setPointEdge7(_lX, _lY,lVertTable[7]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
421
void MarchingCube<DataType, Windowing, PFP>::createPointEdge8(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
422 423 424 425 426
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 256)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX,_lY,_lZ), m_Image->getVoxel(_lX,_lY,_lZ+1) );

427
//		VEC3 newPoint = recalPoint(vPos,VEC3(0., 0., interp));
Pierre Kraemer's avatar
Pierre Kraemer committed
428
//		lVertTable[8] = L_EMB::create(newPoint);
429
		lVertTable[8] = m_map->template newCell<VERTEX>();
430
		m_positions[lVertTable[8]] = recalPoint(vPos,VEC3(0., 0., interp));
Pierre Kraemer's avatar
Pierre Kraemer committed
431 432 433 434 435
		m_Buffer->setPointEdge8(_lX, _lY,lVertTable[8]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
436
void MarchingCube<DataType, Windowing, PFP>::createPointEdge9(const unsigned char _ucCubeIndex,  const int _lX, const int _lY, const int _lZ, unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
437 438 439 440 441
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 512)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX,_lY,_lZ), m_Image->getVoxel(_lX,_lY,_lZ+1) );

442
//		VEC3 newPoint = recalPoint(vPos,VEC3(1., 0., interp));
Pierre Kraemer's avatar
Pierre Kraemer committed
443
//		lVertTable[9] = L_EMB::create(newPoint);
444
		lVertTable[9] = m_map->template newCell<VERTEX>();
445
		m_positions[lVertTable[9]] = recalPoint(vPos,VEC3(1., 0., interp));
Pierre Kraemer's avatar
Pierre Kraemer committed
446 447 448 449 450
		m_Buffer->setPointEdge9(_lX, _lY,lVertTable[9]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
451
void MarchingCube<DataType, Windowing, PFP>::createPointEdge10(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
452 453 454 455 456
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 1024)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX,_lY,_lZ), m_Image->getVoxel(_lX,_lY,_lZ+1) );

457
//		VEC3 newPoint = recalPoint(vPos,VEC3(1., 1., interp));
Pierre Kraemer's avatar
Pierre Kraemer committed
458
//		lVertTable[10] = L_EMB::create(newPoint);
459
		lVertTable[10] = m_map->template newCell<VERTEX>();
460
		m_positions[lVertTable[10]] = recalPoint(vPos,VEC3(1., 1., interp));
Pierre Kraemer's avatar
Pierre Kraemer committed
461 462 463 464 465
		m_Buffer->setPointEdge10(_lX, _lY,lVertTable[10]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
466
void MarchingCube<DataType, Windowing, PFP>::createPointEdge11(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int _lZ, unsigned int * const lVertTable, const VEC3& vPos)
Pierre Kraemer's avatar
Pierre Kraemer committed
467 468 469 470 471
{
	if  (accelMCTable::m_EdgeTable[_ucCubeIndex] & 2048)
	{
		float interp = m_windowFunc.interpole( m_Image->getVoxel(_lX,_lY,_lZ), m_Image->getVoxel(_lX,_lY,_lZ+1) );

472
//		VEC3 newPoint = recalPoint(vPos,VEC3(0., 1., interp));
Pierre Kraemer's avatar
Pierre Kraemer committed
473
//		lVertTable[11] = L_EMB::create(newPoint);
474
		lVertTable[11] = m_map->template newCell<VERTEX>();
475
		m_positions[lVertTable[11]] = recalPoint(vPos,VEC3(0., 1., interp));
Pierre Kraemer's avatar
Pierre Kraemer committed
476 477 478 479 480
		m_Buffer->setPointEdge11(_lX, _lY,lVertTable[11]);
	}
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
481
void MarchingCube<DataType, Windowing, PFP>::createFaces_1(DataType *vox, const int _lX,const int _lY,const int _lZ, unsigned char tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
482 483 484 485 486 487 488
{
	unsigned char ucCubeIndex = computeIndex(vox);
	if ((ucCubeIndex == 0) || (ucCubeIndex == 255))
		return;

	unsigned int  lVertTable[12];

489 490 491
//	VEC3 vPos(float(_lX) , float(_lY) , float(_lZ) );
//	VEC3 vPos(float(_lX) + 0.5f, float(_lY) + 0.5f, (float)_lZ + 0.5f);
	VEC3 vPos(_lX, _lY, _lZ);
Pierre Kraemer's avatar
Pierre Kraemer committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

// create the new  vertices

	int lX = _lX;
	int lY = _lY;
	int lZ = _lZ;

//	float curv = m_Curvature.computeCurvatureSimple(vox);
	float curv = 0.0f;
	createPointEdge0( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	createPointEdge8( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX++;
	createPointEdge1( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	createPointEdge9( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge2( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	createPointEdge10( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX--;
	createPointEdge3( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	createPointEdge11( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lZ++;
	createPointEdge7( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY--;
	createPointEdge4( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX++;
	createPointEdge5( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge6( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);

// get the shared vertices corresponding to the shared edges :
	//all edges
	    lVertTable[ 0]  = m_Buffer->getPointEdge0(_lX, _lY);
	    lVertTable[ 1]  = m_Buffer->getPointEdge1(_lX, _lY);
	    lVertTable[ 2]  = m_Buffer->getPointEdge2(_lX, _lY);
	    lVertTable[ 3]  = m_Buffer->getPointEdge3(_lX, _lY);
	    lVertTable[ 4]  = m_Buffer->getPointEdge4(_lX, _lY);
	    lVertTable[ 5]  = m_Buffer->getPointEdge5(_lX, _lY);
	    lVertTable[ 6]  = m_Buffer->getPointEdge6(_lX, _lY);
	    lVertTable[ 7]  = m_Buffer->getPointEdge7(_lX, _lY);
	    lVertTable[ 8]  = m_Buffer->getPointEdge8(_lX, _lY);
	    lVertTable[ 9]  = m_Buffer->getPointEdge9(_lX, _lY);
	    lVertTable[10]  = m_Buffer->getPointEdge10(_lX, _lY);
	    lVertTable[11]  = m_Buffer->getPointEdge11(_lX, _lY);

// create the new faces, the mask (6 bits) of faces to treat is 111111
	createLocalFaces(ucCubeIndex, _lX, _lY, _lZ,  lVertTable, 0x3F, curv, tag);
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
541
void MarchingCube<DataType, Windowing, PFP>::createFaces_2(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
542 543 544 545 546 547
{
	unsigned char ucCubeIndex = computeIndex(vox);
	if ((ucCubeIndex == 0) || (ucCubeIndex == 255))
		return;

	unsigned int  lVertTable[12];
548
	VEC3 vPos(_lX, _lY, _lZ);
Pierre Kraemer's avatar
Pierre Kraemer committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

// create the new  vertices
	int lX = _lX;
	int lY = _lY;
	int lZ = _lZ;

//	float curv = m_Curvature.computeCurvatureSimple(vox);
	float curv=0.0f;

	createPointEdge0( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos );
	lX++;
	createPointEdge1( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos );
	createPointEdge9( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos );
	lY++;
	createPointEdge2( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	createPointEdge10( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX--;
	lZ++;
	lY--;
	createPointEdge4( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX++;
	createPointEdge5( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos );
	lY++;
	createPointEdge6( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);

// get the shared vertices corresponding to the shared edges :
	//edge 3
	    lVertTable[3]  = m_Buffer->getPointEdge3(_lX, _lY);

	// edge 7
	    lVertTable[7]  = m_Buffer->getPointEdge7(_lX, _lY);

	// edge 8
	    lVertTable[8]  = m_Buffer->getPointEdge8(_lX, _lY);

	//edge 11
	    lVertTable[11] = m_Buffer->getPointEdge11(_lX, _lY);

// create the new faces, the mask (6 bits) of faces to treat is 111110
	createLocalFaces(ucCubeIndex, _lX, _lY, _lZ,  lVertTable, 0x3E, curv, tag);
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
592
void MarchingCube<DataType, Windowing, PFP>::createFaces_3(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
593 594 595 596 597 598
{
	unsigned char ucCubeIndex = computeIndex(vox);
	if ((ucCubeIndex == 0) || (ucCubeIndex == 255))
		return;

	unsigned int  lVertTable[12];
599
	VEC3 vPos(_lX, _lY, _lZ);
Pierre Kraemer's avatar
Pierre Kraemer committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

// create the new  vertices
	int lX = _lX;
	int lY = _lY;
	int lZ = _lZ;

//	float curv = m_Curvature.computeCurvatureSimple(vox);
	float curv=0.0f;

	lX++;
	createPointEdge1( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge2( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	createPointEdge10( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX--;
	createPointEdge3( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	createPointEdge11( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lZ++;
	createPointEdge7( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY--;
	lX++;
	createPointEdge5( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge6( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);

// get the shared vertices corresponding to the shared edges :
	//edge 0
	    lVertTable[0] = m_Buffer->getPointEdge0(_lX, _lY);

	// edge 9
	    lVertTable[9] = m_Buffer->getPointEdge9(_lX, _lY);

	// edge 4
	    lVertTable[4] = m_Buffer->getPointEdge4(_lX, _lY);

	//edge 8
	    lVertTable[8] = m_Buffer->getPointEdge8(_lX, _lY);


// create the new faces, the mask (6 bits) of faces to treat is 111011
	createLocalFaces(ucCubeIndex, _lX, _lY, _lZ,  lVertTable, 0x3B, curv, tag);
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
644
void MarchingCube<DataType, Windowing, PFP>::createFaces_4(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
645 646 647 648 649 650
{
	unsigned char ucCubeIndex = computeIndex(vox);
	if ((ucCubeIndex == 0) || (ucCubeIndex == 255))
		return;

	unsigned int  lVertTable[12];
651
	VEC3 vPos(_lX, _lY, _lZ);
Pierre Kraemer's avatar
Pierre Kraemer committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

// create the new  vertices
	int lX = _lX;
	int lY = _lY;
	int lZ = _lZ;

//	float curv = m_Curvature.computeCurvatureSimple(vox);
	float curv=0.0f;

	lX++;
	createPointEdge1( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge2( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	createPointEdge10( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lZ++;
	lY--;
	createPointEdge5( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge6( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);

// get the shared vertices corresponding to the shared edges :
	//edge 3
	    lVertTable[3]  = m_Buffer->getPointEdge3(_lX, _lY);

	// edge 7
	    lVertTable[7]  = m_Buffer->getPointEdge7(_lX, _lY);

	// edge 8
	    lVertTable[8]  = m_Buffer->getPointEdge8(_lX, _lY);

	//edge 11
	    lVertTable[11] = m_Buffer->getPointEdge11(_lX, _lY);

	//edge 0
	    lVertTable[0] = m_Buffer->getPointEdge0(_lX, _lY);

	// edge 9
	    lVertTable[9] = m_Buffer->getPointEdge9(_lX, _lY);

	// edge 4
	    lVertTable[4] = m_Buffer->getPointEdge4(_lX, _lY);

// create the new faces, the mask (6 bits) of faces to treat is 111010
	createLocalFaces(ucCubeIndex, _lX, _lY, _lZ,  lVertTable, 0x3A, curv, tag);
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
699
void MarchingCube<DataType, Windowing, PFP>::createFaces_5(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
700 701 702 703 704 705
{
	unsigned char ucCubeIndex = computeIndex(vox);
	if ((ucCubeIndex == 0) || (ucCubeIndex == 255))
		return;

	unsigned int  lVertTable[12];
706
	VEC3 vPos(_lX, _lY, _lZ);
Pierre Kraemer's avatar
Pierre Kraemer committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

// create the new  vertices
	int lX = _lX;
	int lY = _lY;
	int lZ = _lZ;

//	float curv = m_Curvature.computeCurvatureSimple(vox);
	float curv=0.0f;

	createPointEdge8( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX++;
	createPointEdge9( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge10( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX--;
	createPointEdge3( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	createPointEdge11( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lZ++;
	createPointEdge7( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY--;
	createPointEdge4( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX++;
	createPointEdge5( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge6( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);

// get the shared vertices corresponding to the shared edges :
	//edge 0
	    lVertTable[0] = m_Buffer->getPointEdge0(_lX, _lY);

	// edge 1
	    lVertTable[1] = m_Buffer->getPointEdge1(_lX, _lY);

	// edge 2
	    lVertTable[2] = m_Buffer->getPointEdge2(_lX, _lY);

	//edge 3
	    lVertTable[3] = m_Buffer->getPointEdge3(_lX, _lY);


// create the new faces, the mask (6 bits) of faces to treat is 101111
	createLocalFaces(ucCubeIndex, _lX, _lY, _lZ,  lVertTable, 0x2F, curv, tag);
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
752
void MarchingCube<DataType, Windowing, PFP>::createFaces_6(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
753 754 755 756 757 758
{
	unsigned char ucCubeIndex = computeIndex(vox);
	if ((ucCubeIndex == 0) || (ucCubeIndex == 255))
		return;

	unsigned int  lVertTable[12];
759
	VEC3 vPos(_lX, _lY, _lZ);
Pierre Kraemer's avatar
Pierre Kraemer committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809

// create the new  vertices
	int lX = _lX+1;
	int lY = _lY;
	int lZ = _lZ;

	vox++;

//	float curv = m_Curvature.computeCurvatureSimple(vox);
	float curv=0.0f;
	createPointEdge9( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge10( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX--;
	lZ++;
	lY--;
	createPointEdge4( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX++;
	createPointEdge5( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge6( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);

// get the shared vertices corresponding to the shared edges :
	//edge 0
	    lVertTable[0] = m_Buffer->getPointEdge0(_lX, _lY);

	// edge 1
	    lVertTable[1] = m_Buffer->getPointEdge1(_lX, _lY);

	// edge 2
	    lVertTable[2] = m_Buffer->getPointEdge2(_lX, _lY);

	//edge 3
	    lVertTable[3] = m_Buffer->getPointEdge3(_lX, _lY);

	// edge 7
	    lVertTable[7] = m_Buffer->getPointEdge7(_lX, _lY);

	// edge 8
	    lVertTable[8] = m_Buffer->getPointEdge8(_lX, _lY);

	// edge 11
	    lVertTable[11] = m_Buffer->getPointEdge11(_lX, _lY);


// create the new faces, the mask (6 bits) of faces to treat is 101110
	createLocalFaces(ucCubeIndex, _lX, _lY, _lZ,  lVertTable, 0x2E, curv, tag);
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
810
void MarchingCube<DataType, Windowing, PFP>::createFaces_7(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
811 812 813 814 815 816
{
	unsigned char ucCubeIndex = computeIndex(vox);
	if ((ucCubeIndex == 0) || (ucCubeIndex == 255))
		return;

	unsigned int  lVertTable[12];
817
	VEC3 vPos(_lX, _lY, _lZ);
Pierre Kraemer's avatar
Pierre Kraemer committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867

// create the new  vertices
	int lX = _lX+1;
	int lY = _lY+1;
	int lZ = _lZ;

	vox += m_Image->getWidthX()+1;

//	float curv = m_Curvature.computeCurvatureSimple(vox);
	float curv=0.0f;

	createPointEdge10( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lX--;
	createPointEdge11( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lZ++;
	createPointEdge7( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY--;
	lX++;
	createPointEdge5( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	lY++;
	createPointEdge6( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);

// get the shared vertices corresponding to the shared edges :
	//edge 0
	    lVertTable[0] = m_Buffer->getPointEdge0(_lX, _lY);

	// edge 1
	    lVertTable[1] = m_Buffer->getPointEdge1(_lX, _lY);

	// edge 2
	    lVertTable[2] = m_Buffer->getPointEdge2(_lX, _lY);

	//edge 3
	    lVertTable[3] = m_Buffer->getPointEdge3(_lX, _lY);

	// edge 4
	    lVertTable[4] = m_Buffer->getPointEdge4(_lX, _lY);

	// edge 8
	    lVertTable[8] = m_Buffer->getPointEdge8(_lX, _lY);

	//edge 9
	    lVertTable[9] = m_Buffer->getPointEdge9(_lX, _lY);


// create the new faces, the mask (6 bits) of faces to treat is 101011
	createLocalFaces(ucCubeIndex, _lX, _lY, _lZ,  lVertTable, 0x2B, curv, tag);
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
868
void MarchingCube<DataType, Windowing, PFP>::createFaces_8(DataType *vox, const int _lX, const int _lY, const int _lZ, unsigned char tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
869 870 871 872 873 874
{
	unsigned char ucCubeIndex = computeIndex(vox);
	if ((ucCubeIndex == 0) || (ucCubeIndex == 255))
		return;

	unsigned int  lVertTable[12];
875
	VEC3 vPos(_lX, _lY, _lZ);
Pierre Kraemer's avatar
Pierre Kraemer committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925

// create the new  vertices
	int lX = _lX+1;
	int lY = _lY+1;
	int lZ = _lZ+1;

//	float curv = m_Curvature.computeCurvatureSimple(vox);
	float curv=0.0f;

	createPointEdge6( ucCubeIndex, lX,  lY,  lZ, lVertTable, vPos);
	createPointEdge5( ucCubeIndex, lX,  _lY,  lZ, lVertTable, vPos);
	createPointEdge10( ucCubeIndex, lX,  lY,  _lZ, lVertTable, vPos);

// get the shared vertices corresponding to the shared edges :
	//edge 0
	    lVertTable[0] = m_Buffer->getPointEdge0(_lX, _lY);

	// edge 1
	    lVertTable[1] = m_Buffer->getPointEdge1(_lX, _lY);

	// edge 2
	    lVertTable[2] = m_Buffer->getPointEdge2(_lX, _lY);

	//edge 3
	    lVertTable[3] = m_Buffer->getPointEdge3(_lX, _lY);

	// edge 4
	    lVertTable[4] = m_Buffer->getPointEdge4(_lX, _lY);

	// edge 8
	    lVertTable[8] = m_Buffer->getPointEdge8(_lX, _lY);

	//edge 9
	    lVertTable[9] = m_Buffer->getPointEdge9(_lX, _lY);

	// edge 7
	    lVertTable[7] = m_Buffer->getPointEdge7(_lX, _lY);

	// edge 11
	    lVertTable[11] = m_Buffer->getPointEdge11(_lX, _lY);


// create the new faces, the mask (6 bits) of faces to treat is 101010
	createLocalFaces(ucCubeIndex, _lX, _lY, _lZ,  lVertTable, 0x2A, curv, tag);
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
void MarchingCube<DataType, Windowing, PFP>::setNeighbourSimple(L_DART d1, L_DART d2)
{
	if (m_map->phi2(d1) != d2)
926
	{
927
		m_map->sewFaces(d1,d2,false);
928 929 930 931 932 933 934 935 936
		#ifdef MC_WIDTH_EDGE_Z_EMBEDED
		if (m_zslice!=NULL)
		{
			unsigned int val = (m_currentZSlice - m_zbound)/m_sliceGroup;
			std::cout << "ZSLICE=" << val << std::endl;
			m_zslice->operator[](d1) = val;
		}
		#endif
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
937 938 939 940 941 942
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
void MarchingCube<DataType, Windowing, PFP>::setNeighbour(L_DART d1, L_DART d2)
{
	if (m_map->phi2(d1) != d2)
943
	{
944
		m_map->sewFaces(d1,d2,false);
945 946 947 948 949 950 951 952 953
		#ifdef MC_WIDTH_EDGE_Z_EMBEDED
		if (m_zslice!=NULL)
		{
			unsigned int val = (m_currentZSlice - m_zbound)/m_sliceGroup;
			std::cout << "ZSLICE=" << val << std::endl;
			m_zslice->operator[](d1) = val;
		}
		#endif
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
954 955 956
}

template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
957
void MarchingCube<DataType, Windowing, PFP>::createLocalFaces(const unsigned char _ucCubeIndex, const int _lX, const int _lY, const int /*_lZ*/,  unsigned int  const *_lVertTable, const unsigned short _usMask, float /*curv*/, unsigned char /*tag*/)
Pierre Kraemer's avatar
Pierre Kraemer committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
{
// TODO parametre _LZ not used => a supprimer ?
// TODO parametre curv not used => a supprimer ?
// TODO parametre tag not used => a supprimer ?
	
	#define EVENMASK	0x55
	#define ODDMASK	0xAA

// initialize number of created face to 0
	int lNumFaces = 0;

// get the address of buffer table
	L_DART *lFacesTab = m_Buffer->getFacesCubeTableAdr(_lX,_lY);

// To speed access to data "precompute indirection"
973 974
	const char* cTriangle = accelMCTable::m_TriTable[_ucCubeIndex];
	const char* cNeighbour = accelMCTable::m_NeighTable[_ucCubeIndex];
Pierre Kraemer's avatar
Pierre Kraemer committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

//	L_DART dartTriangles[5];

// PASS 1: create faces
 	for (int i=0; cTriangle[i]!=-1;/* incrementation inside */)
	{
	// get index of the 3 points of trianglesfrzr
		int lIndexP1 =  cTriangle[i++];
		int lIndexP2 =  cTriangle[i++];
		int lIndexP3 =  cTriangle[i++];

/*		L_DART edge = m_map->createEmbOriTriangle(_lVertTable[lIndexP1],  //first point/edge
							_lVertTable[lIndexP2], //second point/edge
							_lVertTable[lIndexP3]);// third point/edge
*/
		L_DART edge = createTriEmb(_lVertTable[lIndexP1],  //first point/edge
							_lVertTable[lIndexP2], //second point/edge
							_lVertTable[lIndexP3]);// third point/edge

	// add the face to buffer
		lFacesTab[lNumFaces] = edge;
		lNumFaces++;
	}

// PASS 2: update neighbours
	for (int j=0; j<lNumFaces;++j)
	{
		int i = 3*j;

		int lIndexP1 =  cTriangle[i];
		int lIndexP2 =  cTriangle[i+1];
		int lIndexP3 =  cTriangle[i+2];

		L_DART edge = lFacesTab[j];

		int lLocalNeighbour = cNeighbour[i];
		L_DART dn = lFacesTab[ lLocalNeighbour/3 ];
		if (lLocalNeighbour>0)
			lLocalNeighbour = lLocalNeighbour%3;
		switch(lLocalNeighbour)
		{
			case 0:
				setNeighbourSimple(edge,dn);
				break;
			case 1:
				dn =m_map->phi1(dn);
				setNeighbourSimple(edge,dn);
				break;
			case 2:
				dn = m_map->phi_1(dn); // triangle donc pas besoin de faire le tour par 2xphi1
				setNeighbourSimple(edge,dn);
				break;
			default: // -1
				unsigned char ucCodeEdgeFace = accelMCTable::m_EdgeCode[lIndexP1] & accelMCTable::m_EdgeCode[lIndexP2];
				if (ucCodeEdgeFace & EVENMASK)	// the edge has a neighbour
				{
					L_DART neighbour = m_Buffer->getExternalNeighbour(lIndexP1 , _lX, _lY);	// get neighbour from buffer
					setNeighbour(edge,neighbour);
				}
				break;
		}

		// second edge
		edge =m_map->phi1(edge);
		lLocalNeighbour = cNeighbour[i+1];
		dn = lFacesTab[lLocalNeighbour/3];
		if (lLocalNeighbour>0)
			lLocalNeighbour = lLocalNeighbour%3;
		switch(lLocalNeighbour)
		{
			case 0:
				setNeighbourSimple(edge,dn);
				break;
			case 1:
				dn =m_map->phi1(dn);
				setNeighbourSimple(edge,dn);
				break;
			case 2:
				dn = m_map->phi_1(dn); // triangle donc pas besoin de faire le tour par 2xphi1
				setNeighbourSimple(edge,dn);
				break;
			default: // -1
				unsigned char ucCodeEdgeFace = accelMCTable::m_EdgeCode[lIndexP2] & accelMCTable::m_EdgeCode[lIndexP3];
				if (ucCodeEdgeFace & EVENMASK)	// the edge has a neighbour
				{
					L_DART neighbour = m_Buffer->getExternalNeighbour(lIndexP2 , _lX, _lY);	// get neighbour from buffer
					setNeighbour(edge,neighbour);
				}
				break;
		}

		// third edge
		edge =m_map->phi1(edge);
		lLocalNeighbour = cNeighbour[i+2];
		dn = lFacesTab[lLocalNeighbour/3];
		if (lLocalNeighbour>0)
			lLocalNeighbour = lLocalNeighbour%3;
		switch(lLocalNeighbour)
		{
			case 0:
				setNeighbourSimple(edge,dn);
				break;
			case 1:
				dn =m_map->phi1(dn);
				setNeighbourSimple(edge,dn);
				break;
			case 2:
				dn = m_map->phi_1(dn); // triangle donc pas besoin de faire le tour par 2xphi1
				setNeighbourSimple(edge,dn);
				break;
			default: // -1
				unsigned char ucCodeEdgeFace = accelMCTable::m_EdgeCode[lIndexP3] & accelMCTable::m_EdgeCode[lIndexP1];
				if (ucCodeEdgeFace & EVENMASK)	// the edge has a neighbour
				{
					L_DART neighbour = m_Buffer->getExternalNeighbour(lIndexP3 , _lX, _lY);	// get neighbour from buffer
					setNeighbour(edge,neighbour);
				}
				break;
		}
	}

// PASS 2: set the neighbours in buffer

 	for (int i=0; cTriangle[i]!=-1;/* incrementation inside */)
	{
		L_DART edge = lFacesTab[i/3];

		int lIndexP1 =  cTriangle[i];
		int lIndexP2 =  cTriangle[i+1];
		int lIndexP3 =  cTriangle[i+2];

		if (cNeighbour[i] < 0) // update neighbourhood internal to cube
		{
			// compute the code of the edge (define which face of cube it belongs)
			unsigned char ucCodeEdgeFace = accelMCTable::m_EdgeCode[lIndexP1] & accelMCTable::m_EdgeCode[lIndexP2];
			if (ucCodeEdgeFace & _usMask & ODDMASK)	// the neighbour of the edge not yet created
			{
				m_Buffer->setExternalNeighbour(lIndexP2 , _lX, _lY,  edge);	// set the edge in edge of P2 in the buffer
			}
		}

		edge =m_map->phi1(edge); // next edge
		i++;

		if (cNeighbour[i] < 0)  // update neighbourhood internal to cube
		{
			// compute the code of the edge (define which face of cube it belongs)
			unsigned char ucCodeEdgeFace = accelMCTable::m_EdgeCode[lIndexP2] & accelMCTable::m_EdgeCode[lIndexP3];

			if (ucCodeEdgeFace & _usMask & ODDMASK)	// the neighbour of the edge not yet created
			{
				m_Buffer->setExternalNeighbour(lIndexP3 , _lX, _lY,  edge);	// set the edge in edge of P2 in the buffer
			}
		}

		edge =m_map->phi1(edge); // next edge
		i++;

   		if (cNeighbour[i] < 0)  // update neighbourhood internal to cube
		{
			// compute the code of the edge (define which face of cube it belongs)
			unsigned char ucCodeEdgeFace = accelMCTable::m_EdgeCode[lIndexP3] & accelMCTable::m_EdgeCode[lIndexP1];
			if (ucCodeEdgeFace & _usMask & ODDMASK)	// the neighbour of the edge not yet created
			{
				m_Buffer->setExternalNeighbour(lIndexP1 , _lX, _lY,  edge);	// set the edge in edge of P2 in the buffer
 			}
		}
		i++;
	}

// finish buffer table of faces with -1
// PAS FORCEMENT UTILE A VERIFIER
// 	for(int i=lNumFaces; i <5; i++)
// 	{
// 		lFacesTab[i] = m_map->end();
// 	}

	#undef EVENMASK
	#undef ODDMASK
}

Sylvain Thery's avatar
Sylvain Thery committed
1156
template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
1157
void MarchingCube<DataType, Windowing, PFP>::removeFacesOfBoundary(VertexAttribute<unsigned char, L_MAP_IMPL>& boundVertices, unsigned int frameWidth)
Sylvain Thery's avatar
Sylvain Thery committed
1158 1159
{
	float xmin = frameWidth;
Sylvain Thery's avatar
Sylvain Thery committed
1160
	float xmax = m_Image->getWidthX() - frameWidth -1;
Sylvain Thery's avatar
Sylvain Thery committed
1161
	float ymin = frameWidth;
Sylvain Thery's avatar
Sylvain Thery committed
1162
	float ymax = m_Image->getWidthY() - frameWidth -1;
Sylvain Thery's avatar
Sylvain Thery committed
1163
	float zmin = frameWidth;
Sylvain Thery's avatar
Sylvain Thery committed
1164
	float zmax = m_Image->getWidthZ() - frameWidth -1;
Sylvain Thery's avatar
Sylvain Thery committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

	// traverse position and create bound attrib
	for(unsigned int it = m_positions.begin(); it != m_positions.end(); m_positions.next(it))
	{
		bool bound = (m_positions[it][0]<=xmin) || (m_positions[it][0]>=xmax) || \
					 (m_positions[it][1]<=ymin) || (m_positions[it][1]>=ymax) || \
					 (m_positions[it][2]<=zmin) || (m_positions[it][2]>=zmax);

		if (bound)
		{
			boundVertices[it] = 1;
		}
		else
			boundVertices[it] = 0;
	}

1181
//	 traverse face and check if all vertices are bound
1182
	DartMarker<L_MAP> mf(*m_map);
1183
	for (Dart d = m_map->begin(); d != m_map->end();)	// next done inside loop because of deleteFace
Sylvain Thery's avatar
Sylvain Thery committed
1184
	{
Thery Sylvain's avatar
Thery Sylvain committed
1185
		if (!mf.isMarked(d) && !m_map->isBoundaryMarked2(d))
Sylvain Thery's avatar
Sylvain Thery committed
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
		{
			Dart dd = d;
			Dart e = m_map->phi1(d);
			Dart f = m_map->phi1(e);
			m_map->next(d);
			while ((d==e) || (d==f))
			{
				m_map->next(d);
			}
			if ((boundVertices[dd]!=0) && (boundVertices[e]!=0) && (boundVertices[f]!=0))
1196
				m_map->deleteFace(dd,false);
Sylvain Thery's avatar
Sylvain Thery committed
1197
			else
1198
				mf.markOrbit<FACE>(dd);
Sylvain Thery's avatar
Sylvain Thery committed
1199
		}
1200
		else m_map->next(d);
Sylvain Thery's avatar
Sylvain Thery committed
1201
	}
1202 1203 1204 1205 1206 1207 1208 1209
	m_map->closeMap();

////	 VERSION USING DELETE FACE WITH BOUNDARY
//	DartMarker mf(*m_map);
//	std::vector<Dart> vecF;
//	vecF.reserve(8192);
//	for (Dart d = m_map->begin(); d != m_map->end();m_map->next(d))	// next done inside loop because of deleteFace
//	{
Thery Sylvain's avatar
Thery Sylvain committed
1210
//		if ((!mf.isMarked(d)) && (!m_map->isBoundaryMarked2(d)) )
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
//		{
//			Dart dd = d;
//			Dart e = m_map->phi1(d);
//			Dart f = m_map->phi1(e);
//			if ((boundVertices[dd]!=0) && (boundVertices[e]!=0) && (boundVertices[f]!=0))
//				vecF.push_back(d);
//			mf.markOrbit(FACE,dd);
//		}
//	}
//	for (std::vector<Dart>::iterator it = vecF.begin(); it != vecF.end(); ++it)
//		m_map->deleteFace(*it);
Sylvain Thery's avatar
Sylvain Thery committed
1222 1223
}

1224
template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
Sylvain Thery's avatar
Sylvain Thery committed
1225
void MarchingCube<DataType, Windowing, PFP>::recalPoints(const Geom::Vec3f& origin)
1226 1227 1228
{
	for(unsigned int i=m_positions.begin(); i != m_positions.end(); m_positions.next(i))
	{
1229
		VEC3& P = m_positions[i];
1230
		P -= m_fOrigin;
Pierre Kraemer's avatar
Pierre Kraemer committed
1231 1232 1233
		P[0] = (P[0]+0.5f) * m_fScal[0];
		P[1] = (P[1]+0.5f) * m_fScal[1];
		P[2] = (P[2]+0.5f) * m_fScal[2];
Sylvain Thery's avatar
Sylvain Thery committed
1234
		P+=origin;
1235 1236
	}
}
Sylvain Thery's avatar
Sylvain Thery committed
1237

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249

#ifdef MC_WIDTH_EDGE_Z_EMBEDED
template< typename  DataType, template < typename D2 > class Windowing, typename PFP >
void MarchingCube<DataType, Windowing, PFP>::setZSliceAttrib(EdgeAttribute<unsigned long long>* zsatt, unsigned int zbound, unsigned int nbZone)
{
	m_zslice = zsatt;
	m_zbound = zbound;
	m_sliceGroup = m_Image->getWidthZ()/nbZone;
}
#endif


1250
} // namespace MC
Sylvain Thery's avatar
Sylvain Thery committed
1251

1252
} // namespace Surface
1253

1254
} // namespace Algo
Sylvain Thery's avatar
Sylvain Thery committed
1255

1256
} // namespace CGoGN