6 #include "../core/log.h" 7 #include "../render/mesh.h" 9 #include <geometric_shapes/shape_operations.h> 11 ShapeDisplay::ShapeDisplay(
const std::shared_ptr<Mesh> &mesh) {
13 _mesh_renderer = node()->create<
MeshRenderer>(mesh, material());
20 mesh.indices.emplace_back(0);
21 mesh.indices.emplace_back(2);
22 mesh.indices.emplace_back(1);
23 mesh.indices.emplace_back(0);
24 mesh.indices.emplace_back(3);
25 mesh.indices.emplace_back(2);
27 mesh.positions.emplace_back(-1, -1, 0);
28 mesh.positions.emplace_back(-1, 1, 0);
29 mesh.positions.emplace_back(1, 1, 0);
30 mesh.positions.emplace_back(1, -1, 0);
32 mesh.texcoords.emplace_back(0, 0);
33 mesh.texcoords.emplace_back(0, 1);
34 mesh.texcoords.emplace_back(1, 1);
35 mesh.texcoords.emplace_back(1, 0);
37 for (
size_t i = 0; i < 4; i++) {
38 mesh.normals.emplace_back(0, 0, 1);
39 mesh.tangents.emplace_back(1, 0, 0);
40 mesh.bitangents.emplace_back(0, 1, 0);
48 mesh += Eigen::AngleAxisf(M_PI, Eigen::Vector3f::UnitX()) *
49 Eigen::Translation3f(0, 0, 1) * makePlane();
50 mesh += Eigen::Translation3f(0, 0, 1) * makePlane();
51 for (
int i = 0; i < 4; i++) {
52 mesh += Eigen::AngleAxisf(M_PI * 0.5 * i, Eigen::Vector3f::UnitZ()) *
53 Eigen::AngleAxisf(M_PI * 0.5, Eigen::Vector3f::UnitX()) *
54 Eigen::Translation3f(0, 0, 1) * makePlane();
59 MeshData makeSphere(
size_t segments,
size_t rings) {
61 for (
size_t y = 0; y <= rings; y++) {
62 for (
size_t x = 0; x <= segments; x++) {
63 float u = x * 1.0 / segments;
64 float v = y * 1.0 / rings;
65 mesh.texcoords.emplace_back(u, v);
66 Eigen::Vector3f n(-std::cos(u * 2 * M_PI) * std::cos((v - 0.5) * M_PI),
67 std::sin(u * 2 * M_PI) * std::cos((v - 0.5) * M_PI),
68 std::sin((v - 0.5) * M_PI));
69 mesh.positions.emplace_back(n);
70 mesh.normals.emplace_back(n);
73 for (
size_t y = 0; y < rings; y++) {
74 for (
size_t x = 0; x < segments; x++) {
75 mesh.indices.emplace_back((y + 0) * (segments + 1) + (x + 0));
76 mesh.indices.emplace_back((y + 1) * (segments + 1) + (x + 0));
77 mesh.indices.emplace_back((y + 0) * (segments + 1) + (x + 1));
78 mesh.indices.emplace_back((y + 0) * (segments + 1) + (x + 1));
79 mesh.indices.emplace_back((y + 1) * (segments + 1) + (x + 0));
80 mesh.indices.emplace_back((y + 1) * (segments + 1) + (x + 1));
88 mesh.positions.emplace_back(0, 0, 0);
89 mesh.texcoords.emplace_back(0.5, 0.5);
90 mesh.normals.emplace_back(0, 0, 1);
91 for (
size_t i = 0; i <= segments; i++) {
92 float a = i * 1.0 / segments;
93 float x = std::sin(a * 2 * M_PI);
94 float y = std::cos(a * 2 * M_PI);
95 mesh.positions.emplace_back(x, y, 0.0);
96 mesh.texcoords.emplace_back(x * 0.5 + 0.5, y * 0.5 + 0.5);
97 mesh.normals.emplace_back(0, 0, 1);
99 for (
size_t i = 0; i < segments; i++) {
100 mesh.indices.emplace_back(0);
101 mesh.indices.emplace_back(i + 2);
102 mesh.indices.emplace_back(i + 1);
107 MeshData makeCylinder(
size_t segments) {
109 for (
int s : {-1, 1}) {
110 for (
size_t i = 0; i <= segments; i++) {
111 float a = i * 1.0 / segments;
112 float x = std::sin(a * 2 * M_PI);
113 float y = std::cos(a * 2 * M_PI);
114 mesh.positions.emplace_back(x, y, s);
115 mesh.texcoords.emplace_back(a, s * 0.5 + 0.5);
116 mesh.normals.emplace_back(x, y, 0);
119 for (
size_t i = 0; i < segments; i++) {
120 mesh.indices.emplace_back(i + 0);
121 mesh.indices.emplace_back(segments + 1 + i + 0);
122 mesh.indices.emplace_back(segments + 1 + i + 1);
123 mesh.indices.emplace_back(i + 0);
124 mesh.indices.emplace_back(segments + 1 + i + 1);
125 mesh.indices.emplace_back(i + 1);
127 mesh += Eigen::Translation3f(0.0f, 0.0f, 1.0f) * makeDisk(segments);
128 mesh += Eigen::AngleAxisf(M_PI, Eigen::Vector3f::UnitY()) *
129 Eigen::Translation3f(0.0f, 0.0f, 1.0f) * makeDisk(segments);
133 MeshData makeRing(
double inner,
double outer,
size_t segments) {
135 for (
double s : {0.0, 1.0}) {
136 for (
size_t i = 0; i <= segments; i++) {
137 float a = i * 1.0 / segments;
138 float x = std::sin(a * 2 * M_PI);
139 float y = std::cos(a * 2 * M_PI);
140 double f = inner + (outer - inner) * s;
141 mesh.positions.emplace_back(x * f, y * f, 0);
142 mesh.texcoords.emplace_back(a, s);
143 mesh.normals.emplace_back(0, 0, 1);
146 for (
size_t i = 0; i < segments; i++) {
147 mesh.indices.emplace_back(i + 0);
148 mesh.indices.emplace_back(segments + 1 + i + 0);
149 mesh.indices.emplace_back(segments + 1 + i + 1);
150 mesh.indices.emplace_back(i + 0);
151 mesh.indices.emplace_back(segments + 1 + i + 1);
152 mesh.indices.emplace_back(i + 1);
157 MeshData makeCone(
size_t segments) {
159 mesh.positions.emplace_back(0, 0, 1);
160 mesh.texcoords.emplace_back(0.5, 0.5);
161 mesh.normals.emplace_back(0, 0, 0);
162 for (
size_t i = 0; i <= segments; i++) {
163 float a = i * 1.0 / segments;
164 float x = std::sin(a * 2 * M_PI);
165 float y = std::cos(a * 2 * M_PI);
166 mesh.positions.emplace_back(x, y, 0.0);
167 mesh.texcoords.emplace_back(x * 0.5 + 0.5, y * 0.5 + 0.5);
168 mesh.normals.emplace_back(x, y, 1);
170 for (
size_t i = 0; i < segments; i++) {
171 mesh.indices.emplace_back(0);
172 mesh.indices.emplace_back(i + 2);
173 mesh.indices.emplace_back(i + 1);
176 Eigen::AngleAxisf(M_PI, Eigen::Vector3f::UnitY()) * makeDisk(segments);
183 std::make_shared<Mesh>(Eigen::Scaling(0.5f) * makePlane())) {}
186 _mesh_renderer->pose(
187 Eigen::Affine3d(Eigen::Scaling(size().x(), size().y(), 1.0)));
188 ShapeDisplay::renderSync(context);
195 PROPERTY(
double, radius, 1.0, min = 0.0);
196 PROPERTY(
int, segments, 32, min = 3);
198 if (_watcher.changed(segments())) {
199 LOG_DEBUG(
"make sphere");
201 std::make_shared<Mesh>(makeDisk(segments())), material());
203 _mesh_renderer->pose(Eigen::Affine3d(Eigen::Scaling(radius())));
204 ShapeDisplay::renderSync(context);
211 :
ShapeDisplay(std::make_shared<Mesh>(Eigen::Scaling(0.5f) * makeBox())) {
215 _mesh_renderer->pose(Eigen::Affine3d(Eigen::Scaling(size())));
216 ShapeDisplay::renderSync(context);
223 PROPERTY(
double, radius, 0.5, min = 0.0);
224 PROPERTY(
int, rings, 16, min = 3);
225 PROPERTY(
int, segments, 32, min = 3);
227 if (_watcher.changed(segments(), rings())) {
229 std::make_shared<Mesh>(makeSphere(segments(), rings())), material());
231 _mesh_renderer->pose(Eigen::Affine3d(Eigen::Scaling(radius())));
232 ShapeDisplay::renderSync(context);
239 PROPERTY(
double, radius, 0.5, min = 0.0);
240 PROPERTY(
double, height, 1.0, min = 1e-6);
241 PROPERTY(
int, segments, 32, min = 3);
243 if (_watcher.changed(segments())) {
245 std::make_shared<Mesh>(makeCylinder(segments())), material());
247 _mesh_renderer->pose(
248 Eigen::Affine3d(Eigen::Scaling(radius(), radius(), height())));
249 ShapeDisplay::renderSync(context);
256 PROPERTY(
double, radius, 0.5, min = 0.0);
257 PROPERTY(
double, height, 1.0, min = 1e-6);
258 PROPERTY(
int, segments, 32, min = 3);
260 if (_watcher.changed(segments())) {
262 std::make_shared<Mesh>(makeCone(segments())), material());
264 _mesh_renderer->pose(
265 Eigen::Affine3d(Eigen::Scaling(radius(), radius(), height())));
266 ShapeDisplay::renderSync(context);