gwnavruntime/abstractgraph/blobs/halfsquarematrix.h Source File

halfsquarematrix.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 #ifndef Navigation_HalfSquaeMatrix_H
8 #define Navigation_HalfSquaeMatrix_H
9 
10 
13 
14 namespace Kaim
15 {
16 
17 template <class T>
18 class HalfSquareMatrix
19 {
20 public:
21  HalfSquareMatrix()
22  : m_buffer(KY_NULL)
23  , m_bufferSize(0)
24  , m_count(0)
25  {}
26 
27  HalfSquareMatrix(T* buffer, KyUInt32 bufferSize, KyUInt32 count)
28  : m_buffer(buffer)
29  , m_bufferSize(bufferSize)
30  , m_count(count)
31  {}
32 
33  T* GetValue(KyUInt32 rowIdx, KyUInt32 columnIdx) const
34  {
35  if (rowIdx < columnIdx)
36  {
37  KyUInt32 index = GetIndexInHalfMatrix(rowIdx, columnIdx);
38  return &m_buffer[index];
39  }
40  else if (rowIdx > columnIdx)
41  return GetValue(columnIdx, rowIdx); // invert column and row
42  else // if (rowIdx == columnIdx)
43  return KY_NULL;
44  }
45 
46 public: //internal
47 
48  // rowIdx must be strictly smaller than columnIdx
49  KyUInt32 GetIndexInHalfMatrix(KyUInt32 rowIdx, KyUInt32 columnIdx) const
50  {
51  KY_ASSERT(rowIdx < columnIdx);
52  KyUInt32 count = GetCount();
53  // index = (n*i+j) - (((i+1)^2)-(((i+1)^2)-(i+1))/2), where i is row, j is column, n is number of abstractGraphNodeCount
54  KyUInt32 fullMatrixIdx = count * rowIdx + columnIdx; // n*i+j
55  KyUInt32 rowSizedMatrixLength = rowIdx+1; // i+1
56  KyUInt32 rowSizedMatrixSize = rowSizedMatrixLength*rowSizedMatrixLength; // (i+1)*(i+1)
57  KyUInt32 rowSizedHalfMatrixSize = (rowSizedMatrixSize - rowSizedMatrixLength)/2; // (((i+1)*(i+1))-(i+1))/2
58  KyUInt32 elementToRemoveFromFullMatrix = rowSizedMatrixSize - rowSizedHalfMatrixSize; // (((i+1)*(i+1))-(((i+1)*(i+1))-(i+1))/2)
59  KyUInt32 index = fullMatrixIdx - elementToRemoveFromFullMatrix; // (n*i+j) - (((i+1)*(i+1))-(((i+1)*(i+1))-(i+1))/2)
60  return index;
61  }
62 
63  KyUInt32 GetCount() const
64  {
65  return m_count;
66  // if (m_bufferSize == 0)
67  // return 0;
68  //
69  // // quadratic formula : (1)*n*n + (-1)*n + (-size*2) = 0
70  // // discriminant : 1 - (4 * (-size*2)) //< always positive so there are 2 solutions
71  // // n = (1 +/- sqrt(1-4*(-size*2)))/2*1 //< pickup only the positive solution
72  // KyUInt32 res = (KyUInt32) (1.f + Sqrtf(1.f+8.f*((KyFloat32)m_bufferSize)));
73  // return (res >> 1);
74  }
75 
76  KyUInt32 ComputeBufferSize(KyUInt32 count) const
77  {
78  return ((count*count)-count) / 2;
79  }
80 
81 public:
82  // Here's how index of m_buffer are constructed,
83  // values given into the array are the corresponding index in m_buffer
84  //
85  // +-- Idx, m_count by m_count -----+
86  // | |
87  // V | 0 1 2 3 4 <----------------+
88  // --+------------
89  // 0 | # 0 1 2 3 T
90  // 1 | x # 4 5 6 |
91  // 2 | x x # 7 8 |
92  // 3 | x x x # 9 L___ indices in m_buffer
93  // 4 | x x x x #
94  //
95  // size of this array is (((n*n)-n) / 2) where n is m_count
96  //
97  T* m_buffer;
98  KyUInt32 m_bufferSize;
99  KyUInt32 m_count;
100 };
101 
102 }
103 
104 #endif
#define KY_NULL
Null value.
Definition: types.h:247
Definition: gamekitcrowddispersion.h:20
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36