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
|
/*
* Author: Peter Holst Andersen
* Last change: August, 1994
* Contents: Functions to perform vector operations.
*/
#include <stdio.h>
#include <math.h>
#include "ray.h"
myfloat vector_dot(vectorType *A, vectorType *B)
{
return (A->x * B->x + A->y * B->y + A->z * B->z);
}
/*vector_norm(A) = |A|, A = A/|A|*/
myfloat vector_norm(vectorType *A)
{
myfloat a, b;
if ((a = vector_dot(A, A)) <= 0.0) return 0.0;
b = sqrt((double) a);
A->x /= b;
A->y /= b;
A->z /= b;
return b;
}
/* \begin{C-Mix hack} */
myfloat vector_norm_S(vectorType *A) { vector_norm(A); }
/* \end{C-Mix hack} */
void vector_sub(vectorType *A, vectorType *B, vectorType *C)
{
C->x = A->x - B->x;
C->y = A->y - B->y;
C->z = A->z - B->z;
}
void vector_sub_S(vectorType *A, vectorType *B, vectorType *C) {
vector_sub(A, B, C);
}
void vector_cross(vectorType *A, vectorType *B, vectorType *C)
{
C->x = (A->y * B->z) - (A->z * B->y);
C->y = (A->z * B->x) - (A->x * B->z);
C->z = (A->x * B->y) - (A->y * B->x);
}
myfloat vector_norm_cross(vectorType *A, vectorType *B, vectorType *C)
{
vector_cross(A, B, C);
return vector_norm(C);
}
void vector_scale(myfloat s, vectorType *A, vectorType *B)
{
B->x = A->x * s;
B->y = A->y * s;
B->z = A->z * s;
}
|