| 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 |
#ifndef VCG_MT_H |
| 34 |
#define VCG_MT_H |
| 35 |
|
| 36 |
#include <vector> |
| 37 |
#include <queue> |
| 38 |
#include <map> |
| 39 |
|
| 40 |
#include <vcg/space/sphere3.h> |
| 41 |
#include <vcg/space/point3.h> |
| 42 |
#include <wrap/gui/frustum.h> |
| 43 |
|
| 44 |
namespace vcg { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
template <class C> class MT { |
| 57 |
public: |
| 58 |
typedef C Cell; |
| 59 |
|
| 60 |
private: |
| 61 |
|
| 62 |
class Frag:public std::vector<Cell *> {}; |
| 63 |
|
| 64 |
struct Node { |
| 65 |
std::vector<Node *> in; |
| 66 |
std::vector<Node *> out; |
| 67 |
std::vector<Frag> frags; |
| 68 |
float error; |
| 69 |
bool visited; |
| 70 |
}; |
| 71 |
|
| 72 |
std::vector<Node> nodes; |
| 73 |
|
| 74 |
public: |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
class Update { |
| 82 |
public: |
| 83 |
std::vector<Cell *> erased; |
| 84 |
std::vector<Cell *> created; |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
void Load(std::vector<Update> &updates) { |
| 89 |
|
| 90 |
assert(updates[0].erased.size() == 0); |
| 91 |
|
| 92 |
|
| 93 |
std::map<Cell *, unsigned int> cell_node; |
| 94 |
nodes.resize(updates.size()); |
| 95 |
|
| 96 |
|
| 97 |
unsigned int current_node = 0; |
| 98 |
std::vector<Update>::iterator u; |
| 99 |
for(u = updates.begin(); u != updates.end(); u++) { |
| 100 |
|
| 101 |
Node &node = nodes[current_node]; |
| 102 |
node.error = 0; |
| 103 |
|
| 104 |
|
| 105 |
for(unsigned int i = 0; i < (*u).created.size(); i++) { |
| 106 |
Cell *cell = (*u).created[i]; |
| 107 |
if(cell->Error() > node.error) |
| 108 |
node.error = cell->Error(); |
| 109 |
|
| 110 |
cell_node[cell] = current_node; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
std::map<unsigned int, std::vector<Cell *> > node_erased; |
| 116 |
|
| 117 |
for(unsigned int i = 0; i < (*u).erased.size(); i++) { |
| 118 |
Cell *cell = (*u).erased[i]; |
| 119 |
assert(cell_node.count(cell)); |
| 120 |
node_erased[cell_node[cell]].push_back(cell); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
std::map<unsigned int, std::vector<Cell *> >::iterator e; |
| 125 |
for(e = node_erased.begin(); e != node_erased.end(); e++) { |
| 126 |
|
| 127 |
Frag fr; |
| 128 |
float max_err = -1; |
| 129 |
|
| 130 |
|
| 131 |
std::vector<Cell *> &cells = (*e).second; |
| 132 |
std::vector<Cell *>::iterator k; |
| 133 |
for(k = cells.begin(); k != cells.end(); k++) { |
| 134 |
Cell *cell = (*k); |
| 135 |
fr.push_back(cell); |
| 136 |
if(cell->Error() > max_err) |
| 137 |
max_err = cell->Error(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
unsigned int floor_node = (*e).first; |
| 142 |
Node &oldnode = nodes[floor_node]; |
| 143 |
oldnode.frags.push_back(fr); |
| 144 |
if(node.error < max_err) |
| 145 |
node.error = max_err; |
| 146 |
|
| 147 |
|
| 148 |
node.in.push_back(&oldnode); |
| 149 |
oldnode.out.push_back(&node); |
| 150 |
} |
| 151 |
current_node++; |
| 152 |
} |
| 153 |
} |
| 154 |
void Clear() { |
| 155 |
nodes.clear(); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
template <class CONT> void Extract(CONT &selected, float error) { |
| 160 |
std::vector<Node>::iterator n; |
| 161 |
for(n = nodes.begin(); n != nodes.end(); n++) |
| 162 |
(*n).visited = false; |
| 163 |
|
| 164 |
std::queue<Node *> qnodo; |
| 165 |
qnodo.push(&nodes[0]); |
| 166 |
nodes[0].visited = true; |
| 167 |
|
| 168 |
for( ; !qnodo.empty(); qnodo.pop()) { |
| 169 |
Node &node = *qnodo.front(); |
| 170 |
|
| 171 |
std::vector<Frag>::iterator fragment; |
| 172 |
std::vector<Node *>::iterator on; |
| 173 |
for(on = node.out.begin(), fragment = node.frags.begin(); on != node.out.end(); ++on, ++fragment) { |
| 174 |
if((*on)->visited) continue; |
| 175 |
|
| 176 |
if(error < (*on)->error) { |
| 177 |
qnodo.push(*on); |
| 178 |
(*on)->visited = 1; |
| 179 |
} else { |
| 180 |
vector<Cell *>::iterator cell; |
| 181 |
for(cell=(*fragment).begin(); cell != (*fragment).end(); ++cell) |
| 182 |
selected.push_back(*cell); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
template <class CONT, class POLICY> void Extract(CONT &selected, POLICY &policy) { |
| 190 |
std::vector<Node>::iterator n; |
| 191 |
for(n = nodes.begin(); n != nodes.end(); n++) |
| 192 |
(*n).visited = false; |
| 193 |
|
| 194 |
std::queue<Node *> qnodo; |
| 195 |
qnodo.push(&nodes[0]); |
| 196 |
nodes[0].visited = true; |
| 197 |
|
| 198 |
for( ; !qnodo.empty(); qnodo.pop()) { |
| 199 |
Node &node = *qnodo.front(); |
| 200 |
|
| 201 |
std::vector<Frag>::iterator i; |
| 202 |
std::vector<Node *>::iterator on; |
| 203 |
for(i = node.frags.begin(), on = node.out.begin(); i != node.frags.end(); i++, on++) { |
| 204 |
if((*on)->visited) continue; |
| 205 |
Frag &frag = (*i); |
| 206 |
std::vector<Cell *>::iterator cell; |
| 207 |
for(cell = frag.begin(); cell != frag.end(); cell++) { |
| 208 |
if(policy.Expand(*cell)) |
| 209 |
visit(*on, qnodo); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
Extract(selected); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
template <class CONT> void Extract(CONT &selected) { |
| 224 |
selected.clear(); |
| 225 |
std::vector<Node>::iterator i; |
| 226 |
for(i = nodes.begin(); i != nodes.end(); i++) { |
| 227 |
Node &node = *i; |
| 228 |
if(!node.visited) |
| 229 |
continue; |
| 230 |
|
| 231 |
std::vector<Node *>::iterator n; |
| 232 |
std::vector<Frag>::iterator f; |
| 233 |
for(n = node.out.begin(), f = node.frags.begin(); n != node.out.end(); n++, f++) { |
| 234 |
if(!(*n)->visited || (*n)->error == 0) { |
| 235 |
vector<Cell *>::iterator c; |
| 236 |
Frag &frag = (*f); |
| 237 |
for(c = frag.begin(); c != frag.end(); c++) |
| 238 |
selected.insert(selected.end(), *c); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
void Visit(Node *node, std::queue<Node *> &qnode) { |
| 246 |
std::vector<Node *>::iterator n; |
| 247 |
for(n = node->in.begin(); n != node->in.end(); n++) |
| 248 |
if(!(*n)->visited) |
| 249 |
visit(*n, qnode); |
| 250 |
|
| 251 |
node->visited = true; |
| 252 |
qnode.push(node); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
}; |
| 257 |
|
| 258 |
template <class C> class FrustumPolicy { |
| 259 |
public: |
| 260 |
typedef C Cell; |
| 261 |
|
| 262 |
Frustumf frustum; |
| 263 |
|
| 264 |
float error; |
| 265 |
|
| 266 |
bool Expand(Cell *cell) { |
| 267 |
if(cell->Error() == 0) return false; |
| 268 |
float dist = Distance(*cell, frustum.ViewPoint()); |
| 269 |
|
| 270 |
|
| 271 |
if(dist < 0) return true; |
| 272 |
dist = pow(dist, 1.2); |
| 273 |
float res = cell->Error() / (frustum.Resolution()*dist); |
| 274 |
return res; |
| 275 |
} |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
template <class C> class DiracPolicy { |
| 280 |
public: |
| 281 |
typedef C Cell; |
| 282 |
|
| 283 |
Point3f focus; |
| 284 |
|
| 285 |
float error; |
| 286 |
|
| 287 |
DiracPolicy(Point3f &f = Point3f(0, 0, 0), float e = 0): focus(f), error(e) {} |
| 288 |
|
| 289 |
bool Expand(Cell *cell) { |
| 290 |
if(cell->Error() == 0) return false; |
| 291 |
float dist = Distance(*cell, focus); |
| 292 |
if(dist > 0) return true; |
| 293 |
return false; |
| 294 |
} |
| 295 |
}; |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
#endif |