/
Scene.h
164 lines (130 loc) · 3.87 KB
/
Scene.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
#pragma once
#include <cstddef>
#include "inode.h"
#include "ientity.h"
#include "ibrush.h"
#include "ipatch.h"
#include "imodel.h"
namespace test::algorithm
{
// Returns the n-th child of the given parent node
inline scene::INodePtr getNthChild(const scene::INodePtr& parent, std::size_t index)
{
std::size_t n = 0;
scene::INodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
if (n == index)
{
candidate = node;
return false;
}
++n;
return true;
});
return candidate;
}
// Finds the first matching child brush of the given parent node matching the given predicate
inline scene::INodePtr findFirstBrush(const scene::INodePtr& parent,
const std::function<bool(const IBrushNodePtr&)>& predicate)
{
scene::INodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
auto brushNode = std::dynamic_pointer_cast<IBrushNode>(node);
if (brushNode && predicate(brushNode))
{
candidate = node;
return false;
}
return true;
});
return candidate;
}
// Finds the first matching child brush of the given parent node, with any of the brush's faces matching the given material
inline scene::INodePtr findFirstBrushWithMaterial(const scene::INodePtr& parent, const std::string& material)
{
return findFirstBrush(parent, [&](const IBrushNodePtr& brush)
{
return brush->getIBrush().hasShader(material);
});
}
// Finds the first matching child patch of the given parent node matching the given predicate
inline scene::INodePtr findFirstPatch(const scene::INodePtr& parent,
const std::function<bool(const IPatchNodePtr&)>& predicate)
{
scene::INodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
auto patchNode = std::dynamic_pointer_cast<IPatchNode>(node);
if (patchNode && predicate(patchNode))
{
candidate = node;
return false;
}
return true;
});
return candidate;
}
// Finds the first matching child patch of the given parent node, with the given material
inline scene::INodePtr findFirstPatchWithMaterial(const scene::INodePtr& parent, const std::string& material)
{
return findFirstPatch(parent, [&](const IPatchNodePtr& patch)
{
return patch->getPatch().getShader() == material;
});
}
inline scene::INodePtr findFirstEntity(const scene::INodePtr& parent,
const std::function<bool(IEntityNode&)>& predicate)
{
IEntityNodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
auto entity = std::dynamic_pointer_cast<IEntityNode>(node);
if (entity && predicate(*entity))
{
candidate = entity;
return false;
}
return true;
});
return candidate;
}
inline scene::INodePtr getEntityByName(const scene::INodePtr& parent, const std::string& name)
{
return findFirstEntity(parent, [&](IEntityNode& entity)
{
return entity.getEntity().getKeyValue("name") == name;
});
}
inline model::ModelNodePtr findChildModel(const scene::INodePtr& parent)
{
model::ModelNodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
auto model = Node_getModel(node);
if (model)
{
candidate = model;
return false;
}
return true;
});
return candidate;
}
// Returns the number of children of the given parent node matching the given predicate
inline std::size_t getChildCount(const scene::INodePtr& parent,
const std::function<bool(const scene::INodePtr&)>& predicate)
{
std::size_t count = 0;
parent->foreachNode([&](const scene::INodePtr& node)
{
if (predicate(node))
{
++count;
}
return true;
});
return count;
}
}