-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfvector3.hpp
163 lines (136 loc) · 2.53 KB
/
fvector3.hpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
Copyright (c) 2018 Gombe.
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/
#ifndef __FVECTOR3_H
#define __FVECTOR3_H
#include <stdint.h>
#include <math.h>
inline
int sqrt2(int f);
struct fvector3_t{
float x;
float y;
float z;
};
class fvector3{
public:
float x;
float y;
float z;
fvector3(const float x,const float y,const float z){
this->x = x;
this->y = y;
this->z = z;
}
fvector3(const fvector3_t& v){
this->x = v.x;
this->y = v.y;
this->z = v.z;
}
fvector3(const float x,const float y){
this->x = x;
this->y = y;
this->z = 0;
}
fvector3(){
this->x = 0;
this->y = 0;
this->z = 0;
}
fvector3& operator*=(const float p){
this->x = this->x*p;
this->y = this->y*p;
this->z = this->z*p;
return *this;
}
fvector3& operator+=(const fvector3& v){
this->x += v.x;
this->y += v.y;
this->z += v.z;
return *this;
}
fvector3& operator-=(const fvector3& v){
this->x -= v.x;
this->y -= v.y;
this->z -= v.z;
return *this;
}
fvector3 operator+(){return *this;}
fvector3 operator-(){
fvector3 v;
v.x = -this->x;
v.y = -this->y;
v.z = -this->z;
return v;
}
float abs(){
return sqrtf(x*x+y*y+z*z);
}
float sqabs(){
return (x*x+y*y+z*z);
}
fvector3& normalize(){
float r = 1.f/abs();
x *= r;
y *= r;
z *= r;
return *this;
}
void print();
fvector3& operator=(const fvector3& v){
this->x = v.x;
this->y = v.y;
this->z = v.z;
return *this;
}
};
static inline
fvector3 cross(fvector3 p1,fvector3 p2){
fvector3 v;
v.x = p1.y*p2.z - p1.z*p2.y;
v.y = p1.z*p2.x - p1.x*p2.z;
v.z = p1.x*p2.y - p1.y*p2.x;
return v;
}
static inline
fvector3 operator+(const fvector3 &v1,const fvector3& v2){
fvector3 w;
w.x = v1.x+v2.x;
w.y = v1.y+v2.y;
w.z = v1.z+v2.z;
return w;
}
static inline
float operator*(const fvector3 &v1,const fvector3 &v2){
return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z;
}
static inline
fvector3 operator*(const fvector3 &v1,const float n){
fvector3 w;
w.x = v1.x*n;
w.y = v1.y*n;
w.z = v1.z*n;
return w;
}
static inline
fvector3 operator-(const fvector3 &v1,const fvector3& v2){
fvector3 w;
w.x = v1.x-v2.x;
w.y = v1.y-v2.y;
w.z = v1.z-v2.z;
return w;
}
inline
fvector3 calc_nv(const fvector3 v[3]){
fvector3 n;
float rabs;
n = cross(v[1]-v[0],v[2]-v[0]);
rabs = 1.f/n.abs();
n.x = n.x * rabs;
n.y = n.y * rabs;
n.z = n.z * rabs;
return n;
}
#endif