Fixed mesh generation to generate cylinders.

This commit is contained in:
Joseph Lenox 2016-11-27 22:34:10 -06:00 committed by bubnikv
parent b91b98b21e
commit f44df28a96

View file

@ -1163,6 +1163,7 @@ TriangleMesh make_cube(double x, double y, double z) {
// Generate the mesh for a cylinder and return it, using // Generate the mesh for a cylinder and return it, using
// the generated angle to calculate the top mesh triangles. // the generated angle to calculate the top mesh triangles.
// Default is 360 sides, angle fa is in radians.
TriangleMesh make_cylinder(double r, double h, double fa) { TriangleMesh make_cylinder(double r, double h, double fa) {
Pointf3s vertices; Pointf3s vertices;
std::vector<Point3> facets; std::vector<Point3> facets;
@ -1171,31 +1172,36 @@ TriangleMesh make_cylinder(double r, double h, double fa) {
vertices.push_back(Pointf3(0.0, 0.0, 0.0)); vertices.push_back(Pointf3(0.0, 0.0, 0.0));
vertices.push_back(Pointf3(0.0, 0.0, h)); vertices.push_back(Pointf3(0.0, 0.0, h));
// adjust via rounding // adjust via rounding to get an even multiple for any provided angle.
double angle = (2*PI / floor(2*PI / fa)); double angle = (2*PI / floor(2*PI / fa));
// for each line along the polygon approximating the top/bottom of the // for each line along the polygon approximating the top/bottom of the
// circle, generate four points and four facets (2 for the wall, 2 for the // circle, generate four points and four facets (2 for the wall, 2 for the
// top and bottom. // top and bottom.
// Special case: Last line shares 2 vertices with the first line. // Special case: Last line shares 2 vertices with the first line.
unsigned id = 3; unsigned id = vertices.size() - 1;
vertices.push_back(Pointf3(sin(0) * r , cos(0) * r, 0)); vertices.push_back(Pointf3(sin(0) * r , cos(0) * r, 0));
vertices.push_back(Pointf3(sin(0) * r , cos(0) * r, h)); vertices.push_back(Pointf3(sin(0) * r , cos(0) * r, h));
for (double i = angle; i < 2*PI; i+=angle) { for (double i = 0; i < 2*PI; i+=angle) {
vertices.push_back(Pointf3(sin(i) * r , cos(i) * r, 0)); Pointf3 b(0, r, 0);
vertices.push_back(Pointf3(sin(i) * r , cos(i) * r, h)); Pointf3 t(0, r, h);
id += 2; b.rotate(i, Pointf3(0,0,0));
facets.push_back(Point3(0, id - 1, id - 3)); t.rotate(i, Pointf3(0,0,h));
facets.push_back(Point3(1, id, id - 2)); vertices.push_back(b);
facets.push_back(Point3(id, id - 1, id - 3)); vertices.push_back(t);
facets.push_back(Point3(id - 3, id - 2, id)); id = vertices.size() - 1;
facets.push_back(Point3( 0, id - 1, id - 3)); // top
facets.push_back(Point3(id, 1, id - 2)); // bottom
facets.push_back(Point3(id, id - 2, id - 3)); // upper-right of side
facets.push_back(Point3(id, id - 3, id - 1)); // bottom-left of side
} }
facets.push_back(Point3(0, 2, id -1)); // Connect the last set of vertices with the first.
facets.push_back(Point3(1, 3, id)); facets.push_back(Point3( 2, 0, id - 1));
facets.push_back(Point3(id - 1, 2, 3)); facets.push_back(Point3( 1, 3, id));
facets.push_back(Point3(id - 1, 3, id)); facets.push_back(Point3(id, 3, 2));
facets.push_back(Point3(id, 2, id - 1));
TriangleMesh mesh(vertices ,facets); TriangleMesh mesh(vertices, facets);
return mesh; return mesh;
} }
} }