1 /* 2 * Geometric operations. 3 * 4 * Copyright (C) 2013 Paul Boddie 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12 #ifndef __GEO_H__ 13 #define __GEO_H__ 14 15 #include <math.h> 16 #include "bool.h" 17 18 typedef union 19 { 20 struct { double x, y, z; }; 21 double axis[3]; 22 } vectorf; 23 24 double within(double value, double mag); 25 bool vectorf_null(const vectorf *in); 26 void vectorf_reset(vectorf *in); 27 void vectorf_cross(const vectorf *in1, const vectorf *in2, vectorf *out); 28 double vectorf_dot(const vectorf *in1, const vectorf *in2); 29 double vectorf_mag(const vectorf *in); 30 void vectorf_normalise(vectorf *in, vectorf *out); 31 void plane_rotate(vectorf *in1, vectorf *in2, double angle); 32 void vectorf_negate(vectorf *in, vectorf *out); 33 double vectorf_direction(vectorf *in); 34 double vectorf_elevation(vectorf *in); 35 void vectorf_polar(double direction, double elevation, vectorf *out); 36 void vectorf_convert(const vectorf *in, const vectorf *devicex, const vectorf *devicey, const vectorf *devicez, vectorf *out); 37 void vectorf_convert_into(const vectorf *in, const vectorf *x, const vectorf *y, const vectorf *z, vectorf *out); 38 double vectorf_tilt_in_plane(const vectorf *in, const vectorf *x, const vectorf *y); 39 double vectorf_tilt_in_plane_with_axis(const vectorf *in, const vectorf *x, const vectorf *y, const vectorf *z); 40 void vectorf_rotate_in_space(const vectorf *in, const vectorf *x, const vectorf *y, const vectorf *z, double angle, vectorf *out); 41 42 #define degrad(x) ((x) * M_PI / 180.0) 43 #define raddeg(x) ((x) * 180.0 / M_PI) 44 45 #define fdiv(x, y) (floor((int) ((x) / (y)))) 46 47 /* Get the closest number at the given resolution. */ 48 49 #define closest(x, res) (((int) (x) / (res)) * (res)) 50 51 /* Get the maximum and minimum of the given numbers. */ 52 53 #define max(x, y) ((x) >= (y) ? (x) : (y)) 54 #define min(x, y) ((x) <= (y) ? (x) : (y)) 55 56 /* Get the sign of a number. */ 57 58 #define sign(x) ((x) == 0 ? 0 : (x) < 0 ? -1 : 1) 59 60 /* Eliminate noise using the given threshold. */ 61 62 #define noise(x, y) (fabs(x) < (y) ? 0 : (x)) 63 64 #endif /* __GEO_H__ */