Extraction rewrittten (to fix recusive problems).

This commit is contained in:
Federico Ponchio 2005-03-02 10:40:18 +00:00
parent 5b922e3d32
commit ba729ee777
9 changed files with 236 additions and 216 deletions

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.11 2005/02/20 19:49:44 ponchio
cleaning (a bit more).
Revision 1.10 2005/02/20 18:07:00 ponchio
cleaning.
@ -74,14 +77,14 @@ void Extraction::Extract(NexusMt *_mt) {
visible.clear();
visible.resize(mt->size(), true);
heap.clear();
front.clear();
Visit(root);
while(heap.size()) {
pop_heap(heap.begin(), heap.end());
HeapNode hnode = heap.back();
heap.pop_back();
while(front.size()) {
pop_heap(front.begin(), front.end());
HeapNode hnode = front.back();
front.pop_back();
Node *node = hnode.node;
if(Visited(node)) continue;
@ -97,50 +100,51 @@ void Extraction::Init() {
max_error = -1;
front.clear();
back.clear();
errors.clear();
node_errors.clear();
node_errors.resize(mt->history.n_nodes(), -1);
//I want to add all coarsable nodes
//and all refinable node (being careful about recursive dependencies)
for(Node *node = root; node != sink; node++) {
if(!Visited(node)) continue;
if(node != root && CanCoarse(node))
back.push_back(HeapNode(node, GetNodeError(node)));
for(Node::iterator n = node->out_begin; n != node->out_end; n++) {
Node *child = n->node;
if(Visited(child)) continue;
if(node_errors[child - root] != -1) continue; //already visited
float *error = GetNodeError(child);
if(CanRefine(node)) // TODO? && error > target_error
front.push_back(HeapNode(child, error));
if(*error > max_error) max_error = *error;
}
}
//recursively fix error
for(Node *node = root; node != sink; node++)
if(node_errors[node - root] != -1)
SetError(node, node_errors[node-root]);
//estimate cost of all the cut arcs (i need the visible info)
Cost cost;
for(Node *node = root; node != sink; node++) {
if(!Visited(node)) continue;
Node *nodes = mt->history.nodes;
for(unsigned int i = 0; i < visited.size(); i++) {
if(!visited[i]) continue;
Node &node = nodes[i];
bool cancoarse = true;
Node::iterator n;
for(n = node.out_begin; n != node.out_end; n++) {
if(!Visited((*n).node)) {
float maxerror = -1;
for(Node::iterator n = node->out_begin; n != node->out_end; n++) {
Link &link = *n;
if(Visited((*n).node)) continue;
for(unsigned int patch = link.begin; patch != link.end; patch++) {
Entry &entry = (*mt)[patch];
bool visible;
float error = metric->GetError(entry, visible);
if(error > maxerror) maxerror = error;
cost.extr += entry.ram_size;
if(Visible(patch)) cost.draw += entry.ram_size;
if(!entry.patch) cost.disk += entry.disk_size;
}
}
}
SetVisible(patch, visible);
if(visible)
cost.draw += entry.ram_size;
if(!entry.patch)
cost.disk += entry.disk_size;
}
if((*n).node != sink && maxerror > target_error)
front.push_back(HeapNode((*n).node, maxerror));
if(maxerror > max_error) max_error = maxerror;
} else
cancoarse = false;
}
if(cancoarse && (&node != root)) {
float error = GetRefineError(&node);
back.push_back(HeapNode(&node, error));
}
}
make_heap(front.begin(), front.end());
make_heap(back.begin(), back.end(), greater<HeapNode>());
@ -152,8 +156,7 @@ void Extraction::Init() {
void Extraction::Update(NexusMt *_mt) {
mt = _mt;
root = mt->history.Root();
sink = root + (mt->history.n_nodes()-1);
//clear statistics
sink = mt->history.Sink();
if(!visited.size()) {
visited.resize(mt->history.n_nodes(), false);
@ -164,88 +167,70 @@ void Extraction::Update(NexusMt *_mt) {
Init();
bool no_draw = false;
//TODO big problem: nodes a (error 10) with parent b (error -1)
//i try to refine a, refine b (recursive) but fail to refine a
//next step i coarse b whis cause a cycle.
/* Updateing strategy:
if i can refine (not at leaves, have draw and extr buffer, not past target_error)
if i can refine (not at leaves,
have draw and extr buffer,
not past target_error)
i try to refine BUT
i can fail because i finish some buffer (exp. while recursively refine)
i can fail because i finish some buffer
(then i put the operation back on the stack)
if i have finished disk i should just quit
if i cannot refine i consider coarsening:
i need 1) not be at root (eheh)
2) have finished draw and extr buffer
3) do not make global error worse (unless it is < target_error...)
4) check it is not a recursive coarsening (drop it otherwise)
(unless i am at error < target so i want to coarse
3) do not make global error worse
(unless it is error < target_error...)
a stability term is added (1.1) so that we do not flip
nodes in and out quickly
i try to coarse BUT
i can fail because i need disk
(then i put the operation back on the stack)
if i cannot coarse i just quit
if i cannt coarse i just quit
*/
while(1) {
if(!no_draw && //we have buffer
front.size() && //we are not at max level
front[0].error > target_error) { //we are not already at target_error
bool can_refine = true;
max_error = front[0].error;
while(1) {
while(can_refine) { //we have available budget
if(!front.size()) break; //we are at max level
if(*front[0].error < target_error) break; //already at target_error
max_error = *front[0].error;
pop_heap(front.begin(), front.end());
HeapNode hnode = front.back();
front.pop_back();
if(!Visited(hnode.node) && !Refine(hnode))
no_draw = true;
continue;
if(!Visited(hnode.node) && CanRefine(hnode.node)) {
if(!Refine(hnode)) {
can_refine = false;
front.push_back(hnode);
push_heap(front.begin(), front.end());
}
}
}
if(!back.size()) {
//cerr << "nothing to coarse!\n";
break; //nothing to coarse (happen only on extr_max < root.extr
}
if(no_draw) { //suppose i have no more buffer
//TODO see point 3
//if i do error damages coarsening better get out
if(front.size() && ((back.front().error + 0.001) >= front.front().error)) {
//cerr << "Balanced cut\n";
if(!back.size()) //nothing to coarse (happen only on extr_max < root.extr)
break;
}
if(!front.size() && back.front().error >= target_error) {
//cerr << "Maxed out\n";
break;
}
}
//nothing to refine, coarse only if error <= target_error
if(!no_draw && back.front().error >= target_error) {
//cerr << "error dominating\n";
if(*back.front().error >= target_error &&
(!front.size() ||
(*back.front().error * 1.4) >= *front.front().error))
break;
}
pop_heap(back.begin(), back.end(), greater<HeapNode>());
HeapNode hnode = back.back();
back.pop_back();
if(Visited(hnode.node)) {
bool recursive = false;
Node::iterator i;
for(i = hnode.node->out_begin; i != hnode.node->out_end; i++) {
Node *child = (*i).node;
if(Visited(child)) recursive = true;
}
if(!recursive && !Coarse(hnode)) { //no more disk so. push back on heap the heapnode
if(Visited(hnode.node) && //not already coarsed
CanCoarse(hnode.node) && //all children !visited
!Coarse(hnode)) { //no more disk
back.push_back(hnode);
push_heap(back.begin(), back.end(), greater<HeapNode>());
break;
}
}
no_draw = false;
can_refine = true;
}
Select();
@ -264,7 +249,6 @@ void Extraction::Update(NexusMt *_mt) {
Link &link = (*l);
for(unsigned int k = link.begin; k != link.end; k++) {
selected.push_back(Item(k, i));
errors[k] = i;
}
}
} else if(back.size()) {
@ -277,81 +261,63 @@ void Extraction::Update(NexusMt *_mt) {
Link &link = (*l);
for(unsigned int k = link.begin; k != link.end; k++) {
selected.push_back(Item(k, i));
errors[k] = i;
}
}
}
}
}
float Extraction::GetRefineError(Node *node) {
float maxerror = -1;
float *Extraction::GetNodeError(Node *node) {
float &maxerror = node_errors[node-root];
for(Node::iterator i = node->in_begin; i != node->in_end; i++) {
Link &link = *i;
for(unsigned int p = link.begin; p != link.end; p++) {
Entry &entry = (*mt)[p];
bool visible;
float error = metric->GetError(entry, visible);
// cerr << "Error for patch: " << p << " -> " << error << endl;
if(error > maxerror) maxerror = error;
SetVisible(p, visible);
}
}
return maxerror;
return &maxerror;
}
bool Extraction::Refine(HeapNode hnode) {
Node *node = hnode.node;
//recursively refine parent if applicable.
for(Node::iterator i = node->in_begin; i != node->in_end; i++) {
Node *parent = (*i).node;
if(!Visited(parent)) {
//Here i use parent refine error!!!
if(!Refine(HeapNode(parent, hnode.error))) return false;
}
}
Cost cost;
Diff(node, cost);
bool failed = false;
if(disk_used + cost.disk > disk_max) {
//cerr << "Disk failed\n";
failed = true;
}
if(extr_used + cost.extr > extr_max) {
//cerr << "Extr failed\n";
failed = true;
}
if(draw_used + cost.draw > draw_max) {
//cerr << "Draw failed\n";
failed = true;
}
if(failed) {
front.push_back(hnode);
push_heap(front.begin(), front.end());
if(disk_used + cost.disk > disk_max ||
extr_used + cost.extr > extr_max ||
draw_used + cost.draw > draw_max)
return false;
}
extr_used += cost.extr;
draw_used += cost.draw;
disk_used += cost.disk;
SetVisited(node, true);
//now add to the front children (unless sink node)
for(Node::iterator i = node->out_begin; i != node->out_end; i++) {
Link &link = *i;
if(link.node == sink) continue;
float maxerror = GetRefineError(link.node);
if(maxerror > target_error)
front.push_back(HeapNode((*i).node, maxerror));
}
float *error = &node_errors[link.node - root];
if(*error == -1)
error = GetNodeError(link.node);
if(*hnode.error < *error) *hnode.error = *error;
//TODO if(maxerror > target_error)
if(CanRefine((*i).node)) {
front.push_back(HeapNode((*i).node, error));
push_heap(front.begin(), front.end());
}
}
back.push_back(hnode);
push_heap(back.begin(), back.end(), greater<HeapNode>());
@ -362,19 +328,9 @@ bool Extraction::Coarse(HeapNode hnode) {
//cerr << "Coarse node: " << (void *)hnode.node << " err: " << hnode.error << endl;
Node *node = hnode.node;
//recursively coarse children if applicable.
for(Node::iterator i = node->out_begin; i != node->out_end; i++) {
Node *child = (*i).node;
float error = GetRefineError(child);
HeapNode hchild(child, error);
if(Visited(child)) {
if(!Coarse(hchild)) return false;
}
}
Cost cost;
Diff(node, cost);
extr_used -= cost.extr;
draw_used -= cost.draw;
disk_used -= cost.disk;
@ -387,11 +343,17 @@ bool Extraction::Coarse(HeapNode hnode) {
for(Node::iterator i = node->in_begin; i != node->in_end; i++) {
Link &link = *i;
if(link.node == root) continue;
float maxerror = GetRefineError(link.node);
back.push_back(HeapNode(link.node, maxerror));
float *error = &node_errors[link.node - root];
if(*error == -1)
error = GetNodeError(link.node);
if(*error < *hnode.error) *error = *hnode.error;
if(CanCoarse(link.node)) {
back.push_back(HeapNode(link.node, error));
push_heap(back.begin(), back.end(), greater<HeapNode>());
}
}
front.push_back(hnode);
push_heap(front.begin(), front.end());
@ -414,7 +376,6 @@ void Extraction::Select() {
Link &link = *n;
for(unsigned int p = link.begin; p != link.end; p++) {
selected.push_back(Item(p, 0));
errors[p] = 0.0f;
}
}
}
@ -449,9 +410,10 @@ void Extraction::Visit(Node *node) {
}
//TODO this check may be dangerous for non saturating things...
if(maxerror > target_error) {
HeapNode hnode((*i).node, maxerror);
heap.push_back(hnode);
push_heap(heap.begin(), heap.end());
node_errors[(*i).node - root] = maxerror;
HeapNode hnode((*i).node, &node_errors[(*i).node - root]);
front.push_back(hnode);
push_heap(front.begin(), front.end());
}
}
}
@ -460,7 +422,7 @@ bool Extraction::Expand(HeapNode &node) {
if(extr_used >= extr_max) return false;
if(draw_used >= draw_max) return false;
// if(disk_used >= disk_max) return false;
return node.error > target_error;
return *node.error > target_error;
}
void Extraction::Diff(Node *node, Cost &cost) {
@ -471,9 +433,7 @@ void Extraction::Diff(Node *node, Cost &cost) {
Entry &entry = (*mt)[p];
cost.extr -= entry.ram_size;
if(Visible(p)) cost.draw -= entry.ram_size;
if(!entry.patch)
cost.disk -= entry.disk_size;
if(!entry.patch) cost.disk -= entry.disk_size;
}
}
@ -483,10 +443,30 @@ void Extraction::Diff(Node *node, Cost &cost) {
Entry &entry = (*mt)[p];
cost.extr += entry.ram_size;
if(Visible(p)) cost.draw += entry.ram_size;
if(!entry.patch)
cost.disk += entry.disk_size;
if(!entry.patch) cost.disk += entry.disk_size;
}
}
}
void Extraction::SetError(Node *node, float error) {
for(Node::iterator i = node->in_begin; i != node->in_end; i++)
if(node_errors[(*i).node - root] != -1 &&
node_errors[(*i).node - root] < error) {
node_errors[(*i).node - root] = error;
SetError((*i).node, error);
}
}
bool Extraction::CanRefine(Node *node) {
for(Node::iterator i = node->in_begin; i != node->in_end; i++)
if(!Visited((*i).node))
return false;
return true;
}
bool Extraction::CanCoarse(Node *node) {
for(Node::iterator i = node->out_begin; i != node->out_end; i++)
if(Visited((*i).node))
return false;
return true;
}

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.9 2005/02/20 18:07:01 ponchio
cleaning.
Revision 1.8 2005/02/19 16:22:45 ponchio
Minor changes (visited and Cell)
@ -74,13 +77,13 @@ class Extraction {
struct HeapNode {
Node *node;
float error;
float *error;
HeapNode(Node *_node, float _error): node(_node), error(_error) {}
HeapNode(Node *_node, float *_error): node(_node), error(_error) {}
bool operator<(const HeapNode &node) const {
return error < node.error; }
return *error < *node.error; }
bool operator>(const HeapNode &node) const {
return error > node.error; }
return *error > *node.error; }
};
Metric *metric;
@ -93,19 +96,17 @@ class Extraction {
std::vector<bool> visited;
std::vector<bool> visible;
std::map<unsigned int, float> errors;
std::vector<float> node_errors;
std::vector<Item> selected;
unsigned int draw_size; //first in selected should be drawn
std::vector<HeapNode> heap; //no realtime extraxtion TODO (use front)
//nodes that i can expand to
std::vector<HeapNode> front;
//nodes that i can contract
std::vector<HeapNode> back;
unsigned int tot_budget;
std::vector<HeapNode> front; //nodes that i can expand to
std::vector<HeapNode> back; //nodes that i can contract
Extraction();
~Extraction();
@ -113,9 +114,9 @@ class Extraction {
void Extract(NexusMt *mt);
void Update(NexusMt *mt);
bool Visible(unsigned int p) { return visible[p]; }
void SetVisible(unsigned int p, bool v) { visible[p] = v; }
protected:
void Select();
@ -124,11 +125,10 @@ class Extraction {
bool Expand(HeapNode &node);
void Diff(Node *node, Cost &cost);
void Init();
bool Refine(HeapNode node);
bool Coarse(HeapNode node);
void Init();
bool Visited(Node *node) { return visited[node - root]; }
void SetVisited(Node *node, bool v) { visited[node - root] = v; }
@ -137,10 +137,14 @@ class Extraction {
Node *root;
Node *sink;
float GetRefineError(Node *node);
//return inbound links max error. remember to update patch visibility
float *GetNodeError(Node *node);
//this look for parent nodes with error and fix it should be <
void SetError(Node *node, float error);
bool CanCoarse(Node *node);
bool CanRefine(Node *node);
};
}//namespace
#endif

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.7 2005/02/20 19:49:44 ponchio
cleaning (a bit more).
Revision 1.6 2005/02/20 00:43:23 ponchio
Less memory x extraction. (removed frags)
@ -88,7 +91,7 @@ namespace nxs {
~History();
Node *Root() { return &nodes[0]; }
Node *Sink() { return Root() + n_nodes() -1; }
void Clear();
void ClearQuick();
void ClearUpdates();

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.9 2005/02/22 10:37:55 ponchio
Debug, cleaning and optimization.
Revision 1.8 2005/02/21 20:49:30 ponchio
some culling bug.
@ -84,13 +87,15 @@ namespace nxs {
float error = entry.error/frustum.Resolution(dist);
if(culling) {
float remote = frustum.Remoteness(sph.Center(), sph.Radius());
if(remote > 0) {
if(frustum.IsOutside(sph.Center(), sph.Radius())) {
visible = false;
// TODO FIXME remoteness is bugged... (not much only bit
//if we are close to the surface, the projection of
//the bounding sphere in screen space comes out too small
//just using resolution and radius. Im too lazy to fix it.
if(frustum.IsOutside(sph.Center(), sph.Radius()))
visible = false;
if(remote > 0)
error /= remote;
} else if(entry.cone.Backface(sph, frustum.ViewPoint())) {
visible = false;

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.36 2005/02/22 10:38:06 ponchio
Debug, cleaning and optimization.
Revision 1.35 2005/02/20 19:49:44 ponchio
cleaning (a bit more).
@ -148,15 +151,24 @@ void NexusMt::Render(Extraction &extraction, DrawContest &contest,
Stats *stats) {
static ::GLUquadricObj * spr = gluNewQuadric();
//Updating heap errors
map<unsigned int, float> errors;
for(unsigned int i = 0; i < extraction.selected.size(); i++) {
Item &item = extraction.selected[i];
errors[item.id] = item.error;
}
for(unsigned int i = 0; i < heap.size(); i++) {
Item &item = heap[i];
if(!extraction.errors.count(item.id)) {
if(!errors.count(item.id)) {
item.error = 1e20;
} else
item.error = extraction.errors[item.id];
item.error = errors[item.id];
}
make_heap(heap.begin(), heap.end());
preload.post(extraction.selected);
glEnableClientState(GL_VERTEX_ARRAY);

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.44 2005/03/01 11:20:22 ponchio
nothing really
Revision 1.43 2005/02/20 18:07:01 ponchio
cleaning.
@ -202,7 +205,7 @@ using namespace vcg;
using namespace nxs;
bool fullscreen = false;
int width = 1024
int width = 1024;
int height = 768;
Point3f view(0, 0, 5);
@ -534,7 +537,9 @@ int main(int argc, char *argv[]) {
// extraction.frustum->GetView();
extraction.metric->GetView();
if(!realtime) {
extraction.visited.clear();
extraction.Extract(&nexus);
extraction.Update(&nexus);
} else {
extraction.Update(&nexus);
}

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.25 2005/03/01 11:21:20 ponchio
Added line intersection
Revision 1.24 2005/02/22 14:20:44 ponchio
debug and mostly vertex unifying across borders
(still not perfect... :P)
@ -61,6 +64,7 @@ Added copyright
//#include <wrap/strip/tristrip.h>
#include "nxsalgo.h"
#include "extraction.h"
#include "vpartition.h"
#include "vfile.h"
#include "nexus.h"
@ -68,6 +72,7 @@ Added copyright
#include "watch.h"
#include <vcg/space/line3.h>
#include <vcg/space/intersection3.h>
using namespace std;
using namespace nxs;
@ -731,10 +736,10 @@ bool nxs::LineIntersect(Nexus &nexus, Extraction &extraction,
return false;
bool found = false;
bool min_dist = -1;
float min_dist = -1;
float bar1, bar2, dist;
for(unsigned int i = 0; i < extraction.draw_size; i++) {
unsigned int p = extraction.selected[i];
unsigned int p = extraction.selected[i].id;
if(!Intersection(nexus[p].sphere, line, hit, tmp))
continue;
Patch &patch = nexus.GetPatch(p);
@ -747,7 +752,7 @@ bool nxs::LineIntersect(Nexus &nexus, Extraction &extraction,
if(Intersection(line, v0, v1, v2, bar1, bar2, dist) &&
dist > 0 &&
(min_dist == -1 || min_dist > dist)) {
hit = v0*(1-bar1-bar2)+v1*bar1+b2*bar2;
hit = v0*(1-bar1-bar2) + v1*bar1 + v2*bar2;
min_dist = dist;
found = true;
}
@ -761,7 +766,7 @@ bool nxs::LineIntersect(Nexus &nexus, Extraction &extraction,
if(Intersection(line, v0, v1, v2, bar1, bar2, dist) &&
dist > 0 &&
(min_dist == -1 || min_dist > dist)) {
hit = v0*(1-bar1-bar2)+v1*bar1+b2*bar2;
hit = v0*(1-bar1-bar2) + v1*bar1 + v2*bar2;
min_dist = dist;
found = true;
}

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.11 2005/03/01 11:21:20 ponchio
Added line intersection
Revision 1.10 2005/02/22 14:20:44 ponchio
debug and mostly vertex unifying across borders
(still not perfect... :P)
@ -56,14 +59,14 @@ Added copyright
#include <vector>
#include "patch.h"
#include <vcg/space/sphere3.h>
class vcg::Line3f;
#include <vcg/space/line3.h>
namespace nxs {
class Nexus;
class Patch;
class Extraction;
struct ZEntry {
@ -72,8 +75,7 @@ namespace nxs {
bool operator<(const ZEntry &e) const { return pos < e.pos; }
};
//for every patch return close by (sphere intersecting)
//threshold is added to the distance to make sure we do not miss anything
//for every patch return close by (sphere intersecting) //threshold is added to the distance to make sure we do not miss anything
void Connect(Nexus &nexus, std::vector< std::set<unsigned int> > &close,
float threshold);

View File

@ -24,6 +24,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.26 2005/02/22 10:38:14 ponchio
Debug, cleaning and optimization.
Revision 1.25 2005/02/21 17:55:47 ponchio
debug debug debug
@ -369,8 +372,7 @@ int main(int argc, char *argv[]) {
if(add_colors) {
if(!plysource.size()) {
cerr << "No plysource specified when adding color (-p option)\n";
return -1;
}
} else {
if(!tri::io::ImporterPLY<CMesh>::Open(mesh, plysource.c_str())) {
cerr << "Could not load ply: " << plysource << endl;
return -1;
@ -382,6 +384,7 @@ int main(int argc, char *argv[]) {
grid.SetBBox(box);
grid.Set(mesh.face);
}
}
Signature signature = nexus.signature;
if(add_strips) signature.face = Signature::STRIPS;
@ -466,7 +469,7 @@ int main(int argc, char *argv[]) {
vector<unsigned short> strip;
if(add_strips) {
ComputeTriStrip(src_patch.nf, src_patch.FaceBegin(), strip);
assert(strip.size() < 32767);
assert(strip.size() < 65000);
out.AddPatch(src_entry.nvert, strip.size(), src_border.Capacity());
if(verbose) {
cerr << "tri: " << src_patch.nf << " strip: " << strip.size()
@ -553,9 +556,10 @@ int main(int argc, char *argv[]) {
}
if(add_colors) {
// if(!plysource.size())
//source of color:
cerr << "Unsupported color\n";
return -1;
// cerr << "Unsupported color\n";
// return -1;
}
if(qvertex && add_normals) {