ElfPSP_AntColony
Protein Structure Prediction using Ant Colony Optimization
vec3.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include <cmath>
6 #include <iostream>
7 #include <initializer_list>
8 
11 template <typename T>
12 struct vec3 {
14  T x, y, z;
17  vec3(){}
18 
20  vec3(T a, T b, T c) : x(a), y(b), z(c){}
21 
23  vec3(std::initializer_list<T> l) : x(*l.begin()), y(*(l.begin()+1)), z(*(l.begin()+2)){}
24 
26  vec3<T> operator+(const vec3<T> other) const;
27 
29  vec3<T> operator-(const vec3<T> other) const;
30 
32  vec3<T> operator-() const;
33 
35  bool operator==(const vec3<T> other) const;
36 
38  T dot(const vec3<T> other) const;
39 
41  T dot() const;
42 
44  T norm1() const;
45 
47  T norm2() const;
48 };
49 
51 template <typename T>
52 std::ostream& operator<<(std::ostream& stream, const vec3<T> vec);
53 
54 /******/
55 
56 template <typename T>
57 inline vec3<T> vec3<T>::operator+(const vec3<T> other) const {
58  vec3<T> result;
59  result.x = this->x + other.x;
60  result.y = this->y + other.y;
61  result.z = this->z + other.z;
62  return result;
63 }
64 
65 template <typename T>
66 inline vec3<T> vec3<T>::operator-(const vec3<T> other) const {
67  vec3<T> result;
68  result.x = this->x - other.x;
69  result.y = this->y - other.y;
70  result.z = this->z - other.z;
71  return result;
72 }
73 
74 template <typename T>
75 inline vec3<T> vec3<T>::operator-() const {
76  vec3<T> result;
77  result.x = -this->x;
78  result.y = -this->y;
79  result.z = -this->z;
80  return result;
81 }
82 
83 template <typename T>
84 inline bool vec3<T>::operator==(const vec3<T> other) const {
85  return (this->x == other.x) && (this->y == other.y) && (this->z == other.z);
86 }
87 
88 template <typename T>
89 inline T vec3<T>::dot(const vec3<T> other) const {
90  return this->x * other.x + this->y * other.y + this->z * other.z;
91 }
92 
93 template <typename T>
94 inline T vec3<T>::dot() const {
95  return this->x * this->x + this->y * this->y + this->z * this->z;
96 }
97 
98 template <typename T>
99 inline T vec3<T>::norm1() const {
100  return std::abs(this->x) + std::abs(this->y) + std::abs(this->z);
101 }
102 
104 template <typename T>
105 inline T vec3<T>::norm2() const {
106  return std::sqrt(this->dot());
107 }
108 
109 template <typename T>
110 inline std::ostream& operator<<(std::ostream& stream, const vec3<T> vec){
111  stream << "(" << vec.x << "," << vec.y << "," << vec.z << ")";
112  return stream;
113 }
T norm2() const
Returns the euclidean norm of the vector.
Definition: vec3.h:105
Represents a triple (x, y, z) of any type.
Definition: vec3.h:12
vec3()
Internal coordinates are not initialized and may contain junk.
Definition: vec3.h:17
vec3(std::initializer_list< T > l)
Example v = {1, 2, 3}.
Definition: vec3.h:23
vec3< T > operator-() const
Unary minus operation.
Definition: vec3.h:75
vec3< T > operator+(const vec3< T > other) const
Element-wise sum.
Definition: vec3.h:57
T z
(x,y,z) coordinates.
Definition: vec3.h:14
vec3(T a, T b, T c)
Example v = vec3(1, 2, 3).
Definition: vec3.h:20
T norm1() const
Returns the norm1 of the vector (absolute value norm).
Definition: vec3.h:99
T x
(x,y,z) coordinates.
Definition: vec3.h:14
T dot() const
Returns the dot product of the vector with itself.
Definition: vec3.h:94
T y
(x,y,z) coordinates.
Definition: vec3.h:14
bool operator==(const vec3< T > other) const
Returns true if both vectors have equal coordinates.
Definition: vec3.h:84