载入中...
搜索中...
未找到
Vec3.cpp
浏览该文件的文档.
1#include "math/Vec3.h"
2
3#include "common/Exception.h"
4
5#include <cmath>
6
7namespace eve::math {
8
10 float len = length();
11 if (len > 0.f) {
12 x_ /= len;
13 y_ /= len;
14 z_ /= len;
15 }
16}
17
19 auto *v = new Vec3(x_, y_, z_);
20 v->normalize();
21 return v;
22}
23
24float Vec3::dot(const Vec3 *other) const {
25 if (!other) throw eve::Exception("Vec3.dot: other is null");
26 return x_ * other->x_ + y_ * other->y_ + z_ * other->z_;
27}
28
29Vec3 *Vec3::cross(const Vec3 *other) const {
30 if (!other) throw eve::Exception("Vec3.cross: other is null");
31 return new Vec3(y_ * other->z_ - z_ * other->y_, z_ * other->x_ - x_ * other->z_,
32 x_ * other->y_ - y_ * other->x_);
33}
34
35float Vec3::distanceTo(const Vec3 *other) const {
36 if (!other) throw eve::Exception("Vec3.distanceTo: other is null");
37 float dx = x_ - other->x_;
38 float dy = y_ - other->y_;
39 float dz = z_ - other->z_;
40 return std::sqrt(dx * dx + dy * dy + dz * dz);
41}
42
43Vec3 *Vec3::add(const Vec3 *other) const {
44 if (!other) throw eve::Exception("Vec3.add: other is null");
45 return new Vec3(x_ + other->x_, y_ + other->y_, z_ + other->z_);
46}
47
48Vec3 *Vec3::sub(const Vec3 *other) const {
49 if (!other) throw eve::Exception("Vec3.sub: other is null");
50 return new Vec3(x_ - other->x_, y_ - other->y_, z_ - other->z_);
51}
52
53Vec3 *Vec3::scale(float s) const { return new Vec3(x_ * s, y_ * s, z_ * s); }
54
55Vec3 *Vec3::lerpTo(const Vec3 *other, float t) const {
56 if (!other) throw eve::Exception("Vec3.lerpTo: other is null");
57 return new Vec3(x_ + (other->x_ - x_) * t, y_ + (other->y_ - y_) * t,
58 z_ + (other->z_ - z_) * t);
59}
60
61Vec3 *Vec3::clone() const { return new Vec3(x_, y_, z_); }
62
63} // namespace eve::math
int v
uint32_t s
Definition Weather.cpp:28
3D float vector (script-facing math module value).
Definition Vec3.h:8
Vec3()=default
Vec3 * normalized() const
Definition Vec3.cpp:18
Vec3 * cross(const Vec3 *other) const
Definition Vec3.cpp:29
Vec3 * clone() const
Copies this vector.
Definition Vec3.cpp:61
Vec3 * sub(const Vec3 *other) const
Definition Vec3.cpp:48
Vec3 * lerpTo(const Vec3 *other, float t) const
Linear interpolation to other at t in [0,1].
Definition Vec3.cpp:55
float length() const
Magnitude (and squared magnitude).
Definition Vec3.h:29
Vec3 * scale(float s) const
Definition Vec3.cpp:53
Vec3 * add(const Vec3 *other) const
Arithmetic helpers returning new (caller-owned) vectors.
Definition Vec3.cpp:43
float dot(const Vec3 *other) const
Dot/cross product and distance.
Definition Vec3.cpp:24
float distanceTo(const Vec3 *other) const
Definition Vec3.cpp:35
void normalize()
Normalizes in place / returns a normalized copy.
Definition Vec3.cpp:9