forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotoContext.h
473 lines (336 loc) · 14.6 KB
/
RotoContext.h
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
/* ***** 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 ***** */
#ifndef Engine_RotoContext_h
#define Engine_RotoContext_h
// ***** 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 "Global/Macros.h"
#include <list>
#include <set>
#include <string>
#if !defined(Q_MOC_RUN) && !defined(SBK_RUN)
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#endif
CLANG_DIAG_OFF(deprecated-declarations)
#include <QtCore/QObject>
#include <QtCore/QMutex>
#include <QtCore/QMetaType>
CLANG_DIAG_ON(deprecated-declarations)
#include "Global/GlobalDefines.h"
#include "Engine/FitCurve.h"
#include "Engine/RotoItem.h"
#include "Engine/ViewIdx.h"
#include "Engine/EngineFwd.h"
//#define NATRON_ROTO_INVERTIBLE
//#define NATRON_ROTO_ENABLE_MOTION_BLUR
NATRON_NAMESPACE_ENTER
//Small RAII class that properly destroys the cairo image upon destruction
class CairoImageWrapper
{
public:
CairoImageWrapper()
: cairoImg(0)
, ctx(0)
{
}
CairoImageWrapper(cairo_surface_t* img,
cairo_t* context)
: cairoImg(img)
, ctx(context)
{
}
~CairoImageWrapper();
cairo_surface_t* cairoImg;
cairo_t* ctx;
};
/**
* @class This class is a member of all effects instantiated in the context "paint". It describes internally
* all the splines data structures and their state.
* This class is MT-safe.
**/
class RotoContextSerialization;
struct RotoContextPrivate;
class RotoContext
: public QObject
, public boost::enable_shared_from_this<RotoContext>
{
Q_OBJECT
struct MakeSharedEnabler;
// constructors should be privatized in any class that derives from boost::enable_shared_from_this<>
RotoContext(const NodePtr& node);
public:
static RotoContextPtr create(const NodePtr& node);
virtual ~RotoContext();
/**
* @brief We have chosen to disable rotopainting and roto shapes from the same RotoContext because the rendering techniques are
* very much different. The rotopainting systems requires an entire compositing tree held inside whereas the rotoshapes
* are rendered and optimized by Cairo internally.
**/
bool isRotoPaint() const;
void createBaseLayer();
RotoLayerPtr getOrCreateBaseLayer();
/**
* @brief Returns true when the context is empty (it has no shapes)
**/
bool isEmpty() const;
/**
* @brief Turn on/off auto-keying
**/
void setAutoKeyingEnabled(bool enabled);
bool isAutoKeyingEnabled() const;
/**
* @brief Turn on/off feather link. When enabled the feather point will move by the same delta as the corresponding spline point
* is moved by.
**/
void setFeatherLinkEnabled(bool enabled);
bool isFeatherLinkEnabled() const;
/**
* @brief Turn on/off ripple edit. When enabled moving a point at a given keyframe will move it to the same position
* at all keyframes.
**/
void setRippleEditEnabled(bool enabled);
bool isRippleEditEnabled() const;
int getTimelineCurrentTime() const;
/**
* @brief Create a new layer to the currently selected layer.
**/
RotoLayerPtr addLayer();
private:
RotoLayerPtr addLayerInternal(bool declarePython);
public:
/**
* @brief Add an existing layer to the layers
**/
void addLayer(const RotoLayerPtr & layer);
/**
* @brief Make a new bezier curve and append it into the currently selected layer.
* @param baseName A hint to name the item. It can be something like "Bezier", "Ellipse", "Rectangle" , etc...
**/
BezierPtr makeBezier(double x, double y, const std::string & baseName, double time, bool isOpenBezier);
BezierPtr makeEllipse(double x, double y, double diameter, bool fromCenter, double time);
BezierPtr makeSquare(double x, double y, double initialSize, double time);
RotoStrokeItemPtr makeStroke(RotoStrokeType type,
const std::string& baseName,
bool clearSel);
std::string generateUniqueName(const std::string& baseName);
/**
* @brief Removes the given item from the context. This also removes the item from the selection
* if it was selected. If the item has children, this will also remove all the children.
**/
void removeItem(const RotoItemPtr& item, RotoItem::SelectionReasonEnum reason = RotoItem::eSelectionReasonOther);
///This is here for undo/redo purpose. Do not call this
void addItem(const RotoLayerPtr& layer, int indexInLayer, const RotoItemPtr & item, RotoItem::SelectionReasonEnum reason);
/**
* @brief Returns a const ref to the layers list. This can only be called from
* the main thread.
**/
const std::list<RotoLayerPtr> & getLayers() const;
/**
* @brief Returns a bezier curves nearby the point (x,y) and the parametric value
* which would be used to find the exact Bezier point lying on the curve.
**/
BezierPtr isNearbyBezier(double x, double y, double acceptance, int* index, double* t, bool *feather) const;
/**
* @brief Returns the region of definition of the shape unioned to the region of definition of the node
* or the project format.
**/
void getMaskRegionOfDefinition(double time,
ViewIdx view,
RectD* rod) const; //!< rod in canonical coordinates
/**
* @brief Returns true if all items have the same compositing operator and there are only strokes or bezier (because they
* are not masked)
**/
bool isRotoPaintTreeConcatenatable() const;
static bool isRotoPaintTreeConcatenatableInternal(const std::list<RotoDrawableItemPtr>& items, int* blendingMode);
void getGlobalMotionBlurSettings(const double time,
double* startTime,
double* endTime,
double* timeStep) const;
private:
void getItemsRegionOfDefinition(const std::list<RotoItemPtr>& items, double time, ViewIdx view, RectD* rod) const;
public:
/**
* @brief To be called when a change was made to trigger a new render.
**/
void evaluateChange();
void evaluateChange_noIncrement();
void incrementAge();
void clearViewersLastRenderedStrokes();
/**
*@brief Returns the age of the roto context
**/
U64 getAge();
///Serialization
void save(RotoContextSerialization* obj) const;
///Deserialization
void load(const RotoContextSerialization & obj);
/**
* @brief This must be called by the GUI whenever an item is selected. This is recursive for layers.
**/
void select(const RotoItemPtr & b, RotoItem::SelectionReasonEnum reason);
void selectFromLayer(const RotoItemPtr & b, RotoItem::SelectionReasonEnum reason);
///for convenience
void select(const std::list<RotoDrawableItemPtr> & beziers, RotoItem::SelectionReasonEnum reason);
void select(const std::list<RotoItemPtr> & items, RotoItem::SelectionReasonEnum reason);
/**
* @brief This must be called by the GUI whenever an item is deselected. This is recursive for layers.
**/
void deselect(const RotoItemPtr & b, RotoItem::SelectionReasonEnum reason);
///for convenience
void deselect(const std::list<BezierPtr> & beziers, RotoItem::SelectionReasonEnum reason);
void deselect(const std::list<RotoItemPtr> & items, RotoItem::SelectionReasonEnum reason);
void clearAndSelectPreviousItem(const RotoItemPtr& item, RotoItem::SelectionReasonEnum reason);
void clearSelection(RotoItem::SelectionReasonEnum reason);
///only callable on main-thread
void setKeyframeOnSelectedCurves();
///only callable on main-thread
void removeKeyframeOnSelectedCurves();
void removeAnimationOnSelectedCurves();
///only callable on main-thread
void goToPreviousKeyframe();
///only callable on main-thread
void goToNextKeyframe();
/**
* @brief Returns a list of the currently selected curves. Can only be called on the main-thread.
**/
std::list<RotoDrawableItemPtr> getSelectedCurves() const;
/**
* @brief Returns a const ref to the selected items. This can only be called on the main thread.
**/
const std::list<RotoItemPtr> & getSelectedItems() const;
/**
* @brief Returns a list of all the curves in the order in which they should be rendered.
* Non-active curves will not be inserted into the list.
* MT-safe
**/
std::list<RotoDrawableItemPtr> getCurvesByRenderOrder(bool onlyActivated = true) const;
int getNCurves() const;
NodePtr getNode() const;
RotoLayerPtr getLayerByName(const std::string & n) const;
RotoItemPtr getItemByName(const std::string & n) const;
RotoItemPtr getLastInsertedItem() const;
#ifdef NATRON_ROTO_INVERTIBLE
KnobBoolPtr getInvertedKnob() const;
#endif
KnobColorPtr getColorKnob() const;
void resetTransformCenter();
void resetCloneTransformCenter();
void resetTransformsCenter(bool doClone, bool doTransform);
void resetTransform();
void resetCloneTransform();
private:
void resetTransformInternal(const KnobDoublePtr& translate,
const KnobDoublePtr& scale,
const KnobDoublePtr& center,
const KnobDoublePtr& rotate,
const KnobDoublePtr& skewX,
const KnobDoublePtr& skewY,
const KnobBoolPtr& scaleUniform,
const KnobChoicePtr& skewOrder,
const KnobDoublePtr& extraMatrix);
public:
KnobChoicePtr getMotionBlurTypeKnob() const;
RotoItemPtr getLastItemLocked() const;
RotoLayerPtr getDeepestSelectedLayer() const;
void onItemLockedChanged(const RotoItemPtr& item, RotoItem::SelectionReasonEnum reason);
void emitRefreshViewerOverlays();
void getBeziersKeyframeTimes(std::list<double> *times) const;
/**
* @brief Returns the name of the node holding this item
**/
std::string getRotoNodeName() const;
void onItemGloballyActivatedChanged(const RotoItemPtr& item);
void onItemScriptNameChanged(const RotoItemPtr& item);
void onItemLabelChanged(const RotoItemPtr& item);
void onItemKnobChanged();
void declarePythonFields();
void changeItemScriptName(const std::string& oldFullyQualifiedName, const std::string& newFullyQUalifiedName);
void declareItemAsPythonField(const RotoItemPtr& item);
void removeItemAsPythonField(const RotoItemPtr& item);
/**
* @brief Rebuilds the connection between nodes used internally by the rotopaint tree
* To be called whenever changes position in the hierarchy or when one gets removed/inserted
**/
void refreshRotoPaintTree();
void onRotoPaintInputChanged(const NodePtr& node);
void getRotoPaintTreeNodes(NodesList* nodes) const;
NodePtr getRotoPaintBottomMergeNode() const;
void setWhileCreatingPaintStrokeOnMergeNodes(bool b);
/**
* @brief First searches through the selected layer which one is the deepest in the hierarchy.
* If nothing is found, it searches through the selected items and find the deepest selected item's layer
**/
RotoLayerPtr findDeepestSelectedLayer() const;
void dequeueGuiActions();
/**
* @brief When finishing a stroke with the paint brush, we need to re-render it because the interpolation of the curve
* will be much smoother with more points than what it was during painting.
* We explicitly freeze the UI while waiting for the image to be drawn, otherwise the user might attempt to do a
* multiple stroke on top of it which could make some artifacts.
**/
void evaluateNeatStrokeRender();
bool mustDoNeatRender() const;
void setIsDoingNeatRender(bool doing);
bool isDoingNeatRender() const;
void s_breakMultiStroke() { Q_EMIT breakMultiStroke(); }
bool knobChanged(KnobI* k,
ValueChangedReasonEnum reason,
ViewSpec view,
double time,
bool originatedFromMainThread);
static bool allocateAndRenderSingleDotStroke(int brushSizePixel, double brushHardness, double alpha, CairoImageWrapper& wrapper);
Q_SIGNALS:
/**
* @brief Signalled when the gui should force a new stroke to be added
**/
void breakMultiStroke();
/**
* Emitted when the selection is changed. The integer corresponds to the
* RotoItem::SelectionReasonEnum enum.
**/
void selectionChanged(int);
void restorationComplete();
void itemInserted(int, int);
void itemRemoved(const RotoItemPtr&, int);
void refreshViewerOverlays();
void itemLockedChanged(int reason);
void itemGloballyActivatedChanged(const RotoItemPtr&);
void itemScriptNameChanged(const RotoItemPtr&);
void itemLabelChanged(const RotoItemPtr&);
public Q_SLOTS:
void onAutoKeyingChanged(bool enabled);
void onFeatherLinkChanged(bool enabled);
void onRippleEditChanged(bool enabled);
void onSelectedKnobCurveChanged();
void onLifeTimeKnobValueChanged(ViewSpec, int, int);
private:
NodePtr getOrCreateGlobalMergeNode(int *availableInputIndex);
void selectInternal(const RotoItemPtr& b, bool slaveKnobs = true);
void deselectInternal(RotoItemPtr b);
void removeItemRecursively(const RotoItemPtr& item, RotoItem::SelectionReasonEnum reason);
boost::scoped_ptr<RotoContextPrivate> _imp;
};
NATRON_NAMESPACE_EXIT
#endif // Engine_RotoContext_h