File: VoxelGridLoader.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88,688 kB
  • ctags: 27,205
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 67
file content (430 lines) | stat: -rw-r--r-- 13,996 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
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
223
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
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
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* 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.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/

#include <sofa/component/container/VoxelGridLoader.h>

#include <sofa/core/ObjectFactory.h>
#include <sofa/helper/io/Image.h>
#include <sofa/helper/io/ImageRAW.h>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>

namespace sofa
{

  namespace component
  {
    using namespace sofa::defaulttype;

    SOFA_DECL_CLASS ( VoxelGridLoader );

    int VoxelGridLoaderClass = core::RegisterObject ( "Voxel Grid Loader" )
                               .add< VoxelGridLoader >()
                               ;

    VoxelGridLoader::VoxelGridLoader()
        : MeshLoader(),
        voxelSize ( initData ( &voxelSize, Vector3 ( 1.0f,1.0f,1.0f ), "voxelSize", "Dimension of one voxel" ) ),
        dataResolution ( initData ( &dataResolution, Vec3i ( 0,0,0 ), "resolution", "Resolution of the voxel file" ) ),
		roi ( initData ( &roi, Vec6i ( 0,0,0, 0xFFFF, 0xFFFF, 0xFFFF ), "ROI", "Region of interest (xmin, ymin, zmin, xmax, ymax, zmax)" ) ),
        headerSize ( initData ( &headerSize, 0, "header", "Header size in bytes" ) ),
	    segmentationFile( initData(&segmentationFile,"segmentationFile","Segmentation file")),
        segmentationHeaderSize ( initData ( &segmentationHeaderSize, 0, "segmentationHeader", "Header size in bytes" ) ),
		backgroundValue ( initData ( &backgroundValue, "bgValue", "Background values (to be ignored)" ) ),
        activeValue ( initData ( &activeValue, "dataValue", "Active data values" ) ),
	generateHexa( initData ( &generateHexa, true, "generateHexa", "Interpret voxel as either hexa or points")),
	image(NULL),
	segmentation(NULL),
	bpp(8) // bits per pixel
    {	}

	VoxelGridLoader::~VoxelGridLoader()
	{
		clear();
	}

    void VoxelGridLoader::init()
	{
		clear();

		const Vec3i &res = dataResolution.getValue();
		Vec6i&	ROI = (* roi.beginEdit());
		if(ROI[0] < 0)	ROI[0] = 0;
		if(ROI[1] < 0)	ROI[1] = 0;
		if(ROI[2] < 0)	ROI[2] = 0;
		if(ROI[3] >= res[0]) ROI[3] = res[0] - 1;
		if(ROI[4] >= res[1]) ROI[4] = res[1] - 1;
		if(ROI[5] >= res[2]) ROI[5] = res[2] - 1;
		if(ROI[0] > ROI[3]) ROI[3] = ROI[0];
		if(ROI[1] > ROI[4]) ROI[4] = ROI[1];
		if(ROI[2] > ROI[5]) ROI[5] = ROI[2];
		roi.endEdit();

		if(!filename.getValue().empty())
                        image = loadImage(filename.getFullPath(), res, headerSize.getValue());

		if ( image == NULL )
			return;
#ifndef NDEBUG
		else
			std::cout << "[VoxelGridLoader::init] file " << filename.getValue() << " loaded." << std::endl;
#endif

		if(!segmentationFile.getValue().empty())
                        segmentation = loadImage(segmentationFile.getFullPath(), res, segmentationHeaderSize.getValue());

		reinit();
	}

	void VoxelGridLoader::reinit()
	{
      seqPoints.clear();
	  seqHexas.clear();
	  _idxInRegularGrid.clear();

	  const Vec6i&	ROI = roi.getValue();

	  if(generateHexa.getValue())
	  {
		  const unsigned int numVoxelsX = dataResolution.getValue()[0];
		  const unsigned int numVoxelsY = dataResolution.getValue()[1];
	//	  const unsigned int numVoxelsZ = dataResolution.getValue()[2];

	//    const unsigned int numVoxels = numVoxelsX * numVoxelsY * numVoxelsZ;

		  const unsigned int numPointsX = numVoxelsX + 1;
		  const unsigned int numPointsY = numVoxelsY + 1;
	//	  const unsigned int numPointsZ = numVoxelsZ + 1;

	//    const unsigned int numPoints = numPointsX * numPointsY * numPointsZ;

		  std::set<unsigned int> keepPoint;

		  for ( unsigned int k=(unsigned)ROI[2]; k<=(unsigned)ROI[5]; ++k )
			for ( unsigned int j=(unsigned)ROI[1]; j<=(unsigned)ROI[4]; ++j )
			  for ( unsigned int i=(unsigned)ROI[0]; i<=(unsigned)ROI[3]; ++i )
			  {
				unsigned int idx = i + j * numVoxelsX + k * numVoxelsX * numVoxelsY;

				if ( isActive(idx) )
				{
				  for ( unsigned int q=0; q<8; ++q )
				  {
					const unsigned int pidx = ( i + ( q % 2 ) ) + ( j + ( ( q & 2 ) >> 1 ) ) * numPointsX + ( k + ( q / 4 ) ) * numPointsX * numPointsY;
					keepPoint.insert ( pidx );
				  }
				}
			  }

		  std::cout << "[VoxelGridLoader::init] inserting " << keepPoint.size() << " points ... ";

		  unsigned int pointIdx = 0;
		  seqPoints.resize ( keepPoint.size() );
		  std::map<unsigned int, unsigned int>  renumberingMap;
		  for ( unsigned int k=(unsigned)ROI[2]; k<=(unsigned)ROI[5]+1; ++k )
			for ( unsigned int j=(unsigned)ROI[1]; j<=(unsigned)ROI[4]+1; ++j )
			  for ( unsigned int i=(unsigned)ROI[0]; i<=(unsigned)ROI[3]+1; ++i )
			  {
				// add only points that were used above
				const unsigned int pidx = i + j * numPointsX + k * numPointsX * numPointsY;
				if ( keepPoint.find ( pidx ) != keepPoint.end() )
				{
				  renumberingMap[pidx] = pointIdx;
				  Point& pnt = seqPoints[pointIdx];
				  pnt[0] = i*voxelSize.getValue()[0];
				  pnt[1] = j*voxelSize.getValue()[1];
				  pnt[2] = k*voxelSize.getValue()[2];
				  pointIdx++;
				}
			  }
		  keepPoint.clear();

		  std::cout << " done. " << std::endl;

		  std::cout << "[VoxelGridLoader::init] inserting hexa ... ";

		  for ( unsigned int k=(unsigned)ROI[2]; k<=(unsigned)ROI[5]; ++k )
			for ( unsigned int j=(unsigned)ROI[1]; j<=(unsigned)ROI[4]; ++j )
			  for ( unsigned int i=(unsigned)ROI[0]; i<=(unsigned)ROI[3]; ++i )
			  {
				unsigned int idx = i + j * numVoxelsX + k * numVoxelsX * numVoxelsY;

				if ( isActive(idx) )
				{
				  unsigned int p[8];
				  for ( unsigned int q=0; q<8; ++q )
				  {
					p[q] = renumberingMap[ ( i + ( q % 2 ) ) + ( j + ( ( q & 2 ) >> 1 ) ) * numPointsX + ( k + ( q / 4 ) ) * numPointsX * numPointsY];
				  }

	#ifdef SOFA_NEW_HEXA
				  seqHexas.push_back ( Hexa ( p[0], p[1], p[3], p[2], p[4], p[5], p[7], p[6] ) );
	#else
				  seqHexas.push_back ( Hexa ( p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7] ) );
	#endif
				  _idxInRegularGrid.push_back ( idx );
				}
			  }

		  std::cout << "done. (" << seqHexas.size() << ")" << std::endl;
	  }
	  else
	  {
		  const unsigned int numVoxelsX = dataResolution.getValue()[0];
		  const unsigned int numVoxelsY = dataResolution.getValue()[1];
	//	  const unsigned int numVoxelsZ = dataResolution.getValue()[2];

		  std::cout << "[VoxelGridLoader::init] inserting points ... ";

		  for ( unsigned int k=(unsigned)ROI[2]; k<=(unsigned)ROI[5]; ++k )
			for ( unsigned int j=(unsigned)ROI[1]; j<=(unsigned)ROI[4]; ++j )
			  for ( unsigned int i=(unsigned)ROI[0]; i<=(unsigned)ROI[3]; ++i )
			  {
				unsigned int idx = i + j * numVoxelsX + k * numVoxelsX * numVoxelsY;

				if ( isActive(idx) )
				{
				  Point pnt;
				  pnt[0] = i*voxelSize.getValue()[0];
				  pnt[1] = j*voxelSize.getValue()[1];
				  pnt[2] = k*voxelSize.getValue()[2];

				  seqPoints.push_back(pnt);
				  _idxInRegularGrid.push_back ( idx );
				}
			  }

		  std::cout << "done. (" << seqPoints.size() << ")" << std::endl;
	  }
    }

    void VoxelGridLoader::clear()
    {
      MeshLoader::clear();
	  _idxInRegularGrid.clear();

      if(image != NULL)
		  delete image;
	  image = NULL;

	  if(segmentation != NULL)
		  delete segmentation;
	  segmentation = NULL;
    }

	void  VoxelGridLoader::getIndicesInRegularGrid(helper::vector<unsigned int>& idxInRegularGrid) const
	{
      idxInRegularGrid.assign ( _idxInRegularGrid.begin(), _idxInRegularGrid.end() );
	}

    bool VoxelGridLoader::load ( const char* filename )
    {
      clear();

      std::string _filename ( filename );

	  image = loadImage(_filename, dataResolution.getValue(), headerSize.getValue());

	  if(image != NULL)
	  {
	      setFilename ( _filename );
		  return true;
	  }
	  else
	      return false;
    }

    helper::io::Image* VoxelGridLoader::loadImage ( const std::string& filename, const Vec3i& res, const int hsize ) const
	{
	  helper::io::Image* image = NULL;

      std::string _filename ( filename );

	  if(res.norm2() > 0 && bpp > 0)
	  {
		  if ( _filename.length() > 4 && _filename.compare ( _filename.length()-4, 4, ".raw" ) ==0 )
		  {
			helper::io::ImageRAW *imageRAW = new helper::io::ImageRAW();
			imageRAW->init(res[0], res[1], res[2], bpp, hsize);
			if(imageRAW->load( _filename ))
				image = imageRAW;
		  }
	  }

	  if(image == NULL)
      {
		  std::cout << "Unable to load file " <<  _filename << std::endl;
      }

	  return image;
	}


	int VoxelGridLoader::getDataSize() const
	{
		return dataResolution.getValue()[0]*dataResolution.getValue()[1]*dataResolution.getValue()[2];
	}

    void VoxelGridLoader::setResolution ( const Vec3i res )
    {
      ( *dataResolution.beginEdit() ) = res;
      dataResolution.endEdit();
    }

    void VoxelGridLoader::getResolution ( Vec3i& res ) const
    {
      res = dataResolution.getValue();
    }

    void VoxelGridLoader::setVoxelSize ( const defaulttype::Vector3 vSize )
    {
      ( *voxelSize.beginEdit() ) = vSize;
      voxelSize.endEdit();
    }

    void VoxelGridLoader::getVoxelSize ( defaulttype::Vector3& vSize ) const
    {
      vSize = voxelSize.getValue();
    }

    void VoxelGridLoader::addBackgroundValue ( const int value )
	{
		helper::vector<int>& vecVal = ( *backgroundValue.beginEdit() );
		vecVal.push_back(value);
		std::sort(vecVal.begin(), vecVal.end());
		std::unique(vecVal.begin(), vecVal.end());
		backgroundValue.endEdit();
		reinit();
	}

    int VoxelGridLoader::getBackgroundValue(const unsigned int idx) const
    {
		const helper::vector<int>& vecVal = backgroundValue.getValue();
		if(idx < vecVal.size())
			return vecVal[idx];
		else
			return -1;
    }

	void VoxelGridLoader::addActiveDataValue(const int value)
	{
		helper::vector<int>& vecVal = ( *activeValue.beginEdit() );
		vecVal.push_back(value);
		std::sort(vecVal.begin(), vecVal.end());
		std::unique(vecVal.begin(), vecVal.end());
		activeValue.endEdit();
		reinit();
	}

	int VoxelGridLoader::getActiveDataValue(const unsigned int idx) const
	{
		const helper::vector<int>& vecVal = activeValue.getValue();
		if(idx < vecVal.size())
			return vecVal[idx];
		else
			return -1;
	}

	unsigned char * VoxelGridLoader::getData()
	{
		return image->getData();
	}

	const unsigned char * VoxelGridLoader::getData() const
	{
		return image->getData();
	}

  VoxelGridLoader::Vec6i VoxelGridLoader::getROI() const
  {
    return roi.getValue();
  }

	bool VoxelGridLoader::isActive(const unsigned int idx) const
	{
		const helper::vector<int>& activeVal = activeValue.getValue();
		const helper::vector<int>& bgVal = backgroundValue.getValue();

		if(activeVal.empty() && bgVal.empty())
			return true;

		helper::io::Image* img = (segmentation == NULL) ? image : segmentation;
		const unsigned char value = img->getData()[idx];

		if(!activeVal.empty()) // active values were specified
		{
			if(std::binary_search(activeVal.begin(), activeVal.end(), value))
				return true;
			else
				return false;
		}

		if(!bgVal.empty()) // background values were specified
		{
			if(std::binary_search(bgVal.begin(), bgVal.end(), value))
				return false;
			else
				return true;
		}

		return true;
	}

	// fill the texture by 'image' only where there is the 'segmentation' of 'activeValue' and give the 3D texture sizes
	void VoxelGridLoader::createSegmentation3DTexture( unsigned char **textureData, int& width, int& height, int& depth)
	{
		const Vec3i &resol = dataResolution.getValue();

		// with, height and depth are the nearest power of 2 greater or equal to the resolution
		for(width=1; resol[0]>width; width <<= 1) ;
		for(height=1; resol[1]>height; height <<= 1) ;
		for(depth=1; resol[2]>depth; depth <<= 1) ;

		int textureSize = width*height*depth;

		*textureData = new unsigned char [textureSize];
		for(unsigned char *ptr = (*textureData) + textureSize; ptr != *textureData; )
			*(--ptr) = (unsigned char) 0;

		const unsigned char *data = getData();

		// for all "active" data voxels
		for(unsigned i=0; i<_idxInRegularGrid.size(); ++i)
		{
			const int idxData = _idxInRegularGrid[i];
			const int I = idxData % resol[0];
			const int J = (idxData/resol[0]) % resol[1];
			const int K = idxData / (resol[0] * resol[1]);
			const int idxTexture = I + (J + K * height) * width;
			(*textureData)[idxTexture] = data[idxData];
		}
	}

  } // namespace component

} // namespace sofa