geolib2
shapes.cpp
Go to the documentation of this file.
1 #include "geolib/shapes.h"
2 
3 #include "geolib/Mesh.h"
4 #include "geolib/Shape.h"
5 
6 #include <cmath>
7 
8 namespace geo
9 {
10 
11 // ----------------------------------------------------------------------------------------------------
12 
13 void createCylinder(geo::Shape& shape, double radius, double height, unsigned int num_corners)
14 {
15  geo::Mesh mesh;
16 
17  // Calculate vertices
18  for(unsigned int i = 0; i < num_corners; ++i)
19  {
20  double a = 2 * M_PI * i / num_corners;
21  double x = sin(a) * radius;
22  double y = cos(a) * radius;
23 
24  mesh.addPoint(x, y, -height / 2);
25  mesh.addPoint(x, y, height / 2);
26  }
27 
28  // Calculate top and bottom triangles
29  for(unsigned int i = 1; i < num_corners - 1; ++i)
30  {
31  unsigned int i2 = 2 * i;
32 
33  // bottom
34  mesh.addTriangle(0, i2, i2 + 2);
35 
36  // top
37  mesh.addTriangle(1, i2 + 3, i2 + 1);
38  }
39 
40  // Calculate side triangles
41  for(unsigned int i = 0; i < num_corners; ++i)
42  {
43  unsigned int j = (i + 1) % num_corners;
44  mesh.addTriangle(i * 2, i * 2 + 1, j * 2);
45  mesh.addTriangle(i * 2 + 1, j * 2 + 1, j * 2);
46  }
47 
48  shape.setMesh(mesh);
49 }
50 
51 // ----------------------------------------------------------------------------------------------------
52 
53 void createConvexPolygon(geo::Shape& shape, const std::vector<geo::Vec2>& points, double height)
54 {
55  double min_z = -height / 2;
56  double max_z = height / 2;
57 
58  geo::Mesh mesh;
59 
60  // Add vertices
61  for(unsigned int i = 0; i < points.size(); ++i)
62  {
63  mesh.addPoint(geo::Vector3(points[i].x, points[i].y, min_z));
64  mesh.addPoint(geo::Vector3(points[i].x, points[i].y, max_z));
65  }
66 
67  // Calculate top and bottom triangles
68  for(unsigned int i = 1; i < points.size() - 1; ++i)
69  {
70  int i2 = 2 * i;
71 
72  // bottom
73  mesh.addTriangle(0, i2, i2 + 2);
74 
75  // top
76  mesh.addTriangle(1, i2 + 3, i2 + 1);
77  }
78 
79  // Calculate side triangles
80  for(unsigned int i = 0; i < points.size(); ++i)
81  {
82  int j = (i + 1) % points.size();
83  mesh.addTriangle(i * 2, i * 2 + 1, j * 2);
84  mesh.addTriangle(i * 2 + 1, j * 2 + 1, j * 2);
85  }
86 
87  shape.setMesh(mesh);
88 }
89 
90 // ----------------------------------------------------------------------------------------------------
91 
92 } // end namespace geo
93 
Shape.h
geo::Shape::setMesh
virtual void setMesh(const Mesh &mesh)
set the Mesh Any child classes should throw a std::logic_error in case the mesh should not be changed...
Definition: Shape.cpp:429
geo
Definition: Box.h:6
a
void a()
std::vector
std::vector::size
T size(T... args)
cmath
geo::Mesh::addPoint
unsigned int addPoint(double x, double y, double z)
Definition: Mesh.cpp:15
geo::Vector3
Definition: matrix.h:12
geo::createConvexPolygon
void createConvexPolygon(geo::Shape &shape, const std::vector< geo::Vec2 > &points, double height)
Definition: shapes.cpp:53
Mesh.h
shapes.h
geo::Mesh::addTriangle
void addTriangle(unsigned int i1, unsigned int i2, unsigned int i3)
Definition: Mesh.cpp:26
geo::createCylinder
void createCylinder(geo::Shape &shape, double radius, double height, unsigned int num_corners=20)
Definition: shapes.cpp:13
geo::Mesh
Definition: Mesh.h:25
geo::Shape
A geometric description of a shape.
Definition: Shape.h:19