Unravel Engine C++ Reference
Loading...
Searching...
No Matches
svg_writer.hpp
Go to the documentation of this file.
1#ifndef GENERATOR_SVG_HPP
2#define GENERATOR_SVG_HPP
3
4#include <algorithm>
5#include <array>
6#include <map>
7#include <memory>
8#include <sstream>
9#include <vector>
10
11#include "math.hpp"
12
13#include "iterator.hpp"
14#include "mesh_vertex.hpp"
15#include "path_vertex.hpp"
16#include "shape_vertex.hpp"
17#include "triangle.hpp"
18
19namespace generator
20{
21
24{
25private:
26 class BaseElem
27 {
28 public:
29 double z_;
30
31 gml::dvec3 color_;
32
33 BaseElem(double z, const gml::dvec3& color);
34
35 virtual ~BaseElem();
36
37 // Writes this svg element to a stream.
38 virtual void stream(std::ostream&) const = 0;
39 };
40
41 class VertexElem : public BaseElem
42 {
43 public:
44 gml::dvec3 p_;
45
46 VertexElem(const gml::dvec3& p, const gml::dvec3& color);
47
48 virtual void stream(std::ostream& os) const override;
49 };
50
51 class LineElem : public BaseElem
52 {
53 public:
54 gml::dvec3 p1_, p2_;
55
56 LineElem(const gml::dvec3& p1, const gml::dvec3& p2, const gml::dvec3& color);
57
58 virtual void stream(std::ostream& os) const override;
59 };
60
61 class TriangleElem : public BaseElem
62 {
63 public:
64 std::array<gml::dvec3, 3> p_;
65
66 TriangleElem(const gml::dvec3& p1, const gml::dvec3& p2, const gml::dvec3& p3, const gml::dvec3& color);
67
68 virtual void stream(std::ostream& os) const override;
69 };
70
71 gml::dvec3 project(const gml::dvec3& p) const;
72
73 gml::dvec3 normalToColor(const gml::dvec3& normal) const;
74
75 gml::ivec2 size_;
76
77 gml::dmat4 viewMatrix_;
78 gml::dmat4 projMatrix_;
79 gml::dmat4 viewProjMatrix_;
80 gml::ivec2 viewportOrigin_;
81 gml::ivec2 viewportSize_;
82
83 gml::dvec3 lightDir_;
84
85 bool cullface_;
86
87 mutable std::vector<std::unique_ptr<BaseElem>> elems_;
88
89public:
92 svg_writer_t(int width, int height);
93
95 void modelView(const gml::dmat4& matrix);
96
101 void perspective(double fovy, double aspect, double zNear, double zFar);
102
107 void ortho(double left, double right, double bottom, double top);
108
110 void viewport(int x, int y, int width, int height);
111
113 void cullface(bool cullface);
114
116 void writePoint(const gml::dvec3& p, const gml::dvec3& color = {0.0, 0.0, 0.0});
117
119 void writeLine(const gml::dvec3& p1, const gml::dvec3& p2, const gml::dvec3& color = {0.0, 0.0, 0.0});
120
122 void writeTriangle(const gml::dvec3& p1, const gml::dvec3& p2, const gml::dvec3& p3, const gml::dvec3& color);
123
125 void writeTriangle(const gml::dvec3& p1, const gml::dvec3& p2, const gml::dvec3& p3);
126
128 template<typename shape_t>
129 void writeShape(const shape_t& shape, bool writeVertices = false, bool writeAxis = false)
130 {
131 std::vector<shape_vertex_t> vertices{};
132 for(const auto& vertex : shape.vertices())
133 {
134 vertices.push_back(vertex);
135 }
136
137 for(auto e : shape.edges())
138 {
139 auto p1 = gml::dvec3{vertices[e.vertices[0]].position, 0.0};
140 auto p2 = gml::dvec3{vertices[e.vertices[1]].position, 0.0};
141
142 writeLine(p1, p2, {0.5, 0.5, 0.5});
143 }
144
145 if(writeAxis)
146 {
147 for(auto v : vertices)
148 {
149 auto p1 = gml::dvec3{v.position, 0.0};
150 auto p2 = gml::dvec3{v.position + 0.1 * v.tangent, 0.0};
151 auto p3 = gml::dvec3{v.position + 0.1 * v.normal(), 0.0};
152
153 writeLine(p1, p2, {0.0, 1.0, 0.0});
154 writeLine(p1, p3, {1.0, 0.0, 0.0});
155 }
156 }
157
158 if(writeVertices)
159 {
160 for(auto v : shape.vertices())
161 {
162 writePoint(gml::dvec3{v.position, 0.0});
163 }
164 }
165 }
166
169 template<typename Path>
170 void writePath(const Path& path, bool writeVertices = false, bool writeAxis = false)
171 {
172 std::vector<path_vertex_t> vertices{};
173 for(const auto& temp : path.vertices())
174 {
175 vertices.push_back(temp);
176 }
177
178 if(writeAxis)
179 {
180 for(const auto& v : path.vertices())
181 {
182 writeLine(v.position, v.position + 0.1 * v.tangent, {0.0, 0.0, 1.0});
183 writeLine(v.position, v.position + 0.1 * v.normal, {1.0, 0.0, 0.0});
184 writeLine(v.position, v.position + 0.1 * v.binormal(), {0.0, 1.0, 0.0});
185 }
186 }
187
188 if(writeVertices)
189 {
190 for(const auto& v : path.vertices())
191 {
192 writePoint(v.position + 0.001 * v.normal);
193 }
194 }
195
196 for(const auto& e : path.edges())
197 {
198 writeLine(vertices[e.vertices[0]].position, vertices[e.vertices[1]].position);
199 }
200 }
201
203 template<typename Mesh>
204 void writeMesh(const Mesh& mesh, bool writeVertices = false, bool writeNormals = false)
205 {
206 std::vector<mesh_vertex_t> vertices{};
207 for(const mesh_vertex_t& vertex : mesh.vertices())
208 {
209 vertices.push_back(vertex);
210 }
211
212 for(triangle_t t : mesh.triangles())
213 {
214 writeTriangle(vertices[t.vertices[0]].position,
215 vertices[t.vertices[1]].position,
216 vertices[t.vertices[2]].position);
217 }
218
219 if(writeVertices)
220 {
221 for(const auto& v : vertices)
222 {
223 writePoint(v.position);
224 }
225 }
226
227 // Normals
228 if(writeNormals)
229 {
230 for(const auto& v : vertices)
231 {
232 writeLine(v.position, v.position + 0.1 * v.normal, {0.0, 0.0, 1.0});
233 }
234 }
235 }
236
238 std::string str() const;
239};
240} // namespace generator
241
242#endif
btVector3 normal
A simple svg writer class for generating preview and debug images.
void viewport(int x, int y, int width, int height)
Sets the viewport. Default fills the whole image.
void writeShape(const shape_t &shape, bool writeVertices=false, bool writeAxis=false)
Write all shaped edges and optionally vertices, tangents and normals.
void modelView(const gml::dmat4 &matrix)
Sets the model view matrix. Default is the identity matrix.
void writeTriangle(const gml::dvec3 &p1, const gml::dvec3 &p2, const gml::dvec3 &p3, const gml::dvec3 &color)
Write one triangle.
svg_writer_t(int width, int height)
void writeLine(const gml::dvec3 &p1, const gml::dvec3 &p2, const gml::dvec3 &color={0.0, 0.0, 0.0})
Write one line.
void writeMesh(const Mesh &mesh, bool writeVertices=false, bool writeNormals=false)
Write all triangles from a mesh.
void perspective(double fovy, double aspect, double zNear, double zFar)
void ortho(double left, double right, double bottom, double top)
std::string str() const
Generates svg xml from the data written so far.
void writePoint(const gml::dvec3 &p, const gml::dvec3 &color={0.0, 0.0, 0.0})
Write one point. Drawn as a circle.
void cullface(bool cullface)
Sets if backfacing triangles should be culled. Default is true.
void writePath(const Path &path, bool writeVertices=false, bool writeAxis=false)
float y
float x
float z