Blender V5.3
curve_bevel.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
11
12#include <cstring>
13
14#include "BLI_listbase.hh"
15#include "BLI_math_base_c.hh" /* Needed with MSVC for M_PI & M_PI_2. */
16
17#include "MEM_guardedalloc.h"
18
19#include "DNA_curve_types.h"
21#include "DNA_object_types.h"
22
23#include "BKE_curve.hh"
24#include "BKE_displist.h"
25#include "BKE_object_types.hh"
26
27namespace blender {
28
35
37{
38 if (!(curve->flag & (CU_FRONT | CU_BACK))) {
39 return FULL;
40 }
41 if ((curve->flag & CU_FRONT) && (curve->flag & CU_BACK)) {
42 return HALF;
43 }
44
45 return (curve->flag & CU_FRONT) ? FRONT : BACK;
46}
47
48static void bevel_quarter_fill(const Curve *curve,
49 float *quarter_coords_x,
50 float *quarter_coords_y)
51{
52 if (curve->bevel_mode == CU_BEV_MODE_ROUND) {
53 float angle = 0.0f;
54 const float dangle = float(M_PI_2) / (curve->bevresol + 1);
55 for (int i = 0; i < curve->bevresol + 1; i++) {
56 quarter_coords_x[i] = float(cosf(angle) * (curve->bevel_radius));
57 quarter_coords_y[i] = float(sinf(angle) * (curve->bevel_radius));
58 angle += dangle;
59 }
60 }
61 else {
62 /* The curve profile evaluation should be done when the resolution is set. */
63 BLI_assert(curve->bevel_profile->segments != nullptr);
64 BLI_assert(curve->bevel_profile->segments_len == curve->bevresol + 1);
65
66 /* If there aren't enough samples, the curveprofile won't
67 * sample the start vertex, so set it manually instead. */
68 quarter_coords_x[0] = curve->bevel_radius;
69 quarter_coords_y[0] = 0.0f;
70 for (int i = 1; i < curve->bevresol + 1; i++) {
71 quarter_coords_x[i] = float(curve->bevel_profile->segments[i].x * (curve->bevel_radius));
72 quarter_coords_y[i] = float(curve->bevel_profile->segments[i].y * (curve->bevel_radius));
73 }
74 }
75}
76
78 ListBaseT<DispList> *disp,
79 const bool use_extrude,
80 const CurveBevelFillType fill_type)
81{
82 DispList *dl = MEM_new_zeroed<DispList>(__func__);
83
84 /* Calculate the profile of the bevel once to reuse it for each quarter. We will need
85 * to flip around the indices for every other section in order to build around the circle
86 * in a consistent direction.
87 *
88 * These should be small enough for stack allocations because the current limit
89 * for #Curve.bevresol is 32. */
90 float *quarter_coords_x = static_cast<float *>(alloca(sizeof(float) * (cu->bevresol + 1)));
91 float *quarter_coords_y = static_cast<float *>(alloca(sizeof(float) * (cu->bevresol + 1)));
92 bevel_quarter_fill(cu, quarter_coords_x, quarter_coords_y);
93
94 int nr;
95 if (fill_type == FULL) {
96 /* The full loop. */
97 nr = 4 * cu->bevresol + (use_extrude ? 6 : 4);
99 }
100 else if (fill_type == HALF) {
101 /* Half the loop. */
102 nr = 2 * (cu->bevresol + 1) + (use_extrude ? 2 : 1);
104 }
105 else {
106 /* One quarter of the loop (just front or back). */
107 nr = use_extrude ? cu->bevresol + 3 : cu->bevresol + 2;
108 dl->flag = (fill_type == FRONT) ? DL_FRONT_CURVE : DL_BACK_CURVE;
109 }
110
111 dl->verts = MEM_new_array_uninitialized<float>(3 * size_t(nr), __func__);
112 BLI_addtail(disp, dl);
113 /* Use a different type depending on whether the loop is complete or not. */
114 dl->type = (fill_type == FULL) ? DL_POLY : DL_SEGM;
115 dl->parts = 1;
116 dl->nr = nr;
117
118 float *fp = dl->verts;
119
120 /* Build the back section. */
121 if (ELEM(fill_type, BACK, HALF, FULL)) {
122 /* Add the bottom vertex. */
123 fp[0] = 0.0f;
124 fp[1] = 0.0f;
125 fp[2] = -cu->extrude - cu->bevel_radius;
126 fp += 3;
127
128 for (int i = cu->bevresol; i >= 0; i--) {
129 fp[0] = 0.0f;
130 fp[1] = quarter_coords_x[i];
131 fp[2] = -quarter_coords_y[i] - cu->extrude;
132 fp += 3;
133 }
134 }
135
136 /* Add the extrusion if we're only building either the back or the front. */
137 if (use_extrude && ELEM(fill_type, FRONT, BACK)) {
138 fp[0] = 0.0f;
139 fp[1] = cu->bevel_radius;
140 fp[2] = (fill_type == FRONT) ? -cu->extrude : cu->extrude;
141 fp += 3;
142 }
143
144 /* Build the front section. */
145 if (ELEM(fill_type, FRONT, HALF, FULL)) {
146 /* Don't duplicate the last back vertex. */
147 const int front_start = (!use_extrude && ELEM(fill_type, HALF, FULL)) ? 1 : 0;
148 for (int i = front_start; i < cu->bevresol + 1; i++) {
149 fp[0] = 0.0f;
150 fp[1] = quarter_coords_x[i];
151 fp[2] = quarter_coords_y[i] + cu->extrude;
152 fp += 3;
153 }
154 /* Add the top vertex. */
155 fp[0] = 0.0f;
156 fp[1] = 0.0f;
157 fp[2] = cu->extrude + cu->bevel_radius;
158 fp += 3;
159 }
160
161 /* Build the other half only if we're building the full loop. */
162 if (fill_type == FULL) {
163 for (int i = cu->bevresol; i > 0; i--) {
164 fp[0] = 0.0f;
165 fp[1] = -quarter_coords_x[i];
166 fp[2] = quarter_coords_y[i] + cu->extrude;
167 fp += 3;
168 }
169
170 if (use_extrude) {
171 /* Add the extrusion. */
172 fp[0] = 0.0f;
173 fp[1] = -cu->bevel_radius;
174 fp[2] = cu->extrude;
175 fp += 3;
176 }
177
178 for (int i = 0; i < cu->bevresol + 1; i++) {
179 fp[0] = 0.0f;
180 fp[1] = -quarter_coords_x[i];
181 fp[2] = -quarter_coords_y[i] - cu->extrude;
182 fp += 3;
183 }
184 }
185}
186
187static void curve_bevel_make_full_circle(const Curve *cu, ListBaseT<DispList> *disp)
188{
189 const int nr = 4 + 2 * cu->bevresol;
190
191 DispList *dl = MEM_new_zeroed<DispList>(__func__);
192 dl->verts = MEM_new_array_uninitialized<float>(3 * size_t(nr), __func__);
193 BLI_addtail(disp, dl);
194 dl->type = DL_POLY;
195 dl->parts = 1;
196 dl->flag = DL_BACK_CURVE;
197 dl->nr = nr;
198
199 float *fp = dl->verts;
200 const float dangle = (2.0f * float(M_PI) / (nr));
201 float angle = -(nr - 1) * dangle;
202
203 for (int i = 0; i < nr; i++) {
204 fp[0] = 0.0;
205 fp[1] = (cosf(angle) * (cu->bevel_radius));
206 fp[2] = (sinf(angle) * (cu->bevel_radius)) - cu->extrude;
207 angle += dangle;
208 fp += 3;
209 }
210}
211
212static void curve_bevel_make_only_extrude(const Curve *cu, ListBaseT<DispList> *disp)
213{
214 DispList *dl = MEM_new_zeroed<DispList>(__func__);
215 dl->verts = MEM_new_array_uninitialized<float>(3 * 2, __func__);
216 BLI_addtail(disp, dl);
217 dl->type = DL_SEGM;
218 dl->parts = 1;
220 dl->nr = 2;
221
222 float *fp = dl->verts;
223 fp[0] = fp[1] = 0.0;
224 fp[2] = -cu->extrude;
225 fp[3] = fp[4] = 0.0;
226 fp[5] = cu->extrude;
227}
228
229static void curve_bevel_make_from_object(const Curve *cu, ListBaseT<DispList> *disp)
230{
231 if (cu->bevobj == nullptr) {
232 return;
233 }
234 if (cu->bevobj->type != OB_CURVES_LEGACY) {
235 return;
236 }
237
238 Curve *bevcu = id_cast<Curve *>(cu->bevobj->data);
239 if (bevcu->extrude == 0.0f && bevcu->bevel_radius == 0.0f) {
240 ListBaseT<DispList> bevdisp = {nullptr, nullptr};
241 float facx = cu->bevobj->scale[0];
242 float facy = cu->bevobj->scale[1];
243
244 DispList *dl;
245 if (cu->bevobj->runtime->curve_cache) {
246 dl = cu->bevobj->runtime->curve_cache->disp.first();
247 }
248 else {
249 BLI_assert(cu->bevobj->runtime->curve_cache != nullptr);
250 dl = nullptr;
251 }
252
253 while (dl) {
254 if (ELEM(dl->type, DL_POLY, DL_SEGM)) {
255 DispList *dlnew = MEM_new_uninitialized<DispList>(__func__);
256 *dlnew = *dl;
257 dlnew->verts = MEM_new_array_uninitialized<float>(3 * size_t(dl->parts) * size_t(dl->nr),
258 __func__);
259 memcpy(dlnew->verts, dl->verts, sizeof(float[3]) * dl->parts * dl->nr);
260
261 if (dlnew->type == DL_SEGM) {
262 dlnew->flag |= (DL_FRONT_CURVE | DL_BACK_CURVE);
263 }
264
265 BLI_addtail(disp, dlnew);
266 float *fp = dlnew->verts;
267 int nr = dlnew->parts * dlnew->nr;
268 while (nr--) {
269 fp[2] = fp[1] * facy;
270 fp[1] = -fp[0] * facx;
271 fp[0] = 0.0;
272 fp += 3;
273 }
274 }
275 dl = dl->next;
276 }
277
278 BKE_displist_free(&bevdisp);
279 }
280}
281
282ListBaseT<DispList> BKE_curve_bevel_make(const Curve *curve)
283{
284 ListBaseT<DispList> bevel_shape = {nullptr, nullptr};
285
286 if (curve->bevel_mode == CU_BEV_MODE_OBJECT) {
287 if (curve->bevobj != nullptr) {
288 curve_bevel_make_from_object(curve, &bevel_shape);
289 }
290 }
291 else {
292 const bool use_extrude = curve->extrude != 0.0f;
293 const bool use_bevel = curve->bevel_radius != 0.0f;
294 /* Pass. */
295 if (use_extrude && !use_bevel) {
296 curve_bevel_make_only_extrude(curve, &bevel_shape);
297 }
298 else if (use_extrude || use_bevel) {
300
301 if (!use_extrude && fill_type == FULL && curve->bevel_mode == CU_BEV_MODE_ROUND) {
302 curve_bevel_make_full_circle(curve, &bevel_shape);
303 }
304 else {
305 /* The general case for nonzero extrusion or an incomplete loop. */
306 curve_bevel_make_extrude_and_fill(curve, &bevel_shape, use_extrude, fill_type);
307 }
308 }
309 }
310
311 return bevel_shape;
312}
313
314} // namespace blender
display list (or rather multi purpose list) stuff.
#define M_PI_2
#define M_PI
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define ELEM(...)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void bevel_quarter_fill(const Curve *curve, float *quarter_coords_x, float *quarter_coords_y)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_displist_free(ListBaseT< DispList > *lb)
Definition displist.cc:68
static CurveBevelFillType curve_bevel_get_fill_type(const Curve *curve)
static void curve_bevel_make_only_extrude(const Curve *cu, ListBaseT< DispList > *disp)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
@ DL_BACK_CURVE
@ DL_FRONT_CURVE
static void curve_bevel_make_from_object(const Curve *cu, ListBaseT< DispList > *disp)
static void curve_bevel_make_full_circle(const Curve *cu, ListBaseT< DispList > *disp)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static void curve_bevel_make_extrude_and_fill(const Curve *cu, ListBaseT< DispList > *disp, const bool use_extrude, const CurveBevelFillType fill_type)
ListBaseT< DispList > BKE_curve_bevel_make(const Curve *curve)
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< DispList > disp
Definition BKE_curve.hh:44
CurveProfilePoint * segments
eCurveBevelMode bevel_mode
struct CurveProfile * bevel_profile
struct Object * bevobj
struct DispList * next
bke::ObjectRuntime * runtime