gwnavruntime/math/orientedbox2d.h Source File

orientedbox2d.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 // Primary contact: JUBA - secondary contact: NOBODY
9 #ifndef Navigation_OrientedBox2d_H
10 #define Navigation_OrientedBox2d_H
11 
12 
15 
16 namespace Kaim
17 {
18 
19 /*
20  ^ +--------------------+
21  | | |
22  |width | |
23  | | |
24  | | | orientation
25  v A+--------------------+------------->
26 
27  <-------------------->
28  length
29 */
30 
51 class OrientedBox2d
52 {
54 public:
55  // ---------------------------------- Constructors ----------------------------------
56 
60  OrientedBox2d();
61 
68  OrientedBox2d(const Vec3f& a, const Vec2f& orientation, KyFloat32 length, KyFloat32 width, KyFloat32 thickness);
69 
70  // ---------------------------------- Main API Functions ----------------------------------
71 
72  bool IsValid();
73  void Clear();
74 
81  void Set(const Vec3f& a, const Vec2f& orientation, KyFloat32 length, KyFloat32 width, KyFloat32 thickness);
82 
85  void InitAs2dInflatedSegment(const Vec3f& start, const Vec3f& dest, KyFloat32 radius);
86 
87  bool IsInside2d(const Vec3f& pos);
88 
89  // ---------------------------------- Public Data Members ----------------------------------
90 
91  Vec3f m_a;
96 };
97 
98 
99 KY_INLINE OrientedBox2d::OrientedBox2d() : m_length(0.f), m_width(0.f), m_thickness(0.f) {}
100 
101 KY_INLINE OrientedBox2d::OrientedBox2d(const Vec3f& a, const Vec2f& orientation, KyFloat32 length, KyFloat32 width, KyFloat32 thickness) :
102 m_a(a), m_length(length), m_width(width), m_thickness(thickness)
103 {
105 }
106 
107 KY_INLINE bool OrientedBox2d::IsValid() { return m_normalizedOrientation != Vec2f::Zero(); }
108 KY_INLINE void OrientedBox2d::Clear() { Set(Vec3f::Zero(), Vec2f::Zero(), 0.f, 0.f, 0.f); }
110 KY_INLINE void OrientedBox2d::Set(const Vec3f& a, const Vec2f& orientation, KyFloat32 length, KyFloat32 width, KyFloat32 thickness)
111 {
112  KY_DEBUG_ASSERTN(orientation.GetLength() == Vec2f(orientation).Normalize(), ("Wrong BoxObtacle parameter, the orientation should be normalized"));
113 
114  m_a.Set(a.x, a.y, a.z);
115  m_normalizedOrientation.Set(orientation.x, orientation.y);
116  m_length = length;
117  m_width = width;
118  m_thickness = thickness;
119 }
120 
121 inline void OrientedBox2d::InitAs2dInflatedSegment(const Vec3f& start, const Vec3f& dest, KyFloat32 radius)
122 {
123  Vec2f orientation = dest.Get2d() - start.Get2d();
124  KyFloat32 length = orientation.GetNormalized(m_normalizedOrientation);
125 
126  if (length == 0.f)
128 
129  m_length = length + 2.f*radius;
130  m_width = 2.f*radius;
131  // a = start - m_normalizedOrientation * radius - m_normalizedOrientation.PerpCCW() * radius
132  m_a.x = start.x - m_normalizedOrientation.x * radius + m_normalizedOrientation.y * radius;
133  m_a.y = start.y - m_normalizedOrientation.y * radius - m_normalizedOrientation.x * radius;
134  m_a.z = -KyFloat32MAXVAL;
136 }
138 inline bool OrientedBox2d::IsInside2d(const Vec3f& pos)
139 {
140  const Vec2f AtoP = pos.Get2d() - m_a.Get2d();
141  KyFloat32 dotProd1 = DotProduct(AtoP, m_normalizedOrientation);
143  if (dotProd1 < 0.f || dotProd1 > m_length ||
144  dotProd2 < 0.f || dotProd2 > m_width )
145  return false;
146 
147  return true;
148 }
149 
150 }
151 
152 #endif
153 
KyFloat32 Normalize()
Normalizes the vector, making it one unit in length without changing its orientation.
Definition: vec2f.h:267
KyFloat32 m_length
The extents of the box along its orientation vector.
Definition: orientedbox2d.h:109
#define KyFloat32MAXVAL
The maximum value that can be stored in the KyFloat32 variable type.
Definition: types.h:227
This class represents a three-dimensional bounding box with a free orientation on the (X...
Definition: orientedbox2d.h:53
KyFloat32 z
The size of the vector along the Z axis.
Definition: vec3f.h:229
static Vec2f UnitX()
Returns the normalized orientation of the X axis.
Definition: vec2f.h:157
void Set(KyFloat32 _x, KyFloat32 _y, KyFloat32 _z)
Sets the coordinates.
Definition: vec3f.h:52
KyFloat32 x
The size of the vector along the X axis.
Definition: vec2f.h:169
void Set(KyFloat32 _x, KyFloat32 _y)
Sets the coordinates of the vector.
Definition: vec2f.h:47
void Set(const Vec3f &a, const Vec2f &orientation, KyFloat32 length, KyFloat32 width, KyFloat32 thickness)
Sets the dimensions of the box.
Definition: orientedbox2d.h:126
static Vec3f Zero()
Returns a vector of zero size: (0,0,0).
Definition: vec3f.h:209
KyFloat32 GetNormalized(Vec2f &normalized) const
Normalizes the vector, making it one unit in length without changing its orientation.
Definition: vec2f.h:276
void InitAs2dInflatedSegment(const Vec3f &start, const Vec3f &dest, KyFloat32 radius)
Creates a new OrientedBox2d that will be an inflate of the segment going from start to dest of radius...
Definition: orientedbox2d.h:137
KyFloat32 y
The size of the vector along the Y axis.
Definition: vec3f.h:228
OrientedBox2d()
Creates a new OrientedBox2d with 0.0f length, width and thickness.
Definition: orientedbox2d.h:115
Vec2f Get2d() const
Returns a Vec2f that contains the X and Y coordinates of this vector. The Z coordinate is ignored...
Definition: vec3f.h:188
Vec2f PerpCCW() const
Rotates this vector 90 degrees counter-clockwise (negating the Y coordinate).
Definition: vec2f.h:144
KyFloat32 x
The size of the vector along the X axis.
Definition: vec3f.h:227
This class defines a two-dimensional vector whose coordinates are stored using floating-point numbers...
Definition: vec2f.h:24
Definition: gamekitcrowddispersion.h:20
KyFloat32 DotProduct(const Vec2f &v1, const Vec2f &v2)
Returns the dot product of v1 and v2.
Definition: vec2f.h:187
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
KyFloat32 m_thickness
The extents of the box along the vertical Z axis.
Definition: orientedbox2d.h:111
static Vec2f Zero()
Returns a vector of zero size: (0,0).
Definition: vec2f.h:154
Vec2f m_normalizedOrientation
The facing orientation of the box (unit vector).
Definition: orientedbox2d.h:108
KyFloat32 GetLength() const
Returns the magnitude of the vector.
Definition: vec2f.h:126
Vec3f m_a
The position of the reference corner.
Definition: orientedbox2d.h:107
KyFloat32 m_width
The extents of the box perpendicular to its orientation vector on the (X,Y) plane.
Definition: orientedbox2d.h:110
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43
This class defines a three-dimensional vector whose coordinates are stored using floating-point numbe...
Definition: vec3f.h:23
KyFloat32 y
The size of the vector along the Y axis.
Definition: vec2f.h:170