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