Point Cloud Library (PCL)  1.9.1-dev
openni_shift_to_depth_conversion.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2011 Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #pragma once
40 
41 #include <pcl/pcl_config.h>
42 #ifdef HAVE_OPENNI
43 
44 #include <cstdint>
45 #include <vector>
46 #include <limits>
47 
48 namespace openni_wrapper
49 {
50  /** \brief This class provides conversion of the openni 11-bit shift data to depth;
51  */
52  class PCL_EXPORTS ShiftToDepthConverter
53  {
54  public:
55  /** \brief Constructor. */
56  ShiftToDepthConverter () : init_(false) {}
57 
58  /** \brief Destructor. */
59  virtual ~ShiftToDepthConverter () {};
60 
61  /** \brief This method generates a look-up table to convert openni shift values to depth
62  */
63  void
65  {
66  // lookup of 11 bit shift values
67  constexpr size_t table_size = 1 << 10;
68 
69  lookupTable_.clear();
70  lookupTable_.resize(table_size);
71 
72  // constants taken from openni driver
73  constexpr int16_t nConstShift = 800;
74  constexpr double nParamCoeff = 4.000000;
75  constexpr double dPlanePixelSize = 0.104200;
76  constexpr double nShiftScale = 10.000000;
77  constexpr double dPlaneDsr = 120.000000;
78  constexpr double dPlaneDcl = 7.500000;
79 
80  for (size_t i=0; i<table_size; ++i)
81  {
82  // shift to depth calculation from opnni
83  double dFixedRefX = (static_cast<double>(i - nConstShift) / nParamCoeff)-0.375;
84  double dMetric = dFixedRefX * dPlanePixelSize;
85  lookupTable_[i] = static_cast<float>((nShiftScale * ((dMetric * dPlaneDsr / (dPlaneDcl - dMetric)) + dPlaneDsr) ) / 1000.0f);
86  }
87 
88  init_ = true;
89  }
90 
91  /** \brief Generate a look-up table for converting openni shift values to depth
92  */
93  inline float
94  shiftToDepth (uint16_t shift_val)
95  {
96  assert (init_);
97 
98  constexpr float bad_point = std::numeric_limits<float>::quiet_NaN ();
99 
100  float ret = bad_point;
101 
102  // lookup depth value in shift lookup table
103  if (shift_val<lookupTable_.size())
104  ret = lookupTable_[shift_val];
105 
106  return ret;
107  }
108 
109  inline bool isInitialized() const
110  {
111  return init_;
112  }
113 
114  protected:
115  std::vector<float> lookupTable_;
116  bool init_;
117  } ;
118 }
119 
120 #endif
float shiftToDepth(uint16_t shift_val)
Generate a look-up table for converting openni shift values to depth.
void generateLookupTable()
This method generates a look-up table to convert openni shift values to depth.
This class provides conversion of the openni 11-bit shift data to depth;.