Position.hpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3  * Modified work Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 
22 #ifndef PLAYRHO_COMMON_POSITION_HPP
23 #define PLAYRHO_COMMON_POSITION_HPP
24 
28 
29 namespace playrho {
30 namespace d2 {
31 
36 struct Position
37 {
40 };
41 
44 PLAYRHO_CONSTEXPR inline bool operator==(const Position& lhs, const Position& rhs)
45 {
46  return (lhs.linear == rhs.linear) && (lhs.angular == rhs.angular);
47 }
48 
51 PLAYRHO_CONSTEXPR inline bool operator!=(const Position& lhs, const Position& rhs)
52 {
53  return (lhs.linear != rhs.linear) || (lhs.angular != rhs.angular);
54 }
55 
59 {
60  return Position{-value.linear, -value.angular};
61 }
62 
66 {
67  return value;
68 }
69 
73 {
74  lhs.linear += rhs.linear;
75  lhs.angular += rhs.angular;
76  return lhs;
77 }
78 
81 PLAYRHO_CONSTEXPR inline Position operator+ (const Position& lhs, const Position& rhs)
82 {
83  return Position{lhs.linear + rhs.linear, lhs.angular + rhs.angular};
84 }
85 
89 {
90  lhs.linear -= rhs.linear;
91  lhs.angular -= rhs.angular;
92  return lhs;
93 }
94 
97 PLAYRHO_CONSTEXPR inline Position operator- (const Position& lhs, const Position& rhs)
98 {
99  return Position{lhs.linear - rhs.linear, lhs.angular - rhs.angular};
100 }
101 
103 PLAYRHO_CONSTEXPR inline Position operator* (const Position& pos, const Real scalar)
104 {
105  return Position{pos.linear * scalar, pos.angular * scalar};
106 }
107 
110 PLAYRHO_CONSTEXPR inline Position operator* (const Real scalar, const Position& pos)
111 {
112  return Position{pos.linear * scalar, pos.angular * scalar};
113 }
114 
115 } // namespace d2
116 
119 template <>
120 PLAYRHO_CONSTEXPR inline
121 bool IsValid(const d2::Position& value) noexcept
122 {
123  return IsValid(value.linear) && IsValid(value.angular);
124 }
125 
126 namespace d2 {
127 
138  const Real beta) noexcept
139 {
140  assert(IsValid(pos0));
141  assert(IsValid(pos1));
142  assert(IsValid(beta));
143 
144  // Note: have to be careful how this is done.
145  // If pos0 == pos1 then return value should always be equal to pos0 too.
146  // But if Real is float, pos0 * (1 - beta) + pos1 * beta can fail this requirement.
147  // Meanwhile, pos0 + (pos1 - pos0) * beta always works.
148 
149  // pos0 * (1 - beta) + pos1 * beta
150  // pos0 - pos0 * beta + pos1 * beta
151  // pos0 + (pos1 * beta - pos0 * beta)
152  // pos0 + (pos1 - pos0) * beta
153  return pos0 + (pos1 - pos0) * beta;
154 }
155 
156 } // namespace d2
157 } // namespace playrho
158 
159 #endif // PLAYRHO_COMMON_POSITION_HPP