Mesh.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ElementOps.h"
4 
5 #include <RobotAPI/interface/ArViz/Elements.h>
6 
7 #include <Eigen/Core>
8 
9 #include <functional>
10 #include <numeric> // for std::accumulate
11 #include <vector>
12 
13 
14 namespace CGAL
15 {
16  template < class PolyhedronTraits_3,
17  class PolyhedronItems_3,
18  template < class T, class I, class A>
19  class T_HDS,
20  class Alloc>
21  class Polyhedron_3;
22  template<class> class Surface_mesh;
23 }
24 
25 namespace armarx::viz
26 {
27 
28  class Mesh : public ElementOps<Mesh, data::ElementMesh>
29  {
30  public:
32 
33  Mesh& vertices(const Eigen::Vector3f* vs, std::size_t size)
34  {
35  auto& vertices = data_->vertices;
36  vertices.clear();
37  vertices.reserve(size);
38 
39  for (std::size_t i = 0; i < size; ++i)
40  {
41  vertices.push_back(armarx::Vector3f{vs[i].x(), vs[i].y(), vs[i].z()});
42  }
43 
44  return *this;
45  }
46  Mesh& vertices(const std::vector<Eigen::Vector3f>& vs)
47  {
48  return this->vertices(vs.data(), vs.size());
49  }
50 
51  Mesh& vertices(const armarx::Vector3f* vs, std::size_t size)
52  {
53  data_->vertices.assign(vs, vs + size);
54 
55  return *this;
56  }
57  Mesh& vertices(const std::vector<armarx::Vector3f>& vs)
58  {
59  return this->vertices(vs.data(), vs.size());
60  }
61 
62  Mesh& colors(const data::Color* cs, std::size_t size)
63  {
64  data_->colors.assign(cs, cs + size);
65 
66  return *this;
67  }
68  Mesh& colors(const std::vector<data::Color>& cs)
69  {
70  return this->colors(cs.data(), cs.size());
71  }
72 
73  Mesh& faces(const data::Face* fs, std::size_t size)
74  {
75  data_->faces.assign(fs, fs + size);
76 
77  return *this;
78  }
79  Mesh& faces(const std::vector<data::Face>& fs)
80  {
81  return this->faces(fs.data(), fs.size());
82  }
83 
84  template<class T>
85  Mesh& mesh(const CGAL::Surface_mesh<T>& sm);
86 
87  template < class PolyhedronTraits_3,
88  class PolyhedronItems_3,
89  template < class T, class I, class A>
90  class T_HDS,
91  class Alloc>
93  PolyhedronTraits_3,
94  PolyhedronItems_3,
95  T_HDS,
96  Alloc
97  > & p3);
98  /**
99  * @brief Builds a regular 2D grid in the xy-plane.
100  * @param extents The full extents in x and y direction.
101  * @param numPoints The number of points in x and y direction.
102  * @param colorFunc A function determining the color of each vertex.
103  */
104  Mesh& grid2D(Eigen::Vector2f extents, Eigen::Vector2i numPoints,
105  std::function<viz::Color(size_t i, size_t j, const Eigen::Vector3f& p)> colorFunc);
106 
107  /**
108  * @brief Builds a regular 2D grid.
109  *
110  * The shape of `vertices` and `colors` must match, i.e.:
111  * - `vertices` and `colors` must have equal size.
112  * - Each element (nested vector) of `vertices` and `colors` must have equal size.
113  *
114  * @param vertices The vertices.
115  * @param colors The colors.
116  */
117  Mesh& grid2D(const std::vector<std::vector<Eigen::Vector3f>>& vertices,
118  const std::vector<std::vector<viz::data::Color>>& colors);
119  };
120 
121 
122 
123  namespace grid
124  {
125  /**
126  * @brief Builds vertices of a regular 2D grid in the xy-plane.
127  *
128  * If the result is indexed as result[i][j], the i index represents the x-axis,
129  * the j index represents the y-axis.
130  *
131  * @param extents The full extents per axis.
132  * @param numPoints The number of points per axis.
133  * @param height The height (z-value).
134  * @return The vertices.
135  */
136  std::vector<std::vector<Eigen::Vector3f>> makeGrid2DVertices(
137  Eigen::Vector2f extents, Eigen::Vector2i numPoints, float height = 0);
138 
139 
140  /**
141  * @brief Build colors of a 2D grid.
142  * @param vertices The vertices.
143  * @param colorFunc A function determining the color of each vertex.
144  * @return The colors.
145  *
146  * @see `makeGrid2DVertices()`
147  */
148  std::vector<std::vector<viz::data::Color>> makeGrid2DColors(
149  const std::vector<std::vector<Eigen::Vector3f>>& vertices,
150  std::function<viz::Color(size_t x, size_t y, const Eigen::Vector3f& p)> colorFunc);
151 
152 
153  /**
154  * @brief Builds faces of a 2D grid.
155  *
156  * The built indexes refer to flattened arrays of vertices and colors,
157  * such as produced by `flatten()` applied to the result of `makeGrid2DVertices()`.
158  *
159  * @param num_x The number of vertices in x-axis.
160  * @param num_y The number of vertices in y-axis.
161  * @return The faces.
162  */
163  std::vector<viz::data::Face> makeGrid2DFaces(size_t num_x, size_t num_y);
164 
165 
166  template <class T>
167  /// @brief Flattens a 2D vector of nested vectors to a 1D vector.
168  std::vector<T> flatten(const std::vector<std::vector<T>>& vector)
169  {
170  size_t size = std::accumulate(vector.begin(), vector.end(), size_t(0), [](size_t s, const auto & v)
171  {
172  return s + v.size();
173  });
174 
175  std::vector<T> flat;
176  flat.reserve(size);
177  for (const auto& v : vector)
178  {
179  for (const auto& val : v)
180  {
181  flat.push_back(val);
182  }
183  }
184  return flat;
185  }
186  }
187 
188 }
CGAL::Surface_mesh
Definition: Mesh.h:22
armarx::viz::Mesh::vertices
Mesh & vertices(const std::vector< Eigen::Vector3f > &vs)
Definition: Mesh.h:46
ElementOps.h
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
Definition: Mesh.h:28
armarx::viz::Mesh::vertices
Mesh & vertices(const armarx::Vector3f *vs, std::size_t size)
Definition: Mesh.h:51
CGAL
Definition: Mesh.h:14
armarx::viz::ElementOps< Mesh, data::ElementMesh >::data_
IceInternal::Handle< data::ElementMesh > data_
Definition: ElementOps.h:281
armarx::viz::ElementOps::ElementOps
ElementOps(std::string const &id)
Definition: ElementOps.h:111
armarx::viz::ElementOps
Definition: ElementOps.h:108
armarx::viz::Mesh::colors
Mesh & colors(const std::vector< data::Color > &cs)
Definition: Mesh.h:68
Color
uint32_t Color
RGBA color.
Definition: color.h:8
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
CGAL::Polyhedron_3
Definition: Mesh.h:21
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::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::viz::Mesh::vertices
Mesh & vertices(const std::vector< armarx::Vector3f > &vs)
Definition: Mesh.h:57
armarx::viz::Mesh::vertices
Mesh & vertices(const Eigen::Vector3f *vs, std::size_t size)
Definition: Mesh.h:33
armarx::viz::Mesh::mesh
Mesh & mesh(const CGAL::Surface_mesh< T > &sm)
Definition: MeshCGALExtensions.h:15
armarx::viz::Mesh::faces
Mesh & faces(const std::vector< data::Face > &fs)
Definition: Mesh.h:79
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::viz
This file is part of ArmarX.
Definition: ArVizStorage.cpp:416
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.