Blender V5.3
mesh_primitive_grid.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 "BKE_attribute.hh"
10#include "BKE_mesh.hh"
11
13
14namespace blender::geometry {
15
16static void calculate_uvs(Mesh *mesh,
17 const Span<float3> positions,
18 const Span<int> corner_verts,
19 const float size_x,
20 const float size_y,
21 const StringRef uv_map_id)
22{
23 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
25 uv_map_id, bke::AttrDomain::Corner);
26
27 const float dx = (size_x == 0.0f) ? 0.0f : 1.0f / size_x;
28 const float dy = (size_y == 0.0f) ? 0.0f : 1.0f / size_y;
30 uv_attribute.span.size_in_bytes() + positions.size_in_bytes() + corner_verts.size_in_bytes(),
31 [&]() {
32 threading::parallel_for(corner_verts.index_range(), 1024, [&](IndexRange range) {
33 for (const int i : range) {
34 const float3 &co = positions[corner_verts[i]];
35 uv_attribute.span[i].x = (co.x + size_x * 0.5f) * dx;
36 uv_attribute.span[i].y = (co.y + size_y * 0.5f) * dy;
37 }
38 });
39 });
40
41 uv_attribute.finish();
42}
43
44Mesh *create_grid_mesh(const int verts_x,
45 const int verts_y,
46 const float size_x,
47 const float size_y,
48 const std::optional<StringRef> uv_map_id)
49{
50 BLI_assert(verts_x > 0 && verts_y > 0);
51 const int edges_x = verts_x - 1;
52 const int edges_y = verts_y - 1;
53 Mesh *mesh = BKE_mesh_new_nomain(verts_x * verts_y,
54 edges_x * verts_y + edges_y * verts_x,
55 edges_x * edges_y,
56 edges_x * edges_y * 4);
57 MutableSpan<float3> positions = mesh->vert_positions_for_write();
58 MutableSpan<int2> edges = mesh->edges_for_write();
59 MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
60 MutableSpan<int> corner_edges = mesh->corner_edges_for_write();
62
63 offset_indices::fill_constant_group_size(4, 0, mesh->face_offsets_for_write());
64
65 {
66 const float dx = edges_x == 0 ? 0.0f : size_x / edges_x;
67 const float dy = edges_y == 0 ? 0.0f : size_y / edges_y;
68 const float x_shift = edges_x / 2.0f;
69 const float y_shift = edges_y / 2.0f;
71 threading::parallel_for(IndexRange(verts_x), 512, [&](IndexRange x_range) {
72 for (const int x : x_range) {
73 const int y_offset = x * verts_y;
74 threading::parallel_for(IndexRange(verts_y), 512, [&](IndexRange y_range) {
75 for (const int y : y_range) {
76 const int vert = y_offset + y;
77 positions[vert].x = (x - x_shift) * dx;
78 positions[vert].y = (y - y_shift) * dy;
79 positions[vert].z = 0.0f;
80 }
81 });
82 }
83 });
84 });
85 }
86
87 const int y_edges_start = 0;
88 const int x_edges_start = verts_x * edges_y;
89
90 /* Build the horizontal edges in the X direction. */
91 threading::memory_bandwidth_bound_task(edges.size_in_bytes(), [&]() {
92 threading::parallel_for(IndexRange(verts_x), 512, [&](IndexRange x_range) {
93 for (const int x : x_range) {
94 const int y_vert_offset = x * verts_y;
95 const int y_edge_offset = y_edges_start + x * edges_y;
96 threading::parallel_for(IndexRange(edges_y), 512, [&](IndexRange y_range) {
97 for (const int y : y_range) {
98 const int vert = y_vert_offset + y;
99 edges[y_edge_offset + y] = int2(vert, vert + 1);
100 }
101 });
102 }
103 });
104 });
105
106 /* Build the vertical edges in the Y direction. */
107 threading::memory_bandwidth_bound_task(edges.size_in_bytes(), [&]() {
108 threading::parallel_for(IndexRange(verts_y), 512, [&](IndexRange y_range) {
109 for (const int y : y_range) {
110 const int x_edge_offset = x_edges_start + y * edges_x;
111 threading::parallel_for(IndexRange(edges_x), 512, [&](IndexRange x_range) {
112 for (const int x : x_range) {
113 const int vert = x * verts_y + y;
114 edges[x_edge_offset + x] = int2(vert, vert + verts_y);
115 }
116 });
117 }
118 });
119 });
120
121 threading::memory_bandwidth_bound_task(
122 corner_edges.size_in_bytes() + corner_verts.size_in_bytes(), [&]() {
123 threading::parallel_for(IndexRange(edges_x), 512, [&](IndexRange x_range) {
124 for (const int x : x_range) {
125 const int y_offset = x * edges_y;
126 threading::parallel_for(IndexRange(edges_y), 512, [&](IndexRange y_range) {
127 for (const int y : y_range) {
128 const int face = y_offset + y;
129 const int corner = face * 4;
130 const int vert = x * verts_y + y;
131
132 corner_verts[corner] = vert;
133 corner_edges[corner] = x_edges_start + edges_x * y + x;
134
135 corner_verts[corner + 1] = vert + verts_y;
136 corner_edges[corner + 1] = y_edges_start + edges_y * (x + 1) + y;
137
138 corner_verts[corner + 2] = vert + verts_y + 1;
139 corner_edges[corner + 2] = x_edges_start + edges_x * (y + 1) + x;
140
141 corner_verts[corner + 3] = vert + 1;
142 corner_edges[corner + 3] = y_edges_start + edges_y * x + y;
143 }
144 });
145 }
146 });
147 });
148
149 if (uv_map_id && mesh->faces_num != 0) {
150 calculate_uvs(mesh, positions, corner_verts, size_x, size_y, *uv_map_id);
151 }
152
153 if (verts_x > 1 || verts_y > 1) {
154 mesh->tag_loose_verts_none();
155 }
156 if (verts_x > 1 && verts_y > 1) {
157 mesh->tag_loose_edges_none();
158 }
159 mesh->tag_overlapping_none();
160
161 const float3 bounds = float3(size_x * 0.5f, size_y * 0.5f, 0.0f);
162 mesh->bounds_set_eager({-bounds, bounds});
163
164 return mesh;
165}
166
167} // namespace blender::geometry
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:507
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:272
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)
static void calculate_uvs(Mesh *mesh, const Span< float3 > positions, const Span< int > corner_verts, const float size_x, const float size_y, const StringRef uv_map_id)
Mesh * create_grid_mesh(int verts_x, int verts_y, float size_x, float size_y, std::optional< StringRef > uv_map_id)
static void calculate_uvs(const CuboidConfig &config, Mesh *mesh, const StringRef uv_id)
void fill_constant_group_size(int size, int start_offset, MutableSpan< int > offsets)
void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched, const Function &function)
Definition BLI_task.hh:251
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
VecBase< float, 2 > float2
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)