trimesh.cpp
来自「RBF平台」· C++ 代码 · 共 1,522 行 · 第 1/3 页
CPP
1,522 行
float u = e[j] DOT t;
float v = e[j] DOT b;
w[0][0] += u*u;
w[0][1] += u*v;
//w[1][1] += v*v + u*u;
//w[1][2] += u*v;
w[2][2] += v*v;
vec dn = normals[faces[i][PREV(j)]] -
normals[faces[i][NEXT(j)]];
float dnu = dn DOT t;
float dnv = dn DOT b;
m[0] += dnu*u;
m[1] += dnu*v + dnv*u;
m[2] += dnv*v;
}
w[1][1] = w[0][0] + w[2][2];
w[1][2] = w[0][1];
float diag[3];
// Least squares solution
if (!((leqn.ldltdc(w,diag)))) {
//fprintf(stderr, "ldltdc failed!\n");
continue;
}
leqn.ldltsl(w, diag, m, m);
// Push it back out to the vertices
for (j = 0; j < 3; j++) {
int vj = faces[i][j];
float c1, c12, c2;
proj_curv(t, b, m[0], m[1], m[2],
pdir1[vj], pdir2[vj], c1, c12, c2);
float wt = cornerareas[i][j] / pointareas[vj];
curv1[vj] += wt * c1;
curv12[vj] += wt * c12;
curv2[vj] += wt * c2;
}
}
// Compute principal directions and curvatures at each vertex
for (i = 0; i < nv; i++)
diagonalize_curv(pdir1[i], pdir2[i],
curv1[i], curv12[i], curv2[i],
normals[i], pdir1[i], pdir2[i],
curv1[i], curv2[i]);
}
// Compute derivatives of curvature.
void TriMesh::need_dcurv()
{
if (dcurv.size() == vertices.size())
return;
need_curvatures();
int i,j;
// Resize the arrays we'll be using
int nv = vertices.size(), nf = faces.size();
dcurv.clear(); dcurv.resize(nv);
float w[4][4],m[4],d[4];
for(i = 0; i <4; ++i)
{
m[i] = 0; d[i] = 0;
for(j = 0; j < 4; j++)
w[i][j] = 0;
}
LinEqn<float,4> leqn;
// Compute dcurv per-face
for (i = 0; i < nf; i++) {
// Edges
vec e[3] = { vertices[faces[i][2]] - vertices[faces[i][1]],
vertices[faces[i][0]] - vertices[faces[i][2]],
vertices[faces[i][1]] - vertices[faces[i][0]] };
// N-T-B coordinate system per face
vec t = e[0];
normalize(t);
vec n = e[0] CROSS e[1];
vec b = n CROSS t;
normalize(b);
// Project curvature tensor from each vertex into this
// face's coordinate system
vec fcurv[3];
for (j = 0; j < 3; j++) {
int vj = faces[i][j];
proj_curv(pdir1[vj], pdir2[vj], curv1[vj], 0, curv2[vj],
t, b, fcurv[j][0], fcurv[j][1], fcurv[j][2]);
}
// Estimate dcurv based on variation of curvature along edges
for (j = 0; j < 3; j++) {
// Variation of curvature along each edge
vec dfcurv = fcurv[PREV(j)] - fcurv[NEXT(j)];
float u = e[j] DOT t;
float v = e[j] DOT b;
float u2 = u*u, v2 = v*v, uv = u*v;
w[0][0] += u2;
w[0][1] += uv;
//w[1][1] += 2.0f*u2 + v2;
//w[1][2] += 2.0f*uv;
//w[2][2] += u2 + 2.0f*v2;
//w[2][3] += uv;
w[3][3] += v2;
m[0] += u*dfcurv[0];
m[1] += v*dfcurv[0] + 2.0f*u*dfcurv[1];
m[2] += 2.0f*v*dfcurv[1] + u*dfcurv[2];
m[3] += v*dfcurv[2];
}
w[1][1] = 2.0f * w[0][0] + w[3][3];
w[1][2] = 2.0f * w[0][1];
w[2][2] = w[0][0] + 2.0f * w[3][3];
w[2][3] = w[0][1];
// Least squares solution
if (!leqn.ldltdc(w, d)) {
//fprintf(stderr, "ldltdc failed!\n");
continue;
}
leqn.ldltsl(w, d, m, m);
Vec<4> face_dcurv(m);
// Push it back out to each vertex
for (j = 0; j < 3; j++) {
int vj = faces[i][j];
Vec<4> this_vert_dcurv;
proj_dcurv(t, b, face_dcurv,
pdir1[vj], pdir2[vj], this_vert_dcurv);
float wt = cornerareas[i][j] / pointareas[vj];
dcurv[vj] += wt * this_vert_dcurv;
}
}
}
// Compute per-vertex point areas
void TriMesh::need_pointareas()
{
if (pointareas.size() == vertices.size())
return;
need_faces();
dprintf("Computing point areas... ");
int nf = faces.size(), nv = vertices.size();
pointareas.clear();
pointareas.resize(nv);
cornerareas.clear();
cornerareas.resize(nf);
for (int i = 0; i < nf; i++) {
// Edges
vec e[3] = { vertices[faces[i][2]] - vertices[faces[i][1]],
vertices[faces[i][0]] - vertices[faces[i][2]],
vertices[faces[i][1]] - vertices[faces[i][0]] };
// Compute corner weights
float area = 0.5f * len(e[0] CROSS e[1]);
float l2[3] = { len2(e[0]), len2(e[1]), len2(e[2]) };
float ew[3] = { l2[0] * (l2[1] + l2[2] - l2[0]),
l2[1] * (l2[2] + l2[0] - l2[1]),
l2[2] * (l2[0] + l2[1] - l2[2]) };
if (ew[0] <= 0.0f) {
cornerareas[i][1] = -0.25f * l2[2] * area /
(e[0] DOT e[2]);
cornerareas[i][2] = -0.25f * l2[1] * area /
(e[0] DOT e[1]);
cornerareas[i][0] = area - cornerareas[i][1] -
cornerareas[i][2];
} else if (ew[1] <= 0.0f) {
cornerareas[i][2] = -0.25f * l2[0] * area /
(e[1] DOT e[0]);
cornerareas[i][0] = -0.25f * l2[2] * area /
(e[1] DOT e[2]);
cornerareas[i][1] = area - cornerareas[i][2] -
cornerareas[i][0];
} else if (ew[2] <= 0.0f) {
cornerareas[i][0] = -0.25f * l2[1] * area /
(e[2] DOT e[1]);
cornerareas[i][1] = -0.25f * l2[0] * area /
(e[2] DOT e[0]);
cornerareas[i][2] = area - cornerareas[i][0] -
cornerareas[i][1];
} else {
float ewscale = 0.5f * area / (ew[0] + ew[1] + ew[2]);
for (int j = 0; j < 3; j++)
cornerareas[i][j] = ewscale * (ew[(j+1)%3] +
ew[(j+2)%3]);
}
pointareas[faces[i][0]] += cornerareas[i][0];
pointareas[faces[i][1]] += cornerareas[i][1];
pointareas[faces[i][2]] += cornerareas[i][2];
}
}
void TriMesh::reorder_verts(TriMesh *mesh)
{
//TriMesh::dprintf("Reordering vertices... ");
int i,j;
int nv = mesh->vertices.size();
vector<int> remap(nv, -1);
int next = 0;
if (!mesh->tstrips.empty()) {
mesh->faces.clear();
mesh->convert_strips(TriMesh::TSTRIP_TERM);
for (i = 0; i < mesh->tstrips.size(); i++) {
int v = mesh->tstrips[i];
if (v == -1)
continue;
if (remap[v] == -1)
remap[v] = next++;
}
} else {
for (i = 0; i < mesh->faces.size(); i++) {
for ( j = 0; j < 3; j++) {
int v = mesh->faces[i][j];
if (remap[v] == -1)
remap[v] = next++;
}
}
}
if (next != nv) {
// Unreferenced vertices... Just stick them at the end.
for (i = 0; i < nv; i++)
if (remap[i] == -1)
remap[i] = next++;
}
TriMesh *oldmesh = new TriMesh;
*oldmesh = *mesh;
for (i = 0; i < nv; i++)
mesh->vertices[remap[i]] = oldmesh->vertices[i];
if (!mesh->colors.empty()) {
for (i = 0; i < nv; i++)
mesh->colors[remap[i]] = oldmesh->colors[i];
}
if (!mesh->confidences.empty()) {
for (i = 0; i < nv; i++)
mesh->confidences[remap[i]] = oldmesh->confidences[i];
}
if (!mesh->flags.empty()) {
for (i = 0; i < nv; i++)
mesh->flags[remap[i]] = oldmesh->flags[i];
}
if (!mesh->normals.empty()) {
for (i = 0; i < nv; i++)
mesh->normals[remap[i]] = oldmesh->normals[i];
}
if (!mesh->pdir1.empty()) {
for (i = 0; i < nv; i++)
mesh->pdir1[remap[i]] = oldmesh->pdir1[i];
}
if (!mesh->pdir2.empty()) {
for (i = 0; i < nv; i++)
mesh->pdir2[remap[i]] = oldmesh->pdir2[i];
}
if (!mesh->curv1.empty()) {
for (i = 0; i < nv; i++)
mesh->curv1[remap[i]] = oldmesh->curv1[i];
}
if (!mesh->curv2.empty()) {
for (i = 0; i < nv; i++)
mesh->curv2[remap[i]] = oldmesh->curv2[i];
}
if (!mesh->dcurv.empty()) {
for (i = 0; i < nv; i++)
mesh->dcurv[remap[i]] = oldmesh->dcurv[i];
}
if (!mesh->cornerareas.empty()) {
for (i = 0; i < nv; i++)
mesh->cornerareas[remap[i]] = oldmesh->cornerareas[i];
}
if (!mesh->pointareas.empty()) {
for (i = 0; i < nv; i++)
mesh->pointareas[remap[i]] = oldmesh->pointareas[i];
}
delete oldmesh;
mesh->neighbors.clear();
mesh->adjacentfaces.clear();
mesh->across_edge.clear();
if (!mesh->tstrips.empty()) {
for (i = 0; i < mesh->tstrips.size(); i++) {
int &v = mesh->tstrips[i];
if (v == -1)
continue;
v = remap[v];
}
mesh->convert_strips(TriMesh::TSTRIP_LENGTH);
} else {
for (i = 0; i < mesh->faces.size(); i++) {
for (j = 0; j < 3; j++) {
int &v = mesh->faces[i][j];
v = remap[v];
}
}
}
}
/*
Create an offset surface from a mesh. Dumb - just moves along the
normal by the given distance, making no attempt to avoid self-intersection.
Eventually, this could/should be extended to use the method in
Peng, J., Kristjansson, D., and Zorin, D.
"Interactive Modeling of Topologically Complex Geometric Detail" Proc. SIGGRAPH, 2004.
*/
void TriMesh::inflate(TriMesh *mesh, float amount)
{
if(normals.empty())
mesh->need_normals();
TriMesh::dprintf("Creating offset surface... ");
int nv = mesh->vertices.size();
for (int i = 0; i < nv; i++)
mesh->vertices[i] += amount * mesh->normals[i];
TriMesh::dprintf("Done.\n");
mesh->bsphere.valid = false;
}
//-------------------------------Edge flip operation----------------------------//
// Cosine of the maximum angle in triangle (v1,v2,v3)
static float cosmaxangle(const point &v1, const point &v2, const point &v3)
{
float a = dist(v2,v3);
float b = dist(v3,v1);
float c = dist(v1,v2);
float A = a * (b*b + c*c - a*a);
float B = b * (c*c + a*a - b*b);
float C = c * (a*a + b*b - c*c);
return 0.5f * __min(__min(A,B),C) / (a*b*c); // min cosine == max angle
}
/*
v1 v4 v1 v4
+-------+ +-------+
\ . \ ? \ . \
\ . \ ---> \ . \
\ . \ \ . \
+-------+ +-------+
v2 v3 v2 v3
Given a pair of triangles (v1,v2,v3) and (v1,v3,v4), what is the benefit
of re-triangulating as (v1,v2,v4) and (v2,v3,v4)?
In the current incarnation, makes sure we don't create a kink
(a dihedral angle smaller than 90 degrees), then tries to minimize the
maximum angle (which seems to work better than minimizing length of the
diagonal and maximizing minimum angle (Delaunay)). Note that, unlike
the Delaunay case, this isn't guaranteed to converge to the global optimum.
*/
static float flip_benefit(const point &v1, const point &v2,
const point &v3, const point &v4)
{
vec n124 = (v4 - v2) CROSS (v1 - v2);
vec n234 = (v3 - v2) CROSS (v4 - v2);
if ((n124 DOT n234) <= 0.0f)
return 0.0f;
return __max(-cosmaxangle(v1,v2,v3), -cosmaxangle(v1,v3,v4)) -
__max(-cosmaxangle(v1,v2,v4), -cosmaxangle(v2,v3,v4));
}
// Given a mesh edge defined as a (face, whichedge) pair, figure out whether
// it is possible and desirable to do an edge flip. This does some sanity
// checks, figures out the four vertices involved, then calls the above
// function to actually compute the benefit.
static float flip_benefit(const TriMesh *mesh, int f, int e)
{
int ae = mesh->across_edge[f][e];
if (ae < 0)
return 0;
int v2 = mesh->faces[f][e];
int v3 = mesh->faces[f][(e+1)%3];
int v1 = mesh->faces[f][(e+2)%3];
int faei3 = mesh->faces[ae].indexof(v3);
if (mesh->across_edge[ae][(faei3+1)%3] != f) {
fprintf(stderr, "AAAAAAARGH!\n");
fprintf(stderr, "f = %d: %d %d %d\n", f, v1, v2, v3);
fprintf(stderr, "ae = %d\n", ae);
fprintf(stderr, "aef = %d %d %d\n", mesh->faces[ae][0], mesh->faces[ae][1], mesh->faces[ae][2]);
fprintf(stderr, "faei3 = %d\n", faei3);
fprintf(stderr, "aeae = %d %d %d\n", mesh->across_edge[ae][0], mesh->across_edge[ae][1], mesh->across_edge[ae][2]);
return 0; // Topological weirdness...
}
int v4 = mesh->faces[ae][(faei3+1)%3];
if (v2 == v4)
return 0;
return flip_benefit(mesh->vertices[v1], mesh->vertices[v2],
mesh->vertices[v3], mesh->vertices[v4]);
}
// Do an edge flip of edge e on face f, updating the across_edge map.
// Returns the other face involved in the flip.
int edge_flip(TriMesh *mesh, int f1, int e)
{
int f2 = mesh->across_edge[f1][e];
int v2 = mesh->faces[f1][e];
int v3 = mesh->faces[f1][(e+1)%3];
int f2i3 = mesh->faces[f2].indexof(v3);
int v4 = mesh->faces[f2][(f2i3+1)%3];
int ae14 = mesh->across_edge[f2][f2i3];
int ae23 = mesh->across_edge[f1][(e+2)%3];
mesh->faces[f1][(e+1)%3] = v4;
mesh->faces[f2][(f2i3+2)%3] = v2;
mesh->across_edge[f1][e] = ae14;
mesh->across_edge[f1][(e+2)%3] = f2;
mesh->across_edge[f2][f2i3] = f1;
mesh->across_edge[f2][(f2i3+1)%3] = ae23;
int ae14iv4 = (ae14 >= 0) ? mesh->faces[ae14].indexof(v4) : -1;
if (ae14iv4 >= 0 && mesh->across_edge[ae14][(ae14iv4+1)%3] == f2)
mesh->across_edge[ae14][(ae14iv4+1)%3] = f1;
int ae23iv2 = (ae23 >= 0) ? mesh->faces[ae23].indexof(v2) : -1;
if (ae23iv2 >= 0 && mesh->across_edge[ae23][(ae23iv2+1)%3] == f1)
mesh->across_edge[ae23][(ae23iv2+1)%3] = f2;
return f2;
}
typedef pair<int,int> TriMeshEdge; // (face, edge) pair
typedef pair<float, TriMeshEdge> TriMeshEdgeWithBenefit;
// Do as many edge flips as necessary...
void TriMesh::edgeflip(TriMesh *mesh)
{
if(mesh->faces.empty())
mesh->need_faces();
mesh->tstrips.clear();
if(mesh->across_edge.empty())
mesh->need_across_edge();
//TriMesh::dprintf("Flipping edges... ");
// Find edges that need to be flipped, and insert them into
// the to-do list
int i, j;
int nf = mesh->faces.size();
priority_queue<TriMeshEdgeWithBenefit> todo;
for (i = 0; i < nf; i++) {
for (j = 0; j < 3; j++) {
float b = flip_benefit(mesh, i, j);
if (b > 0.0f)
todo.push(make_pair(b, make_pair(i, j)));
}
}
// Process things in order of decreasing benefit
while (!todo.empty()) {
int f = todo.top().second.first;
int e = todo.top().second.second;
todo.pop();
// Re-check in case the mesh has changed under us
if (flip_benefit(mesh, f, e) <= 0.0f)
continue;
// OK, do the edge flip
int f2 = edge_flip(mesh, f, e);
// Insert new edges into queue, if necessary
for (j = 0; j < 3; j++) {
float b = flip_benefit(mesh, f, j);
if (b > 0.0f)
todo.push(make_pair(b, make_pair(f, j)));
}
for (j = 0; j < 3; j++) {
float b = flip_benefit(mesh, f2, j);
if (b > 0.0f)
todo.push(make_pair(b, make_pair(f2, j)));
}
}
//TriMesh::dprintf("Done.\n");
}
//Flip the order of vertices in each face. Turns the mesh inside out.
void TriMesh::faceflip(TriMesh *mesh)
{
if(mesh->faces.empty())
mesh->need_faces();
mesh->tstrips.clear();
//TriMesh::dprintf("Flipping faces... ");
int nf = mesh->faces.size();
for (int i = 0; i < nf; i++)
swap(mesh->faces[i][0], mesh->faces[i][2]);
//TriMesh::dprintf("Done.\n");
}
void TriMesh::vtxnormflip(TriMesh * mesh)
{
if(mesh->normals.empty())
return;
int nv = mesh->normals.size();
for(int i = 0; i < nv; ++i)
mesh->normals[i] = -mesh->normals[i];
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?