vcglib/apps/nexus/nexus.cpp

360 lines
10 KiB
C++
Raw Normal View History

2004-07-02 15:00:02 +02:00
#include <iostream>
2004-09-17 17:25:59 +02:00
#include <assert.h>
2004-07-02 15:00:02 +02:00
#include "nexus.h"
using namespace std;
using namespace vcg;
using namespace nxs;
Nexus::Nexus(): index_file(NULL) {}
Nexus::~Nexus() {
Close();
}
2004-09-17 17:25:59 +02:00
bool Nexus::Create(const string &file, Signature sig) {
2004-07-02 15:00:02 +02:00
index_file = fopen((file + ".nxs").c_str(), "wb+");
if(!index_file) {
cerr << "Could not create file: " << file << ".nxs\n";
return false;
}
2004-09-16 16:25:16 +02:00
signature = sig;
2004-07-02 15:00:02 +02:00
totvert = 0;
totface = 0;
sphere = Sphere3f();
2004-09-16 16:25:16 +02:00
index.clear();
2004-07-02 15:00:02 +02:00
//Important: chunk_size must be 1 so that i can use Region in VFile.
if(!patches.Create(file + ".nxp", 1)) {
cerr << "Could not create file: " << file << ".nxp" << endl;
return false;
}
2004-07-04 16:26:46 +02:00
if(!borders.Create(file + ".nxb", 1)) {
cerr << "Could not create file: " << file << ".nxb" << endl;
2004-07-02 15:00:02 +02:00
return false;
}
2004-09-16 16:25:16 +02:00
history.clear();
2004-07-02 15:00:02 +02:00
return true;
}
bool Nexus::Load(const string &file) {
index_file = fopen((file + ".nxs").c_str(), "rb+");
if(!index_file) return false;
unsigned int readed;
2004-09-16 16:25:16 +02:00
readed = fread(&signature, sizeof(unsigned int), 1, index_file);
if(!readed) return false;
2004-07-02 15:00:02 +02:00
readed = fread(&totvert, sizeof(unsigned int), 1, index_file);
if(!readed) return false;
readed = fread(&totface, sizeof(unsigned int), 1, index_file);
if(!readed) return false;
readed = fread(&sphere, sizeof(Sphere3f), 1, index_file);
if(!readed) return false;
unsigned int size; //size of index
readed = fread(&size, sizeof(unsigned int), 1, index_file);
if(!readed) return false;
index.resize(size);
readed = fread(&index[0], sizeof(Entry), size, index_file);
if(readed != size) return false;
2004-09-16 16:25:16 +02:00
//history size;
fread(&size, sizeof(unsigned int), 1, index_file);
vector<unsigned int> buffer;
buffer.resize(size);
fread(&(buffer[0]), sizeof(unsigned int), size, index_file);
//number of history updates
size = buffer[0];
history.resize(size);
unsigned int pos = 1;
for(unsigned int i = 0; i < size; i++) {
unsigned int erased = buffer[pos++];
unsigned int created = buffer[pos++];
history[i].erased.resize(erased);
history[i].created.resize(created);
for(unsigned int e = 0; e < erased; e++)
history[i].erased[e] = buffer[pos++];
for(unsigned int e = 0; e < created; e++)
history[i].created[e] = buffer[pos++];
}
2004-07-04 16:26:46 +02:00
if(!patches.Load(file + ".nxp", 1)) return false;
if(!borders.Load(file + ".nxb", 1)) return false;
2004-07-02 15:00:02 +02:00
return true;
}
void Nexus::Close() {
if(!index_file) return;
rewind(index_file);
2004-09-16 16:25:16 +02:00
fwrite(&signature, sizeof(unsigned int), 1, index_file);
2004-07-02 15:00:02 +02:00
fwrite(&totvert, sizeof(unsigned int), 1, index_file);
fwrite(&totface, sizeof(unsigned int), 1, index_file);
fwrite(&sphere, sizeof(Sphere3f), 1, index_file);
unsigned int size = index.size(); //size of index
fwrite(&size, sizeof(unsigned int), 1, index_file);
2004-07-30 15:18:25 +02:00
fwrite(&(index[0]), sizeof(Entry), size, index_file);
2004-09-16 16:25:16 +02:00
vector<unsigned int> buffer;
buffer.push_back(history.size());
for(unsigned int i = 0; i < history.size(); i++) {
Update &update = history[i];
buffer.push_back(update.erased.size());
buffer.push_back(update.created.size());
for(unsigned int e = 0; e < update.erased.size(); e++)
buffer.push_back(update.erased[e]);
for(unsigned int e = 0; e < update.created.size(); e++)
buffer.push_back(update.created[e]);
}
size = buffer.size();
fwrite(&size, sizeof(unsigned int), 1, index_file);
fwrite(&(buffer[0]), sizeof(unsigned int), size, index_file);
2004-07-02 15:00:02 +02:00
fclose(index_file);
2004-07-30 15:18:25 +02:00
index_file = NULL;
2004-07-02 15:00:02 +02:00
patches.Close();
borders.Close();
}
Patch Nexus::GetPatch(unsigned int patch) {
Entry &entry = index[patch];
2004-07-04 16:26:46 +02:00
Chunk *start = patches.GetRegion(entry.patch_start, entry.patch_size);
2004-09-16 16:25:16 +02:00
return Patch(signature, start, entry.nvert, entry.nface);
2004-07-04 16:26:46 +02:00
}
Border Nexus::GetBorder(unsigned int patch) {
Entry &entry = index[patch];
Link *start = borders.GetRegion(entry.border_start, entry.border_size);
2004-09-30 02:27:42 +02:00
return Border(start, entry.border_used, entry.border_size);
2004-07-04 16:26:46 +02:00
}
unsigned int Nexus::AddPatch(unsigned int nvert, unsigned int nface,
unsigned int nbord) {
Entry entry;
entry.patch_start = patches.Size();
2004-09-16 16:25:16 +02:00
entry.patch_size = Patch::ChunkSize(signature, nvert, nface);
2004-07-04 16:26:46 +02:00
entry.border_start = borders.Size();
entry.border_size = nbord;
2004-09-17 17:25:59 +02:00
entry.border_used = 0;
2004-07-04 16:26:46 +02:00
entry.nvert = nvert;
entry.nface = nface;
patches.Resize(patches.Size() + entry.patch_size);
borders.Resize(borders.Size() + nbord);
index.push_back(entry);
2004-08-27 02:38:34 +02:00
totvert += nvert;
totface += nface;
2004-07-04 16:26:46 +02:00
return index.size() -1;
}
2004-08-27 02:38:34 +02:00
void Nexus::Join(const std::set<unsigned int> &patches,
2004-07-05 17:49:39 +02:00
std::vector<Point3f> &newvert,
2004-07-04 16:26:46 +02:00
std::vector<unsigned int> &newface,
2004-07-05 17:49:39 +02:00
std::vector<Link> &newbord) {
2004-07-04 16:26:46 +02:00
map<unsigned int, vector<unsigned int> > remap;
2004-09-17 17:25:59 +02:00
set<Link> newborders;
set<unsigned int> erased;
for(int u = 0; u < history.size(); u++)
for(int e = 0; e < history[u].erased.size(); e++)
erased.insert(history[u].erased[e]);
2004-07-04 16:26:46 +02:00
2004-08-27 02:38:34 +02:00
set<unsigned int>::const_iterator i;
2004-07-04 16:26:46 +02:00
for(i = patches.begin(); i != patches.end(); i++) {
unsigned int patch = *i;
Nexus::Entry &entry = index[patch];
remap[*i].resize(entry.nvert, 0xffffffff);
}
2004-09-17 17:25:59 +02:00
2004-07-04 16:26:46 +02:00
unsigned int vcount = 0;
unsigned int fcount = 0;
unsigned int bcount = 0;
for(i = patches.begin(); i != patches.end(); i++) {
unsigned int patch = *i;
2004-09-17 17:25:59 +02:00
vector<unsigned int> &vmap = remap[*i];
2004-07-04 16:26:46 +02:00
Nexus::Entry &entry = index[patch];
fcount += entry.nface;
2004-09-17 17:25:59 +02:00
// assert(fcount < 0xffff);
2004-07-04 16:26:46 +02:00
for(unsigned int k = 0; k < entry.nvert; k++) {
2004-09-17 17:25:59 +02:00
if(vmap[k] == 0xffffffff) { //first time
vmap[k] = vcount++;
2004-07-04 16:26:46 +02:00
}
}
Border border = GetBorder(patch);
for(unsigned int k = 0; k < border.Size(); k++) {
2004-09-17 17:25:59 +02:00
Link link = border[k];
if(link.IsNull()) continue;
assert(link.start_vert < entry.nvert);
assert(vmap[link.start_vert] != 0xffffffff);
if(!remap.count(link.end_patch)) { //external
//test if erased in history... in wich case we do not add border
if(!erased.count(link.end_patch)) {
link.start_vert = vmap[link.start_vert];
newborders.insert(link);
}
continue;
}
//internal
2004-07-04 16:26:46 +02:00
if(remap[link.end_patch][link.end_vert] == 0xffffffff) { //first time
2004-09-17 17:25:59 +02:00
remap[link.end_patch][link.end_vert] = vmap[link.start_vert];
2004-07-04 16:26:46 +02:00
}
}
}
newvert.resize(vcount);
newface.resize(fcount*3);
2004-09-17 17:25:59 +02:00
newbord.resize(0);
2004-07-04 16:26:46 +02:00
fcount = 0;
for(i = patches.begin(); i != patches.end(); i++) {
Patch patch = GetPatch(*i);
2004-09-17 17:25:59 +02:00
// Border border = GetBorder(*i);
2004-07-04 16:26:46 +02:00
vector<unsigned int> &vmap = remap[*i];
2004-09-17 17:25:59 +02:00
for(unsigned int i = 0; i < patch.nv; i++) {
assert(vmap[i] < vcount);
2004-07-04 16:26:46 +02:00
newvert[vmap[i]] = patch.Vert(i);
2004-09-17 17:25:59 +02:00
}
2004-07-04 16:26:46 +02:00
2004-09-17 17:25:59 +02:00
for(unsigned int i = 0; i < patch.nf; i++) {
2004-07-04 16:26:46 +02:00
for(int k = 0; k < 3; k++) {
newface[3*fcount + k] = vmap[patch.Face(i)[k]];
}
fcount++;
2004-09-17 17:25:59 +02:00
assert(fcount *3 <= newface.size());
2004-07-04 16:26:46 +02:00
}
2004-09-17 17:25:59 +02:00
/* for(unsigned int i = 0; i < border.Size(); i++) {
Link link = border[i];
if(patches.count(link.end_patch)) continue;
link.start_vert = vmap[link.start_vert];
newbord.push_back(link);*/
/* if(remap.count(link.end_patch)) continue;
2004-07-04 16:26:46 +02:00
Link newlink = link;
newlink.start_vert = vmap[link.start_vert];
2004-09-17 17:25:59 +02:00
newbord[bcount++] = newlink;*/
// }
2004-07-04 16:26:46 +02:00
}
2004-09-17 17:25:59 +02:00
set<Link>::iterator b;
for(b = newborders.begin(); b != newborders.end(); b++)
newbord.push_back(*b);
2004-07-05 17:49:39 +02:00
/* unsigned int newentry = AddPatch(vcount, fcount, bcount);
2004-07-04 16:26:46 +02:00
Patch newpatch = GetPatch(newentry);
Border newborder = GetBorder(newentry);
memcpy(newpatch.VertBegin(), &(newvert)[0],
newvert.size() * sizeof(Point3f));
memcpy(newpatch.FaceBegin(), &(newface)[0],
newface.size() * sizeof(unsigned short));
memcpy(&(newborder[0]), &(newbord[0]),
2004-07-05 17:49:39 +02:00
newbord.size() * sizeof(Link));*/
return;
2004-07-02 15:00:02 +02:00
}
2004-09-17 17:25:59 +02:00
void Nexus::Unify(float threshold) {
//TODO what if colors or normals or strips?
//TODO update totvert
unsigned int duplicated = 0;
for(unsigned int p = 0; p < index.size(); p++) {
Nexus::Entry &entry = index[p];
Patch patch = GetPatch(p);
unsigned int vcount = 0;
map<Point3f, unsigned short> vertices;
vector<unsigned short> remap;
remap.resize(patch.nv);
// map<unsigned short, unsigned short> remap;
for(unsigned int i = 0; i < patch.nv; i++) {
Point3f &point = patch.Vert(i);
if(!vertices.count(point)) {
vertices[point] = vcount++;
} else {
duplicated++;
}
remap[i] = vertices[point];
}
assert(vertices.size() <= patch.nv);
if(vertices.size() == patch.nv) //no need to unify
continue;
vector<Point3f> newvert;
newvert.resize(vertices.size());
map<Point3f, unsigned short>::iterator k;
for(k = vertices.begin(); k != vertices.end(); k++) {
newvert[(*k).second] = (*k).first;
}
vector<unsigned short> newface;
newface.resize(patch.nf * 3);
for(unsigned int f = 0; f < newface.size(); f++)
newface[f] = remap[patch.FaceBegin()[f]];
//rewrite patch now.
entry.nvert = newvert.size();
patch.Init(signature, entry.nvert, entry.nface);
memcpy(patch.VertBegin(), &(newvert[0]), entry.nvert*sizeof(Point3f));
memcpy(patch.FaceBegin(), &(newface[0]), entry.nface*3*sizeof(unsigned short));
//fix patch borders now
set<unsigned int> close; //bordering pathes
Border border = GetBorder(p);
for(unsigned int b = 0; b < border.Size(); b++) {
if(border[b].IsNull()) continue;
close.insert(border[b].end_patch);
border[b].start_vert = remap[border[b].start_vert];
}
set<unsigned int>::iterator c;
for(c = close.begin(); c != close.end(); c++) {
Border bord = GetBorder(*c);
for(unsigned int b = 0; b < bord.Size(); b++) {
if(bord[b].IsNull()) continue;
if(bord[b].end_patch == p) {
bord[b].end_vert = remap[bord[b].end_vert];
}
}
}
}
//finally: there may be duplicated borders
for(unsigned int p = 0; p < index.size(); p++) {
Border border = GetBorder(p);
//Nexus::Entry &entry = index[p];
set<Link> links;
for(unsigned int b = 0; b < border.Size(); b++) {
if(border[b].IsNull()) continue;
if(links.count(border[b]))
border[b] = Link();
else
links.insert(border[b]);
}
}
totvert -= duplicated;
// cout << "Found " << duplicated << " duplicated vertices" << endl;
}