TrueReality  v0.1.1912
Vec2Impl.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2017 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Author: Maxim Serebrennik
20 */
21 
22 namespace trBase
23 {
25  const int Vec2Impl::NUM_OF_COMPONENTS = 2;
26 
29  : mVec()
30  {
31  }
32 
35  : mVec(x, y)
36  {
37  }
38 
41  : mVec(v.x(), v.y())
42  {
43  }
44 
46  const osg::Vec2Impl& Vec2Impl::GetOSGVector() const
47  {
48  return mVec;
49  }
50 
52  Vec2Impl::value_type Vec2Impl::Length() const
53  {
54 
55  return sqrt(mVec._v[0] * mVec._v[0] + mVec._v[1] * mVec._v[1]);
56  }
57 
59  Vec2Impl::value_type Vec2Impl::Length2() const
60  {
61  return (mVec._v[0] * mVec._v[0] + mVec._v[1] * mVec._v[1]);
62  }
63 
65  Vec2Impl::value_type Vec2Impl::Normalize()
66  {
67  value_type norm = Vec2Impl::Length();
68  if (norm>0.0)
69  {
70  value_type inv = 1.0 / norm;
71  mVec._v[0] *= inv;
72  mVec._v[1] *= inv;
73  }
74  return norm;
75  }
76 
78  Vec2Impl::value_type& Vec2Impl::X()
79  {
80  return mVec._v[0];
81  }
82 
84  Vec2Impl::value_type& Vec2Impl::Y()
85  {
86  return mVec._v[1];
87  }
88 
90  Vec2Impl::value_type Vec2Impl::X() const
91  {
92  return mVec._v[0];
93  }
94 
96  Vec2Impl::value_type Vec2Impl::Y() const
97  {
98  return mVec._v[1];
99  }
100 
102  void Vec2Impl::Set(value_type x, value_type y)
103  {
104  mVec._v[0] = x; mVec._v[1] = y;
105  }
106 
108  void Vec2Impl::Lerp(const Vec2Impl& to, value_type alpha)
109  {
110  mVec._v[0] = trUtil::Math::Lerp<value_type>(mVec._v[0], to.X(), alpha);
111  mVec._v[1] = trUtil::Math::Lerp<value_type>(mVec._v[1], to.Y(), alpha);
112  }
113 
115  Vec2Impl Vec2Impl::Lerp(const Vec2Impl& from, const Vec2Impl& to, value_type alpha)
116  {
117  return Vec2Impl(trUtil::Math::Lerp<value_type>(from.X(), to.X(), alpha), trUtil::Math::Lerp<value_type>(from.Y(), to.Y(), alpha));
118  }
119 
121  std::string Vec2Impl::ToString(int precision)
122  {
123  return std::string("<" + trUtil::StringUtils::ToString<value_type>(mVec._v[0], precision) + ", " + trUtil::StringUtils::ToString<value_type>(mVec._v[1], precision) + ">");
124  }
125 
127  Vec2Impl::value_type* Vec2Impl::Ptr()
128  {
129  return mVec.ptr();
130  }
131 
133  const Vec2Impl::value_type* Vec2Impl::Ptr() const
134  {
135  return mVec.ptr();
136  }
137 
139  Vec2Impl::value_type& Vec2Impl::operator [] (int i)
140  {
141  return mVec[i];
142  }
143 
145  Vec2Impl::value_type Vec2Impl::operator [] (int i) const
146  {
147  return mVec[i];
148  }
149 
151  void Vec2Impl::operator = (const Vec2Impl& v)
152  {
153  mVec._v[0] = v[0]; mVec._v[1] = v[1];
154  }
155 
157  void Vec2Impl::operator = (const osg::Vec2Impl& v)
158  {
159  mVec._v[0] = v[0]; mVec._v[1] = v[1];
160  }
161 
163  bool Vec2Impl::operator == (const Vec2Impl& v) const
164  {
165  return mVec._v[0] == v[0] && mVec._v[1] == v[1];
166  }
167 
169  bool Vec2Impl::operator != (const Vec2Impl& v) const
170  {
171  return mVec._v[0] != v[0] || mVec._v[1] != v[1];
172  }
173 
175  bool Vec2Impl::operator < (const Vec2Impl& v) const
176  {
177  if (mVec._v[0] < v[0]) return true;
178  else if (mVec._v[0] > v[0]) return false;
179  else return (mVec._v[1] < v[1]);
180  }
181 
183  bool Vec2Impl::operator > (const Vec2Impl& v) const
184  {
185  if (mVec._v[0] > v[0]) return true;
186  else if (mVec._v[0] < v[0]) return false;
187  else return (mVec._v[1] > v[1]);
188  }
189 
191  Vec2Impl::value_type Vec2Impl::operator * (const Vec2Impl& rhs) const
192  {
193  return mVec._v[0] * rhs[0] + mVec._v[1] * rhs[1];
194  }
195 
197  const Vec2Impl Vec2Impl::operator * (Vec2Impl::value_type rhs) const
198  {
199  return Vec2Impl(mVec._v[0] * rhs, mVec._v[1] * rhs);
200  }
201 
203  Vec2Impl& Vec2Impl::operator *= (Vec2Impl::value_type rhs)
204  {
205  mVec._v[0] *= rhs;
206  mVec._v[1] *= rhs;
207  return *this;
208  }
209 
211  const Vec2Impl Vec2Impl::operator / (Vec2Impl::value_type rhs) const
212  {
213  return Vec2Impl(mVec._v[0] / rhs, mVec._v[1] / rhs);
214  }
215 
217  Vec2Impl& Vec2Impl::operator /= (Vec2Impl::value_type rhs)
218  {
219  mVec._v[0] /= rhs;
220  mVec._v[1] /= rhs;
221  return *this;
222  }
223 
225  const Vec2Impl Vec2Impl::operator + (const Vec2Impl& rhs) const
226  {
227  return Vec2Impl(mVec._v[0] + rhs[0], mVec._v[1] + rhs[1]);
228  }
229 
231  Vec2Impl& Vec2Impl::operator += (const Vec2Impl& rhs)
232  {
233  mVec._v[0] += rhs[0];
234  mVec._v[1] += rhs[1];
235  return *this;
236  }
237 
239  const Vec2Impl Vec2Impl::operator - (const Vec2Impl& rhs) const
240  {
241  return Vec2Impl(mVec._v[0] - rhs[0], mVec._v[1] - rhs[1]);
242  }
243 
245  Vec2Impl& Vec2Impl::operator -= (const Vec2Impl& rhs)
246  {
247  mVec._v[0] -= rhs[0];
248  mVec._v[1] -= rhs[1];
249  return *this;
250  }
251 
253  const Vec2Impl Vec2Impl::operator - () const
254  {
255  return Vec2Impl(-1 * mVec._v[0], -1 * mVec._v[1]);
256  }
257 
259  Vec2Impl::operator osg::Vec2Impl () const
260  {
261  return mVec;
262  }
263 
265  Vec2Impl::operator osg::Vec2Impl& ()
266  {
267  return mVec;
268  }
269 
271  Vec2Impl::operator const osg::Vec2Impl& () const
272  {
273  return mVec;
274  }
275 
277  Vec2Impl::operator osg::Vec2Impl* ()
278  {
279  return &mVec;
280  }
281 
283  std::ostream& operator << (std::ostream& ios, const Vec2Impl& vec)
284  {
285  ios << "<" << vec.X() << ", " << vec.Y() << ">";
286  return ios;
287  }
288 
290  Vec2Impl ComponentMultiply(const Vec2Impl& lhs, const Vec2Impl& rhs)
291  {
292  return Vec2Impl(lhs[0] * rhs[0], lhs[1] * rhs[1]);
293  }
294 
296  Vec2Impl ComponentDivide(const Vec2Impl& lhs, const Vec2Impl& rhs)
297  {
298  return Vec2Impl(lhs[0] / rhs[0], lhs[1] / rhs[1]);
299  }
300 }
std::string operator+(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:193
bool operator==(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:187
bool operator!=(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:199
TR_BASE_EXPORT std::ostream & operator<<(std::ostream &ios, const Matrixd &q)
Stream insertion operator.
osg::Vec2i mVec
Definition: Vec2i.h:267
std::string ToString(const T &t, int precision=-1)
A utility function to convert a basic type into a string.
Definition: StringUtils.h:334
TR_BASE_EXPORT Vec2b ComponentDivide(const Vec2b &lhs, const Vec2b &rhs)
Divide rhs components by rhs vector components.
TR_BASE_EXPORT Vec2b ComponentMultiply(const Vec2b &lhs, const Vec2b &rhs)
Multiply individual vector components.
T Lerp(T from, T to, T alpha)
Linear Interpolation function.
Definition: Math.h:191
int value_type
Data type of vector components.
Definition: Vec2i.h:44
#define Vec2Impl
Definition: Vec2b.cpp:25