-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_approach.cpp
More file actions
78 lines (66 loc) · 2.72 KB
/
test_approach.cpp
File metadata and controls
78 lines (66 loc) · 2.72 KB
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
#include <cmath>
#include <cassert>
class IVec3 {
float m_x = 0, m_y = 0, m_z = 0;
public:
IVec3() = default;
IVec3(float x, float y, float z) : m_x(x), m_y(y), m_z(z) {}
virtual ~IVec3() = default;
virtual float x() const noexcept { return m_x; }
virtual float y() const noexcept { return m_y; }
virtual float z() const noexcept { return m_z; }
virtual void setX(float v) { m_x = v; }
virtual void setY(float v) { m_y = v; }
virtual void setZ(float v) { m_z = v; }
IVec3 add(const IVec3& rhs) const { return IVec3(x()+rhs.x(), y()+rhs.y(), z()+rhs.z()); }
IVec3 sub(const IVec3& rhs) const { return IVec3(x()-rhs.x(), y()-rhs.y(), z()-rhs.z()); }
IVec3 scale(float s) const { return IVec3(x()*s, y()*s, z()*s); }
float dot(const IVec3& rhs) const { return x()*rhs.x()+y()*rhs.y()+z()*rhs.z(); }
IVec3 cross(const IVec3& rhs) const { return IVec3(y()*rhs.z()-z()*rhs.y(), z()*rhs.x()-x()*rhs.z(), x()*rhs.y()-y()*rhs.x()); }
float magnitude() const { return std::sqrt(x()*x()+y()*y()+z()*z()); }
IVec3 normalized() const { float m=magnitude(); return m>0? scale(1.0f/m) : IVec3(); }
float distanceTo(const IVec3& o) const { return sub(o).magnitude(); }
bool operator==(const IVec3& o) const { return x()==o.x() && y()==o.y() && z()==o.z(); }
static IVec3 zero() { return IVec3(0,0,0); }
static IVec3 one() { return IVec3(1,1,1); }
static IVec3 up() { return IVec3(0,1,0); }
};
class Vec3 final : public IVec3 {
float m_x, m_y, m_z;
public:
Vec3() : m_x(0), m_y(0), m_z(0) {}
Vec3(float x, float y, float z) : m_x(x), m_y(y), m_z(z) {}
float x() const noexcept override { return m_x; }
float y() const noexcept override { return m_y; }
float z() const noexcept override { return m_z; }
void setX(float v) override { m_x = v; }
void setY(float v) override { m_y = v; }
void setZ(float v) override { m_z = v; }
// No need to override add/sub/etc -- base class implementations use virtual x()/y()/z()
};
int main() {
// Test through interface pointer
Vec3 a(1,2,3);
Vec3 b(4,5,6);
IVec3* p = &a;
IVec3 c = p->add(b);
assert(c.x() == 5.0f);
assert(c.y() == 7.0f);
assert(c.z() == 9.0f);
// Test dot product
float d = a.dot(b);
assert(d == 32.0f);
// Test normalized
Vec3 v(0, 3, 4);
IVec3 n = v.normalized();
assert(std::fabs(n.magnitude() - 1.0f) < 1e-5f);
// Test zero vector normalization
IVec3 z = IVec3::zero();
IVec3 zn = z.normalized();
assert(zn.magnitude() < 1e-5f);
// Test through interface pointer
IVec3* p2 = &v;
IVec3 n2 = p2->normalized();
assert(std::fabs(n2.magnitude() - 1.0f) < 1e-5f);
return 0;
}