ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
FastMarching.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // Local
11 #include "CVConst.h"
12 #include "CVGeom.h"
13 
14 // system
15 #include <cfloat>
16 #include <cstdio>
17 #include <cstdlib>
18 #include <cstring>
19 #include <vector>
20 
21 namespace cloudViewer {
22 
23 class DgmOctree;
24 
25 // Maximum number of neighbors
26 #define CC_FM_MAX_NUMBER_OF_NEIGHBOURS 26
27 
29 const int c_FastMarchingNeighbourPosShift[] = { // 6 common faces
30  0, -1, 0, 1, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, -1, 0, 0, 1,
31  // 20 other neighbors
32  -1, -1, -1, -1, -1, 0, -1, -1, 1, -1, 0, -1, -1, 0, 1, -1, 1, -1, -1, 1,
33  0, -1, 1, 1, 0, -1, -1, 0, -1, 1, 0, 1, -1, 0, 1, 1, 1, -1, -1, 1, -1,
34  0, 1, -1, 1, 1, 0, -1, 1, 0, 1, 1, 1, -1, 1, 1, 0, 1, 1, 1};
35 
37 
42 public:
44  FastMarching();
45 
47  virtual ~FastMarching();
48 
50 
53  virtual bool setSeedCell(const Tuple3i& pos);
54 
56 
59  virtual int propagate() = 0;
60 
65  virtual void cleanLastPropagation();
66 
68 
76  virtual float getTime(Tuple3i& pos, bool absoluteCoordinates = false) const;
77 
79 
81  virtual void setExtendedConnectivity(bool state) {
82  m_numberOfNeighbours = state ? 26 : 6;
83  }
84 
85 protected:
86  // Macro: cell position [i,j,k] to table (3D grid) index
87  inline unsigned pos2index(const Tuple3i& pos) const {
88  return static_cast<unsigned>(pos.x - m_minFillIndexes.x) +
89  static_cast<unsigned>(pos.y - m_minFillIndexes.y) * m_rowSize +
90  static_cast<unsigned>(pos.z - m_minFillIndexes.z) * m_sliceSize +
91  m_indexShift;
92  }
93 
95  class Cell {
96  public:
98  inline static float T_INF() { return FLT_MAX; }
99 
101  enum STATE {
102  EMPTY_CELL = 0,
103  FAR_CELL = 1,
104  TRIAL_CELL = 2,
105  ACTIVE_CELL = 3
106  };
107 
109  Cell() : state(FAR_CELL), T(T_INF()) {}
110 
112  virtual ~Cell() = default;
113 
116 
118  float T;
119  };
120 
123 
127  virtual int initGridWithOctree(DgmOctree* octree, unsigned char gridLevel);
128 
130 
134  virtual int initGrid(float step, unsigned dim[3]);
135 
138  virtual int initOther();
139 
141 
145  virtual float computeT(unsigned index);
146 
148 
152  virtual float computeTCoefApprox(Cell* currentCell,
153  Cell* neighbourCell) const = 0;
154 
156 
158  virtual int step() = 0;
159 
161 
163  virtual void initTrialCells();
164 
166 
170  virtual bool instantiateGrid(unsigned size) = 0;
171 
173  template <class T>
174  bool instantiateGridTpl(unsigned size) {
175  if (m_theGrid) return false;
176 
177  T** grid = new T*[size];
178  if (!grid) return false;
179  memset(grid, 0, size * sizeof(T*));
180 
181  m_theGrid = reinterpret_cast<Cell**>(grid);
182 
183  return true;
184  }
185 
187 
189  virtual void addTrialCell(unsigned index);
190 
192 
194  virtual void addActiveCell(unsigned index);
195 
197 
199  virtual void addIgnoredCell(unsigned index);
200 
202 
204  virtual unsigned getNearestTrialCell();
205 
207 
209  void resetCells(std::vector<unsigned>& list);
210 
212  std::vector<unsigned> m_activeCells;
214  std::vector<unsigned> m_trialCells;
216  std::vector<unsigned> m_ignoredCells;
217 
221  unsigned m_dx;
223  unsigned m_dy;
225  unsigned m_dz;
227  unsigned m_rowSize;
229  unsigned m_sliceSize;
231  unsigned m_indexShift;
233  unsigned m_gridSize;
236 
240  unsigned char m_gridLevel;
242  float m_cellSize;
245 
249  int m_neighboursIndexShift[CC_FM_MAX_NUMBER_OF_NEIGHBOURS];
251  float m_neighboursDistance[CC_FM_MAX_NUMBER_OF_NEIGHBOURS];
252 };
253 
254 } // namespace cloudViewer
#define CV_CORE_LIB_API
Definition: CVCoreLibWin.h:15
#define CC_FM_MAX_NUMBER_OF_NEIGHBOURS
Definition: FastMarching.h:26
int size
Type y
Definition: CVGeom.h:137
Type x
Definition: CVGeom.h:137
Type z
Definition: CVGeom.h:137
The octree structure used throughout the library.
Definition: DgmOctree.h:39
A generic Fast Marching grid cell.
Definition: FastMarching.h:95
Cell()
Default constructor.
Definition: FastMarching.h:109
virtual ~Cell()=default
Virtual destructor.
static float T_INF()
Returns infinite time value.
Definition: FastMarching.h:98
STATE
Possible states of a Fast Marching grid cell.
Definition: FastMarching.h:101
float T
Front arrival time.
Definition: FastMarching.h:118
Fast Marching algorithm (front propagation)
Definition: FastMarching.h:41
virtual int propagate()=0
Propagates the front.
float m_cellSize
Octree cell size at equivalent subdivision level.
Definition: FastMarching.h:242
unsigned pos2index(const Tuple3i &pos) const
Definition: FastMarching.h:87
Cell ** m_theGrid
Grid used to process Fast Marching.
Definition: FastMarching.h:235
unsigned m_dx
Grid size along the X dimension.
Definition: FastMarching.h:221
DgmOctree * m_octree
Associated octree.
Definition: FastMarching.h:238
std::vector< unsigned > m_activeCells
ACTIVE cells list.
Definition: FastMarching.h:212
unsigned char m_gridLevel
Equivalent octree subdivision level.
Definition: FastMarching.h:240
virtual bool instantiateGrid(unsigned size)=0
Instantiates grid in memory.
bool m_initialized
Specifiies whether structure is initialized or not.
Definition: FastMarching.h:219
unsigned m_indexShift
First index of innerbound grid.
Definition: FastMarching.h:231
Tuple3i m_minFillIndexes
Octree min fill indexes at 'm_gridLevel'.
Definition: FastMarching.h:244
unsigned m_gridSize
Grid size.
Definition: FastMarching.h:233
std::vector< unsigned > m_trialCells
TRIAL cells list.
Definition: FastMarching.h:214
virtual float computeTCoefApprox(Cell *currentCell, Cell *neighbourCell) const =0
Computes the front acceleration between two cells.
unsigned m_dz
Grid size along the Z dimension.
Definition: FastMarching.h:225
unsigned m_rowSize
Shift for cell access acceleration (Y dimension)
Definition: FastMarching.h:227
virtual void setExtendedConnectivity(bool state)
Sets extended connectivity mode.
Definition: FastMarching.h:81
std::vector< unsigned > m_ignoredCells
IGNORED cells lits.
Definition: FastMarching.h:216
unsigned m_sliceSize
Shift for cell access acceleration (Z dimension)
Definition: FastMarching.h:229
bool instantiateGridTpl(unsigned size)
Grid instantiation helper.
Definition: FastMarching.h:174
unsigned m_numberOfNeighbours
Current number of neighbours (6 or 26)
Definition: FastMarching.h:247
virtual int step()=0
Propagates the front (one step)
unsigned m_dy
Grid size along the Y dimension.
Definition: FastMarching.h:223
Generic file read and write utility for python interface.
const int c_FastMarchingNeighbourPosShift[]
Grid neighboring cells positions.
Definition: FastMarching.h:29
cloudViewer::DgmOctree * octree