Visual Computing Library
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/math/base.h>
27#include <vclib/mesh/requirements.h>
28
29namespace vcl {
30
31template<FaceMeshConcept MeshType>
32MeshType createCone(
33 auto radiusBottom,
34 auto radiusTop,
35 auto height,
36 const uint subdivisions = 36)
37{
38 using CoordType = MeshType::VertexType::CoordType;
39 using ScalarType = CoordType::ScalarType;
40
41 uint vn, fn;
42 if (radiusBottom == 0 || radiusTop == 0) {
43 vn = subdivisions + 2;
44 fn = subdivisions * 2;
45 }
46 else {
47 vn = subdivisions * 2 + 2;
48 fn = subdivisions * 4;
49 }
50
51 MeshType mesh;
52
53 mesh.reserveVertices(vn);
54 mesh.reserveFaces(fn);
55
56 mesh.addVertex(CoordType(0, -height / 2.0, 0));
57 mesh.addVertex(CoordType(0, height / 2.0, 0));
58
59 uint b1 = 2;
60 uint b2 = 2;
61
62 if (radiusBottom != 0) {
63 for (uint i = 0; i < subdivisions; ++i) {
64 ScalarType a = toRad(i * 360.0 / subdivisions);
65 mesh.addVertex(CoordType(
66 radiusBottom * std::cos(a),
67 -height / 2.0,
68 radiusBottom * std::sin(a)));
69 }
70
71 b2 += subdivisions;
72 }
73
74 if (radiusTop != 0) {
75 for (uint i = 0; i < subdivisions; ++i) {
76 ScalarType a = toRad(i * 360.0 / subdivisions);
77 mesh.addVertex(CoordType(
78 radiusTop * std::cos(a),
79 height / 2.0,
80 radiusTop * std::sin(a)));
81 }
82 }
83
84 if (radiusBottom != 0) {
85 for (uint i = 0; i < subdivisions; ++i) {
86 mesh.addFace(0, b1 + i, b1 + (i + 1) % subdivisions);
87 }
88 }
89 else {
90 for (uint i = 0; i < subdivisions; ++i) {
91 mesh.addFace(0, b2 + i, b2 + (i + 1) % subdivisions);
92 }
93 }
94
95 if (radiusTop != 0) {
96 for (uint i = 0; i < subdivisions; ++i) {
97 mesh.addFace(1, b2 + (i + 1) % subdivisions, b2 + i);
98 }
99 }
100 else {
101 for (uint i = 0; i < subdivisions; ++i) {
102 mesh.addFace(1, b1 + (i + 1) % subdivisions, b1 + i);
103 }
104 }
105
106 // side triangles
107 if (radiusBottom != 0 && radiusTop != 0) {
108 for (uint i = 0; i < subdivisions; ++i) {
109 mesh.addFace(b1 + i, b2 + i, b2 + (i + 1) % subdivisions);
110 mesh.addFace(
111 b1 + i,
112 b2 + (i + 1) % subdivisions,
113 b1 + (i + 1) % subdivisions);
114 }
115 }
116
117 return mesh;
118}
119
120template<FaceMeshConcept MeshType>
121MeshType createCylinder(auto radius, auto height, const uint subdivisions = 36)
122{
123 return createCone<MeshType>(radius, radius, height, subdivisions);
124}
125
126} // namespace vcl
127
128#endif // VCL_ALGORITHMS_MESH_CREATE_CONE_H
Scalar toRad(const Scalar &deg)
Converts an angle in degrees to radians.
Definition base.h:83