-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeshFactory.cpp
More file actions
147 lines (139 loc) · 5.82 KB
/
meshFactory.cpp
File metadata and controls
147 lines (139 loc) · 5.82 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
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
#include "meshFactory.h"
#include "plane.h"
#include "point2.h"
#include "vertex.h"
libMesh::Mesh *libMesh::MeshFactory::box(Point3 center, Size2 size, Vector3 xAxis, Vector3 yAxis, float length)
{
std::vector<Vertex> vertices;
std::vector<Index3> indices;
auto centerPlane = Plane(center, xAxis, yAxis);
auto zAxis = centerPlane.getNormal();
auto extruded = zAxis.scaled(length / 2);
// First the front face.
auto frontPlane = Plane(center.added(extruded), xAxis, yAxis);
makeFaceFromRectangle(vertices, indices, frontPlane, size);
// Secondly, the back face.
auto backPlane = Plane(center.subtracted(extruded), xAxis.scaled(-1), yAxis);
makeFaceFromRectangle(vertices, indices, backPlane, size);
// Top face
extruded = yAxis.scaled(size.height / -2);
auto topPlane = Plane(center.added(extruded), xAxis, zAxis);
makeFaceFromRectangle(vertices, indices, topPlane, size);
// Bottom face
auto bottomPlane = Plane(center.subtracted(extruded), xAxis.scaled(-1), zAxis);
makeFaceFromRectangle(vertices, indices, bottomPlane, size);
// Right face
extruded = xAxis.scaled(size.width / -2);
auto rightPlane = Plane(center.added(extruded), zAxis, yAxis);
makeFaceFromRectangle(vertices, indices, rightPlane, size);
// Left face
auto leftPlane = Plane(center.subtracted(extruded), zAxis.scaled(-1), yAxis);
makeFaceFromRectangle(vertices, indices, leftPlane, size);
return new Mesh(vertices, indices);
}
libMesh::Mesh *libMesh::MeshFactory::cylinder(Point3 center, float diameter, Vector3 xAxis, Vector3 yAxis, float length, unsigned int numFaces)
{
std::vector<Vertex> vertices;
std::vector<Index3> indices;
auto plane = Plane(center, xAxis, yAxis);
auto radius = diameter / 2;
auto angle = M_PI / (numFaces * 2);
Point3 points [numFaces * 2];
// Front face
for(int i = 0; i < numFaces; i++)
{
points[i] = plane.get(Point2(sin(angle * i) * radius, cos(angle * i) * radius));
}
makeFaceAroundCenter(vertices, indices, center, points, numFaces);
// Back face
auto extruded = plane.getNormal().scaled(length);
for(int i = 0; i < numFaces; i++)
{
points[numFaces + i] = points[i].added(extruded);
}
makeFaceAroundCenter(vertices, indices, center, points + numFaces, numFaces);
makeFaceFromParallelCurves(vertices, indices, points, points + numFaces, numFaces);
return new Mesh(vertices, indices);
}
void libMesh::MeshFactory::makeFaceFromRectangle(std::vector<Vertex> &vertices, std::vector<Index3> &indices, Plane &plane, Size2 size)
{
Point3 points [4];
auto startIndex = vertices.size();
auto normal = plane.getNormal();
Point2 corner = { size.width * 0.5f, size.height * 0.5f };
auto position = plane.get(corner);
// 3 == 0
// | |
// 2 == 1
Vertex vertex = { position, normal, Point2(1, 1) };
vertices.push_back(vertex);
//std::cout << "0: " << corner << std::endl;
corner.y = -corner.y;
position = plane.get(corner);
vertex = { position, normal, Point2(1, 0) };
vertices.push_back(vertex);
//std::cout << "1: " << corner << std::endl;
corner.x = -corner.x;
position = plane.get(corner);
vertex = { position, normal, Point2(0, 0) };
vertices.push_back(vertex);
//std::cout << "2: " << corner << std::endl;
corner.y = -corner.y;
position = plane.get(corner);
vertex = { position, normal, Point2(0, 1) };
vertices.push_back(vertex);
//std::cout << "3: " << corner << std::endl;
// Create 2 triangles between the four corners.
indices.push_back(Index3(startIndex, startIndex + 3, startIndex + 2));
//std::cout << "Triangle: 0, 3, 2" << std::endl;
indices.push_back(Index3(startIndex + 2, startIndex + 1, startIndex));
//std::cout << "Triangle: 2, 1, 0" << std::endl;
}
void libMesh::MeshFactory::makeFaceFromParallelCurves(std::vector<Vertex> &vertices, std::vector<Index3> &indices, Point3 *left, Point3 *right, size_t size)
{
unsigned int startIndex = vertices.size();
for (int i = 0; i < size; i++)
{
Vertex leftVertex = { left[i], Vector3(), Point2() };
vertices.push_back(leftVertex);
Vertex rightVertex = { right[i], Vector3(), Point2() };
vertices.push_back(rightVertex);
}
unsigned int previousLeft = startIndex;
unsigned int previousRight = startIndex + 1;
for (int i = 1; i < size; i++)
{
const unsigned int currentLeft = startIndex + i;
const unsigned int currentRight = startIndex + i + 1;
// Fill the rectangle between current and previous with 2 triangles.
indices.push_back(Index3(previousLeft, currentLeft, previousRight));
indices.push_back(Index3(currentLeft, currentRight, previousRight));
// Preparte for next iteration
previousLeft = currentLeft;
previousRight = currentRight;
}
}
void libMesh::MeshFactory::makeFaceAroundCenter(std::vector<Vertex> &vertices, std::vector<Index3> &indices, Point3 center, Point3 *edge, size_t size)
{
unsigned int centerIndex = vertices.size();
auto normal = Point3::betweenPoints(center, edge[size >> 2]).cross(Point3::betweenPoints(center, edge[0]));
Vertex centerVertex = { center, normal, Point2() };
vertices.push_back(centerVertex);
for (int i = 0; i < size; i++)
{
Vertex edgeVertex = { edge[i], normal, Point2() };
vertices.push_back(edgeVertex);
}
unsigned int previousIndex = centerIndex + 1;
for (int i = 1; i < size; i++)
{
const unsigned int currentIndex = previousIndex + 1;
// Put triangle between current and previous edge points.
indices.push_back(Index3(centerIndex, currentIndex, previousIndex));
// Preparte for next iteration
previousIndex = currentIndex;
}
}