1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use super::utils;
#[cfg(feature = "dim2")]
use super::Polyline;
#[cfg(feature = "dim3")]
use super::{IndexBuffer, TriMesh};
use na;
#[cfg(feature = "dim3")]
use na::{Point2, Point3, Vector3};
use simba::scalar::RealField;
#[cfg(feature = "dim3")]
pub fn sphere<N>(
diameter: N,
ntheta_subdiv: u32,
nphi_subdiv: u32,
generate_uvs: bool,
) -> TriMesh<N>
where
N: RealField,
{
let mut sphere = unit_sphere(ntheta_subdiv, nphi_subdiv, generate_uvs);
sphere.scale_by_scalar(diameter);
sphere
}
#[cfg(feature = "dim3")]
pub fn unit_sphere<N>(ntheta_subdiv: u32, nphi_subdiv: u32, generate_uvs: bool) -> TriMesh<N>
where
N: RealField,
{
if generate_uvs {
unit_sphere_with_uvs(ntheta_subdiv, nphi_subdiv)
} else {
unit_sphere_without_uvs(ntheta_subdiv, nphi_subdiv)
}
}
#[cfg(feature = "dim3")]
fn unit_sphere_without_uvs<N>(ntheta_subdiv: u32, nphi_subdiv: u32) -> TriMesh<N>
where
N: RealField,
{
let pi = N::pi();
let two_pi = N::two_pi();
let pi_two = N::frac_pi_2();
let dtheta = two_pi / na::convert(ntheta_subdiv as f64);
let dphi = pi / na::convert(nphi_subdiv as f64);
let mut coords = Vec::new();
let mut curr_phi = -pi_two + dphi;
coords.push(Point3::new(na::zero(), -na::one::<N>(), na::zero()));
for _ in 0..nphi_subdiv - 1 {
utils::push_circle(
curr_phi.cos(),
ntheta_subdiv,
dtheta,
curr_phi.sin(),
&mut coords,
);
curr_phi = curr_phi + dphi;
}
coords.push(Point3::new(na::zero(), na::one(), na::zero()));
let normals: Vec<Vector3<N>> = coords.iter().map(|p| p.coords).collect();
let mut idx = Vec::new();
utils::push_degenerate_top_ring_indices(1, 0, ntheta_subdiv, &mut idx);
utils::reverse_clockwising(&mut idx[..]);
for i in 0..nphi_subdiv - 2 {
let bottom = 1 + i * ntheta_subdiv;
let up = bottom + ntheta_subdiv;
utils::push_ring_indices(bottom, up, ntheta_subdiv, &mut idx);
}
utils::push_degenerate_top_ring_indices(
1 + (nphi_subdiv - 2) * ntheta_subdiv,
coords.len() as u32 - 1,
ntheta_subdiv,
&mut idx,
);
let mut res = TriMesh::new(coords, Some(normals), None, Some(IndexBuffer::Unified(idx)));
let _0_5: N = na::convert(0.5);
res.scale_by_scalar(_0_5);
res
}
#[cfg(feature = "dim3")]
fn unit_sphere_with_uvs<N: RealField>(ntheta_subdiv: u32, nphi_subdiv: u32) -> TriMesh<N> {
let pi = N::pi();
let two_pi = N::two_pi();
let pi_two = N::frac_pi_2();
let duvtheta = N::one() / na::convert(ntheta_subdiv as f64);
let duvphi = N::one() / na::convert(nphi_subdiv as f64);
let dtheta = two_pi * duvtheta;
let dphi = pi * duvphi;
let mut coords = Vec::new();
let mut curr_phi = -pi_two;
for _ in 0..nphi_subdiv + 1 {
utils::push_circle(
curr_phi.cos(),
ntheta_subdiv + 1,
dtheta,
curr_phi.sin(),
&mut coords,
);
curr_phi = curr_phi + dphi;
}
let normals: Vec<Vector3<N>> = coords.iter().map(|p| p.coords).collect();
let mut idx = Vec::new();
for i in 0..nphi_subdiv {
let bottom = i * (ntheta_subdiv + 1);
let up = bottom + (ntheta_subdiv + 1);
utils::push_open_ring_indices(bottom, up, ntheta_subdiv + 1, &mut idx);
}
let mut uvs = Vec::new();
let mut curr_uvphi = na::zero::<N>();
for _ in 0..nphi_subdiv + 1 {
let mut curr_uvtheta = na::zero::<N>();
for _ in 0..ntheta_subdiv + 1 {
uvs.push(Point2::new(curr_uvtheta, curr_uvphi));
curr_uvtheta = curr_uvtheta + duvtheta;
}
curr_uvphi = curr_uvphi + duvphi;
}
let mut res = TriMesh::new(
coords,
Some(normals),
Some(uvs),
Some(IndexBuffer::Unified(idx)),
);
let _0_5: N = na::convert(0.5);
res.scale_by_scalar(_0_5);
res
}
#[cfg(feature = "dim3")]
pub fn unit_hemisphere<N: RealField>(ntheta_subdiv: u32, nphi_subdiv: u32) -> TriMesh<N> {
let two_pi = N::two_pi();
let pi_two = N::frac_pi_2();
let dtheta = two_pi / na::convert(ntheta_subdiv as f64);
let dphi = pi_two / na::convert(nphi_subdiv as f64);
let mut coords = Vec::new();
let mut curr_phi = na::zero::<N>();
for _ in 0..nphi_subdiv - 1 {
utils::push_circle(
curr_phi.cos(),
ntheta_subdiv,
dtheta,
curr_phi.sin(),
&mut coords,
);
curr_phi = curr_phi + dphi;
}
coords.push(Point3::new(na::zero(), na::one(), na::zero()));
let mut idx = Vec::new();
for i in 0..nphi_subdiv - 2 {
utils::push_ring_indices(
i * ntheta_subdiv,
(i + 1) * ntheta_subdiv,
ntheta_subdiv,
&mut idx,
);
}
utils::push_degenerate_top_ring_indices(
(nphi_subdiv - 2) * ntheta_subdiv,
coords.len() as u32 - 1,
ntheta_subdiv,
&mut idx,
);
let normals: Vec<Vector3<N>> = coords.iter().map(|p| p.coords).collect();
let mut out = TriMesh::new(coords, Some(normals), None, Some(IndexBuffer::Unified(idx)));
let _0_5: N = na::convert(0.5);
out.scale_by_scalar(_0_5);
out
}
#[cfg(feature = "dim2")]
pub fn circle<N: RealField>(diameter: &N, nsubdivs: u32) -> Polyline<N> {
let two_pi = N::two_pi();
let dtheta = two_pi / na::convert(nsubdivs as f64);
let mut pts = Vec::with_capacity(nsubdivs as usize);
utils::push_xy_arc(*diameter / na::convert(2.0), nsubdivs, dtheta, &mut pts);
Polyline::new(pts, None)
}
#[cfg(feature = "dim2")]
pub fn unit_circle<N: RealField>(nsubdivs: u32) -> Polyline<N> {
circle(&na::convert(1.0), nsubdivs)
}