forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotoContext.cpp
4819 lines (4161 loc) · 176 KB
/
RotoContext.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 "RotoContext.h"
#include <algorithm> // min, max
#include <sstream>
#include <locale>
#include <limits>
#include <cassert>
#include <stdexcept>
#include <cstring> // for std::memcpy, std::memset
#include <sstream> // stringstream
#include <boost/scoped_ptr.hpp>
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 <QtCore/QLineF>
#include <QtCore/QDebug>
//#define ROTO_RENDER_TRIANGLES_ONLY
#include "libtess.h"
#include "Engine/RotoContextPrivate.h"
#include "Engine/AppInstance.h"
#include "Engine/Bezier.h"
#include "Engine/BezierCP.h"
#include "Engine/CreateNodeArgs.h"
#include "Engine/CoonsRegularization.h"
#include "Engine/FeatherPoint.h"
#include "Engine/Format.h"
#include "Engine/Hash64.h"
#include "Engine/Image.h"
#include "Engine/ImageParams.h"
#include "Engine/MemoryInfo.h" // printAsRAM
#include "Engine/NodeSerialization.h"
#include "Engine/Interpolation.h"
#include "Engine/RenderStats.h"
#include "Engine/RotoContextSerialization.h"
#include "Engine/RotoDrawableItem.h"
#include "Engine/RotoLayer.h"
#include "Engine/RotoStrokeItem.h"
#include "Engine/Settings.h"
#include "Engine/TimeLine.h"
#include "Engine/Transform.h"
#include "Engine/ViewerInstance.h"
#include "Engine/ViewIdx.h"
#define kMergeOFXParamOperation "operation"
#define kMergeOFXParamInvertMask "maskInvert"
#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
////////////////////////////////////RotoContext////////////////////////////////////
RotoContext::RotoContext(const NodePtr& node)
: _imp( new RotoContextPrivate(node) )
{
QObject::connect( _imp->lifeTime.lock()->getSignalSlotHandler().get(), SIGNAL(valueChanged(ViewSpec,int,int)), this, SLOT(onLifeTimeKnobValueChanged(ViewSpec,int,int)) );
}
// make_shared enabler (because make_shared needs access to the private constructor)
// see https://stackoverflow.com/a/20961251/2607517
struct RotoContext::MakeSharedEnabler: public RotoContext
{
MakeSharedEnabler(const NodePtr& node) : RotoContext(node) {
}
};
RotoContextPtr
RotoContext::create(const NodePtr& node)
{
return boost::make_shared<RotoContext::MakeSharedEnabler>(node);
}
bool
RotoContext::isRotoPaint() const
{
return _imp->isPaintNode;
}
void
RotoContext::setWhileCreatingPaintStrokeOnMergeNodes(bool b)
{
getNode()->setWhileCreatingPaintStroke(b);
QMutexLocker k(&_imp->rotoContextMutex);
for (NodesList::iterator it = _imp->globalMergeNodes.begin(); it != _imp->globalMergeNodes.end(); ++it) {
(*it)->setWhileCreatingPaintStroke(b);
}
}
NodePtr
RotoContext::getRotoPaintBottomMergeNode() const
{
std::list<RotoDrawableItemPtr> items = getCurvesByRenderOrder(false /*onlyActiveItems*/);
if ( items.empty() ) {
return NodePtr();
}
int bop;
if ( isRotoPaintTreeConcatenatableInternal(items, &bop) ) {
QMutexLocker k(&_imp->rotoContextMutex);
if ( !_imp->globalMergeNodes.empty() ) {
return _imp->globalMergeNodes.front();
}
}
const RotoDrawableItemPtr& firstStrokeItem = items.back();
assert(firstStrokeItem);
NodePtr bottomMerge = firstStrokeItem->getMergeNode();
assert(bottomMerge);
return bottomMerge;
}
void
RotoContext::getRotoPaintTreeNodes(NodesList* nodes) const
{
std::list<RotoDrawableItemPtr> items = getCurvesByRenderOrder(false);
for (std::list<RotoDrawableItemPtr>::iterator it = items.begin(); it != items.end(); ++it) {
NodePtr effectNode = (*it)->getEffectNode();
NodePtr mergeNode = (*it)->getMergeNode();
NodePtr timeOffsetNode = (*it)->getTimeOffsetNode();
NodePtr frameHoldNode = (*it)->getFrameHoldNode();
if (effectNode) {
nodes->push_back(effectNode);
}
if (mergeNode) {
nodes->push_back(mergeNode);
}
if (timeOffsetNode) {
nodes->push_back(timeOffsetNode);
}
if (frameHoldNode) {
nodes->push_back(frameHoldNode);
}
}
QMutexLocker k(&_imp->rotoContextMutex);
for (NodesList::const_iterator it = _imp->globalMergeNodes.begin(); it != _imp->globalMergeNodes.end(); ++it) {
nodes->push_back(*it);
}
}
///Must be done here because at the time of the constructor, the shared_ptr doesn't exist yet but
///addLayer() needs it to get a shared ptr to this
void
RotoContext::createBaseLayer()
{
////Add the base layer
RotoLayerPtr base = addLayerInternal(false);
deselect(base, RotoItem::eSelectionReasonOther);
}
RotoContext::~RotoContext()
{
}
RotoLayerPtr
RotoContext::getOrCreateBaseLayer()
{
QMutexLocker k(&_imp->rotoContextMutex);
if ( _imp->layers.empty() ) {
k.unlock();
addLayer();
k.relock();
}
assert( !_imp->layers.empty() );
return _imp->layers.front();
}
RotoLayerPtr
RotoContext::addLayerInternal(bool declarePython)
{
RotoContextPtr this_shared = shared_from_this();
assert(this_shared);
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
RotoLayerPtr item;
std::string name = generateUniqueName(kRotoLayerBaseName);
int indexInLayer = -1;
{
RotoLayerPtr deepestLayer;
RotoLayerPtr parentLayer;
{
QMutexLocker l(&_imp->rotoContextMutex);
deepestLayer = _imp->findDeepestSelectedLayer();
if (!deepestLayer) {
///find out if there's a base layer, if so add to the base layer,
///otherwise create the base layer
for (std::list<RotoLayerPtr>::iterator it = _imp->layers.begin(); it != _imp->layers.end(); ++it) {
int hierarchy = (*it)->getHierarchyLevel();
if (hierarchy == 0) {
parentLayer = *it;
break;
}
}
} else {
parentLayer = deepestLayer;
}
}
item.reset( new RotoLayer( this_shared, name, RotoLayerPtr() ) );
if (parentLayer) {
parentLayer->addItem(item, declarePython);
indexInLayer = parentLayer->getItems().size() - 1;
}
QMutexLocker l(&_imp->rotoContextMutex);
_imp->layers.push_back(item);
_imp->lastInsertedItem = item;
}
Q_EMIT itemInserted(indexInLayer, RotoItem::eSelectionReasonOther);
clearSelection(RotoItem::eSelectionReasonOther);
select(item, RotoItem::eSelectionReasonOther);
return item;
} // RotoContext::addLayerInternal
RotoLayerPtr
RotoContext::addLayer()
{
return addLayerInternal(true);
} // addLayer
void
RotoContext::addLayer(const RotoLayerPtr & layer)
{
std::list<RotoLayerPtr>::iterator it = std::find(_imp->layers.begin(), _imp->layers.end(), layer);
if ( it == _imp->layers.end() ) {
_imp->layers.push_back(layer);
}
}
RotoItemPtr
RotoContext::getLastInsertedItem() const
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
return _imp->lastInsertedItem;
}
#ifdef NATRON_ROTO_INVERTIBLE
KnobBoolPtr
RotoContext::getInvertedKnob() const
{
return _imp->inverted.lock();
}
#endif
KnobColorPtr
RotoContext::getColorKnob() const
{
return _imp->colorKnob.lock();
}
void
RotoContext::setAutoKeyingEnabled(bool enabled)
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
QMutexLocker l(&_imp->rotoContextMutex);
_imp->autoKeying = enabled;
}
bool
RotoContext::isAutoKeyingEnabled() const
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
QMutexLocker l(&_imp->rotoContextMutex);
return _imp->autoKeying;
}
void
RotoContext::setFeatherLinkEnabled(bool enabled)
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
QMutexLocker l(&_imp->rotoContextMutex);
_imp->featherLink = enabled;
}
bool
RotoContext::isFeatherLinkEnabled() const
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
QMutexLocker l(&_imp->rotoContextMutex);
return _imp->featherLink;
}
void
RotoContext::setRippleEditEnabled(bool enabled)
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
QMutexLocker l(&_imp->rotoContextMutex);
_imp->rippleEdit = enabled;
}
bool
RotoContext::isRippleEditEnabled() const
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
QMutexLocker l(&_imp->rotoContextMutex);
return _imp->rippleEdit;
}
NodePtr
RotoContext::getNode() const
{
return _imp->node.lock();
}
int
RotoContext::getTimelineCurrentTime() const
{
return getNode()->getApp()->getTimeLine()->currentFrame();
}
std::string
RotoContext::generateUniqueName(const std::string& baseName)
{
int no = 1;
bool foundItem;
std::string name;
do {
std::stringstream ss;
ss << baseName;
ss << no;
name = ss.str();
if ( getItemByName(name) ) {
foundItem = true;
} else {
foundItem = false;
}
++no;
} while (foundItem);
return name;
}
BezierPtr
RotoContext::makeBezier(double x,
double y,
const std::string & baseName,
double time,
bool isOpenBezier)
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
RotoLayerPtr parentLayer;
RotoContextPtr this_shared = boost::dynamic_pointer_cast<RotoContext>( shared_from_this() );
assert(this_shared);
std::string name = generateUniqueName(baseName);
{
QMutexLocker l(&_imp->rotoContextMutex);
RotoLayerPtr deepestLayer = _imp->findDeepestSelectedLayer();
if (!deepestLayer) {
///if there is no base layer, create one
if ( _imp->layers.empty() ) {
l.unlock();
addLayer();
l.relock();
}
parentLayer = _imp->layers.front();
} else {
parentLayer = deepestLayer;
}
}
assert(parentLayer);
BezierPtr curve( new Bezier(this_shared, name, RotoLayerPtr(), isOpenBezier) );
curve->createNodes();
int indexInLayer = -1;
if (parentLayer) {
indexInLayer = 0;
parentLayer->insertItem(curve, 0);
}
_imp->lastInsertedItem = curve;
Q_EMIT itemInserted(indexInLayer, RotoItem::eSelectionReasonOther);
clearSelection(RotoItem::eSelectionReasonOther);
select(curve, RotoItem::eSelectionReasonOther);
if ( isAutoKeyingEnabled() ) {
curve->setKeyframe( getTimelineCurrentTime() );
}
curve->addControlPoint(x, y, time);
return curve;
} // makeBezier
RotoStrokeItemPtr
RotoContext::makeStroke(RotoStrokeType type,
const std::string& baseName,
bool clearSel)
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
RotoLayerPtr parentLayer;
RotoContextPtr this_shared = boost::dynamic_pointer_cast<RotoContext>( shared_from_this() );
assert(this_shared);
std::string name = generateUniqueName(baseName);
{
QMutexLocker l(&_imp->rotoContextMutex);
++_imp->age; // increase age
RotoLayerPtr deepestLayer = _imp->findDeepestSelectedLayer();
if (!deepestLayer) {
///if there is no base layer, create one
if ( _imp->layers.empty() ) {
l.unlock();
addLayer();
l.relock();
}
parentLayer = _imp->layers.front();
} else {
parentLayer = deepestLayer;
}
}
assert(parentLayer);
RotoStrokeItemPtr curve( new RotoStrokeItem( type, this_shared, name, RotoLayerPtr() ) );
int indexInLayer = -1;
if (parentLayer) {
indexInLayer = 0;
parentLayer->insertItem(curve, 0);
}
curve->createNodes();
_imp->lastInsertedItem = curve;
Q_EMIT itemInserted(indexInLayer, RotoItem::eSelectionReasonOther);
if (clearSel) {
clearSelection(RotoItem::eSelectionReasonOther);
select(curve, RotoItem::eSelectionReasonOther);
}
return curve;
}
BezierPtr
RotoContext::makeEllipse(double x,
double y,
double diameter,
bool fromCenter,
double time)
{
double half = diameter / 2.;
BezierPtr curve = makeBezier(x, fromCenter ? y - half : y, kRotoEllipseBaseName, time, false);
if (fromCenter) {
curve->addControlPoint(x + half, y, time);
curve->addControlPoint(x, y + half, time);
curve->addControlPoint(x - half, y, time);
} else {
curve->addControlPoint(x + diameter, y - diameter, time);
curve->addControlPoint(x, y - diameter, time);
curve->addControlPoint(x - diameter, y - diameter, time);
}
BezierCPPtr top = curve->getControlPointAtIndex(0);
BezierCPPtr right = curve->getControlPointAtIndex(1);
BezierCPPtr bottom = curve->getControlPointAtIndex(2);
BezierCPPtr left = curve->getControlPointAtIndex(3);
double topX, topY, rightX, rightY, btmX, btmY, leftX, leftY;
top->getPositionAtTime(false, time, ViewIdx(0), &topX, &topY);
right->getPositionAtTime(false, time, ViewIdx(0), &rightX, &rightY);
bottom->getPositionAtTime(false, time, ViewIdx(0), &btmX, &btmY);
left->getPositionAtTime(false, time, ViewIdx(0), &leftX, &leftY);
curve->setLeftBezierPoint(0, time, (leftX + topX) / 2., topY);
curve->setRightBezierPoint(0, time, (rightX + topX) / 2., topY);
curve->setLeftBezierPoint(1, time, rightX, (rightY + topY) / 2.);
curve->setRightBezierPoint(1, time, rightX, (rightY + btmY) / 2.);
curve->setLeftBezierPoint(2, time, (rightX + btmX) / 2., btmY);
curve->setRightBezierPoint(2, time, (leftX + btmX) / 2., btmY);
curve->setLeftBezierPoint(3, time, leftX, (btmY + leftY) / 2.);
curve->setRightBezierPoint(3, time, leftX, (topY + leftY) / 2.);
curve->setCurveFinished(true);
return curve;
}
BezierPtr
RotoContext::makeSquare(double x,
double y,
double initialSize,
double time)
{
BezierPtr curve = makeBezier(x, y, kRotoRectangleBaseName, time, false);
curve->addControlPoint(x + initialSize, y, time);
curve->addControlPoint(x + initialSize, y - initialSize, time);
curve->addControlPoint(x, y - initialSize, time);
curve->setCurveFinished(true);
return curve;
}
void
RotoContext::removeItemRecursively(const RotoItemPtr& item,
RotoItem::SelectionReasonEnum reason)
{
RotoLayerPtr isLayer = boost::dynamic_pointer_cast<RotoLayer>(item);
RotoItemPtr foundSelected;
{
QMutexLocker l(&_imp->rotoContextMutex);
for (std::list<RotoItemPtr>::iterator it = _imp->selectedItems.begin(); it != _imp->selectedItems.end(); ++it) {
if (*it == item) {
foundSelected = *it;
break;
}
}
}
if (foundSelected) {
deselectInternal(foundSelected);
}
if (isLayer) {
const RotoItems & items = isLayer->getItems();
for (RotoItems::const_iterator it = items.begin(); it != items.end(); ++it) {
removeItemRecursively(*it, reason);
}
QMutexLocker l(&_imp->rotoContextMutex);
for (std::list<RotoLayerPtr>::iterator it = _imp->layers.begin(); it != _imp->layers.end(); ++it) {
if (*it == isLayer) {
_imp->layers.erase(it);
break;
}
}
}
Q_EMIT itemRemoved(item, (int)reason);
}
void
RotoContext::removeItem(const RotoItemPtr& item,
RotoItem::SelectionReasonEnum reason)
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
RotoLayerPtr layer = item->getParentLayer();
if (layer) {
layer->removeItem(item);
}
removeItemRecursively(item, reason);
Q_EMIT selectionChanged( (int)reason );
}
void
RotoContext::addItem(const RotoLayerPtr& layer,
int indexInLayer,
const RotoItemPtr & item,
RotoItem::SelectionReasonEnum reason)
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
{
if (layer) {
layer->insertItem(item, indexInLayer);
}
QMutexLocker l(&_imp->rotoContextMutex);
RotoLayerPtr isLayer = boost::dynamic_pointer_cast<RotoLayer>(item);
if (isLayer) {
std::list<RotoLayerPtr>::iterator foundLayer = std::find(_imp->layers.begin(), _imp->layers.end(), isLayer);
if ( foundLayer == _imp->layers.end() ) {
_imp->layers.push_back(isLayer);
}
}
_imp->lastInsertedItem = item;
}
Q_EMIT itemInserted(indexInLayer, reason);
}
const std::list<RotoLayerPtr> &
RotoContext::getLayers() const
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
return _imp->layers;
}
BezierPtr
RotoContext::isNearbyBezier(double x,
double y,
double acceptance,
int* index,
double* t,
bool* feather) const
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
QMutexLocker l(&_imp->rotoContextMutex);
std::list<std::pair<BezierPtr, std::pair<int, double> > > nearbyBeziers;
for (std::list<RotoLayerPtr>::const_iterator it = _imp->layers.begin(); it != _imp->layers.end(); ++it) {
const RotoItems & items = (*it)->getItems();
for (RotoItems::const_iterator it2 = items.begin(); it2 != items.end(); ++it2) {
BezierPtr b = boost::dynamic_pointer_cast<Bezier>(*it2);
if ( b && !b->isLockedRecursive() ) {
double param;
int i = b->isPointOnCurve(x, y, acceptance, ¶m, feather);
if (i != -1) {
nearbyBeziers.push_back( std::make_pair( b, std::make_pair(i, param) ) );
}
}
}
}
std::list<std::pair<BezierPtr, std::pair<int, double> > >::iterator firstNotSelected = nearbyBeziers.end();
for (std::list<std::pair<BezierPtr, std::pair<int, double> > >::iterator it = nearbyBeziers.begin(); it != nearbyBeziers.end(); ++it) {
bool foundSelected = false;
for (std::list<RotoItemPtr>::iterator it2 = _imp->selectedItems.begin(); it2 != _imp->selectedItems.end(); ++it2) {
if ( it2->get() == it->first.get() ) {
foundSelected = true;
break;
}
}
if (foundSelected) {
*index = it->second.first;
*t = it->second.second;
return it->first;
} else if ( firstNotSelected == nearbyBeziers.end() ) {
firstNotSelected = it;
}
}
if ( firstNotSelected != nearbyBeziers.end() ) {
*index = firstNotSelected->second.first;
*t = firstNotSelected->second.second;
return firstNotSelected->first;
}
return BezierPtr();
}
void
RotoContext::onLifeTimeKnobValueChanged(ViewSpec view,
int /*dim*/,
int reason)
{
if ( (ValueChangedReasonEnum)reason != eValueChangedReasonUserEdited ) {
return;
}
int lifetime_i = _imp->lifeTime.lock()->getValue();
_imp->activated.lock()->setSecret(lifetime_i != 4);
KnobIntPtr frame = _imp->lifeTimeFrame.lock();
frame->setSecret(lifetime_i == 4 || lifetime_i == 0);
if (lifetime_i != 4) {
frame->setValue(getTimelineCurrentTime(), view);
}
}
void
RotoContext::onAutoKeyingChanged(bool enabled)
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
QMutexLocker l(&_imp->rotoContextMutex);
_imp->autoKeying = enabled;
}
void
RotoContext::onFeatherLinkChanged(bool enabled)
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
QMutexLocker l(&_imp->rotoContextMutex);
_imp->featherLink = enabled;
}
void
RotoContext::onRippleEditChanged(bool enabled)
{
///MT-safe: only called on the main-thread
assert( QThread::currentThread() == qApp->thread() );
QMutexLocker l(&_imp->rotoContextMutex);
_imp->rippleEdit = enabled;
}
void
RotoContext::getGlobalMotionBlurSettings(const double time,
double* startTime,
double* endTime,
double* timeStep) const
{
*startTime = time, *timeStep = 1., *endTime = time;
#ifdef NATRON_ROTO_ENABLE_MOTION_BLUR
double motionBlurAmnt = _imp->globalMotionBlurKnob.lock()->getValueAtTime(time);
if (motionBlurAmnt == 0) {
return;
}
int nbSamples = std::floor(motionBlurAmnt * 10 + 0.5);
double shutterInterval = _imp->globalMotionBlurKnob.lock()->getValueAtTime(time);
if (shutterInterval == 0) {
return;
}
int shutterType_i = _imp->globalMotionBlurKnob.lock()->getValueAtTime(time);
if (nbSamples != 0) {
*timeStep = shutterInterval / nbSamples;
}
if (shutterType_i == 0) { // centered
*startTime = time - shutterInterval / 2.;
*endTime = time + shutterInterval / 2.;
} else if (shutterType_i == 1) { // start
*startTime = time;
*endTime = time + shutterInterval;
} else if (shutterType_i == 2) { // end
*startTime = time - shutterInterval;
*endTime = time;
} else if (shutterType_i == 3) { // custom
*startTime = time + _imp->globalCustomOffsetKnob.lock()->getValueAtTime(time);
*endTime = *startTime + shutterInterval;
} else {
assert(false);
}
#endif
}
void
RotoContext::getItemsRegionOfDefinition(const std::list<RotoItemPtr>& items,
double time,
ViewIdx /*view*/,
RectD* rod) const
{
double startTime = time, mbFrameStep = 1., endTime = time;
#ifdef NATRON_ROTO_ENABLE_MOTION_BLUR
int mbType_i = getMotionBlurTypeKnob()->getValue();
bool applyGlobalMotionBlur = mbType_i == 1;
if (applyGlobalMotionBlur) {
getGlobalMotionBlurSettings(time, &startTime, &endTime, &mbFrameStep);
}
#endif
bool rodSet = false;
NodePtr activeRotoPaintNode;
RotoStrokeItemPtr activeStroke;
bool isDrawing = false;
getNode()->getApp()->getActiveRotoDrawingStroke(&activeRotoPaintNode, &activeStroke, &isDrawing);
if (!isDrawing) {
activeStroke.reset();
}
QMutexLocker l(&_imp->rotoContextMutex);
for (double t = startTime; t <= endTime; t += mbFrameStep) {
bool first = true;
RectD bbox;
for (std::list<RotoItemPtr>::const_iterator it2 = items.begin(); it2 != items.end(); ++it2) {
Bezier* isBezier = dynamic_cast<Bezier*>( it2->get() );
RotoStrokeItem* isStroke = dynamic_cast<RotoStrokeItem*>( it2->get() );
if (isBezier && !isStroke) {
if ( isBezier->isActivated(time) && (isBezier->getControlPointsCount() > 1) ) {
RectD splineRoD = isBezier->getBoundingBox(t);
if ( splineRoD.isNull() ) {
continue;
}
if (first) {
first = false;
bbox = splineRoD;
} else {
bbox.merge(splineRoD);
}
}
} else if (isStroke) {
RectD strokeRod;
if ( isStroke->isActivated(time) ) {
if ( isStroke == activeStroke.get() ) {
strokeRod = isStroke->getMergeNode()->getPaintStrokeRoD_duringPainting();
} else {
strokeRod = isStroke->getBoundingBox(t);
}
if (first) {
first = false;
bbox = strokeRod;
} else {
bbox.merge(strokeRod);
}
}
}
}
if (!rodSet) {
*rod = bbox;
rodSet = true;
} else {
rod->merge(bbox);
}
}
} // RotoContext::getItemsRegionOfDefinition
void
RotoContext::getMaskRegionOfDefinition(double time,
ViewIdx view,
RectD* rod) // rod is in canonical coordinates
const
{
std::list<RotoItemPtr> allItems;
{
QMutexLocker l(&_imp->rotoContextMutex);
for (std::list<RotoLayerPtr>::const_iterator it = _imp->layers.begin(); it != _imp->layers.end(); ++it) {
RotoItems items = (*it)->getItems_mt_safe();
for (RotoItems::iterator it2 = items.begin(); it2 != items.end(); ++it2) {
allItems.push_back(*it2);
}
}
}
getItemsRegionOfDefinition(allItems, time, view, rod);
}
bool
RotoContext::isRotoPaintTreeConcatenatableInternal(const std::list<RotoDrawableItemPtr>& items,
int* blendingMode)
{
bool operatorSet = false;
int comp_i = -1;
for (std::list<RotoDrawableItemPtr>::const_iterator it = items.begin(); it != items.end(); ++it) {
int op = (*it)->getCompositingOperator();
if (!operatorSet) {
operatorSet = true;
comp_i = op;
} else {
if (op != comp_i) {
//2 items have a different compositing operator
return false;
}
}
RotoStrokeItem* isStroke = dynamic_cast<RotoStrokeItem*>( it->get() );
if (!isStroke) {
assert( dynamic_cast<Bezier*>( it->get() ) );
} else {
if (isStroke->getBrushType() != eRotoStrokeTypeSolid) {
return false;
}
}
}
if (operatorSet) {
*blendingMode = comp_i;
return true;
}
return false;
}
bool
RotoContext::isRotoPaintTreeConcatenatable() const
{
// Do not use only activated items when defining the shape of the RotoPaint tree otherwise we would have to adjust the tree at each frame.
std::list<RotoDrawableItemPtr> items = getCurvesByRenderOrder(false /*onlyActivatedItems*/);
int bop;
return isRotoPaintTreeConcatenatableInternal(items, &bop);
}
bool
RotoContext::isEmpty() const
{
QMutexLocker l(&_imp->rotoContextMutex);
return _imp->layers.empty();
}
void
RotoContext::save(RotoContextSerialization* obj) const
{
QMutexLocker l(&_imp->rotoContextMutex);
obj->_autoKeying = _imp->autoKeying;
obj->_featherLink = _imp->featherLink;
obj->_rippleEdit = _imp->rippleEdit;
///There must always be the base layer
assert( !_imp->layers.empty() );
///Serializing this layer will recursively serialize everything
_imp->layers.front()->save( dynamic_cast<RotoItemSerialization*>(&obj->_baseLayer) );
///the age of the context is not serialized as the images are wiped from the cache anyway
///Serialize the selection
for (std::list<RotoItemPtr>::const_iterator it = _imp->selectedItems.begin(); it != _imp->selectedItems.end(); ++it) {
obj->_selectedItems.push_back( (*it)->getScriptName() );
}
}
static void