-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhenotype.cpp
175 lines (155 loc) · 5.82 KB
/
Phenotype.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
#include "Phenotype.h"
Phenotype::Phenotype()
: metabolism(0_j)
, seedSize(0_j)
, leafColour(Qt::white)
, root(std::make_shared<Node>(nullptr, std::vector<std::shared_ptr<Node>>{}, std::map<int, double>{}, Vec2{}, 0.0, 0.0))
, currentNode(root.get())
{
}
void Phenotype::AddNode(Vec2 stemVector, double stemThickness, double leafRadius, std::map<int, double>&& customParameters)
{
currentNode->daughterNodes.push_back(std::make_shared<Node>(currentNode, std::vector<std::shared_ptr<Node>>{}, std::move(customParameters), stemVector, stemThickness, leafRadius));
}
void Phenotype::AscendNodeTree()
{
if (!currentNode->daughterNodes.empty()) {
currentNode = currentNode->daughterNodes.back().get();
}
}
void Phenotype::DescendNodeTree()
{
if (currentNode->parentNode) {
currentNode = currentNode->parentNode;
}
}
void Phenotype::SelectNextNode()
{
// Iterate all nodes and when we find the current one, set current to the next node
bool nodeIsNext = false;
ForEachNode(*currentNode, [&, this](Node& node, const Point&)
{
if (nodeIsNext) {
currentNode = &node;
nodeIsNext = false;
}
if (&node == currentNode) {
nodeIsNext = true;
}
});
}
void Phenotype::SelectPreviousNode()
{
// Iterate all nodes and when we find the current one, set current to the previous node
Node* lastNode = root.get();
ForEachNode(*currentNode, [&, this](Node& node, const Point&)
{
if (&node == currentNode) {
currentNode = lastNode;
}
lastNode = &node;
});
}
std::map<int, double>& Phenotype::GetCurrentNodesParameters()
{
return currentNode->customParameters;
}
void Phenotype::ForEachNode(const std::function<void (std::map<int, double>&, Vec2&, double&, double&)>& action)
{
ForEachNode(*root, [&](Node& node, const Point& /*nodeLocation*/)
{
std::invoke(action, node.customParameters, node.stemVector, node.thickness, node.leafRadius);
});
}
void Phenotype::ForEachStem(double plantX, std::function<void (QLineF, double, bool, double)>&& action) const
{
ForEachNode(*root, [&](const Node& node, const Point& nodeTip)
{
Point tip = nodeTip;
Point base = nodeTip - node.stemVector;
QLineF stem(base.x + plantX, base.y, tip.x + plantX, tip.y);
bool hasLeaf = node.daughterNodes.empty();
std::invoke(action, stem, node.thickness, hasLeaf, node.leafRadius * 2);
});
}
QRectF Phenotype::GetBounds(double plantX) const
{
util::MinMax<double> xRange(plantX, plantX);
util::MinMax<double> yRange(0, 0);
ForEachNode(*root, [&](const Node& n, const Point& nodeTip)
{
bool hasLeaf = n.daughterNodes.empty();
if (hasLeaf) {
xRange.ExpandToContain(plantX + nodeTip.x + n.leafRadius);
xRange.ExpandToContain(plantX + nodeTip.x - n.leafRadius);
yRange.ExpandToContain(nodeTip.y + n.leafRadius);
yRange.ExpandToContain(nodeTip.y - n.leafRadius);
} else {
xRange.ExpandToContain(plantX + nodeTip.x);
yRange.ExpandToContain(nodeTip.y);
}
});
return QRectF(xRange.Min(), yRange.Min(), xRange.Max() - xRange.Min(), yRange.Max() - yRange.Min());
}
bool Phenotype::IsValid() const
{
double lean = 0;
double height = 0;
ForEachNode(*root, [&](const Node& n, const Point& nodeLocation)
{
bool hasLeaf = n.daughterNodes.empty();
height = std::max(height, nodeLocation.y);
lean += (nodeLocation.x) * (hasLeaf ? 1.25 : 0.5);
});
return (metabolism > 0) && (seedSize > 0) && (std::abs(lean) / height <= 0.6);
}
void Phenotype::ForEachNode(Node& node, const std::function<void (Node&, const Point&)>& action)
{
// We want to efficiently track the node tip positions as we go along
// Find the position of the start node's parent
Point nodeTip = Point{ 0, 0 };
{
Node* currentNode = node.parentNode;
while (currentNode) {
nodeTip = nodeTip + currentNode->stemVector;
currentNode = currentNode->parentNode;
}
}
// A recursive function that pushes and pops the current node location as we go up and down the tree
std::function<void(Node&, const std::function<void (Node&, const Point&)>&)> ForEachNodeRecursive;
ForEachNodeRecursive = [&](Node& node, const std::function<void (Node&, const Point&)>& action)
{
nodeTip = nodeTip + node.stemVector;
std::invoke(action, node, nodeTip);
for (std::shared_ptr<Node>& child : node.daughterNodes) {
ForEachNodeRecursive(*child, action);
}
nodeTip = nodeTip - node.stemVector;
};
ForEachNodeRecursive(node, action);
}
void Phenotype::ForEachNode(const Node& node, const std::function<void (const Node&, const Point&)>& action) const
{
// We want to efficiently track the node tip positions as we go along
// Find the position of the start node's parent
Point nodeTip = Point{ 0, 0 };
{
Node* currentNode = node.parentNode;
while (currentNode) {
nodeTip = nodeTip + currentNode->stemVector;
currentNode = currentNode->parentNode;
}
}
// A recursive function that pushes and pops the current node location as we go up and down the tree
std::function<void(const Node&, const std::function<void (const Node&, const Point&)>&)> ForEachNodeRecursive;
ForEachNodeRecursive = [&](const Node& node, const std::function<void (const Node&, const Point&)>& action)
{
nodeTip = nodeTip + node.stemVector;
std::invoke(action, node, nodeTip);
for (const std::shared_ptr<Node>& child : node.daughterNodes) {
ForEachNodeRecursive(*child, action);
}
nodeTip = nodeTip - node.stemVector;
};
ForEachNodeRecursive(node, action);
}