Debian Codegraph @

avogadro/rendering/vrmlvisitor.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 "vrmlvisitor.h"
7
8 #include "ambientocclusionspheregeometry.h"
9 #include "cylindergeometry.h"
10 #include "linestripgeometry.h"
11 #include "meshgeometry.h"
12 #include "spheregeometry.h"
13
14 #include <iostream>
15 #include <ostream>
16
17 namespace Avogadro::Rendering {
18
19 using std::ofstream;
20 using std::ostream;
21 using std::ostringstream;
22 using std::string;
23
24 namespace {
25 ostream& operator<<(ostream& os, const Vector3f& v)
26 {
27 os << v[0] << " " << v[1] << " " << v[2];
28 return os;
29 }
30
31 ostream& operator<<(ostream& os, const Vector3ub& color)
32 {
33 os << color[0] / 255.0f << "\t" << color[1] / 255.0f << "\t"
34 << color[2] / 255.0f;
35 return os;
36 }
37
38 ostream& operator<<(ostream& os, const Vector4ub& color)
39 {
40 os << color[0] / 255.0f << " " << color[1] / 255.0f << " "
41 << color[2] / 255.0f << color[3] / 255.0f;
42 return os;
43 }
44 } // namespace
45
46 VRMLVisitor::VRMLVisitor(const Camera& c)
47 : m_camera(c), m_backgroundColor(255, 255, 255),
48 m_ambientColor(100, 100, 100), m_aspectRatio(800.0f / 600.0f)
49 {
50 }
51
52 VRMLVisitor::~VRMLVisitor() {}
53
54 void VRMLVisitor::begin()
55 {
56
57 // Initialise the VRML scene
58 Vector3f cameraT = -(m_camera.modelView().linear().adjoint() *
59 m_camera.modelView().translation());
60
61 // Output the POV-Ray initialisation code
62 // orientation should be set
63 // http://cgvr.informatik.uni-bremen.de/teaching/vr_literatur/Calculating%20VRML%20Viewpoints.html
64 ostringstream str;
65 str << "#VRML V2.0 utf8\n"
66 << "DEF DefaultView Viewpoint {\n"
67 << "position " << cameraT << " \n"
68 << "fieldOfView 0.785398\n}\n";
69
70 m_sceneData = str.str();
71 }
72
73 string VRMLVisitor::end()
74 {
75 return m_sceneData;
76 }
77
78 void VRMLVisitor::visit(Drawable&) {}
79
80 void VRMLVisitor::visit(SphereGeometry& geometry)
81 {
82 ostringstream str;
83 for (auto s : geometry.spheres()) {
84 str << "Transform {\n"
85 << "\ttranslation\t" << s.center[0] << "\t" << s.center[1] << "\t"
86 << s.center[2] << "\n\tchildren Shape {\n"
87 << "\t\tgeometry Sphere {\n\t\t\tradius\t" << s.radius << "\n\t\t}\n"
88 << "\t\tappearance Appearance {\n"
89 << "\t\t\tmaterial Material {\n"
90 << "\t\t\t\tdiffuseColor\t" << s.color << "\n\t\t\t}\n\t\t}\n\t}\n}\n";
91 }
92 m_sceneData += str.str();
93 }
94
95 void VRMLVisitor::visit(AmbientOcclusionSphereGeometry&) {}
96
97 void VRMLVisitor::visit(CylinderGeometry& geometry)
98 {
99 ostringstream str;
100 for (auto c : geometry.cylinders()) {
101 // double scale = 1.0;
102 double x1, x2, y1, y2, z1, z2;
103 x1 = c.end1[0];
104 x2 = c.end2[0];
105 y1 = c.end1[1];
106 y2 = c.end2[1];
107 z1 = c.end1[2];
108 z2 = c.end2[2];
109
110 double dx = x2 - x1;
111 double dy = y2 - y1;
112 double dz = z2 - z1;
113
114 double length = std::hypot(dx, dy, dz);
115 double tx = dx / 2 + x1;
116 double ty = dy / 2 + y1;
117 double tz = dz / 2 + z1;
118
119 dx = dx / length;
120 dy = dy / length;
121 dz = dz / length;
122
123 double ax, ay, az, angle;
124
125 if (dy > 0.999) {
126 ax = 1.0;
127 ay = 0.0;
128 az = 0.0;
129 angle = 0.0;
130 } else if (dy < -0.999) {
131 ax = 1.0;
132 ay = 0.0;
133 az = 0.0;
134 angle = 3.14159265359;
135 } else {
136 ax = dz;
137 ay = 0.0;
138 az = dx * -1.0;
139 angle = acos(dy);
140 }
141 length = length / 2.0;
142
143 str << "Transform {\n"
144 << "\ttranslation\t" << tx << "\t" << ty << "\t" << tz << "\n\tscale "
145 << " 1 " << length << " 1"
146 << "\n\trotation " << ax << " " << ay << " " << az << " " << angle
147 << "\n\tchildren Shape {\n"
148 << "\t\tgeometry Cylinder {\n\t\t\tradius\t" << c.radius << "\n\t\t}\n"
149 << "\t\tappearance Appearance {\n"
150 << "\t\t\tmaterial Material {\n"
151 << "\t\t\t\tdiffuseColor\t" << c.color << "\n\t\t\t}\n\t\t}\n\t}\n}\n";
152 }
153 m_sceneData += str.str();
154 }
155
156 void VRMLVisitor::visit(MeshGeometry& geometry)
157 {
158 Core::Array<Rendering::PackedVertex> v = geometry.vertices();
159
160 // If there are no triangles then don't bother doing anything
161 if (v.size() == 0)
162 return;
163
164 ostringstream str, verts, iverts, colors;
165
166 // save the points, coordinates, and colors to separate strings
167 for (unsigned int i = 0; i < v.size(); ++i) {
168 if (i == v.size() - 1) {
169 verts << v[i].vertex;
170 colors << v[i].color;
171 break;
172 }
173 verts << v[i].vertex << ",\n";
174 colors << v[i].color << ", ";
175 }
176 // Now to write out the indices
177 for (unsigned int i = 0; i < v.size(); i += 3) {
178 iverts << i << ", " << i + 1 << ", " << i + 2 << ", -1,\n";
179 }
180
181 // Now to write out the full mesh - could be pretty big...
182 str << "Shape {\n"
183 << "\tgeometry IndexedFaceSet {\n"
184 << "\t\tcoord Coordinate {\n"
185 << "\t\t\tpoint [" << verts.str() << "\t\t\t]\n\t\t}\n"
186 << "\t\tcoordIndex[" << iverts.str() << "\t\t\t]\n"
187 << "color Color {\n color [" << colors.str() << "]\n}\n}\n}";
188
189 m_sceneData += str.str();
190 }
191
192 void VRMLVisitor::visit(LineStripGeometry&) {}
193
194 } // namespace Avogadro::Rendering