-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3d.cpp
More file actions
127 lines (105 loc) · 2.9 KB
/
Vector3d.cpp
File metadata and controls
127 lines (105 loc) · 2.9 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
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
//
// Created by mtakagi on 2024/02/17.
//
#include "Vector3d.h"
#include <cmath>
namespace aobench {
double Vector3d::x() const noexcept {
return x_;
}
double Vector3d::y() const noexcept {
return y_;
}
double Vector3d::z() const noexcept {
return z_;
}
void Vector3d::set(double x, double y, double z) noexcept {
x_ = x;
y_ = y;
z_ = z;
}
double Vector3d::dot(const Vector3d& v) const noexcept {
return x_ * v.x() + y_ * v.y() + z_ * v.z();
}
Vector3d Vector3d::cross(const Vector3d & v) const noexcept {
return {
y_ * v.z() - z_ * v.y(),
z_ * v.x() - x_ * v.z(),
x_ * v.y() - y_ * v.x()
};
}
double Vector3d::length() const noexcept {
return std::sqrt(x_ * x_ + y_ * y_ + z_ * z_);
}
Vector3d Vector3d::normalize() const noexcept {
auto length = this->length();
return {
x_ / length,
y_ / length,
z_ / length
};
}
Vector3d::Basis Vector3d::orthoBasis() const {
auto v1 = Vector3d();
if (x_ < 0.6 && x_ > -0.6) {
v1.set(1.0, 0, 0);
} else if (y_ < 0.6 && y_ > -0.6) {
v1.set(0, 1.0, 0);
} else if (z_ < 0.6 && z_ > -0.6) {
v1.set(0, 0, 1.0);
} else {
v1.set(1.0, 0, 0);
}
auto v0 = v1.cross(*this).normalize();
return {
v0,
cross(v0).normalize(),
*this,
};
}
Vector3d operator-(const Vector3d& lhs, const Vector3d& rhs) noexcept {
return {
lhs.x() - rhs.x(),
lhs.y() - rhs.y(),
lhs.z() - rhs.z(),
};
}
Vector3d operator-(const Vector3d& lhs, double rhs) noexcept {
return {
lhs.x() - rhs,
lhs.y() - rhs,
lhs.z() - rhs,
};
}
Vector3d operator+(const Vector3d& lhs, const Vector3d& rhs) noexcept {
return {
lhs.x() + rhs.x(),
lhs.y() + rhs.y(),
lhs.z() + rhs.z(),
};
}
Vector3d operator+(const Vector3d& lhs, double rhs) noexcept {
return {
lhs.x() - rhs,
lhs.y() - rhs,
lhs.z() - rhs,
};
}
Vector3d operator*(const Vector3d& lhs, const Vector3d& rhs) noexcept {
return {
lhs.x() * rhs.x(),
lhs.y() * rhs.y(),
lhs.z() * rhs.z(),
};
}
Vector3d operator*(const Vector3d& lhs, double rhs) noexcept {
return {
lhs.x() * rhs,
lhs.y() * rhs,
lhs.z() * rhs,
};
}
bool operator==(const Vector3d& lhs, const Vector3d& rhs) noexcept {
return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
}
} // aobench