-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdistance.h
291 lines (273 loc) · 9.2 KB
/
distance.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#pragma once
#include <utility>
#include <vector>
#include <Eigen/Core>
#include "deformerConst.h"
using namespace Eigen;
typedef SparseMatrix<double> SpMat;
typedef Triplet<double> T;
class Distance {
public:
std::vector< std::vector<double> > distPts, distTet; // [i,j]-entry is the distance to the i-th handle to j-th element
std::vector<int> closestPts, closestTet; // [i]-entry is the index of the closest element to i-th handle
int nHdl, nPts, nTet;
Distance(){};
Distance(int _nHandle, int _nPts, int _nTet) {
setNum(_nHandle, _nPts, _nTet);
};
void setNum(int _nHandle, int _nPts, int _nTet);
void findClosestPts();
void findClosestTet();
void computeCageDistPts(short cageMode, const std::vector<Vector3d>& pts, const std::vector<Vector3d>& cagePts, const std::vector<int>& cageTetList);
void computeCageDistTet(short cageMode, const std::vector<Vector3d>& tetCenter, const std::vector<Vector3d>& cagePts, const std::vector<int>& cageTetList);
void computeDistPts(const std::vector<Vector3d>& pts, const std::vector<Vector3d>& hdlPts);
void computeDistTet(const std::vector<Vector3d>& tetCenter, const std::vector<Vector3d>& hdlPts);
double distPtLin(Vector3d p,Vector3d a,Vector3d b);
double distPtTri(Vector3d p,Vector3d a,Vector3d b,Vector3d c);
void MVC(const std::vector<Vector3d>& pts, const std::vector<Vector3d>& cagePts,
const std::vector<int>& cageFaceList, std::vector< std::vector<double> >& w);
void normaliseWeight(short mode, std::vector<double>& w);
};
// initialise
void Distance::setNum(int _nHandle, int _nPts, int _nTet){
nHdl = _nHandle;
nPts = _nPts;
nTet = _nTet;
distPts.resize(nHdl);
distTet.resize(nHdl);
closestPts.resize(nHdl);
closestTet.resize(nHdl);
for(int i=0;i<nHdl;i++){
distPts[i].resize(nPts);
distTet[i].resize(nTet);
}
}
// distance between probe handles and mesh pts
void Distance::computeDistPts(const std::vector<Vector3d>& pts, const std::vector<Vector3d>& hdlPts){
for(int i=0;i<nHdl;i++){
for(int j=0;j<nPts;j++){
distPts[i][j] = (pts[j]-hdlPts[i]).norm();
}
}
}
// distance between probe handles and mesh tet
void Distance::computeDistTet(const std::vector<Vector3d>& tetCenter, const std::vector<Vector3d>& hdlPts){
for(int i=0;i<nHdl;i++){
for(int j=0;j<nTet;j++){
distTet[i][j] = (tetCenter[j]-hdlPts[i]).norm();
}
}
}
// distance between cage and mesh pts
void Distance::computeCageDistPts(short cageMode, const std::vector<Vector3d>& pts, const std::vector<Vector3d>& cagePts, const std::vector<int>& cageTetList){
switch (cageMode){
case TM_FACE:
{
for(int j=0;j<nPts;j++){
for(int i=0;i<nHdl;i++){
Vector3d a=cagePts[cageTetList[4*i]];
Vector3d b=cagePts[cageTetList[4*i+1]];
Vector3d c=cagePts[cageTetList[4*i+2]];
distPts[i][j] = distPtTri(pts[j], a,b,c);
}
}
break;
}
case TM_EDGE:
{
for(int j=0;j<nPts;j++){
for(int i=0;i<nHdl;i++){
Vector3d a=cagePts[cageTetList[4*i]];
Vector3d b=cagePts[cageTetList[4*i+1]];
distPts[i][j] = distPtLin(pts[j], a,b);
}
}
break;
}
case TM_VERTEX:
case TM_VFACE:
{
for(int j=0;j<nPts;j++){
for(int i=0;i<nHdl;i++){
distPts[i][j] = (pts[j]-cagePts[cageTetList[4*i]]).norm();
}
}
break;
}
case CM_MLS_AFF:
case CM_MLS_SIM:
case CM_MLS_RIGID:
{
for(int j=0;j<nPts;j++){
for(int i=0;i<nHdl;i++){
distPts[i][j] = (pts[j]-cagePts[i]).norm();
}
}
break;
}
}
}
// distance between cage and mesh tet
void Distance::computeCageDistTet(short cageMode, const std::vector<Vector3d>& tetCenter, const std::vector<Vector3d>& cagePts, const std::vector<int>& cageTetList){
switch (cageMode){
case TM_FACE:
{
for(int j=0;j<nTet;j++){
for(int i=0;i<nHdl;i++){
Vector3d a=cagePts[cageTetList[4*i]];
Vector3d b=cagePts[cageTetList[4*i+1]];
Vector3d c=cagePts[cageTetList[4*i+2]];
distTet[i][j] = distPtTri(tetCenter[j], a,b,c);
}
}
break;
}
case TM_EDGE:
{
for(int j=0;j<nTet;j++){
for(int i=0;i<nHdl;i++){
Vector3d a=cagePts[cageTetList[4*i]];
Vector3d b=cagePts[cageTetList[4*i+1]];
distTet[i][j] = distPtLin(tetCenter[j], a,b);
}
}
break;
}
case TM_VERTEX:
case TM_VFACE:
{
for(int j=0;j<nTet;j++){
for(int i=0;i<nHdl;i++){
distTet[i][j] = (tetCenter[j]-cagePts[cageTetList[4*i]]).norm();
}
}
break;
}
case CM_MLS_AFF:
case CM_MLS_SIM:
case CM_MLS_RIGID:
{
for(int j=0;j<nTet;j++){
for(int i=0;i<nHdl;i++){
distTet[i][j] = (tetCenter[j]-cagePts[i]).norm();
}
}
break;
}
}
}
// find closest point on mesh from each handle
void Distance::findClosestPts(){
for(int i=0;i<nHdl;i++){
closestPts[i] = 0;
double min_d = HUGE_VAL;
for(int j=0;j<nPts;j++){
if( distPts[i][j] < min_d){
min_d = distPts[i][j];
closestPts[i] = j;
}
}
}
}
// find closest tet on mesh from each handle
void Distance::findClosestTet(){
for(int i=0;i<nHdl;i++){
closestTet[i] = 0;
double min_d = HUGE_VAL;
for(int j=0;j<nTet;j++){
if( distTet[i][j] < min_d){
min_d = distTet[i][j];
closestTet[i] = j;
}
}
}
}
// compute distance between a line segment (ab) and a point p
double Distance::distPtLin(Vector3d p,Vector3d a,Vector3d b){
double t= (a-b).dot(p-b)/(a-b).squaredNorm();
if(t>1){
return (a-p).norm();
}else if(t<0){
return (b-p).norm();
}else{
return (t*(a-b)-(p-b)).norm();
}
}
// compute distance between a triangle (abc) and a point p
double Distance::distPtTri(Vector3d p, Vector3d a, Vector3d b, Vector3d c){
/// if p is in the outer half-space, it returns HUGE_VAL
double s[4];
Vector3d n=(b-a).cross(c-a);
if(n.squaredNorm()<EPSILON){
return (p-a).norm();
}
double k=n.dot(a-p);
if(k<0) return HUGE_VAL;
s[0]=distPtLin(p,a,b);
s[1]=distPtLin(p,b,c);
s[2]=distPtLin(p,c,a);
Matrix3d A;
A << b(0)-a(0), c(0)-a(0), n(0)-a(0),
b(1)-a(1), c(1)-a(1), n(1)-a(1),
b(2)-a(2), c(2)-a(2), n(2)-a(2);
Vector3d v = A.inverse()*(p-a); // barycentric coordinate of p
if(v(0)>0 && v(1)>0 && v(0)+v(1)<1){
s[3]=k;
}else{
s[3] = HUGE_VAL;
}
return min(min(min(s[0],s[1]),s[2]),s[3]);
}
// mean value coordinate
void Distance::MVC(const std::vector<Vector3d>& pts, const std::vector<Vector3d>& cagePts,
const std::vector<int>& cageFaceList, std::vector< std::vector<double> >& w)
{
int numPts=(int) pts.size();
int numCagePts=(int) cagePts.size();
int numFaces=(int) cageFaceList.size()/3;
w.resize(numPts);
#pragma omp parallel for
for(int j=0; j<numPts; j++ ){
w[j].resize(numCagePts);
std::vector<double> mu(numCagePts), a(3), b(3);
std::vector<Vector3d> e(3),n(3);
for(int i=0;i<numFaces;i++){
for(int k=0;k<3;k++)
e[k]=(pts[j]-cagePts[cageFaceList[3*i+k]]).normalized();
for(int k=0;k<3;k++)
n[k]=(e[(k+1)%3].cross(e[(k+2)%3])).normalized();
for(int k=0;k<3;k++){
a[k]=n[(k+1)%3].dot(n[(k+2)%3]);
b[k]=acos(e[(k+1)%3].dot(e[(k+2)%3]));
}
for(int k=0;k<3;k++)
mu[cageFaceList[3*i+k]] -= (b[k]+b[(k+2)%3]*a[(k+1)%3]+b[(k+1)%3]*a[(k+2)%3])/(2.0*e[k].dot(n[k]));
}
double smu=0.0;
for(int i=0;i<numCagePts;i++){
mu[i] /= (pts[j]-cagePts[i]).norm();
smu += mu[i];
}
for(int i=0;i<numCagePts;i++)
w[j][i] = mu[i]/smu;
}
}
// normalise weights
void Distance::normaliseWeight(short mode, std::vector<double>& w){
if(mode == NM_NONE || mode == NM_LINEAR){
double sum = std::accumulate(w.begin(), w.end(), 0.0);
if ((sum > 1 || mode == NM_LINEAR) && sum != 0.0){
for (int i = 0; i < w.size(); i++){
w[i] /= sum;
}
}
}else if(mode == NM_SOFTMAX){
double sum = 0.0;
for (int i = 0; i < w.size(); i++){
sum += exp(w[i]);
}
for (int i = 0; i < w.size(); i++){
w[i] = exp(w[i])/sum;
}
}
}