DFNgen  2.0
DFN Model Generator
vectorFunctions.cpp
Go to the documentation of this file.
1 #include <cmath>
2 #include "structures.h"
3 #include "vectorFunctions.h"
4 #include <iostream>
5 #include <cmath>
6 #include "input.h"//eps
7 
8 //see vectorFunctions.h for template function implimentations
9 //such as crossProduct, dorProduct, magnitude,...
10 
11 
12 /************************************************************/
13 /************************************************************/
14 // Projection funcion without sqrt()
15 // Projects v1 onto v2
16 // Arg 1: Pointer to vector 1 containing {x,y,z}, all doubles
17 // Arg 2: Pointer to vector 2 containing {x,y,z}, all doubles
18 // Return: Resulting vector.
19 // Must delete return array manually. Created with new.
20 double *projection( const double *v1, const double *v2 ) {
21 
22  double v2_ls = v2[0]*v2[0] + v2[1]*v2[1] + v2[2]*v2[2];
23 
24  double *result = new double[3];
25 
26  if (v2_ls < eps) {
27  result[0] = 0;
28  result[1] = 0;
29  result[2] = 0;
30  }
31  else {
32  double temp = ((v2[0]*v1[0] + v2[1]*v1[1] + v2[2]*v1[2] )/v2_ls );
33  result[0] = v2[0] * temp;
34  result[1] = v2[1] * temp;
35  result[2] = v2[2] * temp;
36  }
37  return result;
38 }
39 
40 /************************************************************/
41 /************************************************************/
42 // Calculates the distance between two points in 3D space.
43 // Arg 1: Pointer to 3 element double array {x,y,z}
44 // Arg 2: Pointer to 3 element double array {x,y,z}
45 // Return: Distance between point A and point B
46 double euclideanDistance(double *A, double *B){
47  double temp1 = A[0] - B[0];
48  double temp2 = A[1] - B[1];
49  double temp3 = A[2] - B[2];
50  temp1 = temp1*temp1;
51  temp2 = temp2*temp2;
52  temp3 = temp3*temp3;
53 
54  return std::sqrt(temp1+temp2+temp3);
55 }
56 
57 double euclideanDistance(Point &A, Point &B) {
58  double temp1 = A.x - B.x;
59  double temp2 = A.y - B.y;
60  double temp3 = A.z - B.z;
61  temp1 = temp1*temp1;
62  temp2 = temp2*temp2;
63  temp3 = temp3*temp3;
64 
65  return std::sqrt(temp1+temp2+temp3);
66 }
67 
68 /************************************************************/
69 /************************************************************/
70 // Claculates the angle between two vectors
71 // Arg 1: Pointer to 3 element double array, {x,y,z} vector
72 // Arg 2: Pointer to 3 element double array, {x,y,z} vector
73 // Return: Angle between both vectors in radians
74 double angleBeteenVectors(const double *vector1,const double *vector2) {
75  // cos(x) = u.v / (mag(u) * mag(v))
76  // x = arccos(x) = (mag(u) * mag(v)) / u.v
77  double dot = dotProduct(vector1, vector2);
78  double v1Mag = magnitude(vector1[0], vector1[1], vector1[2]);
79  double v2Mag = magnitude(vector2[0], vector2[1], vector2[2]);
80  double angle = std::acos(dot/(v1Mag*v2Mag));
81 
82  return angle;
83 }
84 
85 
double euclideanDistance(double *A, double *B)
T magnitude(T x, T y, T z)
double angleBeteenVectors(const double *vector1, const double *vector2)
double eps
Definition: DFNmain.cpp:39
double y
Definition: structures.h:113
double x
Definition: structures.h:112
double * projection(const double *v1, const double *v2)
T dotProduct(const T *A, const T *B)
double z
Definition: structures.h:114