1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 #pragma once
20 
21 /** \file
22  * \ingroup bke
23  */
24 
25 #include "DNA_scene_types.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 struct BMEditMesh;
32 struct BezTriple;
33 struct Curve;
34 struct Depsgraph;
35 struct GHash;
36 struct ListBase;
37 struct MDeformVert;
38 struct Main;
39 struct Nurb;
40 struct Object;
41 struct Path;
42 struct TextBox;
43 struct rctf;
44 
45 typedef int eBezTriple_Flag__Alias;
46 
47 typedef struct CurveCache {
48   ListBase disp;
49   ListBase bev;
50   ListBase deformed_nurbs;
51   struct Path *path;
52 } CurveCache;
53 
54 /* Definitions needed for shape keys */
55 typedef struct CVKeyIndex {
56   void *orig_cv;
57   int key_index, nu_index, pt_index, vertex_index;
58   bool switched;
59 } CVKeyIndex;
60 
61 #define KNOTSU(nu) \
62   ((nu)->orderu + (nu)->pntsu + (((nu)->flagu & CU_NURB_CYCLIC) ? ((nu)->orderu - 1) : 0))
63 #define KNOTSV(nu) \
64   ((nu)->orderv + (nu)->pntsv + (((nu)->flagv & CU_NURB_CYCLIC) ? ((nu)->orderv - 1) : 0))
65 
66 /* Non cyclic nurbs have 1 less segment */
67 #define SEGMENTSU(nu) (((nu)->flagu & CU_NURB_CYCLIC) ? (nu)->pntsu : (nu)->pntsu - 1)
68 #define SEGMENTSV(nu) (((nu)->flagv & CU_NURB_CYCLIC) ? (nu)->pntsv : (nu)->pntsv - 1)
69 
70 #define CU_DO_TILT(cu, nu) ((((nu)->flag & CU_2D) && ((cu)->flag & CU_3D) == 0) ? 0 : 1)
71 #define CU_DO_RADIUS(cu, nu) \
72   ((CU_DO_TILT(cu, nu) || ((cu)->flag & CU_PATH_RADIUS) || (cu)->bevobj || (cu)->ext1 != 0.0f || \
73     (cu)->ext2 != 0.0f) ? \
74        1 : \
75        0)
76 
77 /* not 3d and not unfilled */
78 #define CU_DO_2DFILL(cu) \
79   ((((cu)->flag & CU_3D) == 0) && (((cu)->flag & (CU_FRONT | CU_BACK)) != 0))
80 
81 /* ** Curve ** */
82 void BKE_curve_editfont_free(struct Curve *cu);
83 void BKE_curve_init(struct Curve *cu, const short curve_type);
84 struct Curve *BKE_curve_add(struct Main *bmain, const char *name, int type);
85 short BKE_curve_type_get(const struct Curve *cu);
86 void BKE_curve_type_test(struct Object *ob);
87 void BKE_curve_curve_dimension_update(struct Curve *cu);
88 
89 struct BoundBox *BKE_curve_boundbox_get(struct Object *ob);
90 
91 void BKE_curve_texspace_calc(struct Curve *cu);
92 void BKE_curve_texspace_ensure(struct Curve *cu);
93 void BKE_curve_texspace_get(struct Curve *cu, float r_loc[3], float r_size[3]);
94 
95 bool BKE_curve_minmax(struct Curve *cu, bool use_radius, float min[3], float max[3]);
96 bool BKE_curve_center_median(struct Curve *cu, float cent[3]);
97 bool BKE_curve_center_bounds(struct Curve *cu, float cent[3]);
98 void BKE_curve_transform_ex(struct Curve *cu,
99                             const float mat[4][4],
100                             const bool do_keys,
101                             const bool do_props,
102                             const float unit_scale);
103 void BKE_curve_transform(struct Curve *cu,
104                          const float mat[4][4],
105                          const bool do_keys,
106                          const bool do_props);
107 void BKE_curve_translate(struct Curve *cu, const float offset[3], const bool do_keys);
108 void BKE_curve_material_index_remove(struct Curve *cu, int index);
109 bool BKE_curve_material_index_used(struct Curve *cu, int index);
110 void BKE_curve_material_index_clear(struct Curve *cu);
111 bool BKE_curve_material_index_validate(struct Curve *cu);
112 void BKE_curve_material_remap(struct Curve *cu, const unsigned int *remap, unsigned int remap_len);
113 
114 void BKE_curve_smooth_flag_set(struct Curve *cu, const bool use_smooth);
115 
116 ListBase *BKE_curve_nurbs_get(struct Curve *cu);
117 
118 int BKE_curve_nurb_vert_index_get(const struct Nurb *nu, const void *vert);
119 void BKE_curve_nurb_active_set(struct Curve *cu, const struct Nurb *nu);
120 struct Nurb *BKE_curve_nurb_active_get(struct Curve *cu);
121 void *BKE_curve_vert_active_get(struct Curve *cu);
122 void BKE_curve_nurb_vert_active_set(struct Curve *cu, const struct Nurb *nu, const void *vert);
123 bool BKE_curve_nurb_vert_active_get(struct Curve *cu, struct Nurb **r_nu, void **r_vert);
124 void BKE_curve_nurb_vert_active_validate(struct Curve *cu);
125 
126 float (*BKE_curve_nurbs_vert_coords_alloc(struct ListBase *lb, int *r_vert_len))[3];
127 void BKE_curve_nurbs_vert_coords_get(struct ListBase *lb, float (*vert_coords)[3], int vert_len);
128 
129 void BKE_curve_nurbs_vert_coords_apply_with_mat4(struct ListBase *lb,
130                                                  const float (*vert_coords)[3],
131                                                  const float mat[4][4],
132                                                  const bool constrain_2d);
133 
134 void BKE_curve_nurbs_vert_coords_apply(struct ListBase *lb,
135                                        const float (*vert_coords)[3],
136                                        const bool constrain_2d);
137 
138 float (*BKE_curve_nurbs_key_vert_coords_alloc(struct ListBase *lb,
139                                               float *key,
140                                               int *r_vert_len))[3];
141 void BKE_curve_nurbs_key_vert_tilts_apply(struct ListBase *lb, const float *key);
142 
143 void BKE_curve_editNurb_keyIndex_delCV(struct GHash *keyindex, const void *cv);
144 void BKE_curve_editNurb_keyIndex_free(struct GHash **keyindex);
145 void BKE_curve_editNurb_free(struct Curve *cu);
146 struct ListBase *BKE_curve_editNurbs_get(struct Curve *cu);
147 
148 void BKE_curve_bevelList_free(struct ListBase *bev);
149 void BKE_curve_bevelList_make(struct Object *ob, struct ListBase *nurbs, bool for_render);
150 void BKE_curve_bevel_make(struct Object *ob, struct ListBase *disp);
151 
152 void BKE_curve_forward_diff_bezier(
153     float q0, float q1, float q2, float q3, float *p, int it, int stride);
154 void BKE_curve_forward_diff_tangent_bezier(
155     float q0, float q1, float q2, float q3, float *p, int it, int stride);
156 
157 void BKE_curve_rect_from_textbox(const struct Curve *cu,
158                                  const struct TextBox *tb,
159                                  struct rctf *r_rect);
160 
161 void BKE_curve_correct_bezpart(const float v1[2], float v2[2], float v3[2], const float v4[2]);
162 
163 /* ** Nurbs ** */
164 
165 bool BKE_nurbList_index_get_co(struct ListBase *editnurb, const int index, float r_co[3]);
166 
167 int BKE_nurbList_verts_count(struct ListBase *nurb);
168 int BKE_nurbList_verts_count_without_handles(struct ListBase *nurb);
169 
170 void BKE_nurbList_free(struct ListBase *lb);
171 void BKE_nurbList_duplicate(struct ListBase *lb1, const struct ListBase *lb2);
172 void BKE_nurbList_handles_set(struct ListBase *editnurb, const char code);
173 void BKE_nurbList_handles_recalculate(struct ListBase *editnurb,
174                                       const bool calc_length,
175                                       const uint8_t flag);
176 
177 void BKE_nurbList_handles_autocalc(ListBase *editnurb, uint8_t flag);
178 void BKE_nurbList_flag_set(ListBase *editnurb, uint8_t flag, bool set);
179 bool BKE_nurbList_flag_set_from_flag(ListBase *editnurb, uint8_t from_flag, uint8_t flag);
180 
181 void BKE_nurb_free(struct Nurb *nu);
182 struct Nurb *BKE_nurb_duplicate(const struct Nurb *nu);
183 struct Nurb *BKE_nurb_copy(struct Nurb *src, int pntsu, int pntsv);
184 
185 void BKE_nurb_test_2d(struct Nurb *nu);
186 void BKE_nurb_minmax(struct Nurb *nu, bool use_radius, float min[3], float max[3]);
187 float BKE_nurb_calc_length(const struct Nurb *nu, int resolution);
188 
189 void BKE_nurb_makeFaces(
190     const struct Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv);
191 void BKE_nurb_makeCurve(const struct Nurb *nu,
192                         float *coord_array,
193                         float *tilt_array,
194                         float *radius_array,
195                         float *weight_array,
196                         int resolu,
197                         int stride);
198 
199 unsigned int BKE_curve_calc_coords_axis_len(const unsigned int bezt_array_len,
200                                             const unsigned int resolu,
201                                             const bool is_cyclic,
202                                             const bool use_cyclic_duplicate_endpoint);
203 void BKE_curve_calc_coords_axis(const struct BezTriple *bezt_array,
204                                 const unsigned int bezt_array_len,
205                                 const unsigned int resolu,
206                                 const bool is_cyclic,
207                                 const bool use_cyclic_duplicate_endpoint,
208                                 /* array params */
209                                 const unsigned int axis,
210                                 const unsigned int stride,
211                                 float *r_points);
212 
213 void BKE_nurb_knot_calc_u(struct Nurb *nu);
214 void BKE_nurb_knot_calc_v(struct Nurb *nu);
215 
216 /* nurb checks if they can be drawn, also clamp order func */
217 bool BKE_nurb_check_valid_u(const struct Nurb *nu);
218 bool BKE_nurb_check_valid_v(const struct Nurb *nu);
219 bool BKE_nurb_check_valid_uv(const struct Nurb *nu);
220 
221 bool BKE_nurb_order_clamp_u(struct Nurb *nu);
222 bool BKE_nurb_order_clamp_v(struct Nurb *nu);
223 
224 void BKE_nurb_direction_switch(struct Nurb *nu);
225 bool BKE_nurb_type_convert(struct Nurb *nu,
226                            const short type,
227                            const bool use_handles,
228                            const char **r_err_msg);
229 
230 void BKE_nurb_points_add(struct Nurb *nu, int number);
231 void BKE_nurb_bezierPoints_add(struct Nurb *nu, int number);
232 
233 int BKE_nurb_index_from_uv(struct Nurb *nu, int u, int v);
234 void BKE_nurb_index_to_uv(struct Nurb *nu, int index, int *r_u, int *r_v);
235 
236 struct BezTriple *BKE_nurb_bezt_get_next(struct Nurb *nu, struct BezTriple *bezt);
237 struct BezTriple *BKE_nurb_bezt_get_prev(struct Nurb *nu, struct BezTriple *bezt);
238 struct BPoint *BKE_nurb_bpoint_get_next(struct Nurb *nu, struct BPoint *bp);
239 struct BPoint *BKE_nurb_bpoint_get_prev(struct Nurb *nu, struct BPoint *bp);
240 
241 void BKE_nurb_bezt_calc_normal(struct Nurb *nu, struct BezTriple *bezt, float r_normal[3]);
242 void BKE_nurb_bezt_calc_plane(struct Nurb *nu, struct BezTriple *bezt, float r_plane[3]);
243 
244 void BKE_nurb_bpoint_calc_normal(struct Nurb *nu, struct BPoint *bp, float r_normal[3]);
245 void BKE_nurb_bpoint_calc_plane(struct Nurb *nu, struct BPoint *bp, float r_plane[3]);
246 
247 void BKE_nurb_handle_calc(struct BezTriple *bezt,
248                           struct BezTriple *prev,
249                           struct BezTriple *next,
250                           const bool is_fcurve,
251                           const char smoothing);
252 void BKE_nurb_handle_calc_ex(struct BezTriple *bezt,
253                              struct BezTriple *prev,
254                              struct BezTriple *next,
255                              const eBezTriple_Flag__Alias handle_sel_flag,
256                              const bool is_fcurve,
257                              const char smoothing);
258 void BKE_nurb_handle_calc_simple(struct Nurb *nu, struct BezTriple *bezt);
259 void BKE_nurb_handle_calc_simple_auto(struct Nurb *nu, struct BezTriple *bezt);
260 
261 void BKE_nurb_handle_smooth_fcurve(struct BezTriple *bezt, int total, bool cyclic);
262 
263 void BKE_nurb_handles_calc(struct Nurb *nu);
264 void BKE_nurb_handles_autocalc(struct Nurb *nu, uint8_t flag);
265 void BKE_nurb_bezt_handle_test(struct BezTriple *bezt,
266                                const eBezTriple_Flag__Alias sel_flag,
267                                const bool use_handle,
268                                const bool use_around_local);
269 void BKE_nurb_handles_test(struct Nurb *nu, const bool use_handles, const bool use_around_local);
270 
271 /* **** Depsgraph evaluation **** */
272 
273 void BKE_curve_eval_geometry(struct Depsgraph *depsgraph, struct Curve *curve);
274 
275 /* Draw Cache */
276 enum {
277   BKE_CURVE_BATCH_DIRTY_ALL = 0,
278   BKE_CURVE_BATCH_DIRTY_SELECT,
279 };
280 void BKE_curve_batch_cache_dirty_tag(struct Curve *cu, int mode);
281 void BKE_curve_batch_cache_free(struct Curve *cu);
282 
283 extern void (*BKE_curve_batch_cache_dirty_tag_cb)(struct Curve *cu, int mode);
284 extern void (*BKE_curve_batch_cache_free_cb)(struct Curve *cu);
285 
286 /* -------------------------------------------------------------------- */
287 /** \name Decimate Curve (curve_decimate.c)
288  *
289  * Simplify curve data.
290  * \{ */
291 
292 unsigned int BKE_curve_decimate_bezt_array(struct BezTriple *bezt_array,
293                                            const unsigned int bezt_array_len,
294                                            const unsigned int resolu,
295                                            const bool is_cyclic,
296                                            const char flag_test,
297                                            const char flag_set,
298                                            const float error_sq_max,
299                                            const unsigned int error_target_len);
300 
301 void BKE_curve_decimate_nurb(struct Nurb *nu,
302                              const unsigned int resolu,
303                              const float error_sq_max,
304                              const unsigned int error_target_len);
305 
306 /** \} */
307 
308 /* -------------------------------------------------------------------- */
309 /** \name Deform 3D Coordinates by Curve (curve_deform.c)
310  * \{ */
311 
312 void BKE_curve_deform_coords(const struct Object *ob_curve,
313                              const struct Object *ob_target,
314                              float (*vert_coords)[3],
315                              const int vert_coords_len,
316                              const struct MDeformVert *dvert,
317                              const int defgrp_index,
318                              const short flag,
319                              const short defaxis);
320 
321 void BKE_curve_deform_coords_with_editmesh(const Object *ob_curve,
322                                            const Object *ob_target,
323                                            float (*vert_coords)[3],
324                                            const int vert_coords_len,
325                                            const int defgrp_index,
326                                            const short flag,
327                                            const short defaxis,
328                                            struct BMEditMesh *em_target);
329 
330 void BKE_curve_deform_co(const struct Object *ob_curve,
331                          const struct Object *ob_target,
332                          const float orco[3],
333                          float vec[3],
334                          const int no_rot_axis,
335                          float r_mat[3][3]);
336 
337 /** \} */
338 
339 #ifdef __cplusplus
340 }
341 #endif
342