Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gpencil.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdb1123e1c7b063b392626194136b201353e66a2 (plain)
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
/*
 * ***** 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) 2008, Blender Foundation
 * This is a new part of Blender
 *
 * Contributor(s): Joshua Leung, Antonio Vazquez
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/blenkernel/intern/gpencil.c
 *  \ingroup bke
 */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
#include "BLI_math_vector.h"
#include "BLI_math_color.h"
#include "BLI_string_utils.h"
#include "BLI_rand.h"
#include "BLI_ghash.h"

#include "BLT_translation.h"

#include "DNA_anim_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_material_types.h"
#include "DNA_gpencil_types.h"
#include "DNA_userdef_types.h"
#include "DNA_scene_types.h"
#include "DNA_object_types.h"

#include "BKE_context.h"
#include "BKE_action.h"
#include "BKE_animsys.h"
#include "BKE_global.h"
#include "BKE_gpencil.h"
#include "BKE_colortools.h"
#include "BKE_icons.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_object.h"
#include "BKE_material.h"

#include "DEG_depsgraph.h"

/* ************************************************** */
/* Draw Engine */

void(*BKE_gpencil_batch_cache_dirty_cb)(bGPdata *gpd) = NULL;
void(*BKE_gpencil_batch_cache_free_cb)(bGPdata *gpd) = NULL;

void BKE_gpencil_batch_cache_dirty(bGPdata *gpd)
{
	if (gpd) {
		DEG_id_tag_update(&gpd->id, OB_RECALC_DATA);
		BKE_gpencil_batch_cache_dirty_cb(gpd);
	}
}

void BKE_gpencil_batch_cache_free(bGPdata *gpd)
{
	if (gpd) {
		BKE_gpencil_batch_cache_free_cb(gpd);
	}
}

/* ************************************************** */
/* Memory Management */

/* clean vertex groups weights */
void BKE_gpencil_free_point_weights(MDeformVert *dvert)
{
	if (dvert == NULL) {
		return;
	}
	MEM_SAFE_FREE(dvert->dw);
}

void BKE_gpencil_free_stroke_weights(bGPDstroke *gps)
{
	if (gps == NULL) {
		return;
	}

	if (gps->dvert == NULL) {
		return;
	}

	for (int i = 0; i < gps->totpoints; i++) {
		MDeformVert *dvert = &gps->dvert[i];
		BKE_gpencil_free_point_weights(dvert);
	}
}

/* free stroke, doesn't unlink from any listbase */
void BKE_gpencil_free_stroke(bGPDstroke *gps)
{
	if (gps == NULL) {
		return;
	}
	/* free stroke memory arrays, then stroke itself */
	if (gps->points) {
		MEM_freeN(gps->points);
	}
	if (gps->dvert) {
		BKE_gpencil_free_stroke_weights(gps);
		MEM_freeN(gps->dvert);
	}
	if (gps->triangles)
		MEM_freeN(gps->triangles);

	MEM_freeN(gps);
}

/* Free strokes belonging to a gp-frame */
bool BKE_gpencil_free_strokes(bGPDframe *gpf)
{
	bGPDstroke *gps_next;
	bool changed = (BLI_listbase_is_empty(&gpf->strokes) == false);

	/* free strokes */
	for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps_next) {
		gps_next = gps->next;
		BKE_gpencil_free_stroke(gps);
	}
	BLI_listbase_clear(&gpf->strokes);

	return changed;
}

/* Free strokes and colors belonging to a gp-frame */
bool BKE_gpencil_free_frame_runtime_data(bGPDframe *derived_gpf)
{
	bGPDstroke *gps_next;
	if (!derived_gpf) {
		return false;
	}

	/* free strokes */
	for (bGPDstroke *gps = derived_gpf->strokes.first; gps; gps = gps_next) {
		gps_next = gps->next;
		BKE_gpencil_free_stroke(gps);
	}
	BLI_listbase_clear(&derived_gpf->strokes);

	MEM_SAFE_FREE(derived_gpf);

	return true;
}

/* Free all of a gp-layer's frames */
void BKE_gpencil_free_frames(bGPDlayer *gpl)
{
	bGPDframe *gpf_next;

	/* error checking */
	if (gpl == NULL) return;

	/* free frames */
	for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf_next) {
		gpf_next = gpf->next;

		/* free strokes and their associated memory */
		BKE_gpencil_free_strokes(gpf);
		BLI_freelinkN(&gpl->frames, gpf);
	}
	gpl->actframe = NULL;
}



/* Free all of the gp-layers for a viewport (list should be &gpd->layers or so) */
void BKE_gpencil_free_layers(ListBase *list)
{
	bGPDlayer *gpl_next;

	/* error checking */
	if (list == NULL) return;

	/* delete layers */
	for (bGPDlayer *gpl = list->first; gpl; gpl = gpl_next) {
		gpl_next = gpl->next;

		/* free layers and their data */
		BKE_gpencil_free_frames(gpl);
		BLI_freelinkN(list, gpl);
	}
}

/* clear all runtime derived data */
static void BKE_gpencil_clear_derived(bGPDlayer *gpl)
{
	GHashIterator gh_iter;

	if (gpl->runtime.derived_data == NULL) {
		return;
	}

	GHASH_ITER(gh_iter, gpl->runtime.derived_data) {
		bGPDframe *gpf = (bGPDframe *)BLI_ghashIterator_getValue(&gh_iter);
		if (gpf) {
			BKE_gpencil_free_frame_runtime_data(gpf);
		}
	}
}

/* Free all of the gp-layers temp data*/
static void BKE_gpencil_free_layers_temp_data(ListBase *list)
{
	bGPDlayer *gpl_next;

	/* error checking */
	if (list == NULL) return;
	/* delete layers */
	for (bGPDlayer *gpl = list->first; gpl; gpl = gpl_next) {
		gpl_next = gpl->next;
		BKE_gpencil_clear_derived(gpl);

		if (gpl->runtime.derived_data) {
			BLI_ghash_free(gpl->runtime.derived_data, NULL, NULL);
			gpl->runtime.derived_data = NULL;
		}
	}
}

/* Free temp gpf derived frames */
void BKE_gpencil_free_derived_frames(bGPdata *gpd)
{
	/* error checking */
	if (gpd == NULL) return;
	for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
		BKE_gpencil_clear_derived(gpl);

		if (gpl->runtime.derived_data) {
			BLI_ghash_free(gpl->runtime.derived_data, NULL, NULL);
			gpl->runtime.derived_data = NULL;
		}
	}
}

/** Free (or release) any data used by this grease pencil (does not free the gpencil itself). */
void BKE_gpencil_free(bGPdata *gpd, bool free_all)
{
	/* clear animation data */
	BKE_animdata_free(&gpd->id, false);

	/* free layers */
	if (free_all) {
		BKE_gpencil_free_layers_temp_data(&gpd->layers);
	}
	BKE_gpencil_free_layers(&gpd->layers);

	/* materials */
	MEM_SAFE_FREE(gpd->mat);

	/* free all data */
	if (free_all) {
		/* clear cache */
		BKE_gpencil_batch_cache_free(gpd);
	}
}

/* ************************************************** */
/* Container Creation */

/* add a new gp-frame to the given layer */
bGPDframe *BKE_gpencil_frame_addnew(bGPDlayer *gpl, int cframe)
{
	bGPDframe *gpf = NULL, *gf = NULL;
	short state = 0;

	/* error checking */
	if (gpl == NULL)
		return NULL;

	/* allocate memory for this frame */
	gpf = MEM_callocN(sizeof(bGPDframe), "bGPDframe");
	gpf->framenum = cframe;

	/* find appropriate place to add frame */
	if (gpl->frames.first) {
		for (gf = gpl->frames.first; gf; gf = gf->next) {
			/* check if frame matches one that is supposed to be added */
			if (gf->framenum == cframe) {
				state = -1;
				break;
			}

			/* if current frame has already exceeded the frame to add, add before */
			if (gf->framenum > cframe) {
				BLI_insertlinkbefore(&gpl->frames, gf, gpf);
				state = 1;
				break;
			}
		}
	}

	/* check whether frame was added successfully */
	if (state == -1) {
		printf("Error: Frame (%d) existed already for this layer. Using existing frame\n", cframe);

		/* free the newly created one, and use the old one instead */
		MEM_freeN(gpf);

		/* return existing frame instead... */
		BLI_assert(gf != NULL);
		gpf = gf;
	}
	else if (state == 0) {
		/* add to end then! */
		BLI_addtail(&gpl->frames, gpf);
	}

	/* return frame */
	return gpf;
}

/* add a copy of the active gp-frame to the given layer */
bGPDframe *BKE_gpencil_frame_addcopy(bGPDlayer *gpl, int cframe)
{
	bGPDframe *new_frame;
	bool found = false;

	/* Error checking/handling */
	if (gpl == NULL) {
		/* no layer */
		return NULL;
	}
	else if (gpl->actframe == NULL) {
		/* no active frame, so just create a new one from scratch */
		return BKE_gpencil_frame_addnew(gpl, cframe);
	}

	/* Create a copy of the frame */
	new_frame = BKE_gpencil_frame_duplicate(gpl->actframe);

	/* Find frame to insert it before */
	for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
		if (gpf->framenum > cframe) {
			/* Add it here */
			BLI_insertlinkbefore(&gpl->frames, gpf, new_frame);

			found = true;
			break;
		}
		else if (gpf->framenum == cframe) {
			/* This only happens when we're editing with framelock on...
			 * - Delete the new frame and don't do anything else here...
			 */
			BKE_gpencil_free_strokes(new_frame);
			MEM_freeN(new_frame);
			new_frame = NULL;

			found = true;
			break;
		}
	}

	if (found == false) {
		/* Add new frame to the end */
		BLI_addtail(&gpl->frames, new_frame);
	}

	/* Ensure that frame is set up correctly, and return it */
	if (new_frame) {
		new_frame->framenum = cframe;
		gpl->actframe = new_frame;
	}

	return new_frame;
}

/* add a new gp-layer and make it the active layer */
bGPDlayer *BKE_gpencil_layer_addnew(bGPdata *gpd, const char *name, bool setactive)
{
	bGPDlayer *gpl;

	/* check that list is ok */
	if (gpd == NULL)
		return NULL;

	/* allocate memory for frame and add to end of list */
	gpl = MEM_callocN(sizeof(bGPDlayer), "bGPDlayer");

	/* add to datablock */
	BLI_addtail(&gpd->layers, gpl);

	/* annotation vs GP Object behaviour is slightly different */
	if (gpd->flag & GP_DATA_ANNOTATIONS) {
		/* set default color of new strokes for this layer */
		copy_v4_v4(gpl->color, U.gpencil_new_layer_col);
		gpl->opacity = 1.0f;

		/* set default thickness of new strokes for this layer */
		gpl->thickness = 3;

	}
	else {
		/* thickness parameter represents "thickness change", not absolute thickness */
		gpl->thickness = 0;
		gpl->opacity = 1.0f;
		/* onion-skinning settings */
		gpl->onion_flag |= GP_LAYER_ONIONSKIN;
	}

	/* auto-name */
	BLI_strncpy(gpl->info, name, sizeof(gpl->info));
	BLI_uniquename(&gpd->layers, gpl,
	               (gpd->flag & GP_DATA_ANNOTATIONS) ? DATA_("Note") : DATA_("GP_Layer"),
	               '.',
	               offsetof(bGPDlayer, info),
	               sizeof(gpl->info));

	/* make this one the active one */
	if (setactive)
		BKE_gpencil_layer_setactive(gpd, gpl);

	/* return layer */
	return gpl;
}

/* add a new gp-datablock */
bGPdata *BKE_gpencil_data_addnew(Main *bmain, const char name[])
{
	bGPdata *gpd;

	/* allocate memory for a new block */
	gpd = BKE_libblock_alloc(bmain, ID_GD, name, 0);

	/* initial settings */
	gpd->flag = (GP_DATA_DISPINFO | GP_DATA_EXPAND);

	/* general flags */
	gpd->flag |= GP_DATA_VIEWALIGN;

	/* GP object specific settings */
	ARRAY_SET_ITEMS(gpd->line_color, 0.6f, 0.6f, 0.6f, 0.5f);

	gpd->xray_mode = GP_XRAY_3DSPACE;
	gpd->runtime.batch_cache_data = NULL;
	gpd->pixfactor = GP_DEFAULT_PIX_FACTOR;

	/* onion-skinning settings (datablock level) */
	gpd->onion_flag |= (GP_ONION_GHOST_PREVCOL | GP_ONION_GHOST_NEXTCOL);
	gpd->onion_flag |= GP_ONION_FADE;
	gpd->onion_mode = GP_ONION_MODE_RELATIVE;
	gpd->onion_factor = 0.5f;
	ARRAY_SET_ITEMS(gpd->gcolor_prev, 0.145098f, 0.419608f, 0.137255f); /* green */
	ARRAY_SET_ITEMS(gpd->gcolor_next, 0.125490f, 0.082353f, 0.529412f); /* blue */
	gpd->gstep = 1;
	gpd->gstep_next = 1;

	return gpd;
}


/* ************************************************** */
/* Primitive Creation */
/* Utilities for easier bulk-creation of geometry */

/**
 * Populate stroke with point data from data buffers
 *
 * \param array Flat array of point data values. Each entry has GP_PRIM_DATABUF_SIZE values
 * \param mat   4x4 transform matrix to transform points into the right coordinate space
 */
void BKE_gpencil_stroke_add_points(bGPDstroke *gps, const float *array, const int totpoints, const float mat[4][4])
{
	for (int i = 0; i < totpoints; i++) {
		bGPDspoint *pt = &gps->points[i];
		const int x = GP_PRIM_DATABUF_SIZE * i;

		pt->x = array[x];
		pt->y = array[x + 1];
		pt->z = array[x + 2];
		mul_m4_v3(mat, &pt->x);

		pt->pressure = array[x + 3];
		pt->strength = array[x + 4];
	}
}

/* Create a new stroke, with pre-allocated data buffers */
bGPDstroke *BKE_gpencil_add_stroke(bGPDframe *gpf, int mat_idx, int totpoints, short thickness)
{
	/* allocate memory for a new stroke */
	bGPDstroke *gps = MEM_callocN(sizeof(bGPDstroke), "gp_stroke");

	gps->thickness = thickness * 25;
	gps->inittime = 0;

	/* enable recalculation flag by default */
	gps->flag = GP_STROKE_RECALC_CACHES | GP_STROKE_3DSPACE;

	gps->totpoints = totpoints;
	gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points");
	gps->dvert = MEM_callocN(sizeof(MDeformVert) * gps->totpoints, "gp_stroke_weights");

	/* initialize triangle memory to dummy data */
	gps->triangles = MEM_callocN(sizeof(bGPDtriangle), "GP Stroke triangulation");
	gps->flag |= GP_STROKE_RECALC_CACHES;
	gps->tot_triangles = 0;

	gps->mat_nr = mat_idx;

	/* add to frame */
	BLI_addtail(&gpf->strokes, gps);

	return gps;
}


/* ************************************************** */
/* Data Duplication */

/* make a copy of a given gpencil weights */
void BKE_gpencil_stroke_weights_duplicate(bGPDstroke *gps_src, bGPDstroke *gps_dst)
{
	if (gps_src == NULL) {
		return;
	}
	BLI_assert(gps_src->totpoints == gps_dst->totpoints);

	if ((gps_src->dvert == NULL) || (gps_dst->dvert == NULL)) {
		return;
	}

	for (int i = 0; i < gps_src->totpoints; i++) {
		MDeformVert *dvert_src = &gps_src->dvert[i];
		MDeformVert *dvert_dst = &gps_dst->dvert[i];
		if (dvert_src->totweight > 0) {
			dvert_dst->dw = MEM_dupallocN(dvert_src->dw);
		}
		else {
			dvert_dst->dw = NULL;
		}

	}
}

/* make a copy of a given gpencil stroke */
bGPDstroke *BKE_gpencil_stroke_duplicate(bGPDstroke *gps_src)
{
	bGPDstroke *gps_dst = NULL;

	gps_dst = MEM_dupallocN(gps_src);
	gps_dst->prev = gps_dst->next = NULL;

	gps_dst->points = MEM_dupallocN(gps_src->points);

	gps_dst->dvert = MEM_dupallocN(gps_src->dvert);
	BKE_gpencil_stroke_weights_duplicate(gps_src, gps_dst);

	/* Don't clear triangles, so that modifier evaluation can just use
	 * this without extra work first. Most places that need to force
	 * this data to get recalculated will destroy the data anyway though.
	 */
	gps_dst->triangles = MEM_dupallocN(gps_dst->triangles);
	/* gps_dst->flag |= GP_STROKE_RECALC_CACHES; */

	/* return new stroke */
	return gps_dst;
}

/* make a copy of a given gpencil frame */
bGPDframe *BKE_gpencil_frame_duplicate(const bGPDframe *gpf_src)
{
	bGPDstroke *gps_dst = NULL;
	bGPDframe *gpf_dst;

	/* error checking */
	if (gpf_src == NULL) {
		return NULL;
	}

	/* make a copy of the source frame */
	gpf_dst = MEM_dupallocN(gpf_src);
	gpf_dst->prev = gpf_dst->next = NULL;

	/* copy strokes */
	BLI_listbase_clear(&gpf_dst->strokes);
	for (bGPDstroke *gps_src = gpf_src->strokes.first; gps_src; gps_src = gps_src->next) {
		/* make copy of source stroke */
		gps_dst = BKE_gpencil_stroke_duplicate(gps_src);
		BLI_addtail(&gpf_dst->strokes, gps_dst);
	}

	/* return new frame */
	return gpf_dst;
}

/* make a copy of strokes between gpencil frames */
void BKE_gpencil_frame_copy_strokes(bGPDframe *gpf_src, struct bGPDframe *gpf_dst)
{
	bGPDstroke *gps_dst = NULL;
	/* error checking */
	if ((gpf_src == NULL) || (gpf_dst == NULL)) {
		return;
	}

	/* copy strokes */
	BLI_listbase_clear(&gpf_dst->strokes);
	for (bGPDstroke *gps_src = gpf_src->strokes.first; gps_src; gps_src = gps_src->next) {
		/* make copy of source stroke */
		gps_dst = BKE_gpencil_stroke_duplicate(gps_src);
		BLI_addtail(&gpf_dst->strokes, gps_dst);
	}
}

/* make a copy of a given gpencil layer */
bGPDlayer *BKE_gpencil_layer_duplicate(const bGPDlayer *gpl_src)
{
	const bGPDframe *gpf_src;
	bGPDframe *gpf_dst;
	bGPDlayer *gpl_dst;

	/* error checking */
	if (gpl_src == NULL) {
		return NULL;
	}

	/* make a copy of source layer */
	gpl_dst = MEM_dupallocN(gpl_src);
	gpl_dst->prev = gpl_dst->next = NULL;
	gpl_dst->runtime.derived_data = NULL;

	/* copy frames */
	BLI_listbase_clear(&gpl_dst->frames);
	for (gpf_src = gpl_src->frames.first; gpf_src; gpf_src = gpf_src->next) {
		/* make a copy of source frame */
		gpf_dst = BKE_gpencil_frame_duplicate(gpf_src);
		BLI_addtail(&gpl_dst->frames, gpf_dst);

		/* if source frame was the current layer's 'active' frame, reassign that too */
		if (gpf_src == gpl_dst->actframe)
			gpl_dst->actframe = gpf_dst;
	}

	/* return new layer */
	return gpl_dst;
}

/**
 * Only copy internal data of GreasePencil ID from source to already allocated/initialized destination.
 * You probably never want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
 *
 * WARNING! This function will not handle ID user count!
 *
 * \param flag  Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
 */
void BKE_gpencil_copy_data(bGPdata *gpd_dst, const bGPdata *gpd_src, const int UNUSED(flag))
{
	/* cache data is not duplicated */
	gpd_dst->runtime.batch_cache_data = NULL;

	/* duplicate material array */
	if (gpd_src->mat) {
		gpd_dst->mat = MEM_dupallocN(gpd_src->mat);
	}

	/* copy layers */
	BLI_listbase_clear(&gpd_dst->layers);
	for (const bGPDlayer *gpl_src = gpd_src->layers.first; gpl_src; gpl_src = gpl_src->next) {
		/* make a copy of source layer and its data */
		bGPDlayer *gpl_dst = BKE_gpencil_layer_duplicate(gpl_src);  /* TODO here too could add unused flags... */
		BLI_addtail(&gpd_dst->layers, gpl_dst);
	}

}

/* Standard API to make a copy of GP datablock, separate from copying its data */
bGPdata *BKE_gpencil_copy(Main *bmain, const bGPdata *gpd)
{
	bGPdata *gpd_copy;
	BKE_id_copy_ex(bmain, &gpd->id, (ID **)&gpd_copy, 0, false);
	return gpd_copy;
}

/* make a copy of a given gpencil datablock */
// XXX: Should this be deprecated?
bGPdata *BKE_gpencil_data_duplicate(Main *bmain, const bGPdata *gpd_src, bool internal_copy)
{
	bGPdata *gpd_dst;

	/* Yuck and super-uber-hyper yuck!!!
	 * Should be replaceable with a no-main copy (LIB_ID_COPY_NO_MAIN etc.), but not sure about it,
	 * so for now keep old code for that one. */

	/* error checking */
	if (gpd_src == NULL) {
		return NULL;
	}

	if (internal_copy) {
		/* make a straight copy for undo buffers used during stroke drawing */
		gpd_dst = MEM_dupallocN(gpd_src);
	}
	else {
		BLI_assert(bmain != NULL);
		BKE_id_copy_ex(bmain, &gpd_src->id, (ID **)&gpd_dst, 0, false);
		gpd_dst->runtime.batch_cache_data = NULL;
	}

	/* Copy internal data (layers, etc.) */
	BKE_gpencil_copy_data(gpd_dst, gpd_src, 0);

	/* return new */
	return gpd_dst;
}

void BKE_gpencil_make_local(Main *bmain, bGPdata *gpd, const bool lib_local)
{
	BKE_id_make_local_generic(bmain, &gpd->id, true, lib_local);
}

/* ************************************************** */
/* GP Stroke API */

/* ensure selection status of stroke is in sync with its points */
void BKE_gpencil_stroke_sync_selection(bGPDstroke *gps)
{
	bGPDspoint *pt;
	int i;

	/* error checking */
	if (gps == NULL)
		return;

	/* we'll stop when we find the first selected point,
	 * so initially, we must deselect
	 */
	gps->flag &= ~GP_STROKE_SELECT;

	for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
		if (pt->flag & GP_SPOINT_SELECT) {
			gps->flag |= GP_STROKE_SELECT;
			break;
		}
	}
}

/* ************************************************** */
/* GP Frame API */

/* delete the last stroke of the given frame */
void BKE_gpencil_frame_delete_laststroke(bGPDlayer *gpl, bGPDframe *gpf)
{
	bGPDstroke *gps = (gpf) ? gpf->strokes.last : NULL;
	int cfra = (gpf) ? gpf->framenum : 0; /* assume that the current frame was not locked */

	/* error checking */
	if (ELEM(NULL, gpf, gps))
		return;

	/* free the stroke and its data */
	if (gps->points) {
		MEM_freeN(gps->points);
	}
	if (gps->dvert) {
		BKE_gpencil_free_stroke_weights(gps);
		MEM_freeN(gps->dvert);
	}
	MEM_freeN(gps->triangles);
	BLI_freelinkN(&gpf->strokes, gps);

	/* if frame has no strokes after this, delete it */
	if (BLI_listbase_is_empty(&gpf->strokes)) {
		BKE_gpencil_layer_delframe(gpl, gpf);
		BKE_gpencil_layer_getframe(gpl, cfra, 0);
	}
}

/* ************************************************** */
/* GP Layer API */

/* Check if the given layer is able to be edited or not */
bool gpencil_layer_is_editable(const bGPDlayer *gpl)
{
	/* Sanity check */
	if (gpl == NULL)
		return false;

	/* Layer must be: Visible + Editable */
	if ((gpl->flag & (GP_LAYER_HIDE | GP_LAYER_LOCKED)) == 0) {
		/* Opacity must be sufficiently high that it is still "visible"
		 * Otherwise, it's not really "visible" to the user, so no point editing...
		 */
		if (gpl->opacity > GPENCIL_ALPHA_OPACITY_THRESH) {
			return true;
		}
	}

	/* Something failed */
	return false;
}

/* Look up the gp-frame on the requested frame number, but don't add a new one */
bGPDframe *BKE_gpencil_layer_find_frame(bGPDlayer *gpl, int cframe)
{
	bGPDframe *gpf;

	/* Search in reverse order, since this is often used for playback/adding,
	 * where it's less likely that we're interested in the earlier frames
	 */
	for (gpf = gpl->frames.last; gpf; gpf = gpf->prev) {
		if (gpf->framenum == cframe) {
			return gpf;
		}
	}

	return NULL;
}

/* get the appropriate gp-frame from a given layer
 *	- this sets the layer's actframe var (if allowed to)
 *	- extension beyond range (if first gp-frame is after all frame in interest and cannot add)
 */
bGPDframe *BKE_gpencil_layer_getframe(bGPDlayer *gpl, int cframe, eGP_GetFrame_Mode addnew)
{
	bGPDframe *gpf = NULL;
	short found = 0;

	/* error checking */
	if (gpl == NULL) return NULL;

	/* check if there is already an active frame */
	if (gpl->actframe) {
		gpf = gpl->actframe;

		/* do not allow any changes to layer's active frame if layer is locked from changes
		 * or if the layer has been set to stay on the current frame
		 */
		if (gpl->flag & GP_LAYER_FRAMELOCK)
			return gpf;
		/* do not allow any changes to actframe if frame has painting tag attached to it */
		if (gpf->flag & GP_FRAME_PAINT)
			return gpf;

		/* try to find matching frame */
		if (gpf->framenum < cframe) {
			for (; gpf; gpf = gpf->next) {
				if (gpf->framenum == cframe) {
					found = 1;
					break;
				}
				else if ((gpf->next) && (gpf->next->framenum > cframe)) {
					found = 1;
					break;
				}
			}

			/* set the appropriate frame */
			if (addnew) {
				if ((found) && (gpf->framenum == cframe))
					gpl->actframe = gpf;
				else if (addnew == GP_GETFRAME_ADD_COPY)
					gpl->actframe = BKE_gpencil_frame_addcopy(gpl, cframe);
				else
					gpl->actframe = BKE_gpencil_frame_addnew(gpl, cframe);
			}
			else if (found)
				gpl->actframe = gpf;
			else
				gpl->actframe = gpl->frames.last;
		}
		else {
			for (; gpf; gpf = gpf->prev) {
				if (gpf->framenum <= cframe) {
					found = 1;
					break;
				}
			}

			/* set the appropriate frame */
			if (addnew) {
				if ((found) && (gpf->framenum == cframe))
					gpl->actframe = gpf;
				else if (addnew == GP_GETFRAME_ADD_COPY)
					gpl->actframe = BKE_gpencil_frame_addcopy(gpl, cframe);
				else
					gpl->actframe = BKE_gpencil_frame_addnew(gpl, cframe);
			}
			else if (found)
				gpl->actframe = gpf;
			else
				gpl->actframe = gpl->frames.first;
		}
	}
	else if (gpl->frames.first) {
		/* check which of the ends to start checking from */
		const int first = ((bGPDframe *)(gpl->frames.first))->framenum;
		const int last = ((bGPDframe *)(gpl->frames.last))->framenum;

		if (abs(cframe - first) > abs(cframe - last)) {
			/* find gp-frame which is less than or equal to cframe */
			for (gpf = gpl->frames.last; gpf; gpf = gpf->prev) {
				if (gpf->framenum <= cframe) {
					found = 1;
					break;
				}
			}
		}
		else {
			/* find gp-frame which is less than or equal to cframe */
			for (gpf = gpl->frames.first; gpf; gpf = gpf->next) {
				if (gpf->framenum <= cframe) {
					found = 1;
					break;
				}
			}
		}

		/* set the appropriate frame */
		if (addnew) {
			if ((found) && (gpf->framenum == cframe))
				gpl->actframe = gpf;
			else
				gpl->actframe = BKE_gpencil_frame_addnew(gpl, cframe);
		}
		else if (found)
			gpl->actframe = gpf;
		else {
			/* unresolved errogenous situation! */
			printf("Error: cannot find appropriate gp-frame\n");
			/* gpl->actframe should still be NULL */
		}
	}
	else {
		/* currently no frames (add if allowed to) */
		if (addnew)
			gpl->actframe = BKE_gpencil_frame_addnew(gpl, cframe);
		else {
			/* don't do anything... this may be when no frames yet! */
			/* gpl->actframe should still be NULL */
		}
	}

	/* return */
	return gpl->actframe;
}

/* delete the given frame from a layer */
bool BKE_gpencil_layer_delframe(bGPDlayer *gpl, bGPDframe *gpf)
{
	bool changed = false;

	/* error checking */
	if (ELEM(NULL, gpl, gpf))
		return false;

	/* if this frame was active, make the previous frame active instead
	 * since it's tricky to set active frame otherwise
	 */
	if (gpl->actframe == gpf)
		gpl->actframe = gpf->prev;

	/* free the frame and its data */
	changed = BKE_gpencil_free_strokes(gpf);
	BLI_freelinkN(&gpl->frames, gpf);

	return changed;
}

/* get the active gp-layer for editing */
bGPDlayer *BKE_gpencil_layer_getactive(bGPdata *gpd)
{
	bGPDlayer *gpl;

	/* error checking */
	if (ELEM(NULL, gpd, gpd->layers.first))
		return NULL;

	/* loop over layers until found (assume only one active) */
	for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
		if (gpl->flag & GP_LAYER_ACTIVE)
			return gpl;
	}

	/* no active layer found */
	return NULL;
}

/* set the active gp-layer */
void BKE_gpencil_layer_setactive(bGPdata *gpd, bGPDlayer *active)
{
	bGPDlayer *gpl;

	/* error checking */
	if (ELEM(NULL, gpd, gpd->layers.first, active))
		return;

	/* loop over layers deactivating all */
	for (gpl = gpd->layers.first; gpl; gpl = gpl->next)
		gpl->flag &= ~GP_LAYER_ACTIVE;

	/* set as active one */
	active->flag |= GP_LAYER_ACTIVE;
}

/* delete the active gp-layer */
void BKE_gpencil_layer_delete(bGPdata *gpd, bGPDlayer *gpl)
{
	/* error checking */
	if (ELEM(NULL, gpd, gpl))
		return;

	/* free layer */
	BKE_gpencil_free_frames(gpl);

	/* free icon providing preview of icon color */
	BKE_icon_delete(gpl->runtime.icon_id);

	/* free derived data */
	BKE_gpencil_clear_derived(gpl);
	if (gpl->runtime.derived_data) {
		BLI_ghash_free(gpl->runtime.derived_data, NULL, NULL);
		gpl->runtime.derived_data = NULL;
	}

	BLI_freelinkN(&gpd->layers, gpl);
}

Material *BKE_gpencil_get_material_from_brush(Brush *brush)
{
	Material *ma = NULL;

	if ((brush != NULL) && (brush->gpencil_settings != NULL) &&
	    (brush->gpencil_settings->material != NULL))
	{
		ma = brush->gpencil_settings->material;
	}

	return ma;
}

/* Get active color, and add all default settings if we don't find anything */
Material *BKE_gpencil_material_ensure(Main *bmain, Object *ob)
{
	Material *ma = NULL;

	/* sanity checks */
	if (ELEM(NULL, bmain, ob))
		return NULL;

	ma = give_current_material(ob, ob->actcol);
	if (ma == NULL) {
		if (ob->totcol == 0) {
			BKE_object_material_slot_add(bmain, ob);
		}
		ma = BKE_material_add_gpencil(bmain, DATA_("Material"));
		assign_material(bmain, ob, ma, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
	}
	else if (ma->gp_style == NULL) {
		BKE_material_init_gpencil_settings(ma);
	}

	return ma;
}

/* ************************************************** */
/* GP Object - Boundbox Support */

/**
 * Get min/max coordinate bounds for single stroke
 * \return Returns whether we found any selected points
 */
bool BKE_gpencil_stroke_minmax(
        const bGPDstroke *gps, const bool use_select,
        float r_min[3], float r_max[3])
{
	const bGPDspoint *pt;
	int i;
	bool changed = false;

	if (ELEM(NULL, gps, r_min, r_max))
		return false;

	for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
		if ((use_select == false) || (pt->flag & GP_SPOINT_SELECT)) {
			minmax_v3v3_v3(r_min, r_max, &pt->x);
			changed = true;
		}
	}
	return changed;
}

/* get min/max bounds of all strokes in GP datablock */
static void gpencil_minmax(bGPdata *gpd, float r_min[3], float r_max[3])
{
	INIT_MINMAX(r_min, r_max);

	if (gpd == NULL)
		return;

	for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
		bGPDframe *gpf = gpl->actframe;

		if (gpf != NULL) {
			for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
				BKE_gpencil_stroke_minmax(gps, false, r_min, r_max);
			}
		}
	}
}

/* compute center of bounding box */
void BKE_gpencil_centroid_3D(bGPdata *gpd, float r_centroid[3])
{
	float min[3], max[3], tot[3];

	gpencil_minmax(gpd, min, max);

	add_v3_v3v3(tot, min, max);
	mul_v3_v3fl(r_centroid, tot, 0.5f);
}


/* create bounding box values */
static void boundbox_gpencil(Object *ob)
{
	BoundBox *bb;
	bGPdata *gpd;
	float min[3], max[3];

	if (ob->bb == NULL) {
		ob->bb = MEM_callocN(sizeof(BoundBox), "GPencil boundbox");
	}

	bb  = ob->bb;
	gpd = ob->data;

	gpencil_minmax(gpd, min, max);
	BKE_boundbox_init_from_minmax(bb, min, max);

	bb->flag &= ~BOUNDBOX_DIRTY;
}

/* get bounding box */
BoundBox *BKE_gpencil_boundbox_get(Object *ob)
{
	bGPdata *gpd;

	if (ELEM(NULL, ob, ob->data))
		return NULL;

	gpd = ob->data;
	if ((ob->bb) && ((ob->bb->flag & BOUNDBOX_DIRTY) == 0) &&
	    ((gpd->flag & GP_DATA_CACHE_IS_DIRTY) == 0))
	{
		return ob->bb;
	}

	boundbox_gpencil(ob);

	return ob->bb;
}

/* ************************************************** */
/* Apply Transforms */

void BKE_gpencil_transform(bGPdata *gpd, float mat[4][4])
{
	if (gpd == NULL)
		return;

	for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
		/* FIXME: For now, we just skip parented layers.
		 * Otherwise, we have to update each frame to find
		 * the current parent position/effects.
		 */
		if (gpl->parent) {
			continue;
		}

		for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
			for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
				bGPDspoint *pt;
				int i;

				for (pt = gps->points, i = 0; i < gps->totpoints; pt++, i++) {
					mul_m4_v3(mat, &pt->x);
				}

				/* TODO: Do we need to do this? distortion may mean we need to re-triangulate */
				gps->flag |= GP_STROKE_RECALC_CACHES;
				gps->tot_triangles = 0;
			}
		}
	}

}

/* ************************************************** */
/* GP Object - Vertex Groups */

/* remove a vertex group */
void BKE_gpencil_vgroup_remove(Object *ob, bDeformGroup *defgroup)
{
	bGPdata *gpd = ob->data;
	MDeformVert *dvert = NULL;
	MDeformWeight *gpw = NULL;
	const int def_nr = BLI_findindex(&ob->defbase, defgroup);

	/* Remove points data */
	if (gpd) {
		for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
			for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
				for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
					for (int i = 0; i < gps->totpoints; i++) {
						dvert = &gps->dvert[i];
						for (int i2 = 0; i2 < dvert->totweight; i2++) {
							gpw = &dvert->dw[i2];
							if (gpw->def_nr == def_nr) {
								BKE_gpencil_vgroup_remove_point_weight(dvert, def_nr);
							}
							/* if index is greater, must be moved one back */
							if (gpw->def_nr > def_nr) {
								gpw->def_nr--;
							}
						}
					}
				}
			}
		}
	}

	/* Remove the group */
	BLI_freelinkN(&ob->defbase, defgroup);
}

/* add a new weight */
MDeformWeight *BKE_gpencil_vgroup_add_point_weight(MDeformVert *dvert, int index, float weight)
{
	MDeformWeight *new_gpw = NULL;
	MDeformWeight *tmp_gpw;

	/* need to verify if was used before to update */
	for (int i = 0; i < dvert->totweight; i++) {
		tmp_gpw = &dvert->dw[i];
		if (tmp_gpw->def_nr == index) {
			tmp_gpw->weight = weight;
			return tmp_gpw;
		}
	}

	dvert->totweight++;
	if (dvert->totweight == 1) {
		dvert->dw = MEM_callocN(sizeof(MDeformWeight), "gp_weight");
	}
	else {
		dvert->dw = MEM_reallocN(dvert->dw, sizeof(MDeformWeight) * dvert->totweight);
	}
	new_gpw = &dvert->dw[dvert->totweight - 1];
	new_gpw->def_nr = index;
	new_gpw->weight = weight;

	return new_gpw;
}

/* return the weight if use index  or -1*/
float BKE_gpencil_vgroup_use_index(MDeformVert *dvert, int index)
{
	MDeformWeight *gpw;
	for (int i = 0; i < dvert->totweight; i++) {
		gpw = &dvert->dw[i];
		if (gpw->def_nr == index) {
			return gpw->weight;
		}
	}
	return -1.0f;
}

/* add a new weight */
bool BKE_gpencil_vgroup_remove_point_weight(MDeformVert *dvert, int index)
{
	int e = 0;

	if (BKE_gpencil_vgroup_use_index(dvert, index) < 0.0f) {
		return false;
	}

	/* if the array get empty, exit */
	if (dvert->totweight == 1) {
		dvert->totweight = 0;
		MEM_SAFE_FREE(dvert->dw);
		return true;
	}

	/* realloc weights */
	MDeformWeight *tmp = MEM_dupallocN(dvert->dw);
	MEM_SAFE_FREE(dvert->dw);
	dvert->dw = MEM_callocN(sizeof(MDeformWeight) * dvert->totweight - 1, "gp_weights");

	for (int x = 0; x < dvert->totweight; x++) {
		MDeformWeight *gpw = &tmp[e];
		MDeformWeight *final_gpw = &dvert->dw[e];
		if (gpw->def_nr != index) {
			final_gpw->def_nr = gpw->def_nr;
			final_gpw->weight = gpw->weight;
			e++;
		}
	}
	MEM_SAFE_FREE(tmp);
	dvert->totweight--;

	return true;
}


/* ************************************************** */

/**
 * Apply smooth to stroke point
 * \param gps              Stroke to smooth
 * \param i                Point index
 * \param inf              Amount of smoothing to apply
 */
bool BKE_gpencil_smooth_stroke(bGPDstroke *gps, int i, float inf)
{
	bGPDspoint *pt = &gps->points[i];
	// float pressure = 0.0f;
	float sco[3] = { 0.0f };

	/* Do nothing if not enough points to smooth out */
	if (gps->totpoints <= 2) {
		return false;
	}

	/* Only affect endpoints by a fraction of the normal strength,
	* to prevent the stroke from shrinking too much
	*/
	if ((i == 0) || (i == gps->totpoints - 1)) {
		inf *= 0.1f;
	}

	/* Compute smoothed coordinate by taking the ones nearby */
	/* XXX: This is potentially slow, and suffers from accumulation error as earlier points are handled before later ones */
	{
		// XXX: this is hardcoded to look at 2 points on either side of the current one (i.e. 5 items total)
		const int   steps = 2;
		const float average_fac = 1.0f / (float)(steps * 2 + 1);
		int step;

		/* add the point itself */
		madd_v3_v3fl(sco, &pt->x, average_fac);

		/* n-steps before/after current point */
		// XXX: review how the endpoints are treated by this algorithm
		// XXX: falloff measures should also introduce some weighting variations, so that further-out points get less weight
		for (step = 1; step <= steps; step++) {
			bGPDspoint *pt1, *pt2;
			int before = i - step;
			int after = i + step;

			CLAMP_MIN(before, 0);
			CLAMP_MAX(after, gps->totpoints - 1);

			pt1 = &gps->points[before];
			pt2 = &gps->points[after];

			/* add both these points to the average-sum (s += p[i]/n) */
			madd_v3_v3fl(sco, &pt1->x, average_fac);
			madd_v3_v3fl(sco, &pt2->x, average_fac);

		}
	}

	/* Based on influence factor, blend between original and optimal smoothed coordinate */
	interp_v3_v3v3(&pt->x, &pt->x, sco, inf);

	return true;
}

/**
 * Apply smooth for strength to stroke point */
bool BKE_gpencil_smooth_stroke_strength(bGPDstroke *gps, int point_index, float influence)
{
	bGPDspoint *ptb = &gps->points[point_index];

	/* Do nothing if not enough points */
	if (gps->totpoints <= 2) {
		return false;
	}

	/* Compute theoretical optimal value using distances */
	bGPDspoint *pta, *ptc;
	int before = point_index - 1;
	int after = point_index + 1;

	CLAMP_MIN(before, 0);
	CLAMP_MAX(after, gps->totpoints - 1);

	pta = &gps->points[before];
	ptc = &gps->points[after];

	/* the optimal value is the corresponding to the interpolation of the strength
	*  at the distance of point b
	*/
	const float fac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x);
	const float optimal = (1.0f - fac) * pta->strength + fac * ptc->strength;

	/* Based on influence factor, blend between original and optimal */
	ptb->strength = (1.0f - influence) * ptb->strength + influence * optimal;

	return true;
}

/**
 * Apply smooth for thickness to stroke point (use pressure) */
bool BKE_gpencil_smooth_stroke_thickness(bGPDstroke *gps, int point_index, float influence)
{
	bGPDspoint *ptb = &gps->points[point_index];

	/* Do nothing if not enough points */
	if (gps->totpoints <= 2) {
		return false;
	}

	/* Compute theoretical optimal value using distances */
	bGPDspoint *pta, *ptc;
	int before = point_index - 1;
	int after = point_index + 1;

	CLAMP_MIN(before, 0);
	CLAMP_MAX(after, gps->totpoints - 1);

	pta = &gps->points[before];
	ptc = &gps->points[after];

	/* the optimal value is the corresponding to the interpolation of the pressure
	*  at the distance of point b
	*/
	float fac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x);
	float optimal = interpf(ptc->pressure, pta->pressure, fac);

	/* Based on influence factor, blend between original and optimal */
	ptb->pressure = interpf(optimal, ptb->pressure, influence);

	return true;
}

/**
* Apply smooth for UV rotation to stroke point (use pressure) */
bool BKE_gpencil_smooth_stroke_uv(bGPDstroke *gps, int point_index, float influence)
{
	bGPDspoint *ptb = &gps->points[point_index];

	/* Do nothing if not enough points */
	if (gps->totpoints <= 2) {
		return false;
	}

	/* Compute theoretical optimal value */
	bGPDspoint *pta, *ptc;
	int before = point_index - 1;
	int after = point_index + 1;

	CLAMP_MIN(before, 0);
	CLAMP_MAX(after, gps->totpoints - 1);

	pta = &gps->points[before];
	ptc = &gps->points[after];

	/* the optimal value is the corresponding to the interpolation of the pressure
	*  at the distance of point b
	*/
	float fac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x);
	float optimal = interpf(ptc->uv_rot, pta->uv_rot, fac);

	/* Based on influence factor, blend between original and optimal */
	ptb->uv_rot = interpf(optimal, ptb->uv_rot, influence);
	CLAMP(ptb->uv_rot, -M_PI_2, M_PI_2);

	return true;
}

/**
 * Get range of selected frames in layer.
 * Always the active frame is considered as selected, so if no more selected the range
 * will be equal to the current active frame.
 * \param gpl              Layer
 * \param r_initframe      Number of first selected frame
 * \param r_endframe       Number of last selected frame
 */
void BKE_gpencil_get_range_selected(bGPDlayer *gpl, int *r_initframe, int *r_endframe)
{
	*r_initframe = gpl->actframe->framenum;
	*r_endframe = gpl->actframe->framenum;

	for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
		if (gpf->flag & GP_FRAME_SELECT) {
			if (gpf->framenum < *r_initframe) {
				*r_initframe = gpf->framenum;
			}
			if (gpf->framenum > *r_endframe) {
				*r_endframe = gpf->framenum;
			}
		}
	}
}

/**
 * Get Falloff factor base on frame range
 * \param gpf          Frame
 * \param actnum       Number of active frame in layer
 * \param f_init       Number of first selected frame
 * \param f_end        Number of last selected frame
 * \param cur_falloff  Curve with falloff factors
 */
float BKE_gpencil_multiframe_falloff_calc(bGPDframe *gpf, int actnum, int f_init, int f_end, CurveMapping *cur_falloff)
{
	float fnum = 0.5f; /* default mid curve */
	float value;

	/* frames to the right of the active frame */
	if (gpf->framenum < actnum) {
		fnum = (float)(gpf->framenum - f_init) / (actnum - f_init);
		fnum *= 0.5f;
		value = curvemapping_evaluateF(cur_falloff, 0, fnum);
	}
	/* frames to the left of the active frame */
	else if (gpf->framenum > actnum) {
		fnum = (float)(gpf->framenum - actnum) / (f_end - actnum);
		fnum *= 0.5f;
		value = curvemapping_evaluateF(cur_falloff, 0, fnum + 0.5f);
	}
	else {
		value = 1.0f;
	}

	return value;
}

/* remove strokes using a material */
void BKE_gpencil_material_index_remove(bGPdata *gpd, int index)
{
	bGPDstroke *gps, *gpsn;

		for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
			for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
				for (gps = gpf->strokes.first; gps; gps = gpsn) {
					gpsn = gps->next;
					if (gps->mat_nr == index) {
						if (gps->points) {
							MEM_freeN(gps->points);
						}
						if (gps->dvert) {
							BKE_gpencil_free_stroke_weights(gps);
							MEM_freeN(gps->dvert);
						}
						if (gps->triangles) MEM_freeN(gps->triangles);
						BLI_freelinkN(&gpf->strokes, gps);
					}
					else {
						/* reassign strokes */
						if (gps->mat_nr > index) {
							gps->mat_nr--;
						}
					}
				}
			}
		}
}

void BKE_gpencil_material_remap(struct bGPdata *gpd, const unsigned int *remap, unsigned int remap_len)
{
	const short remap_len_short = (short)remap_len;

#define MAT_NR_REMAP(n) \
	if (n < remap_len_short) { \
		BLI_assert(n >= 0 && remap[n] < remap_len_short); \
		n = remap[n]; \
	} ((void)0)

	for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
		for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
			for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
				/* reassign strokes */
				MAT_NR_REMAP(gps->mat_nr);
			}
		}
	}

#undef MAT_NR_REMAP

}

/* statistics functions */
void BKE_gpencil_stats_update(bGPdata *gpd)
{
	gpd->totlayer = 0;
	gpd->totframe = 0;
	gpd->totstroke = 0;
	gpd->totpoint = 0;

	for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
		gpd->totlayer++;
		for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
			gpd->totframe++;
			for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
				gpd->totstroke++;
				gpd->totpoint += gps->totpoints;
			}
		}
	}

}

/* get material index */
int BKE_gpencil_get_material_index(Object *ob, Material *ma)
{
	short *totcol = give_totcolp(ob);
	Material *read_ma = NULL;
	for (short i = 0; i < *totcol; i++) {
		read_ma = give_current_material(ob, i + 1);
		if (ma == read_ma) {
			return i + 1;
		}
	}

	return 0;
}