HoverRace  2.0
Vec.h
Go to the documentation of this file.
1 
2 // Vec.h
3 //
4 // Copyright (c) 2013-2015 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
25 # ifdef MR_ENGINE
26 # define MR_DllDeclare __declspec( dllexport )
27 # else
28 # define MR_DllDeclare __declspec( dllimport )
29 # endif
30 #else
31 # define MR_DllDeclare
32 #endif
33 
34 namespace HoverRace {
35 
36 struct Vec3;
37 
38 struct Vec2
39 {
40  constexpr Vec2(double x, double y) noexcept : x(x), y(y) { }
41 
42  constexpr Vec3 Promote(double z = 0) const noexcept;
43 
44  double x;
45  double y;
46 };
47 
48 inline std::ostream &operator<<(std::ostream &os, const Vec2 &v)
49 {
50  os << '<' << v.x << ", " << v.y << '>';
51  return os;
52 }
53 
54 inline constexpr bool operator==(const Vec2 &lhs, const Vec2 &rhs) noexcept
55 {
56  return
57  lhs.x == rhs.x &&
58  lhs.y == rhs.y;
59 }
60 
61 inline constexpr bool operator!=(const Vec2 &lhs, const Vec2 &rhs) noexcept
62 {
63  return !operator==(lhs, rhs);
64 }
65 
66 inline constexpr Vec2 operator+(const Vec2 &lhs, const Vec2 &rhs) noexcept
67 {
68  return { lhs.x + rhs.x, lhs.y + rhs.y };
69 }
70 
71 inline Vec2 &operator+=(Vec2 &lhs, const Vec2 &rhs) noexcept
72 {
73  lhs.x += rhs.x;
74  lhs.y += rhs.y;
75  return lhs;
76 }
77 
78 inline constexpr Vec2 operator-(const Vec2 &lhs, const Vec2 &rhs) noexcept
79 {
80  return { lhs.x - rhs.x, lhs.y - rhs.y };
81 }
82 
83 inline Vec2 &operator-=(Vec2 &lhs, const Vec2 &rhs) noexcept
84 {
85  lhs.x -= rhs.x;
86  lhs.y -= rhs.y;
87  return lhs;
88 }
89 
90 inline constexpr Vec2 operator*(const Vec2 &lhs, double scale) noexcept
91 {
92  return { lhs.x * scale, lhs.y * scale };
93 }
94 
95 inline Vec2 &operator*=(Vec2 &lhs, double scale) noexcept
96 {
97  lhs.x *= scale;
98  lhs.y *= scale;
99  return lhs;
100 }
101 
102 inline constexpr Vec2 operator/(const Vec2 &lhs, double scale) noexcept
103 {
104  return { lhs.x / scale, lhs.y / scale };
105 }
106 
107 inline Vec2 &operator/=(Vec2 &lhs, double scale) noexcept
108 {
109  lhs.x /= scale;
110  lhs.y /= scale;
111  return lhs;
112 }
113 
114 struct Vec3
115 {
116  constexpr Vec3(double x, double y, double z) noexcept : x(x), y(y), z(z) { }
117 
118  double x;
119  double y;
120  double z;
121 };
122 
123 inline std::ostream &operator<<(std::ostream &os, const Vec3 &v)
124 {
125  os << '<' << v.x << ", " << v.y << ", " << v.z << '>';
126  return os;
127 }
128 
129 inline constexpr bool operator==(const Vec3 &lhs, const Vec3 &rhs) noexcept
130 {
131  return
132  lhs.x == rhs.x &&
133  lhs.y == rhs.y &&
134  lhs.z == rhs.z;
135 }
136 
137 inline constexpr bool operator!=(const Vec3 &lhs, const Vec3 &rhs) noexcept
138 {
139  return !operator==(lhs, rhs);
140 }
141 
142 inline constexpr Vec3 operator+(const Vec3 &lhs, const Vec3 &rhs) noexcept
143 {
144  return { lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z };
145 }
146 
147 inline Vec3 &operator+=(Vec3 &lhs, const Vec3 &rhs) noexcept
148 {
149  lhs.x += rhs.x;
150  lhs.y += rhs.y;
151  lhs.z += rhs.z;
152  return lhs;
153 }
154 
155 inline constexpr Vec3 operator+(const Vec3 &lhs, const Vec2 &rhs) noexcept
156 {
157  return { lhs.x + rhs.x, lhs.y + rhs.y, lhs.z };
158 }
159 
160 inline Vec3 &operator+=(Vec3 &lhs, const Vec2 &rhs) noexcept
161 {
162  lhs.x += rhs.x;
163  lhs.y += rhs.y;
164  return lhs;
165 }
166 
167 inline constexpr Vec3 operator-(const Vec3 &lhs, const Vec3 &rhs) noexcept
168 {
169  return Vec3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
170 }
171 
172 inline Vec3 &operator-=(Vec3 &lhs, const Vec3 &rhs) noexcept
173 {
174  lhs.x -= rhs.x;
175  lhs.y -= rhs.y;
176  lhs.z -= rhs.z;
177  return lhs;
178 }
179 
180 inline constexpr Vec3 operator*(const Vec3 &lhs, double scale) noexcept
181 {
182  return { lhs.x * scale, lhs.y * scale, lhs.z * scale };
183 }
184 
185 inline Vec3 &operator*=(Vec3 &lhs, double scale) noexcept
186 {
187  lhs.x *= scale;
188  lhs.y *= scale;
189  lhs.z *= scale;
190  return lhs;
191 }
192 
193 inline constexpr Vec3 operator/(const Vec3 &lhs, double scale) noexcept
194 {
195  return { lhs.x / scale, lhs.y / scale, lhs.z / scale };
196 }
197 
198 inline Vec3 &operator/=(Vec3 &lhs, double scale) noexcept
199 {
200  lhs.x /= scale;
201  lhs.y /= scale;
202  lhs.z /= scale;
203  return lhs;
204 }
205 
206 inline constexpr Vec3 Vec2::Promote(double z) const noexcept
207 {
208  return { x, y, z };
209 }
210 
211 } // namespace HoverRace
212 
213 #undef MR_DllDeclare
constexpr Vec2 operator-(const Vec2 &lhs, const Vec2 &rhs) noexcept
Definition: Vec.h:78
constexpr Vec3 Promote(double z=0) const noexcept
Definition: Vec.h:206
Vec2 & operator+=(Vec2 &lhs, const Vec2 &rhs) noexcept
Definition: Vec.h:71
double y
Definition: Vec.h:45
constexpr Vec2 operator+(const Vec2 &lhs, const Vec2 &rhs) noexcept
Definition: Vec.h:66
constexpr Vec2 operator/(const Vec2 &lhs, double scale) noexcept
Definition: Vec.h:102
Vec2 & operator-=(Vec2 &lhs, const Vec2 &rhs) noexcept
Definition: Vec.h:83
Definition: Vec.h:38
constexpr bool operator==(const Vec2 &lhs, const Vec2 &rhs) noexcept
Definition: Vec.h:54
double x
Definition: Vec.h:44
Vec2 & operator*=(Vec2 &lhs, double scale) noexcept
Definition: Vec.h:95
double x
Definition: Vec.h:118
double y
Definition: Vec.h:119
constexpr bool operator!=(const Vec2 &lhs, const Vec2 &rhs) noexcept
Definition: Vec.h:61
constexpr Vec2 operator*(const Vec2 &lhs, double scale) noexcept
Definition: Vec.h:90
constexpr Vec2(double x, double y) noexcept
Definition: Vec.h:40
constexpr Vec3(double x, double y, double z) noexcept
Definition: Vec.h:116
std::ostream & operator<<(std::ostream &os, const Vec2 &v)
Definition: Vec.h:48
Vec2 & operator/=(Vec2 &lhs, double scale) noexcept
Definition: Vec.h:107
Definition: Announcement.h:24
double z
Definition: Vec.h:120
Definition: Vec.h:114