Point Cloud Library (PCL)  1.11.1-dev
fern.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/common/common.h>
41 
42 #include <istream>
43 #include <ostream>
44 
45 namespace pcl {
46 
47 /** Class representing a Fern. */
48 template <class FeatureType, class NodeType>
50 public:
51  /** Constructor. */
52  Fern() : num_of_decisions_(0), features_(0), thresholds_(0), nodes_(1) {}
53 
54  /** Destructor. */
55  virtual ~Fern() {}
56 
57  /** Initializes the fern.
58  *
59  * \param num_of_decisions the number of decisions taken to access the nodes
60  */
61  void
62  initialize(const std::size_t num_of_decisions)
63  {
64  num_of_decisions_ = num_of_decisions;
65  features_.resize(num_of_decisions_);
66  thresholds_.resize(num_of_decisions_, std::numeric_limits<float>::quiet_NaN());
67  nodes_.resize(0x1 << num_of_decisions_);
68  }
69 
70  /** Returns the number of nodes the Fern has. */
71  inline std::size_t
73  {
74  return 0x1U << num_of_decisions_;
75  }
76 
77  /** Returns the number of features the Fern has. */
78  inline std::size_t
80  {
81  return num_of_decisions_;
82  }
83 
84  /** Serializes the fern.
85  *
86  * \param[out] stream the destination for the serialization
87  */
88  void
89  serialize(::std::ostream& stream) const
90  {
91  // const int tmp_value = static_cast<int> (num_of_decisions_);
92  // stream.write (reinterpret_cast<char*> (&tmp_value), sizeof (tmp_value));
93  stream.write(reinterpret_cast<const char*>(&num_of_decisions_),
94  sizeof(num_of_decisions_));
95 
96  for (std::size_t feature_index = 0; feature_index < features_.size();
97  ++feature_index) {
98  features_[feature_index].serialize(stream);
99  }
100 
101  for (std::size_t threshold_index = 0; threshold_index < thresholds_.size();
102  ++threshold_index) {
103  stream.write(reinterpret_cast<const char*>(&(thresholds_[threshold_index])),
104  sizeof(thresholds_[threshold_index]));
105  }
106 
107  for (std::size_t node_index = 0; node_index < nodes_.size(); ++node_index) {
108  nodes_[node_index].serialize(stream);
109  }
110  }
111 
112  /** Deserializes the fern.
113  *
114  * \param[in] stream the source for the deserialization
115  */
116  void
117  deserialize(::std::istream& stream)
118  {
119  stream.read(reinterpret_cast<char*>(&num_of_decisions_), sizeof(num_of_decisions_));
120 
121  features_.resize(num_of_decisions_);
122  thresholds_.resize(num_of_decisions_);
123  nodes_.resize(0x1 << num_of_decisions_);
124 
125  for (std::size_t feature_index = 0; feature_index < features_.size();
126  ++feature_index) {
127  features_[feature_index].deserialize(stream);
128  }
129 
130  for (std::size_t threshold_index = 0; threshold_index < thresholds_.size();
131  ++threshold_index) {
132  stream.read(reinterpret_cast<char*>(&(thresholds_[threshold_index])),
133  sizeof(thresholds_[threshold_index]));
134  }
135 
136  for (std::size_t node_index = 0; node_index < nodes_.size(); ++node_index) {
137  nodes_[node_index].deserialize(stream);
138  }
139  }
140 
141  /** Access operator for nodes.
142  *
143  * \param node_index the index of the node to access
144  */
145  inline NodeType&
146  operator[](const std::size_t node_index)
147  {
148  return nodes_[node_index];
149  }
150 
151  /** Access operator for nodes.
152  *
153  * \param node_index the index of the node to access
154  */
155  inline const NodeType&
156  operator[](const std::size_t node_index) const
157  {
158  return nodes_[node_index];
159  }
160 
161  /** Access operator for features.
162  *
163  * \param feature_index the index of the feature to access
164  */
165  inline FeatureType&
166  accessFeature(const std::size_t feature_index)
167  {
168  return features_[feature_index];
169  }
170 
171  /** Access operator for features.
172  *
173  * \param feature_index the index of the feature to access
174  */
175  inline const FeatureType&
176  accessFeature(const std::size_t feature_index) const
177  {
178  return features_[feature_index];
179  }
180 
181  /** Access operator for thresholds.
182  *
183  * \param threshold_index the index of the threshold to access
184  */
185  inline float&
186  accessThreshold(const std::size_t threshold_index)
187  {
188  return thresholds_[threshold_index];
189  }
190 
191  /** Access operator for thresholds.
192  *
193  * \param threshold_index the index of the threshold to access
194  */
195  inline const float&
196  accessThreshold(const std::size_t threshold_index) const
197  {
198  return thresholds_[threshold_index];
199  }
200 
201 private:
202  /** The number of decisions. */
203  std::size_t num_of_decisions_;
204  /** The list of Features used to make the decisions. */
205  std::vector<FeatureType> features_;
206  /** The list of thresholds used to make the decisions. */
207  std::vector<float> thresholds_;
208  /** The list of Nodes accessed by the Fern. */
209  std::vector<NodeType> nodes_;
210 };
211 
212 } // namespace pcl
pcl
Definition: convolution.h:46
common.h
pcl::Fern::accessThreshold
float & accessThreshold(const std::size_t threshold_index)
Access operator for thresholds.
Definition: fern.h:186
pcl::Fern::~Fern
virtual ~Fern()
Destructor.
Definition: fern.h:55
pcl::Fern::operator[]
NodeType & operator[](const std::size_t node_index)
Access operator for nodes.
Definition: fern.h:146
pcl::Fern::getNumOfFeatures
std::size_t getNumOfFeatures()
Returns the number of features the Fern has.
Definition: fern.h:79
pcl::Fern::initialize
void initialize(const std::size_t num_of_decisions)
Initializes the fern.
Definition: fern.h:62
pcl::Fern::operator[]
const NodeType & operator[](const std::size_t node_index) const
Access operator for nodes.
Definition: fern.h:156
pcl::Fern
Class representing a Fern.
Definition: fern.h:49
pcl::Fern::deserialize
void deserialize(::std::istream &stream)
Deserializes the fern.
Definition: fern.h:117
pcl::Fern::accessThreshold
const float & accessThreshold(const std::size_t threshold_index) const
Access operator for thresholds.
Definition: fern.h:196
pcl::Fern::accessFeature
const FeatureType & accessFeature(const std::size_t feature_index) const
Access operator for features.
Definition: fern.h:176
pcl::Fern::accessFeature
FeatureType & accessFeature(const std::size_t feature_index)
Access operator for features.
Definition: fern.h:166
pcl::Fern::serialize
void serialize(::std::ostream &stream) const
Serializes the fern.
Definition: fern.h:89
pcl::Fern::getNumOfNodes
std::size_t getNumOfNodes()
Returns the number of nodes the Fern has.
Definition: fern.h:72
pcl::Fern::Fern
Fern()
Constructor.
Definition: fern.h:52
PCL_EXPORTS
#define PCL_EXPORTS
Definition: pcl_macros.h:323