M5Unit-IMU 0.0.2 git rev:e5b00aa
Loading...
Searching...
No Matches
vector3.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_IMU_MATH_VECTOR3_HPP
11#define M5_UNIT_IMU_MATH_VECTOR3_HPP
12
13#include <cmath>
14
15namespace m5 {
16namespace math {
17
19constexpr float VECTOR3_ZERO_EPSILON{1e-9f};
20
25struct Vector3 {
26 float x{};
27 float y{};
28 float z{};
29
30 Vector3() = default;
32 constexpr Vector3(const float x_, const float y_, const float z_) : x(x_), y(y_), z(z_)
33 {
34 }
35
37 inline Vector3 operator+(const Vector3& rhs) const
38 {
39 return Vector3(x + rhs.x, y + rhs.y, z + rhs.z);
40 }
42 inline Vector3 operator-(const Vector3& rhs) const
43 {
44 return Vector3(x - rhs.x, y - rhs.y, z - rhs.z);
45 }
47 inline Vector3 operator*(const float scalar) const
48 {
49 return Vector3(x * scalar, y * scalar, z * scalar);
50 }
52 inline float dot(const Vector3& rhs) const
53 {
54 return x * rhs.x + y * rhs.y + z * rhs.z;
55 }
57 inline Vector3 cross(const Vector3& rhs) const
58 {
59 return Vector3(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
60 }
62 inline float norm() const
63 {
64 return std::sqrt(x * x + y * y + z * z);
65 }
70 bool normalize()
71 {
72 const float n = norm();
73 if (n < VECTOR3_ZERO_EPSILON) {
74 return false;
75 }
76 const float inv = 1.0f / n;
77 x *= inv;
78 y *= inv;
79 z *= inv;
80 return true;
81 }
83 inline Vector3 normalized() const
84 {
85 Vector3 v(*this);
86 v.normalize();
87 return v;
88 }
89};
90
91} // namespace math
92} // namespace m5
93#endif
Top level namespace of M5Stack.
Definition ahrs_controller.cpp:26
Plain 3D vector (float)
Definition vector3.hpp:25
Vector3 operator-(const Vector3 &rhs) const
Component-wise subtraction.
Definition vector3.hpp:42
float dot(const Vector3 &rhs) const
Dot product.
Definition vector3.hpp:52
constexpr Vector3(const float x_, const float y_, const float z_)
Construct from components.
Definition vector3.hpp:32
Vector3 normalized() const
Return a normalized copy (zero vector maps to zero vector)
Definition vector3.hpp:83
Vector3 operator+(const Vector3 &rhs) const
Component-wise addition.
Definition vector3.hpp:37
Vector3 cross(const Vector3 &rhs) const
Cross product (right-handed)
Definition vector3.hpp:57
Vector3 operator*(const float scalar) const
Scalar multiplication.
Definition vector3.hpp:47
bool normalize()
Normalize this vector in place.
Definition vector3.hpp:70
float z
Z component.
Definition vector3.hpp:28
float norm() const
Euclidean norm (magnitude)
Definition vector3.hpp:62
float x
X component.
Definition vector3.hpp:26
float y
Y component.
Definition vector3.hpp:27
constexpr float VECTOR3_ZERO_EPSILON
Vectors shorter than this are treated as zero length (normalize safety)
Definition vector3.hpp:19