File: Vector3.hpp

package info (click to toggle)
therion 6.3.4-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,544 kB
  • sloc: cpp: 195,273; tcl: 19,779; ansic: 8,434; perl: 1,895; makefile: 1,281; python: 255; asm: 219; sh: 104
file content (140 lines) | stat: -rw-r--r-- 2,667 bytes parent folder | download | duplicates (4)
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
139
140
#ifndef QuickHull_Vector3_hpp
#define QuickHull_Vector3_hpp

#include <cmath>
#include <iostream>

namespace quickhull {
	
	template <typename T>
	class Vector3
	{
	public:
		Vector3() = default;
		
		Vector3(T x, T y, T z) : x(x), y(y), z(z) {
			
		}
		
		T x,y,z;
		
		T dotProduct(const Vector3& other) const {
			return x*other.x+y*other.y+z*other.z;
		}
		
		void normalize() {
			const T len = getLength();
			x/=len;
			y/=len;
			z/=len;
		}
		
		Vector3 getNormalized() const {
			const T len = getLength();
			return Vector3(x/len,y/len,z/len);
		}
		
		T getLength() const {
			return std::sqrt(x*x+y*y+z*z);
		}
		
		Vector3 operator-(const Vector3& other) const {
			return Vector3(x-other.x,y-other.y,z-other.z);
		}
		
		Vector3 operator+(const Vector3& other) const {
			return Vector3(x+other.x,y+other.y,z+other.z);
		}
		
		Vector3& operator+=(const Vector3& other) {
			x+=other.x;
			y+=other.y;
			z+=other.z;
			return *this;
		}
		Vector3& operator-=(const Vector3& other) {
			x-=other.x;
			y-=other.y;
			z-=other.z;
			return *this;
		}
		Vector3& operator*=(T c) {
			x*=c;
			y*=c;
			z*=c;
			return *this;
		}
		
		Vector3& operator/=(T c) {
			x/=c;
			y/=c;
			z/=c;
			return *this;
		}
		
		Vector3 operator-() const {
			return Vector3(-x,-y,-z);
		}

		template<typename S>
		Vector3 operator*(S c) const {
			return Vector3(x*c,y*c,z*c);
		}
		
		template<typename S>
		Vector3 operator/(S c) const {
			return Vector3(x/c,y/c,z/c);
		}
		
		T getLengthSquared() const {
			return x*x + y*y + z*z;
		}
		
		bool operator!=(const Vector3& o) const {
			return x != o.x || y != o.y || z != o.z;
		}
		
		// Projection onto another vector
		Vector3 projection(const Vector3& o) const {
			T C = dotProduct(o)/o.getLengthSquared();
			return o*C;
		}
		
		Vector3 crossProduct (const Vector3& rhs ) {
			T a = y * rhs.z - z * rhs.y ;
			T b = z * rhs.x - x * rhs.z ;
			T c = x * rhs.y - y * rhs.x ;
			Vector3 product( a , b , c ) ;
			return product ;
		}
		
		T getDistanceTo(const Vector3& other) const {
			Vector3 diff = *this - other;
			return diff.getLength();
		}
		
		T getSquaredDistanceTo(const Vector3& other) const {
			const T dx = x-other.x;
			const T dy = y-other.y;
			const T dz = z-other.z;
			return dx*dx+dy*dy+dz*dz;
		}
		
	};
	
	// Overload also << operator for easy printing of debug data
	template <typename T>
	inline std::ostream& operator<<(std::ostream& os, const Vector3<T>& vec) {
		os << "(" << vec.x << "," << vec.y << "," << vec.z << ")";
		return os;
	}
	
	template <typename T>
	inline Vector3<T> operator*(T c, const Vector3<T>& v) {
		return Vector3<T>(v.x*c,v.y*c,v.z*c);
	}
	
}


#endif