forked from ianyyin/artoolkit5
-
Notifications
You must be signed in to change notification settings - Fork 29
/
arFilterTransMat.c
181 lines (164 loc) · 5.4 KB
/
arFilterTransMat.c
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
/*
* arFilterTransMat.c
* ARToolKit5
*
* This file is part of ARToolKit.
*
* ARToolKit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARToolKit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ARToolKit. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent modules, and to
* copy and distribute the resulting executable under terms of your choice,
* provided that you also meet, for each linked independent module, the terms and
* conditions of the license of that module. An independent module is a module
* which is neither derived from nor based on this library. If you modify this
* library, you may extend this exception to your version of the library, but you
* are not obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* Copyright 2015 Daqri, LLC.
* Copyright 2010-2015 ARToolworks, Inc.
*
* Author(s): Philip Lamb
*
*/
#include <AR/arFilterTransMat.h>
struct _ARFilterTransMatInfo {
ARdouble alpha;
ARdouble q[4];
ARdouble p[3];
};
#define MAX(x,y) (x > y ? x : y)
#define MIN(x,y) (x < y ? x : y)
#define CLAMP(x,r1,r2) (MIN(MAX(x,r1),r2))
ARFilterTransMatInfo *arFilterTransMatInit(const ARdouble sampleRate, const ARdouble cutoffFreq)
{
ARFilterTransMatInfo *ftmi = (ARFilterTransMatInfo *)malloc(sizeof(ARFilterTransMatInfo));
if (ftmi) {
#ifdef ARDOUBLE_IS_FLOAT
ftmi->q[0] = 0.0f;
ftmi->q[1] = 0.0f;
ftmi->q[2] = 0.0f;
ftmi->q[3] = 1.0f;
ftmi->p[0] = 0.0f;
ftmi->p[1] = 0.0f;
ftmi->p[2] = 0.0f;
#else
ftmi->q[0] = 0.0;
ftmi->q[1] = 0.0;
ftmi->q[2] = 0.0;
ftmi->q[3] = 1.0;
ftmi->p[0] = 0.0;
ftmi->p[1] = 0.0;
ftmi->p[2] = 0.0;
#endif
if (arFilterTransMatSetParams(ftmi, sampleRate, cutoffFreq) < 0) {
free (ftmi);
ftmi = NULL;
}
}
return (ftmi);
}
int arFilterTransMatSetParams(ARFilterTransMatInfo *ftmi, const ARdouble sampleRate, const ARdouble cutoffFreq)
{
ARdouble dt, RC;
if (!ftmi) return (-1);
if (!sampleRate || !cutoffFreq) return (-2);
#ifdef ARDOUBLE_IS_FLOAT
dt = 1.0f / sampleRate;
RC = 1.0f / cutoffFreq;
#else
dt = 1.0 / sampleRate;
RC = 1.0 / cutoffFreq;
#endif
ftmi->alpha = dt / (dt + RC);
return (0);
}
int arFilterTransMat(ARFilterTransMatInfo *ftmi, ARdouble m[3][4], const int reset)
{
ARdouble q[4], p[3], alpha, oneminusalpha, omega, cosomega, sinomega, s0, s1;
if (!ftmi) return (-1);
if (arUtilMat2QuatPos((const ARdouble (*)[4])m, q, p) < 0) return (-2);
arUtilQuatNorm(q);
if (reset) {
ftmi->q[0] = q[0];
ftmi->q[1] = q[1];
ftmi->q[2] = q[2];
ftmi->q[3] = q[3];
ftmi->p[0] = p[0];
ftmi->p[1] = p[1];
ftmi->p[2] = p[2];
} else {
alpha = ftmi->alpha;
#ifdef ARDOUBLE_IS_FLOAT
oneminusalpha = 1.0f - alpha;
#else
oneminusalpha = 1.0 - alpha;
#endif
// SLERP for orientation.
cosomega = q[0]*ftmi->q[0] + q[1]*ftmi->q[1] + q[2]*ftmi->q[2] + q[3]*ftmi->q[3]; // cos of angle between vectors.
#ifdef ARDOUBLE_IS_FLOAT
if (cosomega < 0.0f) {
cosomega = -cosomega;
q[0] = -q[0];
q[1] = -q[1];
q[2] = -q[2];
q[3] = -q[3];
}
if (cosomega > 0.9995f) {
s0 = oneminusalpha;
s1 = alpha;
} else {
omega = acosf(cosomega);
sinomega = sinf(omega);
s0 = sinf(oneminusalpha * omega) / sinomega;
s1 = sinf(alpha * omega) / sinomega;
}
#else
if (cosomega < 0.0) {
cosomega = -cosomega;
q[0] = -q[0];
q[1] = -q[1];
q[2] = -q[2];
q[3] = -q[3];
}
if (cosomega > 0.9995) {
s0 = oneminusalpha;
s1 = alpha;
} else {
omega = acos(cosomega);
sinomega = sin(omega);
s0 = sin(oneminusalpha * omega) / sinomega;
s1 = sin(alpha * omega) / sinomega;
}
#endif
ftmi->q[0] = q[0]*s1 + ftmi->q[0]*s0;
ftmi->q[1] = q[1]*s1 + ftmi->q[1]*s0;
ftmi->q[2] = q[2]*s1 + ftmi->q[2]*s0;
ftmi->q[3] = q[3]*s1 + ftmi->q[3]*s0;
arUtilQuatNorm(ftmi->q);
// Linear interpolation for position.
ftmi->p[0] = p[0]*alpha + ftmi->p[0]*oneminusalpha;
ftmi->p[1] = p[1]*alpha + ftmi->p[1]*oneminusalpha;
ftmi->p[2] = p[2]*alpha + ftmi->p[2]*oneminusalpha;
}
if (arUtilQuatPos2Mat(ftmi->q, ftmi->p, m) < 0) return (-2);
return (0);
}
void arFilterTransMatFinal(ARFilterTransMatInfo *ftmi)
{
if (!ftmi) return;
free (ftmi);
}