forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bezier.cpp
3702 lines (3241 loc) · 122 KB
/
Bezier.cpp
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
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron 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.
*
* Natron 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 Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "Bezier.h"
#include <algorithm> // min, max
#include <sstream>
#include <locale>
#include <limits>
#include <cmath>
#include <cassert>
#include <stdexcept>
#include <QtCore/QLineF>
#include <QtCore/QDebug>
GCC_DIAG_UNUSED_LOCAL_TYPEDEFS_OFF
#include <boost/shared_ptr.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
GCC_DIAG_UNUSED_LOCAL_TYPEDEFS_ON
#include "Engine/RotoContextPrivate.h"
#include "Engine/AppInstance.h"
#include "Engine/BezierCP.h"
#include "Engine/FeatherPoint.h"
#include "Engine/Interpolation.h"
#include "Engine/TimeLine.h"
#include "Engine/Image.h"
#include "Engine/ImageParams.h"
#include "Engine/Hash64.h"
#include "Engine/Settings.h"
#include "Engine/Format.h"
#include "Engine/RotoLayer.h"
#include "Engine/BezierSerialization.h"
#include "Engine/RenderStats.h"
#include "Engine/Transform.h"
#include "Engine/CoonsRegularization.h"
#include "Engine/ViewIdx.h"
#include "Engine/ViewerInstance.h"
#define kMergeOFXParamOperation "operation"
#define kBlurCImgParamSize "size"
#define kTimeOffsetParamOffset "timeOffset"
#define kFrameHoldParamFirstFrame "firstFrame"
#define kTransformParamTranslate "translate"
#define kTransformParamRotate "rotate"
#define kTransformParamScale "scale"
#define kTransformParamUniform "uniform"
#define kTransformParamSkewX "skewX"
#define kTransformParamSkewY "skewY"
#define kTransformParamSkewOrder "skewOrder"
#define kTransformParamCenter "center"
#define kTransformParamFilter "filter"
#define kTransformParamResetCenter "resetCenter"
#define kTransformParamBlackOutside "black_outside"
//This will enable correct evaluation of beziers
//#define ROTO_USE_MESH_PATTERN_ONLY
// The number of pressure levels is 256 on an old Wacom Graphire 4, and 512 on an entry-level Wacom Bamboo
// 512 should be OK, see:
// http://www.davidrevoy.com/article182/calibrating-wacom-stylus-pressure-on-krita
#define ROTO_PRESSURE_LEVELS 512
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288 /* pi */
#endif
NATRON_NAMESPACE_ENTER
static inline double
lerp(double a,
double b,
double t)
{
return a + (b - a) * t;
}
static inline void
lerpPoint(const Point & a,
const Point & b,
double t,
Point *dest)
{
dest->x = lerp(a.x, b.x, t);
dest->y = lerp(a.y, b.y, t);
}
// compute value using the de Casteljau recursive formula
double
Bezier::bezierEval(double p0,
double p1,
double p2,
double p3,
double t)
{
double p0p1, p1p2, p2p3, p0p1_p1p2, p1p2_p2p3;
p0p1 = lerp(p0, p1, t);
p1p2 = lerp(p1, p2, t);
p2p3 = lerp(p2, p3, t);
p0p1_p1p2 = lerp(p0p1, p1p2, t);
p1p2_p2p3 = lerp(p1p2, p2p3, t);
return lerp(p0p1_p1p2, p1p2_p2p3, t);
}
// compute point using the de Casteljau recursive formula
void
Bezier::bezierFullPoint(const Point & p0,
const Point & p1,
const Point & p2,
const Point & p3,
double t,
Point *p0p1,
Point *p1p2,
Point *p2p3,
Point *p0p1_p1p2,
Point *p1p2_p2p3,
Point *dest)
{
lerpPoint(p0, p1, t, p0p1);
lerpPoint(p1, p2, t, p1p2);
lerpPoint(p2, p3, t, p2p3);
lerpPoint(*p0p1, *p1p2, t, p0p1_p1p2);
lerpPoint(*p1p2, *p2p3, t, p1p2_p2p3);
lerpPoint(*p0p1_p1p2, *p1p2_p2p3, t, dest);
}
void
Bezier::bezierPoint(const Point & p0,
const Point & p1,
const Point & p2,
const Point & p3,
double t,
Point *dest)
{
Point p0p1, p1p2, p2p3, p0p1_p1p2, p1p2_p2p3;
bezierFullPoint(p0, p1, p2, p3, t, &p0p1, &p1p2, &p2p3, &p0p1_p1p2, &p1p2_p2p3, dest);
}
#if 0 //UNUSED CODE
// compute polynomial coefficients so that
// P(t) = A*t^3 + B*t^2 + C*t + D
static inline void
bezierPolyCoeffs(double p0,
double p1,
double p2,
double p3,
double *a,
double *b,
double *c,
double *d)
{
/*
These coefficients are obtained from the Bezier formula above (bezierEval).
Maple code:
p0p1 := p0 + (p1-p0)*t:
p1p2 := p1 + (p2-p1)*t:
p2p3 := p2 + (p3-p2)*t:
p0p1p1p2 := p0p1 +(p1p2-p0p1)*t:
p1p2p2p3 := p1p2 +(p2p3-p1p2)*t:
p := p0p1p1p2 + (p1p2p2p3-p0p1p1p2)*t:
collect(p,t);
*/
// d = P0
*d = p0;
// c = 3*P1-3*P0
*c = 3 * p1 - 3 * p0;
// b = 3*P2-6*P1+3*P0
*b = 3 * p2 - 6 * p1 + 3 * p0;
// a = P3-3*P2+3*P1-P0
*a = p3 - 3 * p2 + 3 * p1 - p0;
}
#endif
// compute polynomial coefficients so that
// P'(t) = A*t^2 + B*t + C
static inline void
bezierPolyDerivativeCoeffs(double p0,
double p1,
double p2,
double p3,
double *a,
double *b,
double *c)
{
/*
These coefficients are obtained from the Bezier formula above (bezierEval).
Maple code:
p0p1 := p0 + (p1-p0)*t:
p1p2 := p1 + (p2-p1)*t:
p2p3 := p2 + (p3-p2)*t:
p0p1p1p2 := p0p1 +(p1p2-p0p1)*t:
p1p2p2p3 := p1p2 +(p2p3-p1p2)*t:
p := p0p1p1p2 + (p1p2p2p3-p0p1p1p2)*t:
collect(p,t);
diff(collect(p, t), t);
*/
// c = 3*P1-3*P0
*c = 3 * p1 - 3 * p0;
// b = 2*(3*P2-6*P1+3*P0)
*b = 2 * (3 * p2 - 6 * p1 + 3 * p0);
// a = 3*(P3-3*P2+3*P1-P0)
*a = 3 * (p3 - 3 * p2 + 3 * p1 - p0);
}
static inline void
updateRange(double x,
double *xmin,
double *xmax)
{
if (x < *xmin) {
*xmin = x;
}
if (x > *xmax) {
*xmax = x;
}
}
// compute the bounds of the Bezier for t \in [0,1]
// algorithm:
// - compute extrema of the cubic, i.e. values of t for
// which the derivative of the x coordinate of the
// Bezier is 0. If they are in [0,1] then they take part in
// range computation (there can be up to two extrema). the
// Bbox is the Bbox of these points and the
// extremal points (P0,P3)
static inline void
bezierBounds(double p0,
double p1,
double p2,
double p3,
double *xmin,
double *xmax)
{
// initialize with the range of the endpoints
*xmin = std::min(p0, p3);
*xmax = std::max(p0, p3);
double a, b, c;
bezierPolyDerivativeCoeffs(p0, p1, p2, p3, &a, &b, &c);
if (a == 0) {
//aX^2 + bX + c well then then this is a simple line
//x= -c / b
double t = b == 0. ? 0. : -c / b;
if ( (0 < t) && (t < 1) ) {
updateRange(Bezier::bezierEval(p0, p1, p2, p3, t), xmin, xmax);
}
return;
}
double disc = b * b - 4 * a * c;
if (disc < 0) {
// no real solution
} else if (disc == 0) {
double t = -b / (2 * a);
if ( (0 < t) && (t < 1) ) {
updateRange(Bezier::bezierEval(p0, p1, p2, p3, t), xmin, xmax);
}
} else {
double t;
t = ( -b - std::sqrt(disc) ) / (2 * a);
if ( (0 < t) && (t < 1) ) {
updateRange(Bezier::bezierEval(p0, p1, p2, p3, t), xmin, xmax);
}
t = ( -b + std::sqrt(disc) ) / (2 * a);
if ( (0 < t) && (t < 1) ) {
updateRange(Bezier::bezierEval(p0, p1, p2, p3, t), xmin, xmax);
}
}
}
// updates param bbox with the bbox of this segment
void
Bezier::bezierPointBboxUpdate(const Point & p0,
const Point & p1,
const Point & p2,
const Point & p3,
RectD *bbox) ///< input/output
{
{
double x1, x2;
bezierBounds(p0.x, p1.x, p2.x, p3.x, &x1, &x2);
if (x1 < bbox->x1) {
bbox->x1 = x1;
}
if (x2 > bbox->x2) {
bbox->x2 = x2;
}
}
{
double y1, y2;
bezierBounds(p0.y, p1.y, p2.y, p3.y, &y1, &y2);
if (y1 < bbox->y1) {
bbox->y1 = y1;
}
if (y2 > bbox->y2) {
bbox->y2 = y2;
}
}
}
// compute a bounding box for the bezier segment
// algorithm:
// - compute extrema of the cubic, i.e. values of t for
// which the derivative of the x or y coordinate of the
// Bezier is 0. If they are in [0,1] then they take part in
// bbox computation (there can be up to four extrema, 2 for
// x and 2 for y). the Bbox is the Bbox of these points and the
// extremal points (P0,P3)
static void
bezierSegmentBboxUpdate(bool useGuiCurves,
const BezierCP & first,
const BezierCP & last,
double time,
ViewIdx view,
unsigned int mipMapLevel,
const Transform::Matrix3x3& transform,
RectD* bbox) ///< input/output
{
Point p0, p1, p2, p3;
Transform::Point3D p0M, p1M, p2M, p3M;
assert(bbox);
try {
first.getPositionAtTime(useGuiCurves, time, view, &p0M.x, &p0M.y);
first.getRightBezierPointAtTime(useGuiCurves, time, view, &p1M.x, &p1M.y);
last.getPositionAtTime(useGuiCurves, time, view, &p3M.x, &p3M.y);
last.getLeftBezierPointAtTime(useGuiCurves, time, view, &p2M.x, &p2M.y);
} catch (const std::exception & e) {
assert(false);
}
p0M.z = p1M.z = p2M.z = p3M.z = 1;
p0M = Transform::matApply(transform, p0M);
p1M = Transform::matApply(transform, p1M);
p2M = Transform::matApply(transform, p2M);
p3M = Transform::matApply(transform, p3M);
p0.x = p0M.x / p0M.z;
p1.x = p1M.x / p1M.z;
p2.x = p2M.x / p2M.z;
p3.x = p3M.x / p3M.z;
p0.y = p0M.y / p0M.z;
p1.y = p1M.y / p1M.z;
p2.y = p2M.y / p2M.z;
p3.y = p3M.y / p3M.z;
if (mipMapLevel > 0) {
int pot = 1 << mipMapLevel;
p0.x /= pot;
p0.y /= pot;
p1.x /= pot;
p1.y /= pot;
p2.x /= pot;
p2.y /= pot;
p3.x /= pot;
p3.y /= pot;
}
Bezier::bezierPointBboxUpdate(p0, p1, p2, p3, bbox);
}
void
Bezier::bezierSegmentListBboxUpdate(bool useGuiCurves,
const BezierCPs & points,
bool finished,
bool isOpenBezier,
double time,
ViewIdx view,
unsigned int mipMapLevel,
const Transform::Matrix3x3& transform,
RectD* bbox) ///< input/output
{
if ( points.empty() ) {
return;
}
if (points.size() == 1) {
// only one point
Transform::Point3D p0;
const BezierCPPtr& p = points.front();
p->getPositionAtTime(useGuiCurves, time, view, &p0.x, &p0.y);
p0.z = 1;
p0 = Transform::matApply(transform, p0);
bbox->x1 = p0.x;
bbox->x2 = p0.x;
bbox->y1 = p0.y;
bbox->y2 = p0.y;
return;
}
BezierCPs::const_iterator next = points.begin();
if ( next != points.end() ) {
++next;
}
for (BezierCPs::const_iterator it = points.begin(); it != points.end(); ++it) {
if ( next == points.end() ) {
if (!finished && !isOpenBezier) {
break;
}
next = points.begin();
}
bezierSegmentBboxUpdate(useGuiCurves, *(*it), *(*next), time, view, mipMapLevel, transform, bbox);
// increment for next iteration
if ( next != points.end() ) {
++next;
}
} // for()
}
inline double euclDist(double x1, double y1, double x2, double y2)
{
double dx = x2 - x1;
double dy = y2 - y1;
return dx * dx + dy * dy;
}
inline void addPointConditionnally(const Point& p, double t, std::list<ParametricPoint >* points)
{
if (points->empty()) {
ParametricPoint x;
x.x = p.x;
x.y = p.y;
x.t = t;
points->push_back(x);
} else {
const ParametricPoint& b = points->back();
if (b.x != p.x || b.y != p.y) {
ParametricPoint x;
x.x = p.x;
x.y = p.y;
x.t = t;
points->push_back(x);
}
}
}
#ifndef ROTO_BEZIER_EVAL_ITERATIVE
/**
* @brief Recursively subdivide the bezier segment p0,p1,p2,p3 until the cubic curve is assumed to be flat. The errorScale is used to determine the stopping criterion.
* The greater it is, the smoother the curve will be.
**/
static void
recursiveBezierInternal(const Point& p0, const Point& p1, const Point& p2, const Point& p3,
double t_p0, double t_p1, double t_p2, double t_p3,
double errorScale, int recursionLevel, int maxRecursion, std::list<ParametricPoint >* points)
{
if (recursionLevel > maxRecursion) {
return;
}
double x12 = (p0.x + p1.x) / 2;
double y12 = (p0.y + p1.y) / 2;
double x23 = (p1.x + p2.x) / 2;
double y23 = (p1.y + p2.y) / 2;
double x34 = (p2.x + p3.x) / 2;
double y34 = (p2.y + p3.y) / 2;
double x123 = (x12 + x23) / 2;
double y123 = (y12 + y23) / 2;
double x234 = (x23 + x34) / 2;
double y234 = (y23 + y34) / 2;
double x1234 = (x123 + x234) / 2;
double y1234 = (y123 + y234) / 2;
double t_p12 = (t_p0 + t_p1) / 2.;
double t_p23 = (t_p1 + t_p2) / 2.;
double t_p34 = (t_p2 + t_p3) / 2;
double t_p123 = (t_p12 + t_p23) / 2.;
double t_p234 = (t_p23 + t_p34) / 2.;
double t_p1234 = (t_p123 + t_p234) / 2.;
static const double angleToleranceMax = 0.;
static const double cuspTolerance = 0.;
static const double collinearityEps = 1e-30;
static const double angleToleranceEps = 0.01;
double distanceToleranceSquare = 0.5 / errorScale;
distanceToleranceSquare *= distanceToleranceSquare;
// approximate the cubic curve by a straight line
// See http://algorithmist.net/docs/subdivision.pdf for stopping criterion
double dx = p3.x - p0.x;
double dy = p3.y - p0.y;
double d2 = std::fabs(((p1.x - p3.x) * dy - (p1.y - p3.y) * dx));
double d3 = std::fabs(((p2.x - p3.x) * dy - (p2.y - p3.y) * dx));
double da1, da2;
double segmentDistanceSq = dx * dx + dy * dy;
int possibleCases = ((int)(d2 > collinearityEps) << 1) + (int)(d3 > collinearityEps);
switch (possibleCases) {
case 0: {
// collinear OR p0 is p4
if (segmentDistanceSq == 0) {
d2 = (p1.x - p0.x) * (p1.x - p0.x) + (p1.y - p0.y) * (p1.y - p0.y);
d3 = (p3.x - p2.x) * (p3.x - p2.x) + (p3.y - p2.y) * (p3.y - p2.y);
} else {
segmentDistanceSq = 1 / segmentDistanceSq;
da1 = p1.x - p0.x;
da2 = p1.y - p0.y;
d2 = segmentDistanceSq * (da1 * dx + da2 * dy);
da1 = p2.x - p0.x;
da2 = p2.y - p0.y;
d3 = segmentDistanceSq * (da1 * dx + da2 * dy);
if (d2 > 0 && d2 < 1 && d3 > 0 && d3 < 1) {
// Simple collinear case, 1---2---3---4
return;
}
if (d2 <= 0) {
d2 = euclDist(p1.x, p1.y, p0.x, p0.y);
} else if (d2 >= 1) {
d2 = euclDist(p1.x, p3.x, p1.y, p3.y);
} else {
d2 = euclDist(p1.x, p1.y, p0.x + d2 * dx, p0.y + d2 * dy);
}
if (d3 <= 0) {
d3 = euclDist(p2.x, p0.y, p2.x, p0.y);
} else if (d3 >= 1) {
d3 = euclDist(p2.x, p2.y, p3.x, p3.y);
} else {
d3 = euclDist(p2.x, p2.y, p0.x + d3 * dx, p0.y + d3 + dy);
}
}
if (d2 > d3) {
if (d2 < distanceToleranceSquare) {
addPointConditionnally(p1, t_p1, points);
return;
}
} else {
if (d3 < distanceToleranceSquare) {
addPointConditionnally(p2, t_p2, points);
return;
}
}
} break;
case 1: {
// p1,p2,p4 are collinear, p3 is significant
if (d3 * d3 <= distanceToleranceSquare * segmentDistanceSq) {
if (angleToleranceMax < angleToleranceEps) {
Point p;
p.x = x23;
p.y = y23;
addPointConditionnally(p, t_p23, points);
return;
}
// Check Angle
da1 = std::fabs(std::atan2(p3.y - p2.y, p3.x - p2.x) - std::atan2(p2.y - p1.y, p2.x - p1.x));
if (da1 >= M_PI) {
da1 = 2. * M_PI - da1;
}
if (da1 < angleToleranceMax) {
addPointConditionnally(p1, t_p1, points);
addPointConditionnally(p2, t_p2, points);
return;
}
if (cuspTolerance != 0.0) {
if (da1 > cuspTolerance) {
addPointConditionnally(p2, t_p2, points);
return;
}
}
}
} break;
case 2: {
// p1,p3,p4 are collinear, p2 is significant
if (d2 * d2 <= distanceToleranceSquare * segmentDistanceSq) {
if (angleToleranceMax < angleToleranceEps) {
Point p;
p.x = x23;
p.y = y23;
addPointConditionnally(p, t_p23, points);
return;
}
// Check Angle
da1 = std::fabs(std::atan2(p2.y - p1.y, p2.x - p1.x) - std::atan2(p1.y - p0.y, p1.x - p0.x));
if (da1 >= M_PI) {
da1 = 2 * M_PI - da1;
}
if (da1 < angleToleranceMax) {
addPointConditionnally(p1, t_p1, points);
addPointConditionnally(p2, t_p2, points);
return;
}
if (cuspTolerance != 0.0) {
if (da1 > cuspTolerance) {
addPointConditionnally(p1, t_p1, points);
return;
}
}
}
} break;
case 3: {
if ((d2 + d3) * (d2 + d3) <= distanceToleranceSquare * segmentDistanceSq) {
// Check curvature
if (angleToleranceMax < angleToleranceEps) {
Point p;
p.x = x23;
p.y = y23;
addPointConditionnally(p, t_p23, points);
return;
}
// Handle cusps
double a23 = std::atan2(p2.y - p1.y, p2.x - p1.x);
da1 = std::fabs(a23 - std::atan2(p1.y - p0.y, p1.x - p0.x));
da2 = std::fabs(std::atan2(p3.y - p2.y, p3.x - p2.x) - a23);
if (da1 >= M_PI) {
da1 = 2 * M_PI - da1;
}
if (da2 >= M_PI) {
da2 = 2 * M_PI - da2;
}
if (da1 + da2 < angleToleranceMax) {
Point p;
p.x = x23;
p.y = y23;
addPointConditionnally(p, t_p23, points);
return;
}
if (cuspTolerance != 0.0) {
if (da1 > cuspTolerance) {
addPointConditionnally(p1, t_p1, points);
return;
}
if (da2 > cuspTolerance) {
addPointConditionnally(p2, t_p2, points);
return;
}
}
}
} break;
default:
assert(false);
break;
} // possibleCases
// Subdivide
Point p12 = {x12, y12};
Point p123 = {x123, y123};
Point p1234 = {x1234, y1234};
Point p234 = {x234, y234};
Point p34 = {x34, y34};
recursiveBezierInternal(p0, p12, p123, p1234, t_p0, t_p12, t_p123, t_p1234, errorScale, recursionLevel + 1, maxRecursion, points);
recursiveBezierInternal(p1234, p234, p34, p3, t_p1234, t_p234, t_p34, t_p3, errorScale, recursionLevel + 1, maxRecursion, points);
}
static void
recursiveBezier(const Point& p0, const Point& p1, const Point& p2, const Point& p3, double errorScale, int maxRecursion, std::list<ParametricPoint >* points)
{
ParametricPoint p0x,p3x;
p0x.x = p0.x;
p0x.y = p0.y;
p0x.t = 0.;
p3x.x = p3.x;
p3x.y = p3.y;
p3x.t = 1.;
points->push_back(p0x);
recursiveBezierInternal(p0, p1, p2, p3, 0., 1. / 3., 2. / 3., 1., errorScale, 0, maxRecursion, points);
points->push_back(p3x);
}
#endif // #ifdef ROTO_BEZIER_EVAL_ITERATIVE
// compute nbPointsperSegment points and update the bbox bounding box for the Bezier
// segment from 'first' to 'last' evaluated at 'time'
// If nbPointsPerSegment is -1 then it will be automatically computed
static void
bezierSegmentEval(bool useGuiCurves,
const BezierCP & first,
const BezierCP & last,
double time,
ViewIdx view,
unsigned int mipMapLevel,
#ifdef ROTO_BEZIER_EVAL_ITERATIVE
int nbPointsPerSegment,
#else
double errorScale,
#endif
const Transform::Matrix3x3& transform,
std::list<ParametricPoint >* points, ///< output
RectD* bbox = NULL) ///< input/output (optional)
{
Transform::Point3D p0M, p1M, p2M, p3M;
Point p0, p1, p2, p3;
try {
first.getPositionAtTime(useGuiCurves, time, view, &p0M.x, &p0M.y);
first.getRightBezierPointAtTime(useGuiCurves, time, view, &p1M.x, &p1M.y);
last.getPositionAtTime(useGuiCurves, time, view, &p3M.x, &p3M.y);
last.getLeftBezierPointAtTime(useGuiCurves, time, view, &p2M.x, &p2M.y);
} catch (const std::exception & e) {
assert(false);
}
p0M.z = p1M.z = p2M.z = p3M.z = 1;
p0M = matApply(transform, p0M);
p1M = matApply(transform, p1M);
p2M = matApply(transform, p2M);
p3M = matApply(transform, p3M);
p0.x = p0M.x / p0M.z; p0.y = p0M.y / p0M.z;
p1.x = p1M.x / p1M.z; p1.y = p1M.y / p1M.z;
p2.x = p2M.x / p2M.z; p2.y = p2M.y / p2M.z;
p3.x = p3M.x / p3M.z; p3.y = p3M.y / p3M.z;
if (mipMapLevel > 0) {
int pot = 1 << mipMapLevel;
p0.x /= pot;
p0.y /= pot;
p1.x /= pot;
p1.y /= pot;
p2.x /= pot;
p2.y /= pot;
p3.x /= pot;
p3.y /= pot;
}
#ifdef ROTO_BEZIER_EVAL_ITERATIVE
if (nbPointsPerSegment == -1) {
/*
* Approximate the necessary number of line segments, using http://antigrain.com/research/adaptive_bezier/
*/
double dx1, dy1, dx2, dy2, dx3, dy3;
dx1 = p1.x - p0.x;
dy1 = p1.y - p0.y;
dx2 = p2.x - p1.x;
dy2 = p2.y - p1.y;
dx3 = p3.x - p2.x;
dy3 = p3.y - p2.y;
double length = std::sqrt(dx1 * dx1 + dy1 * dy1) +
std::sqrt(dx2 * dx2 + dy2 * dy2) +
std::sqrt(dx3 * dx3 + dy3 * dy3);
nbPointsPerSegment = (int)std::max(length * 0.25, 2.);
}
double incr = 1. / (double)(nbPointsPerSegment - 1);
Point cur;
for (int i = 0; i < nbPointsPerSegment; ++i) {
ParametricPoint p;
p.t = incr * i;
Bezier::bezierPoint(p0, p1, p2, p3, p.t, &cur);
p.x = cur.x;
p.y = cur.y;
points->push_back(p);
}
#else
static const int maxRecursion = 32;
recursiveBezier(p0, p1, p2, p3, errorScale, maxRecursion, points);
#endif
if (bbox) {
Bezier::bezierPointBboxUpdate(p0, p1, p2, p3, bbox);
}
} // bezierSegmentEval
/**
* @brief Determines if the point (x,y) lies on the bezier curve segment defined by first and last.
* @returns True if the point is close (according to the acceptance) to the curve, false otherwise.
* @param param[out] It is set to the parametric value at which the subdivision of the bezier segment
* yields the closest point to (x,y) on the curve.
**/
static bool
bezierSegmentMeetsPoint(bool useGuiCurves,
const BezierCP & first,
const BezierCP & last,
const Transform::Matrix3x3& transform,
double time,
ViewIdx view,
double x,
double y,
double distance,
double *param) ///< output
{
Transform::Point3D p0, p1, p2, p3;
p0.z = p1.z = p2.z = p3.z = 1;
first.getPositionAtTime(useGuiCurves, time, view, &p0.x, &p0.y);
first.getRightBezierPointAtTime(useGuiCurves, time, view, &p1.x, &p1.y);
last.getPositionAtTime(useGuiCurves, time, view, &p3.x, &p3.y);
last.getLeftBezierPointAtTime(useGuiCurves, time, view, &p2.x, &p2.y);
p0 = Transform::matApply(transform, p0);
p1 = Transform::matApply(transform, p1);
p2 = Transform::matApply(transform, p2);
p3 = Transform::matApply(transform, p3);
///Use the control polygon to approximate segment length
double length = ( std::sqrt( (p1.x - p0.x) * (p1.x - p0.x) + (p1.y - p0.y) * (p1.y - p0.y) ) +
std::sqrt( (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y) ) +
std::sqrt( (p3.x - p2.x) * (p3.x - p2.x) + (p3.y - p2.y) * (p3.y - p2.y) ) );
// increment is the distance divided by the segment length
double incr = length == 0. ? 1. : distance / length;
Point p02d, p12d, p22d, p32d;
{
p02d.x = p0.x; p02d.y = p0.y;
p12d.x = p1.x; p12d.y = p1.y;
p22d.x = p2.x; p22d.y = p2.y;
p32d.x = p3.x; p32d.y = p3.y;
}
///the minimum square distance between a decasteljau point an the given (x,y) point
///we save a sqrt call
double sqDistance = distance * distance;
double minSqDistance = std::numeric_limits<double>::infinity();
double tForMin = -1.;
// 1/incr = 0.9 -> 2 points + + o
// 1/incr = 1.0 -> 2 points + o
// 1/incr = 1.1 -> 3 points + o +
// 1/incr = 2.0 -> 3 points + o o
// 1/incr = 2.1 -> 4 points + o o+
int nbPoints = std::ceil(1. / incr) + 1;
for (int i = 0; i < nbPoints; ++i) {
double t = std::min(i * incr, 1.);
// the last point should be t == 1;
assert( t < 1 || (t == 1. && i == nbPoints - 1) );
Point p;
Bezier::bezierPoint(p02d, p12d, p22d, p32d, t, &p);
double sqdist = (p.x - x) * (p.x - x) + (p.y - y) * (p.y - y);
if ( (sqdist <= sqDistance) && (sqdist < minSqDistance) ) {
minSqDistance = sqdist;
tForMin = t;
}
}
if (minSqDistance <= sqDistance) {
*param = tForMin;
return true;
}
return false;
} // bezierSegmentMeetsPoint
static bool
isPointCloseTo(bool useGuiCurves,
double time,
ViewIdx view,
const BezierCP & p,
double x,
double y,
const Transform::Matrix3x3& transform,
double acceptance)
{
Transform::Point3D pos;
pos.z = 1;
p.getPositionAtTime(useGuiCurves, time, view, &pos.x, &pos.y);
pos = Transform::matApply(transform, pos);
if ( ( pos.x >= (x - acceptance) ) && ( pos.x <= (x + acceptance) ) && ( pos.y >= (y - acceptance) ) && ( pos.y <= (y + acceptance) ) ) {
return true;
}
return false;
}
static bool
bezierSegmenEqual(bool useGuiCurves,
double time,
ViewIdx view,
const BezierCP & p0,
const BezierCP & p1,
const BezierCP & s0,
const BezierCP & s1)
{
double prevX, prevY, prevXF, prevYF;
double nextX, nextY, nextXF, nextYF;
p0.getPositionAtTime(useGuiCurves, time, view, &prevX, &prevY);
p1.getPositionAtTime(useGuiCurves, time, view, &nextX, &nextY);
s0.getPositionAtTime(useGuiCurves, time, view, &prevXF, &prevYF);
s1.getPositionAtTime(useGuiCurves, time, view, &nextXF, &nextYF);
if ( (prevX != prevXF) || (prevY != prevYF) || (nextX != nextXF) || (nextY != nextYF) ) {
return true;
} else {
///check derivatives
double prevRightX, prevRightY, nextLeftX, nextLeftY;
double prevRightXF, prevRightYF, nextLeftXF, nextLeftYF;
p0.getRightBezierPointAtTime(useGuiCurves, time, view, &prevRightX, &prevRightY);
p1.getLeftBezierPointAtTime(useGuiCurves, time, view, &nextLeftX, &nextLeftY);
s0.getRightBezierPointAtTime(useGuiCurves, time, view, &prevRightXF, &prevRightYF);
s1.getLeftBezierPointAtTime(useGuiCurves, time, view, &nextLeftXF, &nextLeftYF);
if ( (prevRightX != prevRightXF) || (prevRightY != prevRightYF) || (nextLeftX != nextLeftXF) || (nextLeftY != nextLeftYF) ) {
return true;
} else {
return false;
}
}
}
////////////////////////////////////Bezier////////////////////////////////////
namespace {
enum SplineChangedReason
{
DERIVATIVES_CHANGED = 0,
CONTROL_POINT_CHANGED = 1
};
}
Bezier::Bezier(const RotoContextPtr& ctx,
const std::string & name,
const RotoLayerPtr& parent,
bool isOpenBezier)
: RotoDrawableItem(ctx, name, parent, false)
, _imp( new BezierPrivate(isOpenBezier) )
{
}
Bezier::Bezier(const Bezier & other,
const RotoLayerPtr& parent)
: RotoDrawableItem( other.getContext(), other.getScriptName(), other.getParentLayer(), false )
, _imp( new BezierPrivate(false) )
{
clone(&other);
setParentLayer(parent);
}
bool
Bezier::isOpenBezier() const
{
return _imp->isOpenBezier;
}
bool
Bezier::dequeueGuiActions()
{
bool mustCopy;
{
QMutexLocker k2(&_imp->guiCopyMutex);
mustCopy = _imp->mustCopyGui;
if (mustCopy) {
_imp->mustCopyGui = false;
}
}
QMutexLocker k(&itemMutex);
if (mustCopy) {
BezierPtr this_shared = boost::dynamic_pointer_cast<Bezier>( shared_from_this() );
assert(this_shared);
BezierCPs::iterator fit = _imp->featherPoints.begin();
for (BezierCPs::iterator it = _imp->points.begin(); it != _imp->points.end(); ++it, ++fit) {
(*it)->cloneGuiCurvesToInternalCurves();
(*fit)->cloneGuiCurvesToInternalCurves();