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 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
//##########################################################################
//# #
//# 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) #
//# #
//##########################################################################
#include "ChamferDistanceTransform.h"
//system
#include <algorithm>
#include <cassert>
#include <cstring>
using namespace CCLib;
//! Forward mask shifts and weights (Chamfer 3-4-5)
const signed char ForwardNeighbours345[14*4] = {
-1,-1,-1, 5,
0,-1,-1, 4,
1,-1,-1, 5,
-1, 0,-1, 4,
0, 0,-1, 3,
1, 0,-1, 4,
-1, 1,-1, 5,
0, 1,-1, 4,
1, 1,-1, 5,
-1,-1, 0, 4,
0,-1, 0, 3,
1,-1, 0, 4,
-1, 0, 0, 3,
0, 0, 0, 0
};
//! Backward mask shifts and weights (Chamfer 3-4-5)
const signed char BackwardNeighbours345[14*4] = {
0, 0, 0, 0,
1, 0, 0, 3,
-1, 1, 0, 4,
0, 1, 0, 3,
1, 1, 0, 4,
-1,-1, 1, 5,
0,-1, 1, 4,
1,-1, 1, 5,
-1, 0, 1, 4,
0, 0, 1, 3,
1, 0, 1, 4,
-1, 1, 1, 5,
0, 1, 1, 4,
1, 1, 1, 5
};
//! Forward mask shifts and weights (Chamfer 1-1-1)
const signed char ForwardNeighbours111[14*4] = {
-1,-1,-1, 1,
0,-1,-1, 1,
1,-1,-1, 1,
-1, 0,-1, 1,
0, 0,-1, 1,
1, 0,-1, 1,
-1, 1,-1, 1,
0, 1,-1, 1,
1, 1,-1, 1,
-1,-1, 0, 1,
0,-1, 0, 1,
1,-1, 0, 1,
-1, 0, 0, 1,
0, 0, 0, 0
};
//! Backward masks shifts and weights (Chamfer 1-1-1)
const signed char BackwardNeighbours111[14*4] = {
0, 0, 0, 0,
1, 0, 0, 1,
-1, 1, 0, 1,
0, 1, 0, 1,
1, 1, 0, 1,
-1,-1, 1, 1,
0,-1, 1, 1,
1,-1, 1, 1,
-1, 0, 1, 1,
0, 0, 1, 1,
1, 0, 1, 1,
-1, 1, 1, 1,
0, 1, 1, 1,
1, 1, 1, 1
};
//ChamferDistanceTransform::GridElement ChamferDistanceTransform::propagateDistance( unsigned iStart,
// unsigned jStart,
// unsigned kStart,
// bool forward,
// const signed char neighbours[14][4],
// NormalizedProgress* normProgress/*=0*/)
//{
// assert(!m_grid.empty());
//
// GridElement* _grid = &(m_grid[pos2index(iStart, jStart, kStart)]);
//
// //accelerating structure
// int neighborShift[14];
// {
// for (unsigned char v=0; v<14; ++v)
// {
// neighborShift[v] = static_cast<int>(neighbours[v][0]) +
// static_cast<int>(neighbours[v][1]) * static_cast<int>(m_rowSize) +
// static_cast<int>(neighbours[v][2]) * static_cast<int>(m_sliceSize);
// }
// }
//
// GridElement maxDist = 0;
//
// int order = forward ? 1 : -1;
//
// for (unsigned k=0; k<m_innerSize.z; ++k)
// {
// for (unsigned j=0; j<m_innerSize.y; ++j)
// {
// for (unsigned i=0; i<m_innerSize.x; ++i)
// {
// GridElement minVal = _grid[neighborShift[0]] + static_cast<GridElement>(neighbours[0][3]);
//
// for (unsigned char v=1; v<14; ++v)
// {
// GridElement neighborVal = _grid[neighborShift[v]] + static_cast<GridElement>(neighbours[v][3]);
// minVal = std::min(minVal, neighborVal);
// }
//
// *_grid = minVal;
//
// //we track the max distance
// if (minVal > maxDist)
// {
// maxDist = minVal;
// }
//
// _grid += order;
// }
// _grid += order*2; //next line
//
// if (normProgress && !normProgress->oneStep())
// {
// break;
// }
// }
//
// _grid += (order*2) * static_cast<int>(m_rowSize); //next slice
// }
//
// return maxDist;
//}
int ChamferDistanceTransform::propagateDistance(CC_CHAMFER_DISTANCE_TYPE type, GenericProgressCallback* progressCb)
{
if (m_grid.empty())
{
assert(false);
return -1;
}
const signed char* fwNeighbours = nullptr;
const signed char* bwNeighbours = nullptr;
switch (type)
{
case CHAMFER_111:
{
fwNeighbours = ForwardNeighbours111;
bwNeighbours = BackwardNeighbours111;
}
break;
case CHAMFER_345:
{
fwNeighbours = ForwardNeighbours345;
bwNeighbours = BackwardNeighbours345;
}
break;
default:
//unhandled type?!
assert(false);
return -1;
}
NormalizedProgress normProgress(progressCb,m_innerSize.y*m_innerSize.z*2);
if (progressCb)
{
if (progressCb->textCanBeEdited())
{
progressCb->setMethodTitle("Chamfer distance");
char buffer[256];
sprintf(buffer, "Box: [%u x %u x %u]", m_innerSize.x, m_innerSize.y, m_innerSize.z);
progressCb->setInfo(buffer);
}
progressCb->update(0);
progressCb->start();
}
//1st pass: forward scan
{
GridElement* _grid = &(m_grid[pos2index(0, 0, 0)]);
//accelerating structure
int neighborShift[14];
{
for (unsigned char v=0; v<14; ++v)
{
const signed char* fwNeighbour = fwNeighbours + 4*v;
neighborShift[v] = static_cast<int>(fwNeighbour[0]) +
static_cast<int>(fwNeighbour[1]) * static_cast<int>(m_rowSize) +
static_cast<int>(fwNeighbour[2]) * static_cast<int>(m_sliceSize);
}
}
for (unsigned k=0; k<m_innerSize.z; ++k)
{
for (unsigned j=0; j<m_innerSize.y; ++j)
{
for (unsigned i=0; i<m_innerSize.x; ++i)
{
GridElement minVal = _grid[neighborShift[0]] + static_cast<GridElement>(fwNeighbours[3]);
for (unsigned char v=1; v<14; ++v)
{
const signed char* fwNeighbour = fwNeighbours + 4*v;
GridElement neighborVal = _grid[neighborShift[v]] + static_cast<GridElement>(fwNeighbour[3]);
minVal = std::min(minVal, neighborVal);
}
*_grid++ = minVal;
}
_grid += 2; //next line
if (progressCb && !normProgress.oneStep())
{
break;
}
}
_grid += 2*m_rowSize; //next slice
}
}
//2nd pass: backward scan
GridElement maxDist = 0;
{
//accelerating structure
int neighborShift[14];
{
for (unsigned char v=0; v<14; ++v)
{
const signed char* bwNeighbour = bwNeighbours + 4*v;
neighborShift[v] = static_cast<int>(bwNeighbour[0]) +
static_cast<int>(bwNeighbour[1]) * static_cast<int>(m_rowSize) +
static_cast<int>(bwNeighbour[2]) * static_cast<int>(m_sliceSize);
}
}
GridElement* _grid = &(m_grid[pos2index(static_cast<int>(m_innerSize.x) - 1, static_cast<int>(m_innerSize.y) - 1, static_cast<int>(m_innerSize.z) - 1)]);
for (unsigned k = 0; k < m_innerSize.z; ++k)
{
for (unsigned j = 0; j < m_innerSize.y; ++j)
{
for (unsigned i = 0; i < m_innerSize.x; ++i)
{
GridElement minVal = _grid[neighborShift[0]] + static_cast<GridElement>(bwNeighbours[3]);
for (unsigned char v = 1; v < 14; ++v)
{
const signed char* bwNeighbour = bwNeighbours + 4 * v;
GridElement neighborVal = _grid[neighborShift[v]] + static_cast<GridElement>(bwNeighbour[3]);
minVal = std::min(minVal, neighborVal);
}
*_grid-- = minVal;
//we track the max distance
if (minVal > maxDist)
{
maxDist = minVal;
}
}
_grid -= 2; //next line
if (progressCb && !normProgress.oneStep())
{
break;
}
}
_grid -= 2 * m_rowSize; //next slice
}
}
return static_cast<int>(maxDist);
}
|