2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2014, Blender Foundation
20 * Contributor(s): Joshua Leung, Antonio Vazquez
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file blender/editors/gpencil/gpencil_utils.c
35 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_utildefines.h"
40 #include "BLT_translation.h"
43 #include "DNA_gpencil_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_screen_types.h"
47 #include "DNA_space_types.h"
48 #include "DNA_view3d_types.h"
50 #include "BKE_context.h"
51 #include "BKE_gpencil.h"
52 #include "BKE_tracking.h"
53 #include "BKE_action.h"
57 #include "RNA_access.h"
58 #include "RNA_define.h"
59 #include "RNA_enum_types.h"
61 #include "UI_resources.h"
62 #include "UI_view2d.h"
64 #include "ED_gpencil.h"
66 #include "ED_view3d.h"
68 #include "gpencil_intern.h"
70 /* ******************************************************** */
71 /* Context Wrangling... */
73 /* Get pointer to active Grease Pencil datablock, and an RNA-pointer to trace back to whatever owns it,
74 * when context info is not available.
76 bGPdata **ED_gpencil_data_get_pointers_direct(ID *screen_id, Scene *scene, ScrArea *sa, Object *ob, PointerRNA *ptr)
78 /* if there's an active area, check if the particular editor may
79 * have defined any special Grease Pencil context for editing...
82 SpaceLink *sl = sa->spacedata.first;
84 switch (sa->spacetype) {
85 case SPACE_VIEW3D: /* 3D-View */
87 BLI_assert(scene && ELEM(scene->toolsettings->gpencil_src,
88 GP_TOOL_SOURCE_SCENE, GP_TOOL_SOURCE_OBJECT));
90 if (scene->toolsettings->gpencil_src == GP_TOOL_SOURCE_OBJECT) {
91 /* legacy behaviour for usage with old addons requiring object-linked to objects */
93 /* just in case no active/selected object... */
94 if (ob && (ob->flag & SELECT)) {
95 /* for now, as long as there's an object, default to using that in 3D-View */
96 if (ptr) RNA_id_pointer_create(&ob->id, ptr);
99 /* else: defaults to scene... */
102 if (ptr) RNA_id_pointer_create(&scene->id, ptr);
107 case SPACE_NODE: /* Nodes Editor */
109 SpaceNode *snode = (SpaceNode *)sl;
111 /* return the GP data for the active node block/node */
112 if (snode && snode->nodetree) {
113 /* for now, as long as there's an active node tree, default to using that in the Nodes Editor */
114 if (ptr) RNA_id_pointer_create(&snode->nodetree->id, ptr);
115 return &snode->nodetree->gpd;
118 /* even when there is no node-tree, don't allow this to flow to scene */
121 case SPACE_SEQ: /* Sequencer */
123 SpaceSeq *sseq = (SpaceSeq *)sl;
125 /* for now, Grease Pencil data is associated with the space (actually preview region only) */
126 /* XXX our convention for everything else is to link to data though... */
127 if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceSequenceEditor, sseq, ptr);
130 case SPACE_IMAGE: /* Image/UV Editor */
132 SpaceImage *sima = (SpaceImage *)sl;
134 /* for now, Grease Pencil data is associated with the space... */
135 /* XXX our convention for everything else is to link to data though... */
136 if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceImageEditor, sima, ptr);
139 case SPACE_CLIP: /* Nodes Editor */
141 SpaceClip *sc = (SpaceClip *)sl;
142 MovieClip *clip = ED_space_clip_get_clip(sc);
145 if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
146 MovieTrackingTrack *track = BKE_tracking_track_get_active(&clip->tracking);
152 RNA_pointer_create(&clip->id, &RNA_MovieTrackingTrack, track, ptr);
158 RNA_id_pointer_create(&clip->id, ptr);
165 default: /* unsupported space */
170 /* just fall back on the scene's GP data */
171 if (ptr) RNA_id_pointer_create((ID *)scene, ptr);
172 return (scene) ? &scene->gpd : NULL;
175 /* Get pointer to active Grease Pencil datablock, and an RNA-pointer to trace back to whatever owns it */
176 bGPdata **ED_gpencil_data_get_pointers(const bContext *C, PointerRNA *ptr)
178 ID *screen_id = (ID *)CTX_wm_screen(C);
179 Scene *scene = CTX_data_scene(C);
180 ScrArea *sa = CTX_wm_area(C);
181 Object *ob = CTX_data_active_object(C);
183 return ED_gpencil_data_get_pointers_direct(screen_id, scene, sa, ob, ptr);
186 /* -------------------------------------------------------- */
188 /* Get the active Grease Pencil datablock, when context is not available */
189 bGPdata *ED_gpencil_data_get_active_direct(ID *screen_id, Scene *scene, ScrArea *sa, Object *ob)
191 bGPdata **gpd_ptr = ED_gpencil_data_get_pointers_direct(screen_id, scene, sa, ob, NULL);
192 return (gpd_ptr) ? *(gpd_ptr) : NULL;
195 /* Get the active Grease Pencil datablock */
196 bGPdata *ED_gpencil_data_get_active(const bContext *C)
198 bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL);
199 return (gpd_ptr) ? *(gpd_ptr) : NULL;
202 /* -------------------------------------------------------- */
204 // XXX: this should be removed... We really shouldn't duplicate logic like this!
205 bGPdata *ED_gpencil_data_get_active_v3d(Scene *scene, View3D *v3d)
207 Base *base = scene->basact;
209 /* We have to make sure active object is actually visible and selected, else we must use default scene gpd,
210 * to be consistent with ED_gpencil_data_get_active's behavior.
213 if (base && TESTBASE(v3d, base)) {
214 gpd = base->object->gpd;
216 return gpd ? gpd : scene->gpd;
219 /* ******************************************************** */
220 /* Keyframe Indicator Checks */
222 /* Check whether there's an active GP keyframe on the current frame */
223 bool ED_gpencil_has_keyframe_v3d(Scene *scene, Object *ob, int cfra)
225 /* just check both for now... */
226 // XXX: this could get confusing (e.g. if only on the object, but other places don't show this)
228 bGPDlayer *gpl = BKE_gpencil_layer_getactive(scene->gpd);
231 // XXX: assumes that frame has been fetched already
232 return (gpl->actframe->framenum == cfra);
235 /* XXX: disabled as could be too much of a penalty */
236 /* return BKE_gpencil_layer_find_frame(gpl, cfra); */
242 bGPDlayer *gpl = BKE_gpencil_layer_getactive(ob->gpd);
245 // XXX: assumes that frame has been fetched already
246 return (gpl->actframe->framenum == cfra);
249 /* XXX: disabled as could be too much of a penalty */
250 /* return BKE_gpencil_layer_find_frame(gpl, cfra); */
258 /* ******************************************************** */
261 /* poll callback for adding data/layers - special */
262 int gp_add_poll(bContext *C)
264 /* the base line we have is that we have somewhere to add Grease Pencil data */
265 return ED_gpencil_data_get_pointers(C, NULL) != NULL;
268 /* poll callback for checking if there is an active layer */
269 int gp_active_layer_poll(bContext *C)
271 bGPdata *gpd = ED_gpencil_data_get_active(C);
272 bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
274 return (gpl != NULL);
277 /* poll callback for checking if there is an active brush */
278 int gp_active_brush_poll(bContext *C)
280 ToolSettings *ts = CTX_data_tool_settings(C);
281 bGPDbrush *brush = BKE_gpencil_brush_getactive(ts);
283 return (brush != NULL);
286 /* poll callback for checking if there is an active palette */
287 int gp_active_palette_poll(bContext *C)
289 bGPdata *gpd = ED_gpencil_data_get_active(C);
290 bGPDpalette *palette = BKE_gpencil_palette_getactive(gpd);
292 return (palette != NULL);
295 /* poll callback for checking if there is an active palette color */
296 int gp_active_palettecolor_poll(bContext *C)
298 bGPdata *gpd = ED_gpencil_data_get_active(C);
299 bGPDpalette *palette = BKE_gpencil_palette_getactive(gpd);
300 bGPDpalettecolor *palcolor = BKE_gpencil_palettecolor_getactive(palette);
302 return (palcolor != NULL);
305 /* ******************************************************** */
306 /* Dynamic Enums of GP Layers */
307 /* NOTE: These include an option to create a new layer and use that... */
309 /* Just existing layers */
310 EnumPropertyItem *ED_gpencil_layers_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
312 bGPdata *gpd = CTX_data_gpencil_data(C);
314 EnumPropertyItem *item = NULL, item_tmp = {0};
318 if (ELEM(NULL, C, gpd)) {
319 return DummyRNA_DEFAULT_items;
322 /* Existing layers */
323 for (gpl = gpd->layers.first; gpl; gpl = gpl->next, i++) {
324 item_tmp.identifier = gpl->info;
325 item_tmp.name = gpl->info;
328 if (gpl->flag & GP_LAYER_ACTIVE)
329 item_tmp.icon = ICON_GREASEPENCIL;
331 item_tmp.icon = ICON_NONE;
333 RNA_enum_item_add(&item, &totitem, &item_tmp);
336 RNA_enum_item_end(&item, &totitem);
342 /* Existing + Option to add/use new layer */
343 EnumPropertyItem *ED_gpencil_layers_with_new_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
345 bGPdata *gpd = CTX_data_gpencil_data(C);
347 EnumPropertyItem *item = NULL, item_tmp = {0};
351 if (ELEM(NULL, C, gpd)) {
352 return DummyRNA_DEFAULT_items;
355 /* Create new layer */
356 /* TODO: have some way of specifying that we don't want this? */
358 /* active Keying Set */
359 item_tmp.identifier = "__CREATE__";
360 item_tmp.name = "New Layer";
362 item_tmp.icon = ICON_ZOOMIN;
363 RNA_enum_item_add(&item, &totitem, &item_tmp);
366 RNA_enum_item_add_separator(&item, &totitem);
369 /* Existing layers */
370 for (gpl = gpd->layers.first, i = 0; gpl; gpl = gpl->next, i++) {
371 item_tmp.identifier = gpl->info;
372 item_tmp.name = gpl->info;
375 if (gpl->flag & GP_LAYER_ACTIVE)
376 item_tmp.icon = ICON_GREASEPENCIL;
378 item_tmp.icon = ICON_NONE;
380 RNA_enum_item_add(&item, &totitem, &item_tmp);
383 RNA_enum_item_end(&item, &totitem);
391 /* ******************************************************** */
392 /* Brush Tool Core */
395 * Check whether a given stroke segment is inside a circular brush
397 * \param mval The current screen-space coordinates (midpoint) of the brush
398 * \param mvalo The previous screen-space coordinates (midpoint) of the brush (NOT CURRENTLY USED)
399 * \param rad The radius of the brush
401 * \param x0, y0 The screen-space x and y coordinates of the start of the stroke segment
402 * \param x1, y1 The screen-space x and y coordinates of the end of the stroke segment
404 bool gp_stroke_inside_circle(const int mval[2], const int UNUSED(mvalo[2]),
405 int rad, int x0, int y0, int x1, int y1)
407 /* simple within-radius check for now */
408 const float mval_fl[2] = {mval[0], mval[1]};
409 const float screen_co_a[2] = {x0, y0};
410 const float screen_co_b[2] = {x1, y1};
412 if (edge_inside_circle(mval_fl, rad, screen_co_a, screen_co_b)) {
420 /* ******************************************************** */
421 /* Stroke Validity Testing */
423 /* Check whether given stroke can be edited given the supplied context */
424 // XXX: do we need additional flags for screenspace vs dataspace?
425 bool ED_gpencil_stroke_can_use_direct(const ScrArea *sa, const bGPDstroke *gps)
428 if (ELEM(NULL, sa, gps))
431 /* filter stroke types by flags + spacetype */
432 if (gps->flag & GP_STROKE_3DSPACE) {
433 /* 3D strokes - only in 3D view */
434 return (sa->spacetype == SPACE_VIEW3D);
436 else if (gps->flag & GP_STROKE_2DIMAGE) {
437 /* Special "image" strokes - only in Image Editor */
438 return (sa->spacetype == SPACE_IMAGE);
440 else if (gps->flag & GP_STROKE_2DSPACE) {
441 /* 2D strokes (dataspace) - for any 2D view (i.e. everything other than 3D view) */
442 return (sa->spacetype != SPACE_VIEW3D);
445 /* view aligned - anything goes */
450 /* Check whether given stroke can be edited in the current context */
451 bool ED_gpencil_stroke_can_use(const bContext *C, const bGPDstroke *gps)
453 ScrArea *sa = CTX_wm_area(C);
454 return ED_gpencil_stroke_can_use_direct(sa, gps);
457 /* Check whether given stroke can be edited for the current color */
458 bool ED_gpencil_stroke_color_use(const bGPDlayer *gpl, const bGPDstroke *gps)
460 /* check if the color is editable */
461 bGPDpalettecolor *palcolor = gps->palcolor;
462 if (palcolor != NULL) {
463 if (palcolor->flag & PC_COLOR_HIDE)
465 if (((gpl->flag & GP_LAYER_UNLOCK_COLOR) == 0) && (palcolor->flag & PC_COLOR_LOCKED))
472 /* Get palette color or create a new one */
473 bGPDpalettecolor *ED_gpencil_stroke_getcolor(bGPdata *gpd, bGPDstroke *gps)
475 bGPDpalette *palette;
476 bGPDpalettecolor *palcolor;
478 if ((gps->palcolor != NULL) && ((gps->flag & GP_STROKE_RECALC_COLOR) == 0))
479 return gps->palcolor;
482 palette = BKE_gpencil_palette_getactive(gpd);
483 if (palette == NULL) {
484 palette = BKE_gpencil_palette_addnew(gpd, DATA_("GP_Palette"), true);
487 palcolor = BKE_gpencil_palettecolor_getbyname(palette, gps->colorname);
488 if (palcolor == NULL) {
489 if (gps->palcolor == NULL) {
490 palcolor = BKE_gpencil_palettecolor_addnew(palette, DATA_("Color"), true);
491 /* set to a different color */
492 ARRAY_SET_ITEMS(palcolor->color, 1.0f, 0.0f, 1.0f, 0.9f);
495 palcolor = BKE_gpencil_palettecolor_addnew(palette, gps->colorname, true);
496 /* set old color and attributes */
497 bGPDpalettecolor *gpscolor = gps->palcolor;
498 copy_v4_v4(palcolor->color, gpscolor->color);
499 copy_v4_v4(palcolor->fill, gpscolor->fill);
500 palcolor->flag = gpscolor->flag;
504 /* clear flag and set pointer */
505 gps->flag &= ~GP_STROKE_RECALC_COLOR;
506 gps->palcolor = palcolor;
511 /* ******************************************************** */
512 /* Space Conversion */
515 * Init settings for stroke point space conversions
517 * \param r_gsc: [out] The space conversion settings struct, populated with necessary params
519 void gp_point_conversion_init(bContext *C, GP_SpaceConversion *r_gsc)
521 ScrArea *sa = CTX_wm_area(C);
522 ARegion *ar = CTX_wm_region(C);
524 /* zero out the storage (just in case) */
525 memset(r_gsc, 0, sizeof(GP_SpaceConversion));
531 r_gsc->v2d = &ar->v2d;
533 /* init region-specific stuff */
534 if (sa->spacetype == SPACE_VIEW3D) {
535 wmWindow *win = CTX_wm_window(C);
536 Scene *scene = CTX_data_scene(C);
537 View3D *v3d = (View3D *)CTX_wm_space_data(C);
538 RegionView3D *rv3d = ar->regiondata;
540 /* init 3d depth buffers */
541 view3d_operator_needs_opengl(C);
543 view3d_region_operator_needs_opengl(win, ar);
544 ED_view3d_autodist_init(scene, ar, v3d, 0);
546 /* for camera view set the subrect */
547 if (rv3d->persp == RV3D_CAMOB) {
548 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &r_gsc->subrect_data, true); /* no shift */
549 r_gsc->subrect = &r_gsc->subrect_data;
555 * Convert point to parent space
557 * \param pt Original point
558 * \param diff_mat Matrix with the difference between original parent matrix
559 * \param[out] r_pt Pointer to new point after apply matrix
561 void gp_point_to_parent_space(bGPDspoint *pt, float diff_mat[4][4], bGPDspoint *r_pt)
565 mul_v3_m4v3(fpt, diff_mat, &pt->x);
566 copy_v3_v3(&r_pt->x, fpt);
570 * Change points position relative to parent object
572 void gp_apply_parent(bGPDlayer *gpl, bGPDstroke *gps)
578 float diff_mat[4][4];
579 float inverse_diff_mat[4][4];
582 ED_gpencil_parent_location(gpl, diff_mat);
583 invert_m4_m4(inverse_diff_mat, diff_mat);
585 for (i = 0; i < gps->totpoints; i++) {
586 pt = &gps->points[i];
587 mul_v3_m4v3(fpt, inverse_diff_mat, &pt->x);
588 copy_v3_v3(&pt->x, fpt);
593 * Change point position relative to parent object
595 void gp_apply_parent_point(bGPDlayer *gpl, bGPDspoint *pt)
598 float diff_mat[4][4];
599 float inverse_diff_mat[4][4];
602 ED_gpencil_parent_location(gpl, diff_mat);
603 invert_m4_m4(inverse_diff_mat, diff_mat);
605 mul_v3_m4v3(fpt, inverse_diff_mat, &pt->x);
606 copy_v3_v3(&pt->x, fpt);
610 * Convert a Grease Pencil coordinate (i.e. can be 2D or 3D) to screenspace (2D)
612 * \param[out] r_x The screen-space x-coordinate of the point
613 * \param[out] r_y The screen-space y-coordinate of the point
615 * \warning This assumes that the caller has already checked whether the stroke in question can be drawn.
617 void gp_point_to_xy(GP_SpaceConversion *gsc, bGPDstroke *gps, bGPDspoint *pt,
620 ARegion *ar = gsc->ar;
621 View2D *v2d = gsc->v2d;
622 rctf *subrect = gsc->subrect;
626 BLI_assert(!(gps->flag & GP_STROKE_3DSPACE) || (gsc->sa->spacetype == SPACE_VIEW3D));
627 BLI_assert(!(gps->flag & GP_STROKE_2DSPACE) || (gsc->sa->spacetype != SPACE_VIEW3D));
630 if (gps->flag & GP_STROKE_3DSPACE) {
631 if (ED_view3d_project_int_global(ar, &pt->x, xyval, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
636 *r_x = V2D_IS_CLIPPED;
637 *r_y = V2D_IS_CLIPPED;
640 else if (gps->flag & GP_STROKE_2DSPACE) {
641 float vec[3] = {pt->x, pt->y, 0.0f};
642 mul_m4_v3(gsc->mat, vec);
643 UI_view2d_view_to_region_clip(v2d, vec[0], vec[1], r_x, r_y);
646 if (subrect == NULL) {
647 /* normal 3D view (or view space) */
648 *r_x = (int)(pt->x / 100 * ar->winx);
649 *r_y = (int)(pt->y / 100 * ar->winy);
652 /* camera view, use subrect */
653 *r_x = (int)((pt->x / 100) * BLI_rctf_size_x(subrect)) + subrect->xmin;
654 *r_y = (int)((pt->y / 100) * BLI_rctf_size_y(subrect)) + subrect->ymin;
660 * Convert a Grease Pencil coordinate (i.e. can be 2D or 3D) to screenspace (2D)
662 * Just like gp_point_to_xy(), except the resulting coordinates are floats not ints.
663 * Use this version to solve "stair-step" artifacts which may arise when roundtripping the calculations.
665 * \param r_x: [out] The screen-space x-coordinate of the point
666 * \param r_y: [out] The screen-space y-coordinate of the point
668 * \warning This assumes that the caller has already checked whether the stroke in question can be drawn
670 void gp_point_to_xy_fl(GP_SpaceConversion *gsc, bGPDstroke *gps, bGPDspoint *pt,
671 float *r_x, float *r_y)
673 ARegion *ar = gsc->ar;
674 View2D *v2d = gsc->v2d;
675 rctf *subrect = gsc->subrect;
679 BLI_assert(!(gps->flag & GP_STROKE_3DSPACE) || (gsc->sa->spacetype == SPACE_VIEW3D));
680 BLI_assert(!(gps->flag & GP_STROKE_2DSPACE) || (gsc->sa->spacetype != SPACE_VIEW3D));
683 if (gps->flag & GP_STROKE_3DSPACE) {
684 if (ED_view3d_project_float_global(ar, &pt->x, xyval, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
693 else if (gps->flag & GP_STROKE_2DSPACE) {
694 float vec[3] = {pt->x, pt->y, 0.0f};
697 mul_m4_v3(gsc->mat, vec);
698 UI_view2d_view_to_region_clip(v2d, vec[0], vec[1], &t_x, &t_y);
700 if ((t_x == t_y) && (t_x == V2D_IS_CLIPPED)) {
701 /* XXX: Or should we just always use the values as-is? */
711 if (subrect == NULL) {
712 /* normal 3D view (or view space) */
713 *r_x = (pt->x / 100.0f * ar->winx);
714 *r_y = (pt->y / 100.0f * ar->winy);
717 /* camera view, use subrect */
718 *r_x = ((pt->x / 100.0f) * BLI_rctf_size_x(subrect)) + subrect->xmin;
719 *r_y = ((pt->y / 100.0f) * BLI_rctf_size_y(subrect)) + subrect->ymin;
725 * Project screenspace coordinates to 3D-space
727 * For use with editing tools where it is easier to perform the operations in 2D,
728 * and then later convert the transformed points back to 3D.
730 * \param screen_co: The screenspace 2D coordinates to convert to
731 * \param r_out: The resulting 3D coordinates of the input point
733 * \note We include this as a utility function, since the standard method
734 * involves quite a few steps, which are invariably always the same
735 * for all GPencil operations. So, it's nicer to just centralize these.
737 * \warning Assumes that it is getting called in a 3D view only.
739 bool gp_point_xy_to_3d(GP_SpaceConversion *gsc, Scene *scene, const float screen_co[2], float r_out[3])
741 View3D *v3d = gsc->sa->spacedata.first;
742 RegionView3D *rv3d = gsc->ar->regiondata;
743 float *rvec = ED_view3d_cursor3d_get(scene, v3d);
744 float ref[3] = {rvec[0], rvec[1], rvec[2]};
745 float zfac = ED_view3d_calc_zfac(rv3d, rvec, NULL);
747 float mval_f[2], mval_prj[2];
750 copy_v2_v2(mval_f, screen_co);
752 if (ED_view3d_project_float_global(gsc->ar, ref, mval_prj, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
753 sub_v2_v2v2(mval_f, mval_prj, mval_f);
754 ED_view3d_win_to_delta(gsc->ar, mval_f, dvec, zfac);
755 sub_v3_v3v3(r_out, rvec, dvec);
767 * Apply smooth to stroke point
768 * \param gps Stroke to smooth
769 * \param i Point index
770 * \param inf Amount of smoothing to apply
771 * \param affect_pressure Apply smoothing to pressure values too?
773 bool gp_smooth_stroke(bGPDstroke *gps, int i, float inf, bool affect_pressure)
775 bGPDspoint *pt = &gps->points[i];
776 float pressure = 0.0f;
777 float sco[3] = {0.0f};
779 /* Do nothing if not enough points to smooth out */
780 if (gps->totpoints <= 2) {
784 /* Only affect endpoints by a fraction of the normal strength,
785 * to prevent the stroke from shrinking too much
787 if ((i == 0) || (i == gps->totpoints - 1)) {
791 /* Compute smoothed coordinate by taking the ones nearby */
792 /* XXX: This is potentially slow, and suffers from accumulation error as earlier points are handled before later ones */
794 // XXX: this is hardcoded to look at 2 points on either side of the current one (i.e. 5 items total)
796 const float average_fac = 1.0f / (float)(steps * 2 + 1);
799 /* add the point itself */
800 madd_v3_v3fl(sco, &pt->x, average_fac);
802 if (affect_pressure) {
803 pressure += pt->pressure * average_fac;
806 /* n-steps before/after current point */
807 // XXX: review how the endpoints are treated by this algorithm
808 // XXX: falloff measures should also introduce some weighting variations, so that further-out points get less weight
809 for (step = 1; step <= steps; step++) {
810 bGPDspoint *pt1, *pt2;
811 int before = i - step;
812 int after = i + step;
814 CLAMP_MIN(before, 0);
815 CLAMP_MAX(after, gps->totpoints - 1);
817 pt1 = &gps->points[before];
818 pt2 = &gps->points[after];
820 /* add both these points to the average-sum (s += p[i]/n) */
821 madd_v3_v3fl(sco, &pt1->x, average_fac);
822 madd_v3_v3fl(sco, &pt2->x, average_fac);
825 /* XXX: Disabled because get weird result */
826 /* do pressure too? */
827 if (affect_pressure) {
828 pressure += pt1->pressure * average_fac;
829 pressure += pt2->pressure * average_fac;
835 /* Based on influence factor, blend between original and optimal smoothed coordinate */
836 interp_v3_v3v3(&pt->x, &pt->x, sco, inf);
839 /* XXX: Disabled because get weird result */
840 if (affect_pressure) {
841 pt->pressure = pressure;
849 * Apply smooth for strength to stroke point
850 * \param gps Stroke to smooth
851 * \param i Point index
852 * \param inf Amount of smoothing to apply
854 bool gp_smooth_stroke_strength(bGPDstroke *gps, int i, float inf)
856 bGPDspoint *ptb = &gps->points[i];
858 /* Do nothing if not enough points */
859 if (gps->totpoints <= 2) {
863 /* Compute theoretical optimal value using distances */
864 bGPDspoint *pta, *ptc;
868 CLAMP_MIN(before, 0);
869 CLAMP_MAX(after, gps->totpoints - 1);
871 pta = &gps->points[before];
872 ptc = &gps->points[after];
874 /* the optimal value is the corresponding to the interpolation of the strength
875 * at the distance of point b
877 const float fac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x);
878 const float optimal = (1.0f - fac) * pta->strength + fac * ptc->strength;
880 /* Based on influence factor, blend between original and optimal */
881 ptb->strength = (1.0f - inf) * ptb->strength + inf * optimal;
887 * Apply smooth for thickness to stroke point (use pressure)
888 * \param gps Stroke to smooth
889 * \param i Point index
890 * \param inf Amount of smoothing to apply
892 bool gp_smooth_stroke_thickness(bGPDstroke *gps, int i, float inf)
894 bGPDspoint *ptb = &gps->points[i];
896 /* Do nothing if not enough points */
897 if (gps->totpoints <= 2) {
901 /* Compute theoretical optimal value using distances */
902 bGPDspoint *pta, *ptc;
906 CLAMP_MIN(before, 0);
907 CLAMP_MAX(after, gps->totpoints - 1);
909 pta = &gps->points[before];
910 ptc = &gps->points[after];
912 /* the optimal value is the corresponding to the interpolation of the pressure
913 * at the distance of point b
915 float fac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x);
916 float optimal = (1.0f - fac) * pta->pressure + fac * ptc->pressure;
918 /* Based on influence factor, blend between original and optimal */
919 ptb->pressure = (1.0f - inf) * ptb->pressure + inf * optimal;
925 * Subdivide a stroke once, by adding a point half way between each pair of existing points
926 * \param gps Stroke data
927 * \param new_totpoints Total number of points (after subdividing)
929 void gp_subdivide_stroke(bGPDstroke *gps, const int new_totpoints)
931 /* Move points towards end of enlarged points array to leave space for new points */
933 for (int i = gps->totpoints - 1; i > 0; i--) {
934 gps->points[new_totpoints - y] = gps->points[i];
938 /* Create interpolated points */
939 for (int i = 0; i < new_totpoints - 1; i += 2) {
940 bGPDspoint *prev = &gps->points[i];
941 bGPDspoint *pt = &gps->points[i + 1];
942 bGPDspoint *next = &gps->points[i + 2];
944 /* Interpolate all values */
945 interp_v3_v3v3(&pt->x, &prev->x, &next->x, 0.5f);
947 pt->pressure = interpf(prev->pressure, next->pressure, 0.5f);
948 pt->strength = interpf(prev->strength, next->strength, 0.5f);
949 CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f);
950 pt->time = interpf(prev->time, next->time, 0.5f);
953 /* Update to new total number of points */
954 gps->totpoints = new_totpoints;
958 * Add randomness to stroke
959 * \param gps Stroke data
960 * \param brush Brush data
962 void gp_randomize_stroke(bGPDstroke *gps, bGPDbrush *brush)
964 bGPDspoint *pt1, *pt2, *pt3;
967 if (gps->totpoints < 3) {
971 /* get two vectors using 3 points */
972 pt1 = &gps->points[0];
973 pt2 = &gps->points[1];
974 pt3 = &gps->points[(int)(gps->totpoints * 0.75)];
976 sub_v3_v3v3(v1, &pt2->x, &pt1->x);
977 sub_v3_v3v3(v2, &pt3->x, &pt2->x);
981 /* get normal vector to plane created by two vectors */
983 cross_v3_v3v3(normal, v1, v2);
984 normalize_v3(normal);
986 /* get orthogonal vector to plane to rotate random effect */
988 cross_v3_v3v3(ortho, v1, normal);
991 /* Read all points and apply shift vector (first and last point not modified) */
992 for (int i = 1; i < gps->totpoints - 1; ++i) {
993 bGPDspoint *pt = &gps->points[i];
994 /* get vector with shift (apply a division because random is too sensitive */
995 const float fac = BLI_frand() * (brush->draw_random_sub / 10.0f);
997 copy_v3_v3(svec, ortho);
998 if (BLI_frand() > 0.5f) {
999 mul_v3_fl(svec, -fac);
1002 mul_v3_fl(svec, fac);
1006 add_v3_v3(&pt->x, svec);
1010 /* calculate difference matrix */
1011 void ED_gpencil_parent_location(bGPDlayer *gpl, float diff_mat[4][4])
1013 Object *ob = gpl->parent;
1020 if ((gpl->partype == PAROBJECT) || (gpl->partype == PARSKEL)) {
1021 mul_m4_m4m4(diff_mat, ob->obmat, gpl->inverse);
1024 else if (gpl->partype == PARBONE) {
1025 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, gpl->parsubstr);
1027 float tmp_mat[4][4];
1028 mul_m4_m4m4(tmp_mat, ob->obmat, pchan->pose_mat);
1029 mul_m4_m4m4(diff_mat, tmp_mat, gpl->inverse);
1032 mul_m4_m4m4(diff_mat, ob->obmat, gpl->inverse); /* if bone not found use object (armature) */
1037 unit_m4(diff_mat); /* not defined type */
1042 /* reset parent matrix for all layers */
1043 void ED_gpencil_reset_layers_parent(bGPdata *gpd)
1047 float diff_mat[4][4];
1048 float cur_mat[4][4];
1050 for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1051 if (gpl->parent != NULL) {
1052 /* calculate new matrix */
1053 if ((gpl->partype == PAROBJECT) || (gpl->partype == PARSKEL)) {
1054 invert_m4_m4(cur_mat, gpl->parent->obmat);
1056 else if (gpl->partype == PARBONE) {
1057 bPoseChannel *pchan = BKE_pose_channel_find_name(gpl->parent->pose, gpl->parsubstr);
1059 float tmp_mat[4][4];
1060 mul_m4_m4m4(tmp_mat, gpl->parent->obmat, pchan->pose_mat);
1061 invert_m4_m4(cur_mat, tmp_mat);
1065 /* only redo if any change */
1066 if (!equals_m4m4(gpl->inverse, cur_mat)) {
1067 /* first apply current transformation to all strokes */
1068 ED_gpencil_parent_location(gpl, diff_mat);
1069 for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
1070 for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
1071 for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
1072 mul_m4_v3(diff_mat, &pt->x);
1076 /* set new parent matrix */
1077 copy_m4_m4(gpl->inverse, cur_mat);
1082 /* ******************************************************** */
1083 bool ED_gpencil_stroke_minmax(
1084 const bGPDstroke *gps, const bool use_select,
1085 float r_min[3], float r_max[3])
1087 const bGPDspoint *pt;
1089 bool changed = false;
1091 for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
1092 if ((use_select == false) || (pt->flag & GP_SPOINT_SELECT)) {;
1093 minmax_v3v3_v3(r_min, r_max, &pt->x);
1100 /* Dynamic Enums of GP Brushes */
1101 EnumPropertyItem *ED_gpencil_brushes_enum_itemf(
1102 bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop),
1105 ToolSettings *ts = CTX_data_tool_settings(C);
1107 EnumPropertyItem *item = NULL, item_tmp = { 0 };
1111 if (ELEM(NULL, C, ts)) {
1112 return DummyRNA_DEFAULT_items;
1115 /* Existing brushes */
1116 for (brush = ts->gp_brushes.first; brush; brush = brush->next, i++) {
1117 item_tmp.identifier = brush->info;
1118 item_tmp.name = brush->info;
1121 if (brush->flag & GP_BRUSH_ACTIVE)
1122 item_tmp.icon = ICON_BRUSH_DATA;
1124 item_tmp.icon = ICON_NONE;
1126 RNA_enum_item_add(&item, &totitem, &item_tmp);
1129 RNA_enum_item_end(&item, &totitem);
1135 /* Dynamic Enums of GP Palettes */
1136 EnumPropertyItem *ED_gpencil_palettes_enum_itemf(
1137 bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop),
1140 bGPdata *gpd = CTX_data_gpencil_data(C);
1141 bGPDpalette *palette;
1142 EnumPropertyItem *item = NULL, item_tmp = { 0 };
1146 if (ELEM(NULL, C, gpd)) {
1147 return DummyRNA_DEFAULT_items;
1150 /* Existing palettes */
1151 for (palette = gpd->palettes.first; palette; palette = palette->next, i++) {
1152 item_tmp.identifier = palette->info;
1153 item_tmp.name = palette->info;
1156 if (palette->flag & PL_PALETTE_ACTIVE)
1157 item_tmp.icon = ICON_COLOR;
1159 item_tmp.icon = ICON_NONE;
1161 RNA_enum_item_add(&item, &totitem, &item_tmp);
1164 RNA_enum_item_end(&item, &totitem);
1169 /* ******************************************************** */