-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector3.h
146 lines (136 loc) · 3.57 KB
/
vector3.h
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
141
142
143
144
145
146
#ifndef __VECTOR3_H
#define __VECTOR3_H
#include <iostream>
#include <fstream>
#include <cmath>
template <class REAL> struct vector3{
public:
REAL x, y, z;
vector3(){
x = y = z = REAL(0);
}
vector3(const REAL &r){
x = y = z = r;
}
vector3(const REAL &_x, const REAL &_y, const REAL &_z){
x = _x; y = _y; z = _z;
}
vector3(const REAL *p){
x = p[0]; y = p[1]; z = p[2];
}
~vector3(){}
REAL &operator [](int i){
return (&x)[i];
}
template <class real>
operator vector3<real> () const {
return vector3<real> (real(x), real(y), real(z));
}
operator REAL *(){
return &x;
}
operator const REAL *() const{
return &x;
}
REAL (*toPointer())[3]{
return (REAL (*)[3])&x;
}
const REAL (*toConstPointer())[3]{
return (const REAL (*)[3])&x;
}
typedef REAL (*pArrayOfReal3)[3];
typedef const REAL (*cpArrayOfReal3)[3];
operator pArrayOfReal3(){
return toPointer();
}
operator cpArrayOfReal3(){
return toConstPointer();
}
void outv(std::ostream &ofs = std::cout) const{
ofs << "(" << x << ", " << y << ", " << z << ")" << std::endl;
}
bool are_numbers () const{
// returns false if *this has (a) NaN member(s)
return (norm2() >= REAL(0));
}
REAL norm2() const{
return (*this)*(*this);
}
REAL abs() const{
return std::sqrt(norm2());
}
friend std::ostream &operator << (std::ostream &ofs, const vector3<REAL> &v){
ofs << v.x << " " << v.y << " " << v.z;
return ofs;
}
friend std::istream &operator >> (std::istream &ifs, vector3<REAL> &v){
ifs >> v.x >> v.y >> v.z;
return ifs;
}
const vector3<REAL> operator + (const vector3<REAL> &v) const{
return vector3<REAL> (x+v.x, y+v.y, z+v.z);
}
const vector3<REAL> operator - (const vector3<REAL> &v) const{
return vector3<REAL> (x-v.x, y-v.y, z-v.z);
}
const vector3<REAL> operator * (const REAL &s) const{
return vector3<REAL> (x*s, y*s, z*s);
}
friend const vector3<REAL> operator * (const REAL &s, const vector3<REAL> &v){
return v*s;
}
// dot product
const REAL operator * (const vector3<REAL> &v) const{
return (x*v.x + y*v.y + z*v.z);
}
const vector3<REAL> cross(const vector3<REAL> &v) const
{
return vector3<REAL>(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
}
// vector product
const vector3<REAL> operator % (const vector3<REAL> &v) const{
return vector3<REAL> (
y*v.z - z*v.y,
z*v.x - x*v.z,
x*v.y - y*v.x);
}
const vector3<REAL> operator / (const REAL &s) const{
REAL r = REAL(1)/s;
return (*this)*r;
}
const vector3<REAL> operator = (const vector3<REAL> &v){
x = v.x; y=v.y; z=v.z;
return *this;
}
const vector3<REAL> operator - () const {
return vector3<REAL> (-x, -y, -z);
}
const vector3<REAL> &operator += (const vector3<REAL> &v){
*this = *this + v;
return *this;
}
const vector3<REAL> &operator -= (const vector3<REAL> &v){
*this = *this - v;
return *this;
}
const vector3<REAL> &operator *= (const REAL &s){
*this = *this * s;
return *this;
}
const vector3<REAL> &operator /= (const REAL &s){
*this = *this / s;
return *this;
}
friend const vector3<REAL> maxeach (const vector3<REAL> &a, const vector3<REAL> &b){
return vector3<REAL> (std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z));
}
friend const vector3<REAL> mineach (const vector3<REAL> &a, const vector3<REAL> &b){
return vector3<REAL> (std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z));
}
const vector3<REAL> abseach() const {
return vector3<REAL> (std::fabs(x), std::fabs(y), std::fabs(z));
}
};
typedef double real;
typedef vector3<real> vec3;
#endif // __VECTOR3_H