Skip to content

Commit b5464bb

Browse files
committed
CVector refactor
1 parent 33e834e commit b5464bb

File tree

13 files changed

+1203
-693
lines changed

13 files changed

+1203
-693
lines changed

plugin_III/game_III/CVector.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

plugin_III/game_III/CVector.h

Lines changed: 79 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -5,222 +5,96 @@
55
Do not delete this comment block. Respect others' work!
66
*/
77
#pragma once
8-
98
#include "PluginBase.h"
109
#include "RenderWare.h"
11-
#include <math.h>
1210

11+
class CMatrix;
1312
class CVector2D;
1413

15-
class CVector {
14+
class CVector
15+
{
1616
public:
1717
float x, y, z;
1818

19-
inline CVector() {
20-
x = 0.0f;
21-
y = 0.0f;
22-
z = 0.0f;
23-
}
24-
25-
inline CVector(float X, float Y, float Z) {
26-
x = X; y = Y; z = Z;
27-
}
28-
29-
inline CVector(CVector const& src) {
30-
x = src.x; y = src.y; z = src.z;
31-
}
32-
33-
inline CVector(RwV3d const &right) {
34-
FromRwV3d(right);
35-
}
36-
37-
inline void Cross(CVector const& a, CVector const& b) {
38-
this->x = b.z * a.y - a.z * b.y;
39-
this->y = a.z * b.x - a.x * b.z;
40-
this->z = a.x * b.y - b.x * a.y;
41-
}
42-
43-
CVector(const CVector2D& vec2d, float zValue = 0.0f);
44-
45-
CVector2D To2D() const;
46-
void From2D(const CVector2D& vec2d, float zValue = 0.0f);
47-
48-
inline float Heading() const {
49-
return std::atan2(-x, y);
50-
}
51-
52-
inline float Magnitude() {
53-
return sqrtf(this->x * this->x + this->y * this->y + this->z * this->z);
54-
}
55-
56-
inline float MagnitudeSqr() const {
57-
return x * x + y * y + z * z;
58-
}
59-
60-
inline float Magnitude2D() {
61-
return sqrtf(this->x * this->x + this->y * this->y);
62-
}
63-
64-
inline float MagnitudeSqr2D() const {
65-
return x * x + y * y;
66-
}
67-
68-
CVector operator-() const {
69-
return CVector(-x, -y, -z);
70-
}
71-
72-
inline void Sum(CVector &a, CVector &b) {
73-
this->x = a.x + b.x;
74-
this->y = a.y + b.y;
75-
this->z = a.z + b.z;
76-
}
77-
78-
inline void Difference(CVector &a, CVector &b) {
79-
this->x = a.x - b.x;
80-
this->y = a.y - b.y;
81-
this->z = a.z - b.z;
82-
}
83-
84-
inline void operator=(const CVector& right) {
85-
this->x = right.x;
86-
this->y = right.y;
87-
this->z = right.z;
88-
}
89-
90-
inline void operator+=(const CVector& right) {
91-
this->x += right.x;
92-
this->y += right.y;
93-
this->z += right.z;
94-
}
95-
96-
inline void operator-=(const CVector& right) {
97-
this->x -= right.x;
98-
this->y -= right.y;
99-
this->z -= right.z;
100-
}
101-
102-
inline void operator *= (float multiplier) {
103-
this->x *= multiplier;
104-
this->y *= multiplier;
105-
this->z *= multiplier;
106-
}
107-
108-
inline void operator /= (float divisor) {
109-
this->x /= divisor;
110-
this->y /= divisor;
111-
this->z /= divisor;
112-
}
113-
114-
inline bool operator==(const CVector& other) {
115-
return x == other.x && y == other.y && z == other.z;
116-
}
117-
118-
inline bool operator!=(const CVector& other) {
119-
return x != other.x || y != other.y || z != other.z;
120-
}
121-
122-
CVector Normalise() {
123-
float sq = MagnitudeSqr();
124-
if (sq > 0.0f) {
125-
float invsqrt = 1.0f / std::sqrt(sq);
126-
x *= invsqrt;
127-
y *= invsqrt;
128-
z *= invsqrt;
129-
}
130-
else
131-
x = 1.0f;
132-
133-
return *this;
134-
}
135-
136-
void Normalise2D(void) {
137-
float sq = MagnitudeSqr2D();
138-
float invsqrt = 1.0f / std::sqrt(sq);
139-
x *= invsqrt;
140-
y *= invsqrt;
141-
}
142-
143-
float NormaliseAndMag() {
144-
float sq = MagnitudeSqr();
145-
if (sq > 0.0f) {
146-
float invsqrt = 1.0f / std::sqrt(sq);
147-
x *= invsqrt;
148-
y *= invsqrt;
149-
z *= invsqrt;
150-
return 1.0f / invsqrt;
151-
}
152-
else {
153-
x = 1.0f;
154-
return 1.0f;
155-
}
156-
}
157-
158-
inline bool IsNormalized() const {
159-
return std::fabs(MagnitudeSqr() - 1.0f) < 0.001f;
160-
}
161-
162-
inline RwV3d ToRwV3d() const {
163-
return{ x, y, z };
164-
}
165-
166-
inline void FromRwV3d(RwV3d const &rwvec) {
167-
x = rwvec.x; y = rwvec.y; z = rwvec.z;
168-
}
169-
170-
inline void Set(float X, float Y, float Z) {
171-
x = X; y = Y; z = Z;
172-
}
173-
174-
bool IsZero() const {
175-
return x == 0.0f && y == 0.0f && z == 0.0f;
176-
}
177-
178-
inline void Zero() {
179-
x = 0.0f;
180-
y = 0.0f;
181-
z = 0.0f;
182-
}
19+
// constructors
20+
CVector() = default;
21+
explicit CVector(float value);
22+
CVector(float x, float y, float z);
23+
CVector(const CVector& src);
24+
CVector(const RwV3d& src);
25+
explicit CVector(const CVector2D& xy, float z = 0.0f);
26+
27+
// assignments
28+
void Set(float value); // assign value to all components
29+
void Set(float x, float y, float z);
30+
void operator =(const CVector& src);
31+
void FromRwV3d(const RwV3d& src);
32+
void From2D(const CVector2D& xy, float z = 0.0f);
33+
void FromSum(const CVector& left, const CVector& right); // store sum of two vectors
34+
void FromDiff(const CVector& left, const CVector& right); // store left - right substraction result
35+
void FromLerp(const CVector& begin, const CVector& end, float progress); // store result of linear interpolation between points
36+
void FromCross(const CVector& left, const CVector& right); // store result of cross product
37+
void FromMultiply(const CMatrix& matrix, const CVector& vector); // store result of matrix and vector multiplication
38+
void FromMultiply3x3(const CMatrix& matrix, const CVector& vector); // store result of matrix and vector multiplication
39+
40+
// conversions
41+
RwV3d ToRwV3d() const;
42+
CVector2D To2D() const; // get XY
43+
44+
// properties
45+
bool operator ==(const CVector& other) const;
46+
bool operator !=(const CVector& other) const;
47+
CVector operator -() const; // opposite vector
48+
float Distance(const CVector& other) const; // distance to other
49+
float Distance2D(const CVector& other) const; // XY distance to other
50+
float Distance2D(const CVector2D& other) const; // XY distance to other
51+
float Dot(const CVector& other) const; // dot product
52+
CVector Cross(const CVector& other) const; // cross product
53+
float Heading() const; // direction of XY vec in radians
54+
float Magnitude() const; // length
55+
float MagnitudeSqr() const; // length^2
56+
float Magnitude2D() const; // XY vec length
57+
float MagnitudeSqr2D() const; // XY vec length^2
58+
bool IsNormalized() const; // length is 1.0 +/- 0.001
59+
bool IsZero() const; // all components are 0.0
60+
61+
// modifiers
62+
void operator +=(float value); // add to all components
63+
void operator +=(const CVector& other);
64+
void operator -=(float value); // substract from all components
65+
void operator -=(const CVector& other);
66+
void operator *=(float multiplier); // multiply all components
67+
void operator /=(float divisor); // divide all components
68+
void Normalise(); // scale to 1.0 length
69+
float NormaliseAndMag(); // normalize and return previous length
70+
71+
// static functions
72+
static CVector Sum(const CVector& left, const CVector& right); // result of left + right
73+
static CVector Diff(const CVector& left, const CVector& right); // result of left - right
74+
static float Distance(const CVector& left, const CVector& right); // distance between points
75+
static float Distance2D(const CVector& left, const CVector& right); // XY distance between points
76+
static float Distance2D(const CVector& left, const CVector2D& right); // XY distance between points
77+
static CVector Lerp(const CVector& begin, const CVector& end, float progress); // result of linear interpolation between points
78+
static float Dot(const CVector& left, const CVector& right); // result of dot product
79+
static CVector Cross(const CVector& left, const CVector& right); // result of cross product
80+
static CVector Multiply(const CMatrix& matrix, const CVector& point); // result of matrix and point multiplication
81+
static CVector Multiply3x3(const CMatrix& matrix, const CVector& vector); // result of matrix and vector multiplication
18382
};
83+
VALIDATE_SIZE(CVector, 0xC);
18484

185-
inline CVector operator-(const CVector& vecOne, const CVector& vecTwo) {
186-
return CVector(vecOne.x - vecTwo.x, vecOne.y - vecTwo.y, vecOne.z - vecTwo.z);
187-
}
188-
189-
inline CVector operator+(const CVector& vecOne, const CVector& vecTwo) {
190-
return CVector(vecOne.x + vecTwo.x, vecOne.y + vecTwo.y, vecOne.z + vecTwo.z);
191-
}
192-
193-
inline CVector operator*(const CVector& vec, float multiplier) {
194-
return CVector(vec.x * multiplier, vec.y * multiplier, vec.z * multiplier);
195-
}
196-
197-
inline CVector operator*(float multiplier, const CVector& vec) {
198-
return CVector(vec.x * multiplier, vec.y * multiplier, vec.z * multiplier);
199-
}
200-
201-
inline CVector operator*(const CVector& vecOne, const CVector& vecTwo) {
202-
return CVector(vecOne.x * vecTwo.x, vecOne.y * vecTwo.y, vecOne.z * vecTwo.z);
203-
}
204-
205-
inline CVector operator/(const CVector& left, float right) {
206-
return CVector(left.x / right, left.y / right, left.z / right);
207-
}
85+
// static operators
86+
CVector operator +(float value, const CVector& vec);
87+
CVector operator +(const CVector& vec, float value);
88+
CVector operator +(const CVector& left, const CVector& right);
20889

209-
inline CVector operator*(const RwMatrix& mat, const CVector& vec) {
210-
return CVector(mat.right.x * vec.x + mat.up.x * vec.y + mat.at.x * vec.z + mat.pos.x,
211-
mat.right.y * vec.x + mat.up.y * vec.y + mat.at.y * vec.z + mat.pos.y,
212-
mat.right.z * vec.x + mat.up.z * vec.y + mat.at.z * vec.z + mat.pos.z);
213-
}
90+
CVector operator -(float value, const CVector& vec);
91+
CVector operator -(const CVector& vec, float value);
92+
CVector operator -(const CVector& left, const CVector& right);
21493

215-
inline float DistanceBetweenPoints(const CVector &pointOne, const CVector &pointTwo) {
216-
CVector diff = pointTwo - pointOne;
217-
return diff.Magnitude();
218-
}
94+
CVector operator *(float value, const CVector& vec);
95+
CVector operator *(const CVector& vec, float value);
21996

220-
inline float DotProduct(const CVector& v1, const CVector& v2) {
221-
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
222-
}
97+
CVector operator /(float value, const CVector& vec);
98+
CVector operator /(const CVector& vec, float value);
22399

224-
inline CVector CrossProduct(const CVector& v1, const CVector& v2) {
225-
return CVector(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x);
226-
}
100+
#include "CVectorImplementation.h" // inlined functions

0 commit comments

Comments
 (0)