Synthetic.hpp
Go to the documentation of this file.
1 
2 #ifndef LVR2_UTIL_SYNTHETIC_HPP
3 #define LVR2_UTIL_SYNTHETIC_HPP
4 
6 #include "lvr2/io/MeshBuffer.hpp"
7 
8 namespace lvr2 {
9 
10 namespace synthetic {
11 
13  int num_long=50,
14  int num_lat=50)
15 {
16  MeshBufferPtr dst_mesh = MeshBufferPtr(new MeshBuffer);
17 
18  // x = cx + r * sin(alpha) * cos(beta)
19  // y = cy + r * sin(alpha) * sin(beta)
20  // z = cz + r * cos(alpha)
21 
22  // alpha [-pi, pi)
23  // beta [-pi, pi)
24 
25  std::vector<float> vertices;
26  std::vector<unsigned int> face_indices;
27 
28 
29  float long_inc = M_PI / static_cast<float>(num_long + 1);
30  float lat_inc = (2 * M_PI) / static_cast<float>(num_lat);
31 
32  // add first and last point
33 
34 
35  // add first vertex manually
36  vertices.push_back(sin(0.0) * cos(0.0));
37  vertices.push_back(sin(0.0) * sin(0.0));
38  vertices.push_back(cos(0.0));
39 
40  // add first faces manually
41  for(int i=0; i<num_lat; i++)
42  {
43  int id_bl = 0;
44  int id_tl = i;
45  int id_tr = i + 1;
46 
47  if(i == num_lat - 1)
48  {
49  id_tr -= num_lat;
50  }
51 
52  face_indices.push_back(id_bl);
53  face_indices.push_back(id_tl + 1);
54  face_indices.push_back(id_tr + 1);
55  }
56 
57  for(int i=0; i<num_long; i++)
58  {
59  float alpha = long_inc * (i+1);
60 
61  for(int j=0; j<num_lat; j++)
62  {
63  float beta = lat_inc * (j+1);
64 
65  vertices.push_back(sin(alpha) * cos(beta));
66  vertices.push_back(sin(alpha) * sin(beta));
67  vertices.push_back(cos(alpha));
68 
69  if(i > 0)
70  {
71 
72  int id_bl = num_lat * (i-1) + j;
73  int id_br = num_lat * (i-1) + j + 1;
74  int id_tl = num_lat * (i) + j;
75  int id_tr = num_lat * (i) + j + 1;
76 
77  if(j == num_lat - 1)
78  {
79  id_br -= num_lat;
80  id_tr -= num_lat;
81  }
82 
83  // clockwise
84 
85  // first face
86  face_indices.push_back(id_br + 1);
87  face_indices.push_back(id_bl + 1);
88  face_indices.push_back(id_tl + 1);
89 
90  // second face
91  face_indices.push_back(id_tl + 1);
92  face_indices.push_back(id_tr + 1);
93  face_indices.push_back(id_br + 1);
94  }
95  }
96  }
97 
98  // add last vertex
99  vertices.push_back(sin(M_PI) * cos(2.0 * M_PI));
100  vertices.push_back(sin(M_PI) * sin(2.0 * M_PI));
101  vertices.push_back(cos(M_PI));
102 
103  int num_vertices = vertices.size() / 3;
104  for(int i=num_vertices-1-num_lat; i<num_vertices-1; i++)
105  {
106  int id_bl = i;
107  int id_br = i+1;
108  int id_tl = num_vertices-1;
109 
110  if(id_br == id_tl)
111  {
112  id_br -= num_lat;
113  }
114 
115  face_indices.push_back(id_br);
116  face_indices.push_back(id_bl);
117  face_indices.push_back(id_tl);
118  }
119 
120 
121  // COPY
122  floatArr vertex_arr(new float[vertices.size()]);
123  indexArray index_arr(new unsigned int[face_indices.size()]);
124 
125  for(int i=0; i<vertices.size(); i++)
126  {
127  vertex_arr[i] = vertices[i];
128  }
129 
130  for(int i=0; i<face_indices.size(); i++)
131  {
132  index_arr[i] = face_indices[i];
133  }
134 
135 
136  dst_mesh->setVertices(vertex_arr, vertices.size() / 3);
137  dst_mesh->setFaceIndices(index_arr, face_indices.size() / 3);
138 
139  return dst_mesh;
140 }
141 
142 } // namespace synthetic
143 
144 } // namespace lvr2
145 
146 #endif // LVR2_UTIL_SYNTHETIC_HPP
lvr2::floatArr
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
BaseVector.hpp
lvr2::indexArray
boost::shared_array< unsigned int > indexArray
Definition: DataStruct.hpp:128
M_PI
#define M_PI
Definition: Matrix4.hpp:52
lvr2::MeshBuffer
The MeshBuffer Mesh representation for I/O modules.
Definition: MeshBuffer.hpp:41
lvr2::synthetic::genSphere
MeshBufferPtr genSphere(int num_long=50, int num_lat=50)
Definition: Synthetic.hpp:12
MeshBuffer.hpp
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::MeshBufferPtr
std::shared_ptr< MeshBuffer > MeshBufferPtr
Definition: MeshBuffer.hpp:217


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:25