-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathak-optimal_link.cpp
195 lines (177 loc) · 6.25 KB
/
ak-optimal_link.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "pipeline.hpp"
#include <unordered_map>
#include <stdexcept>
#include <cstring>
void ak::optimal_link(
float target_distance, bool do_roll,
std::vector< glm::vec3 > const &source,
std::vector< bool > const &source_linkone,
std::vector< glm::vec3 > const &target,
std::vector< bool > const &target_linkone,
std::vector< std::pair< uint32_t, uint32_t > > *links_) {
assert(source.size() == source_linkone.size());
assert(target.size() == target_linkone.size());
assert(links_);
auto &links = *links_;
links.clear();
//options:
// s[i] s[i] s[i+1] s[i]
// | \ / / |
// t[i] t[i] t[i] t[i+1]
struct State {
uint16_t source_idx = 0xffff;
uint16_t target_idx = 0xffff;
uint16_t source_remain = 0x0; //unlinked source
uint16_t target_remain = 0x0; //unlinked target
uint64_t pack() {
uint64_t ret;
memcpy(reinterpret_cast< char * >(&ret), this, sizeof(uint64_t));
return ret;
}
static State unpack(uint64_t packed) {
State ret;
memcpy(reinterpret_cast< char * >(&ret), &packed, sizeof(uint64_t));
return ret;
}
};
static_assert(sizeof(State) == sizeof(uint64_t), "State should be packed");
struct Action {
Action(uint8_t take_source_, uint8_t take_target_) : take_source(take_source_), take_target(take_target_) { }
Action() = default;
uint8_t take_source = 0;
uint8_t take_target = 0;
};
std::unordered_map< uint64_t, std::pair< float, Action > > distance_via;
std::vector< std::pair< float, uint64_t > > heap;
auto visit = [&distance_via, &heap](uint64_t state, float distance, Action const &action) {
auto f = distance_via.insert(std::make_pair(state, std::make_pair(std::numeric_limits< float >::infinity(), Action()))).first;
if (distance < f->second.first) {
f->second = std::make_pair(distance, action);
heap.emplace_back(-distance, state);
std::push_heap(heap.begin(), heap.end());
}
};
if (do_roll) {
for (uint32_t t = 0; t < target.size(); ++t) {
State s;
s.source_idx = 0;
s.target_idx = t;
s.source_remain = uint32_t(source.size());
s.target_remain = uint32_t(target.size());
visit(s.pack(), 0.0f, Action());
s.source_idx = 1;
visit(s.pack(), 0.0f, Action());
}
} else {
State s;
s.source_idx = 0;
s.target_idx = 0;
s.source_remain = uint32_t(source.size());
s.target_remain = uint32_t(target.size());
visit(s.pack(), 0.0f, Action());
}
auto penalty = [&source, &target, &target_distance](uint32_t si, uint32_t ti) {
assert(si < source.size());
assert(ti < target.size());
float dis = glm::length(source[si] - target[ti]) - target_distance;
return dis*dis;
};
//TODO: could refine this a fair bit & build a table with real link counts
auto is_possible = [](State const &state) {
if (state.source_remain * 2 < state.target_remain) return false;
if (state.target_remain * 2 < state.source_remain) return false;
return true;
};
State best;
while (!heap.empty()) {
std::pop_heap(heap.begin(), heap.end());
State state = State::unpack(heap.back().second);
float distance = -heap.back().first;
heap.pop_back();
auto f = distance_via.find(state.pack());
assert(f != distance_via.end());
assert(f->second.first <= distance);
if (f->second.first < distance) continue;
if (state.source_remain == 0 && state.target_remain == 0) {
best = state;
break;
}
assert(is_possible(state));
//try the actions:
{ // 1-1
float next_distance = distance + penalty(state.source_idx, state.target_idx);
State next = state;
next.source_idx = (next.source_idx + 1) % source.size();
next.target_idx = (next.target_idx + 1) % target.size();
next.source_remain -= 1;
next.target_remain -= 1;
if (is_possible(next)) visit(next.pack(), next_distance, Action(1,1));
}
// 1-2
if ( state.target_remain >= 2
&& !source_linkone[state.source_idx]
&& !target_linkone[state.target_idx]
&& !target_linkone[(state.target_idx+1) % target.size()] ) {
float next_distance = distance
+ penalty(state.source_idx, state.target_idx)
+ penalty(state.source_idx, (state.target_idx+1) % target.size());
State next = state;
next.source_idx = (next.source_idx + 1) % source.size();
next.target_idx = (next.target_idx + 2) % target.size();
next.source_remain -= 1;
next.target_remain -= 2;
if (is_possible(next)) visit(next.pack(), next_distance, Action(1,2));
}
// 2-1
if ( state.source_remain >= 2
&& !source_linkone[state.source_idx]
&& !source_linkone[(state.source_idx+1) % source.size()]
&& !target_linkone[state.target_idx] ) {
float next_distance = distance
+ penalty(state.source_idx, state.target_idx)
+ penalty((state.source_idx+1)%source.size(), state.target_idx);
State next = state;
next.source_idx = (next.source_idx + 2) % source.size();
next.target_idx = (next.target_idx + 1) % target.size();
next.source_remain -= 2;
next.target_remain -= 1;
if (is_possible(next)) visit(next.pack(), next_distance, Action(2,1));
}
}
if (best.source_idx == 0xffff) {
//Failed!
throw std::runtime_error("Failed to link");
}
//Read back:
State at = best;
while (true) {
auto f = distance_via.find(at.pack());
assert(f != distance_via.end());
Action action = f->second.second;
if (action.take_source == 0 && action.take_target == 0) break;
if (action.take_source == 1) {
at.source_idx = (at.source_idx + uint32_t(source.size()) - 1) % uint32_t(source.size());
at.source_remain += 1;
while (action.take_target > 0) {
--action.take_target;
at.target_idx = (at.target_idx + uint32_t(target.size()) - 1) % uint32_t(target.size());
at.target_remain += 1;
links.emplace_back(at.source_idx, at.target_idx);
}
} else if (action.take_target == 1) {
at.target_idx = (at.target_idx + uint32_t(target.size()) - 1) % uint32_t(target.size());
at.target_remain += 1;
while (action.take_source > 0) {
--action.take_source;
at.source_idx = (at.source_idx + uint32_t(source.size()) - 1) % uint32_t(source.size());
at.source_remain += 1;
links.emplace_back(at.source_idx, at.target_idx);
}
} else {
assert(action.take_source == 1 || action.take_target == 1);
}
}
assert(at.source_remain == source.size() && at.target_remain == target.size());
assert(at.source_idx == 0 || at.source_idx == 1);
std::reverse(links.begin(), links.end());
}