image.hpp 18.3 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
31
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

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

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
32
#include "Utils/img3D_IO.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "Zinrimage.h"

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 >
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193

	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;
}

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)
	{
194
		CGoGNerr << " ERROR : try to load an image of type " << typeid(DataType).name() << " although file ";
Pierre Kraemer's avatar
Pierre Kraemer committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
		return false;
	}

	m_Alloc=true;

	return true;
}


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)
		{
223
			CGoGNerr << "problem loading image" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
			return false;
		}

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

		return true;
}




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

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

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

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

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

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

		}

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

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

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

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

	return newImg;
381

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


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

	const DataType *data = getData();

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

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


	return float(vol);
}

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

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

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
466
	}
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
467
468
469
470
471
472
	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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
}

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++)
			{
Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
496
497
				Geom::Vec3f v((float)x,(float)y,(float)z);
				float fLength =  v.norm2();
Pierre Kraemer's avatar
Pierre Kraemer committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
				// 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);
				}
			}
		}
	}
}

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
513
	int noir = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
514
515
	int blanc = 0;

Sylvain Thery's avatar
MAJ MC    
Sylvain Thery committed
516

Pierre Kraemer's avatar
Pierre Kraemer committed
517
518
519
520
521
522
523
524
525
526
527
528
529
	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
530
	if (blanc >= noir)
Pierre Kraemer's avatar
Pierre Kraemer committed
531
532
533
534
535
536
537
538
539
540
	{
		return 1.0f - ((float)noir) / ((float)blanc);
	}
	else
	{
		return 1.0f - ((float)blanc)/((float)noir);
	}
}


Sylvain Thery's avatar
Sylvain Thery committed
541
542
543
544
545
546
547
548
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
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
// 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 >
711
void Image<DataType>::addCross()
Sylvain Thery's avatar
Sylvain Thery committed
712
{
713
714
715
	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
716

717
	for (int z = zm; z < zm+20; z++)
Sylvain Thery's avatar
Sylvain Thery committed
718
	{
719
720
721
722
723
724
725
		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
726
727
	}

728
	for (int z = zm; z < zm+20; z++)
Sylvain Thery's avatar
Sylvain Thery committed
729
	{
730
		for (int y = ym; y < ym+20; y++)
Sylvain Thery's avatar
Sylvain Thery committed
731
		{
732
			for (int x = 0 ; x < m_WX; x++)
Sylvain Thery's avatar
Sylvain Thery committed
733
			{
734
				m_Data[x + m_WX*y + m_WXY*z]=DataType(255);
Sylvain Thery's avatar
Sylvain Thery committed
735
736
737
738
			}
		}
	}

739
740
741
742
743
744
745
746
747
748
	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
749
750
751
752
}



753

Sylvain Thery's avatar
Sylvain Thery committed
754
755
756
757
758
759
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
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
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
870
871
872
873
} // end namespace
} // end namespace
} // end namespace