forked from dmtx/libdmtx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmtxvector2.c
211 lines (177 loc) · 3.36 KB
/
dmtxvector2.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
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
/**
* libdmtx - Data Matrix Encoding/Decoding Library
* Copyright 2008, 2009 Mike Laughton. All rights reserved.
* Copyright 2012-2016 Vadim A. Misbakh-Soloviov. All rights reserved.
*
* See LICENSE file in the main project directory for full
* terms of use and distribution.
*
* Contact:
* Vadim A. Misbakh-Soloviov <[email protected]>
* Mike Laughton <[email protected]>
*
* \file dmtxvector2.c
* \brief 2D Vector math
*/
/**
*
*
*/
extern DmtxVector2 *
dmtxVector2AddTo(DmtxVector2 *v1, const DmtxVector2 *v2)
{
v1->X += v2->X;
v1->Y += v2->Y;
return v1;
}
/**
*
*
*/
extern DmtxVector2 *
dmtxVector2Add(DmtxVector2 *vOut, const DmtxVector2 *v1, const DmtxVector2 *v2)
{
*vOut = *v1;
return dmtxVector2AddTo(vOut, v2);
}
/**
*
*
*/
extern DmtxVector2 *
dmtxVector2SubFrom(DmtxVector2 *v1, const DmtxVector2 *v2)
{
v1->X -= v2->X;
v1->Y -= v2->Y;
return v1;
}
/**
*
*
*/
extern DmtxVector2 *
dmtxVector2Sub(DmtxVector2 *vOut, const DmtxVector2 *v1, const DmtxVector2 *v2)
{
*vOut = *v1;
return dmtxVector2SubFrom(vOut, v2);
}
/**
*
*
*/
extern DmtxVector2 *
dmtxVector2ScaleBy(DmtxVector2 *v, double s)
{
v->X *= s;
v->Y *= s;
return v;
}
/**
*
*
*/
extern DmtxVector2 *
dmtxVector2Scale(DmtxVector2 *vOut, const DmtxVector2 *v, double s)
{
*vOut = *v;
return dmtxVector2ScaleBy(vOut, s);
}
/**
*
*
*/
extern double
dmtxVector2Cross(const DmtxVector2 *v1, const DmtxVector2 *v2)
{
return (v1->X * v2->Y) - (v1->Y * v2->X);
}
/**
*
*
*/
extern double
dmtxVector2Norm(DmtxVector2 *v)
{
double mag;
mag = dmtxVector2Mag(v);
if(mag <= DmtxAlmostZero)
return -1.0; /* XXX this doesn't look clean */
dmtxVector2ScaleBy(v, 1/mag);
return mag;
}
/**
*
*
*/
extern double
dmtxVector2Dot(const DmtxVector2 *v1, const DmtxVector2 *v2)
{
return (v1->X * v2->X) + (v1->Y * v2->Y);
}
/**
*
*
*/
extern double
dmtxVector2Mag(const DmtxVector2 *v)
{
return sqrt(v->X * v->X + v->Y * v->Y);
}
/**
*
*
*/
extern double
dmtxDistanceFromRay2(const DmtxRay2 *r, const DmtxVector2 *q)
{
DmtxVector2 vSubTmp;
/* Assumes that v is a unit vector */
assert(fabs(1.0 - dmtxVector2Mag(&(r->v))) <= DmtxAlmostZero);
return dmtxVector2Cross(&(r->v), dmtxVector2Sub(&vSubTmp, q, &(r->p)));
}
/**
*
*
*/
extern double
dmtxDistanceAlongRay2(const DmtxRay2 *r, const DmtxVector2 *q)
{
DmtxVector2 vSubTmp;
#ifdef DEBUG
/* Assumes that v is a unit vector */
if(fabs(1.0 - dmtxVector2Mag(&(r->v))) > DmtxAlmostZero) {
; /* XXX big error goes here */
}
#endif
return dmtxVector2Dot(dmtxVector2Sub(&vSubTmp, q, &(r->p)), &(r->v));
}
/**
*
*
*/
extern DmtxPassFail
dmtxRay2Intersect(DmtxVector2 *point, const DmtxRay2 *p0, const DmtxRay2 *p1)
{
double numer, denom;
DmtxVector2 w;
denom = dmtxVector2Cross(&(p1->v), &(p0->v));
if(fabs(denom) <= DmtxAlmostZero)
return DmtxFail;
dmtxVector2Sub(&w, &(p1->p), &(p0->p));
numer = dmtxVector2Cross(&(p1->v), &w);
return dmtxPointAlongRay2(point, p0, numer/denom);
}
/**
*
*
*/
extern DmtxPassFail
dmtxPointAlongRay2(DmtxVector2 *point, const DmtxRay2 *r, double t)
{
DmtxVector2 vTmp;
/* Ray should always have unit length of 1 */
assert(fabs(1.0 - dmtxVector2Mag(&(r->v))) <= DmtxAlmostZero);
dmtxVector2Scale(&vTmp, &(r->v), t);
dmtxVector2Add(point, &(r->p), &vTmp);
return DmtxPass;
}