Point Cloud Library (PCL)  1.10.0-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& operator[](const std::size_t node_index)
146  {
147  return nodes_[node_index];
148  }
149 
150  /** Access operator for nodes.
151  *
152  * \param node_index the index of the node to access
153  */
154  inline const NodeType& operator[](const std::size_t node_index) const
155  {
156  return nodes_[node_index];
157  }
158 
159  /** Access operator for features.
160  *
161  * \param feature_index the index of the feature to access
162  */
163  inline FeatureType&
164  accessFeature(const std::size_t feature_index)
165  {
166  return features_[feature_index];
167  }
168 
169  /** Access operator for features.
170  *
171  * \param feature_index the index of the feature to access
172  */
173  inline const FeatureType&
174  accessFeature(const std::size_t feature_index) const
175  {
176  return features_[feature_index];
177  }
178 
179  /** Access operator for thresholds.
180  *
181  * \param threshold_index the index of the threshold to access
182  */
183  inline float&
184  accessThreshold(const std::size_t threshold_index)
185  {
186  return thresholds_[threshold_index];
187  }
188 
189  /** Access operator for thresholds.
190  *
191  * \param threshold_index the index of the threshold to access
192  */
193  inline const float&
194  accessThreshold(const std::size_t threshold_index) const
195  {
196  return thresholds_[threshold_index];
197  }
198 
199 private:
200  /** The number of decisions. */
201  std::size_t num_of_decisions_;
202  /** The list of Features used to make the decisions. */
203  std::vector<FeatureType> features_;
204  /** The list of thresholds used to make the decisions. */
205  std::vector<float> thresholds_;
206  /** The list of Nodes accessed by the Fern. */
207  std::vector<NodeType> nodes_;
208 };
209 
210 } // namespace pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
void initialize(const std::size_t num_of_decisions)
Initializes the fern.
Definition: fern.h:62
const NodeType & operator[](const std::size_t node_index) const
Access operator for nodes.
Definition: fern.h:154
FeatureType & accessFeature(const std::size_t feature_index)
Access operator for features.
Definition: fern.h:164
Define standard C methods and C++ classes that are common to all methods.
std::size_t getNumOfNodes()
Returns the number of nodes the Fern has.
Definition: fern.h:72
void deserialize(::std::istream &stream)
Deserializes the fern.
Definition: fern.h:117
NodeType & operator[](const std::size_t node_index)
Access operator for nodes.
Definition: fern.h:145
const float & accessThreshold(const std::size_t threshold_index) const
Access operator for thresholds.
Definition: fern.h:194
Class representing a Fern.
Definition: fern.h:49
void serialize(::std::ostream &stream) const
Serializes the fern.
Definition: fern.h:89
Fern()
Constructor.
Definition: fern.h:52
float & accessThreshold(const std::size_t threshold_index)
Access operator for thresholds.
Definition: fern.h:184
std::size_t getNumOfFeatures()
Returns the number of features the Fern has.
Definition: fern.h:79
#define PCL_EXPORTS
Definition: pcl_macros.h:253
virtual ~Fern()
Destructor.
Definition: fern.h:55
const FeatureType & accessFeature(const std::size_t feature_index) const
Access operator for features.
Definition: fern.h:174