vcglib/apps/nexus/nexus.cpp

396 lines
11 KiB
C++
Raw Normal View History

2004-09-17 17:25:59 +02:00
#include <assert.h>
2004-12-13 01:44:48 +01:00
#include <iostream>
#include <set>
2004-07-02 15:00:02 +02:00
#include "nexus.h"
using namespace std;
using namespace vcg;
using namespace nxs;
Nexus::~Nexus() {
Close();
}
2004-10-10 19:19:42 +02:00
bool Nexus::Create(const string &file, Signature sig, unsigned int c_size) {
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-10-10 19:19:42 +02:00
chunk_size = c_size;
2005-01-14 16:25:29 +01:00
unsigned int header_size = 256;
if(chunk_size > header_size) header_size = chunk_size;
history.Clear();
ram_used = 0;
ram_max = 50 * (1<<20) / chunk_size;
2004-10-08 16:46:26 +02:00
2005-01-14 16:25:29 +01:00
if(!IndexFile<Entry>::Create(file + ".nxp", header_size)) {
2004-07-02 15:00:02 +02:00
cerr << "Could not create file: " << file << ".nxp" << endl;
return false;
}
2005-01-14 16:25:29 +01:00
2004-10-08 16:46:26 +02:00
//Important: chunk_size must be 1 so that i can use Region in VFile.
2005-01-14 16:25:29 +01:00
if(!borders.Create(file + ".nxb")) {
2004-07-04 16:26:46 +02:00
cerr << "Could not create file: " << file << ".nxb" << endl;
2004-07-02 15:00:02 +02:00
return false;
}
return true;
}
2004-12-13 01:44:48 +01:00
bool Nexus::Load(const string &file, bool rdonly) {
2005-01-14 16:25:29 +01:00
if(!IndexFile<Entry>::Load(file + ".nxp", rdonly)) return false;
ram_used = 0;
ram_max = 50 * (1<<20) / chunk_size;
2004-12-13 01:44:48 +01:00
2005-01-14 16:25:29 +01:00
history.Clear();
SetPosition(history_offset);
unsigned int history_size;
ReadBuffer(&history_size, sizeof(unsigned int));
2004-12-13 01:44:48 +01:00
2005-01-14 16:25:29 +01:00
char *buffer = new char[history_size];
ReadBuffer(buffer, history_size);
if(!history.Load(history_size, buffer)) {
cerr << "Error loading history\n";
2004-12-13 01:44:48 +01:00
return false;
2005-01-14 16:25:29 +01:00
}
borders.Load(file + ".nxb", rdonly);
//TODO on nxsbuilder assure borders are loaded
2004-07-02 15:00:02 +02:00
return true;
}
2005-01-14 16:25:29 +01:00
void Nexus::Close() {
if(!Opened()) return;
Flush();
if(!IsReadOnly()) {
//set history_offset
if(!size()) history_offset = 0;
else
history_offset = (back().patch_start + back().disk_size);
history_offset *= chunk_size;
unsigned int history_size;
char *mem = history.Save(history_size);
Redim(history_offset + history_size + sizeof(unsigned int));
SetPosition(history_offset);
WriteBuffer(&history_size, sizeof(unsigned int));
WriteBuffer(mem, history_size);
delete []mem;
}
2004-10-10 19:19:42 +02:00
borders.Close();
2005-01-14 16:25:29 +01:00
IndexFile<Entry>::Close();
}
void Nexus::SaveHeader() {
unsigned int magic = 0x3053584e; // nxs0
WriteBuffer(&magic, sizeof(unsigned int));
WriteBuffer(&signature, sizeof(unsigned int));
WriteBuffer(&chunk_size, sizeof(unsigned int));
WriteBuffer(&offset, sizeof(int64));
WriteBuffer(&history_offset, sizeof(int64));
WriteBuffer(&totvert, sizeof(unsigned int));
WriteBuffer(&totface, sizeof(unsigned int));
WriteBuffer(&sphere, sizeof(Sphere3f));
}
bool Nexus::LoadHeader() {
unsigned int magic;
ReadBuffer(&magic, sizeof(unsigned int));
if(magic != 0x3053584e) {
cerr << "Invalid magic. Not a nxs file\n";
return false;
}
ReadBuffer(&signature, sizeof(unsigned int));
ReadBuffer(&chunk_size, sizeof(unsigned int));
ReadBuffer(&offset, sizeof(int64));
ReadBuffer(&history_offset, sizeof(int64));
ReadBuffer(&totvert, sizeof(unsigned int));
ReadBuffer(&totface, sizeof(unsigned int));
ReadBuffer(&sphere, sizeof(Sphere3f));
}
void Nexus::Flush(bool all) {
if(all) {
std::map<unsigned int, list<unsigned int>::iterator>::iterator i;
for(i = index.begin(); i != index.end(); i++) {
unsigned int patch = (*i).first;
FlushPatch(patch);
}
pqueue.clear();
index.clear();
} else {
while(ram_used > ram_max) {
unsigned int to_flush = pqueue.back();
pqueue.pop_back();
index.erase(to_flush);
FlushPatch(to_flush);
}
2004-09-16 16:25:16 +02:00
}
2004-07-02 15:00:02 +02:00
}
2005-01-14 16:25:29 +01:00
Patch &Nexus::GetPatch(unsigned int patch, bool flush) {
Entry &entry = operator[](patch);
if(index.count(patch)) {
assert(entry.patch);
list<unsigned int>::iterator &i = index[patch];
pqueue.erase(i);
pqueue.push_front(patch);
} else {
while(flush && ram_used > ram_max) {
unsigned int to_flush = pqueue.back();
pqueue.pop_back();
index.erase(to_flush);
FlushPatch(to_flush);
}
assert(!entry.patch);
entry.patch = LoadPatch(patch);
pqueue.push_front(patch);
list<unsigned int>::iterator i = pqueue.begin();
index[patch] = i;
}
return *(entry.patch);
2004-07-04 16:26:46 +02:00
}
2004-10-01 18:54:57 +02:00
Border Nexus::GetBorder(unsigned int patch, bool flush) {
2004-10-08 16:46:26 +02:00
return borders.GetBorder(patch);
2004-07-04 16:26:46 +02:00
}
2005-01-14 16:25:29 +01:00
/*void Nexus::AddBorder(unsigned int patch, Link &link) {
2004-11-18 19:30:15 +01:00
Border border = GetBorder(patch);
unsigned int pos = border.Size();
2004-11-28 02:23:26 +01:00
if(pos > 65500) {
cerr << "Exceding border size!!!\n";
exit(0);
}
2004-11-18 19:30:15 +01:00
if(borders.ResizeBorder(patch, pos+1)) {
border = GetBorder(patch);
}
assert(border.Available() > pos);
border[pos] = link;
2005-01-14 16:25:29 +01:00
}*/
2004-07-04 16:26:46 +02:00
unsigned int Nexus::AddPatch(unsigned int nvert, unsigned int nface,
unsigned int nbord) {
2004-10-08 16:46:26 +02:00
2005-01-14 16:25:29 +01:00
Entry entry;
entry.patch_start = 0xffffffff;
entry.ram_size = Patch::ChunkSize(signature, nvert, nface, chunk_size);
entry.disk_size = 0xffff;
entry.nvert = nvert;
entry.nface = nface;
entry.error = 0;
//sphere undefined.
entry.patch = NULL;
entry.vbo_array = 0;
entry.vbo_element = 0;
push_back(entry);
2004-07-04 16:26:46 +02:00
2004-10-08 16:46:26 +02:00
borders.AddBorder(nbord);
2004-08-27 02:38:34 +02:00
totvert += nvert;
totface += nface;
2005-01-14 16:25:29 +01:00
return size() - 1;
2004-10-15 18:45:27 +02:00
}
2004-10-08 16:46:26 +02:00
/*void Nexus::Unify(float threshold) {
2004-09-17 17:25:59 +02:00
//TODO what if colors or normals or strips?
unsigned int duplicated = 0;
2004-10-06 18:40:47 +02:00
unsigned int degenerate = 0;
2005-01-14 16:25:29 +01:00
for(unsigned int p = 0; p < size(); p++) {
Entry &entry = operator[](p);
Patch &patch = GetPatch(p);
2004-09-17 17:25:59 +02:00
unsigned int vcount = 0;
map<Point3f, unsigned short> vertices;
2004-10-06 18:40:47 +02:00
2004-09-17 17:25:59 +02:00
vector<unsigned short> remap;
remap.resize(patch.nv);
2004-10-06 18:40:47 +02:00
2004-09-17 17:25:59 +02:00
for(unsigned int i = 0; i < patch.nv; i++) {
Point3f &point = patch.Vert(i);
2004-10-06 18:40:47 +02:00
if(!vertices.count(point))
2004-09-17 17:25:59 +02:00
vertices[point] = vcount++;
2004-10-06 18:40:47 +02:00
else
2004-09-17 17:25:59 +02:00
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;
2004-10-06 18:40:47 +02:00
//check no degenerate faces get created.
for(unsigned int f = 0; f < entry.nface; f++) {
unsigned short *face = patch.Face(f);
if(face[0] != face[1] && face[1] != face[2] && face[0] != face[2] &&
2004-10-09 19:37:45 +02:00
newvert[remap[face[0]]] != newvert[remap[face[1]]] &&
newvert[remap[face[0]]] != newvert[remap[face[2]]] &&
newvert[remap[face[1]]] != newvert[remap[face[2]]]) {
newface.push_back(remap[face[0]]);
newface.push_back(remap[face[1]]);
newface.push_back(remap[face[2]]);
2004-10-06 18:40:47 +02:00
} else {
2004-10-09 19:37:45 +02:00
degenerate++;
2004-10-06 18:40:47 +02:00
}
}
2004-09-17 17:25:59 +02:00
//rewrite patch now.
entry.nvert = newvert.size();
2004-10-06 18:40:47 +02:00
entry.nface = newface.size()/3;
2004-09-17 17:25:59 +02:00
patch.Init(signature, entry.nvert, entry.nface);
memcpy(patch.VertBegin(), &(newvert[0]), entry.nvert*sizeof(Point3f));
2004-10-06 18:40:47 +02:00
memcpy(patch.FaceBegin(), &(newface[0]), entry.nface*3*sizeof(short));
2004-10-09 19:37:45 +02:00
//testiamo il tutto... TODO remove this of course
for(unsigned int i =0; i < patch.nf; i++) {
for(int k =0 ; k < 3; k++)
if(patch.Face(i)[k] >= patch.nv) {
cerr <<" Unify has problems\n";
exit(0);
}
}
2004-10-06 18:40:47 +02:00
2004-09-17 17:25:59 +02:00
//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];
}
}
}
}
2004-12-03 02:20:56 +01:00
//better to compact directly borders than setting them null.
2004-09-17 17:25:59 +02:00
//finally: there may be duplicated borders
2005-01-14 16:25:29 +01:00
for(unsigned int p = 0; p < size(); p++) {
2004-09-17 17:25:59 +02:00
Border border = GetBorder(p);
set<Link> links;
for(unsigned int b = 0; b < border.Size(); b++) {
2004-12-03 02:20:56 +01:00
Link &link = border[b];
assert(!link.IsNull());
//if(border[b].IsNull()) continue;
links.insert(link);
2004-09-17 17:25:59 +02:00
}
2004-12-03 02:20:56 +01:00
int count = 0;
for(set<Link>::iterator k = links.begin(); k != links.end(); k++)
border[count++] = *k;
2005-01-14 16:25:29 +01:00
borders[p].used = links.size();
2004-09-17 17:25:59 +02:00
}
2004-10-06 18:40:47 +02:00
2004-09-17 17:25:59 +02:00
totvert -= duplicated;
2004-10-06 18:40:47 +02:00
if(duplicated)
cerr << "Found " << duplicated << " duplicated vertices" << endl;
if(degenerate)
cerr << "Found " << degenerate << " degenerate face while unmifying\n";
}*/
2005-01-14 16:25:29 +01:00
Patch *Nexus::LoadPatch(unsigned int idx) {
assert(idx < size());
Entry &entry = operator[](idx);
if(entry.patch) return entry.patch;
char *ram = new char[entry.ram_size * chunk_size];
#ifndef NDEBUG
if(!ram) {
cerr << "COuld not allocate ram!\n";
exit(0);
}
#endif
Patch *patch = new Patch(signature, ram, entry.nvert, entry.nface);
if(entry.patch_start != 0xffffffff) { //was allocated.
assert(entry.disk_size != 0xffff);
MFile::SetPosition((int64)entry.patch_start * (int64)chunk_size);
if((signature & NXS_COMPRESSED) == 0) { //not compressed
MFile::ReadBuffer(ram, entry.disk_size * chunk_size);
} else {
unsigned char *disk = new unsigned char[entry.disk_size * chunk_size];
MFile::ReadBuffer(disk, entry.disk_size * chunk_size);
patch->Decompress(entry.ram_size * chunk_size,
disk, entry.disk_size * chunk_size);
delete []disk;
}
}
ram_used += entry.ram_size;
entry.patch = patch;
return patch;
}
void Nexus::FlushPatch(unsigned int id) {
Entry &entry = operator[](id);
assert(entry.patch);
if(!MFile::IsReadOnly()) { //write back patch
if((signature & NXS_COMPRESSED)) {
unsigned int compressed_size;
char *compressed = entry.patch->Compress(entry.ram_size * chunk_size,
compressed_size);
if(entry.disk_size == 0xffff) {//allocate space
assert(entry.patch_start == 0xffffffff);
entry.disk_size = (unsigned int)((compressed_size-1)/chunk_size) + 1;
entry.patch_start = (unsigned int)(Length()/chunk_size);
Redim(Length() + entry.disk_size * chunk_size);
} else {
//cerr << "OOOOPSPPPS not supported!" << endl;
exit(-1);
}
MFile::SetPosition((int64)entry.patch_start * (int64)chunk_size);
MFile::WriteBuffer(compressed, entry.disk_size * chunk_size);
delete []compressed;
} else {
if(entry.disk_size == 0xffff) {
entry.disk_size = entry.ram_size;
entry.patch_start = (unsigned int)(Length()/chunk_size);
Redim(Length() + entry.disk_size * chunk_size);
}
MFile::SetPosition((int64)entry.patch_start * (int64)chunk_size);
MFile::WriteBuffer(entry.patch->start, entry.disk_size * chunk_size);
}
}
delete [](entry.patch->start);
delete entry.patch;
entry.patch = NULL;
ram_used -= entry.ram_size;
}