forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotoDrawableItem.cpp
1526 lines (1329 loc) · 49.9 KB
/
RotoDrawableItem.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 "RotoDrawableItem.h"
#include <algorithm> // min, max
#include <sstream>
#include <locale>
#include <limits>
#include <cassert>
#include <cstring> // for std::memcpy
#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/CreateNodeArgs.h"
#include "Engine/NodeSerialization.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/Interpolation.h"
#include "Engine/Project.h"
#include "Engine/KnobSerialization.h"
#include "Engine/RenderStats.h"
#include "Engine/RotoDrawableItemSerialization.h"
#include "Engine/RotoLayer.h"
#include "Engine/RotoStrokeItem.h"
#include "Engine/RotoContext.h"
#include "Engine/Settings.h"
#include "Engine/TimeLine.h"
#include "Engine/Transform.h"
#include "Engine/ViewIdx.h"
#include "Engine/ViewerInstance.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
NATRON_NAMESPACE_ENTER
////////////////////////////////////RotoDrawableItem////////////////////////////////////
RotoDrawableItem::RotoDrawableItem(const RotoContextPtr& context,
const std::string & name,
const RotoLayerPtr& parent,
bool isStroke)
: RotoItem(context, name, parent)
, _imp( new RotoDrawableItemPrivate(isStroke) )
{
#ifdef NATRON_ROTO_INVERTIBLE
QObject::connect( _imp->inverted->getSignalSlotHandler().get(), SIGNAL(valueChanged(ViewSpec,int,int)), this, SIGNAL(invertedStateChanged()) );
#endif
QObject::connect( this, SIGNAL(overlayColorChanged()), context.get(), SIGNAL(refreshViewerOverlays()) );
QObject::connect( _imp->color->getSignalSlotHandler().get(), SIGNAL(valueChanged(ViewSpec,int,int)), this, SIGNAL(shapeColorChanged()) );
QObject::connect( _imp->compOperator->getSignalSlotHandler().get(), SIGNAL(valueChanged(ViewSpec,int,int)), this,
SIGNAL(compositingOperatorChanged(ViewSpec,int,int)) );
std::vector<ChoiceOption> operators;
Merge::getOperatorStrings(&operators);
_imp->compOperator->populateChoices(operators);
_imp->compOperator->setDefaultValueFromID( Merge::getOperatorString(eMergeCopy) );
}
RotoDrawableItem::~RotoDrawableItem()
{
}
void
RotoDrawableItem::addKnob(const KnobIPtr& knob)
{
_imp->knobs.push_back(knob);
}
void
RotoDrawableItem::setNodesThreadSafetyForRotopainting()
{
assert( boost::dynamic_pointer_cast<RotoStrokeItem>( boost::dynamic_pointer_cast<RotoDrawableItem>( shared_from_this() ) ) );
getContext()->getNode()->setRenderThreadSafety(eRenderSafetyInstanceSafe);
getContext()->setWhileCreatingPaintStrokeOnMergeNodes(true);
if (_imp->effectNode) {
_imp->effectNode->setWhileCreatingPaintStroke(true);
_imp->effectNode->setRenderThreadSafety(eRenderSafetyInstanceSafe);
}
if (_imp->mergeNode) {
_imp->mergeNode->setWhileCreatingPaintStroke(true);
_imp->mergeNode->setRenderThreadSafety(eRenderSafetyInstanceSafe);
}
if (_imp->timeOffsetNode) {
_imp->timeOffsetNode->setWhileCreatingPaintStroke(true);
_imp->timeOffsetNode->setRenderThreadSafety(eRenderSafetyInstanceSafe);
}
if (_imp->frameHoldNode) {
_imp->frameHoldNode->setWhileCreatingPaintStroke(true);
_imp->frameHoldNode->setRenderThreadSafety(eRenderSafetyInstanceSafe);
}
}
void
RotoDrawableItem::createNodes(bool connectNodes)
{
const std::list<KnobIPtr>& knobs = getKnobs();
for (std::list<KnobIPtr>::const_iterator it = knobs.begin(); it != knobs.end(); ++it) {
QObject::connect( (*it)->getSignalSlotHandler().get(), SIGNAL(valueChanged(ViewSpec,int,int)), this, SLOT(onRotoKnobChanged(ViewSpec,int,int)) );
}
RotoContextPtr context = getContext();
NodePtr node = context->getNode();
AppInstancePtr app = node->getApp();
QString fixedNamePrefix = QString::fromUtf8( node->getScriptName_mt_safe().c_str() );
fixedNamePrefix.append( QLatin1Char('_') );
fixedNamePrefix.append( QString::fromUtf8( getScriptName().c_str() ) );
fixedNamePrefix.append( QLatin1Char('_') );
fixedNamePrefix.append( QString::number( context->getAge() ) );
fixedNamePrefix.append( QLatin1Char('_') );
QString pluginId;
RotoStrokeType type;
RotoDrawableItemPtr thisShared = boost::dynamic_pointer_cast<RotoDrawableItem>( shared_from_this() );
assert(thisShared);
RotoStrokeItemPtr isStroke = boost::dynamic_pointer_cast<RotoStrokeItem>(thisShared);
if (isStroke) {
type = isStroke->getBrushType();
} else {
type = eRotoStrokeTypeSolid;
}
switch (type) {
case eRotoStrokeTypeBlur:
pluginId = QString::fromUtf8(PLUGINID_OFX_BLURCIMG);
break;
case eRotoStrokeTypeEraser:
pluginId = QString::fromUtf8(PLUGINID_OFX_CONSTANT);
break;
case eRotoStrokeTypeSolid:
pluginId = QString::fromUtf8(PLUGINID_OFX_ROTO);
break;
case eRotoStrokeTypeClone:
case eRotoStrokeTypeReveal:
pluginId = QString::fromUtf8(PLUGINID_OFX_TRANSFORM);
break;
case eRotoStrokeTypeBurn:
case eRotoStrokeTypeDodge:
//uses merge
break;
case eRotoStrokeTypeSharpen:
//todo
break;
case eRotoStrokeTypeSmear:
pluginId = QString::fromUtf8(PLUGINID_NATRON_ROTOSMEAR);
break;
}
QString baseFixedName = fixedNamePrefix;
if ( !pluginId.isEmpty() ) {
fixedNamePrefix.append( QString::fromUtf8("Effect") );
CreateNodeArgs args( pluginId.toStdString(), NodeCollectionPtr() );
args.setProperty<bool>(kCreateNodeArgsPropOutOfProject, true);
args.setProperty<bool>(kCreateNodeArgsPropNoNodeGUI, true);
args.setProperty<bool>(kCreateNodeArgsPropTrustPluginID, true);
args.setProperty<std::string>(kCreateNodeArgsPropNodeInitialName, fixedNamePrefix.toStdString());
args.setProperty<bool>(kCreateNodeArgsPropAllowNonUserCreatablePlugins, true);
_imp->effectNode = app->createNode(args);
if (!_imp->effectNode) {
throw std::runtime_error("Rotopaint requires the plug-in " + pluginId.toStdString() + " in order to work");
}
assert(_imp->effectNode);
if ( (type == eRotoStrokeTypeClone) || (type == eRotoStrokeTypeReveal) ) {
{
fixedNamePrefix = baseFixedName;
fixedNamePrefix.append( QString::fromUtf8("TimeOffset") );
CreateNodeArgs args(PLUGINID_OFX_TIMEOFFSET, NodeCollectionPtr() );
args.setProperty<bool>(kCreateNodeArgsPropOutOfProject, true);
args.setProperty<bool>(kCreateNodeArgsPropNoNodeGUI, true);
args.setProperty<std::string>(kCreateNodeArgsPropNodeInitialName, fixedNamePrefix.toStdString());
_imp->timeOffsetNode = app->createNode(args);
if (!_imp->timeOffsetNode) {
throw std::runtime_error("Rotopaint requires the plug-in " PLUGINID_OFX_TIMEOFFSET " in order to work");
}
assert(_imp->timeOffsetNode);
}
{
fixedNamePrefix = baseFixedName;
fixedNamePrefix.append( QString::fromUtf8("FrameHold") );
CreateNodeArgs args( PLUGINID_OFX_FRAMEHOLD, NodeCollectionPtr() );
args.setProperty<bool>(kCreateNodeArgsPropOutOfProject, true);
args.setProperty<bool>(kCreateNodeArgsPropNoNodeGUI, true);
args.setProperty<std::string>(kCreateNodeArgsPropNodeInitialName, fixedNamePrefix.toStdString());
_imp->frameHoldNode = app->createNode(args);
if (!_imp->frameHoldNode) {
throw std::runtime_error("Rotopaint requires the plug-in " PLUGINID_OFX_FRAMEHOLD " in order to work");
}
assert(_imp->frameHoldNode);
}
}
}
fixedNamePrefix = baseFixedName;
fixedNamePrefix.append( QString::fromUtf8("Merge") );
CreateNodeArgs args( PLUGINID_OFX_MERGE, NodeCollectionPtr() );
args.setProperty<bool>(kCreateNodeArgsPropOutOfProject, true);
args.setProperty<bool>(kCreateNodeArgsPropNoNodeGUI, true);
args.setProperty<std::string>(kCreateNodeArgsPropNodeInitialName, fixedNamePrefix.toStdString());
_imp->mergeNode = app->createNode(args);
if (!_imp->mergeNode) {
throw std::runtime_error("Rotopaint requires the plug-in " PLUGINID_OFX_MERGE " in order to work");
}
assert(_imp->mergeNode);
if ( (type != eRotoStrokeTypeSolid) && (type != eRotoStrokeTypeSmear) ) {
int maxInp = _imp->mergeNode->getNInputs();
for (int i = 0; i < maxInp; ++i) {
if ( _imp->mergeNode->getEffectInstance()->isInputMask(i) ) {
//Connect this rotopaint node as a mask
bool ok = _imp->mergeNode->connectInput(node, i);
assert(ok);
Q_UNUSED(ok);
break;
}
}
}
KnobIPtr mergeOperatorKnob = _imp->mergeNode->getKnobByName(kMergeOFXParamOperation);
assert(mergeOperatorKnob);
KnobChoice* mergeOp = dynamic_cast<KnobChoice*>( mergeOperatorKnob.get() );
assert(mergeOp);
KnobChoicePtr compOp = getOperatorKnob();
MergingFunctionEnum op;
if ( (type == eRotoStrokeTypeDodge) || (type == eRotoStrokeTypeBurn) ) {
op = (type == eRotoStrokeTypeDodge ? eMergeColorDodge : eMergeColorBurn);
} else if (type == eRotoStrokeTypeSolid) {
op = eMergeOver;
} else {
op = eMergeCopy;
}
if (mergeOp) {
mergeOp->setValueFromID(Merge::getOperatorString(op), 0);
}
compOp->setValueFromID(Merge::getOperatorString(op), 0);
if (isStroke) {
if (type == eRotoStrokeTypeBlur) {
double strength = isStroke->getBrushEffectKnob()->getValue();
KnobIPtr knob = _imp->effectNode->getKnobByName(kBlurCImgParamSize);
KnobDouble* isDbl = dynamic_cast<KnobDouble*>( knob.get() );
if (isDbl) {
isDbl->setValues(strength, strength, ViewSpec::current(), eValueChangedReasonNatronInternalEdited);
}
} else if (type == eRotoStrokeTypeSharpen) {
//todo
} else if (type == eRotoStrokeTypeSmear) {
KnobDoublePtr spacingKnob = isStroke->getBrushSpacingKnob();
assert(spacingKnob);
spacingKnob->setValue(0.05);
}
setNodesThreadSafetyForRotopainting();
}
///Attach this stroke to the underlying nodes used
if (_imp->effectNode) {
_imp->effectNode->attachRotoItem(thisShared);
}
if (_imp->mergeNode) {
_imp->mergeNode->attachRotoItem(thisShared);
}
if (_imp->timeOffsetNode) {
_imp->timeOffsetNode->attachRotoItem(thisShared);
}
if (_imp->frameHoldNode) {
_imp->frameHoldNode->attachRotoItem(thisShared);
}
if (connectNodes) {
refreshNodesConnections();
}
} // RotoDrawableItem::createNodes
std::string
RotoDrawableItem::getCacheID() const
{
assert(_imp->mergeNode);
//The cache iD is the one of the internal merge node + RotoDrawableItem
return _imp->mergeNode->getCacheID() + ".RotoDrawableItem";
}
void
RotoDrawableItem::disconnectNodes()
{
_imp->mergeNode->disconnectInput(0);
_imp->mergeNode->disconnectInput(1);
if (_imp->effectNode) {
_imp->effectNode->disconnectInput(0);
}
if (_imp->timeOffsetNode) {
_imp->timeOffsetNode->disconnectInput(0);
}
if (_imp->frameHoldNode) {
_imp->frameHoldNode->disconnectInput(0);
}
}
void
RotoDrawableItem::deactivateNodes()
{
if (_imp->effectNode) {
_imp->effectNode->deactivate(std::list<NodePtr>(), true, false, false, false);
}
if (_imp->mergeNode) {
_imp->mergeNode->deactivate(std::list<NodePtr>(), true, false, false, false);
}
if (_imp->timeOffsetNode) {
_imp->timeOffsetNode->deactivate(std::list<NodePtr>(), true, false, false, false);
}
if (_imp->frameHoldNode) {
_imp->frameHoldNode->deactivate(std::list<NodePtr>(), true, false, false, false);
}
}
void
RotoDrawableItem::activateNodes()
{
if (_imp->effectNode) {
_imp->effectNode->activate(std::list<NodePtr>(), false, false);
}
_imp->mergeNode->activate(std::list<NodePtr>(), false, false);
if (_imp->timeOffsetNode) {
_imp->timeOffsetNode->activate(std::list<NodePtr>(), false, false);
}
if (_imp->frameHoldNode) {
_imp->frameHoldNode->activate(std::list<NodePtr>(), false, false);
}
}
static RotoDrawableItem*
findPreviousOfItemInLayer(RotoLayer* layer,
RotoItem* item)
{
RotoItems layerItems = layer->getItems_mt_safe();
if ( layerItems.empty() ) {
return 0;
}
RotoItems::iterator found = layerItems.end();
if (item) {
for (RotoItems::iterator it = layerItems.begin(); it != layerItems.end(); ++it) {
if (it->get() == item) {
found = it;
break;
}
}
assert( found != layerItems.end() );
} else {
found = layerItems.end();
}
if ( found != layerItems.end() ) {
++found;
for (; found != layerItems.end(); ++found) {
//We found another stroke below at the same level
RotoDrawableItem* isDrawable = dynamic_cast<RotoDrawableItem*>( found->get() );
if (isDrawable) {
assert(isDrawable != item);
return isDrawable;
}
//Cycle through a layer that is at the same level
RotoLayer* isLayer = dynamic_cast<RotoLayer*>( found->get() );
if (isLayer) {
RotoDrawableItem* si = findPreviousOfItemInLayer(isLayer, 0);
if (si) {
assert(si != item);
return si;
}
}
}
}
//Item was still not found, find in great parent layer
RotoLayerPtr parentLayer = layer->getParentLayer();
if (!parentLayer) {
return 0;
}
RotoItems greatParentItems = parentLayer->getItems_mt_safe();
found = greatParentItems.end();
for (RotoItems::iterator it = greatParentItems.begin(); it != greatParentItems.end(); ++it) {
if (it->get() == layer) {
found = it;
break;
}
}
assert( found != greatParentItems.end() );
RotoDrawableItem* ret = findPreviousOfItemInLayer(parentLayer.get(), layer);
//assert(ret != item); // This assert will pop up when creating 2 layers in a row (one parent and child)
return ret;
} // findPreviousOfItemInLayer
RotoDrawableItem*
RotoDrawableItem::findPreviousInHierarchy()
{
RotoLayerPtr layer = getParentLayer();
if (!layer) {
return 0;
}
return findPreviousOfItemInLayer(layer.get(), this);
}
void
RotoDrawableItem::onRotoKnobChanged(ViewSpec /*view*/,
int /*dimension*/,
int reason)
{
KnobSignalSlotHandler* handler = qobject_cast<KnobSignalSlotHandler*>( sender() );
if (!handler) {
return;
}
KnobIPtr triggerKnob = handler->getKnob();
assert(triggerKnob);
rotoKnobChanged(triggerKnob, (ValueChangedReasonEnum)reason);
}
void
RotoDrawableItem::rotoKnobChanged(const KnobIPtr& knob,
ValueChangedReasonEnum reason)
{
KnobChoicePtr compKnob = getOperatorKnob();
#ifdef NATRON_ROTO_INVERTIBLE
KnobBoolPtr invertKnob = getInvertedKnob();
#endif
RotoStrokeItem* isStroke = dynamic_cast<RotoStrokeItem*>(this);
RotoStrokeType type;
if (isStroke) {
type = isStroke->getBrushType();
} else {
type = eRotoStrokeTypeSolid;
}
if ( (reason == eValueChangedReasonSlaveRefresh) && (knob != _imp->center) && (knob != _imp->cloneCenter) ) {
getContext()->s_breakMultiStroke();
}
if (knob == compKnob) {
KnobIPtr mergeOperatorKnob = _imp->mergeNode->getKnobByName(kMergeOFXParamOperation);
KnobChoice* mergeOp = dynamic_cast<KnobChoice*>( mergeOperatorKnob.get() );
if (mergeOp) {
mergeOp->setValueFromID(compKnob->getEntry( compKnob->getValue() ).id, 0);
}
///Since the compositing operator might have changed, we may have to change the rotopaint tree layout
if (reason == eValueChangedReasonUserEdited) {
getContext()->refreshRotoPaintTree();
}
}
#ifdef NATRON_ROTO_INVERTIBLE
else if (knob == invertKnob) {
KnobIPtr mergeMaskInvertKnob = _imp->mergeNode->getKnobByName(kMergeOFXParamInvertMask);
KnobBool* mergeMaskInv = dynamic_cast<KnobBool*>( mergeMaskInvertKnob.get() );
if (mergeMaskInv) {
mergeMaskInv->setValue( invertKnob->getValue() );
}
///Since the invert state might have changed, we may have to change the rotopaint tree layout
if (reason == eValueChangedReasonUserEdited) {
getContext()->refreshRotoPaintTree();
}
}
#endif
else if (knob == _imp->sourceColor) {
refreshNodesConnections();
} else if (knob == _imp->effectStrength) {
double strength = _imp->effectStrength->getValue();
switch (type) {
case eRotoStrokeTypeBlur: {
KnobIPtr knob = _imp->effectNode->getKnobByName(kBlurCImgParamSize);
KnobDouble* isDbl = dynamic_cast<KnobDouble*>( knob.get() );
if (isDbl) {
isDbl->setValues(strength, strength, ViewSpec::all(), eValueChangedReasonNatronInternalEdited);
}
break;
}
case eRotoStrokeTypeSharpen: {
//todo
break;
}
default:
//others don't have a control
break;
}
} else if ( (knob == _imp->timeOffset) && _imp->timeOffsetNode ) {
int offsetMode_i = _imp->timeOffsetMode->getValue();
KnobIPtr offsetKnob;
if (offsetMode_i == 0) {
offsetKnob = _imp->timeOffsetNode->getKnobByName(kTimeOffsetParamOffset);
} else {
offsetKnob = _imp->frameHoldNode->getKnobByName(kFrameHoldParamFirstFrame);
}
KnobInt* offset = dynamic_cast<KnobInt*>( offsetKnob.get() );
if (offset) {
double value = _imp->timeOffset->getValue();
offset->setValue(value);
}
} else if ( (knob == _imp->timeOffsetMode) && _imp->timeOffsetNode ) {
refreshNodesConnections();
}
if ( (type == eRotoStrokeTypeClone) || (type == eRotoStrokeTypeReveal) ) {
if (knob == _imp->cloneTranslate) {
KnobIPtr translateKnob = _imp->effectNode->getKnobByName(kTransformParamTranslate);
KnobDouble* translate = dynamic_cast<KnobDouble*>( translateKnob.get() );
if (translate) {
translate->clone( _imp->cloneTranslate.get() );
}
} else if (knob == _imp->cloneRotate) {
KnobIPtr rotateKnob = _imp->effectNode->getKnobByName(kTransformParamRotate);
KnobDouble* rotate = dynamic_cast<KnobDouble*>( rotateKnob.get() );
if (rotate) {
rotate->clone( _imp->cloneRotate.get() );
}
} else if (knob == _imp->cloneScale) {
KnobIPtr scaleKnob = _imp->effectNode->getKnobByName(kTransformParamScale);
KnobDouble* scale = dynamic_cast<KnobDouble*>( scaleKnob.get() );
if (scale) {
scale->clone( _imp->cloneScale.get() );
}
} else if (knob == _imp->cloneScaleUniform) {
KnobIPtr uniformKnob = _imp->effectNode->getKnobByName(kTransformParamUniform);
KnobBool* uniform = dynamic_cast<KnobBool*>( uniformKnob.get() );
if (uniform) {
uniform->clone( _imp->cloneScaleUniform.get() );
}
} else if (knob == _imp->cloneSkewX) {
KnobIPtr skewxKnob = _imp->effectNode->getKnobByName(kTransformParamSkewX);
KnobDouble* skewX = dynamic_cast<KnobDouble*>( skewxKnob.get() );
if (skewX) {
skewX->clone( _imp->cloneSkewX.get() );
}
} else if (knob == _imp->cloneSkewY) {
KnobIPtr skewyKnob = _imp->effectNode->getKnobByName(kTransformParamSkewY);
KnobDouble* skewY = dynamic_cast<KnobDouble*>( skewyKnob.get() );
if (skewY) {
skewY->clone( _imp->cloneSkewY.get() );
}
} else if (knob == _imp->cloneSkewOrder) {
KnobIPtr skewOrderKnob = _imp->effectNode->getKnobByName(kTransformParamSkewOrder);
KnobChoice* skewOrder = dynamic_cast<KnobChoice*>( skewOrderKnob.get() );
if (skewOrder) {
skewOrder->clone( _imp->cloneSkewOrder.get() );
}
} else if (knob == _imp->cloneCenter) {
KnobIPtr centerKnob = _imp->effectNode->getKnobByName(kTransformParamCenter);
KnobDouble* center = dynamic_cast<KnobDouble*>( centerKnob.get() );
if (center) {
center->clone( _imp->cloneCenter.get() );
}
} else if (knob == _imp->cloneFilter) {
KnobIPtr filterKnob = _imp->effectNode->getKnobByName(kTransformParamFilter);
KnobChoice* filter = dynamic_cast<KnobChoice*>( filterKnob.get() );
if (filter) {
filter->clone( _imp->cloneFilter.get() );
}
} else if (knob == _imp->cloneBlackOutside) {
KnobIPtr boKnob = _imp->effectNode->getKnobByName(kTransformParamBlackOutside);
KnobBool* bo = dynamic_cast<KnobBool*>( boKnob.get() );
if (bo) {
bo->clone( _imp->cloneBlackOutside.get() );
}
}
}
incrementNodesAge();
} // RotoDrawableItem::rotoKnobChanged
void
RotoDrawableItem::incrementNodesAge()
{
if ( getContext()->getNode()->getApp()->getProject()->isLoadingProject() ) {
return;
}
if (_imp->effectNode) {
_imp->effectNode->incrementKnobsAge();
}
if (_imp->mergeNode) {
_imp->mergeNode->incrementKnobsAge();
}
if (_imp->timeOffsetNode) {
_imp->timeOffsetNode->incrementKnobsAge();
}
if (_imp->frameHoldNode) {
_imp->frameHoldNode->incrementKnobsAge();
}
}
NodePtr
RotoDrawableItem::getEffectNode() const
{
return _imp->effectNode;
}
NodePtr
RotoDrawableItem::getMergeNode() const
{
return _imp->mergeNode;
}
NodePtr
RotoDrawableItem::getTimeOffsetNode() const
{
return _imp->timeOffsetNode;
}
NodePtr
RotoDrawableItem::getFrameHoldNode() const
{
return _imp->frameHoldNode;
}
void
RotoDrawableItem::refreshNodesConnections()
{
RotoDrawableItem* previous = findPreviousInHierarchy();
NodePtr rotoPaintInput = getContext()->getNode()->getInput(0);
NodePtr upstreamNode = previous ? previous->getMergeNode() : rotoPaintInput;
RotoStrokeItem* isStroke = dynamic_cast<RotoStrokeItem*>(this);
RotoStrokeType type;
if (isStroke) {
type = isStroke->getBrushType();
} else {
type = eRotoStrokeTypeSolid;
}
if ( _imp->effectNode &&
( type != eRotoStrokeTypeEraser) ) {
/*
Tree for this effect goes like this:
Effect - <Optional Time Node>------- Reveal input (Reveal/Clone) or Upstream Node otherwise
/
Merge
\--------------------------------------Upstream Node
*/
NodePtr effectInput;
if (!_imp->timeOffsetNode) {
effectInput = _imp->effectNode;
} else {
double timeOffsetMode_i = _imp->timeOffsetMode->getValue();
if (timeOffsetMode_i == 0) {
//relative
effectInput = _imp->timeOffsetNode;
} else {
effectInput = _imp->frameHoldNode;
}
if (_imp->effectNode->getInput(0) != effectInput) {
_imp->effectNode->disconnectInput(0);
_imp->effectNode->connectInputBase(effectInput, 0);
}
}
/*
* This case handles: Stroke, Blur, Sharpen, Smear, Clone
*/
if (_imp->mergeNode->getInput(1) != _imp->effectNode) {
_imp->mergeNode->disconnectInput(1);
_imp->mergeNode->connectInputBase(_imp->effectNode, 1); // A
}
if (_imp->mergeNode->getInput(0) != upstreamNode) {
_imp->mergeNode->disconnectInput(0);
if (upstreamNode) {
_imp->mergeNode->connectInputBase(upstreamNode, 0); // B
}
}
int reveal_i = _imp->sourceColor->getValue();
NodePtr revealInput;
bool shouldUseUpstreamForReveal = true;
if ( ( (type == eRotoStrokeTypeReveal) ||
( type == eRotoStrokeTypeClone) ) && ( reveal_i > 0) ) {
shouldUseUpstreamForReveal = false;
revealInput = getContext()->getNode()->getInput(reveal_i - 1);
}
if (!revealInput && shouldUseUpstreamForReveal) {
if (type != eRotoStrokeTypeSolid) {
revealInput = upstreamNode;
}
}
if (revealInput) {
if (effectInput->getInput(0) != revealInput) {
effectInput->disconnectInput(0);
effectInput->connectInputBase(revealInput, 0);
}
} else {
if ( effectInput->getInput(0) ) {
effectInput->disconnectInput(0);
}
}
} else {
assert(type == eRotoStrokeTypeEraser ||
type == eRotoStrokeTypeDodge ||
type == eRotoStrokeTypeBurn);
if (type == eRotoStrokeTypeEraser) {
/*
Tree for this effect goes like this:
Constant or RotoPaint bg input
/
Merge
\--------------------Upstream Node
*/
NodePtr eraserInput = rotoPaintInput ? rotoPaintInput : _imp->effectNode;
if (_imp->mergeNode->getInput(1) != eraserInput) {
_imp->mergeNode->disconnectInput(1);
if (eraserInput) {
_imp->mergeNode->connectInputBase(eraserInput, 1); // A
}
}
if (_imp->mergeNode->getInput(0) != upstreamNode) {
_imp->mergeNode->disconnectInput(0);
if (upstreamNode) {
_imp->mergeNode->connectInputBase(upstreamNode, 0); // B
}
}
} else if ( (type == eRotoStrokeTypeDodge) || (type == eRotoStrokeTypeBurn) ) {
/*
Tree for this effect goes like this:
Upstream Node
/
Merge (Dodge/Burn)
\Upstream Node
*/
if (_imp->mergeNode->getInput(1) != upstreamNode) {
_imp->mergeNode->disconnectInput(1);
if (upstreamNode) {
_imp->mergeNode->connectInputBase(upstreamNode, 1); // A
}
}
if (_imp->mergeNode->getInput(0) != upstreamNode) {
_imp->mergeNode->disconnectInput(0);
if (upstreamNode) {
_imp->mergeNode->connectInputBase(upstreamNode, 0); // B
}
}
} else {
//unhandled case
assert(false);
}
} //if (_imp->effectNode && type != eRotoStrokeTypeEraser)
} // RotoDrawableItem::refreshNodesConnections
void
RotoDrawableItem::resetNodesThreadSafety()
{
if (_imp->effectNode) {
_imp->effectNode->revertToPluginThreadSafety();
}
_imp->mergeNode->revertToPluginThreadSafety();
if (_imp->timeOffsetNode) {
_imp->timeOffsetNode->revertToPluginThreadSafety();
}
if (_imp->frameHoldNode) {
_imp->frameHoldNode->revertToPluginThreadSafety();
}
getContext()->getNode()->revertToPluginThreadSafety();
}
void
RotoDrawableItem::clone(const RotoItem* other)
{
const RotoDrawableItem* otherDrawable = dynamic_cast<const RotoDrawableItem*>(other);
if (!otherDrawable) {
return;
}
const std::list<KnobIPtr>& otherKnobs = otherDrawable->getKnobs();
assert( otherKnobs.size() == _imp->knobs.size() );
if ( otherKnobs.size() != _imp->knobs.size() ) {
return;
}
std::list<KnobIPtr>::iterator it = _imp->knobs.begin();
std::list<KnobIPtr>::const_iterator otherIt = otherKnobs.begin();
for (; it != _imp->knobs.end(); ++it, ++otherIt) {
(*it)->clone(*otherIt);
}
{
QMutexLocker l(&itemMutex);
std::memcpy(_imp->overlayColor, otherDrawable->_imp->overlayColor, sizeof(double) * 4);
}
RotoItem::clone(other);
}
static void
serializeRotoKnob(const KnobIPtr & knob,
KnobSerialization* serialization)
{
std::pair<int, KnobIPtr> master = knob->getMaster(0);
if (master.second) {
serialization->initialize(master.second);
} else {
serialization->initialize(knob);
}
}
void
RotoDrawableItem::save(RotoItemSerialization *obj) const
{
RotoDrawableItemSerialization* s = dynamic_cast<RotoDrawableItemSerialization*>(obj);
assert(s);
if (!s) {
throw std::logic_error("RotoDrawableItem::save()");
}
for (std::list<KnobIPtr>::const_iterator it = _imp->knobs.begin(); it != _imp->knobs.end(); ++it) {
KnobSerializationPtr k = boost::make_shared<KnobSerialization>(*it);
serializeRotoKnob( *it, k.get() );
s->_knobs.push_back(k);
}
{
QMutexLocker l(&itemMutex);
std::memcpy(s->_overlayColor, _imp->overlayColor, sizeof(double) * 4);
}
RotoItem::save(obj);
}
void
RotoDrawableItem::load(const RotoItemSerialization &obj)
{
RotoItem::load(obj);
const RotoDrawableItemSerialization & s = dynamic_cast<const RotoDrawableItemSerialization &>(obj);
for (std::list<KnobSerializationPtr>::const_iterator it = s._knobs.begin(); it != s._knobs.end(); ++it) {
for (std::list<KnobIPtr>::const_iterator it2 = _imp->knobs.begin(); it2 != _imp->knobs.end(); ++it2) {
if ( (*it2)->getName() == (*it)->getName() ) {
KnobSignalSlotHandlerPtr s = (*it2)->getSignalSlotHandler();
s->blockSignals(true);
(*it2)->clone( (*it)->getKnob().get() );
s->blockSignals(false);
break;
}
}
}
{
QMutexLocker l(&itemMutex);
std::memcpy(_imp->overlayColor, s._overlayColor, sizeof(double) * 4);
}
RotoStrokeType type;
RotoStrokeItem* isStroke = dynamic_cast<RotoStrokeItem*>(this);
if (isStroke) {
type = isStroke->getBrushType();
} else {
type = eRotoStrokeTypeSolid;
}
KnobChoicePtr compKnob = getOperatorKnob();
KnobIPtr mergeOperatorKnob = _imp->mergeNode->getKnobByName(kMergeOFXParamOperation);
KnobChoice* mergeOp = dynamic_cast<KnobChoice*>( mergeOperatorKnob.get() );
if (mergeOp) {
mergeOp->setValueFromID(compKnob->getEntry( compKnob->getValue() ).id, 0);
}
if ( (type == eRotoStrokeTypeClone) || (type == eRotoStrokeTypeReveal) ) {
KnobIPtr translateKnob = _imp->effectNode->getKnobByName(kTransformParamTranslate);
KnobDouble* translate = dynamic_cast<KnobDouble*>( translateKnob.get() );
if (translate) {
translate->clone( _imp->cloneTranslate.get() );
}
KnobIPtr rotateKnob = _imp->effectNode->getKnobByName(kTransformParamRotate);
KnobDouble* rotate = dynamic_cast<KnobDouble*>( rotateKnob.get() );
if (rotate) {
rotate->clone( _imp->cloneRotate.get() );
}
KnobIPtr scaleKnob = _imp->effectNode->getKnobByName(kTransformParamScale);
KnobDouble* scale = dynamic_cast<KnobDouble*>( scaleKnob.get() );
if (scale) {
scale->clone( _imp->cloneScale.get() );
}
KnobIPtr uniformKnob = _imp->effectNode->getKnobByName(kTransformParamUniform);
KnobBool* uniform = dynamic_cast<KnobBool*>( uniformKnob.get() );
if (uniform) {
uniform->clone( _imp->cloneScaleUniform.get() );
}
KnobIPtr skewxKnob = _imp->effectNode->getKnobByName(kTransformParamSkewX);
KnobDouble* skewX = dynamic_cast<KnobDouble*>( skewxKnob.get() );
if (skewX) {
skewX->clone( _imp->cloneSkewX.get() );
}
KnobIPtr skewyKnob = _imp->effectNode->getKnobByName(kTransformParamSkewY);
KnobDouble* skewY = dynamic_cast<KnobDouble*>( skewyKnob.get() );
if (skewY) {
skewY->clone( _imp->cloneSkewY.get() );
}
KnobIPtr skewOrderKnob = _imp->effectNode->getKnobByName(kTransformParamSkewOrder);
KnobChoice* skewOrder = dynamic_cast<KnobChoice*>( skewOrderKnob.get() );
if (skewOrder) {
skewOrder->clone( _imp->cloneSkewOrder.get() );
}
KnobIPtr centerKnob = _imp->effectNode->getKnobByName(kTransformParamCenter);
KnobDouble* center = dynamic_cast<KnobDouble*>( centerKnob.get() );
if (center) {
center->clone( _imp->cloneCenter.get() );
}
KnobIPtr filterKnob = _imp->effectNode->getKnobByName(kTransformParamFilter);
KnobChoice* filter = dynamic_cast<KnobChoice*>( filterKnob.get() );