image.hpp 19.9 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
32
33
34
35
36
37
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
#include <math.h>
#include <typeinfo>

namespace CGoGN
{

namespace Algo
{

38
39
40
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
namespace MC
{

template< typename  DataType >
Image<DataType>::Image():
	m_Data	(NULL),
	m_OX	(0),
	m_OY	(0),
	m_OZ	(0)
{
}



template< typename  DataType >
56
Image<DataType>::Image(DataType *data, int wx, int wy, int wz, float sx, float sy, float sz, bool copy ):
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	m_WX   (wx),
	m_WY   (wy),
	m_WZ   (wz),
	m_OX   (0),
	m_OY   (0),
	m_OZ   (0),
	m_SX   (sx),
	m_SY   (sy),
	m_SZ   (sz)
{
	if ( copy )
	{
		m_Data = new DataType[wx*wy*wz];
		memcpy( m_Data, data, wx*wy*wz*sizeof(DataType) );
		m_Alloc=true;
	}
	else
	{
		m_Data = data;
		m_Alloc=false;
	}

	m_WXY = m_WX * m_WY;
}



template< typename  DataType >
void Image<DataType>::loadRaw(char *filename)
{
	std::ifstream fp( filename, std::ios::in|std::ios::binary);
	if (!fp.good())
	{
90
		CGoGNerr << "Mesh_Base::loadRaw: Unable to open file " << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
91
92
93
94
		exit(0);
	}

	// read size
95
96
97
	fp.read(reinterpret_cast<char*>(&m_WX),sizeof(int));
	fp.read(reinterpret_cast<char*>(&m_WY),sizeof(int));
	fp.read(reinterpret_cast<char*>(&m_WZ),sizeof(int));
Pierre Kraemer's avatar
Pierre Kraemer committed
98
99
100

	m_WXY = m_WX * m_WY;

101
	int total = m_WXY * m_WZ;
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

	m_Data = new DataType[total];
	// read data
	fp.read( reinterpret_cast<char*>(m_Data),total*sizeof(DataType) );

	m_SX = 1.0;
	m_SY = 1.0;
	m_SZ = 1.0;

	m_Alloc=true;

}



template< typename  DataType >
void Image<DataType>::loadVox(char *filename)
{
	std::ifstream in(filename);
	if (!in)
	{
123
		CGoGNerr << "Mesh_Base::loadVox: Unable to open file " << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
		exit(0);
	}

	m_SX = 1.0 ;
	m_SY = 1.0 ;
	m_SZ = 1.0 ;

	// read encoding
	while(in.good())
	{
		char line[1024] ;
		in.getline(line, 1024) ;
		std::istringstream line_input(line) ;
		std::string keyword ;
		line_input >> keyword ;

		if(keyword == "encoding") {
			std::string encoding_str ;
			line_input >> encoding_str ;
			if(encoding_str != "GRAY") {
144
				CGoGNerr << "loadVox : invalid encoding: \'" << encoding_str << "\'" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
				exit(0) ;
			}
		} else if(keyword == "nu") {
			line_input >> m_WX ;
		} else if(keyword == "nv") {
			line_input >> m_WY ;
		} else if(keyword == "nw") {
			line_input >> m_WZ ;
		} else if(keyword == "scale_u") {
			line_input >> m_SX ;
		} else if(keyword == "scale_v") {
			line_input >> m_SY ;
		} else if(keyword == "scale_w") {
			line_input >> m_SZ ;
		}
	}

	m_WXY = m_WX * m_WY;
163
	int total = m_WXY * m_WZ;
Pierre Kraemer's avatar
Pierre Kraemer committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

	m_Data = new DataType[total];

	int filename_s = strlen(filename) ;
	char datafile[filename_s] ;
	strcpy(datafile, filename) ;
	datafile[filename_s-3] = 'r' ;
	datafile[filename_s-2] = 'a' ;
	datafile[filename_s-1] = 'w' ;

	std::ifstream fp(datafile, std::ios::in|std::ios::binary);
	fp.read(reinterpret_cast<char*>(m_Data), total*sizeof(DataType));

	m_Alloc=true;
}

Sylvain Thery's avatar
Sylvain Thery committed
180
#ifdef WITH_QT
Pierre Kraemer's avatar
Pierre Kraemer committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
template< typename  DataType >
bool Image<DataType>::loadPNG3D(const char* filename)
{

	int tag;
	//en fonction de DataType utiliser la bonne fonction de chargement,
	if ( (typeid(DataType)==typeid(char)) ||  (typeid(DataType)==typeid(unsigned char)) )
		m_Data = reinterpret_cast<DataType*>(CGoGN::Utils::Img3D_IO::loadVal_8(const_cast<char*>(filename), m_WX,m_WY,m_WZ, m_SX,m_SY,m_SZ,tag));

	m_WXY = m_WX * m_WY;


	if (m_Data==NULL)
	{
195
		CGoGNerr << " ERROR : try to load an image of type " << typeid(DataType).name() << " although file ";
Pierre Kraemer's avatar
Pierre Kraemer committed
196
197
198
199
200
201
202
		return false;
	}

	m_Alloc=true;

	return true;
}
Sylvain Thery's avatar
Sylvain Thery committed
203
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
204

Sylvain Thery's avatar
Sylvain Thery committed
205
#ifdef WITH_ZINRI
Pierre Kraemer's avatar
Pierre Kraemer committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
template< typename  DataType >
bool Image<DataType>::loadInrgz(const char* filename)
{
		mImage = readZInrimage(filename);

		// A modifier en verifiant le type de donne
		// contenu dans l'image.

		m_Data = (DataType*)mImage->data;
		m_Alloc = false;
		m_WX = mImage->ncols;
		m_WY = mImage->nrows;
		m_WZ = mImage->nplanes;


		m_WXY = m_WX*m_WY;

		if (m_Data==NULL)
		{
225
			CGoGNerr << "problem loading image" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
226
227
228
229
230
231
232
233
234
			return false;
		}

		m_SX = 1.0;
		m_SY = 1.0;
		m_SZ = 1.0;

		return true;
}
Sylvain Thery's avatar
Sylvain Thery committed
235
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303



template< typename  DataType >
bool Image<DataType>::loadIPB(const char* filename)
{
	// chargement fichier

	// taille de l'image en X
	m_WX= 256;// A MODIFIER
	// taille de l'image en Y
	m_WY= 256;// A MODIFIER
	// taille de l'image en Z
	m_WZ= 256;// A MODIFIER
// taille d'une coupe XY de l'image
	m_WXY = m_WX * m_WY;

	// taille des voxels en
	m_SX= 1.0f;// A MODIFIER
	m_SY= 1.0f;// A MODIFIER
	m_SZ= 1.0f;// A MODIFIER

	// pointeur sur les donnees
	m_Data =  NULL;// A MODIFIER
	return true;
}




template< typename  DataType >
Image<DataType>::~Image()
{
	// ATTENTION A MODIFIER PEUT-ETRE SI m_data EST ALLOUE AILLEURS !!

	if ( m_Alloc && (m_Data != NULL))
	{
		delete[] m_Data;
	}
}


template< typename  DataType >
DataType Image<DataType>::getVoxel(int _lX, int _lY, int _lZ)
{
  return m_Data[_lX + m_WX*_lY + m_WXY*_lZ];
}


template< typename  DataType >
const DataType* Image<DataType>::getVoxelPtr(int lX, int lY, int lZ) const
{
	return m_Data + lX + m_WX*lY + m_WXY*lZ;
}


template< typename  DataType >
DataType* Image<DataType>::getVoxelPtr(int lX, int lY, int  lZ)
{
	return m_Data + lX + m_WX*lY + m_WXY*lZ;
}



/*
*  add a frame of Zero to the image
*/
template< typename  DataType >
304
Image<DataType>* Image<DataType>::addFrame(int frameWidth) const
Pierre Kraemer's avatar
Pierre Kraemer committed
305
{
306
307
308
	int lTx = m_WX+2*frameWidth;
	int lTy = m_WY+2*frameWidth;
	int lTz = m_WZ+2*frameWidth;
Pierre Kraemer's avatar
Pierre Kraemer committed
309
310
311
312
313
314
315
316
317
318
319
320
321
	int lTxy = lTx*lTy;

	// define Zero
	DataType Zero = DataType();

	// allocate new data
	DataType *newData = new DataType[lTxy*lTz];

	// get pointer to original data
	DataType *original = m_Data;


	DataType *data = newData;
322
	int sizeFrameZ = lTxy * frameWidth;
Pierre Kraemer's avatar
Pierre Kraemer committed
323
324

	// frame Z upper
325
	for(int i=0; i<sizeFrameZ; i++)
Pierre Kraemer's avatar
Pierre Kraemer committed
326
327
328
329
	{
		*data++ = Zero;
	}

330
331
	int nbsl = lTz - 2*frameWidth;
	for(int i=0; i<nbsl; i++)
Pierre Kraemer's avatar
Pierre Kraemer committed
332
	{
333
		int sizeFrameY = lTx*frameWidth;
Pierre Kraemer's avatar
Pierre Kraemer committed
334
		// frame Y upper
335
		for(int j=0; j<sizeFrameY; j++)
Pierre Kraemer's avatar
Pierre Kraemer committed
336
337
338
339
		{
			*data++ = Zero;
		}

340
341
		int nbrow = lTy - 2*frameWidth;
		for(int k=0; k<nbrow; k++)
Pierre Kraemer's avatar
Pierre Kraemer committed
342
343
		{
			// frame X upper
344
			for(int l=0; l< frameWidth; l++)
Pierre Kraemer's avatar
Pierre Kraemer committed
345
346
347
348
349
			{
				*data++ = Zero;
			}

			// copy original Data
350
351
			int nbcol = lTx - 2*frameWidth;
			for(int l=0; l<nbcol; l++)
Pierre Kraemer's avatar
Pierre Kraemer committed
352
353
354
355
356
			{
				*data++ = *original++;
			}

			// frame X lower
357
			for(int l=0; l< frameWidth; l++)
Pierre Kraemer's avatar
Pierre Kraemer committed
358
359
360
361
362
363
364
			{
				*data++ = Zero;
			}

		}

		// frame Y upper
365
		for(int j=0; j<sizeFrameY; j++)
Pierre Kraemer's avatar
Pierre Kraemer committed
366
367
368
369
370
371
		{
			*data++ = Zero;
		}
	}

	// frame Z lower
372
	for(int i=0; i<sizeFrameZ; i++)
Pierre Kraemer's avatar
Pierre Kraemer committed
373
374
375
376
377
378
379
	{
		*data++ = Zero;
	}

	Image<DataType>* newImg = new Image<DataType>(newData,lTx,lTy,lTz,getVoxSizeX(),getVoxSizeY(),getVoxSizeZ());

	// set origin of real data in image
380
	newImg->setOrigin(m_OX+frameWidth, m_OY+frameWidth, m_OZ+frameWidth);
Pierre Kraemer's avatar
Pierre Kraemer committed
381
382

	return newImg;
383

Pierre Kraemer's avatar
Pierre Kraemer committed
384
385
386
387
388
389
390
}


template< typename  DataType >
template< typename Windowing >
float Image<DataType>::computeVolume(const Windowing& wind) const
{
391
	int nbv = getWidthXY()*getWidthZ();
Pierre Kraemer's avatar
Pierre Kraemer committed
392
393
394
395

	const DataType *data = getData();

	// volume in number of voxel
396
	int vol=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
397

398
	for(int i=0; i<nbv; i++)
Pierre Kraemer's avatar
Pierre Kraemer committed
399
400
401
402
403
404
405
406
407
408
409
410
411
	{
		if (wind.inside(*data))
		{
			vol++;
		}
		data++;
	}


	return float(vol);
}

template< typename  DataType >
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
412
Image<DataType>* Image<DataType>::Blur3()
Pierre Kraemer's avatar
Pierre Kraemer committed
413
{
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
414

Pierre Kraemer's avatar
Pierre Kraemer committed
415
416
417
418
	int txm = m_WX-1;
	int tym = m_WY-1;
	int tzm = m_WZ-1;

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
419
420
421
422
	DataType* data2 = new DataType[m_WX*m_WY*m_WZ];
	Image<DataType>* newImg = new Image<DataType>(data2,m_WX,m_WY,m_WZ,getVoxSizeX(),getVoxSizeY(),getVoxSizeZ());
	newImg->m_Alloc=true;
	// set origin of real data in image ??
Pierre Kraemer's avatar
Pierre Kraemer committed
423

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
424
425
426
427
	// for frame
	for(int y=0; y<=tym; ++y)
		for(int x=0; x<=txm; ++x)
			*(newImg->getVoxelPtr(x,y,0)) = *(getVoxelPtr(x,y,0));
Pierre Kraemer's avatar
Pierre Kraemer committed
428

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
429
	for(int z=1; z<tzm; ++z)
Pierre Kraemer's avatar
Pierre Kraemer committed
430
	{
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
431
432
		for(int x=0; x<=txm; ++x)
			*(newImg->getVoxelPtr(x,0,z)) = *(getVoxelPtr(x,0,z));
Pierre Kraemer's avatar
Pierre Kraemer committed
433
434
		for(int y=1; y<tym; ++y)
		{
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
435
			*(newImg->getVoxelPtr(0,y,z)) = *(getVoxelPtr(0,y,z));
436
437

//			#pragma omp parallel for // OpenMP
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
438
			for(int x=1; x<txm; ++x)
Pierre Kraemer's avatar
Pierre Kraemer committed
439
			{
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
440
441
442
443
				DataType* ori = getVoxelPtr(x,y,z);
				DataType* dest = newImg->getVoxelPtr(x,y,z);
				DataType* ptr = ori - m_WXY - m_WX -1;
				double val=0.0;
Pierre Kraemer's avatar
Pierre Kraemer committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
				for (int i=0; i<3;++i)
				{
					val += (*ptr++);
					val += (*ptr++);
					val += (*ptr);
					ptr += m_WX;
					val += (*ptr--);
					val += (*ptr--);
					val += (*ptr);
					ptr += m_WX;
					val += (*ptr++);
					val += (*ptr++);
					val += (*ptr);
					ptr += m_WXY -( 2+m_WX*2);
				}
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
459
460
461
462
				val += 3.0 * (*ori);
				val /= (27.0 + 3.0);
				DataType res(val);
				*dest= res;
Pierre Kraemer's avatar
Pierre Kraemer committed
463
			}
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
464
			*(newImg->getVoxelPtr(txm,y,z)) = *(getVoxelPtr(txm,y,z));
Pierre Kraemer's avatar
Pierre Kraemer committed
465
		}
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
466
467
468
		for(int x=0; x<=txm; ++x)
			*(newImg->getVoxelPtr(x,tym,z)) = *(getVoxelPtr(x,tym,z));

Pierre Kraemer's avatar
Pierre Kraemer committed
469
	}
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
470
471
472
473
474
475
	for(int y=0; y<=tym; ++y)
		for(int x=1; x<txm; ++x)
			*(newImg->getVoxelPtr(x,y,tzm)) = *(getVoxelPtr(x,y,tzm));


	return newImg;
Pierre Kraemer's avatar
Pierre Kraemer committed
476
477
}

Sylvain Thery's avatar
Sylvain Thery committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//template<typename DataType>
//void Image<DataType>::createMaskOffsetSphere(std::vector<int>& table, int _i32radius)
//{
//	// compute the width of the sphere for memory allocation
//	int i32Width = 2*_i32radius + 1;
//	// squared radius
//    float fRad2 = (float)(_i32radius*_i32radius);
//
//	// memory allocation
//	// difficult to know how many voxels before computing,
//	// so the reserve for the BB
//	table.reserve(i32Width*i32Width*i32Width);
//	table.clear();
//
//	// scan all the BB of the sphere
//	for (int z = -_i32radius;  z<=_i32radius; z++)
//	{
//		for (int y = -_i32radius;  y<=_i32radius; y++)
//		{
//			for (int x = -_i32radius;  x<=_i32radius; x++)
//			{
//				Geom::Vec3f v((float)x,(float)y,(float)z);
//				float fLength =  v.norm2();
//				// if inside the sphere
//				if (fLength<=fRad2)
//				{
//					// the the index of the voxel
//					int index = z * m_WXY + y * m_WX + x;
//					table.push_back(index);
//				}
//			}
//		}
//	}
//}


Pierre Kraemer's avatar
Pierre Kraemer committed
514
515
516
template<typename DataType>
void Image<DataType>::createMaskOffsetSphere(std::vector<int>& table, int _i32radius)
{
Sylvain Thery's avatar
Sylvain Thery committed
517
518
519
520
521
522
	float smin = std::min(m_SX, std::min(m_SY,m_SZ));

	float xs = m_SX/smin;
	float ys = m_SY/smin;
	float zs = m_SZ/smin;

523
524
525
	int radX = ceil(float(_i32radius)/xs);
	int radY = ceil(float(_i32radius)/ys);
	int radZ = ceil(float(_i32radius)/zs);
Sylvain Thery's avatar
Sylvain Thery committed
526

527
	float sRadius = sqrt( double(_i32radius)/xs*double(_i32radius)/xs +  double(_i32radius)/ys*double(_i32radius)/ys +  double(_i32radius)/zs*double(_i32radius)/zs);
Pierre Kraemer's avatar
Pierre Kraemer committed
528
529
530

	// memory allocation
	// difficult to know how many voxels before computing,
531
532
	// so the reserve for the BB (not really a pb)
	table.reserve(radX*radY*radZ*8);
Pierre Kraemer's avatar
Pierre Kraemer committed
533
534
535
	table.clear();

	// scan all the BB of the sphere
536
	for (int z = -radZ; z<radZ; z++)
Pierre Kraemer's avatar
Pierre Kraemer committed
537
	{
538
		for (int y = -radY; y<radY; y++)
Pierre Kraemer's avatar
Pierre Kraemer committed
539
		{
540
			for (int x = -radX; x<radX; x++)
Pierre Kraemer's avatar
Pierre Kraemer committed
541
			{
Sylvain Thery's avatar
Sylvain Thery committed
542
				Geom::Vec3f v(float(x)*xs,float(y)*ys,float(z)*zs);
543
				float fLength =  v.norm();
Pierre Kraemer's avatar
Pierre Kraemer committed
544
				// if inside the sphere
545
				if (fLength<=sRadius)
Pierre Kraemer's avatar
Pierre Kraemer committed
546
547
548
549
550
551
552
553
554
555
				{
					// the the index of the voxel
					int index = z * m_WXY + y * m_WX + x;
					table.push_back(index);
				}
			}
		}
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
556

557
558
559
//template<typename DataType>
//float Image<DataType>::computeCurvatureCount(const DataType *ptrVox, const std::vector<int>& sphere, DataType val)

Pierre Kraemer's avatar
Pierre Kraemer committed
560
template<typename DataType>
561
float Image<DataType>::computeCurvatureCount(float x, float y, float z, const std::vector<int>& sphere, DataType val)
Pierre Kraemer's avatar
Pierre Kraemer committed
562
{
563
564
//	std::cout << "XYZ"<< x << " , "<<y<<" , "<<z <<"   => "<< int(round(x/m_SX)) << " , "<<int(round(y/m_SY))<<" , "<<int(round(z/m_SZ)) << std::endl;

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
565
	int noir = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
566
	int blanc = 0;
567
	const DataType *ptrVox = this->getVoxelPtr(int(ceil(x)), int(ceil(y)), int(ceil(z)));
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
568

Pierre Kraemer's avatar
Pierre Kraemer committed
569
570
571
572
573
574
575
576
577
578
579
580
581
	for (std::vector<int>::const_iterator it=sphere.begin(); it!=sphere.end();it++)
	{
		const DataType *data = ptrVox + *it;
		if (*data != val)
		{
			blanc++;
		}
		else
		{
			noir++;
		}
	}

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
582
	if (blanc >= noir)
Pierre Kraemer's avatar
Pierre Kraemer committed
583
584
585
586
587
588
589
590
591
592
	{
		return 1.0f - ((float)noir) / ((float)blanc);
	}
	else
	{
		return 1.0f - ((float)blanc)/((float)noir);
	}
}


Sylvain Thery's avatar
Sylvain Thery committed
593
594
595
596
597
598
599
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
644
645
646
647
648
649
650
651
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
699
700
701
702
703
704
705
706
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
// TESTING NEW CURVATURE METHODS
template<typename DataType>
void Image<DataType>::createMaskOffsetCylinders(std::vector<int>& tableX, std::vector<int>& tableY, std::vector<int>& tableZ, int _i32radius)
{
	// compute the width of the sphere for memory allocation
	int i32Width = 2*_i32radius + 1;
	// squared radius
    float fRad2 = (float)(_i32radius*_i32radius);

	// memory allocation
	// difficult to know how many voxels before computing,
	// so the reserve for the BB
	tableX.reserve(i32Width*i32Width*7);
	tableX.clear();
	tableY.reserve(i32Width*i32Width*7);
	tableY.clear();
	tableZ.reserve(i32Width*i32Width*7);
	tableZ.clear();


	// scan all the BB of the sphere
	for (int z = -_i32radius;  z<=_i32radius; z++)
	{
		for (int y = -_i32radius;  y<=_i32radius; y++)
		{
			for (int x = -_i32radius;  x<=_i32radius; x++)
			{
				Geom::Vec3f v((float)x,(float)y,(float)z);
				float fLength =  v.norm2();
				// if inside the sphere
				if (fLength<=fRad2)
				{
					// the the index of the voxel
					int index = z * m_WXY + y * m_WX + x;

					if ((x<=3) && (x>=-3))
						tableX.push_back(index);
					if ((y<=3) && (y>=-3))
						tableY.push_back(index);
					if ((z<=3) && (z>=-3))
						tableZ.push_back(index);

				}
			}
		}
	}
}

template<typename DataType>
float Image<DataType>::computeCurvatureCount3(const DataType *ptrVox, const std::vector<int>& cylX, const std::vector<int>& cylY, const std::vector<int>& cylZ, DataType val)
{
	int noir = 0;
	int blanc = 0;

	float vals[3];

	for (std::vector<int>::const_iterator it=cylX.begin(); it!=cylX.end();it++)
	{
		const DataType *data = ptrVox + *it;
		if (*data != val)
		{
			blanc++;
		}
		else
		{
			noir++;
		}
	}


	if (blanc >= noir)
	{
		vals[0] = 1.0f - ((float)noir) / ((float)blanc);
	}
	else
	{
		vals[0] =  -1.0f + ((float)blanc)/((float)noir);
	}

	noir = 0;
	blanc = 0;

	for (std::vector<int>::const_iterator it=cylY.begin(); it!=cylY.end();it++)
	{
		const DataType *data = ptrVox + *it;
		if (*data != val)
		{
			blanc++;
		}
		else
		{
			noir++;
		}
	}

	if (blanc >= noir)
	{
		vals[1] = 1.0f - ((float)noir) / ((float)blanc);
	}
	else
	{
		vals[1] =  -1.0f + ((float)blanc)/((float)noir);
	}


	noir = 0;
	blanc = 0;

	for (std::vector<int>::const_iterator it=cylZ.begin(); it!=cylZ.end();it++)
	{
		const DataType *data = ptrVox + *it;
		if (*data != val)
		{
			blanc++;
		}
		else
		{
			noir++;
		}
	}

	if (blanc >= noir)
	{
		vals[2] = 1.0f - ((float)noir) / ((float)blanc);
	}
	else
	{
		vals[2] =  -1.0f + ((float)blanc)/((float)noir);
	}

//	if ((valZ>valX) && (valZ>valY))
//		return (valX + valY)/2.0f;
//
//	if ((valY>valX) && (valY>valZ))
//		return (valX + valZ)/2.0f;
//
//	return (valY + valZ)/2.0f;

	unsigned short m1,m2;
Pierre Kraemer's avatar
Pierre Kraemer committed
732
	if ((fabs(vals[0]) < fabs(vals[1])) && (fabs(vals[0]) < fabs(vals[2])))
Sylvain Thery's avatar
Sylvain Thery committed
733
734
735
736
737
738
739
	{
		m1 =1;
		m2 =2;
	}
	else
	{
		m1=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
740
		if (fabs(vals[1]) < fabs(vals[2]))
Sylvain Thery's avatar
Sylvain Thery committed
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
			m2 = 2;
		else
			m2 = 1;
	}

	if (vals[m1]>0.0f)
		if (vals[m2]<0.0f)
			if ((vals[m1] - vals[m2])>0.8f)
				return 1.0f;

	if (vals[m1]<0.0f)
		if (vals[m2]>0.0f)
			if ((vals[m2] - vals[m1])>0.8f)
			return 1.0f;

Pierre Kraemer's avatar
Pierre Kraemer committed
756
	return std::max(std::max(fabs(vals[0]),fabs(vals[1])),fabs(vals[2]));
Sylvain Thery's avatar
Sylvain Thery committed
757
758
759
760
761
762

}



template< typename  DataType >
763
void Image<DataType>::addCross()
Sylvain Thery's avatar
Sylvain Thery committed
764
{
765
766
767
	int zm = m_WZ/2 - 10;
	int ym = m_WY/2 - 10;
	int xm = m_WX/2 - 10;
Sylvain Thery's avatar
Sylvain Thery committed
768

769
	for (int z = zm; z < zm+20; z++)
Sylvain Thery's avatar
Sylvain Thery committed
770
	{
771
772
773
774
775
776
777
		for (int x = xm; x < xm+20; x++)
		{
			for (int y = 0 ; y < m_WY; y++)
			{
				m_Data[x + m_WX*y + m_WXY*z]=DataType(255);
			}
		}
Sylvain Thery's avatar
Sylvain Thery committed
778
779
	}

780
	for (int z = zm; z < zm+20; z++)
Sylvain Thery's avatar
Sylvain Thery committed
781
	{
782
		for (int y = ym; y < ym+20; y++)
Sylvain Thery's avatar
Sylvain Thery committed
783
		{
784
			for (int x = 0 ; x < m_WX; x++)
Sylvain Thery's avatar
Sylvain Thery committed
785
			{
786
				m_Data[x + m_WX*y + m_WXY*z]=DataType(255);
Sylvain Thery's avatar
Sylvain Thery committed
787
788
789
790
			}
		}
	}

791
792
793
794
795
796
797
798
799
800
	for (int y = ym; y < ym+20; y++)
	{
		for (int x = xm; x < xm+20; x++)
		{
			for (int z = 0 ; z < m_WZ; z++)
			{
				m_Data[x + m_WX*y + m_WXY*z]=DataType(255);
			}
		}
	}
Sylvain Thery's avatar
Sylvain Thery committed
801
802
803
804
}



805

Sylvain Thery's avatar
Sylvain Thery committed
806
807
808
809
810
811
812
813
814
815
816
817
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
868
869
870
871
872
873
874
875
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
template<typename DataType>
void Image<DataType>::createNormalSphere(std::vector<Geom::Vec3f>& table, int _i32radius)
{
	// compute the width of the sphere for memory allocation
	int i32Width = 2*_i32radius + 1;

	table.reserve(i32Width*i32Width*i32Width);
	table.clear();

	// scan all the BB of the sphere
	for (int z = -_i32radius;  z<=_i32radius; z++)
	{
		for (int y = -_i32radius;  y<=_i32radius; y++)
		{
			for (int x = -_i32radius;  x<=_i32radius; x++)
			{
				Geom::Vec3f v((float)x,(float)y,(float)z);
				float fLength =  v.normalize();
				// if inside the sphere
				if (fLength<=_i32radius)
					table.push_back(v);
				else
					table.push_back(Geom::Vec3f(0.0f,0.0f,0.0f));
			}
		}
	}
}


template<typename DataType>
Geom::Vec3f Image<DataType>::computeNormal(DataType *ptrVox, const std::vector<Geom::Vec3f>& sphere, DataType val, unsigned int radius)
{

	DataType *ucDat1 = ptrVox - radius*(m_WX + m_WX*m_WY);
	Geom::Vec3f normal(0.0f,0.0f,0.0f);
	unsigned int width = 2*radius+1;

	unsigned int ind = 0;
	for (unsigned int i=0; i<width;++i)
	{
		DataType *ucDat2 = ucDat1;
		for (unsigned int j=0; j<width;++j)
		{
			DataType *ucDat3 = ucDat2;
			for (unsigned int k=0; k<width;++k)
			{
				if (*ucDat3 == val)
				{
					normal += sphere[ind];
				}
				++ucDat3;
				++ind;
			}

			ucDat2 += m_WX;
		}
		ucDat1 += m_WXY;
	}

	normal.normalize();
	return normal;
}

template<typename DataType>
bool Image<DataType>::checkSaddlecomputeNormal(const Geom::Vec3f& P, const Geom::Vec3f& normal, unsigned int radius)
{
	Geom::Vec3f V1;
	if ( (fabs(normal[0]) <fabs(normal[1])) && (fabs(normal[0]) <fabs(normal[2])) )
	{
		V1 = normal ^ Geom::Vec3f(1.0f,0.0f,0.0);
	}
	else if (fabs(normal[1]) <fabs(normal[2]))
	{
		V1 = normal ^ Geom::Vec3f(0.0f,1.0f,0.0);
	}
	else
	{
		V1 = normal ^ Geom::Vec3f(0.0f,0.0f,1.0);
	}

	Geom::Vec3f V2 = normal ^ V1;

	Geom::Vec3f Q = P + (0.5f * radius)*normal;

	float le = 0.866f * radius; // (cos30)
	Geom::Vec3f Q1 = Q + le*V1;
	Geom::Vec3f Q2 = Q - le*V1;
	DataType X1 = getVoxel(int(floor(Q1[0])), int(floor(Q1[1])), int(floor(Q1[2])));
	DataType X2 = getVoxel(int(floor(Q2[0])), int(floor(Q2[1])), int(floor(Q2[2])));
	Q1 = Q + le*V2;
	Q2 = Q - le*V2;
	DataType X3 = getVoxel(int(floor(Q1[0])), int(floor(Q1[1])), int(floor(Q1[2])));
	DataType X4 = getVoxel(int(floor(Q2[0])), int(floor(Q2[1])), int(floor(Q2[2])));

	le *= 0.707f; // (sqrt(2)/2)
	Q1 = Q + le*(V1+V2);
	Q2 = Q - le*(V1+V2);
	DataType X5 = getVoxel(int(floor(Q1[0])), int(floor(Q1[1])), int(floor(Q1[2])));
	DataType X6 = getVoxel(int(floor(Q2[0])), int(floor(Q2[1])), int(floor(Q2[2])));

	Q1 = Q + le*(V1-V2);
	Q2 = Q - le*(V1-V2);
	DataType X7 = getVoxel(int(floor(Q1[0])), int(floor(Q1[1])), int(floor(Q1[2])));
	DataType X8 = getVoxel(int(floor(Q2[0])), int(floor(Q2[1])), int(floor(Q2[2])));

	if  ((X1 == X2) && (X3==X4) && (X1!=X3))
		return true;

	if  ((X5 == X6) && (X7==X8) && (X5!=X7))
		return true;

	return false;
}



Pierre Kraemer's avatar
Pierre Kraemer committed
922
923
924
} // end namespace
} // end namespace
} // end namespace
925
}