-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3d_basic_figures.cpp
More file actions
297 lines (242 loc) · 11.6 KB
/
Copy path3d_basic_figures.cpp
File metadata and controls
297 lines (242 loc) · 11.6 KB
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
//
// Created by kobedb on 20.05.22.
//
#include "3d_basic_figures.h"
#include <iostream>
using namespace std;
namespace KDBRenderUtils
{
/// in: face point-indexes that start from 1 --> returns: indexes that start from 0
vector<Face> toFacesStartingWithIndex0(const vector<Face>& in)
{
vector<Face> result;
for(const auto& f : in ) {
vector<int> newIndexes;
for(auto index : f.point_indexes) {
newIndexes.push_back(index-1);
}
result.push_back(Face{newIndexes});
}
return result;
}
Figure createSphereRecursive(int n, const Figure& sphereToRefine)
{
if(n <= 0) return sphereToRefine;
vector<Vector3D> vertices;
vector<Face> faces;
for(const auto& f : sphereToRefine.faces) {
if(f.point_indexes.size() != 3) cerr << "intermediate sphere's face doesn't have exactly 3 points!\n";
const Vector3D& p0 = sphereToRefine.vertices[f.point_indexes[0]];
const Vector3D& p1 = sphereToRefine.vertices[f.point_indexes[1]];
const Vector3D& p2 = sphereToRefine.vertices[f.point_indexes[2]];
Vector3D p0p1 = midPoint(p0,p1);
Vector3D p1p2 = midPoint(p1,p2);
Vector3D p2p0 = midPoint(p2,p0);
vertices.push_back(p0); int p0_i = vertices.size()-1; // Store the index of each point in the vertices vector
vertices.push_back(p1); int p1_i = vertices.size()-1;
vertices.push_back(p2); int p2_i = vertices.size()-1;
vertices.push_back(p0p1); int p0p1_i = vertices.size()-1;
vertices.push_back(p1p2); int p1p2_i = vertices.size()-1;
vertices.push_back(p2p0); int p2p0_i = vertices.size()-1;
faces.push_back(Face{{p0_i, p0p1_i, p2p0_i}});
faces.push_back(Face{{p0p1_i, p1_i, p1p2_i}});
faces.push_back(Face{{p2p0_i, p1p2_i, p2_i}});
faces.push_back(Face{{p2p0_i, p0p1_i, p1p2_i}}); // Middle triangle also has its own face apparently
}
return createSphereRecursive(n-1, Figure{vertices, faces, magenta});
}
Figure createSphere(double radius, int n, const NormColor& color)
{
Figure icosahedron = createIcosahedron(color);
Figure sphere = createSphereRecursive(n, icosahedron);
for(auto& p : sphere.vertices) {
double r = p.length();
p = Vector3D::point(p.x/r, p.y/r, p.z/r);
}
sphere.color = color;
return sphere;
}
Figure createDodecahedron(const NormColor& color)
{
Figure icosahedron = createIcosahedron(color);
vector<Vector3D> dodecaVertices;
for(const auto& f : icosahedron.faces) {
if(f.point_indexes.size() != 3) cerr << "icosahedron's face doesn't have exactly 3 points!\n";
const Vector3D& p0 = icosahedron.vertices[f.point_indexes[0]];
const Vector3D& p1 = icosahedron.vertices[f.point_indexes[1]];
const Vector3D& p2 = icosahedron.vertices[f.point_indexes[2]];
double dodecaX = (p0.x + p1.x + p2.x) / 3;
double dodecaY = (p0.y + p1.y + p2.y) / 3;
double dodecaZ = (p0.z + p1.z + p2.z) / 3;
dodecaVertices.push_back(Vector3D::point(dodecaX, dodecaY, dodecaZ));
}
vector<Vector3D> vertices = dodecaVertices;
vector<Face> faces = toFacesStartingWithIndex0({
Face{{1,2,3,4,5}},
{{1,6,7,8,2}},
{{2,8,9,10,3}},
{{3,10,11,12,4}},
{{4,12,13,14,5}},
{{5,14,15,6,1}},
{{20,19,18,17,16}},
{{20,15,14,13,19}},
{{19,13,12,11,18}},
{{18,11,10,9,17}},
{{17,9,8,7,16}},
{{16,7,6,15,20}}
});
return {vertices, faces, color};
}
Figure createIcosahedron(const NormColor& color)
{
vector<Vector3D> vertices{12};
vertices[1 - 1] = Vector3D::point(0,0,sqrt(5)/2);
for(int i = 2; i <= 6; i++) {
double magic = (i-2)*(2*KDB_PI/5);
vertices[i-1] = Vector3D::point(cos(magic), sin(magic), 0.5);
}
for(int i = 7; i <= 11; i++) {
double magic = (KDB_PI/5)+(i-7)*(2*KDB_PI/5);
vertices[i-1] = Vector3D::point(cos(magic), sin(magic), -0.5);
}
vertices[12 - 1] = Vector3D::point(0,0,-sqrt(5)/2);
vector<Face> faces = toFacesStartingWithIndex0({Face {{1,2,3}},
{{1,3,4}},
{{1,4,5}},
{{1,5,6}},
{{1,6,2}},
{{2,7,3}},
{{3,7,8}},
{{3,8,4}},
{{4,8,9}},
{{4,9,5}},
{{5,9,10}},
{{5,10,6}},
{{6,10,11}},
{{6,11,2}},
{{2,11,7}},
{{12,8,7}},
{{12,9,8}},
{{12,10,9}},
{{12,11,10}},
{{12,7,11}}});
return Figure{vertices, faces, color};
}
Figure createOctahedron(const NormColor& color)
{
vector<Vector3D> vertices = { Vector3D::point(1,0,0),
Vector3D::point(0,1,0),
Vector3D::point(-1,0,0),
Vector3D::point(0,-1,0),
Vector3D::point(0,0,-1),
Vector3D::point(0,0,1)};
vector<Face> faces = toFacesStartingWithIndex0(
{Face{{1,2,6}},{{2,3,6}},{{3,4,6}}, {{4,1,6}}, {{2,1,5}}, {{3,2,5}}, {{4,3,5}}, {{1,4,5}}});
return {vertices, faces, color};
}
Figure createTetrahedron(const NormColor& color)
{
vector<Vector3D> vertices = {Vector3D::point(1,-1,-1),
Vector3D::point(-1,1,-1),
Vector3D::point(1,1,1),
Vector3D::point(-1,-1,1)};
vector<Face> faces = toFacesStartingWithIndex0({Face{{1,2,3}}, {{2,4,3}}, {{1,4,2}}, {{1,3,4}}});
return {vertices, faces, color};
}
Figure createCube(const NormColor& color)
{
vector<Vector3D> vertices = {
Vector3D::point(1, -1, -1),
Vector3D::point(-1, 1, -1),
Vector3D::point(1, 1, 1),
Vector3D::point(-1, -1, 1),
Vector3D::point(1, 1, -1),
Vector3D::point(-1, -1, -1),
Vector3D::point(1, -1, 1),
Vector3D::point(-1, 1,1)};
vector<Face> faces = toFacesStartingWithIndex0({Face{{1,5,3,7}}, {{5,2,8,3}}, {{2,6,4,8}}, {{6,1,7,4}}, {{7,3,8,4}}, {{1,6,2,5}}});
return {vertices, faces, color};
}
Figure createCone(int n, double h, const NormColor& color)
{
if(n < 3) cerr << "Trying to create a cone with less than 3 faces huh? You're a funny lad\n";
double r = 1;
vector<Vector3D> vertices;
for(double a = 0; a < 2*KDB_PI; a += 2*KDB_PI/n) {
Vector3D circlePoint = Vector3D::point(cos(a) * r, sin(a) * r, 0);
vertices.push_back(circlePoint);
}
Vector3D top = Vector3D::point(0,0,h);
vertices.push_back(top);
vector<Face> faces;
for(int i = 0; i < vertices.size(); i++) { // index 2 -> p1 on circle (second point)
faces.push_back(Face{{i,(i+1)%n, (int)vertices.size()-1}});
}
Face bottom;
for(int i = vertices.size() - 2; i >= 0; i--) {
bottom.point_indexes.push_back(i);
}
faces.push_back(bottom);
return {vertices, faces, color};
}
Figure createCylinder(int n, double h, const NormColor& c)
{
if(n < 3) cerr << "Trying to create a cylinder with less than 3 faces huh? You're a funny lad\n";
double r = 1;
vector<Vector3D> vertices;
// Bottom circle
for(double a = 0; a < 2*KDB_PI; a += 2*KDB_PI/n) {
Vector3D circlePoint = Vector3D::point(cos(a) * r, sin(a) * r, 0);
vertices.push_back(circlePoint);
}
// Top circle
for(double a = 0; a < 2*KDB_PI; a += 2*KDB_PI/n) {
Vector3D circlePoint = Vector3D::point(cos(a) * r, sin(a) * r, h);
vertices.push_back(circlePoint);
}
vector<Face> faces;
int indexFirstPointTopCircle = (int)vertices.size()/2;
for(int i = 1; i < indexFirstPointTopCircle; i++) {
faces.push_back(Face{{i, i + indexFirstPointTopCircle, i-1 + indexFirstPointTopCircle, i-1}});
}
//last face
faces.push_back(Face{{0, 0 + indexFirstPointTopCircle, (int)vertices.size()-1, indexFirstPointTopCircle-1 }});
Face bottomCircleFace;
for(int i = 0; i < indexFirstPointTopCircle; i++) {
bottomCircleFace.point_indexes.push_back(i);
}
Face topCircleFace;
for(int i = indexFirstPointTopCircle; i < vertices.size(); i++) {
topCircleFace.point_indexes.push_back(i);
}
faces.push_back(bottomCircleFace);
faces.push_back(topCircleFace);
return{vertices, faces, c};
}
Figure createTorus(double r, double R, int n, int m, const NormColor& color)
{
vector<Vector3D> vertices;
for(int u_i = 0; u_i < n; u_i++) {
double u = u_i * (2 * KDB_PI / n);
for(int v_i = 0; v_i < m; v_i++) {
double v = v_i * (2 * KDB_PI / m);
Vector3D p = Vector3D::point((R+r*cos(v))*cos(u), (R+r*cos(v))*sin(u), r*sin(v));
vertices.push_back(p);
}
}
// n == aantal cirkels waarin de buis wordt opgedeeld
// m == aantal punten op elke cirkel
vector<Face> faces;
for(int u_i = 0; u_i < n; u_i++) {
for(int v_i = 0; v_i < m; v_i++) {
faces.push_back(Face{{
u_i * m + v_i,
(u_i+1)%n * m + v_i,
(u_i+1)%n * m + (v_i+1)%m,
u_i * m + (v_i+1)%m,
}});
}
}
return {vertices, faces, color};
}
};