Daily backup. Preparing for compression.

This commit is contained in:
Federico Ponchio 2004-10-04 16:49:54 +00:00
parent 2c7e862e83
commit da80ed5243
13 changed files with 326 additions and 150 deletions

View File

@ -17,6 +17,7 @@
//#include "border.h" //#include "border.h"
#include "decimate.h" #include "decimate.h"
//#include <wrap/io_trimesh/export_ply.h>
using namespace vcg; using namespace vcg;
using namespace tri; using namespace tri;
@ -57,6 +58,13 @@ float nxs::Decimate(Decimation mode,
vector<unsigned int> &newface, vector<unsigned int> &newface,
vector<Link> &newbord, vector<Link> &newbord,
vector<int> &vert_remap) { vector<int> &vert_remap) {
//Temporary test:
for(unsigned int i = 0; i < newface.size(); i+= 3) {
assert(newface[i*3] != newface[i*3+1]);
assert(newface[i*3] != newface[i*3+2]);
assert(newface[i*3+1] != newface[i*3+2]);
}
MyMesh mesh; MyMesh mesh;
@ -89,6 +97,9 @@ float nxs::Decimate(Decimation mode,
// if(FinalSize > target_faces) FinalSize = target_faces; // if(FinalSize > target_faces) FinalSize = target_faces;
/* if(target_faces == 2404) {
vcg::tri::io::ExporterPLY<MyMesh>::Save(mesh, "bum");
}*/
printf("mesh loaded %d %d \n",mesh.vn,mesh.fn); printf("mesh loaded %d %d \n",mesh.vn,mesh.fn);
printf("reducing it to %i\n", target_faces); printf("reducing it to %i\n", target_faces);
@ -158,6 +169,14 @@ float nxs::Decimate(Decimation mode,
assert(vert_remap[v] != -1); assert(vert_remap[v] != -1);
v = vert_remap[v]; v = vert_remap[v];
} }
//Temporary test again:
for(unsigned int i = 0; i < newface.size(); i+= 3) {
assert(newface[i*3] != newface[i*3+1]);
assert(newface[i*3] != newface[i*3+2]);
assert(newface[i*3+1] != newface[i*3+2]);
}
return error; return error;
} }

112
apps/nexus/file.cpp Normal file
View File

@ -0,0 +1,112 @@
#include "file.h"
#include <assert.h>
using namespace std;
using namespace nxs;
bool File::Create(const string &filename) {
size = 0;
#ifdef WIN32
fp = CreateFile(filename.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
NULL, CREATE_ALWAYS, 0, NULL);
if(fp == INVALID_HANDLE_VALUE) return false;
#else
fp = fopen(filename.c_str(), "wb+");
if(!fp) return false;
#endif
return true;
}
bool File::Load(const string &filename) {
#ifdef WIN32
fp = CreateFile(filename.c_str(), GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
if(fp == INVALID_HANDLE_VALUE) return false;
#else
fp = fopen(filename.c_str(), "rb+");
if(!fp) return false;
#endif
#ifdef WIN32
size = GetFileSize(fp, NULL);
#else
//TODO use stat()
fseek(fp, 0, SEEK_END);
size = ftell(fp);
#endif
return true;
}
void File::Close() {
if(fp) {
#ifdef WIN32
CloseHandle(fp);
#else
fclose(fp);
#endif
fp = NULL;
}
}
void File::Resize(unsigned int elem) {
assert(fp);
if(elem > size) {
#ifdef WIN32
if(INVALID_SET_FILE_POINTER ==
SetFilePointer(fp, elem - 1, 0, FILE_BEGIN))
#else
if(-1 == fseek(fp, elem - 1, SEEK_SET))
#endif
assert(0 && "Could not resize");
unsigned char a;
#ifdef WIN32
DWORD tmp;
WriteFile(fp, &a, 1, &tmp, NULL);
#else
fwrite(&a, sizeof(unsigned char), 1, fp);
#endif
} else {
//TODO optimize: we do not need flush for buffers over elem.
#ifndef WIN32
int fd = fileno(fp);
ftruncate(fd, elem);
#else
SetFilePointer(fp, elem, 0, FILE_BEGIN);
SetEndOfFile(fp);
#endif
}
size = elem;
}
void File::SetPosition(unsigned int pos) {
#ifdef WIN32
SetFilePointer(fp, pos, 0, FILE_BEGIN);
#else
fseek(fp, pos, SEEK_SET);
#endif
}
void File::ReadBuffer(void *data, unsigned int sz) {
#ifdef WIN32
DWORD tmp;
ReadFile(fp, data, sz, &tmp, NULL);
if(tmp != sz)
assert(0 && "Could not read");
#else
if(sz != fread(data, 1, sz, fp))
assert(0 && "Could not read");
#endif
}
void File::WriteBuffer(void *data, unsigned int sz) {
#ifdef WIN32
DWORD tmp;
WriteFile(fp, data, sz, &tmp, NULL);
assert(tmp == sz);
#else
if(sz != fwrite(data, 1, sz, fp))
assert(0 && "Could not write");
#endif
}

45
apps/nexus/file.h Normal file
View File

@ -0,0 +1,45 @@
#ifndef NXS_FILE_H
#define NXS_FILE_H
//TODO move includes in cpp
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <stdio.h>
#include <string>
namespace nxs {
class File {
public:
File(): fp(NULL) {}
~File() { Close(); }
bool Create(const std::string &filename);
bool Load(const std::string &filename);
void Close();
void Resize(unsigned int elem);
void SetPosition(unsigned int chunk);
void ReadBuffer(void *data, unsigned int size);
void WriteBuffer(void *data, unsigned int size);
protected:
#ifdef WIN32
HANDLE fp;
#else
FILE *fp;
#endif
unsigned int size;
};
}
#endif

View File

@ -126,7 +126,7 @@ void Nexus::Close() {
Patch Nexus::GetPatch(unsigned int patch, bool flush) { Patch Nexus::GetPatch(unsigned int patch, bool flush) {
Entry &entry = index[patch]; Entry &entry = index[patch];
Chunk *start = patches.GetRegion(entry.patch_start, entry.patch_size,flush); Chunk *start = patches.GetRegion(entry.patch_start, entry.patch_used,flush);
return Patch(signature, start, entry.nvert, entry.nface); return Patch(signature, start, entry.nvert, entry.nface);
} }
@ -142,6 +142,7 @@ unsigned int Nexus::AddPatch(unsigned int nvert, unsigned int nface,
Entry entry; Entry entry;
entry.patch_start = patches.Size(); entry.patch_start = patches.Size();
entry.patch_size = Patch::ChunkSize(signature, nvert, nface); entry.patch_size = Patch::ChunkSize(signature, nvert, nface);
entry.patch_used = entry.patch_size;
entry.border_start = borders.Size(); entry.border_start = borders.Size();
entry.border_size = nbord; entry.border_size = nbord;
entry.border_used = 0; entry.border_used = 0;
@ -236,6 +237,13 @@ void Nexus::Join(const std::set<unsigned int> &patches,
for(int k = 0; k < 3; k++) { for(int k = 0; k < 3; k++) {
newface[3*fcount + k] = vmap[patch.Face(i)[k]]; newface[3*fcount + k] = vmap[patch.Face(i)[k]];
} }
assert(patch.Face(i)[0] != patch.Face(i)[1]);
assert(patch.Face(i)[0] != patch.Face(i)[2]);
assert(patch.Face(i)[1] != patch.Face(i)[2]);
assert(newface[3*fcount + 0] != newface[3*fcount + 1]);
assert(newface[3*fcount + 0] != newface[3*fcount + 2]);
assert(newface[3*fcount + 1] != newface[3*fcount + 2]);
fcount++; fcount++;
assert(fcount *3 <= newface.size()); assert(fcount *3 <= newface.size());
} }

View File

@ -24,7 +24,8 @@ class Nexus {
unsigned int patch_start; //granularita' Chunk unsigned int patch_start; //granularita' Chunk
unsigned int border_start; //granuralita' Link unsigned int border_start; //granuralita' Link
unsigned short patch_size; //in cuhnks unsigned short patch_size; //in chunks
unsigned short patch_used; // in chunks (if compressed is < patch_size)
unsigned short border_size; //in Links unsigned short border_size; //in Links
unsigned short border_used; //in Links unsigned short border_used; //in Links

View File

@ -33,12 +33,18 @@ bool FrustumPolicy::Expand(unsigned int patch, Nexus::Entry &entry) {
} }
NexusMt::NexusMt(): vbo(VBO_AUTO), vbo_size(0), NexusMt::NexusMt(): vbo(VBO_AUTO), vbo_size(0), ram_size(128000000),
policy(NULL), error(4), realtime(true), policy(NULL), error(4), realtime(true),
mode(SMOOTH) { mode(SMOOTH) {
policy = new FrustumPolicy(); policy = new FrustumPolicy();
} }
NexusMt::~NexusMt() {
for(unsigned int i = 0; i < ram_buffer.size(); i++)
if(ram_buffer[i].patch)
delete ram_buffer[i].patch;
}
bool NexusMt::Load(const string &filename) { bool NexusMt::Load(const string &filename) {
if(!Nexus::Load(filename)) return false; if(!Nexus::Load(filename)) return false;
LoadHistory(); LoadHistory();
@ -53,6 +59,9 @@ bool NexusMt::Load(const string &filename) {
SetComponent(TEXTURE, true); SetComponent(TEXTURE, true);
SetComponent(DATA, true); SetComponent(DATA, true);
frame = 0;
ram_used = 0;
ram_buffer.resize(index.size());
return true; return true;
} }
@ -88,10 +97,11 @@ void NexusMt::Render() {
Nexus::Entry &entry = index[cell]; Nexus::Entry &entry = index[cell];
//frustum culling //frustum culling
// if(frustum.Outside(entry.sphere.center, entry.sphere.radius)) if(frustum.IsOutside(entry.sphere.Center(), entry.sphere.Radius()))
// continue; continue;
Patch &patch = LoadPatch(cell);
Patch patch = GetPatch(cell);
glVertexPointer(3, GL_FLOAT, 0, patch.VertBegin()); glVertexPointer(3, GL_FLOAT, 0, patch.VertBegin());
if(use_colors) if(use_colors)
glColorPointer(4, GL_UNSIGNED_BYTE, 0, patch.ColorBegin()); glColorPointer(4, GL_UNSIGNED_BYTE, 0, patch.ColorBegin());
@ -138,11 +148,13 @@ void NexusMt::SetPolicy(PolicyKind kind, float _error, bool _realtime) {
realtime = _realtime; realtime = _realtime;
} }
void NexusMt::SetVbo(Vbo _vbo, unsigned int _vbo_size) { void NexusMt::SetVbo(Vbo _vbo, unsigned int _vbo_size,
unsigned int _ram_size) {
vbo = _vbo; vbo = _vbo;
if(!GLEW_ARB_vertex_buffer_object) if(!GLEW_ARB_vertex_buffer_object)
vbo = VBO_OFF; vbo = VBO_OFF;
vbo_size = _vbo_size; vbo_size = _vbo_size;
ram_size = _ram_size;
} }
bool NexusMt::SetMode(Mode _mode) { bool NexusMt::SetMode(Mode _mode) {
@ -331,3 +343,29 @@ void NexusMt::Select(vector<unsigned int> &selected) {
} }
} }
} }
Patch &NexusMt::LoadPatch(unsigned int p) {
Sgurz &sgurz = ram_buffer[p];
if(sgurz.patch) {
sgurz.last_frame = frame;
return *(sgurz.patch);
}
Entry &entry = index[p];
Chunk *start = new Chunk[entry.patch_size];
ram_used += entry.patch_size * sizeof(Chunk);
patches.SetPosition(entry.patch_start * sizeof(Chunk));
patches.ReadBuffer(start, entry.patch_used * sizeof(Chunk));
Patch *patch = new Patch(signature, start, entry.nvert, entry.nface);
sgurz.patch = patch;
if(ram_used > ram_size * 1.5)
FlushRam();
cerr << "Ram: " << ram_used << endl;
return *(sgurz.patch);
}
void NexusMt::FlushRam() {
//use drame info and error to prune
}

View File

@ -67,6 +67,7 @@ class NexusMt: public Nexus {
Vbo vbo; Vbo vbo;
unsigned int vbo_size; unsigned int vbo_size;
unsigned int ram_size;
Policy *policy; Policy *policy;
float error; float error;
@ -81,6 +82,7 @@ class NexusMt: public Nexus {
bool use_data; bool use_data;
NexusMt(); NexusMt();
~NexusMt();
bool Load(const std::string &filename); bool Load(const std::string &filename);
bool InitGL(); bool InitGL();
@ -88,7 +90,8 @@ class NexusMt: public Nexus {
void Render(); void Render();
void SetPolicy(Policy *policy, bool realtime = true); void SetPolicy(Policy *policy, bool realtime = true);
void SetPolicy(PolicyKind kind, float error, bool realtime = true); void SetPolicy(PolicyKind kind, float error, bool realtime = true);
void SetVbo(Vbo mode, unsigned int vbo_size = 0); void SetVbo(Vbo mode, unsigned int vbo_size = 0,
unsigned int ram_size = 128000000);
bool SetMode(Mode mode); bool SetMode(Mode mode);
bool SetComponent(Component c, bool on); bool SetComponent(Component c, bool on);
bool SetComponents(unsigned int mask); bool SetComponents(unsigned int mask);
@ -100,6 +103,23 @@ class NexusMt: public Nexus {
void LoadHistory(); void LoadHistory();
void ClearHistory(); void ClearHistory();
void Select(std::vector<unsigned int> &selected); void Select(std::vector<unsigned int> &selected);
Patch &LoadPatch(unsigned int p);
void FlushRam();
unsigned int frame;
unsigned int ram_used;
struct Sgurz {
Sgurz(Patch *_patch = NULL, unsigned int _vbo = 0,
unsigned int _vio = 0, unsigned int _last_frame = 0):
patch(_patch), vbo(_vbo), vio(_vio), last_frame(_last_frame) {}
Patch *patch;
unsigned int vbo; //vertex buffer
unsigned int vio; //index buffer
unsigned int last_frame;
};
std::vector<Sgurz> ram_buffer;
}; };
} }

View File

@ -24,6 +24,9 @@
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.10 2004/10/01 16:54:57 ponchio
Daily backup.
Revision 1.9 2004/09/30 23:56:33 ponchio Revision 1.9 2004/09/30 23:56:33 ponchio
Backup (added strips and normals) Backup (added strips and normals)
@ -199,6 +202,9 @@ int main(int argc, char *argv[]) {
case SDL_QUIT: quit = 1; break; case SDL_QUIT: quit = 1; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
switch(event.key.keysym.sym) { switch(event.key.keysym.sym) {
case SDLK_RCTRL:
case SDLK_LCTRL:
track.ButtonDown(Trackball::KEY_CTRL); break;
case SDLK_q: exit(0); break; case SDLK_q: exit(0); break;
case SDLK_b: show_borders = !show_borders; break; case SDLK_b: show_borders = !show_borders; break;
case SDLK_c: show_colors = !show_colors; break; case SDLK_c: show_colors = !show_colors; break;
@ -220,20 +226,32 @@ int main(int argc, char *argv[]) {
cerr << "error: " << error << endl; break; cerr << "error: " << error << endl; break;
} }
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_KEYUP:
switch(event.key.keysym.sym) {
case SDLK_RCTRL:
case SDLK_LCTRL:
track.ButtonUp(Trackball::KEY_CTRL); break;
}
break;
case SDL_MOUSEBUTTONDOWN:
x = event.button.x; x = event.button.x;
y = height - event.button.y; y = height - event.button.y;
if(event.button.button == SDL_BUTTON_WHEELUP) { if(event.button.button == SDL_BUTTON_WHEELUP)
track.MouseWheel(1); track.MouseWheel(1);
} else if(event.button.button == SDL_BUTTON_WHEELDOWN) { else if(event.button.button == SDL_BUTTON_WHEELDOWN)
track.MouseWheel(-1); track.MouseWheel(-1);
} else else if(event.button.button == SDL_BUTTON_LEFT)
track.MouseDown(x, y, 1); track.MouseDown(x, y, Trackball::BUTTON_LEFT);
else if(event.button.button == SDL_BUTTON_RIGHT)
track.MouseDown(x, y, Trackball::BUTTON_RIGHT);
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
x = event.button.x; x = event.button.x;
y = height - event.button.y; y = height - event.button.y;
track.MouseUp(x, y, 1); if(event.button.button == SDL_BUTTON_LEFT)
track.MouseUp(x, y, Trackball::BUTTON_LEFT);
else if(event.button.button == SDL_BUTTON_RIGHT)
track.MouseUp(x, y, Trackball::BUTTON_RIGHT);
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
while(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEMOTIONMASK)); while(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEMOTIONMASK));
@ -276,7 +294,10 @@ int main(int argc, char *argv[]) {
Point3f center = sphere.Center(); Point3f center = sphere.Center();
glTranslatef(-center[0], -center[1], -center[2]); glTranslatef(-center[0], -center[1], -center[2]);
glColor3f(0.9, 0.9, 0.9); Point3f &p = nexus.sphere.Center();
float r = nexus.sphere.Radius();
glColor3f(0.8, 0.8, 0.8);
nexus.SetMode(mode); nexus.SetMode(mode);
nexus.SetPolicy(policy, error); nexus.SetPolicy(policy, error);
nexus.SetComponent(NexusMt::COLOR, show_colors); nexus.SetComponent(NexusMt::COLOR, show_colors);

View File

@ -45,7 +45,7 @@ void nxs::NexusAllocate(Crude &crude,
entry.patch_start = totchunks; entry.patch_start = totchunks;
entry.patch_size = Patch::ChunkSize(nexus.signature, entry.patch_size = Patch::ChunkSize(nexus.signature,
patch_verts[i], patch_faces[i]); patch_verts[i], patch_faces[i]);
entry.patch_used = entry.patch_size;
totchunks += entry.patch_size; totchunks += entry.patch_size;
entry.border_start = 0xffffffff; entry.border_start = 0xffffffff;
entry.nvert = patch_verts[i]; entry.nvert = patch_verts[i];
@ -69,6 +69,12 @@ void nxs::NexusAllocate(Crude &crude,
Patch patch = nexus.GetPatch(npatch); Patch patch = nexus.GetPatch(npatch);
Crude::Face *faces = (Crude::Face *)patch.start; 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;
}
faces[entry.nface] = face; faces[entry.nface] = face;
entry.nface++; entry.nface++;
} }
@ -92,6 +98,7 @@ void nxs::NexusFill(Crude &crude,
//make a copy of faces (we need to write there :P) //make a copy of faces (we need to write there :P)
Crude::Face *faces = new Crude::Face[patch.nf]; Crude::Face *faces = new Crude::Face[patch.nf];
//Test for degenerate faces?
memcpy(faces, (Crude::Face *)patch.start, memcpy(faces, (Crude::Face *)patch.start,
patch.nf * sizeof(Crude::Face)); patch.nf * sizeof(Crude::Face));
@ -112,6 +119,10 @@ void nxs::NexusFill(Crude &crude,
} }
patch.FaceBegin()[k*3 + j] = remap[face[j]]; patch.FaceBegin()[k*3 + j] = remap[face[j]];
} }
//test for degenerate faces.
assert(patch.FaceBegin()[k*3] != patch.FaceBegin()[k*3+1]);
assert(patch.FaceBegin()[k*3] != patch.FaceBegin()[k*3+2]);
assert(patch.FaceBegin()[k*3+1] != patch.FaceBegin()[k*3+2]);
} }
assert(count == remap.size()); assert(count == remap.size());
assert(entry.nvert == remap.size()); assert(entry.nvert == remap.size());

View File

@ -24,6 +24,9 @@
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.3 2004/07/15 14:32:49 ponchio
Debug.
Revision 1.2 2004/07/05 15:49:39 ponchio Revision 1.2 2004/07/05 15:49:39 ponchio
Windows (DevCpp, mingw) port. Windows (DevCpp, mingw) port.
@ -78,7 +81,8 @@ int main(int argc, char *argv[]) {
} }
string output = argv[argc-1]; string output = argv[argc-1];
//test last one is not a ply //test last one is not a ply
if(output.substr(output.size()-4, output.size()) == ".ply") { if(output.size() > 4 &&
output.substr(output.size()-4, output.size()) == ".ply") {
cerr << "Last argument is output (so not a .ply)\n"; cerr << "Last argument is output (so not a .ply)\n";
return -1; return -1;
} }

View File

@ -24,6 +24,9 @@
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.11 2004/10/01 16:00:12 ponchio
Added include <assert.h>
Revision 1.10 2004/09/30 23:56:33 ponchio Revision 1.10 2004/09/30 23:56:33 ponchio
Backup (added strips and normals) Backup (added strips and normals)
@ -69,15 +72,9 @@ Created
#ifndef VFILE_H #ifndef VFILE_H
#define VFILE_H #define VFILE_H
#ifndef WIN32 #include "file.h"
#include <unistd.h>
#else
#include <windows.h>
#endif
#include <assert.h> #include <assert.h>
#include <errno.h>
//#include <hash_map>
#include <map> #include <map>
#include <list> #include <list>
#include <string> #include <string>
@ -91,7 +88,7 @@ Created
namespace nxs { namespace nxs {
template <class T> class VFile { template <class T> class VFile: public File {
public: public:
struct Buffer { struct Buffer {
@ -100,13 +97,8 @@ template <class T> class VFile {
T *data; T *data;
}; };
private: protected:
#ifdef WIN32 unsigned int n_elements;
HANDLE fp;
#else
FILE *fp;
#endif
std::list<Buffer> buffers; std::list<Buffer> buffers;
typedef typename std::list<Buffer>::iterator list_iterator; typedef typename std::list<Buffer>::iterator list_iterator;
@ -115,7 +107,6 @@ template <class T> class VFile {
unsigned int chunk_size; //default buffer size (expressed in number of T) unsigned int chunk_size; //default buffer size (expressed in number of T)
unsigned int queue_size; unsigned int queue_size;
unsigned int n_elements; //size of the vector
public: public:
class iterator { class iterator {
@ -129,29 +120,19 @@ template <class T> class VFile {
VFile *buffer; VFile *buffer;
}; };
VFile(): fp(NULL), last_buffer(NULL) {} VFile(): last_buffer(NULL) {}
~VFile() { Close(); } ~VFile() { Close(); }
bool Create(const std::string &filename, bool Create(const std::string &filename,
unsigned int _chunk_size = 4096/sizeof(T), unsigned int _chunk_size = 4096/sizeof(T),
unsigned int _queue_size = 1000) { unsigned int _queue_size = 1000) {
assert(_chunk_size > 0); assert(_chunk_size > 0);
n_elements = 0;
last_buffer = NULL; last_buffer = NULL;
chunk_size = _chunk_size; chunk_size = _chunk_size;
queue_size = _queue_size; queue_size = _queue_size;
n_elements = 0;
#ifdef WIN32 return File::Create(filename);
fp = CreateFile(filename.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
0, NULL);
if(fp == INVALID_HANDLE_VALUE) return false;
#else
fp = fopen(filename.c_str(), "wb+");
if(!fp) return false;
#endif
return true;
} }
bool Load(const std:: string &filename, bool Load(const std:: string &filename,
@ -162,35 +143,14 @@ template <class T> class VFile {
last_buffer = NULL; last_buffer = NULL;
chunk_size = _chunk_size; chunk_size = _chunk_size;
queue_size = _queue_size; queue_size = _queue_size;
#ifdef WIN32
fp = CreateFile(filename.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
0, NULL);
if(fp == INVALID_HANDLE_VALUE) return false;
#else
fp = fopen(filename.c_str(), "rb+");
if(!fp) return false;
#endif
if(!File::Load(filename)) return false;
#ifdef WIN32 n_elements = size/sizeof(T);
n_elements = GetFileSize(fp, NULL)/ sizeof(T);
#else
fseek(fp, 0, SEEK_END);
n_elements = ftell(fp)/ sizeof(T);
#endif
return true; return true;
} }
void Close() { void Close() {
if(fp) {
Flush(); Flush();
#ifdef WIN32
CloseHandle(fp);
#else
fclose(fp);
#endif
fp = NULL;
}
} }
void Flush() { void Flush() {
@ -203,51 +163,14 @@ template <class T> class VFile {
} }
void FlushBuffer(Buffer buffer) { void FlushBuffer(Buffer buffer) {
SetPosition(buffer.key); SetPosition(buffer.key * chunk_size * sizeof(T));
WriteBuffer(buffer.data, buffer.size); WriteBuffer((char *)(buffer.data), buffer.size * sizeof(T));
/*#ifdef WIN32
SetFilePointer(fp, buffer.key * chunk_size * sizeof(T), FILE_BEGIN);
unsigned int tmp;
WriteFile(fp, buffer.data, sizeof(T) * buffer.size, &tmp, NULL);
if(tmp != sizeof(T) * buffer.size)
assert(0 && "Could not write");
#else
fseek(fp, buffer.key * chunk_size * sizeof(T), SEEK_SET);
if(buffer.size != fwrite(buffer.data, sizeof(T), buffer.size, fp))
assert(0 && "Could not write");
#endif*/
delete []buffer.data; delete []buffer.data;
} }
void Resize(unsigned int elem) { void Resize(unsigned int elem) {
assert(fp);
Flush(); Flush();
if(elem > n_elements) { File::Resize(elem * sizeof(T));
#ifdef WIN32
if(INVALID_SET_FILE_POINTER == SetFilePointer(fp, elem*sizeof(T)-1, 0, FILE_BEGIN))
#else
if(-1 == fseek(fp, elem*sizeof(T) -1, SEEK_SET))
#endif
assert(0 && "Could not resize");
unsigned char a;
#ifdef WIN32
DWORD tmp;
WriteFile(fp, &a, 1, &tmp, NULL);
#else
fwrite(&a, sizeof(unsigned char), 1, fp);
#endif
} else {
//TODO optimize: we do not need flush for buffers over elem.
#ifndef WIN32
int fd = fileno(fp);
ftruncate(fd, elem*sizeof(T));
#else
SetFilePointer(fp, elem*sizeof(T), 0, FILE_BEGIN);
SetEndOfFile(fp);
#endif
}
n_elements = elem; n_elements = elem;
} }
@ -291,8 +214,8 @@ template <class T> class VFile {
index[buffer.key] = buffers.begin(); index[buffer.key] = buffers.begin();
last_buffer = &*buffers.begin(); last_buffer = &*buffers.begin();
SetPosition(chunk); SetPosition(chunk * chunk_size * sizeof(T));
ReadBuffer(buffer.data, buffer.size); ReadBuffer((char *)(buffer.data), buffer.size * sizeof(T));
return *(buffer.data + offset); return *(buffer.data + offset);
} }
@ -327,8 +250,8 @@ template <class T> class VFile {
buffers.push_front(buffer); buffers.push_front(buffer);
index[chunk] = buffers.begin(); index[chunk] = buffers.begin();
SetPosition(chunk); SetPosition(chunk * chunk_size * sizeof(T));
ReadBuffer(buffer.data, buffer.size); ReadBuffer((char *)(buffer.data), buffer.size * sizeof(T));
return buffer.data; return buffer.data;
} }
@ -342,37 +265,6 @@ template <class T> class VFile {
unsigned int QueueSize() { return queue_size; } unsigned int QueueSize() { return queue_size; }
iterator Begin() { return iterator(0, this); } iterator Begin() { return iterator(0, this); }
iterator End() { return iterator(Size(), this); } iterator End() { return iterator(Size(), this); }
protected:
void SetPosition(unsigned int chunk) {
#ifdef WIN32
SetFilePointer(fp, chunk * chunk_size * sizeof(T), 0, FILE_BEGIN);
#else
fseek(fp, chunk * chunk_size * sizeof(T), SEEK_SET);
#endif
}
void ReadBuffer(T *data, unsigned int size) {
#ifdef WIN32
DWORD tmp;
ReadFile(fp, data, sizeof(T) * size, &tmp, NULL);
if(tmp != sizeof(T) * size)
assert(0 && "Could not read");
#else
if(size != fread(data, sizeof(T), size, fp))
assert(0 && "Could not read");
#endif
}
void WriteBuffer(T *data, unsigned int size) {
#ifdef WIN32
DWORD tmp;
WriteFile(fp, data, sizeof(T) * size, &tmp, NULL);
if(tmp != sizeof(T) * size)
assert(0 && "Could not write");
#else
if(size != fwrite(data, sizeof(T), size, fp))
assert(0 && "Could not write");
#endif
}
}; };
}//namespace }//namespace

View File

@ -24,6 +24,9 @@
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.7 2004/10/01 16:54:57 ponchio
Daily backup.
Revision 1.6 2004/09/30 00:27:42 ponchio Revision 1.6 2004/09/30 00:27:42 ponchio
Lot of changes. Backup. Lot of changes. Backup.
@ -238,6 +241,7 @@ unsigned int VoronoiChain::Locate(unsigned int level,
return fine + coarse * levels[level].size();*/ return fine + coarse * levels[level].size();*/
} }
//TODO move this to nxsbuild
void VoronoiChain::RemapFaces(Crude &crude, VFile<unsigned int> &face_remap, void VoronoiChain::RemapFaces(Crude &crude, VFile<unsigned int> &face_remap,
vector<unsigned int> &patch_faces, vector<unsigned int> &patch_faces,
float scaling, int steps) { float scaling, int steps) {
@ -267,10 +271,7 @@ void VoronoiChain::RemapFaces(Crude &crude, VFile<unsigned int> &face_r
} else } else
patch = patches[make_pair(coarse, fine)]; patch = patches[make_pair(coarse, fine)];
//BEWARE unkomment this!
face_remap[i] = patch; face_remap[i] = patch;
//face_remap[i] = fine;
// face_remap[i] = coarse;
if(patch_faces.size() <= patch) if(patch_faces.size() <= patch)
patch_faces.resize(patch+1, 0); patch_faces.resize(patch+1, 0);
patch_faces[patch]++; patch_faces[patch]++;

View File

@ -24,6 +24,9 @@
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.6 2004/10/01 16:54:57 ponchio
Daily backup.
Revision 1.5 2004/09/30 00:27:42 ponchio Revision 1.5 2004/09/30 00:27:42 ponchio
Lot of changes. Backup. Lot of changes. Backup.
@ -161,7 +164,8 @@ int main(int argc, char *argv[]) {
cerr << " -f N: use N faces per patch (default 1000, max 32000)\n"; cerr << " -f N: use N faces per patch (default 1000, max 32000)\n";
cerr << " -t N: mini faces per patch (default 200)\n"; cerr << " -t N: mini faces per patch (default 200)\n";
cerr << " -l N: number of levels\n"; cerr << " -l N: number of levels\n";
cerr << " -s F: scaling factor (0 < F < 1, default 0.5)\n\n"; cerr << " -s F: scaling factor (0 < F < 1, default 0.5)\n";
cerr << " -o N: nomber of optimization steps\n";
cerr << " -d <method>: decimation method: quadric, cluster. (default quadric)\n"; cerr << " -d <method>: decimation method: quadric, cluster. (default quadric)\n";
return -1; return -1;
} }