surface_radiance.h 10.2 KB
Newer Older
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
#ifndef _SURFACE_RADIANCE_PLUGIN_H_
#define _SURFACE_RADIANCE_PLUGIN_H_

#include "plugin_interaction.h"
#include "surface_radiance_dockTab.h"

#include "Utils/sphericalHarmonics.h"
#include "Utils/Shaders/shaderRadiancePerVertex.h"

#include "Algo/Import/import.h"
#include "Algo/Import/import2tables.h"

namespace CGoGN
{

namespace SCHNApps
{

typedef SphericalHarmonics<PFP2::REAL, PFP2::VEC3> SH_TYPE;

struct MapParameters
{
	MapParameters() :
		positionVBO(NULL),
		normalVBO(NULL),
		radianceTexture(NULL),
		paramVBO(NULL)
	{}

	Utils::VBO* positionVBO;
	Utils::VBO* normalVBO;

	CGoGN::Utils::ShaderRadiancePerVertex* radiancePerVertexShader;

	VertexAttribute<SH_TYPE, PFP2::MAP> radiance;
	Utils::Texture<2, Geom::Vec3f>* radianceTexture;
	VertexAttribute<Geom::Vec2i, PFP2::MAP> param;
	Utils::VBO* paramVBO;
};

class Surface_Radiance_Plugin : public PluginInteraction
{
	Q_OBJECT
	Q_INTERFACES(CGoGN::SCHNApps::Plugin)

	friend class Surface_Radiance_DockTab;

public:
	Surface_Radiance_Plugin()
	{}

	~Surface_Radiance_Plugin()
	{}

	virtual bool enable();
	virtual void disable();

	virtual void draw(View *view) {}
	virtual void drawMap(View* view, MapHandlerGen* map);

	virtual void keyPress(View* view, QKeyEvent* event) {}
	virtual void keyRelease(View* view, QKeyEvent* event) {}
	virtual void mousePress(View* view, QMouseEvent* event) {}
	virtual void mouseRelease(View* view, QMouseEvent* event) {}
	virtual void mouseMove(View* view, QMouseEvent* event) {}
	virtual void wheelEvent(View* view, QWheelEvent* event) {}

	virtual void viewLinked(View *view) {}
	virtual void viewUnlinked(View *view) {}

private slots:
	// slots called from SCHNApps signals
	void selectedViewChanged(View* prev, View* cur);
	void selectedMapChanged(MapHandlerGen* prev, MapHandlerGen* cur);
	void mapAdded(MapHandlerGen* map);
	void mapRemoved(MapHandlerGen* map);

	// slots called from MapHandler signals
	void vboAdded(Utils::VBO* vbo);
	void vboRemoved(Utils::VBO* vbo);
	void attributeModified(unsigned int orbit, QString nameAttr);

	void importFromFileDialog();

public slots:
	// slots for Python calls
	void changePositionVBO(const QString& map, const QString& vbo);
	void changeNormalVBO(const QString& map, const QString& vbo);
	MapHandlerGen* importFromFile(const QString& fileName);

protected:
	Surface_Radiance_DockTab* m_dockTab;
	QHash<MapHandlerGen*, MapParameters> h_mapParameterSet;

	QAction* m_importAction;
};


class MeshTablesSurface_Radiance : public CGoGN::Algo::Surface::Import::MeshTablesSurface<PFP2>
{
public:
	MeshTablesSurface_Radiance(MAP& m) : MeshTablesSurface<PFP2>(m)
	{}

	/**
	 * @brief importPLY
	 * @param filename: the ply file to be imported. This file is supposed to have a three position, three normal and at least three radiance scalars per vertex.
	 * @return succeeded
	 */
	template <typename SH_TYPE>
	bool importPLY(const std::string& filename)
	{
		// Open file
		std::ifstream fp(filename, std::ios::in | std::ios::binary) ;
		if (!fp.good())
		{
			CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
			return false ;
		}

		// Read quantities : #vertices, #faces, #properties, degree of polynomials
		std::string tag ;

		fp >> tag;
		if (tag != std::string("ply")) // verify file type
		{
			CGoGNerr << filename << " is not a ply file !" <<  CGoGNout ;
			return false ;
		}

		do // go to "format"
		{
			fp >> tag ;
		} while (tag != std::string("format")) ;

		fp >> tag ;
		bool binary = (tag != "ascii") ;

		do // go to #vertices
		{
			fp >> tag ;
		} while (tag != std::string("vertex")) ;
		unsigned int nbVertices ;
		fp >> nbVertices ; // Read #vertices

		bool position = false ;
		bool normal = false ;
		bool radiance = false ;

		unsigned int propSize = 0 ; // for binary read
		unsigned int nbProps = 0 ;		// # properties
		unsigned int nbCoefs = 0 ; 	// # coefficients
		do // go to #faces and count #properties
		{
			fp >> tag ;
			if (tag == std::string("property"))
			{
				++nbProps ;
			}
			else if (tag == std::string("int8") || tag == std::string("uint8"))
			{
				if (propSize < 2)
				{
					propSize = 1 ;
					std::cerr << "only float32 of float64 is yet handled" << std::endl ;
					assert(!"only float32 or float64 is yet handled") ;
				}
				else
				{
					std::cerr << "only float32 of float64 is yet handled" << std::endl ;
					assert(!"only float32 or float64 is yet handled") ;
				}
			}
			else if (tag == std::string("int16") || tag == std::string("uint16"))
			{
				if (propSize == 0 || propSize == 2)
				{
					propSize = 2 ;
					std::cerr << "only float32 of float64 is yet handled" << std::endl ;
					assert(!"only float32 or float64 is yet handled") ;
				}
				else
				{
					std::cerr << "only float32 of float64 is yet handled" << std::endl ;
					assert(!"only float32 or float64 is yet handled") ;
				}
			}
			else if (tag == std::string("int32") || tag == std::string("float32") || tag == std::string("uint32"))
			{
				if (propSize == 0 || propSize == 4)
				{
					propSize = 4 ;
				}
				else
				{
					std::cerr << "only float32 of float64 is yet handled" << std::endl ;
					assert(!"only float32 or float64 is yet handled") ;
				}
			}
			else if (tag == std::string("int64") || tag == std::string("float64"))
			{
				if (propSize == 0 || propSize == 8)
				{
					propSize = 8 ;
				}
				else
				{
					std::cerr << "only float32 of float64 is yet handled" << std::endl ;
					assert(!"only float32 or float64 is yet handled") ;
				}
			}
			else if (tag == std::string("x") || tag == std::string("y") || tag == std::string("z"))
				position = true ;
			else if (tag == std::string("nx") || tag == std::string("ny") || tag == std::string("nz"))
				normal = true ;
			else if (tag.substr(0,6) == std::string("SHcoef"))
			{
				radiance = true ;
				++nbCoefs ;
			}
		} while (tag != std::string("face")) ;

		assert((nbCoefs % 3) == 0 || !"Import only supports RGB SphericalHarmonics (i.e. Tcoef==VEC3)") ;
		nbCoefs /= 3 ;
		SH_TYPE::set_nb_coefs(nbCoefs) ;

		fp >> this->m_nbFaces ; // Read #vertices

		do // go to end of header
		{
			fp >> tag ;
		} while (tag != std::string("end_header")) ;

		if (binary)
		{
			char* endline = new char[1] ;
			fp.read(endline, sizeof(char)) ;
			if (*endline == '\r') // for windows
				fp.read(endline, sizeof(char)) ;
			assert(*endline == '\n') ;
			delete[] endline ;
		}

		// Define containers
		assert((position && normal && radiance) || !"Import: position, normal and radiance attributs should be provided") ;
		if (!(position && normal && radiance))
			return false ;

		VertexAttribute<VEC3, MAP> positions =  this->m_map.template checkAttribute<VEC3, VERTEX, MAP>("position") ;
		VertexAttribute<VEC3, MAP> normals =  this->m_map.template checkAttribute<VEC3, VERTEX, MAP>("normal") ;
		VertexAttribute<SH_TYPE, MAP> radiances = this->m_map.template checkAttribute<SH_TYPE, VERTEX, MAP>("radiance") ;

		// Read vertices
		std::vector<unsigned int> verticesID ;
		if (propSize == 4)
		{
			this->m_nbVertices = readVerticesPLY<float, SH_TYPE>(fp, binary, nbVertices, nbProps, propSize, positions, normals, radiances, verticesID) ;
		}
		else if (propSize == 8)
		{
			this->m_nbVertices = readVerticesPLY<double, SH_TYPE>(fp, binary, nbVertices, nbProps, propSize, positions, normals, radiances, verticesID) ;
		}

		// Read faces index
		this->m_nbEdges.reserve(this->m_nbFaces) ;
		this->m_emb.reserve(3 * this->m_nbFaces) ;
		for (unsigned int i = 0 ; i < this->m_nbFaces ; ++i)
		{
			// read the indices of vertices for current face
			unsigned int nbEdgesForFace ;
			if (binary)
			{
				unsigned char tmp ;
				fp.read((char*)&(tmp), sizeof(unsigned char)) ;
				nbEdgesForFace = tmp ;
			}
			else
				fp >> nbEdgesForFace ;
			this->m_nbEdges.push_back(nbEdgesForFace);

			unsigned int vertexID ;
			if (binary)
			{
				for (unsigned int j=0 ; j < nbEdgesForFace ; ++j)
				{
					fp.read((char*)&vertexID, sizeof(unsigned int)) ;
					this->m_emb.push_back(verticesID[vertexID]);
				}
			}
			else
			{
				for (unsigned int j=0 ; j < nbEdgesForFace ; ++j)
				{
					fp >> vertexID ;
					this->m_emb.push_back(verticesID[vertexID]);
				}
			}
		}

		// Close file
		fp.close() ;

		return true ;
	}

private:
	/**
	 * \brief reads the list of vertices in a radiance PLY file. The function is template of the type of arithmetic (float, double) used in the file to be read
	 * \param fp IN: the file pointer descriptor
	 * \param binary IN: binary mode for reading fp
	 * \param nbVertices IN: the number of vertices to read
	 * \param nbProps IN: the number of properties according to the header
	 * \param positions IN/OUT: the positions container
	 * \param normals IN/OUT: the normals container
	 * \param radiances IN/OUT: the radiances container
	 * \param verticesID IN/OUT: the vector of vertex IDs
	 * \return number of vertices readVerticesPLY
	 */
	template <typename REAL, typename SH_TYPE>
	unsigned int readVerticesPLY(
		std::ifstream& fp,	bool binary,
		const unsigned int& nbVertices, const unsigned int& nbProps, const unsigned int& propSize,
		VertexAttribute<PFP2::VEC3, PFP2::MAP>& positions,
		VertexAttribute<PFP2::VEC3, PFP2::MAP>& normals,
		VertexAttribute<SH_TYPE, PFP2::MAP>& radiances,
		std::vector<unsigned int>& verticesID
	)
	{
		verticesID.reserve(nbVertices) ;

		REAL* properties = new REAL[nbProps] ;
		AttributeContainer& container = this->m_map.template getAttributeContainer<VERTEX>() ;
		for (unsigned int i = 0 ; i < nbVertices ; ++i) // Read and store properties for current vertex
		{
			unsigned int id = container.insertLine() ;
			verticesID.push_back(id) ;

			if (binary)
			{
				fp.read((char*)properties,nbProps * propSize) ;
			}
			else
			{
				for (unsigned int j = 0 ; j < nbProps ; ++j) // get all properties
					fp >> properties[j] ;
			}

			positions[id] = VEC3(properties[0],properties[1],properties[2]) ; // position
			normals[id] = VEC3(properties[3],properties[4],properties[5]) ; // normal
			int coefno = 6 ;
			for (int l = 0 ; l <= SH_TYPE::get_resolution() ; ++l) // radiance
				for (int m = -l ; m <= l ; ++m)
				{
					radiances[id].get_coef(l,m) = VEC3(properties[coefno],properties[coefno+1],properties[coefno+2]) ;
					coefno += 3 ;
				}
		}
		this->m_nbVertices = verticesID.size() ;
		delete[] properties ;

		return verticesID.size() ;
	}
};

} // namespace SCHNApps

} // namespace CGoGN

#endif