Mesh.cpp
Go to the documentation of this file.
1 #include "Mesh.h"
2 
4 
5 
6 namespace armarx::viz
7 {
8  Mesh& Mesh::grid2D(Eigen::Vector2f extents, Eigen::Vector2i numPoints,
9  std::function<viz::Color(size_t, size_t, const Eigen::Vector3f&)> colorFunc)
10  {
11  // Create vertices.
12  std::vector<std::vector<Eigen::Vector3f>> vertices = grid::makeGrid2DVertices(extents, numPoints);
13 
14  // Create colors.
15  std::vector<std::vector<viz::data::Color>> colors = grid::makeGrid2DColors(vertices, colorFunc);
16 
17  return this->grid2D(vertices, colors);
18  }
19 
20 
21  Mesh& Mesh::grid2D(const std::vector<std::vector<Eigen::Vector3f> >& vertices,
22  const std::vector<std::vector<viz::data::Color>>& colors)
23  {
24  ARMARX_CHECK_EQUAL(vertices.size(), colors.size()) << "Numbers of vertices and colors must match.";
25 
26  if (vertices.empty())
27  {
28  return *this;
29  }
30 
31  const size_t num_x = vertices.size();
32  const size_t num_y = vertices.front().size();
33 
34  bool check = false; // This could unnecessarily slow down building large meshes.
35  if (check)
36  {
37  // Check consistent sizes.
38  for (const auto& vv : vertices)
39  {
40  ARMARX_CHECK_EQUAL(vv.size(), num_y) << "All nested vectors must have equal length.";
41  }
42  for (const auto& cv : colors)
43  {
44  ARMARX_CHECK_EQUAL(cv.size(), num_y) << "All nested vectors must have equal length.";
45  }
46  }
47 
48  // Create faces.
49  const std::vector<viz::data::Face> faces = grid::makeGrid2DFaces(num_x, num_y);
50 
51  // Flatten
52  return this->vertices(grid::flatten(vertices)).colors(grid::flatten(colors)).faces(faces);
53  }
54 
55 
56  std::vector<std::vector<Eigen::Vector3f>> grid::makeGrid2DVertices(
57  Eigen::Vector2f extents, Eigen::Vector2i numPoints, float height)
58  {
59  const Eigen::Vector2f minimum = - extents / 2;
60 
61  // extents = (num - 1) * step => step = extents / (num - 1)
62  const Eigen::Vector2f step = (extents.array() / (numPoints.array() - 1).cast<float>()).matrix();
63 
64  ARMARX_CHECK_POSITIVE(numPoints.minCoeff()) << "Number of points must be positive. " << VAROUT(numPoints);
65  const size_t num_x = size_t(numPoints.x());
66  const size_t num_y = size_t(numPoints.y());
67 
68  // Create vertices.
69  std::vector<std::vector<Eigen::Vector3f>> gridVertices(
70  num_x, std::vector<Eigen::Vector3f>(num_y, Eigen::Vector3f::Zero()));
71 
72  for (size_t i = 0; i < num_x; i++)
73  {
74  for (size_t j = 0; j < num_y; j++)
75  {
76  gridVertices[i][j].x() = minimum.x() + i * step.x();
77  gridVertices[i][j].y() = minimum.y() + j * step.y();
78  gridVertices[i][j].z() = height;
79  }
80  }
81 
82  return gridVertices;
83  }
84 
85 
86  std::vector<std::vector<viz::data::Color> > grid::makeGrid2DColors(
87  const std::vector<std::vector<Eigen::Vector3f> >& vertices,
88  std::function<viz::Color(size_t x, size_t y, const Eigen::Vector3f& p)> colorFunc)
89  {
90  size_t num_x = vertices.size();
91  size_t num_y = vertices.front().size();
92 
93  std::vector<std::vector<viz::data::Color>> colors(
94  num_x, std::vector<viz::data::Color>(num_y, viz::Color::black()));
95 
96  for (size_t i = 0; i < num_x; i++)
97  {
98  for (size_t j = 0; j < num_y; j++)
99  {
100  colors[i][j] = colorFunc(i, j, vertices[i][j]);
101  }
102  }
103 
104  return colors;
105  }
106 
107 
108  std::vector<viz::data::Face> grid::makeGrid2DFaces(size_t num_x, size_t num_y)
109  {
110  std::vector<viz::data::Face> faces(2 * (num_x - 1) * (num_y - 1));
111 
112  size_t index = 0;
113  for (size_t x = 0; x < num_x - 1; x++)
114  {
115  for (size_t y = 0; y < num_y - 1; y++)
116  {
117  /* In counter-clockwise order.
118  * (x) (x+1)
119  * (y) *----*
120  * | \f1|
121  * |f2\ |
122  * (y+1) *----*
123  */
124  faces[index].v0 = faces[index].c0 = int(x * num_y + y);
125  faces[index].v1 = faces[index].c1 = int((x + 1) * num_y + (y + 1));
126  faces[index].v2 = faces[index].c2 = int((x + 1) * num_y + y);
127  index++;
128 
129  faces[index].v0 = faces[index].c0 = int(x * num_y + y);
130  faces[index].v1 = faces[index].c1 = int(x * num_y + (y + 1));
131  faces[index].v2 = faces[index].c2 = int((x + 1) * num_y + (y + 1));
132  index++;
133  }
134  }
135  return faces;
136  }
137 
138 
139 
140 
141 }
armarx::viz::Color::black
static Color black(int a=255)
Definition: Color.h:62
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::viz::grid::flatten
std::vector< T > flatten(const std::vector< std::vector< T >> &vector)
Flattens a 2D vector of nested vectors to a 1D vector.
Definition: Mesh.h:168
armarx::viz::Mesh::faces
Mesh & faces(const data::Face *fs, std::size_t size)
Definition: Mesh.h:73
armarx::viz::grid::makeGrid2DVertices
std::vector< std::vector< Eigen::Vector3f > > makeGrid2DVertices(Eigen::Vector2f extents, Eigen::Vector2i numPoints, float height=0)
Builds vertices of a regular 2D grid in the xy-plane.
Definition: Mesh.cpp:56
armarx::viz::Mesh::colors
Mesh & colors(const data::Color *cs, std::size_t size)
Definition: Mesh.h:62
armarx::viz::Color
Definition: Color.h:13
ARMARX_CHECK_POSITIVE
#define ARMARX_CHECK_POSITIVE(number)
This macro evaluates whether number is positive (> 0) and if it turns out to be false it will throw a...
Definition: ExpressionException.h:145
ExpressionException.h
Mesh.h
armarx::viz::grid::makeGrid2DFaces
std::vector< viz::data::Face > makeGrid2DFaces(size_t num_x, size_t num_y)
Builds faces of a 2D grid.
Definition: Mesh.cpp:108
armarx::viz::Mesh::vertices
Mesh & vertices(const Eigen::Vector3f *vs, std::size_t size)
Definition: Mesh.h:33
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
cv
Definition: helper.h:35
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::viz
This file is part of ArmarX.
Definition: ArVizStorage.cpp:370
armarx::viz::Mesh::grid2D
Mesh & grid2D(Eigen::Vector2f extents, Eigen::Vector2i numPoints, std::function< viz::Color(size_t i, size_t j, const Eigen::Vector3f &p)> colorFunc)
Builds a regular 2D grid in the xy-plane.
armarx::viz::grid::makeGrid2DColors
std::vector< std::vector< viz::data::Color > > makeGrid2DColors(const std::vector< std::vector< Eigen::Vector3f >> &vertices, std::function< viz::Color(size_t x, size_t y, const Eigen::Vector3f &p)> colorFunc)
Build colors of a 2D grid.