Skip to content

Commit 27b10de

Browse files
Clean up code
Signed-off-by: Éloïse Brosseau <[email protected]>
1 parent b74e65a commit 27b10de

File tree

7 files changed

+213
-221
lines changed

7 files changed

+213
-221
lines changed

src/lib/ip/IPBaseNodes/IPBaseNodes/PaintIPNode.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ namespace IPCore
4141
class LocalCommand
4242
{
4343
public:
44-
LocalCommand()
45-
: ghostColor(Color(0.0))
46-
{
47-
}
48-
44+
LocalCommand() = default;
4945
virtual ~LocalCommand() = default;
5046

5147
int startFrame{};
@@ -57,7 +53,7 @@ namespace IPCore
5753
int eye{-1};
5854

5955
bool ghostOn{};
60-
TwkMath::Col4f ghostColor;
56+
TwkMath::Col4f ghostColor{Color(0.0)};
6157
};
6258

6359
class LocalPolyLine
@@ -99,8 +95,6 @@ namespace IPCore
9995
void readCompleted(const std::string& typeName,
10096
unsigned int version) override;
10197

102-
void clearAll();
103-
10498
protected:
10599
void compilePenComponent(Component*);
106100
void compileTextComponent(Component*);

src/lib/ip/IPBaseNodes/OverlayIPNode.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,12 @@ namespace IPCore
144144
namespace
145145
{
146146

147-
static void initializeOutline(Paint::PolyLine& outline,
148-
const Vec2f* points, size_t npoints,
149-
TwkPaint::Color oc, float outlineWidth,
150-
const string& brush)
147+
void initializeOutline(Paint::PolyLine& outline, const Vec2f* points,
148+
size_t npoints, TwkPaint::Color oc,
149+
float outlineWidth, const string& brush)
151150
{
152151
// these polyline owns the memory of their own points
153-
outline.points = new Vec2f[npoints];
154-
memcpy((void*)outline.points, (void*)points,
155-
npoints * sizeof(Vec2f));
152+
outline.points.assign(points->begin(), points->end());
156153
outline.ownPoints = true;
157154

158155
outline.npoints = npoints;

src/lib/ip/IPBaseNodes/PaintIPNode.cpp

Lines changed: 103 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
// SPDX-License-Identifier: Apache-2.0
55
//
66
#include <IPBaseNodes/PaintIPNode.h>
7+
#include <IPCore/PaintCommand.h>
78
#include <IPCore/Exception.h>
89
#include <IPCore/IPGraph.h>
910
#include <TwkGLText/TwkGLText.h>
1011
#include <mutex>
1112
#include <stl_ext/string_algo.h>
1213
#include <cstdlib>
1314
#include <IPCore/ShaderCommon.h>
15+
#include <tuple>
1416

1517
namespace
1618
{
1719
using namespace IPCore;
20+
using PerFramePaintCommands = std::map<int, PaintIPNode::LocalCommands>;
1821

1922
// Calculates the opacity based on the distance from the annotated frame to
2023
// make the ghosted annotation more visible closer to the frame and less
@@ -42,17 +45,12 @@ namespace
4245
return ghostOpacity;
4346
}
4447

45-
PaintIPNode::LocalCommands
46-
generateVisibleCommands(const PaintIPNode::LocalCommands& commands,
47-
const int frame, const size_t eye)
48+
auto
49+
separateCommandsByFrameGroup(const PaintIPNode::LocalCommands& commands,
50+
const int frame, const size_t eye)
4851
{
4952
PaintIPNode::LocalCommands
50-
allCommands; // visible commands, including hold and ghost
51-
PaintIPNode::LocalCommands
52-
CurrentFrameCommands; // visible commands, excluding hold and ghost
53-
54-
using PerFramePaintCommands = std::map<int, PaintIPNode::LocalCommands>;
55-
53+
currentFrameCommands; // visible commands, excluding hold and ghost
5654
PerFramePaintCommands beforeCommands;
5755
PerFramePaintCommands afterCommands;
5856

@@ -80,7 +78,7 @@ namespace
8078
if (frame >= startFrame
8179
&& frame <= endFrame) // Command is visible on the current frame
8280
{
83-
CurrentFrameCommands.push_back(localCommand);
81+
currentFrameCommands.push_back(localCommand);
8482
}
8583
else
8684
{
@@ -97,86 +95,114 @@ namespace
9795
}
9896
}
9997

100-
bool noCurrentFrameCommands = CurrentFrameCommands.empty();
98+
return std::make_tuple(currentFrameCommands, beforeCommands,
99+
afterCommands);
100+
}
101+
102+
void addBeforeCommands(PaintIPNode::LocalCommands* allCommands,
103+
PaintIPNode::LocalCommands* currentFrameCommands,
104+
const PerFramePaintCommands& beforeCommands,
105+
const int frame)
106+
{
107+
int levelIndex = 1;
108+
bool isHoldedCommandsInFirstLevel = false;
101109

110+
for (const auto& beforeCommand : beforeCommands)
102111
{
103-
int levelIndex = 1;
104-
bool isHoldedCommandsInFirstLevel = false;
112+
int ghostLevel =
113+
levelIndex - (isHoldedCommandsInFirstLevel ? 1 : 0);
105114

106-
for (const auto& beforeCommand : beforeCommands)
115+
for (auto* command : beforeCommand.second)
107116
{
108-
int ghostLevel =
109-
levelIndex - (isHoldedCommandsInFirstLevel ? 1 : 0);
110-
111-
for (auto* command : beforeCommand.second)
117+
if (levelIndex == 1 && currentFrameCommands->empty()
118+
&& command->hold != 0)
112119
{
113-
if (levelIndex == 1 && noCurrentFrameCommands
114-
&& command->hold != 0)
115-
{
116-
isHoldedCommandsInFirstLevel = true;
117-
command->ghostOn = true;
120+
isHoldedCommandsInFirstLevel = true;
121+
command->ghostOn = true;
118122

119-
if (auto* polyLine =
120-
dynamic_cast<PaintIPNode::LocalPolyLine*>(
121-
command))
122-
{
123-
command->ghostColor = polyLine->color;
124-
}
125-
else if (auto* text =
126-
dynamic_cast<PaintIPNode::LocalText*>(
127-
command))
128-
{
129-
command->ghostColor = text->color;
130-
}
131-
132-
CurrentFrameCommands.push_back(command);
123+
if (auto* polyLine =
124+
dynamic_cast<PaintIPNode::LocalPolyLine*>(command))
125+
{
126+
command->ghostColor = polyLine->color;
133127
}
134-
else if (command->ghost != 0
135-
&& command->ghostBefore >= ghostLevel)
128+
else if (auto* text =
129+
dynamic_cast<PaintIPNode::LocalText*>(command))
136130
{
137-
command->ghostOn = true;
138-
command->ghostColor = PaintIPNode::Color(
139-
1.0, 0.0, 0.0, 1.0); // Ghosted "Before" commands
140-
// are drawn in green
141-
command->ghostColor[3] = getGhostOpacity(
142-
frame, command->startFrame, command->duration);
143-
allCommands.push_back(command);
131+
command->ghostColor = text->color;
144132
}
133+
134+
currentFrameCommands->push_back(command);
135+
}
136+
else if (command->ghost != 0
137+
&& command->ghostBefore >= ghostLevel)
138+
{
139+
command->ghostOn = true;
140+
command->ghostColor = PaintIPNode::Color(
141+
1.0, 0.0, 0.0, 1.0); // Ghosted "Before" commands
142+
// are drawn in green
143+
command->ghostColor[3] = getGhostOpacity(
144+
frame, command->startFrame, command->duration);
145+
allCommands->push_back(command);
145146
}
146-
levelIndex++;
147147
}
148+
levelIndex++;
148149
}
150+
}
149151

150-
{
151-
int levelIndex = 1;
152+
void addAfterCommands(PaintIPNode::LocalCommands* allCommands,
153+
const PerFramePaintCommands& afterCommands,
154+
const int frame)
155+
{
156+
int levelIndex = 1;
152157

153-
for (const auto& afterCommand : afterCommands)
158+
for (const auto& afterCommand : afterCommands)
159+
{
160+
for (auto* command : afterCommand.second)
154161
{
155-
for (auto* command : afterCommand.second)
162+
if (command->ghost != 0 && command->ghostAfter >= levelIndex)
156163
{
157-
if (command->ghost != 0
158-
&& command->ghostAfter >= levelIndex)
159-
{
160-
command->ghostOn = true;
161-
command->ghostColor = PaintIPNode::Color(
162-
0.0, 1.0, 0.0,
163-
1.0); // Ghosted "After" commands are drawn in red
164-
command->ghostColor[3] = getGhostOpacity(
165-
frame, command->startFrame, command->duration);
166-
allCommands.push_back(command);
167-
}
164+
command->ghostOn = true;
165+
command->ghostColor = PaintIPNode::Color(
166+
0.0, 1.0, 0.0,
167+
1.0); // Ghosted "After" commands are drawn in red
168+
command->ghostColor[3] = getGhostOpacity(
169+
frame, command->startFrame, command->duration);
170+
allCommands->push_back(command);
168171
}
169-
levelIndex++;
170172
}
173+
levelIndex++;
171174
}
175+
}
172176

177+
void
178+
addVisibleCommands(PaintIPNode::LocalCommands* allCommands,
179+
const PaintIPNode::LocalCommands& currentFrameCommands)
180+
{
181+
for (auto* command : currentFrameCommands)
173182
{
174-
for (auto* command : CurrentFrameCommands)
175-
{
176-
command->ghostOn = false;
177-
allCommands.push_back(command);
178-
}
183+
command->ghostOn = false;
184+
allCommands->push_back(command);
179185
}
186+
}
187+
188+
PaintIPNode::LocalCommands
189+
generateVisibleCommands(const PaintIPNode::LocalCommands& commands,
190+
const int frame, const size_t eye)
191+
{
192+
PaintIPNode::LocalCommands
193+
allCommands; // visible commands, including hold and ghost
194+
PaintIPNode::LocalCommands
195+
currentFrameCommands; // visible commands, excluding hold and ghost
196+
PerFramePaintCommands beforeCommands;
197+
PerFramePaintCommands afterCommands;
198+
199+
std::tie(currentFrameCommands, beforeCommands, afterCommands) =
200+
separateCommandsByFrameGroup(commands, frame, eye);
201+
202+
addBeforeCommands(&allCommands, &currentFrameCommands, beforeCommands,
203+
frame);
204+
addAfterCommands(&allCommands, afterCommands, frame);
205+
addVisibleCommands(&allCommands, currentFrameCommands);
180206

181207
return allCommands;
182208
}
@@ -305,17 +331,21 @@ namespace IPCore
305331
if (widthP && pointsP && widthP->size() == pointsP->size()
306332
&& widthP->size() > 1)
307333
{
308-
p.widths = (const float*)widthP->rawData();
334+
p.widths.assign(static_cast<const float*>(widthP->rawData()),
335+
static_cast<const float*>(widthP->rawData())
336+
+ widthP->size());
309337
}
310338

311339
if (pointsP && pointsP->size())
312340
{
313-
p.points = (const Vec2f*)pointsP->rawData();
341+
p.points.assign(static_cast<const Vec2f*>(pointsP->rawData()),
342+
static_cast<const Vec2f*>(pointsP->rawData())
343+
+ pointsP->size());
314344
p.npoints = pointsP->size();
315345
}
316346
else
317347
{
318-
p.points = 0;
348+
p.points.clear();
319349
p.npoints = 0;
320350
}
321351

@@ -327,8 +357,8 @@ namespace IPCore
327357
const size_t prevNumTexts = m_texts.size();
328358
LocalText& p = m_texts[c];
329359

330-
// Add newly created strokes to the commands vector
331-
if (prevNumTexts != m_penStrokes.size())
360+
// Add newly created text boxes to the commands vector
361+
if (prevNumTexts != m_texts.size())
332362
{
333363
m_commands.push_back(&p);
334364
}
@@ -409,14 +439,6 @@ namespace IPCore
409439
p.ghostAfter = ghostAfter;
410440
}
411441

412-
void PaintIPNode::clearAll()
413-
{
414-
std::lock_guard<std::mutex> commandsGuard{m_commandsMutex};
415-
m_texts.clear();
416-
m_penStrokes.clear();
417-
m_commands.clear();
418-
}
419-
420442
void PaintIPNode::compileFrame(Component* comp)
421443
{
422444
const StringProperty* orderP = comp->property<StringProperty>("order");
@@ -703,16 +725,12 @@ namespace IPCore
703725
if (auto* polyLine = dynamic_cast<PaintIPNode::LocalPolyLine*>(
704726
visibleCommand))
705727
{
706-
polyLine->ghostOn = visibleCommand->ghostOn;
707-
polyLine->ghostColor = visibleCommand->ghostColor;
708728
head->commands.push_back(polyLine);
709729
}
710730
else if (auto* localText =
711731
dynamic_cast<PaintIPNode::LocalText*>(
712732
visibleCommand))
713733
{
714-
localText->ghostOn = visibleCommand->ghostOn;
715-
localText->ghostColor = visibleCommand->ghostColor;
716734
head->commands.push_back(localText);
717735
}
718736
}

0 commit comments

Comments
 (0)