Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

exportPov.h 10.4 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
#ifndef EXPORT_POV_H
2
#define EXPORT_POV_H
Pierre Kraemer's avatar
Pierre Kraemer committed
3
4

#include "Topology/generic/attributeHandler.h"
5
#include "Utils/cgognStream.h"
6
#include "Algo/Geometry/normal.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
7
8
9
10
11
12
13
14
15
16
17
18
19

namespace CGoGN
{

namespace Algo
{

namespace ExportPov
{

template <typename PFP>
void exportTriangleWire(std::ofstream& out,typename PFP::VEC3& p1,typename PFP::VEC3& p2,typename PFP::VEC3& p3)
{
Thomas's avatar
Thomas committed
20
		out << "cylinder { <" << p1[0] << "," << p1[2] << "," << p1[1] << ">, <" << p2[0] << "," << p2[2] << "," << p2[1] << ">, 0.5 }" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
21

Thomas's avatar
Thomas committed
22
		out << "cylinder { <" << p1[0] << "," << p1[2] << "," << p1[1] << ">, <" << p3[0] << "," << p3[2] << "," << p3[1] << ">, 0.5 }" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
23

Thomas's avatar
Thomas committed
24
		out << "cylinder { <" << p3[0] << "," << p3[2] << "," << p3[1] << ">, <" << p2[0] << "," << p2[2] << "," << p2[1] << ">, 0.5 }" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
25
26
27
28
29
}

template <typename PFP>
void exportTrianglePlain(std::ofstream& out,typename PFP::VEC3& p1,typename PFP::VEC3& p2,typename PFP::VEC3& p3)
{
Thomas's avatar
Thomas committed
30
31
32
33
34
		out << "triangle {" << std::endl;
		out << "<" << p1[0] << "," << p1[2] << "," << p1[1] << ">," << std::endl;
		out << "<" << p2[0] << "," << p2[2] << "," << p2[1] << ">, " << std::endl;
		out << "<" << p3[0] << "," << p3[2] << "," << p3[1] << "> " << std::endl;
		out << "}" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
35
36
37
}

template <typename PFP>
38
void exportMeshPlain(std::ofstream& out, typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position, const std::string& meshName, const FunctorSelect& good = allDarts)
Pierre Kraemer's avatar
Pierre Kraemer committed
39
{
Thomas's avatar
Thomas committed
40
	out << "#declare " << meshName << "= union {" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
41
42
43

	DartMarkerStore traite(map);

44
45
46
47
	for(Dart d = map.begin() ; d!= map.end() ; map.next(d))
	{
		if(good(d) && !traite.isMarked(d))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
48
49
			unsigned int nb = 0;
			Dart dd = d;
50
51
			do
			{
52
				traite.mark(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
53
54
				dd = map.phi1(dd);
				nb++;
Pierre Kraemer's avatar
Pierre Kraemer committed
55
			} while(dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
56

57
			if(nb == 3)
Pierre Kraemer's avatar
Pierre Kraemer committed
58
				Algo::ExportPov::exportTrianglePlain<PFP>(out,position[dd],position[map.phi1(dd)],position[map.phi1(map.phi1(dd))]);
59
60
			else
			{
Thomas's avatar
Thomas committed
61
					out << "polygon{ " << nb+1 << std::endl;
62
63
64
					dd = d;
					do
					{
Thomas's avatar
Thomas committed
65
						out << "<" << position[dd][0] << "," << position[dd][2] << "," << position[dd][1] << ">," << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
66
						dd = map.phi1(dd);
67
					} while(dd!=d);
Thomas's avatar
Thomas committed
68
69
					out << "<" << position[d][0] << "," << position[d][2] << "," << position[d][1] << ">" << std::endl;
					out << "}" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
70
71
72
73
			}
		}
	}

Thomas's avatar
Thomas committed
74
	out << "}" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
75
76
}

77
template <typename PFP>
78
void export3MeshPlainSmooth(std::ofstream& out, typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position, const std::string& meshName, const FunctorSelect& good = allDarts)
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
{
	typedef typename PFP::VEC3 VEC3;

	out << "#declare " << meshName << "= mesh2 {" << std::endl;

	unsigned int nbDarts = map.getNbDarts() ;

	//vector containing the degree of faces
	std::vector<unsigned int> facesSize ;
	//vector containing the list of index of vertices
	std::vector<std::vector<unsigned int> > facesIdx ;
	facesSize.reserve(nbDarts/3) ;
	facesIdx.reserve(nbDarts/3) ;

	//map : attribute place / index in declaration (for vertices and normals)
	std::map<unsigned int, unsigned int> vIndex ;
	//index : start from 0 and increase (used to ignore the gaps potentially present in the container)
	unsigned int vCpt = 0 ;

	//remember the attribute lines
	std::vector<unsigned int> vertices ;

	std::vector<VEC3> normals ;
	vertices.reserve(nbDarts/6) ;
	normals.reserve(nbDarts/6) ;

105
	CellMarker<VERTEX> markV(map) ;
106
107
108
109
110
111
	DartMarker markF(map) ;
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{

		if(good(d) && !markF.isMarked(d) && map.phi3(d)==d)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
112
			markF.markOrbit<FACE>(d) ;
113
114
115
116
117
			std::vector<unsigned int> fidx ;
			fidx.reserve(4) ;
			Dart dd = d ;
			do
			{
118
				unsigned int vNum = map.getEmbedding<VERTEX>(dd) ;
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
				if(!markV.isMarked(dd))
				{
					markV.mark(dd) ;
					VEC3 norm = Algo::Geometry::vertexBorderNormal<PFP>(map,dd,position);

					vIndex[vNum] = vCpt++ ;
					vertices.push_back(vNum) ;

					normals.push_back(norm) ;
				}

				fidx.push_back(vIndex[vNum]) ;

				dd = map.phi1(dd) ;
			} while(dd != d) ;

			facesSize.push_back(map.faceDegree(d)) ;
			facesIdx.push_back(fidx) ;
		}
	}

	//export all vertices
	out << "vertex_vectors {" << std::endl;
	out << vertices.size() << "," << std::endl;
	for(unsigned int i = 0; i < vertices.size(); ++i)
	{
		const VEC3& v = position[vertices[i]] ;
		out << "<" << v[0] << ", " << v[1] << ", " << v[2] << ">"<< std::endl ;
	}
	out << "}" << std::endl;

	//export all normals
	out << "normal_vectors {" << std::endl;
	out << normals.size() << "," << std::endl;
	for(unsigned int i = 0; i < normals.size(); ++i)
	{
		const VEC3& v = normals[i];
		out << "<" << v[0] << ", " << v[1] << ", " << v[2] << ">"<< std::endl ;
	}
	out << "}" << std::endl;

	//export all faces
	out << "face_indices {" << std::endl;
	out << facesSize.size() << "," << std::endl;
	for(unsigned int i = 0; i < facesSize.size(); ++i)
	{
		out << "<" << facesIdx[i][0];
		for(unsigned int j = 1; j < facesIdx[i].size(); ++j)
			out << ", " << facesIdx[i][j] ;
		out << ">" << std::endl ;
	}
	out << "}" << std::endl;

	out << "}" << std::endl;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
175
template <typename PFP>
176
void exportMeshWire(std::ofstream& out, typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position, const std::string& meshName, const FunctorSelect& good = allDarts)
Pierre Kraemer's avatar
Pierre Kraemer committed
177
{
Thomas's avatar
Thomas committed
178
	out << "#declare " << meshName << "= union {" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
179
180
181

	DartMarkerStore traite(map);

182
183
184
185
186
187
188
189
	for(Dart d = map.begin() ; d!= map.end() ; map.next(d))
	{
		if(good(d) && !traite.isMarked(d))
		{
			unsigned int nb = 0;
			Dart dd = d;
			do
			{
190
				traite.mark(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
191
192
				dd = map.phi1(dd);
				nb++;
193
			} while(dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
194

195
			if(nb == 3)
Pierre Kraemer's avatar
Pierre Kraemer committed
196
				Algo::ExportPov::exportTriangleWire<PFP>(out,position[dd],position[map.phi1(dd)],position[map.phi1(map.phi1(dd))]);
197
198
199
200
201
202
203
			else
			{
					dd = d;
					do
					{
						if(position[dd][0]!=position[map.phi1(dd)][0] || position[dd][1]!=position[map.phi1(dd)][1] || position[dd][2]!=position[map.phi1(dd)][2])
						{
Thomas's avatar
Thomas committed
204
205
206
207
							out << "cylinder{ " << std::endl;
							out << "<" << position[dd][0] << "," << position[dd][2] << "," << position[dd][1] << ">," << std::endl;
							out << "<" << position[map.phi1(dd)][0] << "," << position[map.phi1(dd)][2] << "," << position[map.phi1(dd)][1] << ">, 0.5" << std::endl;
							out << "}" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
208
209
						}
						dd = map.phi1(dd);
210
					} while(dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
211
212
213
214
			}
		}
	}

Thomas's avatar
Thomas committed
215
	out << "}" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
216
217
218
}

template <typename PFP>
219
bool exportScenePov(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position, const std::string& filename, typename PFP::VEC3 cameraPos, typename PFP::VEC3 cameraLook, typename PFP::VEC3 translate, float angle_X, float angle_Y, float angle_Z,const FunctorSelect& good = allDarts)
Pierre Kraemer's avatar
Pierre Kraemer committed
220
221
{
	std::ofstream out(filename.c_str(), std::ios::out);
222
223
	if (!out.good())
	{
224
		CGoGNerr << "(export) Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
225
226
227
228
229
230
231
232
		return false;
	}

	float angleX = angle_X;
	float angleY = angle_Y;
	float angleZ = angle_Z;

	//define the camera position
Thomas's avatar
Thomas committed
233
	out << "camera { location <" << cameraPos[0] << "," << cameraPos[1] << "," << cameraPos[2] << "> look_at <" << cameraLook[0] << "," << cameraLook[1] << "," << cameraLook[2] <<">}" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
234
	//set a "infinite" plane 
Thomas's avatar
Thomas committed
235
// 	out << "plane { y, -1 pigment { color rgb 1 } }" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
236
	//set a sky sphere
Thomas's avatar
Thomas committed
237
238
	out << "sphere { <0, 0, 0>, 5000";
	out << "texture{ pigment { color rgb <1, 1, 1>}	finish { ambient 1 diffuse 0 } } }" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
239
	//put some lights
Thomas's avatar
Thomas committed
240
241
	out << "light_source { <" << cameraPos[0] << "," << cameraPos[1] << "," << cameraPos[2] << "> color rgb 0.45}" << std::endl;
// 	out << "light_source { <-120, -300, -10> color rgb 0.25 }"<< std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
242
	//set a high quality rendering
Thomas's avatar
Thomas committed
243
244
245
	out << "global_settings {" << std::endl;
	out << "radiosity {" << std::endl;
	out << "pretrace_start 0.08 pretrace_end 0.04" << std::endl;
246
247
	out << "count 100 nearest_count 10 error_bound 0.15 recursion_limit 1 low_error_factor 0.2 gray_threshold 0.0 minimum_reuse 0.015 brightness 1 adc_bailout 0.01/2 normal off media off}" << std::endl;
	out << "max_trace_level 255}" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
248
249
250

	Algo::ExportPov::exportMeshPlain<PFP>(out,map,position,"myMesh",good);

Thomas's avatar
Thomas committed
251
252
253
254
	out << "object {myMesh" << std::endl;
 	out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] << ">" << std::endl;
 	out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> " << std::endl;
 	out << "texture{ pigment{ color rgb<1.0,1.0,1>} finish { ambient rgb 0.05 brilliance 0.5 } } }" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
255
256
257
258
259

	out.close();
	return true;
}

260
template <typename PFP>
261
bool exportScenePovSmooth(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position, const std::string& filename, typename PFP::VEC3 cameraPos, typename PFP::VEC3 cameraLook, typename PFP::VEC3 translate, float angle_X, float angle_Y, float angle_Z,const FunctorSelect& good = allDarts)
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
{
	std::ofstream out(filename.c_str(), std::ios::out);
	if (!out.good()) {
		CGoGNerr << "(export) Unable to open file " << filename << CGoGNendl;
		return false;
	}

	float angleX = angle_X;
	float angleY = angle_Y;
	float angleZ = angle_Z;

	//define the camera position
	out << "camera { location <" << cameraPos[0] << "," << cameraPos[1] << "," << cameraPos[2] << "> look_at <" << cameraLook[0] << "," << cameraLook[1] << "," << cameraLook[2] <<">}" << std::endl;

	//set a "infinite" plane
// 	out << "plane { y, -1 pigment { color rgb 1 } }" << std::endl;

	//set a sky sphere
	out << "sphere { <0, 0, 0>, 5000";
	out << "texture{ pigment { color rgb <1, 1, 1>}	finish { ambient 1 diffuse 0 } } }" << std::endl;

	//put some lights
	out << "light_source { <" << cameraPos[0] << "," << cameraPos[1] << "," << cameraPos[2] << "> color rgb 0.45}" << std::endl;

	//set a high quality rendering
	out << "global_settings {" << std::endl;
//	out << "radiosity {" << std::endl;
//	out << "pretrace_start 0.08 pretrace_end 0.04" << std::endl;
//	out << "count 300 nearest_count 10 error_bound 0.15 recursion_limit 1 low_error_factor 0.2 gray_threshold 0.0 minimum_reuse 0.015 brightness 1 adc_bailout 0.01/2 normal off media off}" << std::endl;
	out << "max_trace_level 60}" << std::endl;

	Algo::ExportPov::export3MeshPlainSmooth<PFP>(out,map,position,"myMesh",good);

	out << "object {myMesh" << std::endl;
 	out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] << ">" << std::endl;
 	out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> " << std::endl;
 	out << "double_illuminate" << std::endl;
 	out << "texture{ pigment{ color rgb <0.5,1.0,0.5>} finish { ambient 0.5 roughness 0.2 } } }" << std::endl;

	out.close();
	return true;
}

305
} // namespace ExportPov
Pierre Kraemer's avatar
Pierre Kraemer committed
306

307
} // namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
308

309
} // namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
310

untereiner's avatar
untereiner committed
311
#endif