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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/*
Project: Adun
Copyright (C) 2005 Michael Johnston & Jordi Villa-Freixa
Author: Michael Johnston
This application is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#include "Base/AdVector.h"
/**
Allocates a matrix of Vector3D structs
*/
Vector3D** AdAllocate3DVectorMatrix(unsigned int numberOfRows, unsigned int numberOfColumns)
{
unsigned int i, j;
Vector3D* array, **matrix;
array = malloc(numberOfRows*numberOfColumns*sizeof(Vector3D));
matrix = malloc(numberOfRows*sizeof(Vector3D*));
for(i=0, j=0; i < numberOfRows; i++, j = j + numberOfColumns)
matrix[i] = array + j;
return matrix;
}
int AdCartesianDistanceCheck(double* position1, double* position2, double separation)
{
if(fabs(position1[0] - position2[0]) > separation)
return 0;
if(fabs(position1[1] - position2[1]) > separation)
return 0;
if(fabs(position1[2] - position2[2]) > separation)
return 0;
return 1;
}
int AdCartesianDistanceVectorCheck(Vector3D* separation, double distance)
{
double* components;
components = separation->vector;
if(fabs(components[0]) > distance)
return 0;
if(fabs(components[1]) > distance)
return 0;
if(fabs(components[2]) > distance)
return 0;
return 1;
}
/**
Frees a matrix of Vector3D structs.
It must have been allocated using AdAllocate3DVectorMatrix().
*/
void AdFree3DVectorMatrix(Vector3D** matrix)
{
if(matrix != NULL)
{
free(matrix[0]); //frees the vector array
free(matrix); //frees the pointer array
}
}
void Ad3DVectorInit(Vector3D* vector)
{
double* array;
array = vector->vector;
array[0] = array[1] = array[2] = 0;
vector->length = 0;
}
void Ad3DVectorScalarMultiply(Vector3D* vector, double value)
{
double* array;
array = vector->vector;
array[0] *= value;
array[1] *= value;
array[2] *= value;
}
double Ad3DDotProduct(Vector3D* vector_one, Vector3D* vector_two)
{
register int i;
double product;
for(product = 0, i=3; --i>=0;)
product += vector_one->vector[i] * vector_two->vector[i];
return product;
}
void Ad3DCrossProduct(Vector3D* v_one, Vector3D* v_two, Vector3D* result)
{
//calculate the cross product of the two vectors v_one X v_two
result->vector[0] = v_one->vector[1]*v_two->vector[2] - v_one->vector[2]*v_two->vector[1];
result->vector[1] = v_one->vector[2]*v_two->vector[0] - v_one->vector[0]*v_two->vector[2];
result->vector[2] = v_one->vector[0]*v_two->vector[1] - v_one->vector[1]*v_two->vector[0];
}
void Ad3DVectorLength(Vector3D* vec)
{
(vec->length) = *(vec->vector) * *(vec->vector) + *(vec->vector + 1) * *(vec->vector + 1);
(vec->length) += *(vec->vector + 2) * *(vec->vector + 2);
(vec->length) = sqrt(vec->length);
}
void Ad3DVectorLengthSquared(Vector3D* vec)
{
(vec->length) = *(vec->vector) * *(vec->vector) + *(vec->vector + 1) * *(vec->vector + 1);
(vec->length) += *(vec->vector + 2) * *(vec->vector + 2);
}
void AdGet3DUnitVector(Vector3D* vector, Vector3D* unit_vector)
{
register int i;
double length;
length = 1/vector->length;
for(i=0; i< 3; i++)
unit_vector->vector[i] = vector->vector[i]*length;
unit_vector->length = 1;
}
void AdGetRandom3DUnitVector(Vector3D* vector, gsl_rng* generator)
{
double r, t;
//Uses trig-method - see docs
vector->vector[2] = gsl_ran_flat(generator, -1, 1);
r = sqrt(1 - vector->vector[2]*vector->vector[2]);
t = gsl_ran_flat(generator, 0, 2*M_PI);
vector->vector[0] = r*cos(t);
vector->vector[1] = r*sin(t);
vector->length = 1;
}
int AdDoubleArrayMin(double* array, int noElements)
{
int minIndex, i;
double minValue;
minIndex = 0;
minValue = array[0];
for(i=1; i<noElements; i++)
{
if(array[i] < minValue)
{
minValue = array[i];
minIndex = i;
}
}
return minIndex;
}
int AdDoubleArrayMax(double* array, int noElements)
{
int maxIndex, i;
double maxValue;
maxIndex = 0;
maxValue = array[0];
for(i=1; i<noElements; i++)
{
if(array[i] > maxValue)
{
maxValue = array[i];
maxIndex = i;
}
}
return maxIndex;
}
/**
Returns the intersection and complement of two arrays as
an InterSectionStruct
**/
void AdIntArrayIntersectionAndDifference(IntArrayStruct *prime, IntArrayStruct *query, IntArrayStruct *intersection, IntArrayStruct *complement)
{
uintptr_t i, j;
uintptr_t complementCount, intersectionCount;
uintptr_t* intersectionArray, *complementArray, *primeArray, *queryArray;
intersection->array = (uintptr_t*)malloc(prime->length*sizeof(uintptr_t));
complement->array = (uintptr_t*)malloc(prime->length*sizeof(uintptr_t));
primeArray = prime->array;
queryArray = query->array;
intersectionArray = intersection->array;
complementArray = complement->array;
i = j = intersectionCount = complementCount = 0;
while(i<prime->length)
{
if(primeArray[i] == queryArray[j])
{
intersectionArray[intersectionCount] = primeArray[i];
i++;
j++;
intersectionCount++;
}
else if(primeArray[i] < queryArray[j])
{
complementArray[complementCount] = primeArray[i];
i++;
complementCount++;
}
else
{
j++;
}
//check if we have exhausted the query array
if(j > query->length)
break;
}
//if we didnt exhause the prime array copy all left over members to complement
for(j=i; j<prime->length; j++)
{
complementArray[complementCount] = primeArray[j];
complementCount++;
}
intersection->length = intersectionCount;
complement->length = complementCount;
}
void AdIntArrayIntersection(IntArrayStruct *prime, IntArrayStruct *query, IntArrayStruct *intersection)
{
uintptr_t i, j;
uintptr_t intersectionCount;
uintptr_t* intersectionArray, *primeArray, *queryArray;
intersection->array = (uintptr_t*)malloc(prime->length*sizeof(uintptr_t));
primeArray = prime->array;
queryArray = query->array;
intersectionArray = intersection->array;
i = j = intersectionCount = 0;
while(i<prime->length)
{
if(primeArray[i] == queryArray[j])
{
intersectionArray[intersectionCount] = primeArray[i];
i++;
j++;
intersectionCount++;
}
else if(primeArray[i] < queryArray[j])
{
i++;
}
else
{
j++;
}
//check if we have exhausted the query array
if(j > query->length)
break;
}
intersection->length = intersectionCount;
}
void AdIntArrayDifference(IntArrayStruct *prime, IntArrayStruct *query, IntArrayStruct *difference)
{
uintptr_t i, j;
uintptr_t differenceCount;
uintptr_t* differenceArray, *primeArray, *queryArray;
difference->array = (uintptr_t*)malloc(prime->length*sizeof(uintptr_t));
primeArray = prime->array;
queryArray = query->array;
differenceArray = difference->array;
i = j = differenceCount = 0;
while(i<prime->length)
{
if(primeArray[i] == queryArray[j])
{
i++;
j++;
}
else if(primeArray[i] < queryArray[j])
{
differenceArray[differenceCount] = primeArray[i];
differenceCount++;
i++;
}
else
{
j++;
}
if(j > query->length)
break;
}
difference->length = differenceCount;
}
|