dt.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2006 Pedro Felzenszwalb
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 /* distance transform */
20 
21 #pragma once
22 
23 #include <algorithm>
24 
25 #include "image.h"
26 #include "imconv.h"
27 
29 {
30  const double INF = 1E20;
31 
32  /* dt of 1d function using squared distance */
33  static float *dt(float *f, int n) {
34  float *d = new float[n];
35  int *v = new int[n];
36  float *z = new float[n+1];
37  int k = 0;
38  v[0] = 0;
39  z[0] = -INF;
40  z[1] = +INF;
41  for (int q = 1; q <= n-1; q++) {
42  float s = ((f[q]+square(q))-(f[v[k]]+square(v[k])))/(2*q-2*v[k]);
43  while (s <= z[k]) {
44  k--;
45  s = ((f[q]+square(q))-(f[v[k]]+square(v[k])))/(2*q-2*v[k]);
46  }
47  k++;
48  v[k] = q;
49  z[k] = s;
50  z[k+1] = +INF;
51  }
52 
53  k = 0;
54  for (int q = 0; q <= n-1; q++) {
55  while (z[k+1] < q)
56  k++;
57  d[q] = square(q-v[k]) + f[v[k]];
58  }
59 
60  delete [] v;
61  delete [] z;
62  return d;
63  }
64 
65  /* dt of 2d function using squared distance */
66  static void dt(image<float> *im) {
67  int width = im->width();
68  int height = im->height();
69  float *f = new float[std::max(width,height)];
70 
71  // transform along columns
72  for (int x = 0; x < width; x++) {
73  for (int y = 0; y < height; y++) {
74  f[y] = imRef(im, x, y);
75  }
76  float *d = dt(f, height);
77  for (int y = 0; y < height; y++) {
78  imRef(im, x, y) = d[y];
79  }
80  delete [] d;
81  }
82 
83  // transform along rows
84  for (int y = 0; y < height; y++) {
85  for (int x = 0; x < width; x++) {
86  f[x] = imRef(im, x, y);
87  }
88  float *d = dt(f, width);
89  for (int x = 0; x < width; x++) {
90  imRef(im, x, y) = d[x];
91  }
92  delete [] d;
93  }
94 
95  delete [] f;
96  }
97 
98  /* dt of binary image using squared distance */
99  static image<float> *dt(image<uchar> *im, uchar on = 1) {
100  int width = im->width();
101  int height = im->height();
102 
103  image<float> *out = new image<float>(width, height, false);
104  for (int y = 0; y < height; y++) {
105  for (int x = 0; x < width; x++) {
106  if (imRef(im, x, y) == on)
107  imRef(out, x, y) = 0;
108  else
109  imRef(out, x, y) = INF;
110  }
111  }
112 
113  dt(out);
114  return out;
115  }
116 
117 } // namespace
d
f
int width() const
Definition: image.h:44
XmlRpcServer s
TFSIMD_FORCE_INLINE const tfScalar & y() const
static float * dt(float *f, int n)
Definition: dt.h:33
#define imRef(im, x, y)
Definition: image.h:60
int height() const
Definition: image.h:47
TFSIMD_FORCE_INLINE const tfScalar & x() const
T square(const T &x)
Definition: misc.h:42
unsigned char uchar
Definition: misc.h:27
TFSIMD_FORCE_INLINE const tfScalar & z() const
const double INF
Definition: dt.h:30


grid_map_sdf
Author(s): Takahiro Miki , Péter Fankhauser
autogenerated on Tue Jun 1 2021 02:13:49