BRL-CAD
anim.h
Go to the documentation of this file.
1 /* A N I M . H
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2024 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 /*----------------------------------------------------------------------*/
22 /** @addtogroup bn_anim
23  * @brief
24  * Routines useful for moving geometry.
25  *
26  * Orientation conventions: The default object orientation is facing
27  * the positive x-axis, with the positive y-axis to the object's left
28  * and the positive z-axis above the object.
29  *
30  * The default view orientation for rt and mged is facing the negative
31  * z-axis, with the negative x-axis leading to the left and the
32  * positive y-axis going upwards.
33  */
34 /** @{ */
35 /** @file bn/anim.h */
36 
37 #ifndef BN_ANIM_H
38 #define BN_ANIM_H
39 
40 #include "common.h"
41 #include <stdio.h> /* For FILE */
42 #include "vmath.h"
43 #include "bn/defines.h"
44 
45 __BEGIN_DECLS
46 
47 #define ANIM_STEER_NEW 0
48 #define ANIM_STEER_END 1
49 
50 #define VSUBUNIT(a, b, c) { \
51  VSUB2(a, b, c); \
52  VUNITIZE(a); \
53  }
54 #define FVSCAN(f, a) fscanf(f, "%lf %lf %lf", (a), (a)+1, (a)+2)
55 #define FMATSCAN(f, m) { \
56  FVSCAN(f, (m)); \
57  FVSCAN(f, (m)+4); \
58  FVSCAN(f, (m)+8); \
59  FVSCAN(f, (m)+12); \
60  }
61 #define VSCAN(a) scanf("%lf %lf %lf", (a), (a)+1, (a)+2)
62 #define VPRINTS(t, a) printf("%s %f %f %f ", t, (a)[0], (a)[1], (a)[2])
63 #define VPRINTN(t, a) printf("%s %f %f %f\n", t, (a)[0], (a)[1], (a)[2])
64 
65 #define MAT_MOVE(m, n) MAT_COPY(m, n)
66 
67 /***** 3x3 matrix format *****/
68 
69 typedef fastf_t mat3_t[9];
70 
71 #define MAT3ZERO(m) {\
72  int _j; for (_j=0;_j<9;_j++) m[_j]=0.0;}
73 
74 #define MAT3IDN(m) {\
75  int _j; for (_j=0;_j<9;_j++) m[_j]=0.0;\
76  m[0] = m[4] = m[8] = 1.0;}
77 
78 #define MAT3MUL(o, a, b) {\
79  (o)[0] = (a)[0]*(b)[0] + (a)[1]*(b)[3] + (a)[2]*(b)[6];\
80  (o)[1] = (a)[0]*(b)[1] + (a)[1]*(b)[4] + (a)[2]*(b)[7];\
81  (o)[2] = (a)[0]*(b)[2] + (a)[1]*(b)[5] + (a)[2]*(b)[8];\
82  (o)[3] = (a)[3]*(b)[0] + (a)[4]*(b)[3] + (a)[5]*(b)[6];\
83  (o)[4] = (a)[3]*(b)[1] + (a)[4]*(b)[4] + (a)[5]*(b)[7];\
84  (o)[5] = (a)[3]*(b)[2] + (a)[4]*(b)[5] + (a)[5]*(b)[8];\
85  (o)[6] = (a)[6]*(b)[0] + (a)[7]*(b)[3] + (a)[8]*(b)[6];\
86  (o)[7] = (a)[6]*(b)[1] + (a)[7]*(b)[4] + (a)[8]*(b)[7];\
87  (o)[8] = (a)[6]*(b)[2] + (a)[7]*(b)[5] + (a)[8]*(b)[8];}
88 
89 #define MAT3SUM(o, a, b) {\
90  int _j; for (_j=0;_j<9;_j++) (o)[_j]=(a)[_j]+(b)[_j];}
91 
92 #define MAT3DIF(o, a, b) {\
93  int _j; for (_j=0;_j<9;_j++) (o)[_j]=(a)[_j]-(b)[_j];}
94 
95 #define MAT3SCALE(o, a, s) {\
96  int _j; for (_j=0;_j<9;_j++) (o)[_j]=(a)[_j] * (s);}
97 
98 #define MAT3MOVE(o, a) {\
99  int _j; for (_j=0;_j<9;_j++) (o)[_j] = (a)[_j];}
100 
101 #define MAT3XVEC(u, m, v) {\
102  (u)[0] = (m)[0]*(v)[0] + (m)[1]*(v)[1] + (m)[2]*(v)[2];\
103  (u)[1] = (m)[3]*(v)[0] + (m)[4]*(v)[1] + (m)[5]*(v)[2];\
104  (u)[2] = (m)[6]*(v)[0] + (m)[7]*(v)[1] + (m)[8]*(v)[2];}
105 
106 #define MAT3TO4(o, i) {\
107  (o)[0] = (i)[0];\
108  (o)[1] = (i)[1];\
109  (o)[2] = (i)[2];\
110  (o)[4] = (i)[3];\
111  (o)[5] = (i)[4];\
112  (o)[6] = (i)[5];\
113  (o)[8] = (i)[6];\
114  (o)[9] = (i)[7];\
115  (o)[10] = (i)[8];\
116  (o)[3]=(o)[7]=(o)[11]=(o)[12]=(o)[13]=(o)[14]=0.0;\
117  (o)[15]=1.0;}
118 
119 #define MAT4TO3(o, i) {\
120  (o)[0] = (i)[0];\
121  (o)[1] = (i)[1];\
122  (o)[2] = (i)[2];\
123  (o)[3] = (i)[4];\
124  (o)[4] = (i)[5];\
125  (o)[5] = (i)[6];\
126  (o)[6] = (i)[8];\
127  (o)[7] = (i)[9];\
128  (o)[8] = (i)[10];}
129 
130 /** tilde matrix: [M]a = v X a */
131 #define MAKE_TILDE(m, v) {\
132  MAT3ZERO(m);\
133  m[1]= -v[2]; m[2]=v[1]; m[3]= v[2];\
134  m[5]= -v[0]; m[6]= -v[1]; m[7]= v[0];}
135 
136 /** a = Ix Iy Iz b = Ixy Ixz Iyz*/
137 #define INERTIAL_MAT3(m, a, b) {\
138  (m)[0] = (a)[0]; (m)[1] = -(b)[0]; (m)[2] = -(b)[1];\
139  (m)[3] = -(b)[0]; (m)[4] = (a)[1]; (m)[5] = -(b)[2];\
140  (m)[6] = -(b)[1]; (m)[7] = -(b)[2]; (m)[8]= (a)[2];}
141 
142 
143 /* FIXME: These should all have bn_ prefixes */
144 
145 /**
146  * @brief Pre-multiply a rotation matrix by a matrix which maps the
147  * z-axis to the negative x-axis, the y-axis to the z-axis and the
148  * x-axis to the negative y-axis.
149  *
150  * This has the effect of twisting an object in the default view
151  * orientation into the default object orientation before applying the
152  * matrix. Given a matrix designed to operate on an object, yield a
153  * matrix which operates on the view.
154  */
155 BN_EXPORT extern void anim_v_permute(mat_t m);
156 
157 /**
158  * @brief Undo the mapping done by anim_v_permute().
159  *
160  * This has the effect of twisting an object in the default object
161  * orientation into the default view orientation before applying the
162  * matrix. Given a matrix designed to operate on the view, yield a
163  * matrix which operates on an object.
164  */
165 BN_EXPORT extern void anim_v_unpermute(mat_t m);
166 
167 /**
168  * @brief Transpose matrix in place
169  */
170 BN_EXPORT extern void anim_tran(mat_t m);
171 
172 /**
173  * @brief
174  * Convert the rotational part of a 4x4 transformation matrix to zyx
175  * form, that is to say, rotations carried out in the order z, y, and
176  * then x. The angles are stored in radians as a vector in the order
177  * x, y, z. A return value of ERROR1 means that arbitrary assumptions
178  * were necessary. ERROR2 means that the conversion failed.
179  */
180 BN_EXPORT extern int anim_mat2zyx(const mat_t viewrot,
181  vect_t angle);
182 
183 /**
184  * @brief
185  * Convert the rotational part of a 4x4 transformation matrix to
186  * yaw-pitch-roll form, that is to say, +roll degrees about the
187  * x-axis, -pitch degrees about the y-axis, and +yaw degrees about the
188  * z-axis. The angles are stored in radians as a vector in the order
189  * y, p, r. A return of ERROR1 means that arbitrary assumptions were
190  * necessary. ERROR2 means that the conversion failed.
191  */
192 BN_EXPORT extern int anim_mat2ypr(mat_t viewrot,
193  vect_t angle);
194 
195 /**
196  * @brief
197  * This interprets the rotational part of a 4x4 transformation matrix
198  * in terms of unit quaternions. The result is stored as a vector in
199  * the order x, y, z, w. The algorithm is from Ken Shoemake,
200  * Animating Rotation with Quaternion Curves, 1985 SIGGraph Conference
201  * Proceeding, p.245.
202  */
203 BN_EXPORT extern int anim_mat2quat(quat_t quat,
204  const mat_t viewrot);
205 
206 /**
207  * @brief Create a premultiplication rotation matrix to turn the front
208  * of an object (its x-axis) to the given yaw, pitch, and roll, which
209  * is stored in radians in the vector a.
210  */
211 BN_EXPORT extern void anim_ypr2mat(mat_t m,
212  const vect_t a);
213 
214 /**
215  * @brief Create a post-multiplication rotation matrix, which could be
216  * used to move the virtual camera to the given yaw, pitch, and roll,
217  * which are stored in radians in the given vector a. The following
218  * are equivalent sets of commands:
219  *
220  * ypr2vmat(matrix, a);
221  * -- or --
222  * ypr2mat(matrix, a);
223  * v_permute(matrix);
224  * transpose(matrix;
225  */
226 BN_EXPORT extern void anim_ypr2vmat(mat_t m,
227  const vect_t a);
228 
229 /**
230  * @brief
231  * Make matrix to rotate an object to the given yaw, pitch, and
232  * roll. (Specified in radians.)
233  */
234 BN_EXPORT extern void anim_y_p_r2mat(mat_t m,
235  double y,
236  double p,
237  double r);
238 
239 /**
240  * @brief Make matrix to rotate an object to the given yaw, pitch, and
241  * roll. (Specified in degrees.)
242  */
243 BN_EXPORT extern void anim_dy_p_r2mat(mat_t m,
244  double y,
245  double p,
246  double r);
247 
248 /**
249  * @brief Make a view rotation matrix, given desired yaw, pitch and
250  * roll. (Note that the matrix is a permutation of the object rotation
251  * matrix).
252  */
253 BN_EXPORT extern void anim_dy_p_r2vmat(mat_t m,
254  double yaw,
255  double pch,
256  double rll);
257 
258 /**
259  * @brief Make a rotation matrix corresponding to a rotation of "x"
260  * radians about the x-axis, "y" radians about the y-axis, and then
261  * "z" radians about the z-axis.
262  */
263 BN_EXPORT extern void anim_x_y_z2mat(mat_t m,
264  double x,
265  double y,
266  double z);
267 
268 /**
269  * @brief Make a rotation matrix corresponding to a rotation of "x"
270  * degrees about the x-axis, "y" degrees about the y-axis, and then
271  * "z" degrees about the z-axis.
272  */
273 BN_EXPORT extern void anim_dx_y_z2mat(mat_t m,
274  double x,
275  double y,
276  double z);
277 
278 /**
279  * @brief Make a rotation matrix corresponding to a rotation of "z"
280  * radians about the z-axis, "y" radians about the y-axis, and then
281  * "x" radians about the x-axis.
282  */
283 BN_EXPORT extern void anim_zyx2mat(mat_t m,
284  const vect_t a);
285 
286 /**
287  * @brief Make a rotation matrix corresponding to a rotation of "z"
288  * radians about the z-axis, "y" radians about the y-axis, and then
289  * "x" radians about the x-axis.
290  */
291 BN_EXPORT extern void anim_z_y_x2mat(mat_t m,
292  double x,
293  double y,
294  double z);
295 
296 /**
297  * @brief
298  * Make a rotation matrix corresponding to a rotation of "z" degrees
299  * about the z-axis, "y" degrees about the y-axis, and then "x"
300  * degrees about the x-axis.
301  */
302 BN_EXPORT extern void anim_dz_y_x2mat(mat_t m,
303  double x,
304  double y,
305  double z);
306 
307 /**
308  * @brief
309  * Make 4x4 matrix from the given quaternion Note: these quaternions
310  * are the conjugates of the quaternions used in the librt/qmath.c
311  * quat_quat2mat()
312  */
313 BN_EXPORT extern void anim_quat2mat(mat_t m,
314  const quat_t qq);
315 
316 /**
317  * @brief
318  * make a matrix which turns a vehicle from the x-axis to point in the
319  * desired direction, staying "right-side up" (i.e. the y-axis never has
320  * a z-component). A second direction vector is consulted when the
321  * given direction is vertical. This is intended to represent the
322  * direction from a previous frame.
323  */
324 BN_EXPORT extern void anim_dir2mat(mat_t m,
325  const vect_t d,
326  const vect_t d2);
327 
328 /**
329  * @brief make a matrix which turns a vehicle from the x-axis to point
330  * in the desired direction, staying "right-side up". In cases where
331  * the direction is vertical, the second vector is consulted. The
332  * second vector defines a normal to the vertical plane into which
333  * the vehicle's x and z axes should be put. A good choice to put here
334  * is the direction of the vehicle's y-axis in the previous frame.
335  */
336 BN_EXPORT extern void anim_dirn2mat(mat_t m,
337  const vect_t dx,
338  const vect_t dn);
339 
340 /**
341  * @brief given the next frame's position, remember the value of the
342  * previous frame's position and calculate a matrix which points the
343  * x-axis in the direction defined by those two positions. Return new
344  * matrix, and the remembered value of the current position, as
345  * arguments; return 1 as the normal value, and 0 when there is not
346  * yet information to remember.
347  */
348 BN_EXPORT extern int anim_steer_mat(mat_t mat,
349  vect_t point,
350  int end);
351 
352 /**
353  * @brief Add pre- and post- translation to a rotation matrix. The
354  * resulting matrix has the effect of performing the first
355  * translation, followed by the rotation, followed by the second
356  * translation.
357  */
358 BN_EXPORT extern void anim_add_trans(mat_t m,
359  const vect_t post,
360  const vect_t pre);
361 
362 /**
363  * @brief Rotate the vector "d" through "a" radians about the z-axis.
364  */
365 BN_EXPORT extern void anim_rotatez(fastf_t a,
366  vect_t d);
367 
368 /**
369  * @brief print out 4X4 matrix, with optional colon
370  */
371 BN_EXPORT extern void anim_mat_print(FILE *fp,
372  const mat_t m,
373  int s_colon);
374 
375 /**
376  * @brief print out 4X4 matrix. formstr must be less than twenty
377  * chars
378  */
379 BN_EXPORT extern void anim_mat_printf(FILE *fp,
380  const mat_t m,
381  const char *formstr,
382  const char *linestr,
383  const char *endstr);
384 
385 /**
386  * @brief Reverse the direction of a view matrix, keeping it
387  * right-side up
388  */
389 BN_EXPORT extern void anim_view_rev(mat_t m);
390 
391 __END_DECLS
392 
393 #endif /* BN_ANIM_H */
394 /** @} */
395 /*
396  * Local Variables:
397  * mode: C
398  * tab-width: 8
399  * indent-tabs-mode: t
400  * c-file-style: "stroustrup"
401  * End:
402  * ex: shiftwidth=4 tabstop=8
403  */
Header file for the BRL-CAD common definitions.
void anim_view_rev(mat_t m)
Reverse the direction of a view matrix, keeping it right-side up.
void anim_v_unpermute(mat_t m)
Undo the mapping done by anim_v_permute().
int anim_mat2quat(quat_t quat, const mat_t viewrot)
This interprets the rotational part of a 4x4 transformation matrix in terms of unit quaternions....
void anim_x_y_z2mat(mat_t m, double x, double y, double z)
Make a rotation matrix corresponding to a rotation of "x" radians about the x-axis,...
void anim_dz_y_x2mat(mat_t m, double x, double y, double z)
Make a rotation matrix corresponding to a rotation of "z" degrees about the z-axis,...
void anim_dir2mat(mat_t m, const vect_t d, const vect_t d2)
make a matrix which turns a vehicle from the x-axis to point in the desired direction,...
void anim_v_permute(mat_t m)
Pre-multiply a rotation matrix by a matrix which maps the z-axis to the negative x-axis,...
void anim_ypr2mat(mat_t m, const vect_t a)
Create a premultiplication rotation matrix to turn the front of an object (its x-axis) to the given y...
void anim_mat_printf(FILE *fp, const mat_t m, const char *formstr, const char *linestr, const char *endstr)
print out 4X4 matrix. formstr must be less than twenty chars
void anim_mat_print(FILE *fp, const mat_t m, int s_colon)
print out 4X4 matrix, with optional colon
void anim_rotatez(fastf_t a, vect_t d)
Rotate the vector "d" through "a" radians about the z-axis.
void anim_tran(mat_t m)
Transpose matrix in place.
void anim_dirn2mat(mat_t m, const vect_t dx, const vect_t dn)
make a matrix which turns a vehicle from the x-axis to point in the desired direction,...
void anim_add_trans(mat_t m, const vect_t post, const vect_t pre)
Add pre- and post- translation to a rotation matrix. The resulting matrix has the effect of performin...
void anim_zyx2mat(mat_t m, const vect_t a)
Make a rotation matrix corresponding to a rotation of "z" radians about the z-axis,...
void anim_quat2mat(mat_t m, const quat_t qq)
Make 4x4 matrix from the given quaternion Note: these quaternions are the conjugates of the quaternio...
void anim_dy_p_r2vmat(mat_t m, double yaw, double pch, double rll)
Make a view rotation matrix, given desired yaw, pitch and roll. (Note that the matrix is a permutatio...
fastf_t mat3_t[9]
Definition: anim.h:69
void anim_z_y_x2mat(mat_t m, double x, double y, double z)
Make a rotation matrix corresponding to a rotation of "z" radians about the z-axis,...
void anim_dy_p_r2mat(mat_t m, double y, double p, double r)
Make matrix to rotate an object to the given yaw, pitch, and roll. (Specified in degrees....
int anim_mat2ypr(mat_t viewrot, vect_t angle)
Convert the rotational part of a 4x4 transformation matrix to yaw-pitch-roll form,...
void anim_y_p_r2mat(mat_t m, double y, double p, double r)
Make matrix to rotate an object to the given yaw, pitch, and roll. (Specified in radians....
void anim_dx_y_z2mat(mat_t m, double x, double y, double z)
Make a rotation matrix corresponding to a rotation of "x" degrees about the x-axis,...
int anim_mat2zyx(const mat_t viewrot, vect_t angle)
Convert the rotational part of a 4x4 transformation matrix to zyx form, that is to say,...
int anim_steer_mat(mat_t mat, vect_t point, int end)
given the next frame's position, remember the value of the previous frame's position and calculate a ...
void anim_ypr2vmat(mat_t m, const vect_t a)
Create a post-multiplication rotation matrix, which could be used to move the virtual camera to the g...
void float float * y
Definition: tig.h:73
void float float float * z
Definition: tig.h:90
void float * x
Definition: tig.h:72
void int char int int double double * dx
Definition: tig.h:183
fastf_t vect_t[ELEMENTS_PER_VECT]
3-tuple vector
Definition: vmath.h:349
double fastf_t
fastest 64-bit (or larger) floating point type
Definition: vmath.h:334
fastf_t mat_t[ELEMENTS_PER_MAT]
4x4 matrix
Definition: vmath.h:370
hvect_t quat_t
4-element quaternion
Definition: vmath.h:364
fundamental vector, matrix, quaternion math macros