Visual Computing Library  devel
Loading...
Searching...
No Matches
cone.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_ALGORITHMS_MESH_CREATE_CONE_H
24#define VCL_ALGORITHMS_MESH_CREATE_CONE_H
25
26#include <vclib/mesh.h>
27
28namespace vcl {
29
30template<FaceMeshConcept MeshType>
31MeshType createCone(
32 auto radiusBottom,
33 auto radiusTop,
34 auto height,
35 const uint subdivisions = 36)
36{
37 using PositionType = MeshType::VertexType::PositionType;
38 using ScalarType = PositionType::ScalarType;
39
40 uint vn, fn;
41 if (radiusBottom == 0 || radiusTop == 0) {
42 vn = subdivisions + 2;
43 fn = subdivisions * 2;
44 }
45 else {
46 vn = subdivisions * 2 + 2;
47 fn = subdivisions * 4;
48 }
49
50 MeshType mesh;
51
52 mesh.reserveVertices(vn);
53 mesh.reserveFaces(fn);
54
55 mesh.addVertex(PositionType(0, -height / 2.0, 0));
56 mesh.addVertex(PositionType(0, height / 2.0, 0));
57
58 uint b1 = 2;
59 uint b2 = 2;
60
61 if (radiusBottom != 0) {
62 for (uint i = 0; i < subdivisions; ++i) {
63 ScalarType a = toRad(i * 360.0 / subdivisions);
64 mesh.addVertex(PositionType(
65 radiusBottom * std::cos(a),
66 -height / 2.0,
67 radiusBottom * std::sin(a)));
68 }
69
70 b2 += subdivisions;
71 }
72
73 if (radiusTop != 0) {
74 for (uint i = 0; i < subdivisions; ++i) {
75 ScalarType a = toRad(i * 360.0 / subdivisions);
76 mesh.addVertex(PositionType(
77 radiusTop * std::cos(a),
78 height / 2.0,
79 radiusTop * std::sin(a)));
80 }
81 }
82
83 if (radiusBottom != 0) {
84 for (uint i = 0; i < subdivisions; ++i) {
85 mesh.addFace(0, b1 + i, b1 + (i + 1) % subdivisions);
86 }
87 }
88 else {
89 for (uint i = 0; i < subdivisions; ++i) {
90 mesh.addFace(0, b2 + i, b2 + (i + 1) % subdivisions);
91 }
92 }
93
94 if (radiusTop != 0) {
95 for (uint i = 0; i < subdivisions; ++i) {
96 mesh.addFace(1, b2 + (i + 1) % subdivisions, b2 + i);
97 }
98 }
99 else {
100 for (uint i = 0; i < subdivisions; ++i) {
101 mesh.addFace(1, b1 + (i + 1) % subdivisions, b1 + i);
102 }
103 }
104
105 // side triangles
106 if (radiusBottom != 0 && radiusTop != 0) {
107 for (uint i = 0; i < subdivisions; ++i) {
108 mesh.addFace(b1 + i, b2 + i, b2 + (i + 1) % subdivisions);
109 mesh.addFace(
110 b1 + i,
111 b2 + (i + 1) % subdivisions,
112 b1 + (i + 1) % subdivisions);
113 }
114 }
115
116 return mesh;
117}
118
119template<FaceMeshConcept MeshType>
120MeshType createCylinder(auto radius, auto height, const uint subdivisions = 36)
121{
122 return createCone<MeshType>(radius, radius, height, subdivisions);
123}
124
125} // namespace vcl
126
127#endif // VCL_ALGORITHMS_MESH_CREATE_CONE_H
Scalar toRad(const Scalar &deg)
Converts an angle in degrees to radians.
Definition math.h:81