Point Cloud Library (PCL)  1.9.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 #ifndef PCL_ML_FERNS_FERN
39 #define PCL_ML_FERNS_FERN
40 
41 #include <pcl/common/common.h>
42 
43 #include <istream>
44 #include <ostream>
45 
46 namespace pcl
47 {
48 
49  /** \brief Class representing a Fern. */
50  template <class FeatureType, class NodeType>
51  class PCL_EXPORTS Fern
52  {
53  public:
54 
55  /** \brief Constructor. */
56  Fern ()
57  : num_of_decisions_ (0)
58  , features_ (0)
59  , thresholds_ (0)
60  , nodes_ (1)
61  {}
62 
63  /** \brief Destructor. */
64  virtual
65  ~Fern () {}
66 
67  /** \brief Initializes the fern.
68  * \param num_of_decisions The number of decisions taken to access the nodes.
69  */
70  void
71  initialize (const size_t num_of_decisions)
72  {
73  num_of_decisions_ = num_of_decisions;
74  features_.resize (num_of_decisions_);
75  thresholds_.resize (num_of_decisions_, std::numeric_limits<float>::quiet_NaN ());
76  nodes_.resize (0x1 << num_of_decisions_);
77  }
78 
79  /** \brief Returns the number of nodes the Fern has. */
80  inline size_t
82  {
83  return 0x1U << num_of_decisions_;
84  }
85 
86  /** \brief Returns the number of features the Fern has. */
87  inline size_t
89  {
90  return num_of_decisions_;
91  }
92 
93  /** \brief Serializes the fern.
94  * \param[out] stream The destination for the serialization.
95  */
96  void
97  serialize (::std::ostream & stream) const
98  {
99  //const int tmp_value = static_cast<int> (num_of_decisions_);
100  //stream.write (reinterpret_cast<char*> (&tmp_value), sizeof (tmp_value));
101  stream.write (reinterpret_cast<const char*> (&num_of_decisions_), sizeof (num_of_decisions_));
102 
103  for (size_t feature_index = 0; feature_index < features_.size (); ++feature_index)
104  {
105  features_[feature_index].serialize (stream);
106  }
107 
108  for (size_t threshold_index = 0; threshold_index < thresholds_.size (); ++threshold_index)
109  {
110  stream.write (reinterpret_cast<const char*> (&(thresholds_[threshold_index])), sizeof (thresholds_[threshold_index]));
111  }
112 
113  for (size_t node_index = 0; node_index < nodes_.size (); ++node_index)
114  {
115  nodes_[node_index].serialize (stream);
116  }
117  }
118 
119  /** \brief Deserializes the fern.
120  * \param[in] stream The source for the deserialization.
121  */
122  void deserialize (::std::istream & stream)
123  {
124  stream.read (reinterpret_cast<char*> (&num_of_decisions_), sizeof (num_of_decisions_));
125 
126  features_.resize (num_of_decisions_);
127  thresholds_.resize (num_of_decisions_);
128  nodes_.resize (0x1 << num_of_decisions_);
129 
130  for (size_t feature_index = 0; feature_index < features_.size (); ++feature_index)
131  {
132  features_[feature_index].deserialize (stream);
133  }
134 
135  for (size_t threshold_index = 0; threshold_index < thresholds_.size (); ++threshold_index)
136  {
137  stream.read (reinterpret_cast<char*> (&(thresholds_[threshold_index])), sizeof (thresholds_[threshold_index]));
138  }
139 
140  for (size_t node_index = 0; node_index < nodes_.size (); ++node_index)
141  {
142  nodes_[node_index].deserialize (stream);
143  }
144  }
145 
146  /** \brief Access operator for nodes.
147  * \param node_index The index of the node to access.
148  */
149  inline NodeType &
150  operator[] (const size_t node_index)
151  {
152  return nodes_[node_index];
153  }
154 
155  /** \brief Access operator for nodes.
156  * \param node_index The index of the node to access.
157  */
158  inline const NodeType &
159  operator[] (const size_t node_index) const
160  {
161  return nodes_[node_index];
162  }
163 
164  /** \brief Access operator for features.
165  * \param feature_index The index of the feature to access.
166  */
167  inline FeatureType &
168  accessFeature (const size_t feature_index)
169  {
170  return features_[feature_index];
171  }
172 
173  /** \brief Access operator for features.
174  * \param feature_index The index of the feature to access.
175  */
176  inline const FeatureType &
177  accessFeature (const size_t feature_index) const
178  {
179  return features_[feature_index];
180  }
181 
182  /** \brief Access operator for thresholds.
183  * \param threshold_index The index of the threshold to access.
184  */
185  inline float &
186  accessThreshold (const size_t threshold_index)
187  {
188  return thresholds_[threshold_index];
189  }
190 
191  /** \brief Access operator for thresholds.
192  * \param threshold_index The index of the threshold to access.
193  */
194  inline const float &
195  accessThreshold (const size_t threshold_index) const
196  {
197  return thresholds_[threshold_index];
198  }
199 
200  private:
201  /** The number of decisions. */
202  size_t num_of_decisions_;
203  /** The list of Features used to make the decisions. */
204  std::vector<FeatureType> features_;
205  /** The list of thresholds used to make the decisions. */
206  std::vector<float> thresholds_;
207 
208  /** The list of Nodes accessed by the Fern. */
209  std::vector<NodeType> nodes_;
210  };
211 
212 
213 }
214 
215 #endif
size_t getNumOfFeatures()
Returns the number of features the Fern has.
Definition: fern.h:88
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
float & accessThreshold(const size_t threshold_index)
Access operator for thresholds.
Definition: fern.h:186
FeatureType & accessFeature(const size_t feature_index)
Access operator for features.
Definition: fern.h:168
void deserialize(::std::istream &stream)
Deserializes the fern.
Definition: fern.h:122
void serialize(::std::ostream &stream) const
Serializes the fern.
Definition: fern.h:97
size_t getNumOfNodes()
Returns the number of nodes the Fern has.
Definition: fern.h:81
Class representing a Fern.
Definition: fern.h:51
Fern()
Constructor.
Definition: fern.h:56
void initialize(const size_t num_of_decisions)
Initializes the fern.
Definition: fern.h:71
const FeatureType & accessFeature(const size_t feature_index) const
Access operator for features.
Definition: fern.h:177
virtual ~Fern()
Destructor.
Definition: fern.h:65
const float & accessThreshold(const size_t threshold_index) const
Access operator for thresholds.
Definition: fern.h:195