UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
Vector3D.hpp
[詳解]
1//
2// 三次元ベクトル 前方定義
3//
4// Copyright (c) 2022-2024 udonrobo
5//
6
7#pragma once
8
11
12namespace Udon
13{
14
15 struct Vec2;
16
17
19 struct Vec3
20 {
21
23 using ValueType = double;
24
27
30
33
35 static constexpr size_t Dimension = 3;
36
38 constexpr Vec3() noexcept
39 : x()
40 , y()
41 , z()
42 {
43 }
44
48 constexpr Vec3(ValueType x, ValueType y, ValueType z) noexcept
49 : x(x)
50 , y(y)
51 , z(z)
52 {
53 }
54
56 Vec3(const Vec3&) = default;
57
59 Vec3& operator=(const Vec3&) = default;
60
61 friend constexpr Vec3 operator+(const Vec3& lhs, const Vec3& rhs) noexcept { return { lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z }; }
62 friend constexpr Vec3 operator-(const Vec3& lhs, const Vec3& rhs) noexcept { return { lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z }; }
63 friend constexpr Vec3 operator*(const Vec3& lhs, const Vec3& rhs) noexcept { return { lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z }; }
64 friend constexpr Vec3 operator/(const Vec3& lhs, const Vec3& rhs) noexcept { return { lhs.x / rhs.x, lhs.y / rhs.y, lhs.z / rhs.z }; }
65
66 friend constexpr Vec3 operator+(const Vec3& lhs, ValueType rhs) noexcept { return { lhs.x + rhs, lhs.y + rhs, lhs.z + rhs }; }
67 friend constexpr Vec3 operator-(const Vec3& lhs, ValueType rhs) noexcept { return { lhs.x - rhs, lhs.y - rhs, lhs.z - rhs }; }
68 friend constexpr Vec3 operator*(const Vec3& lhs, ValueType rhs) noexcept { return { lhs.x * rhs, lhs.y * rhs, lhs.z * rhs }; }
69 friend constexpr Vec3 operator/(const Vec3& lhs, ValueType rhs) noexcept { return { lhs.x / rhs, lhs.y / rhs, lhs.z / rhs }; }
70
71 friend constexpr Vec3 operator+(ValueType lhs, const Vec3& rhs) noexcept { return { lhs + rhs.x, lhs + rhs.y, lhs + rhs.z }; }
72 friend constexpr Vec3 operator-(ValueType lhs, const Vec3& rhs) noexcept { return { lhs - rhs.x, lhs - rhs.y, lhs - rhs.z }; }
73 friend constexpr Vec3 operator*(ValueType lhs, const Vec3& rhs) noexcept { return { lhs * rhs.x, lhs * rhs.y, lhs * rhs.z }; }
74 friend constexpr Vec3 operator/(ValueType lhs, const Vec3& rhs) noexcept { return { lhs / rhs.x, lhs / rhs.y, lhs / rhs.z }; }
75
76 Vec3& operator+=(const Vec3& rhs) noexcept { return *this = *this + rhs; };
77 Vec3& operator-=(const Vec3& rhs) noexcept { return *this = *this - rhs; };
78 Vec3& operator*=(const Vec3& rhs) noexcept { return *this = *this * rhs; };
79 Vec3& operator/=(const Vec3& rhs) noexcept { return *this = *this / rhs; };
80
81 Vec3& operator+=(ValueType rhs) noexcept { return *this = *this + rhs; };
82 Vec3& operator-=(ValueType rhs) noexcept { return *this = *this - rhs; };
83 Vec3& operator*=(ValueType rhs) noexcept { return *this = *this * rhs; };
84 Vec3& operator/=(ValueType rhs) noexcept { return *this = *this / rhs; };
85
88 friend constexpr bool operator==(const Vec3& lhs, const Vec3& rhs) noexcept { return lhs.x == rhs.x and lhs.y == rhs.y and lhs.z == rhs.z; }
89 friend constexpr bool operator!=(const Vec3& lhs, const Vec3& rhs) noexcept { return not(lhs == rhs); }
90
93 explicit constexpr operator bool() const noexcept
94 {
95 return x || y || z;
96 }
97
99 constexpr bool isZero() const noexcept
100 {
101 return not operator bool();
102 }
103
105 void clear() noexcept
106 {
107 *this = {};
108 }
109
110 Udon::Vec2 xy() const noexcept;
111
112#ifdef SIV3D_INCLUDED
113
116 template <typename T>
117 constexpr Vec3(const s3d::Vector3D<T>& v) noexcept
118 : x(static_cast<double>(v.x))
119 , y(static_cast<double>(v.y))
120 , y(static_cast<double>(v.z))
121 {
122 }
123
125 template <typename T>
126 [[nodiscard]] s3d::Vector3D<T> asSivVec3() const noexcept
127 {
128 return {
129 static_cast<T>(x),
130 static_cast<T>(y),
131 static_cast<T>(z),
132 };
133 }
134
136 template <typename T>
137 [[nodiscard]]
138 operator s3d::Vector3D<T>() const noexcept
139 {
140 return asSivVec3<T>();
141 }
142#endif
143
144#ifdef ARDUINO
146 void show() const
147 {
148 Serial.print("x: "), Serial.print(x), Serial.print('\t');
149 Serial.print("y: "), Serial.print(y), Serial.print('\t');
150 Serial.print("z: "), Serial.print(z), Serial.print('\t');
151 }
152#endif
154 };
155
156} // namespace Udon
Definition Bit.hpp:12
二次元ベクトル
Definition Vector2D.hpp:22
三次元ベクトル
Definition Vector3D.hpp:20
Vec3 & operator/=(ValueType rhs) noexcept
Definition Vector3D.hpp:84
ValueType z
Z成分
Definition Vector3D.hpp:32
ValueType x
X成分
Definition Vector3D.hpp:26
friend constexpr Vec3 operator/(const Vec3 &lhs, ValueType rhs) noexcept
Definition Vector3D.hpp:69
Vec3 & operator+=(const Vec3 &rhs) noexcept
Definition Vector3D.hpp:76
friend constexpr bool operator!=(const Vec3 &lhs, const Vec3 &rhs) noexcept
Definition Vector3D.hpp:89
void clear() noexcept
値クリア
Definition Vector3D.hpp:105
UDON_ENUMERABLE(x, y, z)
OpenSiv3D との連携
friend constexpr Vec3 operator*(const Vec3 &lhs, ValueType rhs) noexcept
Definition Vector3D.hpp:68
friend constexpr Vec3 operator+(const Vec3 &lhs, ValueType rhs) noexcept
Definition Vector3D.hpp:66
friend constexpr Vec3 operator/(const Vec3 &lhs, const Vec3 &rhs) noexcept
Definition Vector3D.hpp:64
Vec3 & operator*=(const Vec3 &rhs) noexcept
Definition Vector3D.hpp:78
Vec3 & operator-=(const Vec3 &rhs) noexcept
Definition Vector3D.hpp:77
ValueType y
Y成分
Definition Vector3D.hpp:29
Vec3 & operator-=(ValueType rhs) noexcept
Definition Vector3D.hpp:82
friend constexpr Vec3 operator/(ValueType lhs, const Vec3 &rhs) noexcept
Definition Vector3D.hpp:74
constexpr Vec3(ValueType x, ValueType y, ValueType z) noexcept
コンストラクタ
Definition Vector3D.hpp:48
double ValueType
要素の型
Definition Vector3D.hpp:23
friend constexpr Vec3 operator*(const Vec3 &lhs, const Vec3 &rhs) noexcept
Definition Vector3D.hpp:63
friend constexpr Vec3 operator*(ValueType lhs, const Vec3 &rhs) noexcept
Definition Vector3D.hpp:73
friend constexpr bool operator==(const Vec3 &lhs, const Vec3 &rhs) noexcept
比較演算子
Definition Vector3D.hpp:88
Vec3 & operator*=(ValueType rhs) noexcept
Definition Vector3D.hpp:83
friend constexpr Vec3 operator+(ValueType lhs, const Vec3 &rhs) noexcept
Definition Vector3D.hpp:71
friend constexpr Vec3 operator-(const Vec3 &lhs, const Vec3 &rhs) noexcept
Definition Vector3D.hpp:62
Vec3 & operator+=(ValueType rhs) noexcept
Definition Vector3D.hpp:81
friend constexpr Vec3 operator+(const Vec3 &lhs, const Vec3 &rhs) noexcept
Definition Vector3D.hpp:61
static constexpr size_t Dimension
次元数
Definition Vector3D.hpp:35
friend constexpr Vec3 operator-(const Vec3 &lhs, ValueType rhs) noexcept
Definition Vector3D.hpp:67
Vec3 & operator/=(const Vec3 &rhs) noexcept
Definition Vector3D.hpp:79
constexpr bool isZero() const noexcept
ゼロベクトルであるかを返す
Definition Vector3D.hpp:99
Vec3(const Vec3 &)=default
デフォルトコピーコンストラクタ
Vec3 & operator=(const Vec3 &)=default
デフォルトコピー代入演算子
friend constexpr Vec3 operator-(ValueType lhs, const Vec3 &rhs) noexcept
Definition Vector3D.hpp:72
constexpr Vec3() noexcept
デフォルトコンストラクタ
Definition Vector3D.hpp:38
Udon::Vec2 xy() const noexcept
Definition Vector3D.hpp:12