1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
//##########################################################################
//# #
//# CCLIB #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU Library General Public License as #
//# published by the Free Software Foundation; version 2 or later of the #
//# License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
//# #
//##########################################################################
#ifndef FAST_MARCHING_HEADER
#define FAST_MARCHING_HEADER
//Local
#include "CCConst.h"
#include "CCGeom.h"
//system
#include <cfloat>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
namespace CCLib
{
class DgmOctree;
// Maximum number of neighbors
#define CC_FM_MAX_NUMBER_OF_NEIGHBOURS 26
//! Grid neighboring cells positions
const int c_FastMarchingNeighbourPosShift[] = { //6 common faces
0,-1, 0,
1, 0, 0,
0, 1, 0,
-1, 0, 0,
0, 0,-1,
0, 0, 1,
// 20 other neighbors
-1,-1,-1,
-1,-1, 0,
-1,-1, 1,
-1, 0,-1,
-1, 0, 1,
-1, 1,-1,
-1, 1, 0,
-1, 1, 1,
0,-1,-1,
0,-1, 1,
0, 1,-1,
0, 1, 1,
1,-1,-1,
1,-1, 0,
1,-1, 1,
1, 0,-1,
1, 0, 1,
1, 1,-1,
1, 1, 0,
1, 1, 1 };
//! Fast Marching algorithm (front propagation)
/** Implementation of the Fast Marching algorithm [Sethian 1996].
Inspired from the "vtkFastMarching" class of the "Slicer"
project (http://www.slicer.org).
**/
class CC_CORE_LIB_API FastMarching
{
public:
//! Default constructor
FastMarching();
//! Destructor
virtual ~FastMarching();
//! Sets a given cell as "seed"
/** \param pos the cell position in the grid (3 integer coordinates)
\return whether the cell could be set as a seed or not
**/
virtual bool setSeedCell(const Tuple3i& pos);
//! Propagates the front
/** The seeds should have already been initialized
\return propagation result (errors = negative values)
**/
virtual int propagate() = 0;
/** Finalizes an iteration process
Resets the different lists and the grid. This method should be
called after each propagation (before starting a new one).
**/
virtual void cleanLastPropagation();
//! Returns the front arrival time at a given cell
/** This method should only be called after the propagation
succeeded. The coordinates of the cell can be absolute
(i.e. expressed relatively to the octree borders) or relative
(i.e. expressed relatively to the Fast Marching grid).
\param pos the cell position (3 integer coordinates)
\param absoluteCoordinates whether the cell coordinates are absolute or relative
**/
virtual float getTime(Tuple3i& pos, bool absoluteCoordinates = false) const;
//! Sets extended connectivity mode
/** To use common edges instead of common faces (much slower)
**/
virtual void setExtendedConnectivity(bool state) { m_numberOfNeighbours = state ? 26 : 6; }
protected:
// Macro: cell position [i,j,k] to table (3D grid) index
inline unsigned pos2index(const Tuple3i& pos) const
{
return static_cast<unsigned>(pos.x - m_minFillIndexes.x)
+ static_cast<unsigned>(pos.y - m_minFillIndexes.y) * m_rowSize
+ static_cast<unsigned>(pos.z - m_minFillIndexes.z) * m_sliceSize + m_indexShift;
}
//! A generic Fast Marching grid cell
class Cell
{
public:
//! Returns infinite time value
inline static float T_INF() { return FLT_MAX; }
//! Possible states of a Fast Marching grid cell
enum STATE { EMPTY_CELL = 0,
FAR_CELL = 1,
TRIAL_CELL = 2,
ACTIVE_CELL = 3 };
//! Default constructor
Cell()
: state(FAR_CELL)
, T(T_INF())
{}
//! Virtual destructor
virtual ~Cell() = default;
//! Cell state
STATE state;
//! Front arrival time
float T;
};
//! Intializes the grid as a snapshot of an octree structure at a given subdivision level
/** \param octree input octree
\param gridLevel subdivision level
\return a negative value if a problem occurred
**/
virtual int initGridWithOctree(DgmOctree* octree, unsigned char gridLevel);
//! Intializes the grid with a given step and dimensions
/** \param step grid step
\param dim grid dimensions in 3D
\return a negative value if a problem occurred
**/
virtual int initGrid(float step, unsigned dim[3]);
//! Intializes other dimension-related variables and finishes the initialization job
virtual int initOther();
//! Computes the front arrival time at a given cell
/** the cell is represented by its index in the cell list
\param index the cell index
\return the computed front arrival time
**/
virtual float computeT(unsigned index);
//! Computes the front acceleration between two cells
/** \param currentCell the "central" cell
\param neighbourCell the other cell
\return the front acceleration
**/
virtual float computeTCoefApprox(Cell* currentCell, Cell* neighbourCell) const = 0;
//! Propagates the front (one step)
/** \return a negative value if a problem occurred
**/
virtual int step() = 0;
//! Initializes the TRIAL cells list
/** See the Fast Marching algorithm theory for more information
**/
virtual void initTrialCells();
//! Instantiates grid in memory
/** Grid is also filled with zeros.
\param size grid size
\return success
**/
virtual bool instantiateGrid(unsigned size) = 0;
//! Grid instantiation helper
template <class T> bool instantiateGridTpl(unsigned size)
{
if (m_theGrid)
return false;
T** grid = new T*[size];
if (!grid)
return false;
memset(grid,0,size*sizeof(T*));
m_theGrid = reinterpret_cast<Cell **>(grid);
return true;
}
//! Add a cell to the TRIAL cells list
/** \param index index of the cell
**/
virtual void addTrialCell(unsigned index);
//! Add a cell to the ACTIVE cells list
/** \param index index of the cell
**/
virtual void addActiveCell(unsigned index);
//! Add a cell to the IGNORED cells list
/** \param index index of the cell
**/
virtual void addIgnoredCell(unsigned index);
//! Returns the TRIAL cell with the smallest front arrival time
/** \return the index of the "earliest" TRIAL cell (or 0 in case of error)
**/
virtual unsigned getNearestTrialCell();
//! Resets the state of cells in a given list
/** Warning: the list will be cleared!
**/
void resetCells(std::vector<unsigned>& list);
//! ACTIVE cells list
std::vector<unsigned> m_activeCells;
//! TRIAL cells list
std::vector<unsigned> m_trialCells;
//! IGNORED cells lits
std::vector<unsigned> m_ignoredCells;
//! Specifiies whether structure is initialized or not
bool m_initialized;
//! Grid size along the X dimension
unsigned m_dx;
//! Grid size along the Y dimension
unsigned m_dy;
//! Grid size along the Z dimension
unsigned m_dz;
//! Shift for cell access acceleration (Y dimension)
unsigned m_rowSize;
//! Shift for cell access acceleration (Z dimension)
unsigned m_sliceSize;
//! First index of innerbound grid
unsigned m_indexShift;
//! Grid size
unsigned m_gridSize;
//! Grid used to process Fast Marching
Cell** m_theGrid;
//! Associated octree
DgmOctree* m_octree;
//! Equivalent octree subdivision level
unsigned char m_gridLevel;
//! Octree cell size at equivalent subdivision level
float m_cellSize;
//! Octree min fill indexes at 'm_gridLevel'
Tuple3i m_minFillIndexes;
//! Current number of neighbours (6 or 26)
unsigned m_numberOfNeighbours;
//! Neighbours coordinates shifts in grid
int m_neighboursIndexShift[CC_FM_MAX_NUMBER_OF_NEIGHBOURS];
//! Neighbours distance weight
float m_neighboursDistance[CC_FM_MAX_NUMBER_OF_NEIGHBOURS];
};
}
#endif //FAST_MARCHING_HEADER
|