Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vector2.h
Go to the documentation of this file.
1 /*
2 
3 License
4 
5 Menge
6 Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill.
7 All rights reserved.
8 
9 Permission to use, copy, modify, and distribute this software and its documentation
10 for educational, research, and non-profit purposes, without fee, and without a
11 written agreement is hereby granted, provided that the above copyright notice,
12 this paragraph, and the following four paragraphs appear in all copies.
13 
14 This software program and documentation are copyrighted by the University of North
15 Carolina at Chapel Hill. The software program and documentation are supplied "as is,"
16 without any accompanying services from the University of North Carolina at Chapel
17 Hill or the authors. The University of North Carolina at Chapel Hill and the
18 authors do not warrant that the operation of the program will be uninterrupted
19 or error-free. The end-user understands that the program was developed for research
20 purposes and is advised not to rely exclusively on the program for any reason.
21 
22 IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS
23 BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
24 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
25 DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
26 AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
29 DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY
31 OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
32 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS
33 TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 
35 Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu
36 
37 */
38 
44 #ifndef __VECTOR2_H__
45 #define __VECTOR2_H__
46 
47 #include "CoreConfig.h"
48 #include <cmath>
49 #include <ostream>
50 #include "Logger.h"
51 #include "Math/consts.h"
52 
53 #ifdef _MSC_VER
54  // To export templated classes and functions, requires declaring specializations
55  // extern. This became standard in C++11, but prior to that it is considered a
56  // "microsoft extension". This silences the warnings.
57  #pragma warning( disable : 4231 )
58 #endif
59 
60 namespace Menge {
61 
62  namespace Math {
68  inline MENGE_API float sqr(float a) {
69  return a * a;
70  }
71 
75  template <class Type>
76  class MENGE_API Vector2d {
77  public:
79 
82  inline Vector2d() {}
83 
90  inline Vector2d( Type x, Type y ): _x(x), _y(y) {}
91 
97  inline Vector2d( const Vector2d & v ): _x(v._x), _y(v._y) {}
98 
100 
106  inline Type x() const { return _x; }
107 
113  inline Type y() const { return _y; }
114 
121  inline void set( Type x, Type y ) {
122  _x = x;
123  _y = y;
124  }
125 
131  inline void set( const Vector2d & v ) {
132  _x = v._x;
133  _y = v._y;
134  }
135 
141  inline void setX( Type x ) {
142  _x = x;
143  }
144 
150  inline void setY( Type y ) {
151  _y = y;
152  }
153 
157  inline void zero() {
158  _x = _y = (Type)0;
159  }
160 
162 
169  inline Vector2d<Type> operator-() const {
170  return Vector2d<Type>( -_x, -_y );
171  }
172 
182  inline float operator*(const Vector2d& v) const {
183  return _x * v._x + _y * v._y;
184  }
185 
194  inline Vector2d<Type> operator*( float s ) const {
195  return Vector2d<Type>( _x * s, _y * s );
196  }
197 
206  inline Vector2d<Type> operator/( float s ) const {
207  const float invS = 1.f / s;
208  return Vector2d<Type>( _x * invS, _y * invS );
209  }
210 
219  inline Vector2d operator+( const Vector2d & v ) const {
220  return Vector2d<Type>( _x + v._x, _y + v._y );
221  }
222 
231  inline Vector2d operator-( const Vector2d & v ) const {
232  return Vector2d<Type>( _x - v._x, _y - v._y );
233  }
234 
243  inline bool operator==( const Vector2d & v ) const {
244  return _x == v._x && _y == v._y;
245  }
246 
255  inline bool operator!=( const Vector2d & v ) const {
256  return _x != v._x || _y != v._y;
257  }
258 
260 
267  inline Vector2d & operator*=( float s ) {
268  _x *= s;
269  _y *= s;
270  return *this;
271  }
272 
279  inline Vector2d & operator/=( float s ) {
280  const float invS = 1.f / s;
281  _x *= invS;
282  _y *= invS;
283  return *this;
284  }
285 
292  inline Vector2d & operator+=( const Vector2d & v ) {
293  _x += v._x;
294  _y += v._y;
295  return *this;
296  }
297 
304  inline Vector2d & operator-=( const Vector2d & v ) {
305  _x -= v._x;
306  _y -= v._y;
307  return *this;
308  }
309 
313  inline void negate() {
314  _x = -_x;
315  _y = -_y;
316  }
317 
321  inline void normalize() {
322  float len = sqrtf( _x * _x + _y * _y );
323  if ( len > EPS ) {
324  _x /= len;
325  _y /= len;
326  } else {
327  _x = _y = 0.f;
328  }
329  }
330 
332 
338  inline Type Length() const {
339  return sqrt( _x * _x + _y * _y );
340  }
341 
349  inline float distance( const Vector2d & p ) const {
350  float dx = _x - p._x;
351  float dy = _y - p._y;
352  return sqrtf( dx * dx + dy * dy );
353  }
354 
362  inline float distance( float x, float y ) const {
363  float dx = _x - x;
364  float dy = _y - y;
365  return sqrtf( dx * dx + dy * dy );
366  }
367 
375  inline float distanceSq( const Vector2d & p ) const {
376  float dx = _x - p._x;
377  float dy = _y - p._y;
378  return dx * dx + dy * dy ;
379  }
380 
388  inline float distanceSq( float x, float y ) const {
389  float dx = _x - x;
390  float dy = _y - y;
391  return dx * dx + dy * dy;
392  }
393 
397  Type _x;
398 
402  Type _y;
403  };
404 
409  MATHEXTERN template class MENGE_API Vector2d<float>;
410 
414  typedef Vector2d<float> Vector2;
415 
417 
418 
429  template< class Type >
430  inline MENGE_API Vector2d<Type> operator*( Type s, const Vector2d<Type>& v) {
431  return Vector2d<Type>( s * v._x, s * v._y );
432  }
433 
438  MATHEXTERN template MENGE_API Vector2d<float> operator*( float s, const Vector2d<float>& v);
439 
449  template< class Type >
450  inline MENGE_API Logger& operator<<(Logger& logger, const Vector2d<Type>& v ) {
451  logger << "(" << v.x() << "," << v.y() << ")";
452  return logger;
453  }
454 
465  template< class Type >
466  inline MENGE_API std::ostream& operator<<(std::ostream& os, const Vector2d<Type>& v ) {
467  os << "(" << v.x() << "," << v.y() << ")";
468  return os;
469  }
470 
475  MATHEXTERN template MENGE_API Logger& operator<<(Logger& os, const Vector2d<float>& v );
476 
484  template< class Type >
485  inline MENGE_API Type abs( const Vector2d<Type>& v ) {
486  return std::sqrt( v * v );
487  }
488 
493  MATHEXTERN template MENGE_API float abs( const Vector2d<float>& v );
494 
503  template< class Type >
504  inline MENGE_API Type absSq( const Vector2d<Type>& v ) {
505  return v * v;
506  }
507 
512  MATHEXTERN template MENGE_API float absSq( const Vector2d<float>& v );
513 
524  template< class Type >
525  inline MENGE_API Type det( const Vector2d<Type>& v1, const Vector2d<Type>& v2) {
526  return v1.x() * v2.y() - v1.y() * v2.x();
527  }
528 
533  MATHEXTERN template MENGE_API float det( const Vector2d<float>& v1, const Vector2d<float>& v2);
534 
543  template< class Type >
544  inline MENGE_API Vector2d<Type> norm(const Vector2d<Type>& vector) {
545  float mag = abs( vector );
546  if ( mag < EPS ) {
547  // This may not be the "right" behavior. I do this because the "normalized" vector has unit
548  // length. This guarantees that the result is always unit length. Although it introduces other
549  // issues.
550  return Vector2d<Type>(1.f, 0.f);
551  } else {
552  return vector / mag;
553  }
554  }
555 
560  MATHEXTERN template MENGE_API Vector2d<float> norm(const Vector2d<float>& vector);
561 
572  template< class Type >
573  inline MENGE_API bool equivalent( const Vector2d<Type> & v1, const Vector2d<Type> & v2, float threshSqd=0.000001f ) {
574  return absSq( v1 - v2 ) < threshSqd;
575  }
576 
581  MATHEXTERN template MENGE_API bool equivalent( const Vector2d<float> & v1, const Vector2d<float> & v2, float threshSqd );
582 
592  template< class Type >
593  inline MENGE_API Type leftOf(const Vector2d<Type>& a, const Vector2d<Type>& b, const Vector2d<Type>& c) {
594  return det(a - c, b - a);
595  }
596 
601  MATHEXTERN template MENGE_API float leftOf(const Vector2d<float>& a, const Vector2d<float>& b, const Vector2d<float>& c);
602  } // namespace Math
603 } // namespace Menge
604 
605  #ifdef _MSC_VER
606  // To export templated classes and functions, requires declaring specializations
607  // extern. This became standard in C++11, but prior to that it is considered a
608  // "microsoft extension". This silences the warnings.
609  #pragma warning( default : 4231 )
610  #endif
611 #endif //__VECTOR2_H__
Vector2d()
Default constructor. It does NOT initialize the fields.
Definition: Vector2.h:82
void set(const Vector2d &v)
Set the x- and y-values from a vector.
Definition: Vector2.h:131
Vector2d< Type > operator-() const
Vector negation. Creates a new vector which is the negation of this vector.
Definition: Vector2.h:169
Vector2d & operator+=(const Vector2d &v)
Perform in-place vector addition on this vector.
Definition: Vector2.h:292
Vector2d< Type > operator*(float s) const
Computes the scalar multiplication of this vector with the given scalar value.
Definition: Vector2.h:194
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
Type _x
x-component of the vector
Definition: Vector2.h:397
Sets up the proper compiler directives for platform and dll export/import.
float distance(float x, float y) const
Compute the distance from this vector to another point.
Definition: Vector2.h:362
float operator*(const Vector2d &v) const
Computes the dot product of this vector with the given vector.
Definition: Vector2.h:182
Templated vector in R2.
Definition: Vector2.h:76
A compilation of file system operations.
MENGE_API const float EPS
Suitably small number for testing for functional zero values.
Definition: geomQuery.cpp:52
float distance(const Vector2d &p) const
Compute the distance from this vector to another point.
Definition: Vector2.h:349
void negate()
Negate the vector in place.
Definition: Vector2.h:313
void set(Type x, Type y)
Set the x- and y-values from scalar values.
Definition: Vector2.h:121
An html logger - writes messages to a formatted html file.
Definition: Logger.h:59
void setX(Type x)
Set the x-value.
Definition: Vector2.h:141
Vector2d & operator/=(float s)
Perform in-place scalar division on this vector.
Definition: Vector2.h:279
Vector2d operator+(const Vector2d &v) const
Computes the vector sum of this vector with the given vector.
Definition: Vector2.h:219
The specificaiton of a message logger for menge, such that all messages to the system get properly re...
Type _y
y-component of the vector
Definition: Vector2.h:402
Vector2d & operator*=(float s)
Perform in-place scalar multiplication on this vector.
Definition: Vector2.h:267
Some common mathematical constants.
Vector2d operator-(const Vector2d &v) const
Computes the vector difference of this vector with the given vector.
Definition: Vector2.h:231
void zero()
Set the vector to zero.
Definition: Vector2.h:157
The namespace for math primitives for simulation and visualization.
Vector2d & operator-=(const Vector2d &v)
Perform in-place vector subtraction on this vector.
Definition: Vector2.h:304
Type Length() const
Compute the magnitude (aka length) of the vector.
Definition: Vector2.h:338
bool operator!=(const Vector2d &v) const
Reports if this vector is the different from the given vector.
Definition: Vector2.h:255
bool operator==(const Vector2d &v) const
Reports if this vector is the same as the given vector.
Definition: Vector2.h:243
void setY(Type y)
Set the y-value.
Definition: Vector2.h:150
Logger logger
Globally available Logger.
Definition: Logger.cpp:49
void normalize()
Normalize the vector in place.
Definition: Vector2.h:321
float distanceSq(const Vector2d &p) const
Compute the squared-distance from this vector to another point.
Definition: Vector2.h:375
Vector2d(const Vector2d &v)
Copy constructor.
Definition: Vector2.h:97
Vector2d(Type x, Type y)
Constructor with arguments.
Definition: Vector2.h:90
Type x() const
Get the x-value.
Definition: Vector2.h:106
Vector2d< Type > operator/(float s) const
Computes the scalar division of this vector with the given scalar value.
Definition: Vector2.h:206
Type y() const
Get the y-value.
Definition: Vector2.h:113
float distanceSq(float x, float y) const
Compute the squared-distance from this vector to another point.
Definition: Vector2.h:388