Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

image.hpp 19.4 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

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

namespace CGoGN
{

namespace Algo
{

namespace MC
{

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



template< typename  DataType >
53
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
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
	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())
	{
87
		CGoGNerr << "Mesh_Base::loadRaw: Unable to open file " << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
88
89
90
91
		exit(0);
	}

	// read size
92
93
94
	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
95
96
97

	m_WXY = m_WX * m_WY;

98
	int total = m_WXY * m_WZ;
Pierre Kraemer's avatar
Pierre Kraemer committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

	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)
	{
120
		CGoGNerr << "Mesh_Base::loadVox: Unable to open file " << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
		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") {
141
				CGoGNerr << "loadVox : invalid encoding: \'" << encoding_str << "\'" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
				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;
160
	int total = m_WXY * m_WZ;
Pierre Kraemer's avatar
Pierre Kraemer committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

	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
177
#ifdef WITH_QT
Pierre Kraemer's avatar
Pierre Kraemer committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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)
	{
192
		CGoGNerr << " ERROR : try to load an image of type " << typeid(DataType).name() << " although file ";
Pierre Kraemer's avatar
Pierre Kraemer committed
193
194
195
196
197
198
199
		return false;
	}

	m_Alloc=true;

	return true;
}
Sylvain Thery's avatar
Sylvain Thery committed
200
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
201

Sylvain Thery's avatar
Sylvain Thery committed
202
#ifdef WITH_ZINRI
Pierre Kraemer's avatar
Pierre Kraemer committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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)
		{
222
			CGoGNerr << "problem loading image" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
223
224
225
226
227
228
229
230
231
			return false;
		}

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

		return true;
}
Sylvain Thery's avatar
Sylvain Thery committed
232
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
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
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



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 >
301
Image<DataType>* Image<DataType>::addFrame(int frameWidth) const
Pierre Kraemer's avatar
Pierre Kraemer committed
302
{
303
304
305
	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
306
307
308
309
310
311
312
313
314
315
316
317
318
	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;
319
	int sizeFrameZ = lTxy * frameWidth;
Pierre Kraemer's avatar
Pierre Kraemer committed
320
321

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

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

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

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

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

		}

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

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

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

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

	return newImg;
380

Pierre Kraemer's avatar
Pierre Kraemer committed
381
382
383
384
385
386
387
}


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

	const DataType *data = getData();

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

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


	return float(vol);
}

template< typename  DataType >
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
409
Image<DataType>* Image<DataType>::Blur3()
Pierre Kraemer's avatar
Pierre Kraemer committed
410
{
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
411

Pierre Kraemer's avatar
Pierre Kraemer committed
412
413
414
415
	int txm = m_WX-1;
	int tym = m_WY-1;
	int tzm = m_WZ-1;

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
416
417
418
419
	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
420

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
421
422
423
424
	// 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
425

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
426
	for(int z=1; z<tzm; ++z)
Pierre Kraemer's avatar
Pierre Kraemer committed
427
	{
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
428
429
		for(int x=0; x<=txm; ++x)
			*(newImg->getVoxelPtr(x,0,z)) = *(getVoxelPtr(x,0,z));
Pierre Kraemer's avatar
Pierre Kraemer committed
430
431
		for(int y=1; y<tym; ++y)
		{
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
432
433
434
			*(newImg->getVoxelPtr(0,y,z)) = *(getVoxelPtr(0,y,z));
			#pragma omp parallel for // OpenMP
			for(int x=1; x<txm; ++x)
Pierre Kraemer's avatar
Pierre Kraemer committed
435
			{
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
436
437
438
439
				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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
				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
455
456
457
458
				val += 3.0 * (*ori);
				val /= (27.0 + 3.0);
				DataType res(val);
				*dest= res;
Pierre Kraemer's avatar
Pierre Kraemer committed
459
			}
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
460
			*(newImg->getVoxelPtr(txm,y,z)) = *(getVoxelPtr(txm,y,z));
Pierre Kraemer's avatar
Pierre Kraemer committed
461
		}
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
462
463
464
		for(int x=0; x<=txm; ++x)
			*(newImg->getVoxelPtr(x,tym,z)) = *(getVoxelPtr(x,tym,z));

Pierre Kraemer's avatar
Pierre Kraemer committed
465
	}
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
466
467
468
469
470
471
	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
472
473
}

Sylvain Thery's avatar
Sylvain Thery committed
474
475
476
477
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
//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
510
511
512
template<typename DataType>
void Image<DataType>::createMaskOffsetSphere(std::vector<int>& table, int _i32radius)
{
Sylvain Thery's avatar
Sylvain Thery committed
513
514
515
516
517
518
519
	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;


Pierre Kraemer's avatar
Pierre Kraemer committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
	// 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++)
			{
Sylvain Thery's avatar
Sylvain Thery committed
538
				Geom::Vec3f v(float(x)*xs,float(y)*ys,float(z)*zs);
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
539
				float fLength =  v.norm2();
Pierre Kraemer's avatar
Pierre Kraemer committed
540
541
542
543
544
545
546
547
548
549
550
551
				// 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);
				}
			}
		}
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
552

Pierre Kraemer's avatar
Pierre Kraemer committed
553
554
555
template<typename DataType>
float Image<DataType>::computeCurvatureCount(const DataType *ptrVox, const std::vector<int>& sphere, DataType val)
{
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
556
	int noir = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
557
558
	int blanc = 0;

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
559

Pierre Kraemer's avatar
Pierre Kraemer committed
560
561
562
563
564
565
566
567
568
569
570
571
572
	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
573
	if (blanc >= noir)
Pierre Kraemer's avatar
Pierre Kraemer committed
574
575
576
577
578
579
580
581
582
583
	{
		return 1.0f - ((float)noir) / ((float)blanc);
	}
	else
	{
		return 1.0f - ((float)blanc)/((float)noir);
	}
}


Sylvain Thery's avatar
Sylvain Thery committed
584
585
586
587
588
589
590
591
592
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// 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;
	if ((abs(vals[0]) < abs(vals[1])) && (abs(vals[0]) < abs(vals[2])))
	{
		m1 =1;
		m2 =2;
	}
	else
	{
		m1=0;
		if (abs(vals[1]) < abs(vals[2]))
			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;

	return std::max(std::max(abs(vals[0]),abs(vals[1])),abs(vals[2]));

}



template< typename  DataType >
754
void Image<DataType>::addCross()
Sylvain Thery's avatar
Sylvain Thery committed
755
{
756
757
758
	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
759

760
	for (int z = zm; z < zm+20; z++)
Sylvain Thery's avatar
Sylvain Thery committed
761
	{
762
763
764
765
766
767
768
		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
769
770
	}

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

782
783
784
785
786
787
788
789
790
791
	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
792
793
794
795
}



796

Sylvain Thery's avatar
Sylvain Thery committed
797
798
799
800
801
802
803
804
805
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
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
913
914
915
916
} // end namespace
} // end namespace
} // end namespace