-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeleton.h
executable file
·398 lines (374 loc) · 12.4 KB
/
skeleton.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
#ifndef SKELETON_H
#define SKELETON_H
#include <boost/graph/adjacency_list.hpp>
#include <glm/gtx/string_cast.hpp>
#include <glm/mat4x4.hpp>
#include "cgaltypedefs.h"
#include "drawableskeleton.h"
#include "undirectedgraph.h"
struct SkeletonPart {
SkeletonGraph skeleton;
std::vector<std::set<SkeletonGraph::vertex_descriptor>>
possibleJunctioNodes; // the vector holds the segment's loops and
// each set holds the skeleton nodes which
// are close to the loop.
size_t segmentIndex;
SkeletonPart(SkeletonGraph s,
std::vector<std::set<SkeletonGraph::vertex_descriptor>> v,
size_t pi)
: skeleton(s), possibleJunctioNodes(v), segmentIndex(pi) {}
};
class Skeleton : public DrawableSkeleton {
// public member functions
// NOTE PointSphere should not be an argument
// but for some reason PointSphere cannot be
// loaded in this class but only through
// Scene::load.Maybe because
// GLWidget::makeCurrent() needs to be called?
std::vector<std::optional<SkeletonPart>>
m_skeletonParts; // skeleton parts of which the skeleton consists
const UndirectedGraph &m_segmentGraph;
public:
Skeleton(const PointSphere &ps, const UndirectedGraph &sg)
: DrawableSkeleton(ps), m_segmentGraph(sg) {}
void clear() { DrawableSkeleton::clearDrawableSkeleton(); }
void setNumberOfSkeletonParts(size_t numOfSkeletonParts) {
m_skeletonParts.clear();
m_skeletonParts.resize(numOfSkeletonParts, std::nullopt);
}
const std::vector<glm::vec3> &getNodePositions() const {
return DrawableSkeleton::m_vertices;
}
void initialize() { // is called when there is an active OpenGL context
DrawableSkeleton::initializeDrawingBuffers();
}
// using Node = Node<CGALSurfaceMesh::Point>;
// constructs the skeleton of the whole mesh
// nodes could be extracted from edges but are provided for efficiency
template <typename Graph>
void populateSkeleton(Graph skeleton) {
populateDrawableSkeleton(skeleton);
std::cout << "Finished populating skeleton using boost graph."
<< std::endl;
}
// adds to the skeleton one part based on the segment graph and
// the
// provided index
void addSkeletonPart(SkeletonPart sp) {
assert(m_skeletonParts.size() != 0);
if (!m_skeletonParts[sp.segmentIndex]) {
populateDrawableSkeleton(sp.skeleton);
m_skeletonParts[sp.segmentIndex] = sp;
}
// std::cout << "Finished adding skeleton part using
//boost graph."
// << std::endl;
}
void connectNeighbourSegments() {
size_t numOfComputedSkeletonParts = 0;
for (auto opt_skPart : m_skeletonParts) {
if (opt_skPart) numOfComputedSkeletonParts++;
}
assert(numOfComputedSkeletonParts == m_skeletonParts.size() &&
m_skeletonParts.size() != 0);
// for (auto skeletonGraph : m_skeletonParts) {
// for (auto setVD :
// skeletonGraph.value().possibleJunctioNodes) {
// for (auto VD : setVD) {
// const auto &p = skeletonGraph.value()
// .skeleton[VD]
// .point;
// std::cout << p.x() << " " << p.y()
// << " " << p.z() << std::endl;
// }
// }
//}
// auto junctionNodesPerSegment =
// getPossibleJunctionPositions();
// std::cout << junctionNodesPerSegment.size() << std::endl;
// for (auto loopsPerSegment : junctionNodesPerSegment) {
// for (auto loop : loopsPerSegment) {
// for (auto possibleJunctioPosition : loop) {
// std::cout
// << possibleJunctioPosition->x()
// << " "
// << possibleJunctioPosition->y()
// << " "
// << possibleJunctioPosition->z()
// << std::endl;
// }
// }
//}
// center of mass for
// each loop of each
// segment
std::vector<std::vector<Point3D>>
representativePositionOfEachLoopOfEachSegment =
getCenterOfMassForEachLoop();
// for (size_t segmentIndex = 0;
// segmentIndex < m_skeletonParts.size(); segmentIndex++) {
// std::cout << "Center of mass of" << std::endl;
// auto skeleton =
// m_skeletonParts[segmentIndex].value().skeleton;
// for (std::set<SkeletonGraph::vertex_descriptor>
// sOfNodes : m_skeletonParts[segmentIndex]
// .value()
// .possibleJunctioNodes) {
// size_t loopIndex = 0;
// for (SkeletonGraph::vertex_descriptor node :
// sOfNodes) {
// auto p = skeleton[node].point;
// std::cout << p.x() << " " << p.y()
// << " " << p.z() << " "
// << std::endl;
// }
// auto p =
// representativePositionOfEachLoopOfEachSegment
// [segmentIndex][loopIndex];
// std::cout << " is " << p.x() << " " << p.y()
// << " " << p.z() << std::endl;
// loopIndex++;
// }
//}
// convert representativePositionsOfEachLoopOfEachSegment'
// elements to optional so the next part of the algorithm does
// not need to recalculate connections between loops already
// assigned.
std::vector<std::vector<std::optional<Point3D>>> queueLike(
representativePositionOfEachLoopOfEachSegment.size());
for (size_t i = 0;
i < representativePositionOfEachLoopOfEachSegment.size();
i++) {
for (auto p3d :
representativePositionOfEachLoopOfEachSegment[i]) {
queueLike[i].push_back(std::make_optional(p3d));
}
}
for (auto vop3d : queueLike) {
for (auto op3d : vop3d) {
auto p3d = op3d.value();
// std::cout << p3d.x() << " " << p3d.y() << " "
// << p3d.z() << std::endl;
}
}
// assert(queueLike.size() ==
// representativePositionOfEachLoopOfEachSegment.size());
// define the following structure for cleaner code
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using SegmentIndex = size_t;
using LoopIndex = size_t;
struct SegmentLoopPair {
SegmentIndex segment;
LoopIndex loop;
SegmentLoopPair() {}
SegmentLoopPair(SegmentIndex si, LoopIndex li)
: segment(si), loop(li) {}
};
struct SegmentLoopConnection {
SegmentLoopPair a;
SegmentLoopPair b;
SegmentLoopConnection(SegmentLoopPair slp1,
SegmentLoopPair slp2)
: a(slp1.segment, slp1.loop),
b(slp2.segment, slp2.loop) {}
};
// find which loops should be connected to which based on the
// center of mass of each loop
std::vector<SegmentLoopConnection>
connectionVector; // Thats what I want to populate
for (SegmentIndex s1 = 0; s1 < m_segmentGraph.num_vertices();
s1++) {
std::vector<SegmentIndex> adjSegments =
m_segmentGraph.getAdjacentNodes(s1);
for (LoopIndex l1 = 0; l1 < queueLike[s1].size();
l1++) {
if (!queueLike[s1][l1]) continue;
SegmentLoopPair SLP1(s1, l1);
Point3D loop1CM = queueLike[s1][l1].value();
Point3D loop2CM;
std::optional<double> minDistance;
SegmentLoopPair closestSLP;
for (SegmentIndex adjSegmentIndex :
adjSegments) {
for (size_t li2 = 0;
li2 <
queueLike[adjSegmentIndex].size();
li2++) {
assert(adjSegmentIndex != s1);
if (queueLike[adjSegmentIndex]
[li2]) {
loop2CM =
queueLike
[adjSegmentIndex]
[li2]
.value();
double distance = CGAL::
squared_distance(
loop1CM,
loop2CM);
if (!minDistance
.has_value() ||
minDistance >
distance) {
minDistance =
distance;
closestSLP =
SegmentLoopPair(
adjSegmentIndex,
li2);
}
}
}
}
SegmentLoopConnection connection(SLP1,
closestSLP);
connectionVector.push_back(connection);
queueLike[closestSLP.segment][closestSLP.loop] =
std::nullopt;
queueLike[SLP1.segment][SLP1.loop] =
std::nullopt;
}
}
//// find actuall closest skeleton nodes in loops found above
for (auto SLC : connectionVector) {
auto SLP1 = SLC.a;
auto SLP2 = SLC.b;
auto SI1 = SLP1.segment;
auto LI1 = SLP1.loop;
auto SI2 = SLP2.segment;
auto LI2 = SLP2.loop;
auto loop1 = m_skeletonParts[SI1]
.value()
.possibleJunctioNodes[LI1];
auto loop2 = m_skeletonParts[SI2]
.value()
.possibleJunctioNodes[LI2];
auto skeleton1 = m_skeletonParts[SI1].value().skeleton;
auto skeleton2 = m_skeletonParts[SI2].value().skeleton;
std::optional<double> minDistance;
std::pair<Point3D, Point3D> closestNodePositions;
for (auto skeletonNodeInLoop1 : loop1) {
for (auto skeletonNodeInLoop2 : loop2) {
auto nodePos1 =
skeleton1[skeletonNodeInLoop1]
.point;
auto nodePos2 =
skeleton2[skeletonNodeInLoop2]
.point;
auto distance = CGAL::squared_distance(
nodePos1, nodePos2);
if (!minDistance.has_value() ||
distance < minDistance) {
minDistance = distance;
closestNodePositions =
std::make_pair(nodePos1,
nodePos2);
}
}
}
auto node1 = closestNodePositions.first;
auto node2 = closestNodePositions.second;
// visualize connection for this segment-loop connection
m_vertices.push_back(
glm::vec3(node1.x(), node1.y(), node1.z()));
m_vertices.push_back(
glm::vec3(node2.x(), node2.y(), node2.z()));
}
updateDrawingBuffers();
}
std::vector<std::vector<Point3D>> getCenterOfMassForEachLoop() {
std::vector<std::vector<Point3D>> representativePositions(
m_segmentGraph.num_vertices());
for (size_t segmentIndex = 0;
segmentIndex < m_segmentGraph.num_vertices();
segmentIndex++) {
std::vector<Point3D>
segmentRepresentativePositions; // each 3D point
// represents a
// segment's loop
const auto &vectorOfSets =
m_skeletonParts[segmentIndex]->possibleJunctioNodes;
const auto &skeleton =
m_skeletonParts[segmentIndex]->skeleton;
for (auto segmentLoop : vectorOfSets) {
Point3D centerOfMassOfLoop(0, 0, 0);
for (auto loopNode : segmentLoop) {
centerOfMassOfLoop = Point3D(
skeleton[loopNode].point.x() +
centerOfMassOfLoop.x(),
skeleton[loopNode].point.y() +
centerOfMassOfLoop.y(),
skeleton[loopNode].point.z() +
centerOfMassOfLoop.z());
}
centerOfMassOfLoop = Point3D(
centerOfMassOfLoop.x() / segmentLoop.size(),
centerOfMassOfLoop.y() / segmentLoop.size(),
centerOfMassOfLoop.z() /
segmentLoop.size());
segmentRepresentativePositions.push_back(
centerOfMassOfLoop);
}
assert(segmentRepresentativePositions.size() ==
vectorOfSets.size());
representativePositions[segmentIndex] =
segmentRepresentativePositions;
}
return representativePositions;
}
// returns segment->loops->optional(loop node positions)
std::vector<std::vector<std::vector<std::optional<Point3D>>>>
getPossibleJunctionPositions() {
std::vector<std::vector<std::vector<std::optional<Point3D>>>>
segments(m_segmentGraph.num_vertices());
for (size_t segmentIndex = 0;
segmentIndex < m_segmentGraph.num_vertices();
segmentIndex++) {
std::vector<std::vector<std::optional<Point3D>>>
segmentLoops;
const auto &vectorOfSets =
m_skeletonParts[segmentIndex]->possibleJunctioNodes;
const auto &skeleton =
m_skeletonParts[segmentIndex]->skeleton;
for (auto segmentLoop : vectorOfSets) {
std::vector<std::optional<Point3D>> loop;
for (auto loopNode : segmentLoop) {
loop.push_back(std::make_optional(
skeleton[loopNode].point));
}
segmentLoops.push_back(loop);
}
segments.push_back(segmentLoops);
}
return segments;
}
};
inline Skeleton join(const Skeleton &s1, const Skeleton &s2) {}
inline double glmSquaredDistance(glm::vec3 v1, glm::vec3 v2) {
return std::pow(v1.x - v2.x, 2) + std::pow(v1.y - v2.y, 2) +
std::pow(v1.z - v2.z, 2);
}
inline std::pair<size_t, size_t> findClosestNodes(
const Skeleton &s1,
const Skeleton
&s2) // returns pair(indexInFirstSkeleton,indexInSecondSkeleton)
{
const std::vector<glm::vec3> &s1Nodes = s1.getNodePositions();
const std::vector<glm::vec3> &s2Nodes = s2.getNodePositions();
double minDistance = glmSquaredDistance(s1Nodes[0], s2Nodes[0]);
std::pair<size_t, size_t> indicesOfMinDistance(0, 0);
for (size_t index1 = 0; index1 < s1Nodes.size(); index1++) {
glm::vec3 s1NodePos = s1Nodes[index1];
for (size_t index2 = 0; index2 < s2Nodes.size(); index2++) {
glm::vec3 s2NodePos = s2Nodes[index2];
double d = glmSquaredDistance(s1NodePos, s2NodePos);
if (d < minDistance) {
minDistance = d;
indicesOfMinDistance =
std::pair<size_t, size_t>(index1, index2);
}
}
}
return indicesOfMinDistance;
}
#endif // SKELETON_H