-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
100 lines (87 loc) · 1.86 KB
/
utils.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
#include "mex.h"
#include "math.h"
#include "matrix.h"
#include <stdlib.h>
extern const double angle_bound = 1.85;
extern const int max_len = 20;
double* getV(double* V, int i)
{
return V + i*3;
}
int* getV(int* V, int i)
{
return V + i*3;
}
double* a_to_b(double* a, double* b)
{
double* vec = (double*) mxMalloc(3*sizeof(double));
vec[0] = b[0] - a[0];
vec[1] = b[1] - a[1];
vec[2] = b[2] - a[2];
return vec;
}
double dist(double* a, double* b)
{
double dx = a[0] - b[0];
double dy = a[1] - b[1];
double dz = a[2] - b[2];
return sqrt(dx*dx + dy*dy + dz*dz);
}
double norm(double* a)
{
if (a == NULL) {return 0;}
return sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
}
double dot_product(double* a, double* b)
{
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}
double cal_cos(double* a, double* b)
{
return dot_product(a,b)/(norm(a)*norm(b));
}
double cal_cos_xy(double* a, double* b)
{
double* c = (double*) mxMalloc(3*sizeof(double));
double* d = (double*) mxMalloc(3*sizeof(double));
c[0] = a[0];c[1] = 0.0;c[2] = a[2];
d[0] = b[0];d[1] = 0.0;d[2] = b[2];
return cal_cos(c,d);
}
double cal_angle(double* point, double* center, double center_dist, double* std_vec, double norm_std_vec, double angle_bound)
{
double* vec = a_to_b(center,point);
double angle = acos(dot_product(vec,std_vec)/(center_dist*norm_std_vec));
angle = fmax(fmin(angle,angle_bound),-angle_bound);
return angle;
}
double fnval(const mxArray* pp, double angle)
{
mxArray* ans[1];
mxArray* input[2];
input[0] = (mxArray*) pp;
input[1] = mxCreateDoubleScalar(angle);
mexCallMATLAB(1,ans,2,input,"fnval");
return mxGetScalar(ans[0]);
}
int* double_capacity(int* a, int len)
{
int* b = (int*) mxMalloc(len*2*sizeof(int));
for (int i = 0;i < len;i++)
{
b[i] = a[i];
}
return b;
}
int d2i(double a)
{
int b = int(a);
if (fabs(double(b)-a) < 0.01)
{
return b;
}
else
{
return b+1;
}
}