vcglib/apps/nexus/nxsbuild.cpp

214 lines
6.4 KiB
C++
Raw Normal View History

2004-10-01 18:54:57 +02:00
#include "nxsbuild.h"
#include <set>
#include <map>
2004-10-08 16:46:26 +02:00
#include <iostream>
2004-10-01 18:54:57 +02:00
using namespace nxs;
using namespace vcg;
using namespace std;
void nxs::RemapVertices(Crude &crude,
VertRemap &vert_remap,
VFile<unsigned int> &face_remap,
vector<unsigned int> &patch_verts) {
for(unsigned int i = 0; i < crude.Faces(); i++) {
Crude::Face &face = crude.GetFace(i);
unsigned int patch = face_remap[i];
for(int k = 0; k < 3; k++) {
set<unsigned int> pp;
vert_remap.GetValues(face[k], pp);
if(!pp.count(patch)) {
vert_remap.Insert(face[k], patch);
patch_verts[patch]++;
}
}
}
}
void nxs::NexusAllocate(Crude &crude,
Nexus &nexus,
VFile<unsigned int> &face_remap,
vector<unsigned int> &patch_faces,
vector<unsigned int> &patch_verts) {
nexus.index.resize(patch_faces.size());
2004-10-08 16:46:26 +02:00
// unsigned int totchunks = 0;
2004-10-01 18:54:57 +02:00
//now that we know various sizes, lets allocate space
for(unsigned int i = 0; i < nexus.index.size(); i++) {
2004-10-08 16:46:26 +02:00
Nexus::PatchInfo &entry = nexus.index[i];
2004-10-01 18:54:57 +02:00
if(patch_faces[i] == 0 || patch_verts[i] == 0)
cerr << "Warning! Empty patch.\n";
2004-10-08 16:46:26 +02:00
nexus.patches.AddPatch(patch_verts[i], patch_faces[i]);
2004-10-01 18:54:57 +02:00
entry.nvert = patch_verts[i];
2004-10-08 16:46:26 +02:00
entry.nface = patch_faces[i];
2004-10-01 18:54:57 +02:00
entry.error = 0;
}
2004-10-08 16:46:26 +02:00
patch_faces.clear();
patch_faces.resize(nexus.index.size(), 0);
2004-10-01 18:54:57 +02:00
//now we sort the faces into the patches (but still using absolute indexing
//instead of relative indexing
for(unsigned int i = 0; i < crude.face.Size(); i++) {
Crude::Face &face = crude.face[i];
unsigned int npatch = face_remap[i];
2004-10-08 16:46:26 +02:00
Nexus::PatchInfo &entry = nexus.index[npatch];
// cerr << "Entrynf: " << entry.nface << endl;
2004-10-01 18:54:57 +02:00
//TODO this is slow because we have to initialize patch.
//just get patch.start.
Patch patch = nexus.GetPatch(npatch);
Crude::Face *faces = (Crude::Face *)patch.start;
//REMOVING degenerate faces
if(face[0] == face[1] || face[1] == face[2] || face[0] == face[2]) {
cerr << "Found degenerate.\n";
continue;
}
2004-10-08 16:46:26 +02:00
faces[patch_faces[npatch]++] = face;
}
for(unsigned int i = 0; i < nexus.index.size(); i++) {
Nexus::PatchInfo &entry = nexus.index[i];
entry.nface = patch_faces[i];
2004-10-01 18:54:57 +02:00
}
}
void nxs::NexusFill(Crude &crude,
Nexus &nexus,
VertRemap &vert_remap,
VFile<RemapLink> &border_remap) {
//finally for every patch we collect the vertices
//and fill the patch.
//we need to remember start and size in border_remap;
for(unsigned int i = 0; i < nexus.index.size(); i++) {
Patch patch = nexus.GetPatch(i);
2004-10-08 16:46:26 +02:00
Nexus::PatchInfo &entry = nexus.index[i];
// cerr << "entryf: " << entry.nface << endl;
// cerr << "nf: " << patch.nf << endl;
2004-10-01 18:54:57 +02:00
//make a copy of faces (we need to write there :P)
Crude::Face *faces = new Crude::Face[patch.nf];
//Test for degenerate faces?
2004-10-01 18:54:57 +02:00
memcpy(faces, (Crude::Face *)patch.start,
patch.nf * sizeof(Crude::Face));
//collect all vertices we need.
//TODO an hash_map would be faster?
unsigned int count = 0;
map<unsigned int, unsigned short> remap;
2004-10-06 18:40:47 +02:00
2004-10-08 16:46:26 +02:00
assert(patch.nf != 0);
2004-10-01 18:54:57 +02:00
for(unsigned int k = 0; k < patch.nf; k++) {
Crude::Face &face = faces[k];
for(int j = 0; j < 3; j++) {
if(!remap.count(face[j])) {
Point3f &v = crude.vert[face[j]];
patch.VertBegin()[remap.size()] = v;
entry.sphere.Add(v);
remap[face[j]] = count++;
}
patch.FaceBegin()[k*3 + j] = remap[face[j]];
}
}
assert(count == remap.size());
assert(entry.nvert == remap.size());
//record start of border:
2004-10-08 16:46:26 +02:00
unsigned int border_start = border_remap.Size();
2004-10-01 18:54:57 +02:00
//TODO hash_set?
set<unsigned int> border_patches;
map<unsigned int, unsigned short>::iterator m;
for(m = remap.begin(); m != remap.end(); m++) {
RemapLink link;
link.abs_vert = (*m).first;
link.rel_vert = (*m).second;
vert_remap.GetValues(link.abs_vert, border_patches);
assert(border_patches.size() >= 1);
if(border_patches.size() == 1) continue; //its not a border
set<unsigned int>::iterator s;
for(s = border_patches.begin(); s != border_patches.end(); s++) {
if((*s) == i) continue;
link.patch = *s;
border_remap.PushBack(link);
}
}
2004-10-08 16:46:26 +02:00
unsigned int needed = border_remap.Size() - border_start;
nexus.borders.AddBorder(2 * needed, needed);
2004-10-01 18:54:57 +02:00
delete []faces;
}
//we can now update bounding sphere.
for(unsigned int i = 0; i < nexus.index.size(); i++)
nexus.sphere.Add(nexus.index[i].sphere);
2004-10-06 18:40:47 +02:00
2004-10-01 18:54:57 +02:00
//and last convert RemapLinks into Links
2004-10-08 16:46:26 +02:00
for(unsigned int i = 0; i < nexus.borders.borders.size(); i++) {
BorderEntry &local = nexus.borders.borders[i];
2004-10-01 18:54:57 +02:00
//* 2 is to accomodate future borders
2004-10-08 16:46:26 +02:00
unsigned int remap_start = local.border_start/2;
2004-10-01 18:54:57 +02:00
// K is the main iterator (where we write to in nexus.borders)
for(unsigned int k = 0; k < local.border_used; k++) {
2004-10-08 16:46:26 +02:00
2004-10-01 18:54:57 +02:00
RemapLink start_link = border_remap[k + remap_start];
2004-10-08 16:46:26 +02:00
BorderEntry &remote = nexus.borders.borders[start_link.patch];
2004-10-01 18:54:57 +02:00
bool found = false;
unsigned int remote_remap_start = remote.border_start/2;
for(unsigned int j = 0; j < remote.border_used; j++) {
RemapLink end_link = border_remap[j + remote_remap_start];
2004-10-06 18:40:47 +02:00
// assert(end_link.rel_vert < remote.nvert);
2004-10-01 18:54:57 +02:00
if(start_link.abs_vert == end_link.abs_vert &&
end_link.patch == i) { //found the match
2004-10-06 18:40:47 +02:00
// assert(!found);
2004-10-01 18:54:57 +02:00
nexus.borders[k + local.border_start] = Link(start_link.rel_vert,
end_link.rel_vert,
start_link.patch);
found = true;
}
}
}
}
nexus.borders.Flush();
//Checking border consistency:
/* for(unsigned int i = 0; i < nexus.index.size(); i++) {
Border border = nexus.GetBorder(i);
2004-10-08 16:46:26 +02:00
Nexus::PatchInfo &entry = nexus.index[i];
2004-10-01 18:54:57 +02:00
for(unsigned int k = 0; k < border.Size(); k++) {
Link &link = border[k];
if(link.start_vert >= entry.nvert) {
cerr << "K: " << k << endl;
cerr << "patch: " << i << " nvert: " << entry.nvert << " startv: "
<< link.start_vert << endl;
2004-10-08 16:46:26 +02:00
// cerr << "bstart: " << entry.border_start
// << "bsize: " << entry.border_size << endl;
2004-10-01 18:54:57 +02:00
}
assert(link.end_patch < nexus.index.size());
assert(link.start_vert < entry.nvert);
2004-10-08 16:46:26 +02:00
Nexus::PatchInfo &remote = nexus.index[link.end_patch];
2004-10-01 18:54:57 +02:00
assert(link.end_vert < remote.nvert);
}
}*/
}