gwnavruntime/math/orientedbox2d.h Source File

orientedbox2d.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 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 #pragma once
8 
12 
13 namespace Kaim
14 {
15 
27 {
28  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
29 public:
30  // ---------------------------------- Constructors ----------------------------------
31 
33  OrientedBox2d() : m_forward_length(0.0f), m_left_length(0.0f), m_height(0.0f) {}
34 
36  OrientedBox2d(const Vec3f& corner, const Vec2f& forward, KyFloat32 forward_length, KyFloat32 left_length, KyFloat32 height) :
37  m_corner(corner), m_forward_length(forward_length), m_left_length(left_length), m_height(height)
38  {
39  forward.GetNormalized(m_forward_dir2d);
40  }
41 
42  // ---------------------------------- Main API Functions ----------------------------------
43 
44  bool IsValid() { return m_forward_dir2d != Vec2f::Zero(); }
45 
46  void Clear() { Set(Vec3f::Zero(), Vec2f::Zero(), 0.0f, 0.0f, 0.0f); }
47 
49  void Set(const Vec3f& corner, const Vec2f& forward_dir2d, KyFloat32 forward_length, KyFloat32 left_length, KyFloat32 height)
50  {
51  KY_DEBUG_ASSERTN(forward_dir2d.GetLength() == Vec2f(forward_dir2d).Normalize(), ("Wrong OrientedBox2d.Set() parameters, forward_dir2d should be normalized"));
52 
53  m_corner = corner;
54  m_forward_dir2d = forward_dir2d;
55  m_forward_length = forward_length;
56  m_left_length = left_length;
57  m_height = height;
58  }
59 
62  void InitAs2dInflatedSegment(const Vec3f& start, const Vec3f& dest, KyFloat32 radius);
63 
64  bool IsInside2d(const Vec3f& pos);
65 
66  Box3f ComputeAABB() const;
67 
68 public:
69  // ---------------------------------- Public Data Members ----------------------------------
70  Vec3f m_corner;
71  Vec2f m_forward_dir2d;
72  KyFloat32 m_forward_length;
73  KyFloat32 m_left_length;
74  KyFloat32 m_height;
75 };
76 
77 inline void OrientedBox2d::InitAs2dInflatedSegment(const Vec3f& P, const Vec3f& Q, KyFloat32 radius)
78 {
79  Vec2f dir2d = Q.Get2d() - P.Get2d();
80  KyFloat32 length = dir2d.GetNormalized(m_forward_dir2d);
81 
82  if (length == 0.0f)
83  m_forward_dir2d = Vec2f::UnitX();
84 
85  m_forward_length = length + 2.0f * radius;
86  m_left_length = 2.0f * radius;
87  m_corner.x = P.x - m_forward_dir2d.x * radius + m_forward_dir2d.y * radius;
88  m_corner.y = P.y - m_forward_dir2d.y * radius - m_forward_dir2d.x * radius;
89  m_corner.z = -KyFloat32MAXVAL;
90  m_height = KyFloat32MAXVAL;
91 }
92 
93 inline bool OrientedBox2d::IsInside2d(const Vec3f& P)
94 {
95  const Vec2f& AP = P.Get2d() - m_corner.Get2d();
96  KyFloat32 along_dir = DotProduct(AP, m_forward_dir2d);
97  KyFloat32 ortho_dir = DotProduct(AP, m_forward_dir2d.PerpCCW());
98  if (along_dir < 0.0f || along_dir > m_forward_length || ortho_dir < 0.0f || ortho_dir > m_left_length)
99  return false;
100 
101  return true;
102 }
103 
104 inline Box3f OrientedBox2d::ComputeAABB() const
105 {
106  Vec2f leftDir = m_forward_dir2d.PerpCCW();
107  Vec3f B = m_corner + m_forward_dir2d * m_forward_length;
108  Vec3f C = B + leftDir * m_left_length;
109  Vec3f D = m_corner + leftDir * m_left_length;
110  D.z += m_height; // used to expand altitude as well
111 
112  Box3f aabb;
113  aabb.ExpandByPos(m_corner);
114  aabb.ExpandByPos(B);
115  aabb.ExpandByPos(C);
116  aabb.ExpandByPos(D);
117  return aabb;
118 }
119 
120 
121 }
122 
123 
#define KyFloat32MAXVAL
KyFloat32 max value
Definition: types.h:71
OrientedBox2d is a 3d box with rotation constrained to be along Z.
Definition: orientedbox2d.h:26
void Set(const Vec3f &corner, const Vec2f &forward_dir2d, KyFloat32 forward_length, KyFloat32 left_length, KyFloat32 height)
Init OrientedBox2d. dir2d must be normalized.
Definition: orientedbox2d.h:49
static Vec2f UnitX()
Returns {1.0f, 0.0f}.
Definition: vec2f.h:97
static Vec3f Zero()
Returns {0.0f, 0.0f, 0.0f}.
Definition: vec3f.h:99
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:77
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
OrientedBox2d()
Construct a zero size OrientedBox2d.
Definition: orientedbox2d.h:33
3d axis aligned box of 32bits floating points
Definition: box3f.h:16
Vec2f PerpCCW() const
Rotates by 90 degrees counter-clockwise.
Definition: vec2f.h:79
2d vector using KyFloat32.
Definition: vec2f.h:18
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
OrientedBox2d(const Vec3f &corner, const Vec2f &forward, KyFloat32 forward_length, KyFloat32 left_length, KyFloat32 height)
Construct OrientedBox2d. forward will be normalized.
Definition: orientedbox2d.h:36
static Vec2f Zero()
Returns {0.0f, 0.0f}.
Definition: vec2f.h:93
float KyFloat32
float
Definition: types.h:32
3d vector using 32bits floating points.
Definition: vec3f.h:16