image.hpp 11.6 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

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

#include "img3D_IO.h"
#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 >
Image<DataType>::Image(DataType *data, int32 wx, int32 wy, int32 wz, float sx, float sy, float sz, bool copy ):
	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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
		exit(0);
	}

	// read size
	fp.read(reinterpret_cast<char*>(&m_WX),sizeof(int32));
	fp.read(reinterpret_cast<char*>(&m_WY),sizeof(int32));
	fp.read(reinterpret_cast<char*>(&m_WZ),sizeof(int32));

	m_WXY = m_WX * m_WY;

	int32 total = m_WXY * m_WZ;

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

	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)
{
	CGoGN::Utils::Img3D_IO::initIO();

	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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
		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)
		{
226
			CGoGNerr << "problem loading image" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
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
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
541
542
543
544
545
546
547
548
549
550
551
			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 >
Image<DataType>* Image<DataType>::addFrame(int frameMax)
{

	float minVS = std::max(m_SX, std::max(m_SY, m_SZ));
	float realFS = static_cast<float>(frameMax) * minVS;

    // real frame size for anisotropic images
    	int32 lFX = static_cast<int32>( ceilf( realFS / m_SX) );
	int32 lFY = static_cast<int32>( ceilf( realFS / m_SY) );
	int32 lFZ = static_cast<int32>( ceilf( realFS / m_SZ) );

	int lTx = m_WX+2*lFX;
	int lTy = m_WY+2*lFY;
	int lTz = m_WZ+2*lFZ;
	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;
	int32 sizeFrameZ = lTxy * lFZ;

	// frame Z upper
	for(int32 i=0; i<sizeFrameZ; i++)
	{
		*data++ = Zero;
	}

	int32 nbsl = lTz - 2*lFZ;
	for(int32 i=0; i<nbsl; i++)
	{
		int32 sizeFrameY = lTx*lFY;
		// frame Y upper
		for(int32 j=0; j<sizeFrameY; j++)
		{
			*data++ = Zero;
		}

		int32 nbrow = lTy - 2*lFY;
		for(int32 k=0; k<nbrow; k++)
		{
			// frame X upper
			for(int32 l=0; l<	lFX; l++)
			{
				*data++ = Zero;
			}

			// copy original Data
			int32 nbcol = lTx - 2*lFX;
			for(int32 l=0; l<nbcol; l++)
			{
				*data++ = *original++;
			}

			// frame X lower
			for(int32 l=0; l<	lFX; l++)
			{
				*data++ = Zero;
			}

		}

		// frame Y upper
		for(int32 j=0; j<sizeFrameY; j++)
		{
			*data++ = Zero;
		}
	}

	// frame Z lower
	for(int32 i=0; i<sizeFrameZ; i++)
	{
		*data++ = Zero;
	}

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

	// set origin of real data in image
	newImg->setOrigin(m_OX+lFX, m_OY+lFY, m_OZ+lFZ);

	return newImg;
}


template< typename  DataType >
template< typename Windowing >
float Image<DataType>::computeVolume(const Windowing& wind) const
{
	int32 nbv = getWidthXY()*getWidthZ();

	const DataType *data = getData();

	// volume in number of voxel
	int32 vol=0;

	for(int32 i=0; i<nbv; i++)
	{
		if (wind.inside(*data))
		{
			vol++;
		}
		data++;
	}


	return float(vol);
}

template< typename  DataType >
void Image<DataType>::Blur3()
{
	int txm = m_WX-1;
	int tym = m_WY-1;
	int tzm = m_WZ-1;


	// coef sur le mask 3x3: 8/4/2/1 pour vox/6-vois/18-vois/26-vois

	for(int x=1; x<txm; ++x)
	{
		for(int y=1; y<tym; ++y)
		{
			for(int z=1; z<tzm; ++z)
			{

				DataType* ptr = getVoxelPtr(x,y,z) - m_WXY - m_WX -1;
				DataType* ori = ptr;
				double val=0;
// 				for (int i=0; i<3;++i)
// 				{
// 					double coef=1.0f;
// 					if (i%2)
// 						coef=2.0f;
//
// 					val += coef*(*ptr++);
// 					val += 2.0*coef*(*ptr++);
// 					val += coef*(*ptr);
// 					ptr += m_WX;
// 					val += 2.0*coef*(*ptr--);
// 					val += 4.0*coef*(*ptr--);
// 					val += 2.0*coef*(*ptr);
// 					ptr += m_WX;
// 					val += coef*(*ptr++);
// 					val += 2.0*coef*(*ptr++);
// 					val += coef*(*ptr);
// 					ptr += m_WXY -( 2+m_WX*2);
// 				}
// 				val /= 64.0;
				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);
				}
				val /= 27.0;

				*ori= DataType(val);
			}
		}
	}
}

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++)
			{
				gmtl::Vec3f v((float)x,(float)y,(float)z);
				float fLength =  gmtl::lengthSquared(v);
				// 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)
{
	int noir =0;
	int blanc = 0;

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

	if (blanc > noir)
	{
		return 1.0f - ((float)noir) / ((float)blanc);
	}
	else
	{
		return 1.0f - ((float)blanc)/((float)noir);
	}
}


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