gwnavgeneration/navraster/navrastercellscanlinepainter.h Source File

navrastercellscanlinepainter.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: LASI - secondary contact: NONE
9 #ifndef GwNavGen_NavRasterCellScanlinePainter_H
10 #define GwNavGen_NavRasterCellScanlinePainter_H
11 
12 
19 
20 namespace Kaim
21 {
22 
23 class DynamicNavRasterCell;
24 class NavRasterPixel;
25 class NavRasterWorkingPixel;
26 class DynamicNavRasterFinalColumn;
27 
28 class NavRasterCellScanlinePainter
29 {
30 public:
31  enum SearchResult { SEARCH_RESULT_FOUND, NO_SEARCH_RESULT };
32  class ScanPixel
33  {
34  public:
35  ScanPixel() : m_navPixel(KY_NULL), m_pos(KyInt32MAXVAL,KyInt32MAXVAL), m_floorIdx(KyUInt32MAXVAL) {}
36  ScanPixel(NavRasterPixel* columnElement, const PixelPos& pos, KyUInt32 floor) :
37  m_navPixel(columnElement), m_pos(pos), m_floorIdx(floor) {}
38 
39  NavRasterPixel* m_navPixel;
40  PixelPos m_pos;
41  KyUInt32 m_floorIdx;
42  };
43 
44  class ColorBoundary
45  {
46  public:
47  ColorBoundary() {}
48 
49  ScanPixel m_fisrtBoundaryPixel;
50  ScanPixel m_lastBoundaryPixel;
51  CardinalDir m_boundaryDir;
52  };
53 
54  class ColorData
55  {
56  public:
57  ColorData(PixelColor color = PixelColor_Unset, KyUInt32 connectedComponentIdx = KyUInt32MAXVAL) :
58  m_color(color), m_pixelCount(0), m_connectedComponentIdx(connectedComponentIdx),
59  m_boundaryPixelBorderCount(0) , m_indexInBinaryHeap(BinaryHeapInvalidReference) {}
60  void UpdateColorDataFromNewPixel(NavRasterPixel* pixel, const PixelPos& pos);
61  public:
62  PixelColor m_color;
63  PixelBox m_box;
64  KyUInt32 m_pixelCount;
65  KyUInt32 m_connectedComponentIdx;
66  KyUInt32 m_boundaryPixelBorderCount;
67  KyUInt32 m_indexInBinaryHeap;
68  FloorAltitudeRange m_altRange;
69  };
70 
71 public: //keep it simple
72 
73  NavRasterCellScanlinePainter() : m_navRaster(KY_NULL), m_paintingMs(KY_NULL) {}
74 
75  void Clear();
76  KyResult Paint();
77 
78  void SetNavRasterCell(DynamicNavRasterCell* cell) { m_navRaster = cell; }
79  void SetConnexIdxToNavTagIdxArray(KyArrayTLS_POD<KyUInt32>* connexIdxToNavTagIdxArray) { m_connexIdxToNavTagIdxArray = connexIdxToNavTagIdxArray ;}
80  void SetPaintingMs(KyFloat32* paintingMs) { m_paintingMs = paintingMs; }
81 
82  KyUInt32 GetNbColorsUsed() const { return m_colorCount; }
83 private:
84  // 1 -
85  void Init();
86 
87  // 2 -
88  void RemoveSmallNavTagArea();
89 
90  // 3 -
91  void ComputeConnectedComponent();
92 
93  // 4 -
94  void ComputeColorsByPropagation();
95 
96  // 5 -
97  void MergeColors();
98 
99  // 6 -
100  void TagConnex();
101 
102 private:
103  void PropagateInSouthWestBox();
104  void PropagateInSouthEastBox();
105  void PropagateInNorthWestBox();
106  void PropagateInNorthEastBox();
107 
108  void PropagateFromCornerColumn(const PixelBox& paintBox, const PixelPos& cornerPos, CardinalDir expandLineDir, CardinalDir lineMoveDir);
109  void PropagateFromCornerPixel(const PixelBox& paintBox, const ScanPixel& seedPixel, CardinalDir expandLineDir, CardinalDir lineMoveDir);
110  PixelBox ComputeLocalPaintBoxForCorner(const PixelBox& paintBox, const ScanPixel& corner, CardinalDir expandLineDir, CardinalDir lineMoveDir);
111 
112  void PropagateFromBoxBorder(const PixelBox& paintBox, const PixelPos& startPos, CardinalDir expandLineDir, CardinalDir lineMoveDir);
113  void PropagateFromBorderPixel(const PixelBox& paintBox, const ScanPixel& seedPixel, CardinalDir expandLineDir, CardinalDir lineMoveDir);
114  PixelBox ComputeLocalPaintBoxForBorder(const PixelBox& paintBox, const ScanPixel& seedPixel, CardinalDir expandLineDir, CardinalDir lineMoveDir);
115 
116  void DisconnectOutsidePixelsAtBoxBounds(const PixelBox& paintBox, const ScanPixel& corner, CardinalDir dirAlongBorder,
117  CardinalDir dirToOutside, PixelColor color);
118 
119  void PropagateFromAllRemainingUncoloredPixels();
120  void PropagateFromUncoloredNonBorderPixel(const ScanPixel& startPixel);
121 
122  void ColorCellBoundaryPixelLineInBox(const PixelBox& localPaintBox, const ScanPixel& startPixel, PixelColor color,
123  CardinalDir expandDir, CardinalDir borderDir, ScanPixel& lastPixelOnLine);
124 
125  bool TwoNeighborPixelHaveSameColor(const ScanPixel& pixel1, const ScanPixel& pixel2, CardinalDir dir, PixelColor currentColor);
126  bool CanPropagateToPixel(PixelPos& pixelPos, KyUInt32 currentFloorIdx, KyUInt32 propagationIdx);
127 
128  void FindLowerConnectedPixel(const ScanPixel& startPixel, ScanPixel& lowerPixel);
129  void CastPixelUntillBoundary(const ScanPixel& startScanPixel, CardinalDir castDir);
130 
131  PixelPos CastCellBoundaryPixel(const PixelBox& bbox, const ScanPixel& borderPixel, CardinalDir castDir, CardinalDir borderDir);
132  void CastPixelCheckingColor(const ScanPixel& scanLine, PixelColor color, const PixelBox& bbox, CardinalDir castdir);
133  void PaintLastPixelCastResultAndOutsideNeighbors(PixelColor color, CardinalDir borderDir);
134 
135  bool IsConnectedInDir(const ScanPixel& pixel, CardinalDir dir, ScanPixel& result);
136  void GetNeighborScanPixel(const ScanPixel& borderPixel, CardinalDir neighborDir, ScanPixel& neighborPixel);
137  void GetNeighborScanPixel(const PixelBox& bbox, const ScanPixel& borderPixel, CardinalDir neighborDir, ScanPixel& neighborPixel);
138 
139  PixelColor GetNewColor(KyUInt32 connectedComponentIdx);
140  void ColorPixel(const ScanPixel& pixel, PixelColor color);
141 
142  bool CanSetColorToPixel(PixelPos& pixelPos, KyUInt32 floor, PixelColor newColor);
143  bool IsColorPresentInOtherFloorOfColumn(const DynamicNavRasterFinalColumn& column, KyUInt32 ignoredFloorIdx, PixelColor newColor);
144  bool IsColorPresentInOtherFloorOfColumn(const DynamicNavRasterFinalColumn& column, KyUInt32 ignoredFloorIdx1, KyUInt32 ignoredFloorIdx2, PixelColor newColor);
145 
146  bool CanPaintNeighborLine(const ScanPixel& startScanPixel, const ScanPixel& endScanPixel, PixelColor color, const PixelBox& bbox,
147  CardinalDir lineDir, CardinalDir expandDir, ScanPixel& borderLineStartScanPixel, ScanPixel& borderLineEndScanPixel,
148  KyUInt32& neighborLineCount, KyFloat32& altRangeDiffWithNeighborLine);
149 
150  // 4 -
151  void DetectForbiddenColorMergeAndConnectedColors();
152  void ComputeMergedColors();
153  void ApplyFinalColorToPixels();
154 
155  class ScanPixelSorterByAlt
156  {
157  public:
158  bool operator() (const ScanPixel& pixel1, const ScanPixel& pixel2) const
159  {
160  return pixel1.m_navPixel->m_altitude < pixel2.m_navPixel->m_altitude;
161  }
162  };
163 
164  void TagNavTagConnectedComponent(const PixelBox& safenavTagRemovalBox, const ScanPixel& startPixel, KyUInt32 connectedComponentIdx, KyUInt32 minPixelCount);
165  void TagConnectedComponent(const ScanPixel& startPixel, KyUInt32 connectedComponentIdx);
166  void TagConnexConnectedComponentInFloor(const ScanPixel& startPixel, KyUInt32 currentConnexIdx);
167 private:
168  DynamicNavRasterCell* m_navRaster;
169  KyFloat32* m_paintingMs;
170  KyUInt32 m_colorCount;
171  KyArrayTLS<ScanPixel> m_popagationStatck; // cache the array between floodFills to avoid multiple resizes
172  KyArrayTLS_POD<ScanPixel> m_navTagArea;
173  KyArrayTLS_POD<ScanPixel> m_navTagAreaNeighbors;
174  KyArrayTLS<ColorData> m_colorData;
175 
176  BitFieldTLS m_forbiddenColorMerge;
177  BitFieldTLS m_connectedColors;
178  KyArrayTLS_POD<PixelColor> m_finalColor; // m_finalColor[color] = finalColor after merge !
179 
180  KyArrayTLS<ScanPixel> m_castForwardResult;
181  KyArrayTLS<ScanPixel> m_castBackwardResult;
182  KyArrayTLS<ScanPixel> m_onePixelPerFinalColor;
183 
184  BinaryHeapTls<ScanPixel, ScanPixelSorterByAlt> m_orderedPixels;
185  KyArrayTLS_POD<KyUInt32>* m_connexIdxToNavTagIdxArray;
186 
187  KyInt32 m_midPixelPosX;
188  KyInt32 m_midPixelPosY;
189  KyUInt32 m_currentPropagationId;
190 };
191 
192 
193 KY_INLINE bool NavRasterCellScanlinePainter::IsConnectedInDir(const ScanPixel& pixel, CardinalDir dir, ScanPixel& result)
194 {
195  GetNeighborScanPixel(pixel, dir, result);
196  return result.m_navPixel != KY_NULL;
197 }
198 
199 KY_INLINE PixelColor NavRasterCellScanlinePainter::GetNewColor(KyUInt32 connectedComponentIdx)
200 {
201  PixelColor color = m_colorData.GetCount();
202  m_colorData.PushBack(ColorData(color, connectedComponentIdx));
203  return color;
204 }
205 KY_INLINE void NavRasterCellScanlinePainter::ColorPixel(const ScanPixel& pixel, PixelColor color)
206 {
207  KY_ASSERT(pixel.m_navPixel != KY_NULL);
208  KY_ASSERT(pixel.m_navPixel->m_floorColor == PixelColor_Unset);
209  KY_ASSERT(color < m_colorData.GetCount());
210 
211  pixel.m_navPixel->m_floorColor = color;
212  m_colorData[color].UpdateColorDataFromNewPixel(pixel.m_navPixel, pixel.m_pos);
213 }
214 KY_INLINE void NavRasterCellScanlinePainter::ColorData::UpdateColorDataFromNewPixel(NavRasterPixel* currentPixel, const PixelPos& pos)
215 {
216  KY_LOG_ERROR_IF(currentPixel->m_floorColor != m_color, ("Wrong color data for this pixel"));
217  ++m_pixelCount;
218  m_altRange.Update(currentPixel->m_altitude);
219  m_box.ExpandByVec2(pos);
220  for(KyUInt32 i = 0; i < 4; ++i)
221  {
222  if (currentPixel->m_neighborFloorIdx[i] == NavRasterFloorIdx_Invalid)
223  ++m_boundaryPixelBorderCount;
224  }
225 }
226 
227 
228 }
229 
230 #endif
KyInt32 KyResult
Defines a type that can be returned by methods or functions in the Gameware Navigation SDK to indicat...
Definition: types.h:254
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
#define KY_NULL
Null value.
Definition: types.h:247
KyUInt32 CardinalDir
Defines a type that refers to one of the cardinal points on the compass:
Definition: cardinaldir.h:23
#define KyInt32MAXVAL
The maximum value that can be stored in the KyInt32 variable type.
Definition: types.h:224
Definition: gamekitcrowddispersion.h:20
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
#define KyUInt32MAXVAL
The maximum value that can be stored in the KyUInt32 variable type.
Definition: types.h:226
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43