23#ifndef VCL_ALGORITHMS_MESH_CREATE_SPHERE_H
24#define VCL_ALGORITHMS_MESH_CREATE_SPHERE_H
26#include "icosahedron.h"
28#include <vclib/algorithms/mesh/clean.h>
29#include <vclib/algorithms/mesh/update/normal.h>
30#include <vclib/algorithms/mesh/update/transform.h>
32#include <vclib/mesh.h>
33#include <vclib/space/core.h>
64 CreateSphereMode mode = UV;
120template<FaceMeshConcept MeshType>
126 using VertexType = MeshType::VertexType;
127 using PositionType = VertexType::PositionType;
128 using Facetype = MeshType::FaceType;
132 mesh.addVertex(PositionType(0, 1, 0));
133 for (uint
j = 0;
j < parallels - 1; ++
j) {
135 const double sp = std::sin(
polar);
136 const double cp = std::cos(
polar);
137 for (uint
i = 0;
i < meridians; ++
i) {
141 const double x =
sp *
ca;
143 const double z =
sp *
sa;
144 mesh.addVertex(PositionType(x, y, z));
147 mesh.addVertex(PositionType(0, -1, 0));
149 for (uint
i = 0;
i < meridians; ++
i) {
150 VertexType* v = &mesh.vertex(0);
151 VertexType*
a = &mesh.vertex(
i + 1);
152 VertexType*
b = &mesh.vertex((
i + 1) % meridians + 1);
153 mesh.addFace(0,
b,
a);
160 VertexType*
a = &mesh.vertex(
aStart +
i);
161 VertexType*
a1 = &mesh.vertex(
aStart + (
i + 1) % meridians);
162 VertexType*
b = &mesh.vertex(
bStart +
i);
163 VertexType*
b1 = &mesh.vertex(
bStart + (
i + 1) % meridians);
165 mesh.addFace(
a,
a1,
b1);
166 mesh.addFace(
b1,
b,
a);
169 mesh.addFace(
a,
a1,
b1,
b);
175 VertexType*
a = &mesh.vertex(
i + meridians * (parallels - 2) + 1);
177 &mesh.vertex((
i + 1) % meridians + meridians * (parallels - 2) + 1);
178 VertexType* v = &mesh.vertex(mesh.vertexNumber() - 1);
179 mesh.addFace(v,
a,
b);
182 scale(mesh,
sp.radius());
201template<FaceMeshConcept MeshType>
206 using VertexType = MeshType::VertexType;
207 using PositionType = VertexType::PositionType;
208 using Facetype = MeshType::FaceType;
209 using ScalarType = PositionType::ScalarType;
213 const double step = 1.0 /
double(divisions);
214 const PositionType
step3(step, step, step);
216 for (uint face = 0; face < 6; ++face) {
217 const PositionType
origin =
218 detail::cts::origins[face].cast<ScalarType>();
219 const PositionType
right = detail::cts::rights[face].cast<ScalarType>();
220 const PositionType up = detail::cts::ups[face].cast<ScalarType>();
221 for (uint
j = 0;
j < divisions + 1; ++
j) {
222 const PositionType
j3(
j,
j,
j);
223 for (uint
i = 0;
i < divisions + 1; ++
i) {
224 const PositionType
i3(
i,
i,
i);
225 const PositionType
p =
228 mesh.addVertex(
p.normalized());
233 const uint
k = divisions + 1;
234 for (uint face = 0; face < 6; ++face) {
235 for (uint
j = 0;
j < divisions; ++
j) {
236 const bool bottom =
j < (divisions / 2);
237 for (uint
i = 0;
i < divisions; ++
i) {
238 const bool left =
i < (divisions / 2);
239 VertexType*
a = &mesh.vertex((face *
k +
j) *
k +
i);
240 VertexType*
b = &mesh.vertex((face *
k +
j) *
k +
i + 1);
241 VertexType* c = &mesh.vertex((face *
k +
j + 1) *
k +
i);
242 VertexType* d = &mesh.vertex((face *
k +
j + 1) *
k +
i + 1);
246 mesh.addFace(
a, c,
b);
247 mesh.addFace(c, d,
b);
250 mesh.addFace(
a, c, d);
251 mesh.addFace(
a, d,
b);
255 mesh.addFace(
a, c, d,
b);
261 scale(mesh,
sp.radius());
280template<FaceMeshConcept MeshType>
285 using VertexType = MeshType::VertexType;
286 using PositionType = VertexType::PositionType;
287 using ScalarType = PositionType::ScalarType;
288 using Facetype = MeshType::FaceType;
292 const double step = 1.0 /
double(divisions);
293 const PositionType
step3(step, step, step);
295 for (uint face = 0; face < 6; ++face) {
296 const PositionType
origin =
297 detail::cts::origins[face].cast<ScalarType>();
298 const PositionType
right = detail::cts::rights[face].cast<ScalarType>();
299 const PositionType up = detail::cts::ups[face].cast<ScalarType>();
300 for (uint
j = 0;
j < divisions + 1; ++
j) {
301 const PositionType
j3(
j,
j,
j);
302 for (uint
i = 0;
i < divisions + 1; ++
i) {
303 const PositionType
i3(
i,
i,
i);
304 const PositionType
p =
306 const PositionType
p2 =
p.mul(
p);
307 const PositionType
n(
309 1.0 - 0.5 * (
p2.y() +
p2.z()) +
310 p2.y() *
p2.z() / 3.0),
312 1.0 - 0.5 * (
p2.z() +
p2.x()) +
313 p2.z() *
p2.x() / 3.0),
315 1.0 - 0.5 * (
p2.x() +
p2.y()) +
316 p2.x() *
p2.y() / 3.0));
322 const uint
k = divisions + 1;
323 for (uint face = 0; face < 6; ++face) {
324 for (uint
j = 0;
j < divisions; ++
j) {
325 const bool bottom =
j < (divisions / 2);
326 for (uint
i = 0;
i < divisions; ++
i) {
327 const bool left =
i < (divisions / 2);
328 VertexType*
a = &mesh.vertex((face *
k +
j) *
k +
i);
329 VertexType*
b = &mesh.vertex((face *
k +
j) *
k +
i + 1);
330 VertexType* c = &mesh.vertex((face *
k +
j + 1) *
k +
i);
331 VertexType* d = &mesh.vertex((face *
k +
j + 1) *
k +
i + 1);
334 mesh.addFace(
a, c,
b);
335 mesh.addFace(c, d,
b);
338 mesh.addFace(
a, c, d);
339 mesh.addFace(
a, d,
b);
343 mesh.addFace(
a, c, d,
b);
349 scale(mesh,
sp.radius());
368template<FaceMeshConcept MeshType>
371 using VertexType = MeshType::VertexType;
372 using PositionType = VertexType::PositionType;
373 using FaceType = MeshType::FaceType;
377 for (uint d = 0; d < divisions; d++) {
378 uint
nf = mesh.faceNumber();
379 for (uint f = 0; f <
nf; f++) {
380 FaceType&
f0 = mesh.face(f);
381 VertexType&
v0 = *
f0.vertex(0);
382 VertexType& v1 = *
f0.vertex(1);
383 VertexType& v2 = *
f0.vertex(2);
384 uint
v1id = mesh.index(v1);
385 uint
v2id = mesh.index(v2);
387 PositionType
pa = (
v0.position() + v1.position());
389 PositionType
pb = (v1.position() + v2.position());
391 PositionType
pc = (v2.position() +
v0.position());
393 uint
vaid = mesh.addVertex(
pa);
394 uint
vbid = mesh.addVertex(
pb);
395 uint
vcid = mesh.addVertex(
pc);
407 scale(mesh,
sp.radius());
428template<FaceMeshConcept MeshType>
435 case CreateSphereArgs::UV:
438 case CreateSphereArgs::NORMALIZED_CUBE:
441 case CreateSphereArgs::SPHERIFIED_CUBE:
444 case CreateSphereArgs::ICOSAHEDRON:
464template<FaceMeshConcept MeshType>
A class representing a box in N-dimensional space.
Definition box.h:46
PointT center() const
Calculates the center point of the box.
Definition box.h:259
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:55
Definition face_requirements.h:52
A concept representing a Sphere.
Definition sphere.h:127
uint removeDuplicatedVertices(MeshType &m)
Marks as deleted the duplicate vertices of the mesh, by looking only at their spatial positions.
Definition clean.h:211
MeshType createSphereUV(const SphereConcept auto &sp, uint parallels=10, uint meridians=20)
Creates and returns a sphere mesh using the UV mode, starting from a sphere object.
Definition sphere.h:121
MeshType createSphere()
Creates a Sphere Mesh using the UV mode (https://github.com/caosdoar/spheres), centered in (0,...
Definition sphere.h:465
MeshType createSphereIcosahedron(const SphereConcept auto &sp, uint divisions)
Creates and returns a sphere mesh using the icosahedron mode, starting from a sphere object.
Definition sphere.h:369
MeshType createSphereSpherifiedCube(const SphereConcept auto &sp, uint divisions)
Creates and returns a sphere mesh using the spherified cube mode, starting from a sphere object.
Definition sphere.h:281
MeshType createSphereNormalizedCube(const SphereConcept auto &sp, uint divisions)
Creates and returns a sphere mesh using the normalized cube mode, starting from a sphere object.
Definition sphere.h:202
Point3< double > Point3d
A convenience alias for a 3-dimensional Point with double-precision floating-point components.
Definition point.h:779
The CreateSphereArgs structs contains a series of parameters to generate a sphere.
Definition sphere.h:56