ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvDepthBuffer.cpp
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 #include "ecvDepthBuffer.h"
9 
10 // algorithm
11 #include <string.h>
12 
13 #include <vector>
14 
16  : deltaPhi(0), deltaTheta(0), width(0), height(0) {}
17 
19  zBuff.resize(0);
20  width = height = 0;
21  deltaPhi = deltaTheta = 0;
22 }
23 
25 
27  if (zBuff.empty()) {
28  // z-buffer not initialized!
29  return -1;
30  }
31 
32  // new temp buffer
33  int dx = width + 2;
34  int dy = height + 2;
35  unsigned tempZBuffSize = dx * dy;
36  std::vector<PointCoordinateType> zBuffTemp;
37  try {
38  zBuffTemp.resize(tempZBuffSize);
39  } catch (const std::bad_alloc&) {
40  // not enough memory
41  return -2;
42  }
43 
44  // copy old zBuffer in temp one (with 1 pixel border)
45  {
46  PointCoordinateType* _zBuffTemp =
47  zBuffTemp.data() + (dx + 1); // 2nd line, 2nd column
48  const PointCoordinateType* _zBuff =
49  zBuff.data(); // first line, first column of the true buffer
50  for (unsigned y = 0; y < height; ++y) {
51  memcpy(_zBuffTemp, _zBuff, width * sizeof(PointCoordinateType));
52  _zBuffTemp += dx;
53  _zBuff += width;
54  }
55  }
56 
57  // fill holes with their neighbor's mean value
58  {
59  for (unsigned y = 0; y < height; ++y) {
60  const PointCoordinateType* zu = zBuffTemp.data() + y * dx;
61  const PointCoordinateType* z = zu + dx;
62  const PointCoordinateType* zd = z + dx;
63  for (unsigned x = 0; x < width; ++x, ++zu, ++z, ++zd) {
64  if (z[1] == 0) // hole
65  {
66  unsigned char nsup = 0; // non empty holes
67  // upper line
68  nsup += (zu[0] > 0);
69  nsup += (zu[1] > 0);
70  nsup += (zu[2] > 0);
71  // current line
72  nsup += (z[0] > 0);
73  nsup += (z[2] > 0);
74  // next line
75  nsup += (zd[0] > 0);
76  nsup += (zd[1] > 0);
77  nsup += (zd[2] > 0);
78 
79  if (nsup > 3) {
80  zBuff[x + y * width] = (zu[0] + zu[1] + zu[2] + z[0] +
81  z[2] + zd[0] + zd[1] + zd[2]) /
82  nsup;
83  }
84  }
85  }
86  }
87  }
88 
89  return 0;
90 }
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
int width
int height
ccDepthBuffer()
Default constructor.
unsigned height
Buffer height.
PointCoordinateType deltaTheta
Yaw step (may differ from the sensor's)
std::vector< PointCoordinateType > zBuff
Z-Buffer grid.
void clear()
Clears the buffer.
PointCoordinateType deltaPhi
Pitch step (may differ from the sensor's)
unsigned width
Buffer width.
virtual ~ccDepthBuffer()
Destructor.
normal_z y
normal_z x
normal_z z