1 /** \file elbeem/intern/mvmcoords.cpp
2  *  \ingroup elbeem
3  */
4 /******************************************************************************
5  *
6 // El'Beem - the visual lattice boltzmann freesurface simulator
7 // All code distributed as part of El'Beem is covered by the version 2 of the
8 // GNU General Public License. See the file COPYING for details.
9 //
10 // Copyright 2008 Nils Thuerey , Richard Keiser, Mark Pauly, Ulrich Ruede
11 //
12  *
13  * Mean Value Mesh Coords class
14  *
15  *****************************************************************************/
17 #include "mvmcoords.h"
18 #include <algorithm>
20 #if defined(_MSC_VER) && _MSC_VER > 1600
21 // std::greater
22 #include <functional>
23 #endif
26 using std::vector;
28 void MeanValueMeshCoords::clear()
29 {
30         mVertices.resize(0);
31         mNumVerts = 0;
32 }
34 void MeanValueMeshCoords::calculateMVMCs(vector<ntlVec3Gfx> &reference_vertices, vector<ntlTriangle> &tris,
35                 vector<ntlVec3Gfx> &points, gfxReal numweights)
36 {
37         clear();
38         mvmTransferPoint tds;
39         int mem = 0;
40         int i = 0;
42         mNumVerts = (int)reference_vertices.size();
44         for (vector<ntlVec3Gfx>::iterator iter = points.begin(); iter != points.end(); ++iter, ++i) {
45                 /*
46                 if(i%(points.size()/10)==1) debMsgStd("MeanValueMeshCoords::calculateMVMCs",DM_MSG,"Computing weights, points: "<<i<<"/"<<points.size(),5 );
47                 */
48                 tds.lastpos = *iter;
49                 tds.weights.resize(0);  // clear
50                 computeWeights(reference_vertices, tris, tds, numweights);
51                 mem += (int)tds.weights.size();
52                 mVertices.push_back(tds);
53         }
54         int mbmem = mem * sizeof(mvmFloat) / (1024*1024);
55         debMsgStd("MeanValueMeshCoords::calculateMVMCs",DM_MSG,"vertices:"<<mNumVerts<<" points:"<<points.size()<<" weights:"<<mem<<", wmem:"<<mbmem<<"MB ",7 );
56 }
58 // from: mean value coordinates for closed triangular meshes
59 // attention: fails if a point is exactly (or very close) to a vertex
60 void MeanValueMeshCoords::computeWeights(vector<ntlVec3Gfx> &reference_vertices, vector<ntlTriangle>& tris,
61                 mvmTransferPoint& tds, gfxReal numweights)
62 {
63         const bool mvmFullDebug=false;
64         //const ntlVec3Gfx cEPS = 1.0e-6;
65         const mvmFloat cEPS = 1.0e-14;
67         //mvmFloat d[3], s[3], phi[3],c[3];
68         ntlVec3d u[3],c,d,s,phi;
69         int indices[3];
71         for (int i = 0; i < (int)reference_vertices.size(); ++i) {
72                 tds.weights.push_back(mvmIndexWeight(i, 0.0));
73         }
75         // for each triangle
76         //for (vector<ntlTriangle>::iterator iter = tris.begin(); iter != tris.end();) {
77         for(int t=0; t<(int)tris.size(); t++) {
79                 for (int i = 0; i < 3; ++i) { //, ++iter) {
80                         indices[i] = tris[t].getPoints()[i];
81                         u[i] = vec2D(reference_vertices[ indices[i] ]-tds.lastpos);
82                         d[i] = normalize(u[i]); //.normalize();
83                         //assert(d[i] != 0.);
84                         if(mvmFullDebug) errMsg("MeanValueMeshCoords::computeWeights","t"<<t<<" i"<<indices[i] //<<" lp"<<tds.lastpos
85                                         <<" v"<<reference_vertices[indices[i]]<<" u"<<u[i]<<" ");
86                         // on vertex!
87                         //? if(d[i]<=0.) continue;
88                 }
89                 //for (int i = 0; i < 3; ++i) { errMsg("III"," "<<i             <<" i"<<indices[i]<<reference_vertices[ indices[i] ] ); }
91                 // arcsin is not needed, see paper
92                 phi[0] = 2.*asin( (mvmFloat)(0.5* norm(u[1]-u[2]) )  );
93                 phi[1] = 2.*asin( (mvmFloat)(0.5* norm(u[0]-u[2]) )  );
94                 phi[2] = 2.*asin( (mvmFloat)(0.5* norm(u[0]-u[1]) )  );
95                 mvmFloat h = (phi[0] + phi[1] + phi[2])*0.5;
96                 if (M_PI-h < cEPS) {
97                         if(mvmFullDebug) errMsg("MeanValueMeshCoords::computeWeights","point on triangle");
98                         tds.weights.resize(0);
99                         tds.weights.push_back( mvmIndexWeight(indices[0], sin(phi[0])*d[1]*d[2]));
100                         tds.weights.push_back( mvmIndexWeight(indices[1], sin(phi[1])*d[0]*d[2]));
101                         tds.weights.push_back( mvmIndexWeight(indices[2], sin(phi[2])*d[1]*d[0]));
102                         break;
103                 }
104                 mvmFloat sinh = 2.*sin(h);
105                 c[0] = (sinh*sin(h-phi[0]))/(sin(phi[1])*sin(phi[2]))-1.;
106                 c[1] = (sinh*sin(h-phi[1]))/(sin(phi[0])*sin(phi[2]))-1.;
107                 c[2] = (sinh*sin(h-phi[2]))/(sin(phi[0])*sin(phi[1]))-1.;
108                 if(mvmFullDebug) errMsg("MeanValueMeshCoords::computeWeights","c="<<c<<" phi="<<phi<<" d="<<d);
109                 //if (c[0] > 1. || c[0] < 0. || c[1] > 1. || c[1] < 0. || c[2] > 1. || c[2] < 0.) continue;
111                 s[0] = sqrt((float)(1.-c[0]*c[0]));
112                 s[1] = sqrt((float)(1.-c[1]*c[1]));
113                 s[2] = sqrt((float)(1.-c[2]*c[2]));
115                 if(mvmFullDebug) errMsg("MeanValueMeshCoords::computeWeights","s");
116                 if (s[0] <= cEPS || s[1] <= cEPS || s[2] <= cEPS) {
117                         //MSG("position lies outside the triangle on the same plane -> ignore it");
118                         continue;
119                 }
120                 const mvmFloat u0x = u[0][0];
121                 const mvmFloat u0y = u[0][1];
122                 const mvmFloat u0z = u[0][2];
123                 const mvmFloat u1x = u[1][0];
124                 const mvmFloat u1y = u[1][1];
125                 const mvmFloat u1z = u[1][2];
126                 const mvmFloat u2x = u[2][0];
127                 const mvmFloat u2y = u[2][1];
128                 const mvmFloat u2z = u[2][2];
129                 mvmFloat det = u0x*u1y*u2z - u0x*u1z*u2y + u0y*u1z*u2x - u0y*u1x*u2z + u0z*u1x*u2y - u0z*u1y*u2x;
130                 //assert(det != 0.);
131                 if (det < 0.) {
132                         s[0] = -s[0];
133                         s[1] = -s[1];
134                         s[2] = -s[2];
135                 }
137                 tds.weights[indices[0]].weight += (phi[0]-c[1]*phi[2]-c[2]*phi[1])/(d[0]*sin(phi[1])*s[2]);
138                 tds.weights[indices[1]].weight += (phi[1]-c[2]*phi[0]-c[0]*phi[2])/(d[1]*sin(phi[2])*s[0]);
139                 tds.weights[indices[2]].weight += (phi[2]-c[0]*phi[1]-c[1]*phi[0])/(d[2]*sin(phi[0])*s[1]);
140                 if(mvmFullDebug) { errMsg("MeanValueMeshCoords::computeWeights","i"<<indices[0]<<" o"<<tds.weights[indices[0]].weight);
141                 errMsg("MeanValueMeshCoords::computeWeights","i"<<indices[1]<<" o"<<tds.weights[indices[1]].weight);
142                 errMsg("MeanValueMeshCoords::computeWeights","i"<<indices[2]<<" o"<<tds.weights[indices[2]].weight);
143                 errMsg("MeanValueMeshCoords::computeWeights","\n\n\n"); }
144         }
146         //sort weights
147         if((numweights>0.)&& (numweights<1.) ) {
148         //if( ((int)tds.weights.size() > maxNumWeights) && (maxNumWeights > 0) ) {
149           int maxNumWeights = (int)(tds.weights.size()*numweights);
150                 if(maxNumWeights<=0) maxNumWeights = 1;
151                 std::sort(tds.weights.begin(), tds.weights.end(), std::greater<mvmIndexWeight>());
152                 // only use maxNumWeights-th largest weights
153                 tds.weights.resize(maxNumWeights);
154         }
156         // normalize weights
157         mvmFloat totalWeight = 0.;
158         for (vector<mvmIndexWeight>::const_iterator witer = tds.weights.begin();
159                         witer != tds.weights.end(); ++witer) {
160                 totalWeight += witer->weight;
161         }
162         mvmFloat invTotalWeight;
163         if (totalWeight == 0.) {
164                 if(mvmFullDebug) errMsg("MeanValueMeshCoords::computeWeights","totalWeight == 0");
165                 invTotalWeight = 0.0;
166         } else {
167                 invTotalWeight = 1.0/totalWeight;
168         }
170         for (vector<mvmIndexWeight>::iterator viter = tds.weights.begin();
171                         viter != tds.weights.end(); ++viter) {
172                 viter->weight *= invTotalWeight;
173                 //assert(finite(viter->weight) != 0);
174                 if(!finite(viter->weight)) viter->weight=0.;
175         }
176 }
178 void MeanValueMeshCoords::transfer(vector<ntlVec3Gfx> &vertices, vector<ntlVec3Gfx>& displacements)
179 {
180         displacements.resize(0);
182         //debMsgStd("MeanValueMeshCoords::transfer",DM_MSG,"vertices:"<<mNumVerts<<" curr_verts:"<<vertices.size()<<" ",7 );
183         if((int)vertices.size() != mNumVerts) {
184                 errMsg("MeanValueMeshCoords::transfer","Different no of verts: "<<vertices.size()<<" vs "<<mNumVerts);
185                 return;
186         }
188         for (vector<mvmTransferPoint>::iterator titer = mVertices.begin(); titer != mVertices.end(); ++titer) {
189                 mvmTransferPoint &tds = *titer;
190                 ntlVec3Gfx newpos(0.0);
192                 for (vector<mvmIndexWeight>::iterator witer = tds.weights.begin();
193                                 witer != tds.weights.end(); ++witer) {
194                         newpos += vertices[witer->index] * witer->weight;
195                         //errMsg("transfer","np"<<newpos<<" v"<<vertices[witer->index]<<" w"<< witer->weight);
196                 }
198                 displacements.push_back(newpos);
199                 //displacements.push_back(newpos - tds.lastpos);
200                 //tds.lastpos = newpos;
201         }
202 }