Nugget
Loading...
Searching...
No Matches
vector.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2023 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <concepts>
30#include <type_traits>
31
32#include "psyqo/fixed-point.hh"
34
35namespace psyqo {
36
37template <unsigned N, unsigned precisionBits = 12, std::integral T = int32_t>
38 requires((N >= 2) && (N <= 4))
39struct Vector {
40 typedef FixedPoint<precisionBits, T> FixedPointType;
41 FixedPoint<precisionBits, T> x, y;
42 struct EmptyZ {};
43 [[no_unique_address]] std::conditional_t<(N > 2), FixedPoint<precisionBits, T>, EmptyZ> z;
44 struct EmptyW {};
45 [[no_unique_address]] std::conditional_t<(N > 3), FixedPoint<precisionBits, T>, EmptyW> w;
46 constexpr FixedPointType& get(unsigned i) {
47 if constexpr (N == 2) {
48 return (i == 0) ? x : y;
49 } else if constexpr (N == 3) {
50 return (i == 0) ? x : (i == 1) ? y : z;
51 } else if constexpr (N == 4) {
52 return (i == 0) ? x : (i == 1) ? y : (i == 2) ? z : w;
53 }
54 }
55 constexpr const FixedPointType& get(unsigned i) const {
56 if constexpr (N == 2) {
57 return (i == 0) ? x : y;
58 } else if constexpr (N == 3) {
59 return (i == 0) ? x : (i == 1) ? y : z;
60 } else if constexpr (N == 4) {
61 return (i == 0) ? x : (i == 1) ? y : (i == 2) ? z : w;
62 }
63 }
64 constexpr FixedPointType& operator[](unsigned i) { return get(i); }
65 constexpr operator Vertex() const
66 requires((N == 2) && std::is_signed<T>::value)
67 {
68 return {{.x = x.template integer<int16_t>(), .y = y.template integer<int16_t>()}};
69 }
70 constexpr Vector& operator+=(const Vector& rhs) {
71 for (unsigned i = 0; i < N; i++) {
72 get(i) += rhs.get(i);
73 }
74 return *this;
75 }
76 constexpr Vector& operator-=(const Vector& rhs) {
77 for (unsigned i = 0; i < N; i++) {
78 get(i) -= rhs.get(i);
79 }
80 return *this;
81 }
82 constexpr Vector& operator*=(const FixedPointType& rhs) {
83 for (unsigned i = 0; i < N; i++) {
84 get(i) *= rhs;
85 }
86 return *this;
87 }
88 constexpr Vector& operator/=(const FixedPointType& rhs) {
89 for (unsigned i = 0; i < N; i++) {
90 get(i) /= rhs;
91 }
92 return *this;
93 }
94 template <std::integral U>
95 constexpr Vector& operator*=(U rhs) {
96 for (unsigned i = 0; i < N; i++) {
97 get(i) *= rhs;
98 }
99 return *this;
100 }
101 template <std::integral U>
102 constexpr Vector& operator/=(U rhs) {
103 for (unsigned i = 0; i < N; i++) {
104 get(i) /= rhs;
105 }
106 return *this;
107 }
108 constexpr Vector operator-() const {
110 for (unsigned i = 0; i < N; i++) {
111 result.get(i) = -get(i);
112 }
113 return result;
114 }
115 constexpr Vector operator+(const Vector& rhs) const {
117 for (unsigned i = 0; i < N; i++) {
118 result.get(i) = get(i) + rhs.get(i);
119 }
120 return result;
121 }
122 constexpr Vector operator-(const Vector& rhs) const {
124 for (unsigned i = 0; i < N; i++) {
125 result.get(i) = get(i) - rhs.get(i);
126 }
127 return result;
128 }
129 constexpr Vector operator*(const FixedPointType& rhs) const {
131 for (unsigned i = 0; i < N; i++) {
132 result.get(i) = get(i) * rhs;
133 }
134 return result;
135 }
136 constexpr Vector operator/(const FixedPointType& rhs) const {
138 for (unsigned i = 0; i < N; i++) {
139 result.get(i) = get(i) / rhs;
140 }
141 return result;
142 }
143 template <std::integral U>
144 constexpr Vector operator*(U rhs) const {
146 for (unsigned i = 0; i < N; i++) {
147 result.get(i) = get(i) * rhs;
148 }
149 return result;
150 }
151 template <std::integral U>
152 constexpr Vector operator/(U rhs) const {
154 for (unsigned i = 0; i < N; i++) {
155 result.get(i) = get(i) / rhs;
156 }
157 return result;
158 }
159 static constexpr Vector ZERO()
160 requires(N <= 3)
161 {
163 for (unsigned i = 0; i < N; i++) {
164 result.get(i) = FixedPointType(0.0);
165 }
166 return result;
167 }
168 static constexpr Vector ONE()
169 requires(N <= 3)
170 {
172 for (unsigned i = 0; i < N; i++) {
173 result.get(i) = FixedPointType(1.0);
174 }
175 return result;
176 }
177 static constexpr Vector UP()
178 requires(N <= 3)
179 {
181 for (unsigned i = 0; i < N; i++) {
182 result.get(i) = (i == 1) ? FixedPointType(1.0) : FixedPointType(0.0);
183 }
184 return result;
185 }
186 static constexpr Vector DOWN()
187 requires(N <= 3)
188 {
190 for (unsigned i = 0; i < N; i++) {
191 result.get(i) = (i == 1) ? -FixedPointType(1.0) : FixedPointType(0.0);
192 }
193 return result;
194 }
195 static constexpr Vector LEFT()
196 requires(N <= 3)
197 {
199 for (unsigned i = 0; i < N; i++) {
200 result.get(i) = (i == 0) ? -FixedPointType(1.0) : FixedPointType(0.0);
201 }
202 return result;
203 }
204 static constexpr Vector RIGHT()
205 requires(N <= 3)
206 {
208 for (unsigned i = 0; i < N; i++) {
209 result.get(i) = (i == 0) ? FixedPointType(1.0) : FixedPointType(0.0);
210 }
211 return result;
212 }
213 static constexpr Vector FORWARD()
214 requires(N == 3)
215 {
217 for (unsigned i = 0; i < N; i++) {
218 result.get(i) = (i == 2) ? FixedPointType(1.0) : FixedPointType(0.0);
219 }
220 return result;
221 }
222 static constexpr Vector BACKWARD()
223 requires(N == 3)
224 {
226 for (unsigned i = 0; i < N; i++) {
227 result.get(i) = (i == 2) ? -FixedPointType(1.0) : FixedPointType(0.0);
228 }
229 return result;
230 }
231};
232
236
237static_assert(sizeof(Vec2) == 8);
238static_assert(sizeof(Vec3) == 12);
239static_assert(sizeof(Vec4) == 16);
240
241} // namespace psyqo
void * result
Definition memcpy.c:47
Definition cdrom-loader.hh:39
Vector< 4 > Vec4
Definition vector.hh:235
Vector< 3 > Vec3
Definition vector.hh:234
Vector< 2 > Vec2
Definition vector.hh:233
Definition vector.hh:44
Definition vector.hh:42
Definition vector.hh:39
static constexpr Vector ZERO()
Definition vector.hh:159
static constexpr Vector LEFT()
Definition vector.hh:195
constexpr Vector operator*(U rhs) const
Definition vector.hh:144
FixedPoint< precisionBits, T > x
Definition vector.hh:41
static constexpr Vector ONE()
Definition vector.hh:168
constexpr Vector & operator/=(U rhs)
Definition vector.hh:102
constexpr Vector operator/(U rhs) const
Definition vector.hh:152
constexpr Vector operator-() const
Definition vector.hh:108
constexpr FixedPointType & operator[](unsigned i)
Definition vector.hh:64
constexpr Vector & operator+=(const Vector &rhs)
Definition vector.hh:70
constexpr Vector operator-(const Vector &rhs) const
Definition vector.hh:122
static constexpr Vector BACKWARD()
Definition vector.hh:222
constexpr Vector & operator*=(U rhs)
Definition vector.hh:95
constexpr Vector & operator-=(const Vector &rhs)
Definition vector.hh:76
static constexpr Vector RIGHT()
Definition vector.hh:204
static constexpr Vector UP()
Definition vector.hh:177
constexpr Vector operator*(const FixedPointType &rhs) const
Definition vector.hh:129
constexpr Vector operator/(const FixedPointType &rhs) const
Definition vector.hh:136
static constexpr Vector DOWN()
Definition vector.hh:186
constexpr Vector operator+(const Vector &rhs) const
Definition vector.hh:115
constexpr const FixedPointType & get(unsigned i) const
Definition vector.hh:55
FixedPoint< precisionBits, T > FixedPointType
Definition vector.hh:40
static constexpr Vector FORWARD()
Definition vector.hh:213
constexpr FixedPointType & get(unsigned i)
Definition vector.hh:46
constexpr Vector & operator*=(const FixedPointType &rhs)
Definition vector.hh:82
constexpr Vector & operator/=(const FixedPointType &rhs)
Definition vector.hh:88
static int value
Definition syscalls.h:534
The Vertex struct.
Definition common.hh:47
int16_t x
Definition common.hh:50