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