Blender V5.3
mesh_primitive_uv_sphere.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10
11#include "BLI_math_base.hh"
12
13#include "BKE_attribute.hh"
14#include "BKE_mesh.hh"
15
17
18namespace blender::geometry {
19
21 const float radius_bottom,
22 const int segments,
23 const float height)
24{
25 const float radius = std::max(radius_top, radius_bottom);
26 const float delta_phi = (2.0f * std::numbers::pi) / float(segments);
27
28 const float x_max = radius;
29 const float x_min = std::cos(std::round(0.5f * segments) * delta_phi) * radius;
30 const float y_max = std::sin(std::round(0.25f * segments) * delta_phi) * radius;
31 const float y_min = -y_max;
32
33 const float3 bounds_min(x_min, y_min, -height);
34 const float3 bounds_max(x_max, y_max, height);
35
36 return {bounds_min, bounds_max};
37}
38
39static int sphere_vert_total(const int segments, const int rings)
40{
41 return segments * (rings - 1) + 2;
42}
43
44static int sphere_edge_total(const int segments, const int rings)
45{
46 return segments * (rings * 2 - 1);
47}
48
49static int sphere_corner_total(const int segments, const int rings)
50{
51 const int quad_corners = 4 * segments * (rings - 2);
52 const int tri_corners = 3 * segments * 2;
53 return quad_corners + tri_corners;
54}
55
56static int sphere_face_total(const int segments, const int rings)
57{
58 const int quads = segments * (rings - 2);
59 const int triangles = segments * 2;
60 return quads + triangles;
61}
62
68 MutableSpan<float3> vert_normals,
69 const float radius,
70 const int segments,
71 const int rings)
72{
73 const float delta_theta = std::numbers::pi / rings;
74 const float delta_phi = (2.0f * std::numbers::pi) / segments;
75
76 Array<float, 64> segment_cosines(segments + 1);
77 for (const int segment : IndexRange(1, segments)) {
78 const float phi = segment * delta_phi;
79 segment_cosines[segment] = std::cos(phi);
80 }
81 Array<float, 64> segment_sines(segments + 1);
82 for (const int segment : IndexRange(1, segments)) {
83 const float phi = segment * delta_phi;
84 segment_sines[segment] = std::sin(phi);
85 }
86
87 positions[0] = float3(0.0f, 0.0f, radius);
88 vert_normals.first() = float3(0.0f, 0.0f, 1.0f);
89
90 int vert_index = 1;
91 for (const int ring : IndexRange(1, rings - 1)) {
92 const float theta = ring * delta_theta;
93 const float sin_theta = std::sin(theta);
94 const float z = std::cos(theta);
95 for (const int segment : IndexRange(1, segments)) {
96 const float x = sin_theta * segment_cosines[segment];
97 const float y = sin_theta * segment_sines[segment];
98 positions[vert_index] = float3(x, y, z) * radius;
99 vert_normals[vert_index] = float3(x, y, z);
100 vert_index++;
101 }
102 }
103
104 positions.last() = float3(0.0f, 0.0f, -radius);
105 vert_normals.last() = float3(0.0f, 0.0f, -1.0f);
106}
107
109 const int segments,
110 const int rings)
111{
112 int edge_index = 0;
113
114 /* Add the edges connecting the top vertex to the first ring. */
115 const int first_vert_ring_index_start = 1;
116 for (const int segment : IndexRange(segments)) {
117 int2 &edge = edges[edge_index++];
118 edge[0] = 0;
119 edge[1] = first_vert_ring_index_start + segment;
120 }
121
122 int ring_vert_index_start = 1;
123 for (const int ring : IndexRange(rings - 1)) {
124 const int next_ring_vert_index_start = ring_vert_index_start + segments;
125
126 /* Add the edges running along each ring. */
127 for (const int segment : IndexRange(segments)) {
128 int2 &edge = edges[edge_index++];
129 edge[0] = ring_vert_index_start + segment;
130 edge[1] = ring_vert_index_start + ((segment + 1) % segments);
131 }
132
133 /* Add the edges connecting to the next ring. */
134 if (ring < rings - 2) {
135 for (const int segment : IndexRange(segments)) {
136 int2 &edge = edges[edge_index++];
137 edge[0] = ring_vert_index_start + segment;
138 edge[1] = next_ring_vert_index_start + segment;
139 }
140 }
141 ring_vert_index_start += segments;
142 }
143
144 /* Add the edges connecting the last ring to the bottom vertex. */
145 const int last_vert_index = sphere_vert_total(segments, rings) - 1;
146 const int last_vert_ring_start = last_vert_index - segments;
147 for (const int segment : IndexRange(segments)) {
148 int2 &edge = edges[edge_index++];
149 edge[0] = last_vert_index;
150 edge[1] = last_vert_ring_start + segment;
151 }
152}
153
154BLI_NOINLINE static void calculate_sphere_faces(MutableSpan<int> face_offsets, const int segments)
155{
156 MutableSpan<int> face_sizes = face_offsets.drop_back(1);
157 /* Add the triangles connected to the top vertex. */
158 face_sizes.take_front(segments).fill(3);
159 /* Add the middle quads. */
160 face_sizes.drop_front(segments).drop_back(segments).fill(4);
161 /* Add the triangles connected to the bottom vertex. */
162 face_sizes.take_back(segments).fill(3);
163
165}
166
168 MutableSpan<int> corner_edges,
169 const int segments,
170 const int rings)
171{
172 auto segment_next_or_first = [&](const int segment) {
173 return segment == segments - 1 ? 0 : segment + 1;
174 };
175
176 /* Add the triangles connected to the top vertex. */
177 const int first_vert_ring_start = 1;
178 for (const int segment : IndexRange(segments)) {
179 const int loop_start = segment * 3;
180 const int segment_next = segment_next_or_first(segment);
181
182 corner_verts[loop_start + 0] = 0;
183 corner_edges[loop_start + 0] = segment;
184
185 corner_verts[loop_start + 1] = first_vert_ring_start + segment;
186 corner_edges[loop_start + 1] = segments + segment;
187
188 corner_verts[loop_start + 2] = first_vert_ring_start + segment_next;
189 corner_edges[loop_start + 2] = segment_next;
190 }
191
192 const int rings_vert_start = 1;
193 const int rings_edge_start = segments;
194 const int rings_loop_start = segments * 3;
195 for (const int ring : IndexRange(1, rings - 2)) {
196 const int ring_vert_start = rings_vert_start + (ring - 1) * segments;
197 const int ring_edge_start = rings_edge_start + (ring - 1) * segments * 2;
198 const int ring_loop_start = rings_loop_start + (ring - 1) * segments * 4;
199
200 const int next_ring_vert_start = ring_vert_start + segments;
201 const int next_ring_edge_start = ring_edge_start + segments * 2;
202 const int ring_vertical_edge_start = ring_edge_start + segments;
203
204 for (const int segment : IndexRange(segments)) {
205 const int loop_start = ring_loop_start + segment * 4;
206 const int segment_next = segment_next_or_first(segment);
207
208 corner_verts[loop_start + 0] = ring_vert_start + segment;
209 corner_edges[loop_start + 0] = ring_vertical_edge_start + segment;
210
211 corner_verts[loop_start + 1] = next_ring_vert_start + segment;
212 corner_edges[loop_start + 1] = next_ring_edge_start + segment;
213
214 corner_verts[loop_start + 2] = next_ring_vert_start + segment_next;
215 corner_edges[loop_start + 2] = ring_vertical_edge_start + segment_next;
216
217 corner_verts[loop_start + 3] = ring_vert_start + segment_next;
218 corner_edges[loop_start + 3] = ring_edge_start + segment;
219 }
220 }
221
222 /* Add the triangles connected to the bottom vertex. */
223 const int bottom_loop_start = rings_loop_start + segments * (rings - 2) * 4;
224 const int last_edge_ring_start = segments * (rings - 2) * 2 + segments;
225 const int bottom_edge_fan_start = last_edge_ring_start + segments;
226 const int last_vert_index = sphere_vert_total(segments, rings) - 1;
227 const int last_vert_ring_start = last_vert_index - segments;
228 for (const int segment : IndexRange(segments)) {
229 const int loop_start = bottom_loop_start + segment * 3;
230 const int segment_next = segment_next_or_first(segment);
231
232 corner_verts[loop_start + 0] = last_vert_index;
233 corner_edges[loop_start + 0] = bottom_edge_fan_start + segment_next;
234
235 corner_verts[loop_start + 1] = last_vert_ring_start + segment_next;
236 corner_edges[loop_start + 1] = last_edge_ring_start + segment;
237
238 corner_verts[loop_start + 2] = last_vert_ring_start + segment;
239 corner_edges[loop_start + 2] = bottom_edge_fan_start + segment;
240 }
241}
242
244 const float segments,
245 const float rings,
246 const StringRef uv_map_id)
247{
248 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
249
252 MutableSpan<float2> uvs = uv_attribute.span;
253
254 const float dy = 1.0f / rings;
255
256 const float segments_inv = 1.0f / segments;
257
258 for (const int i_segment : IndexRange(segments)) {
259 const int loop_start = i_segment * 3;
260 const float segment = float(i_segment);
261 uvs[loop_start + 0] = float2((segment + 0.5f) * segments_inv, 0.0f);
262 uvs[loop_start + 1] = float2(segment * segments_inv, dy);
263 uvs[loop_start + 2] = float2((segment + 1.0f) * segments_inv, dy);
264 }
265
266 const int rings_loop_start = segments * 3;
267 for (const int i_ring : IndexRange(1, rings - 2)) {
268 const int ring_loop_start = rings_loop_start + (i_ring - 1) * segments * 4;
269 const float ring = float(i_ring);
270 for (const int i_segment : IndexRange(segments)) {
271 const int loop_start = ring_loop_start + i_segment * 4;
272 const float segment = float(i_segment);
273 uvs[loop_start + 0] = float2(segment * segments_inv, ring / rings);
274 uvs[loop_start + 1] = float2(segment * segments_inv, (ring + 1.0f) / rings);
275 uvs[loop_start + 2] = float2((segment + 1.0f) * segments_inv, (ring + 1.0f) / rings);
276 uvs[loop_start + 3] = float2((segment + 1.0f) * segments_inv, ring / rings);
277 }
278 }
279
280 const int bottom_loop_start = rings_loop_start + segments * (rings - 2) * 4;
281 for (const int i_segment : IndexRange(segments)) {
282 const int loop_start = bottom_loop_start + i_segment * 3;
283 const float segment = float(i_segment);
284 uvs[loop_start + 0] = float2((segment + 0.5f) * segments_inv, 1.0f);
285 uvs[loop_start + 1] = float2((segment + 1.0f) * segments_inv, 1.0f - dy);
286 uvs[loop_start + 2] = float2(segment * segments_inv, 1.0f - dy);
287 }
288
289 uv_attribute.finish();
290}
291
293 const int segments,
294 const int rings)
295{
296 const float delta_theta = std::numbers::pi / float(rings);
297 const float sin_equator = std::sin(std::round(0.5f * rings) * delta_theta);
298
299 return calculate_bounds_radial_primitive(0.0f, radius * sin_equator, segments, radius);
300}
301
302Mesh *create_uv_sphere_mesh(const float radius,
303 const int segments,
304 const int rings,
305 const std::optional<StringRef> uv_map_id)
306{
308 sphere_edge_total(segments, rings),
309 sphere_face_total(segments, rings),
310 sphere_corner_total(segments, rings));
311 MutableSpan<float3> positions = mesh->vert_positions_for_write();
312 MutableSpan<int2> edges = mesh->edges_for_write();
313 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
314 MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
315 MutableSpan<int> corner_edges = mesh->corner_edges_for_write();
316 bke::mesh_smooth_set(*mesh, false);
317
319 1024 < segments * rings,
320 [&]() {
321 Vector<float3> vert_normals(mesh->verts_num);
322 calculate_sphere_vertex_data(positions, vert_normals, radius, segments, rings);
323 bke::mesh_vert_normals_assign(*mesh, std::move(vert_normals));
324 },
325 [&]() { calculate_sphere_edge_indices(edges, segments, rings); },
326 [&]() { calculate_sphere_faces(face_offsets, segments); },
327 [&]() { calculate_sphere_corners(corner_verts, corner_edges, segments, rings); },
328 [&]() {
329 if (uv_map_id) {
330 calculate_sphere_uvs(mesh, segments, rings, *uv_map_id);
331 }
332 });
333
334 mesh->tag_loose_verts_none();
335 mesh->tag_loose_edges_none();
336 mesh->tag_overlapping_none();
337 mesh->bounds_set_eager(calculate_bounds_uv_sphere(radius, segments, rings));
338
339 return mesh;
340}
341
342} // namespace blender::geometry
#define BLI_NOINLINE
ccl_device_inline float delta_phi(const int p, const float gamma_o, const float gamma_t)
ccl_device_inline float sin_theta(const float3 w)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
constexpr IndexRange()=default
constexpr MutableSpan take_back(const int64_t n) const
Definition BLI_span.hh:646
constexpr MutableSpan drop_back(const int64_t n) const
Definition BLI_span.hh:624
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr MutableSpan drop_front(const int64_t n) const
Definition BLI_span.hh:613
constexpr T & first() const
Definition BLI_span.hh:685
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void mesh_vert_normals_assign(Mesh &mesh, Span< float3 > vert_normals)
static int sphere_corner_total(const int segments, const int rings)
static BLI_NOINLINE void calculate_sphere_faces(MutableSpan< int > face_offsets, const int segments)
static int sphere_face_total(const int segments, const int rings)
Mesh * create_uv_sphere_mesh(float radius, int segments, int rings, std::optional< StringRef > uv_map_id)
static int sphere_vert_total(const int segments, const int rings)
static BLI_NOINLINE void calculate_sphere_corners(MutableSpan< int > corner_verts, MutableSpan< int > corner_edges, const int segments, const int rings)
static BLI_NOINLINE void calculate_sphere_uvs(Mesh *mesh, const float segments, const float rings, const StringRef uv_map_id)
static BLI_NOINLINE void calculate_sphere_vertex_data(MutableSpan< float3 > positions, MutableSpan< float3 > vert_normals, const float radius, const int segments, const int rings)
Bounds< float3 > calculate_bounds_radial_primitive(float radius_top, float radius_bottom, int segments, float height)
static int sphere_edge_total(const int segments, const int rings)
static BLI_NOINLINE void calculate_sphere_edge_indices(MutableSpan< int2 > edges, const int segments, const int rings)
static Bounds< float3 > calculate_bounds_uv_sphere(const float radius, const int segments, const int rings)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
void parallel_invoke(Functions &&...functions)
Definition BLI_task.hh:197
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
VecBase< float, 3 > float3
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float