Adun  0.83
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
AdVector.h
1 /*
2  Project: Adun
3 
4  Copyright (C) 2005 Michael Johnston & Jordi Villą--Freixa
5 
6  Author: Michael Johnston
7 
8  This application is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This application is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU General Public
19  License along with this library; if not, write to the Free
20  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21 */
22 
23 #ifndef VECTORS
24 #define VECTORS
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <gsl/gsl_rng.h>
30 #include <gsl/gsl_randist.h>
31 #include <stdio.h>
32 
33 
39 typedef struct
40 {
41  double length;
42  double vector[3];
43 }
44 Vector3D;
45 
46 typedef struct
47 {
48  double length[2];
49  double vector[6];
50 }
51 DoubleVector3D;
52 
58 typedef struct
59 {
60  int length;
61  int* array;
62 }
64 
77 #ifdef __GNUC__
78 
82 extern inline void Ad3DVectorInit(Vector3D* vector)
83 {
84  double* array;
85 
86  array = vector->vector;
87  array[0] = array[1] = array[2] = 0;
88  vector->length = 0;
89 }
90 
91 
99 extern inline int AdCartesianDistanceCheck(double* position1, double* position2, double separation)
100 {
101  if(fabs(position1[0] - position2[0]) > separation)
102  return 0;
103 
104  if(fabs(position1[1] - position2[1]) > separation)
105  return 0;
106 
107  if(fabs(position1[2] - position2[2]) > separation)
108  return 0;
109 
110  return 1;
111 }
112 
118 extern inline int AdCartesianDistanceVectorCheck(Vector3D* separation, double distance)
119 {
120  double* components;
121 
122  components = separation->vector;
123 
124  if(fabs(components[0]) > distance)
125  return 0;
126 
127  if(fabs(components[1]) > distance)
128  return 0;
129 
130  if(fabs(components[2]) > distance)
131  return 0;
132 
133  return 1;
134 }
135 
143 extern inline void Ad3DVectorScalarMultiply(Vector3D* vector, double value)
144 {
145  double* array;
146 
147  array = vector->vector;
148  array[0] *= value;
149  array[1] *= value;
150  array[2] *= value;
151 }
152 
158 extern inline double Ad3DDotProduct(Vector3D* vector_one, Vector3D* vector_two)
159 {
160  register int i;
161  double product;
162 
163  for(product = 0, i=3; --i>=0;)
164  product += vector_one->vector[i] * vector_two->vector[i];
165 
166  return product;
167 }
168 
175 extern inline void Ad3DCrossProduct(Vector3D* v_one, Vector3D* v_two, Vector3D* result)
176 {
177  //calculate the cross product of the two vectors v_one X v_two
178  result->vector[0] = v_one->vector[1]*v_two->vector[2] - v_one->vector[2]*v_two->vector[1];
179  result->vector[1] = v_one->vector[2]*v_two->vector[0] - v_one->vector[0]*v_two->vector[2];
180  result->vector[2] = v_one->vector[0]*v_two->vector[1] - v_one->vector[1]*v_two->vector[0];
181 }
182 
189 extern inline void Ad3DVectorSubstract(Vector3D* vector_one, Vector3D* vector_two, Vector3D* result)
190 {
191  result->vector[0] = vector_one->vector[0] - vector_two->vector[0];
192  result->vector[1] = vector_one->vector[1] - vector_two->vector[1];
193  result->vector[2] = vector_one->vector[2] - vector_two->vector[2];
194 }
195 
202 extern inline void Ad3DVectorAdd(Vector3D* vector_one, Vector3D* vector_two, Vector3D* result)
203 {
204  result->vector[0] = vector_one->vector[0] + vector_two->vector[0];
205  result->vector[1] = vector_one->vector[1] + vector_two->vector[1];
206  result->vector[2] = vector_one->vector[2] + vector_two->vector[2];
207 }
208 
214 extern inline void Ad3DVectorLength(Vector3D* vec)
215 {
216  (vec->length) = *(vec->vector) * *(vec->vector) + *(vec->vector + 1) * *(vec->vector + 1);
217  (vec->length) += *(vec->vector + 2) * *(vec->vector + 2);
218 
219  (vec->length) = sqrt(vec->length);
220 }
221 
225 extern inline void Ad3DVectorLengthSquared(Vector3D* vec)
226 {
227  (vec->length) = *(vec->vector) * *(vec->vector) + *(vec->vector + 1) * *(vec->vector + 1);
228  (vec->length) += *(vec->vector + 2) * *(vec->vector + 2);
229 }
230 
231 #else
232 
233 //Non-inline declarations of the above functions
234 
235 void Ad3DVectorInit(Vector3D* vector);
236 double Ad3DDotProduct(Vector3D*, Vector3D*);
237 void Ad3DCrossProduct(Vector3D*, Vector3D*, Vector3D*);
238 void Ad3DVectorLength(Vector3D* vec);
239 int AdCartesianDistanceCheck(double* position1, double* position2, double separation);
240 int AdCartesianDistanceVectorCheck(Vector3D* separation, double distance);
241 void Ad3DVectorScalarMultiply(Vector3D* vector, double value);
242 void Ad3DVectorLengthSquared(Vector3D* vec);
243 void Ad3DVectorSubstract(Vector3D* vector_one, Vector3D* vector_two, Vector3D* result);
244 void Ad3DVectorAdd(Vector3D* vector_one, Vector3D* vector_two, Vector3D* result);
245 
246 #endif
247 
248 
252 Vector3D** AdAllocate3DVectorMatrix(unsigned int numberOfRows, unsigned int numberOfColumns);
253 
258 void AdFree3DVectorMatrix(Vector3D** matrix);
259 
266 
274 void AdGetRandom3DUnitVector(Vector3D* vector, gsl_rng* generator);
275 
280 int AdDoubleArrayMin(double*, int);
285 int AdDoubleArrayMax(double*, int);
286 
288 void AdIntArrayIntersection(IntArrayStruct *prime, IntArrayStruct *query, IntArrayStruct *intersection);
289 void AdIntArrayDifference(IntArrayStruct *prime, IntArrayStruct *query, IntArrayStruct *difference);
290 
293 #endif
294