DFNgen  2.0
DFN Model Generator
vectorFunctions.h
Go to the documentation of this file.
1 //vectorFunctions.h
2 #ifndef _vectorFunctions_h_
3 #define _vectorFunctions_h_
4 #include <fstream>
5 #include <string>
6 #include <iostream>
7 #include <cmath>
8 #include "input.h"
9 #include "structures.h"
10 
11 
12 double *projection( const double *v1, const double *v2 );
13 double euclideanDistance(double *A, double *B);
14 double euclideanDistance(Point &A, Point &B);
15 double angleBeteenVectors(const double *vector1, const double *vector2);
16 
17 /**************************************************/
23 template <typename T>
24 inline T* crossProduct(const T *v1, const T *v2) {
25  T *result = new T[3];
26  result[0] = v1[1]*v2[2] - v1[2]*v2[1];
27  result[1] = v1[2]*v2[0] - v1[0]*v2[2];
28  result[2] = v1[0]*v2[1] - v1[1]*v2[0];
29  return result;
30 }
31 
32 /**************************************************/
35 template <typename T>
36 inline void normalize(T *vec){
37  float invMag = 1.0f / sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
38 
39  if (!isinf(invMag)){
40  vec[0] = vec[0] * invMag;
41  vec[1] = vec[1] * invMag;
42  vec[2] = vec[2] * invMag;
43  }
44  else {
45  std::cout<<"\nERROR: Attempted to normalize a vector with magnitude = 0\n";
46  exit(1);
47  }
48 }
49 
50 
51 /**************************************************/
57 template <typename T>
58 inline T dotProduct(const T *A, const T *B){
59  T result = A[0]*B[0] + A[1]*B[1] + A[2]*B[2];
60  return result;
61 }
62 
63 
64 /**************************************************/
69 template <typename T>
70 void printVertices(T *vert, int numVertices) {
71  std::cout<<"Vertices:\n";
72  for (int i = 0; i < numVertices; i++ ) {
73  int x = i*3;
74  std::cout<< "{" << vert[x] << "," << vert[x+1] << "," << vert[x+2] << "}\n";
75  }
76 }
77 
78 
79 /**************************************************/
85 template <typename T>
86 inline T magnitude(T x, T y, T z){
87  return sqrt((x*x) + (y*y) + (z*z));
88 }
89 
90 
91 /**************************************************/
97 template <typename T>
98 inline T sqrMagnitude(T x, T y, T z){
99  return (x*x) + (y*y) + (z*z);
100 }
101 
102 
103 /**************************************************/
109 inline bool parallel(double *v1, double *v2){
110  normalize(v1);
111  normalize(v2);
112 
113  double dotProd = dotProduct(v1, v2);
114 
115  if (1-eps < dotProd && dotProd < 1+eps){
116  return 1;
117  }
118  else{
119  return 0;
120  }
121 }
122 
123 #endif
124 
125 
T magnitude(T x, T y, T z)
void normalize(T *vec)
double angleBeteenVectors(const double *vector1, const double *vector2)
double eps
Definition: DFNmain.cpp:39
bool parallel(double *v1, double *v2)
void printVertices(T *vert, int numVertices)
T dotProduct(const T *A, const T *B)
double euclideanDistance(double *A, double *B)
T sqrMagnitude(T x, T y, T z)
T * crossProduct(const T *v1, const T *v2)
double * projection(const double *v1, const double *v2)