liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
vector3.h
Go to the documentation of this file.
1
5
6#ifndef LIBA_VECTOR3_H
7#define LIBA_VECTOR3_H
8
9#include "a.h"
10#include "math.h"
11
17
18/* clang-format off */
20#define A_VECTOR3_C(x, y, z) {a_real_c(x), a_real_c(y), a_real_c(z)}
21/* clang-format on */
22
24#define a_vector3_c(x) a_cast_s(a_vector3, x)
25#define a_vector3_(_, x) a_cast_s(a_vector3 _, x)
26
27typedef struct a_vector3 a_vector3;
28
29#if defined(__cplusplus)
30namespace a
31{
32typedef struct a_vector3 vector3;
33} /* namespace a */
34extern "C" {
35#endif /* __cplusplus */
36#if !defined A_HAVE_INLINE || defined(LIBA_VECTOR3_C)
37#undef A_INTERN
38#define A_INTERN A_PUBLIC extern
39#endif /* A_HAVE_INLINE */
40
41A_INTERN a_real a_vector3_set_dir(a_vector3 *ctx, a_real x, a_real y, a_real z);
42A_INTERN void a_vector3_val(a_vector3 const *ctx, a_real *x, a_real *y, a_real *z);
43A_INTERN void a_vector3_set_val(a_vector3 *ctx, a_real x, a_real y, a_real z);
44A_INTERN void a_vector3_pol(a_vector3 const *ctx, a_real *rho, a_real *theta, a_real *z);
45A_INTERN void a_vector3_set_pol(a_vector3 *ctx, a_real rho, a_real theta, a_real z);
46A_INTERN void a_vector3_sph(a_vector3 const *ctx, a_real *rho, a_real *theta, a_real *alpha);
47A_INTERN void a_vector3_set_sph(a_vector3 *ctx, a_real rho, a_real theta, a_real alpha);
48
49A_INTERN void a_vector3_add(a_vector3 const *lhs, a_vector3 const *rhs, a_vector3 *res);
50A_INTERN void a_vector3_sub(a_vector3 const *lhs, a_vector3 const *rhs, a_vector3 *res);
51A_INTERN void a_vector3_mul(a_vector3 const *lhs, a_real rhs, a_vector3 *res);
52A_INTERN void a_vector3_div(a_vector3 const *lhs, a_real rhs, a_vector3 *res);
53A_INTERN void a_vector3_neg(a_vector3 const *ctx, a_vector3 *res);
54
55A_EXTERN a_real a_vector3_unit(a_vector3 *ctx);
56A_EXTERN a_real a_vector3_norm(a_vector3 const *ctx);
57A_EXTERN a_real a_vector3_norm1(a_vector3 const *ctx);
58A_EXTERN a_real a_vector3_norm2(a_vector3 const *ctx);
59A_EXTERN a_real a_vector3_dot(a_vector3 const *lhs, a_vector3 const *rhs);
60A_EXTERN a_real a_vector3_dist(a_vector3 const *lhs, a_vector3 const *rhs);
61A_EXTERN a_real a_vector3_dist1(a_vector3 const *lhs, a_vector3 const *rhs);
62A_EXTERN a_real a_vector3_dist2(a_vector3 const *lhs, a_vector3 const *rhs);
63A_EXTERN a_real a_vector3_angle(a_vector3 const *lhs, a_vector3 const *rhs);
64A_EXTERN a_bool a_vector3_isver(a_vector3 const *lhs, a_vector3 const *rhs);
65A_EXTERN a_bool a_vector3_ispar(a_vector3 const *lhs, a_vector3 const *rhs);
66A_EXTERN void a_vector3_cross(a_vector3 const *lhs, a_vector3 const *rhs, a_vector3 *res);
67A_EXTERN void a_vector3_outer(a_vector3 const *lhs, a_vector3 const *rhs, a_real res[9]);
68A_EXTERN int a_vector3_ortho(a_vector3 const *ctx, a_vector3 *u, a_vector3 *v);
69A_EXTERN void a_vector3_rot_(a_vector3 const *ctx, a_vector3 const *dir, a_real sin, a_real cos, a_vector3 *res);
70A_EXTERN void a_vector3_rot(a_vector3 const *ctx, a_vector3 const *dir, a_real angle, a_vector3 *res);
71A_EXTERN void a_vector3_rot2d(a_vector3 const *iu, a_vector3 const *iv, a_real angle,
72 a_vector3 *ou, a_vector3 *ov);
73
74#if !defined A_HAVE_INLINE || defined(LIBA_VECTOR3_C)
75#undef A_INTERN
76#define A_INTERN static A_INLINE
77#endif /* A_HAVE_INLINE */
78#if defined(__cplusplus)
79} /* extern "C" */
80#endif /* __cplusplus */
81
82struct a_point3;
87{
88 a_real x, y, z;
89#if defined(__cplusplus)
90 A_INLINE void set(a_point3 const &p, a_point3 const &q);
91 A_INLINE a_real set_dir(a_real x_, a_real y_, a_real z_)
92 {
93 return a_vector3_set_dir(this, x_, y_, z_);
94 }
95 A_INLINE void val(a_real &x_, a_real &y_, a_real &z_) const
96 {
97 a_vector3_val(this, &x_, &y_, &z_);
98 }
99 A_INLINE void set_val(a_real x_, a_real y_, a_real z_)
100 {
101 a_vector3_set_val(this, x_, y_, z_);
102 }
103 A_INLINE void pol(a_real &rho, a_real &theta, a_real &z_) const
104 {
105 a_vector3_pol(this, &rho, &theta, &z_);
106 }
107 A_INLINE void set_pol(a_real rho, a_real theta, a_real z_)
108 {
109 a_vector3_set_pol(this, rho, theta, z_);
110 }
111 A_INLINE void sph(a_real &rho, a_real &theta, a_real &alpha) const
112 {
113 a_vector3_sph(this, &rho, &theta, &alpha);
114 }
115 A_INLINE void set_sph(a_real rho, a_real theta, a_real alpha)
116 {
117 a_vector3_set_sph(this, rho, theta, alpha);
118 }
119 A_INLINE void add(a_vector3 const &rhs, a_vector3 &res) const
120 {
121 a_vector3_add(this, &rhs, &res);
122 }
123 A_INLINE void sub(a_vector3 const &rhs, a_vector3 &res) const
124 {
125 a_vector3_sub(this, &rhs, &res);
126 }
127 A_INLINE void mul(a_real rhs, a_vector3 &res) const
128 {
129 a_vector3_mul(this, rhs, &res);
130 }
131 A_INLINE void div(a_real rhs, a_vector3 &res) const
132 {
133 a_vector3_div(this, rhs, &res);
134 }
135 A_INLINE void neg(a_vector3 &res) const
136 {
137 a_vector3_neg(this, &res);
138 }
139 A_INLINE a_real unit() { return a_vector3_unit(this); }
140 A_INLINE a_real norm() const { return a_vector3_norm(this); }
141 A_INLINE a_real norm1() const { return a_vector3_norm1(this); }
142 A_INLINE a_real norm2() const { return a_vector3_norm2(this); }
143 A_INLINE a_real dot(a_vector3 const &rhs) const { return a_vector3_dot(this, &rhs); }
144 A_INLINE a_real dist(a_vector3 const &rhs) const { return a_vector3_dist(this, &rhs); }
145 A_INLINE a_real dist1(a_vector3 const &rhs) const { return a_vector3_dist1(this, &rhs); }
146 A_INLINE a_real dist2(a_vector3 const &rhs) const { return a_vector3_dist2(this, &rhs); }
147 A_INLINE a_real angle(a_vector3 const &rhs) const { return a_vector3_angle(this, &rhs); }
148 A_INLINE a_bool isver(a_vector3 const &rhs) const { return a_vector3_isver(this, &rhs); }
149 A_INLINE a_bool ispar(a_vector3 const &rhs) const { return a_vector3_ispar(this, &rhs); }
150 A_INLINE void cross(a_vector3 const &rhs, a_vector3 &res) const
151 {
152 a_vector3_cross(this, &rhs, &res);
153 }
154 A_INLINE void outer(a_vector3 const &rhs, a_real res[9]) const
155 {
156 a_vector3_outer(this, &rhs, res);
157 }
158 A_INLINE int ortho(a_vector3 &u, a_vector3 &v) const
159 {
160 return a_vector3_ortho(this, &u, &v);
161 }
162 A_INLINE void rot(a_vector3 const &dir, a_real angle, a_vector3 &res) const
163 {
164 a_vector3_rot(this, &dir, angle, &res);
165 }
166 static A_INLINE void rot2d(a_vector3 const &iu, a_vector3 const &iv, a_real angle,
167 a_vector3 &ou, a_vector3 &ov)
168 {
169 a_vector3_rot2d(&iu, &iv, angle, &ou, &ov);
170 }
171 friend A_INLINE a_vector3 operator^(a_vector3 const &lhs, a_vector3 const &rhs)
172 {
173 a_vector3 res;
174 a_vector3_cross(&lhs, &rhs, &res);
175 return res;
176 }
177 friend A_INLINE void operator+=(a_vector3 &lhs, a_vector3 const &rhs) { a_vector3_add(&lhs, &rhs, &lhs); }
178 friend A_INLINE a_vector3 operator+(a_vector3 const &lhs, a_vector3 const &rhs)
179 {
180 a_vector3 res;
181 a_vector3_add(&lhs, &rhs, &res);
182 return res;
183 }
184 friend A_INLINE void operator-=(a_vector3 &lhs, a_vector3 const &rhs) { a_vector3_sub(&lhs, &rhs, &lhs); }
185 friend A_INLINE a_vector3 operator-(a_vector3 const &lhs, a_vector3 const &rhs)
186 {
187 a_vector3 res;
188 a_vector3_sub(&lhs, &rhs, &res);
189 return res;
190 }
191 friend A_INLINE void operator*=(a_vector3 &lhs, a_real rhs) { a_vector3_mul(&lhs, rhs, &lhs); }
192 friend A_INLINE a_real operator*(a_vector3 const &lhs, a_vector3 const &rhs)
193 {
194 return a_vector3_dot(&lhs, &rhs);
195 }
196 friend A_INLINE a_vector3 operator*(a_real lhs, a_vector3 const &rhs)
197 {
198 a_vector3 res;
199 a_vector3_mul(&rhs, lhs, &res);
200 return res;
201 }
202 friend A_INLINE a_vector3 operator*(a_vector3 const &lhs, a_real rhs)
203 {
204 a_vector3 res;
205 a_vector3_mul(&lhs, rhs, &res);
206 return res;
207 }
208 friend A_INLINE void operator/=(a_vector3 &lhs, a_real rhs) { a_vector3_div(&lhs, rhs, &lhs); }
209 friend A_INLINE a_vector3 operator/(a_vector3 const &lhs, a_real rhs)
210 {
211 a_vector3 res;
212 a_vector3_div(&lhs, rhs, &res);
213 return res;
214 }
215 friend A_INLINE a_vector3 operator+(a_vector3 const &rhs) { return rhs; }
216 friend A_INLINE a_vector3 operator-(a_vector3 const &rhs)
217 {
218 a_vector3 res;
219 a_vector3_neg(&rhs, &res);
220 return res;
221 }
222#endif /* __cplusplus */
223};
224
225#if defined(LIBA_VECTOR3_C)
226#undef A_INTERN
227#define A_INTERN A_INLINE
228#endif /* LIBA_VECTOR3_C */
229#if defined(A_HAVE_INLINE) || defined(LIBA_VECTOR3_C)
230
231A_INLINE a_real a_vector3_set_dir(a_vector3 *ctx, a_real x, a_real y, a_real z)
232{
233 ctx->x = x;
234 ctx->y = y;
235 ctx->z = z;
236 return a_vector3_unit(ctx);
237}
238A_INTERN void a_vector3_val(a_vector3 const *ctx, a_real *x, a_real *y, a_real *z)
239{
240 *x = ctx->x;
241 *y = ctx->y;
242 *z = ctx->z;
243}
244A_INTERN void a_vector3_set_val(a_vector3 *ctx, a_real x, a_real y, a_real z)
245{
246 ctx->x = x;
247 ctx->y = y;
248 ctx->z = z;
249}
250A_INTERN void a_vector3_pol(a_vector3 const *ctx, a_real *rho, a_real *theta, a_real *z)
251{
252 a_real_cart2pol(ctx->x, ctx->y, rho, theta);
253 *z = ctx->z;
254}
255A_INTERN void a_vector3_set_pol(a_vector3 *ctx, a_real rho, a_real theta, a_real z)
256{
257 a_real_pol2cart(rho, theta, &ctx->x, &ctx->y);
258 ctx->z = z;
259}
260A_INTERN void a_vector3_sph(a_vector3 const *ctx, a_real *rho, a_real *theta, a_real *alpha)
261{
262 a_real_cart2sph(ctx->x, ctx->y, ctx->z, rho, theta, alpha);
263}
264A_INTERN void a_vector3_set_sph(a_vector3 *ctx, a_real rho, a_real theta, a_real alpha)
265{
266 a_real_sph2cart(rho, theta, alpha, &ctx->x, &ctx->y, &ctx->z);
267}
268A_INTERN void a_vector3_add(a_vector3 const *lhs, a_vector3 const *rhs, a_vector3 *res)
269{
270 res->x = lhs->x + rhs->x;
271 res->y = lhs->y + rhs->y;
272 res->z = lhs->z + rhs->z;
273}
274A_INTERN void a_vector3_sub(a_vector3 const *lhs, a_vector3 const *rhs, a_vector3 *res)
275{
276 res->x = lhs->x - rhs->x;
277 res->y = lhs->y - rhs->y;
278 res->z = lhs->z - rhs->z;
279}
280A_INTERN void a_vector3_mul(a_vector3 const *lhs, a_real rhs, a_vector3 *res)
281{
282 res->x = lhs->x * rhs;
283 res->y = lhs->y * rhs;
284 res->z = lhs->z * rhs;
285}
286A_INTERN void a_vector3_div(a_vector3 const *lhs, a_real rhs, a_vector3 *res)
287{
288 res->x = lhs->x / rhs;
289 res->y = lhs->y / rhs;
290 res->z = lhs->z / rhs;
291}
292A_INTERN void a_vector3_neg(a_vector3 const *ctx, a_vector3 *res)
293{
294 res->x = -ctx->x;
295 res->y = -ctx->y;
296 res->z = -ctx->z;
297}
298
299#endif /* A_HAVE_INLINE */
300#if defined(LIBA_VECTOR3_C)
301#undef A_INTERN
302#define A_INTERN static A_INLINE
303#endif /* LIBA_VECTOR3_C */
304
306
307#endif /* a/vector3.h */
algorithm library
double a_real
compiler built-in floating-point number type
Definition a.h:1012
bool a_bool
type, capable of holding one of the two values: 1 and 0
Definition a.h:320
mathematical algorithm library
instance structure for three-dimensional point
Definition point3.h:77
instance structure for three-dimensional vector
Definition vector3.h:87