diff --git a/apps/nexus/nxsbuild.cpp b/apps/nexus/nxsbuild.cpp index 3f803c0e..f0eb0963 100644 --- a/apps/nexus/nxsbuild.cpp +++ b/apps/nexus/nxsbuild.cpp @@ -19,8 +19,8 @@ void nxs::RemapVertices(Crude &crude, set pp; vert_remap.GetValues(face[k], pp); if(!pp.count(patch)) { - vert_remap.Insert(face[k], patch); - patch_verts[patch]++; + vert_remap.Insert(face[k], patch); + patch_verts[patch]++; } } } @@ -37,6 +37,7 @@ void nxs::NexusAllocate(Crude &crude, // unsigned int totchunks = 0; //now that we know various sizes, lets allocate space + cerr << "Fixing entries\n"; for(unsigned int i = 0; i < nexus.index.size(); i++) { Nexus::PatchInfo &entry = nexus.index[i]; @@ -47,7 +48,7 @@ void nxs::NexusAllocate(Crude &crude, entry.nvert = patch_verts[i]; entry.nface = patch_faces[i]; - entry.error = 0; + entry.error = 0; } patch_faces.clear(); @@ -55,12 +56,18 @@ void nxs::NexusAllocate(Crude &crude, //now we sort the faces into the patches (but still using absolute indexing //instead of relative indexing + cerr << "Moving faces into patches\n"; + for(unsigned int i = 0; i < crude.face.Size(); i++) { Crude::Face &face = crude.face[i]; unsigned int npatch = face_remap[i]; - - Nexus::PatchInfo &entry = nexus.index[npatch]; - // cerr << "Entrynf: " << entry.nface << endl; +#ifdef CONTROLS + if(npatch > nexus.index.size()) { + cerr << "INvalid index\n"; + exit(0); + } +#endif + Nexus::PatchInfo &entry = nexus.index[npatch]; //TODO this is slow because we have to initialize patch. //just get patch.start. Patch patch = nexus.GetPatch(npatch); @@ -72,8 +79,17 @@ void nxs::NexusAllocate(Crude &crude, cerr << "Found degenerate.\n"; continue; } +#ifdef CONTROLS + if(patch_faces[npatch] > entry.nface) { + cerr << "patch_faces[npatch]: " << patch_faces[npatch] + << " should be <= " << entry.nface << endl; + cerr << "something not workin here!\n"; + exit(0); + } +#endif faces[patch_faces[npatch]++] = face; } + cerr << "Fixing entry faces\n"; for(unsigned int i = 0; i < nexus.index.size(); i++) { Nexus::PatchInfo &entry = nexus.index[i]; entry.nface = patch_faces[i]; diff --git a/apps/nexus/patchserver.cpp b/apps/nexus/patchserver.cpp index 2709bde6..0d7f4032 100644 --- a/apps/nexus/patchserver.cpp +++ b/apps/nexus/patchserver.cpp @@ -99,6 +99,12 @@ Patch &PatchServer::GetPatch(unsigned int idx, PTime nptime(idx); char *ram = new char[entry.ram_size * chunk_size]; +#ifdef CONTROLS + if(!ram) { + cerr << "COuld not allocate ram!\n"; + exit(0); + } +#endif nptime.patch = new Patch(signature, ram, nvert, nface); if(entry.patch_start != 0xffffffff) { //was allocated. @@ -107,15 +113,15 @@ Patch &PatchServer::GetPatch(unsigned int idx, SetPosition(entry.patch_start * chunk_size); if((signature & NXS_COMPRESSED) == 0) { //not compressed - ReadBuffer(ram, entry.disk_size * chunk_size); + ReadBuffer(ram, entry.disk_size * chunk_size); } else { - unsigned char *disk = new unsigned char[entry.disk_size * chunk_size]; - ReadBuffer(disk, entry.disk_size * chunk_size); + unsigned char *disk = new unsigned char[entry.disk_size * chunk_size]; + ReadBuffer(disk, entry.disk_size * chunk_size); - nptime.patch->Decompress(entry.ram_size * chunk_size, - disk, entry.disk_size * chunk_size); - delete []disk; + nptime.patch->Decompress(entry.ram_size * chunk_size, + disk, entry.disk_size * chunk_size); + delete []disk; } } diff --git a/apps/nexus/voronoichain.cpp b/apps/nexus/voronoichain.cpp index 2aaf6773..05c89130 100644 --- a/apps/nexus/voronoichain.cpp +++ b/apps/nexus/voronoichain.cpp @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.15 2004/10/21 12:22:21 ponchio +Small changes. + Revision 1.14 2004/10/19 04:23:29 ponchio *** empty log message *** @@ -276,16 +279,13 @@ void VoronoiChain::Init(Crude &crude, float scaling, int steps) { unsigned int VoronoiChain::Locate(unsigned int level, const vcg::Point3f &p) { return levels[level].Locate(p); - /* assert(levels.size() > level+1); - unsigned int fine = levels[level].Locate(p); - unsigned int coarse = levels[level+1].Locate(p); - return fine + coarse * levels[level].size();*/ } //TODO move this to nxsbuild -void VoronoiChain::RemapFaces(Crude &crude, VFile &face_remap, - vector &patch_faces, - float scaling, int steps) { +void VoronoiChain::RemapFaces(Crude &crude, + VFile &face_remap, + vector &patch_faces, + float scaling, int steps) { Init(crude, scaling, steps); @@ -300,7 +300,7 @@ void VoronoiChain::RemapFaces(Crude &crude, VFile &face_r Point3f bari; for(unsigned int i = 0; i < crude.Faces(); i++) { bari = crude.GetBari(i); - // unsigned int patch = Locate(0, bari); + unsigned int fine = Locate(0, bari); unsigned int coarse = Locate(1, bari); @@ -315,14 +315,13 @@ void VoronoiChain::RemapFaces(Crude &crude, VFile &face_r face_remap[i] = patch; //face_remap[i] = fine; - if(patch_faces.size() <= patch) - patch_faces.resize(patch+1, 0); + while(patch_faces.size() <= patch) + patch_faces.push_back(0); patch_faces[patch]++; } - - //prune faces (now only 0 faces); + //TODO prune really small faces unsigned int tot_patches = 0; vector patch_remap; for(unsigned int i = 0; i < patch_faces.size(); i++) { @@ -331,6 +330,12 @@ void VoronoiChain::RemapFaces(Crude &crude, VFile &face_r patch_remap.push_back(-1); else patch_remap.push_back(tot_patches++); + + if(patch_faces[i] > 32000) { + //TODO do something to reduce patch size... :P + cerr << "Found a patch too big... sorry\n"; + exit(0); + } } @@ -344,23 +349,26 @@ void VoronoiChain::RemapFaces(Crude &crude, VFile &face_r } //remapping faces + patch_faces.clear(); + patch_faces.resize(totpatches, 0); for(unsigned int i = 0; i < face_remap.Size(); i++) { unsigned int patch = face_remap[i]; - assert(patch != 0xffffffff); - assert(patch_remap[patch] != -1); - face_remap[i] = patch_remap[patch]; - } - - //remapping patch_faces - for(unsigned int i = 0; i < patch_faces.size(); i++) { - assert(patch_remap[i] <= (int)i); - if(patch_remap[i] != -1) { - assert(patch_faces[i] > 0); - patch_faces[patch_remap[i]] = patch_faces[i]; +#ifdef CONTROLS + if(patch == 0xffffffff) { + cerr << "RESIGH\n"; + exit(0); } + if(patch_remap[patch] == -1) { + cerr << "SIGH!\n"; + exit(0); + } +#endif + unsigned int newpatch = patch_remap[patch]; + face_remap[i] = newpatch; + patch_faces[newpatch]++; } - patch_faces.resize(tot_patches); + } void VoronoiChain::BuildLevel(Nexus &nexus, unsigned int offset, @@ -425,12 +433,12 @@ void VoronoiChain::BuildLevel(Nexus &nexus, unsigned int offset, Patch patch = nexus.GetPatch(idx); for(unsigned int i = 0; i < patch.nv; i++) { - unsigned int ctarget = 0xffffffff; - float dist = coarse.Closest(patch.Vert(i), ctarget); - assert(ctarget != 0xffffffff); - ccentroids[ctarget] += patch.Vert(i); - ccount[ctarget]++; - if(dist > radius[ctarget]) radius[ctarget] = dist; + unsigned int ctarget = 0xffffffff; + float dist = coarse.Closest(patch.Vert(i), ctarget); + assert(ctarget != 0xffffffff); + ccentroids[ctarget] += patch.Vert(i); + ccount[ctarget]++; + if(dist > radius[ctarget]) radius[ctarget] = dist; } } for(unsigned int v = 0; v < coarse.size(); v++) { @@ -457,71 +465,6 @@ void VoronoiChain::BuildLevel(Nexus &nexus, unsigned int offset, if(coarse.size() == 0) coarse.push_back(Point3f(0,0,0)); coarse.Init(); - -//Coarse optimization -/* vector< map > ccentroids; - vector< map > ccount; - - for(unsigned int step = 0; step < steps; step++) { - cerr << "Optimization step " << levels.size()-1 << ":" - << step << "/" << steps << endl; - ccentroids.clear(); - ccount.clear(); - ccentroids.resize(coarse.size()); - ccount.resize(coarse.size()); - - for(unsigned int idx = offset; idx < nexus.index.size(); idx++) { - Patch patch = nexus.GetPatch(idx); - - for(unsigned int i = 0; i < patch.nv; i++) { - Point3f &v = patch.Vert(i); - unsigned int ftarget; - float dist = fine.Closest(v, ftarget); - - dist = fine.Closest(v, ftarget); - assert(ftarget != -1); - - unsigned int ctarget; - dist = coarse.Closest(v, ctarget); - assert(ctarget != -1); - - map ¢roids = ccentroids[ctarget]; - map &count = ccount[ctarget]; - - if(!centroids.count(ftarget)) - centroids[ftarget]= Point3f(0, 0, 0); - - if(!count.count(ftarget)) - count[ftarget] = 0; - - centroids[ftarget] += v; - count[ftarget]++; - } - } - - cerr << "recentring" << endl; - for(unsigned int v = 0; v < coarse.size(); v++) { - - map ¢roids = ccentroids[v]; - map &count = ccount[v]; - - coarse[v].p = Point3f(0, 0, 0); - float weight = 0; - unsigned int tot_size =0; - map::iterator k; - for(k = centroids.begin();k != centroids.end(); k++) { - unsigned int size = count[(*k).first]; - tot_size += size; - coarse[v].p += (*k).second / size; - weight += 1; - } - assert(weight > 0); - coarse[v].p /= weight; - //TODO find a solution! - // coarse[v].weight = pow(tot_size/coarse_vmean, 0.25f); - } - coarse.Init(); - }*/ newfragments.clear(); //TODO add some optimization }