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/transform.h>
30#include <vclib/mesh/requirements.h>
31#include <vclib/space/core/sphere.h>
62 CreateSphereMode mode = UV;
118template<FaceMeshConcept MeshType>
124 using VertexType = MeshType::VertexType;
125 using CoordType = VertexType::CoordType;
126 using Facetype = MeshType::FaceType;
130 mesh.addVertex(CoordType(0, 1, 0));
131 for (uint
j = 0;
j < parallels - 1; ++
j) {
133 const double sp = std::sin(
polar);
134 const double cp = std::cos(
polar);
135 for (uint
i = 0;
i < meridians; ++
i) {
139 const double x =
sp *
ca;
141 const double z =
sp *
sa;
142 mesh.addVertex(CoordType(x, y, z));
145 mesh.addVertex(CoordType(0, -1, 0));
147 for (uint
i = 0;
i < meridians; ++
i) {
148 VertexType* v = &mesh.vertex(0);
149 VertexType*
a = &mesh.vertex(
i + 1);
150 VertexType*
b = &mesh.vertex((
i + 1) % meridians + 1);
151 mesh.addFace(0,
b,
a);
158 VertexType*
a = &mesh.vertex(
aStart +
i);
159 VertexType*
a1 = &mesh.vertex(
aStart + (
i + 1) % meridians);
160 VertexType*
b = &mesh.vertex(
bStart +
i);
161 VertexType*
b1 = &mesh.vertex(
bStart + (
i + 1) % meridians);
163 mesh.addFace(
a,
a1,
b1);
164 mesh.addFace(
b1,
b,
a);
167 mesh.addFace(
a,
a1,
b1,
b);
173 VertexType*
a = &mesh.vertex(
i + meridians * (parallels - 2) + 1);
175 &mesh.vertex((
i + 1) % meridians + meridians * (parallels - 2) + 1);
176 VertexType* v = &mesh.vertex(mesh.vertexNumber() - 1);
177 mesh.addFace(v,
a,
b);
180 scale(mesh,
sp.radius());
181 translate(mesh,
sp.center());
199template<FaceMeshConcept MeshType>
204 using VertexType = MeshType::VertexType;
205 using CoordType = VertexType::CoordType;
206 using Facetype = MeshType::FaceType;
207 using ScalarType = CoordType::ScalarType;
214 for (uint face = 0; face < 6; ++face) {
215 const CoordType
origin = detail::cts::origins[face].cast<ScalarType>();
216 const CoordType
right = detail::cts::rights[face].cast<ScalarType>();
217 const CoordType up = detail::cts::ups[face].cast<ScalarType>();
218 for (uint
j = 0;
j < divisions + 1; ++
j) {
219 const CoordType
j3(
j,
j,
j);
220 for (uint
i = 0;
i < divisions + 1; ++
i) {
221 const CoordType
i3(
i,
i,
i);
225 mesh.addVertex(
p.normalized());
230 const uint
k = divisions + 1;
231 for (uint face = 0; face < 6; ++face) {
232 for (uint
j = 0;
j < divisions; ++
j) {
233 const bool bottom =
j < (divisions / 2);
234 for (uint
i = 0;
i < divisions; ++
i) {
235 const bool left =
i < (divisions / 2);
236 VertexType*
a = &mesh.vertex((face *
k +
j) *
k +
i);
237 VertexType*
b = &mesh.vertex((face *
k +
j) *
k +
i + 1);
238 VertexType* c = &mesh.vertex((face *
k +
j + 1) *
k +
i);
239 VertexType* d = &mesh.vertex((face *
k +
j + 1) *
k +
i + 1);
243 mesh.addFace(
a, c,
b);
244 mesh.addFace(c, d,
b);
247 mesh.addFace(
a, c, d);
248 mesh.addFace(
a, d,
b);
252 mesh.addFace(
a, c, d,
b);
258 scale(mesh,
sp.radius());
259 translate(mesh,
sp.center());
277template<FaceMeshConcept MeshType>
282 using VertexType = MeshType::VertexType;
283 using CoordType = VertexType::CoordType;
284 using ScalarType = CoordType::ScalarType;
285 using Facetype = MeshType::FaceType;
292 for (uint face = 0; face < 6; ++face) {
293 const CoordType
origin = detail::cts::origins[face].cast<ScalarType>();
294 const CoordType
right = detail::cts::rights[face].cast<ScalarType>();
295 const CoordType up = detail::cts::ups[face].cast<ScalarType>();
296 for (uint
j = 0;
j < divisions + 1; ++
j) {
297 const CoordType
j3(
j,
j,
j);
298 for (uint
i = 0;
i < divisions + 1; ++
i) {
299 const CoordType
i3(
i,
i,
i);
302 const CoordType
p2 =
p.mul(
p);
305 1.0 - 0.5 * (
p2.y() +
p2.z()) +
306 p2.y() *
p2.z() / 3.0),
308 1.0 - 0.5 * (
p2.z() +
p2.x()) +
309 p2.z() *
p2.x() / 3.0),
311 1.0 - 0.5 * (
p2.x() +
p2.y()) +
312 p2.x() *
p2.y() / 3.0));
318 const uint
k = divisions + 1;
319 for (uint face = 0; face < 6; ++face) {
320 for (uint
j = 0;
j < divisions; ++
j) {
321 const bool bottom =
j < (divisions / 2);
322 for (uint
i = 0;
i < divisions; ++
i) {
323 const bool left =
i < (divisions / 2);
324 VertexType*
a = &mesh.vertex((face *
k +
j) *
k +
i);
325 VertexType*
b = &mesh.vertex((face *
k +
j) *
k +
i + 1);
326 VertexType* c = &mesh.vertex((face *
k +
j + 1) *
k +
i);
327 VertexType* d = &mesh.vertex((face *
k +
j + 1) *
k +
i + 1);
330 mesh.addFace(
a, c,
b);
331 mesh.addFace(c, d,
b);
334 mesh.addFace(
a, c, d);
335 mesh.addFace(
a, d,
b);
339 mesh.addFace(
a, c, d,
b);
345 scale(mesh,
sp.radius());
346 translate(mesh,
sp.center());
364template<FaceMeshConcept MeshType>
367 using VertexType = MeshType::VertexType;
368 using CoordType = VertexType::CoordType;
369 using FaceType = MeshType::FaceType;
373 for (uint d = 0; d < divisions; d++) {
374 uint
nf = mesh.faceNumber();
375 for (uint f = 0; f <
nf; f++) {
376 FaceType&
f0 = mesh.face(f);
377 VertexType&
v0 = *
f0.vertex(0);
378 VertexType& v1 = *
f0.vertex(1);
379 VertexType& v2 = *
f0.vertex(2);
380 uint
v1id = mesh.index(v1);
381 uint
v2id = mesh.index(v2);
383 CoordType
pa = (
v0.coord() + v1.coord());
385 CoordType
pb = (v1.coord() + v2.coord());
387 CoordType
pc = (v2.coord() +
v0.coord());
389 uint
vaid = mesh.addVertex(
pa);
390 uint
vbid = mesh.addVertex(
pb);
391 uint
vcid = mesh.addVertex(
pc);
403 scale(mesh,
sp.radius());
404 translate(mesh,
sp.center());
424template<FaceMeshConcept MeshType>
431 case CreateSphereArgs::UV:
434 case CreateSphereArgs::NORMALIZED_CUBE:
437 case CreateSphereArgs::SPHERIFIED_CUBE:
440 case CreateSphereArgs::ICOSAHEDRON:
460template<FaceMeshConcept MeshType>
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:58
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Concept for types representing spheres in Euclidean space.
Definition sphere.h:64
uint removeDuplicatedVertices(MeshType &m)
Marks as deleted the duplicate vertices of the mesh, by looking only at their spatial positions.
Definition clean.h:301
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:119
MeshType createSphere()
Creates a Sphere Mesh using the UV mode (https://github.com/caosdoar/spheres), centered in (0,...
Definition sphere.h:461
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:365
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:278
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:200
Point3< double > Point3d
A convenience alias for a 3-dimensional Point with double-precision floating-point components.
Definition point.h:804
The CreateSphereArgs structs contains a series of parameters to generate a sphere.
Definition sphere.h:54