image.h 8.48 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef IMAGE_H
#define IMAGE_H


Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
29
#include "Geometry/vector_gen.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30

Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
31
#include "Utils/img3D_IO.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
32

Sylvain Thery's avatar
Sylvain Thery committed
33
#ifdef WITH_ZINRI
34
#include "Zinri/Zinrimage.h"
Sylvain Thery's avatar
Sylvain Thery committed
35
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
36 37 38 39 40 41 42

namespace CGoGN
{

namespace Algo
{

43 44 45
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
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
namespace MC
{

/**
 * voxel image class
 *
 * The class Image manage 3D voxel image
 * of any type
 * @param DataType the type of voxel
 */
template< typename  DataType >
class Image
{
protected:
	/**
	 * pointer to data
	 */
	DataType *m_Data;

	/**
	 * X size
	 */
	int m_WX;

	/**
	 * Y size
	 */
	int m_WY;

	/**
	 * Z size
	 */
	int m_WZ;

	/**
	 * slice size
	 */
83
	int m_WXY;
Pierre Kraemer's avatar
Pierre Kraemer committed
84 85 86 87 88


	/**
	 * X origin
	 */
89
	int m_OX;
Pierre Kraemer's avatar
Pierre Kraemer committed
90 91 92 93

	/**
	 * Y origin
	 */
94
	int m_OY;
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97 98

	/**
	 * Z origin
	 */
99
	int m_OZ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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


	/**
	 * Voxel X size
	 */
	float m_SX;

	/**
	 * Voxel Y size
	 */
	float m_SY;

	/**
	 * Voxel Z size
	 */
	float m_SZ;

	/**
	 * memory allocated ?
	 */
	bool m_Alloc;

	/**
	* Test if a point is in the image
	*
	* @param  _V the coordinates to test
	*/
	//bool correct(const gmtl::Vec3i& _V);

Sylvain Thery's avatar
Sylvain Thery committed
129
#ifdef WITH_ZINRI
Pierre Kraemer's avatar
Pierre Kraemer committed
130 131 132 133
	/**
	 * internal inrimage prt
	 */
	PTRINRIMAGE mImage;
Sylvain Thery's avatar
Sylvain Thery committed
134
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
135

Sylvain Thery's avatar
Sylvain Thery committed
136 137 138
	template< typename T>
	void readVTKBuffer(std::ifstream& in);

Pierre Kraemer's avatar
Pierre Kraemer committed
139 140 141 142 143 144 145 146 147 148 149 150 151
public:

	/**
	* default constructor
	*/
	Image();

	/**
	* destructor
	*/
	~Image();


152 153 154
	template< typename  DataTypeIn >
	void createMask(const Image<DataTypeIn>& img );

Pierre Kraemer's avatar
Pierre Kraemer committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	/**
	* Load a file (png) in an empty image
	* @param _cName file to open
	*/
	bool loadPNG3D(const char *_cName);


	/**
	* Load an ipb-format file in an empty image
	* @param _cName file to open
	*/
	bool loadIPB(const char* _cName);

	/**
	* Load an inr.gz format file in an empty image
	* @param filname file to open
	*/
	bool loadInrgz(const char* filename);

Sylvain Thery's avatar
Sylvain Thery committed
174 175 176 177 178 179 180
	/**
	 * @brief load VTK binary mask image
	 * @param filename
	 * @return
	 */
	bool loadVTKBinaryMask(const char* filename);

Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183 184 185 186 187 188 189 190 191
	/**
	* Constructor
	* @param data pointer on voxel
	* @param wx width in X
	* @param wy width in Y
	* @param wz width in Z
	* @param sx voxel size in X
	* @param sy voxel size in Y
	* @param sz voxel size in Z
	* @param copy	sets to true if data must be copying, false if data are used directly (and release by the destructor).
	*/
192
	Image(DataType *data, int wx, int wy, int wz, float sx, float sy, float sz, bool copy = false );
Pierre Kraemer's avatar
Pierre Kraemer committed
193 194 195 196 197


	/**
	* Load a raw image
	*/
Lionel Untereiner's avatar
Lionel Untereiner committed
198
	void loadRaw(const char *filename);
Pierre Kraemer's avatar
Pierre Kraemer committed
199 200 201 202

	/**
	 * Load a vox file
	 */
Lionel Untereiner's avatar
Lionel Untereiner committed
203
	void loadVox(const char *filename);
Pierre Kraemer's avatar
Pierre Kraemer committed
204 205 206 207 208 209 210 211 212 213

	/**
	* save current image into file
	* @param _cName file to save
	*/
    void saveInrMask(const char* _cName);

    /**
	* get the width along X axis
	*/
214
	int getWidthX() const { return m_WX;}
Pierre Kraemer's avatar
Pierre Kraemer committed
215 216 217 218

	/**
	* get the width along Y axis
	*/
219
	int getWidthY() const { return m_WY;}
Pierre Kraemer's avatar
Pierre Kraemer committed
220 221 222 223

	/**
	* get the width along Z axis
	*/
224
	int getWidthZ() const { return m_WZ;}
Pierre Kraemer's avatar
Pierre Kraemer committed
225 226 227 228

	/**
	* get widthX*widthY (size of a slice)
	*/
229
	int getWidthXY() const { return m_WXY;}
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231 232 233 234 235 236 237 238 239 240 241 242

	/** set the real size of voxel of image
	 * @param vx x size
	 * @param vy y size
	 * @param vz z size
	 */
	void setVoxelSize(float vx, float vy, float vz) { m_SX = vx; m_SY = vy; m_SZ = vz;}

	/** set origin (equivalent of frame size)
	 * @param ox x size
	 * @param oy y size
	 * @param oz z size
	 */
243
	void setOrigin(int ox, int oy, int oz) {m_OX = ox; m_OY = oy; m_OZ = oz;}
Pierre Kraemer's avatar
Pierre Kraemer committed
244 245 246 247 248

	/**
	 * get the origin
	 * @return a vector with origin
	 */
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
249
	Geom::Vec3i getOrigin() const { return Geom::Vec3i(m_OX, m_OY, m_OZ);}
Pierre Kraemer's avatar
Pierre Kraemer committed
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

	/**
	* get the data const version
	*/
	const DataType* getData() const {return m_Data;}

	/**
	* get the data non const version
	*/
	DataType* getData() {return m_Data;}

	/**
	* get the subsampling width in X of current slot
	*/
	float getVoxSizeX() const { return m_SX;}

	/**
	* get the subsampling width in Y of current slot
	*/
	float getVoxSizeY() const { return m_SY;}

	/**
	* get the subsampling width in Z of current slot
	*/
	float getVoxSizeZ() const { return m_SZ;}


	/**
	* get the voxel value
	*
	* @param  _lX,_lY,_lZ position
	* @return the value of the voxel
	*/
283
	DataType getVoxel(int _lX,int _lY, int _lZ);
Pierre Kraemer's avatar
Pierre Kraemer committed
284 285 286 287 288 289 290

	/**
	* get the voxel address (const ptr)
	*
	* @param _lX,_lY,_lZ position
	* @return the address of the voxel
	*/
291
	const DataType* getVoxelPtr(int _lX,int _lY, int _lZ) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
292 293 294 295 296 297 298

	/**
	* get the voxel address
	*
	* @param  _lX,_lY,_lZ position
	* @return the address of the voxel
	*/
299
	DataType* getVoxelPtr(int _lX,int _lY, int _lZ);
Pierre Kraemer's avatar
Pierre Kraemer committed
300 301 302 303 304 305 306 307


	/**
	* get the voxel value
	*
	* @param  _Vec vector of voxel position
	* @return the value of the voxel
	*/
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
308
	DataType getVoxel( const Geom::Vec3i &_Vec);
Pierre Kraemer's avatar
Pierre Kraemer committed
309 310 311 312 313 314 315


	/**
	*  Blur the image with pseudo gaussian filter
	* @param   _lWidth width of filtering
	* @return the new image
	*/
316
	Image<DataType>* filtering(int _lWidth);
Pierre Kraemer's avatar
Pierre Kraemer committed
317 318 319 320 321 322

	/**
	*  add Frame of zero around the image
	* @param  _lWidth the width of frame to add
	* @return the new image
	*/
323
	Image<DataType>* addFrame(int _lWidth) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
324 325 326 327 328

	/**
	 * Get the lower corner of bounding AABB
	 */
//	gmtl::Vec3f boundMin() const { return gmtl::Vec3f(0.0f, 0.0f, 0.0f);}
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
329
	Geom::Vec3f boundMin() const { return Geom::Vec3f(m_SX*m_OX, m_SY*m_OY, m_SZ*m_OZ);}
Pierre Kraemer's avatar
Pierre Kraemer committed
330 331 332 333 334

	/**
	 * Get the upper corner of bounding AABB
	 */
//	gmtl::Vec3f boundMax() const { return gmtl::Vec3f(m_SX*(m_WX-2*m_OX), m_SY*(m_WY-2*m_OY), m_SZ*(m_WZ-2*m_OZ));}
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
335
	Geom::Vec3f boundMax() const { return Geom::Vec3f(m_SX*(m_WX-m_OX), m_SY*(m_WY-m_OY), m_SZ*(m_WZ-m_OZ));}
Pierre Kraemer's avatar
Pierre Kraemer committed
336 337 338 339 340 341 342
	/**
	 * Compute the volume in cm3
	 * @param wind the windowing function
	 */
	template< typename Windowing >
	float computeVolume(const Windowing& wind) const;

Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
343 344 345 346
	/**
	 * local (3x3) blur of image
	 */
	Image<DataType>* Blur3();
Pierre Kraemer's avatar
Pierre Kraemer committed
347

348 349 350 351 352
	/**
	 * create a virtual sphere for computing curvature
	 * @param table the vector a ptr offset of voxels of sphere
	 * @param _i32radius radius of sphere
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
353 354
	void createMaskOffsetSphere(std::vector<int>& table, int _i32radius);

355 356 357 358 359 360 361 362 363
	/**
	 * compute the curvature with method that count voxels in a virtual sphere
	 * @param x position in x
	 * @param y position in y
	 * @param z position in z
	 * @param sphere the precomputed sphere
	 * @param outsideVal value of outside object
	 */
	float computeCurvatureCount(float x, float y, float z, const std::vector<int>& sphere, DataType outsideVal);
Pierre Kraemer's avatar
Pierre Kraemer committed
364

Sylvain Thery's avatar
Sylvain Thery committed
365 366 367 368
	float computeCurvatureCount3(const DataType *ptrVox, const std::vector<int>& cylX, const std::vector<int>& cylY, const std::vector<int>& cyl2, DataType val);

	void createMaskOffsetCylinders(std::vector<int>& tableX, std::vector<int>& tableY, std::vector<int>& tableZ, int _i32radius);

369
	void addCross();
Sylvain Thery's avatar
Sylvain Thery committed
370 371 372 373 374 375

	void createNormalSphere(std::vector<Geom::Vec3f>& table, int _i32radius);

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

	bool checkSaddlecomputeNormal(const Geom::Vec3f& P, const Geom::Vec3f& normal, unsigned int radius);
Pierre Kraemer's avatar
Pierre Kraemer committed
376 377 378 379 380 381
};


} // end namespace
} // end namespace
} // end namespace
382
}
Pierre Kraemer's avatar
Pierre Kraemer committed
383

Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
384
#include "Algo/MC/image.hpp"
Pierre Kraemer's avatar
Pierre Kraemer committed
385 386 387 388



#endif