FCT
载入中...
搜索中...
未找到
Vec.h
浏览该文件的文档.
1//
2// Created by Administrator on 2025/4/15.
3//
4
5#ifndef VEC_H
6#define VEC_H
7namespace FCT {
8 struct Vec2
9 {
10 float x, y;
11 Vec2(int x, int y) {
12 this->x = static_cast<float>(x);
13 this->y = static_cast<float>(y);
14 }
15 Vec2(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
16 Vec2(uint32_t x, uint32_t y)
17 {
18 this->x = static_cast<float>(x);
19 this->y = static_cast<float>(y);
20 }
21
22 Vec2 &operator+=(const Vec2 &rhs)
23 {
24 x += rhs.x;
25 y += rhs.y;
26 return *this;
27 }
28 Vec2 operator+(const Vec2 &rhs) const
29 {
30 return Vec2(x + rhs.x, y + rhs.y);
31 }
32 Vec2 operator-(const Vec2 &rhs) const
33 {
34 return Vec2(x - rhs.x, y - rhs.y);
35 }
36 Vec2 operator*(const Vec2 &rhs) const
37 {
38 return Vec2(x * rhs.x, y * rhs.y);
39 }
40 Vec2 operator*(float scalar) const
41 {
42 return Vec2(x * scalar, y * scalar);
43 }
44 Vec2 operator/(float scalar) const
45 {
46 return Vec2(x / scalar, y / scalar);
47 }
48 Vec2 operator/(const Vec2 &rhs) const
49 {
50 return Vec2(x / rhs.x, y / rhs.y);
51 }
52 bool operator==(const Vec2& other) const
53 {
54 return (x == other.x) && (y == other.y);
55 }
56 float length() const
57 {
58 return std::sqrt(x * x + y * y);
59 }
60 float distance(const Vec2 &other) const
61 {
62 return std::sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
63 }
64 };
65
66 struct Vec3
67 {
68#ifdef FCT_USE_PHYSX
69 operator physx::PxExtendedVec3() const {
70 return physx::PxExtendedVec3(x, y, z);
71 }
72 operator physx::PxVec3() const {
73 return physx::PxVec3(x, y, z);
74 }
75 Vec3(const physx::PxVec3& other) {
76 x = other.x;
77 y = other.y;
78 z = other.z;
79 }
80#endif
81 float x, y, z;
82 Vec3(float x = 0.0f, float y = 0.0f, float z = 0.0f) : x(x), y(y), z(z) {}
83
84 Vec3(const Vec3 &other)
85 {
86 x = other.x;
87 y = other.y;
88 z = other.z;
89 }
90
92 {
93 return {-x, -y, -z};
94 }
95
96 bool operator==(const Vec3 &other) const
97 {
98 return (x == other.x) && (y == other.y) && (z == other.z);
99 }
100
101 Vec3 &operator+=(const Vec3 &rhs)
102 {
103 x += rhs.x;
104 y += rhs.y;
105 z += rhs.z;
106 return *this;
107 }
108 bool operator<(const Vec3 &other) const
109 {
110 return (x < other.x) && (y < other.y) && (z < other.z);
111 }
112
113 float lengthSquared() const
114 {
115 return x * x + y * y + z * z;
116 }
117 Vec3 &operator-=(const Vec3 &rhs)
118 {
119 x -= rhs.x;
120 y -= rhs.y;
121 z -= rhs.z;
122 return *this;
123 }
124 Vec3 operator+(const Vec3 &rhs) const
125 {
126 return Vec3(x + rhs.x, y + rhs.y, z + rhs.z);
127 }
128
129 Vec3 operator-(const Vec3 &rhs) const
130 {
131 return Vec3(x - rhs.x, y - rhs.y, z - rhs.z);
132 }
133 Vec3 operator*(float scalar) const
134 {
135 return Vec3(x * scalar, y * scalar, z * scalar);
136 }
137 float length() const
138 {
139 return std::sqrt(x * x + y * y + z * z);
140 }
142 {
143 float length = std::sqrt(x * x + y * y + z * z);
144 return Vec3(x / length, y / length, z / length);
145 }
146 Vec3 cross(const Vec3 &other) const
147 {
148 return Vec3(
149 y * other.z - z * other.y,
150 z * other.x - x * other.z,
151 x * other.y - y * other.x);
152 }
153 Vec3 rotate(const Vec3 &axis, float angle) const
154 {
155 float cosTheta = std::cos(angle);
156 float sinTheta = std::sin(angle);
157 Vec3 normalizedAxis = axis.normalize();
158
159 return Vec3(
160 (cosTheta + (1 - cosTheta) * normalizedAxis.x * normalizedAxis.x) * x +
161 ((1 - cosTheta) * normalizedAxis.x * normalizedAxis.y - sinTheta * normalizedAxis.z) * y +
162 ((1 - cosTheta) * normalizedAxis.x * normalizedAxis.z + sinTheta * normalizedAxis.y) * z,
163
164 ((1 - cosTheta) * normalizedAxis.x * normalizedAxis.y + sinTheta * normalizedAxis.z) * x +
165 (cosTheta + (1 - cosTheta) * normalizedAxis.y * normalizedAxis.y) * y +
166 ((1 - cosTheta) * normalizedAxis.y * normalizedAxis.z - sinTheta * normalizedAxis.x) * z,
167
168 ((1 - cosTheta) * normalizedAxis.x * normalizedAxis.z - sinTheta * normalizedAxis.y) * x +
169 ((1 - cosTheta) * normalizedAxis.y * normalizedAxis.z + sinTheta * normalizedAxis.x) * y +
170 (cosTheta + (1 - cosTheta) * normalizedAxis.z * normalizedAxis.z) * z);
171 }
172 float dot(const Vec3& rhs) const {
173 return x * rhs.x + y * rhs.y + z * rhs.z;
174 }
175 };
176 inline Vec3 normalize(const Vec3 &v)
177 {
178 float length = std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
179 return Vec3(v.x / length, v.y / length, v.z / length);
180 }
181
182 inline Vec3 cross(const Vec3 &a, const Vec3 &b)
183 {
184 return Vec3(
185 a.y * b.z - a.z * b.y,
186 a.z * b.x - a.x * b.z,
187 a.x * b.y - a.y * b.x);
188 }
189
190 inline float dot(const Vec3 &a, const Vec3 &b)
191 {
192 return a.x * b.x + a.y * b.y + a.z * b.z;
193 }
194 struct Vec4
195 {
196 float x, y, z, w;
197 Vec4(float x = 0.0f, float y = 0.0f, float z = 0.0f, float w = 0.0f)
198 : x(x), y(y), z(z), w(w) {}
199 Vec4(Vec3 vec, float w = 0.0f)
200 : x(vec.x), y(vec.y), z(vec.z), w(w) {}
201 Vec4(Vec2 v, float z = 0.0f, float w = 0.0f)
202 : x(v.x), y(v.y), z(z), w(w) {}
203 Vec4 operator-(const Vec4 &rhs) const
204 {
205 return Vec4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
206 }
207
209 {
210 return Vec4(-x, -y, -z, -w);
211 }
213 {
214 float length = std::sqrt(x * x + y * y + z * z + w * w);
215 if (length > 0.0f) {
216 return Vec4(x / length, y / length, z / length, w / length);
217 }
218 return Vec4(0.0f, 0.0f, 0.0f, 0.0f);
219 }
220 Vec3 xyz() const
221 {
222 return Vec3(x, y, z);
223 }
224 };
225 template<typename T>
226 struct Vector4
227 {
228 T x, y, z, w;
229 Vector4(T x = 0, T y = 0, T z = 0, T w = 0) : x(x), y(y), z(z), w(w) {}
230 };
231
232}
233#endif //VEC_H
float dot(const Vec3 &a, const Vec3 &b)
定义 Vec.h:190
Vec3 normalize(const Vec3 &v)
定义 Vec.h:176
Vec3 cross(const Vec3 &a, const Vec3 &b)
定义 Vec.h:182
Vec2 operator*(const Vec2 &rhs) const
定义 Vec.h:36
Vec2(int x, int y)
定义 Vec.h:11
float distance(const Vec2 &other) const
定义 Vec.h:60
float x
定义 Vec.h:10
float length() const
定义 Vec.h:56
bool operator==(const Vec2 &other) const
定义 Vec.h:52
Vec2 operator/(const Vec2 &rhs) const
定义 Vec.h:48
Vec2(float x=0.0f, float y=0.0f)
定义 Vec.h:15
Vec2 operator*(float scalar) const
定义 Vec.h:40
Vec2(uint32_t x, uint32_t y)
定义 Vec.h:16
Vec2 operator/(float scalar) const
定义 Vec.h:44
Vec2 & operator+=(const Vec2 &rhs)
定义 Vec.h:22
Vec2 operator-(const Vec2 &rhs) const
定义 Vec.h:32
Vec2 operator+(const Vec2 &rhs) const
定义 Vec.h:28
float y
定义 Vec.h:10
float x
定义 Vec.h:81
Vec3(const Vec3 &other)
定义 Vec.h:84
float dot(const Vec3 &rhs) const
定义 Vec.h:172
Vec3 operator-(const Vec3 &rhs) const
定义 Vec.h:129
Vec3 operator-() const
定义 Vec.h:91
bool operator<(const Vec3 &other) const
定义 Vec.h:108
Vec3(float x=0.0f, float y=0.0f, float z=0.0f)
定义 Vec.h:82
Vec3 cross(const Vec3 &other) const
定义 Vec.h:146
Vec3 normalize() const
定义 Vec.h:141
Vec3 rotate(const Vec3 &axis, float angle) const
定义 Vec.h:153
float z
定义 Vec.h:81
float y
定义 Vec.h:81
float length() const
定义 Vec.h:137
bool operator==(const Vec3 &other) const
定义 Vec.h:96
Vec3 & operator+=(const Vec3 &rhs)
定义 Vec.h:101
Vec3 operator*(float scalar) const
定义 Vec.h:133
float lengthSquared() const
定义 Vec.h:113
Vec3 operator+(const Vec3 &rhs) const
定义 Vec.h:124
Vec3 & operator-=(const Vec3 &rhs)
定义 Vec.h:117
float z
定义 Vec.h:196
float y
定义 Vec.h:196
float w
定义 Vec.h:196
Vec4(Vec2 v, float z=0.0f, float w=0.0f)
定义 Vec.h:201
Vec4 operator-() const
定义 Vec.h:208
Vec3 xyz() const
定义 Vec.h:220
Vec4(float x=0.0f, float y=0.0f, float z=0.0f, float w=0.0f)
定义 Vec.h:197
Vec4 normalize() const
定义 Vec.h:212
Vec4 operator-(const Vec4 &rhs) const
定义 Vec.h:203
float x
定义 Vec.h:196
Vec4(Vec3 vec, float w=0.0f)
定义 Vec.h:199
Vector4(T x=0, T y=0, T z=0, T w=0)
定义 Vec.h:229