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