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