Debian Codegraph @

vcg/wrap/mt/mt.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25
26 $Log: mt.h,v $
27 Revision 1.1 2004/04/01 11:33:41 ponchio
28 Untested first draft.
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 /** \addtogroup mt */
47 /*@{*/
48 /**
49 The templated class for representing a mt structure.
50 The class is templated over the Cell class which MUST implement this functions:
51 float Error();
52 float Distance(Cell &c, Point3f &p);
53 see mt_file.h for an example.
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 /** Update class for defining the history.
77 Notice how the Update class holds pointers.
78 You are responsible of holding the Cell structure and rebuild the structure
79 if the pointers change.
80 */
81 class Update {
82 public:
83 std::vector<Cell *> erased;
84 std::vector<Cell *> created;
85 };
86
87 ///build mt strucure from a sequence of updates.
88 void Load(std::vector<Update> &updates) {
89 //The last update erases everything.
90 assert(updates[0].erased.size() == 0);
91
92 //maps cell -> node containing it
93 std::map<Cell *, unsigned int> cell_node;
94 nodes.resize(updates.size());
95
96 //building fragments and nodes.
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 //created cells belong to this node, we look also for max error.
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 //Every erased cell already belonged to a node.
114 //we record for each node its cells.
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 //for every node with erased cells we build a frag and put the corresponding cells in it.
124 std::map<unsigned int, std::vector<Cell *> >::iterator e;
125 for(e = node_erased.begin(); e != node_erased.end(); e++) {
126 //Build a new Frag.
127 Frag fr;
128 float max_err = -1;
129
130 //Fill it with erased cells.
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 //Add the new Frag to the node.
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 //Update in and out of the nodes.
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 ///error based extraction
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) { //need to expand this node.
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 //custom extraction
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 //extract using a user class which must provides:
217 //bool refineCell(Cell *cell, Frustum &frustum);
218 //void addCell(Cell *cell);
219 //void removeCell(Cell *cell);
220 //template <class CONT> void extract(std::vector<Cell *> &selected, Frustum &frustum, T &error);
221
222 ///extract using the visited flag in nodes to a STL container of Cell *
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 ///add a node to the queue recursively mantaining coherence.
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 ///target error in screen space (pixels)
264 float error;
265
266 bool Expand(Cell *cell) {
267 if(cell->Error() == 0) return false;
268 float dist = Distance(*cell, frustum.ViewPoint());
269 /*Point3f line = frustum.viewPoint() - cell->sphere.center;
270 float dist = line.Norm() - cell->sphere.radius; */
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 ///really simple example policy
279 template <class C> class DiracPolicy {
280 public:
281 typedef C Cell;
282
283 Point3f focus;
284 ///max error in object space at distance 0
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 }//namespace vcg
298
299 #endif