-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcomputeEdgeCost.cpp
201 lines (170 loc) · 8.25 KB
/
computeEdgeCost.cpp
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
// Tutorial:
// https://www.caam.rice.edu/~optimization/L1/optseminar/Getreuer-slides-cmex.pdf
// Usage:
// 1. Compile
// >> mex computeEdgeCost.cpp
// 2. Call the function
// >> computeEdgeCost(R1, R2, xxF, xyF, xzF, yyF, yzF, zzF)
#include <iostream>
#include <math.h>
#include "mex.h"
#define R1_in prhs[0]
#define R2_in prhs[1]
#define xxF_in prhs[2]
#define xyF_in prhs[3]
#define xzF_in prhs[4]
#define yyF_in prhs[5]
#define yzF_in prhs[6]
#define zzF_in prhs[7]
#define cost_out plhs[0]
void DoComputation(double *cost, double *R1, double *R2, double *xxF,
double *xyF, double *xzF, double *yyF, double *yzF, double *zzF)
{
double r1[3], r2[3], r3[3];
r1[0] = R1[0] * R2[0] + R1[3] * R2[3] + R1[6] * R2[6];
r1[1] = R1[0] * R2[1] + R1[3] * R2[4] + R1[6] * R2[7];
r1[2] = R1[0] * R2[2] + R1[3] * R2[5] + R1[6] * R2[8];
r2[0] = R1[1] * R2[0] + R1[4] * R2[3] + R1[7] * R2[6];
r2[1] = R1[1] * R2[1] + R1[4] * R2[4] + R1[7] * R2[7];
r2[2] = R1[1] * R2[2] + R1[4] * R2[5] + R1[7] * R2[8];
r3[0] = R1[2] * R2[0] + R1[5] * R2[3] + R1[8] * R2[6];
r3[1] = R1[2] * R2[1] + R1[5] * R2[4] + R1[8] * R2[7];
r3[2] = R1[2] * R2[2] + R1[5] * R2[5] + R1[8] * R2[8];
double m11 = 0;
double m12 = 0;
double m13 = 0;
double m21 = 0;
double m22 = 0;
double m23 = 0;
double m31 = 0;
double m32 = 0;
double m33 = 0;
// m11 = r3*yyF*r3'-2*r3*yzF*r2'+r2*zzF*r2';
m11 += r3[0]*(r3[0]*yyF[0]+r3[1]*yyF[3]+r3[2]*yyF[6]);
m11 += r3[1]*(r3[0]*yyF[1]+r3[1]*yyF[4]+r3[2]*yyF[7]);
m11 += r3[2]*(r3[0]*yyF[2]+r3[1]*yyF[5]+r3[2]*yyF[8]);
m11 -= 2*r2[0]*(r3[0]*yzF[0]+r3[1]*yzF[3]+r3[2]*yzF[6]);
m11 -= 2*r2[1]*(r3[0]*yzF[1]+r3[1]*yzF[4]+r3[2]*yzF[7]);
m11 -= 2*r2[2]*(r3[0]*yzF[2]+r3[1]*yzF[5]+r3[2]*yzF[8]);
m11 += r2[0]*(r2[0]*zzF[0]+r2[1]*zzF[3]+r2[2]*zzF[6]);
m11 += r2[1]*(r2[0]*zzF[1]+r2[1]*zzF[4]+r2[2]*zzF[7]);
m11 += r2[2]*(r2[0]*zzF[2]+r2[1]*zzF[5]+r2[2]*zzF[8]);
// m22 = r1*zzF*r1'-2*r1*xzF*r3'+r3*xxF*r3';
m22 += r1[0]*(r1[0]*zzF[0]+r1[1]*zzF[3]+r1[2]*zzF[6]);
m22 += r1[1]*(r1[0]*zzF[1]+r1[1]*zzF[4]+r1[2]*zzF[7]);
m22 += r1[2]*(r1[0]*zzF[2]+r1[1]*zzF[5]+r1[2]*zzF[8]);
m22 -= 2*r3[0]*(r1[0]*xzF[0]+r1[1]*xzF[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1[1]*yzF[4]+r1[2]*yzF[7]);
m13 += r2[2]*(r1[0]*yzF[2]+r1[1]*yzF[5]+r1[2]*yzF[8]);
// m23 = r1*xzF*r2'-r1*yzF*r1'-r3*xxF*r2'+r3*xyF*r1';
m23 += r2[0]*(r1[0]*xzF[0]+r1[1]*xzF[3]+r1[2]*xzF[6]);
m23 += r2[1]*(r1[0]*xzF[1]+r1[1]*xzF[4]+r1[2]*xzF[7]);
m23 += r2[2]*(r1[0]*xzF[2]+r1[1]*xzF[5]+r1[2]*xzF[8]);
m23 -= r1[0]*(r1[0]*yzF[0]+r1[1]*yzF[3]+r1[2]*yzF[6]);
m23 -= r1[1]*(r1[0]*yzF[1]+r1[1]*yzF[4]+r1[2]*yzF[7]);
m23 -= r1[2]*(r1[0]*yzF[2]+r1[1]*yzF[5]+r1[2]*yzF[8]);
m23 -= r2[0]*(r3[0]*xxF[0]+r3[1]*xxF[3]+r3[2]*xxF[6]);
m23 -= r2[1]*(r3[0]*xxF[1]+r3[1]*xxF[4]+r3[2]*xxF[7]);
m23 -= r2[2]*(r3[0]*xxF[2]+r3[1]*xxF[5]+r3[2]*xxF[8]);
m23 += r1[0]*(r3[0]*xyF[0]+r3[1]*xyF[3]+r3[2]*xyF[6]);
m23 += r1[1]*(r3[0]*xyF[1]+r3[1]*xyF[4]+r3[2]*xyF[7]);
m23 += r1[2]*(r3[0]*xyF[2]+r3[1]*xyF[5]+r3[2]*xyF[8]);
double b1 = -m11-m22-m33;
double b2 = -m13*m13-m23*m23-m12*m12+m11*m22+m11*m33+m22*m33;
double b3 = m22*m13*m13+m11*m23*m23+m33*m12*m12-m11*m22*m33-2*m12*m23*m13;
double s = 0.5*(2*b1*b1*b1-9*b1*b2+27*b3);
double t = sqrt(pow(b1*b1-3*b2, 3));
double k = pow(t, 1.0/3.0)*cos(acos(s/t)/3);
double lambda = (-b1-2*k)/3;
if (lambda < 0)
lambda = -lambda;
cost[0] = sqrt(lambda);
}
/* The gateway function. */
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
double *cost, *R1, *R2, *xxF, *xyF, *xzF, *yyF, *yzF, *zzF;
/*** Check Inputs **/
if(nrhs != 8)
mexErrMsgTxt("Must have 8 input arguments.");
if(nlhs != 1)
mexErrMsgTxt("Must have 1 output arguments.");
if(mxIsComplex(R1_in) || mxGetNumberOfDimensions(R1_in) != 2
|| mxIsSparse(R1_in) || !mxIsDouble(R1_in))
mexErrMsgTxt("Sorry! R1 must be a real 2D full double matrix.");
if(mxIsComplex(R2_in) || mxGetNumberOfDimensions(R2_in) != 2
|| mxIsSparse(R2_in) || !mxIsDouble(R2_in))
mexErrMsgTxt("Sorry! R2 must be a real 2D full double matrix.");
if(mxIsComplex(xxF_in) || mxGetNumberOfDimensions(xxF_in) != 2
|| mxIsSparse(xxF_in) || !mxIsDouble(xxF_in))
mexErrMsgTxt("Sorry! xxF must be a real 2D full double matrix.");
if(mxIsComplex(xyF_in) || mxGetNumberOfDimensions(xyF_in) != 2
|| mxIsSparse(xyF_in) || !mxIsDouble(xyF_in))
mexErrMsgTxt("Sorry! xyF must be a real 2D full double matrix.");
if(mxIsComplex(xzF_in) || mxGetNumberOfDimensions(xzF_in) != 2
|| mxIsSparse(xzF_in) || !mxIsDouble(xzF_in))
mexErrMsgTxt("Sorry! xzF must be a real 2D full double matrix.");
if(mxIsComplex(yyF_in) || mxGetNumberOfDimensions(yyF_in) != 2
|| mxIsSparse(yyF_in) || !mxIsDouble(yyF_in))
mexErrMsgTxt("Sorry! yyF must be a real 2D full double matrix.");
if(mxIsComplex(yzF_in) || mxGetNumberOfDimensions(yzF_in) != 2
|| mxIsSparse(yzF_in) || !mxIsDouble(yzF_in))
mexErrMsgTxt("Sorry! yzF must be a real 2D full double matrix.");
if(mxIsComplex(zzF_in) || mxGetNumberOfDimensions(zzF_in) != 2
|| mxIsSparse(zzF_in) || !mxIsDouble(zzF_in))
mexErrMsgTxt("Sorry! zzF must be a real 2D full double matrix.");
//
// p = mxGetScalar(P_IN); /* Get the value of p */
/*** Read input ***/
R1 = mxGetPr(R1_in);
R2 = mxGetPr(R2_in);
xxF = mxGetPr(xxF_in);
xyF = mxGetPr(xyF_in);
xzF = mxGetPr(xzF_in);
yyF = mxGetPr(yyF_in);
yzF = mxGetPr(yzF_in);
zzF = mxGetPr(zzF_in);
/*** Create the output matrix ***/
cost_out = mxCreateDoubleMatrix(1,1,mxREAL);
cost = mxGetPr(cost_out);
/* Do the computation */
DoComputation(cost, R1, R2, xxF, xyF, xzF, yyF, yzF, zzF);
}