Debian Codegraph @

avogadro/rendering/plyvisitor.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "plyvisitor.h"
7
8 namespace Avogadro::Rendering {
9 using std::ofstream;
10 using std::ostream;
11 using std::ostringstream;
12 using std::string;
13 using std::vector;
14
15 namespace {
16 ostream& operator<<(ostream& os, const Vector3f& v)
17 {
18 os << v[0] << " " << v[1] << " " << v[2];
19 return os;
20 }
21
22 ostream& operator<<(ostream& os, const Vector3ub& color)
23 {
24 // PLY expects same number of parameters every time, so if no alpha given use
25 // 1
26 os << color[0] / 255.0f << " " << color[1] / 255.0f << " "
27 << color[2] / 255.0f << " " << 1;
28 return os;
29 }
30
31 ostream& operator<<(ostream& os, const Vector4ub& color)
32 {
33 os << color[0] / 255.0f << " " << color[1] / 255.0f << " "
34 << color[2] / 255.0f << " " << color[3] / 255.0f;
35 return os;
36 }
37 } // namespace
38
39 PLYVisitor::PLYVisitor(const Camera& c)
40 : m_camera(c), m_backgroundColor(255, 255, 255),
41 m_ambientColor(100, 100, 100), m_aspectRatio(800.0f / 600.0f)
42 {
43 }
44
45 PLYVisitor::~PLYVisitor() {}
46
47 void PLYVisitor::begin() {}
48
49 string PLYVisitor::end()
50 {
51 // Adds the PLY header after the final counts are known
52 ostringstream header;
53
54 // Header format
55 header << "ply" << '\n'
56 << "format ascii 1.0" << '\n'
57
58 << "element vertex " << m_vertexCount << '\n'
59 << "property float x" << '\n'
60 << "property float y" << '\n'
61 << "property float z" << '\n'
62 << "property float red" << '\n'
63 << "property float green" << '\n'
64 << "property float blue" << '\n'
65 << "property float alpha" << '\n'
66
67 << "element face " << m_faceCount << '\n'
68 << "property list uchar uint vertex_index" << '\n'
69 << "end_header" << '\n';
70
71 // Add the vertices and faces
72 return header.str() + m_sceneVertices + m_sceneFaces;
73 }
74
75 void PLYVisitor::visit(Drawable&) {}
76
77 void PLYVisitor::visit(SphereGeometry& geometry)
78 {
79 for (const auto& s : geometry.spheres()) {
80 // Uses an Icosphere method (logic and new functions could be added here to
81 // pick different methods)
82 visitSphereIcosphereRecursionMethod(s, 5);
83 }
84 }
85
86 void PLYVisitor::visitSphereIcosphereRecursionMethod(const SphereColor& sphere,
87 unsigned int subdivisions)
88 {
89 Vector3f center = sphere.center;
90 float radius = sphere.radius;
91
92 // Defines an Icosahedron Vertices and Faces
93 float phi = (1.0f + sqrt(5.0f)) / 2.0f;
94 float a = 1.0f;
95 float b = 1.0f / phi;
96
97 vector<Vector3f> vertices = { Vector3f(0, b, -a), Vector3f(b, a, 0),
98 Vector3f(-b, a, 0), Vector3f(0, b, a),
99 Vector3f(0, -b, a), Vector3f(-a, 0, b),
100 Vector3f(0, -b, -a), Vector3f(a, 0, -b),
101 Vector3f(a, 0, b), Vector3f(-a, 0, -b),
102 Vector3f(b, -a, 0), Vector3f(-b, -a, 0) };
103
104 // Local Indexes for the faces
105 vector<vector<unsigned int>> faces = {
106 vector<unsigned int>{ 2, 1, 0 }, vector<unsigned int>{ 1, 2, 3 },
107 vector<unsigned int>{ 5, 4, 3 }, vector<unsigned int>{ 4, 8, 3 },
108 vector<unsigned int>{ 7, 6, 0 }, vector<unsigned int>{ 6, 9, 0 },
109 vector<unsigned int>{ 11, 10, 4 }, vector<unsigned int>{ 10, 11, 6 },
110 vector<unsigned int>{ 9, 5, 2 }, vector<unsigned int>{ 5, 9, 11 },
111 vector<unsigned int>{ 8, 7, 1 }, vector<unsigned int>{ 7, 8, 10 },
112 vector<unsigned int>{ 2, 5, 3 }, vector<unsigned int>{ 8, 1, 3 },
113 vector<unsigned int>{ 9, 2, 0 }, vector<unsigned int>{ 1, 7, 0 },
114 vector<unsigned int>{ 11, 9, 6 }, vector<unsigned int>{ 7, 10, 6 },
115 vector<unsigned int>{ 5, 11, 4 }, vector<unsigned int>{ 10, 8, 4 }
116 };
117
118 // For every subdivision
119 for (unsigned int i = 0; i < subdivisions; ++i) {
120 // Prerecord face size so doesn't change mid loop
121 int facesSize = faces.size();
122
123 // For every face
124 for (int j = 0; j < facesSize; ++j) {
125 // Face vertices
126 Vector3f faceVertexOne(vertices.at(faces.at(j).at(0)));
127 Vector3f faceVertexTwo(vertices.at(faces.at(j).at(1)));
128 Vector3f faceVertexThree(vertices.at(faces.at(j).at(2)));
129 unsigned int faceIndexOne = faces.at(j).at(0);
130 unsigned int faceIndexTwo = faces.at(j).at(1);
131 unsigned int faceIndexThree = faces.at(j).at(2);
132
133 // Get the vertex at the midpoint between One and Two and its Index
134 Vector3f vertexOneTwo((faceVertexOne[0] + faceVertexTwo[0]) / 2.0f,
135 (faceVertexOne[1] + faceVertexTwo[1]) / 2.0f,
136 (faceVertexOne[2] + faceVertexTwo[2]) / 2.0f);
137 unsigned int indexOneTwo = vertices.size();
138 vertices.push_back(vertexOneTwo);
139
140 // Get the vertex at the midpoint between Two and Three and its Index
141 Vector3f vertexTwoThree((faceVertexTwo[0] + faceVertexThree[0]) / 2.0f,
142 (faceVertexTwo[1] + faceVertexThree[1]) / 2.0f,
143 (faceVertexTwo[2] + faceVertexThree[2]) / 2.0f);
144 unsigned int indexTwoThree = vertices.size();
145 vertices.push_back(vertexTwoThree);
146
147 // Get the vertex at the midpoint between One and Three and its Index
148 Vector3f vertexOneThree((faceVertexOne[0] + faceVertexThree[0]) / 2.0f,
149 (faceVertexOne[1] + faceVertexThree[1]) / 2.0f,
150 (faceVertexOne[2] + faceVertexThree[2]) / 2.0f);
151 unsigned int indexOneThree = vertices.size();
152 vertices.push_back(vertexOneThree);
153
154 // Replace the original face with one new face and push the others to the
155 // back
156 vector<unsigned int> subdividedFaceOne = { faceIndexOne, indexOneTwo,
157 indexOneThree };
158 vector<unsigned int> subdividedFaceTwo = { faceIndexTwo, indexTwoThree,
159 indexOneTwo };
160 vector<unsigned int> subdividedFaceThree = { faceIndexThree,
161 indexOneThree,
162 indexTwoThree };
163 vector<unsigned int> subdividedFaceFour = { indexOneTwo, indexTwoThree,
164 indexOneThree };
165 faces.at(j) = subdividedFaceOne;
166 faces.push_back(subdividedFaceTwo);
167 faces.push_back(subdividedFaceThree);
168 faces.push_back(subdividedFaceFour);
169 }
170 }
171
172 ostringstream vertexStr;
173 ostringstream faceStr;
174
175 // Project every vertex onto the sphere and record it
176 for (unsigned int i = 0; i < vertices.size(); ++i) {
177 // Normalize the vertex and then project it
178 float x = vertices.at(i)[0];
179 float y = vertices.at(i)[1];
180 float z = vertices.at(i)[2];
181 float distance = std::hypot(x, y, z);
182 vertices.at(i)[0] = (x / distance) * radius;
183 vertices.at(i)[1] = (y / distance) * radius;
184 vertices.at(i)[2] = (z / distance) * radius;
185
186 // Adjust to be around sphere's radius
187 vertices.at(i)[0] += center[0];
188 vertices.at(i)[1] += center[1];
189 vertices.at(i)[2] += center[2];
190
191 // Add the vertex
192 vertexStr << vertices.at(i) << " " << sphere.color << '\n';
193 }
194
195 // Adjust every face to have indices for the PLY file and add it
196 for (unsigned int i = 0; i < faces.size(); ++i) {
197 faceStr << 3 << " " << faces.at(i)[0] + m_vertexCount << " "
198 << faces.at(i)[1] + m_vertexCount << " "
199 << faces.at(i)[2] + m_vertexCount << '\n';
200 }
201
202 // Adjust the counts and add the new vertices and faces
203 m_vertexCount += vertices.size();
204 m_faceCount += faces.size();
205 m_sceneVertices += vertexStr.str();
206 m_sceneFaces += faceStr.str();
207 }
208
209 void PLYVisitor::visit(AmbientOcclusionSphereGeometry&) {}
210
211 void PLYVisitor::visit(CylinderGeometry& geometry)
212 {
213 for (const auto& c : geometry.cylinders()) {
214 // Uses an Icosphere method (logic and new functions could be added here to
215 // pick different methods)
216 visitCylinderLateralMethod(c, 20);
217 }
218 }
219
220 void PLYVisitor::visitCylinderLateralMethod(const CylinderColor& geometry,
221 unsigned int lateralFaces)
222 {
223 ostringstream vertexStr;
224 ostringstream faceStr;
225
226 // Add each end of the cylinder to the vertices
227 Vector3f end1 = geometry.end1;
228 Vector3f end2 = geometry.end2;
229 vertexStr << end1 << " " << geometry.color << '\n';
230 vertexStr << end2 << " " << geometry.color << '\n';
231 m_vertexCount += 2;
232
233 // Radius and the length vector of cylinder
234 float radius = geometry.radius;
235 float length =
236 std::hypot(end1[0] - end2[0], end1[1] - end2[1], end1[2] - end2[2]);
237
238 // Normalize the plane vector of the cylinder
239 Vector3f normalVector((end1[0] - end2[0]) / length,
240 (end1[1] - end2[1]) / length,
241 (end1[2] - end2[2]) / length);
242
243 // Find a basis vector orthogonal to the plane vector
244 Vector3f u;
245
246 // If the Plane Vector doesn't point entirely in the y direction
247 if (normalVector[1] != 1.0f) {
248 // Choose a orthogonal vector for the y-axis (if plane vector points
249 // entirely in y, this will be 0-vector)
250 u[0] = normalVector[2];
251 u[1] = 0;
252 u[2] = -1 * normalVector[0];
253 }
254 // Otherwise, must use different orthogonal vector since y-axis one would be
255 // 0-vector
256 else {
257 u[0] = 0;
258 u[1] = normalVector[2];
259 u[2] = -1 * normalVector[1];
260 }
261
262 // Length of u vector
263 float uLength = std::hypot(u[0], u[1], u[2]);
264
265 // Normalize the vector
266 u[0] /= uLength;
267 u[1] /= uLength;
268 u[2] /= uLength;
269
270 // Find the other basis vector orthogonal to the plane vector via u x v =
271 // normalVector
272 Vector3f v;
273 v[0] = normalVector[1] * u[2] - normalVector[2] * u[1];
274 v[1] = normalVector[2] * u[0] - normalVector[0] * u[2];
275 v[2] = normalVector[0] * u[1] - normalVector[1] * u[0];
276
277 // Angle between each lateral face
278 float baseAngle = 2 * 3.14159265359 / lateralFaces;
279
280 // Find the vertices and faces for each lateral face
281 for (unsigned int i = 0; i < lateralFaces; ++i) {
282 // Current angle
283 float angle = baseAngle * i;
284
285 // Code works by assuming there are 2 * lateralFaces vertices in the
286 // cylinder (not including the ends). i corresponds to the lateral face
287 // number, to which there are two corresponding vertices (top and bottom).
288
289 // Place the top and bottom lateral face vertex into the file (p = center +
290 // R*(u*cos(a) + v*sin(a)))
291 vertexStr << Vector3f(end1[0] + radius * u[0] * cos(angle) +
292 radius * v[0] * sin(angle),
293 end1[1] + radius * u[1] * cos(angle) +
294 radius * v[1] * sin(angle),
295 end1[2] + radius * u[2] * cos(angle) +
296 radius * v[2] * sin(angle))
297 << " " << geometry.color << '\n';
298 vertexStr << Vector3f(end2[0] + radius * u[0] * cos(angle) +
299 radius * v[0] * sin(angle),
300 end2[1] + radius * u[1] * cos(angle) +
301 radius * v[1] * sin(angle),
302 end2[2] + radius * u[2] * cos(angle) +
303 radius * v[2] * sin(angle))
304 << " " << geometry.color << '\n';
305
306 // Code then needs to create the two lateral faces these vertices create
307 // with the next vertices 2 * i gives the index of the first vertex, plus 1
308 // for second vertex, plus 2 or 3 for next faces vertex Modulo included for
309 // +2 or +3 to prevent going out of bounds
310
311 // Lateral face made up of the next face's first vertex, first vertex, and
312 // second vertex
313 faceStr << 3 << " " << (2 * i + 2) % (2 * lateralFaces) + m_vertexCount
314 << " " << 2 * i + m_vertexCount << " " << 2 * i + 1 + m_vertexCount
315 << '\n';
316 // Lateral face made up of next face's first vertex, second vertex, and next
317 // face's second vertex
318 faceStr << 3 << " " << (2 * i + 2) % (2 * lateralFaces) + m_vertexCount
319 << " " << 2 * i + 1 + m_vertexCount << " "
320 << (2 * i + 3) % (2 * lateralFaces) + m_vertexCount << '\n';
321
322 // Code needs to deal with faces on top and bottom of cylinder
323 // To turn local indices to the indices of the PLY file, -2 is used for end1
324 // and -1 is used for end2
325
326 // Top face made up of end1, first vertex, and next face's first vertex
327 faceStr << 3 << " " << -2 + m_vertexCount << " " << 2 * i + m_vertexCount
328 << " " << (2 * i + 2) % (2 * lateralFaces) + m_vertexCount << '\n';
329 // Bottom face made up of next face's second vertex, second vertex, and end2
330 faceStr << 3 << " " << (2 * i + 3) % (2 * lateralFaces) + m_vertexCount
331 << " " << 2 * i + 1 + m_vertexCount << " " << -1 + m_vertexCount
332 << '\n';
333 }
334
335 // Adjust the counts and add the new vertices and faces
336 m_vertexCount += 2 * lateralFaces;
337 m_faceCount += 4 * lateralFaces;
338 m_sceneVertices += vertexStr.str();
339 m_sceneFaces += faceStr.str();
340 }
341
342 void PLYVisitor::visit(MeshGeometry& geometry)
343 {
344 Core::Array<Rendering::PackedVertex> v = geometry.vertices();
345 Core::Array<unsigned int> tris = geometry.triangles();
346 ostringstream vertexStr;
347 ostringstream faceStr;
348
349 // Record every vertex in the mesh
350 for (size_t i = 0; i < v.size(); ++i) {
351 vertexStr << v[i].vertex << " " << v[i].color << '\n';
352 }
353
354 // Record every face and adjust the indices
355 for (size_t i = 0; i < tris.size(); i += 3) {
356 faceStr << 3 << " " << tris[i] + m_vertexCount << " "
357 << tris[i + 1] + m_vertexCount << " " << tris[i + 2] + m_vertexCount
358 << '\n';
359 }
360
361 // Adjust the counts and add the vertices and faces
362 // I think the vertex order on the meshes are messed up for the Coordinate
363 // System Mesh resulting in mismatched normals, since my code shouldn't be
364 // doing anything to it.
365 m_vertexCount += v.size();
366 m_faceCount += tris.size() / 3;
367 m_sceneVertices += vertexStr.str();
368 m_sceneFaces += faceStr.str();
369 }
370
371 void PLYVisitor::visit(LineStripGeometry&) {}
372
373 } // namespace Avogadro::Rendering