DigitalCurling3  1.0.0
A curling simulation system for curling AIs
coordinate.hpp
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2022 UEC Takeshi Ito Laboratory
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
25 
26 #ifndef DIGITALCURLING3_COORDINATE_HPP
27 #define DIGITALCURLING3_COORDINATE_HPP
28 
29 #include <cassert>
30 #include "vector2.hpp"
31 
33 
34 namespace detail {
35 
37 constexpr float kHogLineYOnSimulation = 10.9725f;
38 constexpr float kTeeLineYOnSimulation = 17.3735f;
39 constexpr float kBackLineYOnSimulation = 19.2025f;
40 constexpr float kHackYOnSimulation = 21.0315f;
41 constexpr float kBackBoardYOnSimulation = 22.8605f;
43 
44 } // namespace detail
45 
46 
47 
49 constexpr float kHouseRadius = 1.829f;
50 
51 
52 
54 enum class Id {
59 
63  kShot0,
64 
68  kShot1,
69 };
70 
71 
72 
77 constexpr Id GetShotSide(std::uint8_t end) noexcept
78 {
79  if (end % 2 == 0) {
80  return Id::kShot0;
81  } else {
82  return Id::kShot1;
83  }
84 }
85 
86 
87 
94 constexpr Vector2 TransformPosition(Vector2 position, Id from, Id to) noexcept
95 {
96  if (from == to) return position;
97 
98  switch (to) {
99  case Id::kSimulation:
100  switch (from) {
101  case Id::kShot0:
102  return { position.x, position.y - detail::kHackYOnSimulation };
103  case Id::kShot1:
104  return { -position.x, -position.y + detail::kHackYOnSimulation };
105  default:
106  assert(false);
107  return Vector2();
108  }
109  break;
110 
111  case Id::kShot0:
112  switch (from) {
113  case Id::kSimulation:
114  return { position.x, position.y + detail::kHackYOnSimulation };
115  case Id::kShot1:
116  return { -position.x, -position.y + 2 * detail::kHackYOnSimulation };
117  default:
118  assert(false);
119  return Vector2();
120  }
121  break;
122 
123  case Id::kShot1:
124  switch (from) {
125  case Id::kSimulation:
126  return { -position.x, -position.y + detail::kHackYOnSimulation };
127  case Id::kShot0:
128  return { -position.x, -position.y + 2 * detail::kHackYOnSimulation };
129  default:
130  assert(false);
131  return Vector2();
132  }
133  break;
134 
135  default:
136  assert(false);
137  return Vector2();
138  }
139 }
140 
141 
142 
149 constexpr Vector2 TransformVelocity(Vector2 velocity, Id from, Id to) noexcept
150 {
151  if (from == to) return velocity;
152 
153  // 軸の方向が変化するのはどちらかがKShot1の場合のみ
154  if (from == Id::kShot1 || to == Id::kShot1) {
155  return -velocity;
156  }
157  return velocity;
158 }
159 
160 
161 
168 constexpr float TransformAngle(float angle, Id from, Id to) noexcept
169 {
170  if (from == to) return angle;
171 
172  // 軸の方向が変化するのはfrom/toのどちらかがkShot1の場合のみ
173  if (from == Id::kShot1) {
174  return angle - kPi;
175  }
176  if (to == Id::kShot1) {
177  return angle + kPi;
178  }
179  return angle;
180 }
181 
182 
183 
190 constexpr float TransformAngularVelocity(float angular_velocity, Id from, Id to) noexcept
191 {
192  // 角速度は座標系が変化しても同じ
193  return angular_velocity;
194 }
195 
196 
197 
202 constexpr float GetCenterLineX(Id coord) noexcept
203 {
204  return 0.f;
205 }
206 
207 
208 
214 constexpr float GetHogLineY(bool side, Id coord) noexcept
215 {
216  auto y_simulation = side ? detail::kHogLineYOnSimulation : -detail::kHogLineYOnSimulation;
217  return TransformPosition(Vector2(0.f, y_simulation), Id::kSimulation, coord).y;
218 }
219 
220 
221 
227 constexpr float GetTeeLineY(bool side, Id coord) noexcept
228 {
229  auto y_simulation = side ? detail::kTeeLineYOnSimulation : -detail::kTeeLineYOnSimulation;
230  return TransformPosition(Vector2(0.f, y_simulation), Id::kSimulation, coord).y;
231 }
232 
233 
234 
240 constexpr float GetBackLineY(bool side, Id coord) noexcept
241 {
242  auto y_simulation = side ? detail::kBackLineYOnSimulation : -detail::kBackLineYOnSimulation;
243  return TransformPosition(Vector2(0.f, y_simulation), Id::kSimulation, coord).y;
244 }
245 
246 
247 
253 constexpr float GetHackY(bool side, Id coord) noexcept
254 {
255  auto y_simulation = side ? detail::kHackYOnSimulation : -detail::kHackYOnSimulation;
256  return TransformPosition(Vector2(0.f, y_simulation), Id::kSimulation, coord).y;
257 }
258 
259 
260 
266 constexpr float GetBackBoardY(bool side, Id coord) noexcept
267 {
268  auto y_simulation = side ? detail::kBackBoardYOnSimulation : -detail::kBackBoardYOnSimulation;
269  return TransformPosition(Vector2(0.f, y_simulation), Id::kSimulation, coord).y;
270 }
271 
272 
273 
274 } // namespace digitalcurling3::coordinate
275 
276 #endif // DIGITALCURLING3_COORDINATE_HPP
digitalcurling3::coordinate::Id::kShot0
@ kShot0
ショット座標系(サイド0).
digitalcurling3::coordinate::TransformPosition
constexpr Vector2 TransformPosition(Vector2 position, Id from, Id to) noexcept
位置を座標変換する.
Definition: coordinate.hpp:94
digitalcurling3::coordinate::Id
Id
座標系ID
Definition: coordinate.hpp:54
digitalcurling3::coordinate::Id::kShot1
@ kShot1
ショット座標系(サイド1).
digitalcurling3::Vector2
2次元ベクトル
Definition: vector2.hpp:37
digitalcurling3::coordinate::GetHackY
constexpr float GetHackY(bool side, Id coord) noexcept
ハックのy座標を得る.
Definition: coordinate.hpp:253
digitalcurling3::coordinate::TransformAngle
constexpr float TransformAngle(float angle, Id from, Id to) noexcept
角度を座標系間で変換する.
Definition: coordinate.hpp:168
digitalcurling3::Vector2::y
float y
y座標
Definition: vector2.hpp:39
digitalcurling3::coordinate::GetShotSide
constexpr Id GetShotSide(std::uint8_t end) noexcept
エンドに対応するショット座標系を返す.
Definition: coordinate.hpp:77
digitalcurling3::kPi
constexpr float kPi
円周率
Definition: constants.hpp:34
digitalcurling3::coordinate::Id::kSimulation
@ kSimulation
シミュレーション座標系.
digitalcurling3::coordinate::GetBackLineY
constexpr float GetBackLineY(bool side, Id coord) noexcept
バックラインのy座標を得る.
Definition: coordinate.hpp:240
digitalcurling3::coordinate::GetBackBoardY
constexpr float GetBackBoardY(bool side, Id coord) noexcept
バックボード(シートの端)のy座標を得る.
Definition: coordinate.hpp:266
vector2.hpp
Vector2 を定義します
digitalcurling3::coordinate::kHouseRadius
constexpr float kHouseRadius
ハウスの半径
Definition: coordinate.hpp:49
digitalcurling3::coordinate::GetCenterLineX
constexpr float GetCenterLineX(Id coord) noexcept
センターラインのx座標を得る.
Definition: coordinate.hpp:202
digitalcurling3::coordinate::TransformVelocity
constexpr Vector2 TransformVelocity(Vector2 velocity, Id from, Id to) noexcept
速度を座標系間で変換する.
Definition: coordinate.hpp:149
digitalcurling3::coordinate::GetHogLineY
constexpr float GetHogLineY(bool side, Id coord) noexcept
ホグラインのy座標を得る.
Definition: coordinate.hpp:214
digitalcurling3::coordinate
座標変換に関連するものを定義する名前空間
Definition: coordinate.hpp:32
digitalcurling3::coordinate::GetTeeLineY
constexpr float GetTeeLineY(bool side, Id coord) noexcept
ティーラインのy座標を得る.
Definition: coordinate.hpp:227
digitalcurling3::coordinate::TransformAngularVelocity
constexpr float TransformAngularVelocity(float angular_velocity, Id from, Id to) noexcept
角速度を座標系間で変換する.
Definition: coordinate.hpp:190