File size: 8,380 Bytes
0c51b93 |
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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2013-2016 NVIDIA Corporation. All rights reserved.
#include "sdf.h"
#include <vector>
#include <float.h>
#include <math.h>
using namespace std;
namespace
{
inline float Sqr(float x) { return x*x; }
inline int Clamp(int x, int lower, int upper) { return min(max(lower, x), upper); }
uint32_t Sample(const uint32_t* image, uint32_t w, uint32_t h, int x, int y)
{
return image[Clamp(y, 0, h-1)*w + Clamp(x, 0, w-1)];
}
uint32_t Sample(const uint32_t* image, uint32_t w, uint32_t h, uint32_t d, int x, int y, int z)
{
return image[Clamp(z, 0, d-1)*w*h + Clamp(y, 0, h-1)*w + Clamp(x, 0, w-1)];
}
// returns true if point is on the surface
bool EdgeDetect(const uint32_t* img, uint32_t w, uint32_t h, int x, int y)
{
bool center = Sample(img, w, h, x, y) != 0;
for (int j=y-1; j <= y+1; ++j)
{
for (int i=x-1; i <= x+1; ++i)
{
if ((0 != Sample(img, w, h, i, j)) != center)
{
return true;
}
}
}
return false;
}
// returns true if point is on the surface
bool EdgeDetect(const uint32_t* img, uint32_t w, uint32_t h, uint32_t d, int x, int y, int z, float& dist)
{
bool center = Sample(img, w, h, d, x, y, z) != 0;
float minDist = FLT_MAX;
for (int k=z-1; k <= z+1; ++k)
{
for (int j=y-1; j <= y+1; ++j)
{
for (int i=x-1; i <= x+1; ++i)
{
if ((0 != Sample(img, w, h, d, i, j, k)) != center)
{
int dx = x-i;
int dy = y-j;
int dz = z-k;
minDist = min(sqrtf(float(dx*dx + dy*dy + dz*dz))*0.5f, minDist);
}
}
}
}
dist = minDist;
return minDist != FLT_MAX;
}
}
// 2D fast marching method (FMM J. Sethian. A fast marching level set method for monotonically advancing fronts. Proc. Natl. Acad. Sci., 93:1591�1595, 1996.)
namespace
{
struct Coord2D
{
int i, j;
float d;
int si, sj;
bool operator < (const Coord2D& c) const { return d > c.d; }
};
}
void MakeSDF(const uint32_t* img, uint32_t w, uint32_t h, float* output)
{
const float scale = 1.0f / max(w, h);
std::vector<Coord2D> queue;
// find surface points
for (uint32_t y=0; y < h; ++y)
{
for (uint32_t x=0; x < w; ++x)
{
if (EdgeDetect(img, w, h, x, y))
{
Coord2D c = {(int)x, (int)y, 0.0f, (int)x, (int)y};
queue.push_back(c);
}
output[y*w + x] = FLT_MAX;
}
}
std::make_heap(queue.begin(), queue.end());
while (!queue.empty())
{
std::pop_heap(queue.begin(), queue.end());
Coord2D c = queue.back();
queue.pop_back();
// freeze coord if not already frozen
if (output[c.j*w + c.i] == FLT_MAX)
{
output[c.j*w + c.i] = c.d;
// update neighbours
int xmin = max(c.i-1, 0), xmax = min(c.i+1, int(w-1));
int ymin = max(c.j-1, 0), ymax = min(c.j+1, int(h-1));
for (int y=ymin; y <= ymax; ++y)
{
for (int x=xmin; x <= xmax; ++x)
{
if (c.i != x || c.j != y)
{
int dx = x-c.si;
int dy = y-c.sj;
// calculate distance to source coord
float d = sqrtf(float(dx*dx + dy*dy));
Coord2D newc = {x, y, d, c.si, c.sj};
queue.push_back(newc);
std::push_heap(queue.begin(), queue.end());
}
}
}
}
}
for (uint32_t y=0; y < h; ++y)
{
for (uint32_t x=0; x < w; ++x)
{
assert(output[y*w + x] < FLT_MAX);
// flip sign for interior
output[y*w + x] *= (img[y*w + x]?-1.0f:1.0f)*scale;
}
}
}
// 3D fast marching method (FMM J. Sethian. A fast marching level set method for monotonically advancing fronts. Proc. Natl. Acad. Sci., 93:1591�1595, 1996.)
namespace
{
struct Coord3D
{
int i, j, k;
float d;
int si, sj, sk;
bool operator < (const Coord3D& c) const { return d > c.d; }
};
}
void MakeSDF(const uint32_t* img, uint32_t w, uint32_t h, uint32_t d, float* output)
{
const float scale = 1.0f / max(max(w, h), d);
std::vector<Coord3D> queue;
// find surface points
for (uint32_t z=0; z < d; ++z)
{
for (uint32_t y=0; y < h; ++y)
{
for (uint32_t x=0; x < w; ++x)
{
float dist;
if (EdgeDetect(img, w, h, d, x, y, z, dist))
{
Coord3D c = {(int)x, (int)y, (int)z, dist, (int)x, (int)y, (int)z};
queue.push_back(c);
}
output[z*w*h + y*w + x] = FLT_MAX;
}
}
}
// no occupied voxels so quit
if (queue.empty())
return;
std::make_heap(queue.begin(), queue.end());
while (!queue.empty())
{
std::pop_heap(queue.begin(), queue.end());
Coord3D c = queue.back();
queue.pop_back();
// freeze coord if not already frozen
if (output[c.k*w*h + c.j*w + c.i] == FLT_MAX)
{
output[c.k*w*h + c.j*w + c.i] = c.d;
// update neighbours
int xmin = max(c.i-1, 0), xmax = min(c.i+1, int(w-1));
int ymin = max(c.j-1, 0), ymax = min(c.j+1, int(h-1));
int zmin = max(c.k-1, 0), zmax = min(c.k+1, int(d-1));
for (int z=zmin; z <= zmax; ++z)
{
for (int y=ymin; y <= ymax; ++y)
{
for (int x=xmin; x <= xmax; ++x)
{
if ((c.i != x || c.j != y || c.k != z) && output[z*w*h + y*w + x] == FLT_MAX)
{
int dx = x-c.si;
int dy = y-c.sj;
int dz = z-c.sk;
// calculate distance to source coord
float d = sqrtf(float(dx*dx + dy*dy + dz*dz)) + output[c.sk*w*h + c.sj*w + c.si];
assert(d > 0.0f);
Coord3D newc = {x, y, z, d, c.si, c.sj, c.sk};
queue.push_back(newc);
std::push_heap(queue.begin(), queue.end());
}
}
}
}
}
}
for (uint32_t z=0; z < d; ++z)
{
for (uint32_t y=0; y < h; ++y)
{
for (uint32_t x=0; x < w; ++x)
{
assert(output[z*w*h + y*w + x] < FLT_MAX);
// flip sign for interior
output[z*w*h + y*w + x] *= (img[z*w*h + y*w + x]?-1.0f:1.0f)*scale;
}
}
}
}
/*
// Brute-force 2D SDF generation
void FindNeighbour(const uint32_t* image, uint32_t w, uint32_t h, uint32_t cx, uint32_t cy, uint32_t& i, uint32_t& j, float& d)
{
float minDistSq=FLT_MAX;
float fx = float(cx);
float fy = float(cy);
for (uint32_t y=0; y < h; ++y)
{
for (uint32_t x=0; x < w; ++x)
{
if ((x != cx || y != cy) && image[y*w + x])
{
float dSq = Sqr(fx-float(x)) + Sqr(fy-float(y));
if (dSq < minDistSq)
{
minDistSq = dSq;
i = x;
j = y;
}
}
}
}
d = sqrtf(minDistSq);
}
// brute force
void MakeSDF(const uint32_t* img, uint32_t w, uint32_t h, float* output)
{
// find surface points
vector<uint32_t> surface(w*h);
for (uint32_t y=0; y < h; ++y)
{
for (uint32_t x=0; x < w; ++x)
{
if (EdgeDetect(img, w, h, x, y))
{
surface[y*w + x] = 1;
}
}
}
// brute force search
for (uint32_t y=0; y < h; ++y)
{
for (uint32_t x=0; x < w; ++x)
{
uint32_t i, j;
float d;
FindNeighbour(&surface[0], w, h, x, y, i, j, d);
// flip sign for pixels inside the shape
float sign = (img[y*w + x])?-1.0f:1.0f;
output[y*w + x] = d*sign/w;
}
}
}
*/
|