1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2019, Blender Foundation.
* This is a new part of Blender
*
* Contributor(s): Antonio Vazquez
*
* ***** END GPL LICENSE BLOCK *****
*
* Operators for merge Grease Pencil strokes
*/
/** \file blender/editors/gpencil/gpencil_merge.c
* \ingroup edgpencil
*/
#include <stdio.h>
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BLI_ghash.h"
#include "BLI_math.h"
#include "DNA_gpencil_types.h"
#include "BKE_brush.h"
#include "BKE_context.h"
#include "BKE_gpencil.h"
#include "BKE_material.h"
#include "WM_api.h"
#include "WM_types.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "ED_gpencil.h"
#include "ED_object.h"
#include "ED_screen.h"
#include "ED_view3d.h"
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"
#include "gpencil_intern.h"
typedef struct tGPencilPointCache {
float factor; /* value to sort */
bGPDstroke *gps;
float x, y, z;
float pressure;
float strength;
} tGPencilPointCache;
/* helper function to sort points */
static int gpencil_sort_points(const void *a1, const void *a2)
{
const tGPencilPointCache *ps1 = a1, *ps2 = a2;
if (ps1->factor < ps2->factor) return -1;
else if (ps1->factor > ps2->factor) return 1;
return 0;
}
static void gpencil_insert_points_to_stroke(
bGPDstroke *gps, tGPencilPointCache *points_array, int totpoints)
{
tGPencilPointCache *point_elem = NULL;
for (int i = 0; i < totpoints; i++) {
point_elem = &points_array[i];
bGPDspoint *pt_dst = &gps->points[i];
copy_v3_v3(&pt_dst->x, &point_elem->x);
pt_dst->pressure = point_elem->pressure;
pt_dst->strength = point_elem->strength;
pt_dst->uv_fac = 1.0f;
pt_dst->uv_rot = 0;
pt_dst->flag |= GP_SPOINT_SELECT;
}
}
static bGPDstroke *gpencil_prepare_stroke(bContext *C, wmOperator *op, int totpoints)
{
ToolSettings *ts = CTX_data_tool_settings(C);
Depsgraph *depsgraph = CTX_data_depsgraph(C);
Object *ob = CTX_data_active_object(C);
bGPDlayer *gpl = CTX_data_active_gpencil_layer(C);
int cfra_eval = (int)DEG_get_ctime(depsgraph);
const bool back = RNA_boolean_get(op->ptr, "back");
const bool additive = RNA_boolean_get(op->ptr, "additive");
const bool cyclic = RNA_boolean_get(op->ptr, "cyclic");
Paint *paint = &ts->gp_paint->paint;
/* if not exist, create a new one */
if (paint->brush == NULL) {
/* create new brushes */
BKE_brush_gpencil_presets(C);
}
Brush *brush = paint->brush;
/* frame */
short add_frame_mode;
if (additive) {
add_frame_mode = GP_GETFRAME_ADD_COPY;
}
else {
add_frame_mode = GP_GETFRAME_ADD_NEW;
}
bGPDframe *gpf = BKE_gpencil_layer_getframe(gpl, cfra_eval, add_frame_mode);
/* stroke */
bGPDstroke *gps = MEM_callocN(sizeof(bGPDstroke), "gp_stroke");
gps->totpoints = totpoints;
gps->inittime = 0.0f;
gps->thickness = brush->size;
gps->flag |= GP_STROKE_SELECT;
gps->flag |= GP_STROKE_3DSPACE;
gps->mat_nr = ob->actcol - 1;
/* allocate memory for points */
gps->points = MEM_callocN(sizeof(bGPDspoint) * totpoints, "gp_stroke_points");
/* initialize triangle memory to dummy data */
gps->tot_triangles = 0;
gps->triangles = NULL;
gps->flag |= GP_STROKE_RECALC_GEOMETRY;
if (cyclic) {
gps->flag |= GP_STROKE_CYCLIC;
}
/* add new stroke to frame */
if (back) {
BLI_addhead(&gpf->strokes, gps);
}
else {
BLI_addtail(&gpf->strokes, gps);
}
return gps;
}
static void gpencil_get_elements_len(bContext *C, int *totstrokes, int *totpoints)
{
bGPDspoint *pt;
int i;
/* count number of strokes and selected points */
CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes)
{
if (gps->flag & GP_STROKE_SELECT) {
*totstrokes += 1;
for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
if (pt->flag & GP_SPOINT_SELECT) {
*totpoints += 1;
}
}
}
}
CTX_DATA_END;
}
static void gpencil_dissolve_points(bContext *C)
{
bGPDstroke *gps, *gpsn;
CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers)
{
bGPDframe *gpf = gpl->actframe;
if (gpf == NULL) {
continue;
}
for (gps = gpf->strokes.first; gps; gps = gpsn) {
gpsn = gps->next;
gp_stroke_delete_tagged_points(gpf, gps, gpsn, GP_SPOINT_TAG, false);
}
}
CTX_DATA_END;
}
/* Calc a factor of each selected point and fill an array with all the data.
*
* The factor is calculated using an imaginary circle, using the angle relative
* to this circle and the distance to the calculated center of the selected points.
*
* All the data is saved to be sorted and used later.
*/
static void gpencil_calc_points_factor(
bContext *C, const int mode, int totpoints,
const bool clear_point, const bool clear_stroke,
tGPencilPointCache *src_array)
{
bGPDspoint *pt;
int i;
int idx = 0;
/* create selected point array an fill it */
bGPDstroke **gps_array = MEM_callocN(sizeof(bGPDstroke *) * totpoints, __func__);
bGPDspoint *pt_array = MEM_callocN(sizeof(bGPDspoint) * totpoints, __func__);
CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers)
{
bGPDframe *gpf = gpl->actframe;
if (gpf == NULL) {
continue;
}
for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
if (gps->flag & GP_STROKE_SELECT) {
for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
if (clear_stroke) {
pt->flag |= GP_SPOINT_TAG;
}
else {
pt->flag &= ~GP_SPOINT_TAG;
}
if (pt->flag & GP_SPOINT_SELECT) {
bGPDspoint *pt2 = &pt_array[idx];
copy_v3_v3(&pt2->x, &pt->x);
pt2->pressure = pt->pressure;
pt2->strength = pt->strength;
pt->flag &= ~GP_SPOINT_SELECT;
if (clear_point) {
pt->flag |= GP_SPOINT_TAG;
}
/* save stroke */
gps_array[idx] = gps;
idx++;
}
}
gps->flag &= ~GP_STROKE_SELECT;
}
}
}
CTX_DATA_END;
/* project in 2d plane */
int direction = 0;
float(*points2d)[2] = MEM_mallocN(sizeof(*points2d) * totpoints, "GP Stroke temp 2d points");
BKE_gpencil_stroke_2d_flat(pt_array, totpoints, points2d, &direction);
/* calc center */
float center[2] = { 0.0f, 0.0f };
for (i = 0; i < totpoints; i++) {
center[0] += points2d[i][0];
center[1] += points2d[i][1];
}
mul_v2_fl(center, 1.0f / totpoints);
/* calc angle and distance to center for each point */
const float axis[2] = { 1.0f, 0.0f };
float v1[3];
for (i = 0; i < totpoints; i++) {
float ln = len_v2v2(center, points2d[i]);
sub_v2_v2v2(v1, points2d[i], center);
float angle = angle_signed_v2v2(axis, v1);
if (angle < 0.0f) {
angle = fabsf(angle);
}
else {
angle = (M_PI * 2.0) - angle;
}
tGPencilPointCache *sort_pt = &src_array[i];
bGPDspoint *pt2 = &pt_array[i];
copy_v3_v3(&sort_pt->x, &pt2->x);
sort_pt->pressure = pt2->pressure;
sort_pt->strength = pt2->strength;
sort_pt->gps = gps_array[i];
if (mode == GP_MERGE_STROKE) {
sort_pt->factor = angle;
}
else {
sort_pt->factor = (angle * 100000.0f) + ln;
}
}
MEM_SAFE_FREE(points2d);
MEM_SAFE_FREE(gps_array);
MEM_SAFE_FREE(pt_array);
}
/* insert a group of points in destination array */
static int gpencil_insert_to_array(
tGPencilPointCache *src_array, tGPencilPointCache *dst_array, int totpoints,
bGPDstroke *gps_filter, bool reverse, int last)
{
tGPencilPointCache *src_elem = NULL;
tGPencilPointCache *dst_elem = NULL;
int idx = 0;
for (int i = 0; i < totpoints; i++) {
if (!reverse) {
idx = i;
}
else {
idx = totpoints - i - 1;
}
src_elem = &src_array[idx];
/* check if all points or only a stroke */
if ((gps_filter != NULL) && (gps_filter != src_elem->gps)) {
continue;
}
dst_elem = &dst_array[last];
last++;
copy_v3_v3(&dst_elem->x, &src_elem->x);
dst_elem->gps = src_elem->gps;
dst_elem->pressure = src_elem->pressure;
dst_elem->strength = src_elem->strength;
dst_elem->factor = src_elem->factor;
}
return last;
}
/* get first and last point location */
static void gpencil_get_extremes(
tGPencilPointCache *src_array, int totpoints,
bGPDstroke *gps_filter, float *start, float *end)
{
tGPencilPointCache *array_pt = NULL;
int i;
/* find first point */
for (i = 0; i < totpoints; i++) {
array_pt = &src_array[i];
if (gps_filter == array_pt->gps) {
copy_v3_v3(start, &array_pt->x);
break;
}
}
/* find last point */
for (i = totpoints - 1; i >= 0; i--) {
array_pt = &src_array[i];
if (gps_filter == array_pt->gps) {
copy_v3_v3(end, &array_pt->x);
break;
}
}
}
static int gpencil_analyze_strokes(
tGPencilPointCache *src_array, int totstrokes, int totpoints,
tGPencilPointCache *dst_array)
{
int i;
int last = 0;
GHash *all_strokes = BLI_ghash_ptr_new(__func__);
/* add first stroke to array */
tGPencilPointCache *sort_pt = &src_array[0];
bGPDstroke *gps = sort_pt->gps;
last = gpencil_insert_to_array(src_array, dst_array, totpoints, gps, false, last);
float start[3];
float end[3];
float end_prv[3];
gpencil_get_extremes(src_array, totpoints, gps, start, end);
copy_v3_v3(end_prv, end);
BLI_ghash_insert(all_strokes, sort_pt->gps, sort_pt->gps);
/* look for near stroke */
bool loop = (bool)(totstrokes > 1);
while (loop) {
bGPDstroke *gps_next = NULL;
GHash *strokes = BLI_ghash_ptr_new(__func__);
float dist_start = 0.0f;
float dist_end = 0.0f;
float dist = FLT_MAX;
bool reverse = false;
for (i = 0; i < totpoints; i++) {
sort_pt = &src_array[i];
/* avoid dups */
if (BLI_ghash_haskey(all_strokes, sort_pt->gps)) {
continue;
}
if (!BLI_ghash_haskey(strokes, sort_pt->gps)) {
gpencil_get_extremes(src_array, totpoints, sort_pt->gps, start, end);
/* distances to previous end */
dist_start = len_v3v3(end_prv, start);
dist_end = len_v3v3(end_prv, end);
if (dist > dist_start) {
gps_next = sort_pt->gps;
dist = dist_start;
reverse = false;
}
if (dist > dist_end) {
gps_next = sort_pt->gps;
dist = dist_end;
reverse = true;
}
BLI_ghash_insert(strokes, sort_pt->gps, sort_pt->gps);
}
}
BLI_ghash_free(strokes, NULL, NULL);
/* add the stroke to array */
if (gps->next != NULL) {
BLI_ghash_insert(all_strokes, gps_next, gps_next);
last = gpencil_insert_to_array(src_array, dst_array, totpoints, gps_next, reverse, last);
/* replace last end */
sort_pt = &dst_array[last - 1];
copy_v3_v3(end_prv, &sort_pt->x);
}
/* loop exit */
if (last >= totpoints) {
loop = false;
}
}
BLI_ghash_free(all_strokes, NULL, NULL);
return last;
}
static bool gp_strokes_merge_poll(bContext *C)
{
/* only supported with grease pencil objects */
Object *ob = CTX_data_active_object(C);
if ((ob == NULL) || (ob->type != OB_GPENCIL)) {
return false;
}
/* check material */
Material *ma = NULL;
ma = give_current_material(ob, ob->actcol);
if ((ma == NULL) || (ma->gp_style == NULL)) {
return false;
}
/* check hidden or locked materials */
MaterialGPencilStyle *gp_style = ma->gp_style;
if ((gp_style->flag & GP_STYLE_COLOR_HIDE) ||
(gp_style->flag & GP_STYLE_COLOR_LOCKED))
{
return false;
}
/* check layer */
bGPDlayer *gpl = CTX_data_active_gpencil_layer(C);
if ((gpl == NULL) ||
(gpl->flag & GP_LAYER_LOCKED) ||
(gpl->flag & GP_LAYER_HIDE))
{
return false;
}
/* NOTE: this is a bit slower, but is the most accurate... */
return (CTX_DATA_COUNT(C, editable_gpencil_strokes) != 0) && ED_operator_view3d_active(C);
}
static int gp_stroke_merge_exec(bContext *C, wmOperator *op)
{
const int mode = RNA_enum_get(op->ptr, "mode");
const bool clear_point = RNA_boolean_get(op->ptr, "clear_point");
const bool clear_stroke = RNA_boolean_get(op->ptr, "clear_stroke");
Object *ob = CTX_data_active_object(C);
/* sanity checks */
if (!ob || ob->type != OB_GPENCIL) {
return OPERATOR_CANCELLED;
}
bGPdata *gpd = (bGPdata *)ob->data;
bGPDlayer *gpl = CTX_data_active_gpencil_layer(C);
if (gpl == NULL) {
return OPERATOR_CANCELLED;
}
int totstrokes = 0;
int totpoints = 0;
/* count number of strokes and selected points */
gpencil_get_elements_len(C, &totstrokes, &totpoints);
if (totpoints == 0) {
return OPERATOR_CANCELLED;
}
/* calc factor of each point and fill an array with all data */
tGPencilPointCache *sorted_array = NULL;
tGPencilPointCache *original_array = MEM_callocN(sizeof(tGPencilPointCache) * totpoints, __func__);
gpencil_calc_points_factor(C, mode, totpoints, clear_point, clear_stroke, original_array);
/* for strokes analyze strokes and load sorted array */
if (mode == GP_MERGE_STROKE) {
sorted_array = MEM_callocN(sizeof(tGPencilPointCache) * totpoints, __func__);
totpoints = gpencil_analyze_strokes(original_array, totstrokes, totpoints, sorted_array);
}
else {
/* make a copy to sort */
sorted_array = MEM_dupallocN(original_array);
/* sort by factor around center */
qsort(sorted_array, totpoints, sizeof(tGPencilPointCache), gpencil_sort_points);
}
/* prepare the new stroke */
bGPDstroke *gps = gpencil_prepare_stroke(C, op, totpoints);
/* copy original points to final stroke */
gpencil_insert_points_to_stroke(gps, sorted_array, totpoints);
/* dissolve all tagged points */
if ((clear_point) || (clear_stroke)) {
gpencil_dissolve_points(C);
}
/* free memory */
MEM_SAFE_FREE(original_array);
MEM_SAFE_FREE(sorted_array);
/* notifiers */
DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
return OPERATOR_FINISHED;
}
void GPENCIL_OT_stroke_merge(wmOperatorType *ot)
{
static const EnumPropertyItem mode_type[] = {
{GP_MERGE_STROKE, "STROKE", 0, "Stroke", ""},
{GP_MERGE_POINT, "POINT", 0, "Point", ""},
{0, NULL, 0, NULL, NULL}
};
/* identifiers */
ot->name = "Merge Strokes";
ot->idname = "GPENCIL_OT_stroke_merge";
ot->description = "Create a new stroke with the selected stroke points";
/* api callbacks */
ot->exec = gp_stroke_merge_exec;
ot->poll = gp_strokes_merge_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
ot->prop = RNA_def_enum(ot->srna, "mode", mode_type, GP_MERGE_STROKE, "Mode", "");
RNA_def_boolean(ot->srna, "back", 0, "Draw on Back", "Draw new stroke below all previous strokes");
RNA_def_boolean(ot->srna, "additive", 0, "Additive Drawing", "Add to previous drawing");
RNA_def_boolean(ot->srna, "cyclic", 0, "Cyclic", "Close new stroke");
RNA_def_boolean(ot->srna, "clear_point", 0, "Dissolve Points", "Dissolve old selected points");
RNA_def_boolean(ot->srna, "clear_stroke", 0, "Delete Strokes", "Delete old selected strokes");
}
|