Blender V5.3
annotations_geom.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10#include <cstdlib>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_listbase.hh"
16#include "BLI_math_vector_c.hh"
18#include "BLI_polyfill_2d.hh"
19#include "BLI_span.hh"
20
22#include "DNA_mesh_types.h"
23#include "DNA_meshdata_types.h"
24
25#include "BKE_annotations.h"
27
28namespace blender {
29
30/* -------------------------------------------------------------------- */
33
34static void stroke_2d_flat(const bGPDspoint *points,
35 int totpoints,
36 float (*points2d)[2],
37 int *r_direction)
38{
39 BLI_assert(totpoints >= 2);
40
41 const bGPDspoint *pt0 = &points[0];
42 const bGPDspoint *pt1 = &points[1];
43 const bGPDspoint *pt3 = &points[int(totpoints * 0.75)];
44
45 float locx[3];
46 float locy[3];
47 float loc3[3];
48 float normal[3];
49
50 /* local X axis (p0 -> p1) */
51 sub_v3_v3v3(locx, &pt1->x, &pt0->x);
52
53 /* point vector at 3/4 */
54 float v3[3];
55 if (totpoints == 2) {
56 mul_v3_v3fl(v3, &pt3->x, 0.001f);
57 }
58 else {
59 copy_v3_v3(v3, &pt3->x);
60 }
61
62 sub_v3_v3v3(loc3, v3, &pt0->x);
63
64 /* vector orthogonal to polygon plane */
65 cross_v3_v3v3(normal, locx, loc3);
66
67 /* local Y axis (cross to normal/x axis) */
68 cross_v3_v3v3(locy, normal, locx);
69
70 /* Normalize vectors */
71 normalize_v3(locx);
72 normalize_v3(locy);
73
74 /* Calculate last point first. */
75 const bGPDspoint *pt_last = &points[totpoints - 1];
76 float tmp[3];
77 sub_v3_v3v3(tmp, &pt_last->x, &pt0->x);
78
79 points2d[totpoints - 1][0] = dot_v3v3(tmp, locx);
80 points2d[totpoints - 1][1] = dot_v3v3(tmp, locy);
81
82 /* Calculate the scalar cross product of the 2d points. */
83 float cross = 0.0f;
84 float *co_curr;
85 float *co_prev = reinterpret_cast<float *>(&points2d[totpoints - 1]);
86
87 /* Get all points in local space */
88 for (int i = 0; i < totpoints - 1; i++) {
89 const bGPDspoint *pt = &points[i];
90 float loc[3];
91
92 /* Get local space using first point as origin */
93 sub_v3_v3v3(loc, &pt->x, &pt0->x);
94
95 points2d[i][0] = dot_v3v3(loc, locx);
96 points2d[i][1] = dot_v3v3(loc, locy);
97
98 /* Calculate cross product. */
99 co_curr = (&points2d[i][0]);
100 cross += (co_curr[0] - co_prev[0]) * (co_curr[1] + co_prev[1]);
101 co_prev = (&points2d[i][0]);
102 }
103
104 /* Concave (-1), Convex (1) */
105 *r_direction = (cross >= 0.0f) ? 1 : -1;
106}
107
108/* Calc texture coordinates using flat projected points. */
109static void gpencil_calc_stroke_fill_uv(const float (*points2d)[2],
110 bGPDstroke *gps,
111 const float minv[2],
112 const float maxv[2],
113 float (*r_uv)[2])
114{
115 const float s = sin(gps->uv_rotation);
116 const float c = cos(gps->uv_rotation);
117
118 /* Calc center for rotation. */
119 const float center[2] = {0.5f, 0.5f};
120 float d[2];
121 d[0] = maxv[0] - minv[0];
122 d[1] = maxv[1] - minv[1];
123 for (int i = 0; i < gps->totpoints; i++) {
124 r_uv[i][0] = (points2d[i][0] - minv[0]) / d[0];
125 r_uv[i][1] = (points2d[i][1] - minv[1]) / d[1];
126
127 /* Apply translation. */
128 add_v2_v2(r_uv[i], gps->uv_translation);
129
130 /* Apply Rotation. */
131 r_uv[i][0] -= center[0];
132 r_uv[i][1] -= center[1];
133
134 float x = r_uv[i][0] * c - r_uv[i][1] * s;
135 float y = r_uv[i][0] * s + r_uv[i][1] * c;
136
137 r_uv[i][0] = x + center[0];
138 r_uv[i][1] = y + center[1];
139
140 /* Apply scale. */
141 if (gps->uv_scale != 0.0f) {
142 mul_v2_fl(r_uv[i], 1.0f / gps->uv_scale);
143 }
144 }
145}
146
148
149/* -------------------------------------------------------------------- */
152
154{
155 BLI_assert(gps->totpoints >= 3);
156
157 /* allocate memory for temporary areas */
158 gps->tot_triangles = gps->totpoints - 2;
159 uint(*tmp_triangles)[3] = MEM_new_array_uninitialized<uint[3]>(size_t(gps->tot_triangles),
160 "GP Stroke temp triangulation");
161 float (*points2d)[2] = MEM_new_array_uninitialized<float[2]>(size_t(gps->totpoints),
162 "GP Stroke temp 2d points");
164 "GP Stroke temp 2d uv data");
165
166 int direction = 0;
167
168 /* convert to 2d and triangulate */
169 stroke_2d_flat(gps->points, gps->totpoints, points2d, &direction);
170 BLI_polyfill_calc(points2d, uint(gps->totpoints), direction, tmp_triangles);
171
172 /* calc texture coordinates automatically */
173 float minv[2];
174 float maxv[2];
175 /* first needs bounding box data */
176 ARRAY_SET_ITEMS(minv, -1.0f, -1.0f);
177 ARRAY_SET_ITEMS(maxv, 1.0f, 1.0f);
178
179 /* calc uv data */
180 gpencil_calc_stroke_fill_uv(points2d, gps, minv, maxv, uv);
181
182 /* Save triangulation data. */
183 if (gps->tot_triangles > 0) {
184 MEM_SAFE_DELETE(gps->triangles);
185 gps->triangles = MEM_new_array<bGPDtriangle>(gps->tot_triangles, "GP Stroke triangulation");
186
187 for (int i = 0; i < gps->tot_triangles; i++) {
188 memcpy(gps->triangles[i].verts, tmp_triangles[i], sizeof(uint[3]));
189 }
190
191 /* Copy UVs to bGPDspoint. */
192 for (int i = 0; i < gps->totpoints; i++) {
193 copy_v2_v2(gps->points[i].uv_fill, uv[i]);
194 }
195 }
196 else {
197 /* No triangles needed - Free anything allocated previously */
198 if (gps->triangles) {
199 MEM_delete(gps->triangles);
200 }
201
202 gps->triangles = nullptr;
203 }
204
205 /* clear memory */
206 MEM_SAFE_DELETE(tmp_triangles);
207 MEM_SAFE_DELETE(points2d);
208 MEM_SAFE_DELETE(uv);
209}
210
212{
213 if (gps == nullptr || gps->totpoints == 0) {
214 return;
215 }
216
217 bGPDspoint *pt = gps->points;
218 float totlen = 0.0f;
219 pt[0].uv_fac = totlen;
220 for (int i = 1; i < gps->totpoints; i++) {
221 totlen += len_v3v3(&pt[i - 1].x, &pt[i].x);
222 pt[i].uv_fac = totlen;
223 }
224}
225
227{
228 if (gps == nullptr) {
229 return;
230 }
231
232 if (gps->totpoints > 2) {
234 }
235 else {
236 gps->tot_triangles = 0;
237 MEM_SAFE_DELETE(gps->triangles);
238 }
239
240 /* calc uv data along the stroke */
241 stroke_uv_update(gps);
242}
243
245
246} // namespace blender
Read Blender memory allocation and freeing.
#define ARRAY_SET_ITEMS(...)
#define sin
#define cos
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE void mul_v2_fl(float r[2], float f)
MINLINE void copy_v2_v2(float r[2], const float a[2])
void BLI_polyfill_calc(const float(*coords)[2], unsigned int coords_num, int coords_sign, unsigned int(*r_tris)[3])
void BKE_annotation_stroke_geometry_update(struct bGPdata *gpd, struct bGPDstroke *gps)
static void stroke_fill_triangulate(bGPDstroke *gps)
static void gpencil_calc_stroke_fill_uv(const float(*points2d)[2], bGPDstroke *gps, const float minv[2], const float maxv[2], float(*r_uv)[2])
MINLINE void add_v2_v2(float r[2], const float a[2])
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
static void stroke_uv_update(bGPDstroke *gps)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE float normalize_v3(float n[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
static void stroke_2d_flat(const bGPDspoint *points, int totpoints, float(*points2d)[2], int *r_direction)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float