vcglib/apps/nexus/nxsalgo.cpp

403 lines
11 KiB
C++
Raw Normal View History

2004-10-01 14:40:32 +02:00
#include <vector>
#include <map>
#include <set>
2004-10-08 16:46:26 +02:00
#include <iostream>
2004-10-01 14:40:32 +02:00
2004-10-09 13:02:05 +02:00
//#include <wrap/strip/tristrip.h>
2004-10-01 14:40:32 +02:00
#include "nxsalgo.h"
2005-01-14 16:25:29 +01:00
#include "vfile.h"
2004-10-01 14:40:32 +02:00
#include "nexus.h"
2004-12-04 15:19:22 +01:00
#include "watch.h"
2004-10-01 14:40:32 +02:00
using namespace std;
using namespace nxs;
using namespace vcg;
#include "tristripper/tri_stripper.h"
using namespace triangle_stripper;
void nxs::ComputeNormals(Nexus &nexus) {
assert(nexus.signature & NXS_NORMALS_SHORT ||
nexus.signature & NXS_NORMALS_FLOAT);
2004-12-04 16:48:57 +01:00
//setting borders readonly:
2004-12-09 23:33:28 +01:00
assert(!nexus.borders.IsReadOnly());
2004-12-04 16:48:57 +01:00
nexus.borders.SetReadOnly(true);
2004-10-01 14:40:32 +02:00
bool use_short = (nexus.signature & NXS_NORMALS_SHORT) != 0;
2004-10-15 13:41:03 +02:00
//TODO use a temporary file to store border normals
unsigned int tmpb_offset = 0;
vector<unsigned int> tmpb_start;
VFile<Point3f> tmpb;
if(!tmpb.Create("tmpb.tmp")) {
cerr << "Could not create temporary border file\n";
exit(0);
}
2005-01-14 16:25:29 +01:00
for(unsigned int p = 0; p < nexus.size(); p++) {
2004-10-15 13:41:03 +02:00
Border border = nexus.GetBorder(p);
tmpb_start.push_back(tmpb_offset);
tmpb_offset += border.Size();
}
Point3f zero(0.0f, 0.0f, 0.0f);
2004-12-04 15:19:22 +01:00
2004-10-15 13:41:03 +02:00
tmpb.Resize(tmpb_offset);
for(unsigned int i = 0; i < tmpb.Size(); i++)
tmpb[i] = zero;
2004-10-19 18:50:27 +02:00
tmpb.Flush();
2004-10-15 13:41:03 +02:00
2004-10-01 14:40:32 +02:00
//first step normals in the same patch.
2004-12-04 15:19:22 +01:00
cerr << "First Step\n";
2005-01-14 16:25:29 +01:00
Report report(nexus.size(), 5);
2004-12-04 16:48:57 +01:00
vector<Point3f> normals;
2005-01-14 16:25:29 +01:00
for(unsigned int p = 0; p < nexus.size(); p++) {
2004-12-04 15:19:22 +01:00
report.Step(p);
2004-10-08 16:46:26 +02:00
Patch &patch = nexus.GetPatch(p);
2004-10-15 13:41:03 +02:00
2004-12-04 16:48:57 +01:00
normals.clear();
2004-10-01 14:40:32 +02:00
normals.resize(patch.nv, Point3f(0, 0, 0));
if(nexus.signature & NXS_FACES)
for(unsigned int i = 0; i < patch.nf; i++) {
2004-12-04 16:48:57 +01:00
unsigned short *f = patch.Face(i);
Point3f &v0 = patch.Vert(f[0]);
Point3f &v1 = patch.Vert(f[1]);
Point3f &v2 = patch.Vert(f[2]);
2004-10-01 14:40:32 +02:00
2004-12-04 16:48:57 +01:00
Point3f norm = (v1 - v0) ^ (v2 - v0);
normals[f[0]] += norm;
normals[f[1]] += norm;
normals[f[2]] += norm;
2004-10-01 14:40:32 +02:00
}
2004-10-01 18:54:57 +02:00
2004-10-01 14:40:32 +02:00
if(nexus.signature & NXS_STRIP)
2004-10-09 16:46:47 +02:00
for(int i = 0; i < patch.nf - 2; i++) {
2004-12-04 16:48:57 +01:00
unsigned short *f = patch.FaceBegin() + i;
Point3f &v0 = patch.Vert(f[0]);
Point3f &v1 = patch.Vert(f[1]);
Point3f &v2 = patch.Vert(f[2]);
2004-10-01 14:40:32 +02:00
2004-12-04 16:48:57 +01:00
Point3f norm = (v1 - v0) ^ (v2 - v0);
if(i%2) norm = -norm;
normals[f[0]] += norm;
normals[f[1]] += norm;
normals[f[2]] += norm;
2004-10-01 14:40:32 +02:00
}
if(use_short) {
for(unsigned int i = 0; i < patch.nv; i++) {
2004-12-04 16:48:57 +01:00
Point3f &norm = normals[i];
norm.Normalize();
short *n = patch.Norm16(i);
for(int k = 0; k < 3; k++) {
n[k] = (short)(norm[k] * 32766);
}
n[3] = 0;
2004-10-01 14:40:32 +02:00
}
} else {
memcpy(patch.Norm16Begin(), &*normals.begin(),
normals.size() * sizeof(Point3f));
}
2004-12-04 15:19:22 +01:00
2004-10-01 14:40:32 +02:00
Border border = nexus.GetBorder(p);
2004-12-04 15:19:22 +01:00
map<unsigned int, map<unsigned short, Point3f> > bnorm;
2004-12-04 16:48:57 +01:00
unsigned int poff = tmpb_start[p];
2004-12-04 15:19:22 +01:00
for(unsigned int i = 0; i < border.Size(); i++) {
Link &link = border[i];
if(link.IsNull()) continue;
Point3f pt = normals[link.start_vert];
2004-12-04 16:48:57 +01:00
//bnorm[p][link.start_vert] = pt;
2004-12-04 15:19:22 +01:00
bnorm[link.end_patch][link.end_vert] = pt;
2004-12-04 16:48:57 +01:00
tmpb[poff + i] += pt;
2004-12-04 15:19:22 +01:00
}
map<unsigned int, map<unsigned short, Point3f> >::iterator k;
for(k = bnorm.begin(); k != bnorm.end(); k++) {
unsigned int patch = (*k).first;
Border border = nexus.GetBorder(patch);
unsigned int offset = tmpb_start[patch];
for(unsigned int i = 0; i < border.Size(); i++) {
2004-12-04 16:48:57 +01:00
Link &link = border[i];
//assert(!link.IsNull());
//TODO not accurate
if(link.end_patch != p) continue;
if((*k).second.count(link.start_vert))
tmpb[offset + i] += (*k).second[link.start_vert];
2004-12-04 15:19:22 +01:00
}
}
/* set<unsigned int> close;
2004-10-01 14:40:32 +02:00
for(unsigned int i = 0; i < border.Size(); i++) {
Link &link = border[i];
if(link.IsNull()) continue;
2004-10-15 13:41:03 +02:00
unsigned int off = tmpb_start[p];
2004-10-19 18:50:27 +02:00
Point3f p = tmpb.read(off + i);
p += normals[link.start_vert];
tmpb.write(off + i, p);
// tmpb[off + i] += normals[link.start_vert];
2004-10-15 13:41:03 +02:00
close.insert(link.end_patch);
}
2004-10-01 14:40:32 +02:00
2004-10-15 13:41:03 +02:00
set<unsigned int>::iterator k;
for(k = close.begin(); k != close.end(); k++) {
Border remote = nexus.GetBorder(*k);
unsigned int off = tmpb_start[*k];
for(unsigned int i = 0; i < remote.Size(); i++) {
Link &link = remote[i];
if(link.IsNull()) continue;
if(link.end_patch != p) continue;
2004-10-19 18:50:27 +02:00
Point3f p = tmpb.read(off + i);
p += normals[link.end_vert];
tmpb.write(off + i, p);
// tmpb[off + i] += normals[link.end_vert];
2004-10-01 14:40:32 +02:00
}
2004-12-04 15:19:22 +01:00
}*/
2004-10-15 13:41:03 +02:00
}
2004-10-01 14:40:32 +02:00
2004-10-15 13:41:03 +02:00
//Second step unify normals across borders
2004-12-04 15:19:22 +01:00
cerr << "Second step\n";
2005-01-14 16:25:29 +01:00
report.Init(nexus.size());
for(unsigned int p = 0; p < nexus.size(); p++) {
2004-12-04 15:19:22 +01:00
report.Step(p);
2004-10-15 13:41:03 +02:00
Patch &patch = nexus.GetPatch(p);
Border border = nexus.GetBorder(p);
2004-10-01 14:40:32 +02:00
for(unsigned int i = 0; i < border.Size(); i++) {
Link &link = border[i];
if(link.IsNull()) continue;
2004-10-15 13:41:03 +02:00
unsigned int off = tmpb_start[p];
2004-10-19 18:50:27 +02:00
// Point3f &n = tmpb[off + i];
2004-12-04 15:19:22 +01:00
Point3f n = tmpb[off + i];
2004-10-15 13:41:03 +02:00
n.Normalize();
2004-10-01 14:40:32 +02:00
if(use_short) {
2004-10-19 03:23:02 +02:00
n *= 32766;
2004-10-01 14:40:32 +02:00
short *np = patch.Norm16(link.start_vert);
2004-10-15 13:41:03 +02:00
np[0] = (short)n[0];
np[1] = (short)n[1];
np[2] = (short)n[2];
2004-10-19 03:23:02 +02:00
np[3] = 0;
2004-10-01 14:40:32 +02:00
} else {
2004-10-15 13:41:03 +02:00
patch.Norm32(link.start_vert) = n;
2004-10-01 14:40:32 +02:00
}
}
}
2004-12-04 15:19:22 +01:00
tmpb.Close();
tmpb.Delete();
2004-10-15 13:41:03 +02:00
//TODO remove temporary file.
2004-12-04 16:48:57 +01:00
nexus.borders.SetReadOnly(false);
2004-10-01 14:40:32 +02:00
}
void nxs::ComputeTriStrip(unsigned short nfaces, unsigned short *faces,
vector<unsigned short> &strip) {
vector<unsigned int> index;
index.resize(nfaces*3);
2004-10-09 16:46:47 +02:00
for(int i = 0; i < nfaces*3; i++) {
2004-10-01 14:40:32 +02:00
index[i] = faces[i];
}
int cache_size = 0;
tri_stripper stripper(index);
stripper.SetCacheSize(cache_size);
// = 0 will disable the cache optimizer
stripper.SetMinStripSize(0);
tri_stripper::primitives_vector primitives;
stripper.Strip(&primitives);
if(primitives.back().m_Indices.size() < 3) {
primitives.pop_back();
}
//TODO spostare questo dentro il ciclo che rimonta le strip.
if(primitives.back().m_Type == tri_stripper::PT_Triangles) {
tri_stripper::primitives p;
p = primitives.back();
primitives.pop_back();
2004-10-09 16:46:47 +02:00
for(unsigned int i = 0; i < p.m_Indices.size(); i += 3) {
2004-10-01 14:40:32 +02:00
tri_stripper::primitives s;
s.m_Type = tri_stripper::PT_Triangle_Strip;
s.m_Indices.push_back(p.m_Indices[i]);
s.m_Indices.push_back(p.m_Indices[i+1]);
s.m_Indices.push_back(p.m_Indices[i+2]);
primitives.push_back(s);
}
}
for(unsigned int i = 0; i < primitives.size(); i++) {
tri_stripper::primitives &primitive = primitives[i];
assert(primitive.m_Indices.size() != 0);
2004-10-09 16:46:47 +02:00
int len = primitive.m_Indices.size();
2004-10-01 14:40:32 +02:00
for(int l = 0; l < len; l++)
strip.push_back(primitive.m_Indices[l]);
if(i < primitives.size()-1) { //not the last primitive.
strip.push_back(primitive.m_Indices[len-1]);
//TODO optimize this!
if((len%2) == 1) //do not change orientation....
strip.push_back(primitive.m_Indices[len-1]);
strip.push_back(primitives[i+1].m_Indices[0]);
}
}
}
2004-10-19 03:23:02 +02:00
void nxs::Reorder(unsigned int signature, Patch &patch) {
vector<unsigned> remap;
remap.resize(patch.nv, 0xffff);
int nf = patch.nf;
if(signature & NXS_FACES)
nf *= 3;
//building remap
unsigned short *f = patch.FaceBegin();
unsigned int count = 0;
for(int i = 0; i < nf; i++) {
assert(f[i] < remap.size());
if(remap[f[i]] == 0xffff) {
remap[f[i]] = count++;
}
}
//test no unreferenced vertices
for(int i = 0; i < patch.nv; i++)
if(remap[i] == 0xffff)
remap[i] = i;
//converting faces
for(int i = 0; i < nf; i++)
f[i] = remap[f[i]];
vector<Point3f> vert;
vert.resize(patch.nv);
memcpy(&*vert.begin(), patch.VertBegin(), patch.nv * sizeof(Point3f));
for(int i = 0; i < patch.nv; i++)
patch.Vert(remap[i]) = vert[i];
}
//TODO actually use threshold
void nxs::Unify(Nexus &nexus, float threshold) {
//TODO what if colors or normals or strips?
unsigned int duplicated = 0;
unsigned int degenerate = 0;
for(unsigned int p = 0; p < nexus.size(); p++) {
Entry &entry = nexus[p];
Patch &patch = nexus.GetPatch(p);
unsigned int vcount = 0;
map<Point3f, unsigned short> vertices;
vector<unsigned short> remap;
remap.resize(patch.nv);
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;
//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] &&
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]]);
} else {
degenerate++;
}
}
//rewrite patch now.
entry.nvert = newvert.size();
entry.nface = newface.size()/3;
patch.Init(nexus.signature, entry.nvert, entry.nface);
memcpy(patch.VertBegin(), &(newvert[0]), entry.nvert*sizeof(Point3f));
memcpy(patch.FaceBegin(), &(newface[0]), entry.nface*3*sizeof(short));
//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);
}
}
//fix patch borders now
set<unsigned int> close; //bordering pathes
Border border = nexus.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 = nexus.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];
}
}
}
}
//better to compact directly borders than setting them null.
//finally: there may be duplicated borders
for(unsigned int p = 0; p < nexus.size(); p++) {
Border border = nexus.GetBorder(p);
set<Link> links;
for(unsigned int b = 0; b < border.Size(); b++) {
Link &link = border[b];
assert(!link.IsNull());
//if(border[b].IsNull()) continue;
links.insert(link);
}
int count = 0;
for(set<Link>::iterator k = links.begin(); k != links.end(); k++)
border[count++] = *k;
nexus.borders[p].used = links.size();
}
nexus.totvert -= duplicated;
if(duplicated)
cerr << "Found " << duplicated << " duplicated vertices" << endl;
if(degenerate)
cerr << "Found " << degenerate << " degenerate face while unmifying\n";
}