-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector3d.cpp
executable file
·139 lines (127 loc) · 3.67 KB
/
vector3d.cpp
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
#include "vector3d.h"
#include <algorithm>
#include <math.h>
#include <sstream>
#include <iostream>
#include <numeric>
#include <iostream>
#include <vector>
#include <functional>
vector3D::vector3D(double _X, double _Y, double _Z) {
X = _X;
Y = _Y;
Z = _Z;
}
// Redefenition of operator +
vector3D vector3D::operator+(vector3D Addend_vector) {
vector3D Sum = vector3D(0., 0., 0.);
Sum.X = X + Addend_vector.X;
Sum.Y = Y + Addend_vector.Y;
Sum.Z = Z + Addend_vector.Z;
return Sum;
}
// Redefenition of operator -
vector3D vector3D::operator-(vector3D Subtrahend_vector) {
vector3D Difference = vector3D(0., 0., 0.);
Difference.X = X - Subtrahend_vector.X;
Difference.Y = Y - Subtrahend_vector.Y;
Difference.Z = Z - Subtrahend_vector.Z;
return Difference;
}
// Redefenition of operator *
vector3D vector3D::operator*(double Scalar) {
vector3D Product = vector3D(1., 1., 1.);
Product.X = X * Scalar;
Product.Y = Y * Scalar;
Product.Z = Z * Scalar;
return Product;
}
// Redefinition of operator == (Ture/False) if X,Y,Z coordinates are equal.
bool vector3D::operator==(vector3D &v) {
double error = 1e-10;
return ((abs(X - v.X) <= error) && (abs(Y - v.Y) <= error) &&
(abs(Z - v.Z) <= error));
}
//#Recheck danial: why?
bool vector3D::compare(double a) {
double error = 1e-10;
return ((abs(X - a) <= error) && (abs(Y - a) <= error) &&
(abs(Z - a) <= error));
}
// Redefinition of operator [] (access by index).
double vector3D::operator[](int i) {
if (i == 0) {
return X;
} else if (i == 1) {
return Y;
} else if (i == 2) {
return Z;
} else {
cout << "Incorrect index for vector3D" << endl;
exit(1);
}
}
// This function returns the norm of a vector
double vector3D::getNorm() {
double norm = sqrt((X * X) + (Y * Y) + (Z * Z));
return norm;
}
// This function normalizes the vector
void vector3D::NormalizeYourself() {
vector3D normalize = vector3D(X / getNorm(), Y / getNorm(), Z / getNorm());
X = normalize.X;
Y = normalize.Y;
Z = normalize.Z;
}
// Create a new normalized vector
vector3D vector3D::getNormalizedVector() {
double Norm_of_vector = getNorm();
if (fabs(Norm_of_vector) > 1e-9) {
X /= Norm_of_vector;
Y /= Norm_of_vector;
Z /= Norm_of_vector;
} else {
X = 0;
Y = 0.;
Z = 0.;
}
vector3D normalized_vector = vector3D(X, Y, Z);
return normalized_vector;
}
// Calculate scalar product of 2 vectors
double getScalarproduct(vector3D &u, vector3D &v) {
double scalar_product = (u.X * v.X) + (u.Y * v.Y) + (u.Z * v.Z);
return scalar_product;
// It is possible to change this with built-in inner_product function
// vector <double> a(3);
// vector <double> b(3);
// for (int i=0;i<3;i++)
// {
// a[i]=u[i];
// b[i]=v[i];
// }
//
// return double (inner_product(a.begin(), a.end(), b.begin(), 0.0));
}
// This function calculates distance between 2 vectors
double getdist(vector3D &u, vector3D &v) {
double dist =
sqrt(pow((u.X - v.X), 2) + pow((u.Y - v.Y), 2) + pow((u.Z - v.Z), 2));
return dist;
}
// This function computes projections of neighbors
//#Recheck danial: What is this for ?!?
double getProjection(vector3D &u, vector3D &v) {
double projection =
getScalarproduct(u, v) /
(u.getNorm() * v.getNorm()); // getScalarproduct() is outside of
// vector3D class so it is called as a
// function; getNorm() is a property of
// vector (to access .getNorm())
return projection;
}
string vector3D::print() {
stringstream res;
res << " X: " << X << ", Y: " << Y << ", Z: " << Z << " ";
return res.str();
}