Point Cloud Library (PCL)  1.8.1
kmeans.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author : Christian Potthast
36  * Email : potthast@usc.edu
37  *
38  */
39 
40 #ifndef PCL_KMEANS_HPP_
41 #define PCL_KMEANS_HPP_
42 
43 #include <pcl/ml/kmeans.h>
44 
45 //#include <pcl/common/io.h>
46 
47 //#include <stdio.h>
48 //#include <stdlib.h>
49 //#include <time.h>
50 
51 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
52 template <typename PointT>
54  : cluster_field_name_ ("")
55 {
56 }
57 
58 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
59 template <typename PointT>
61 {
62 }
63 
64 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65 template <typename PointT> void
67 {
68 }
69 
70 template <typename PointT> void
71 pcl::Kmeans<PointT>::cluster (std::vector<PointIndices> &clusters)
72 {
73  if (!initCompute () ||
74  (input_ != 0 && input_->points.empty ()) ||
75  (indices_ != 0 && indices_->empty ()))
76  {
77  clusters.clear ();
78  return;
79  }
80 
82  std::vector<pcl::PCLPointField> fields;
83 
84  int user_index = -1;
85  // if no cluster field name is set, check for X Y Z
86  if (strcmp (cluster_field_name_.c_str (), "") == 0)
87  {
88  int x_index = -1;
89  int y_index = -1;
90  int z_index = -1;
91  x_index = pcl::getFieldIndex (point, "x", fields);
92  if (y_index != -1)
93  y_index = pcl::getFieldIndex (point, "y", fields);
94  if (z_index != -1)
95  z_index = pcl::getFieldIndex (point, "z", fields);
96 
97  if (x_index == -1 && y_index == -1 && z_index == -1)
98  {
99  PCL_ERROR ("Failed to find match for field 'x y z'\n" );
100  return;
101  }
102 
103  PCL_INFO ("Use X Y Z as input data\n");
104  // create input data
105 /*
106  for (size_t i = 0; i < input_->points.size (); i++)
107  {
108  DataPoint data (3);
109  data[0] = input_->points[i].data[0];
110 
111 
112 
113  }
114 */
115 
116  std::cout << "x index: " << x_index << std::endl;
117 
118  float x = 0.0;
119  memcpy (&x, &input_->points[0] + fields[x_index].offset, sizeof(float));
120 
121  std::cout << "xxx: " << x << std::endl;
122 
123 
124  //memcpy (&x, reinterpret_cast<float*> (&input_->points[0]) + x_index, sizeof (float));
125 
126 
127  //int rgba_index = 1;
128 
129  //pcl::RGB rgb;
130  //memcpy (&rgb, reinterpret_cast<const char*> (&input_->points[index_vector[i].cloud_point_index]) + rgba_index, sizeof (RGB));
131 
132 
133 
134  }
135  // if cluster field name is set, check if field name is valied
136  else
137  {
138  user_index = pcl::getFieldIndex (point, cluster_field_name_.c_str (), fields);
139 
140  if (user_index == -1)
141  {
142  PCL_ERROR ("Failed to find match for field '%s'\n", cluster_field_name_.c_str ());
143  return;
144  }
145  }
146 
147 
148 
149 
150 /*
151  int xyz_index = -1;
152  pcl::PointCloud <PointT> point;
153  xyz_index = pcl::getFieldIndex (point, "r", fields);
154 
155 
156  if (xyz_index == -1 && strcmp (cluster_field_name_.c_str (), "") == 0)
157  {
158  PCL_ERROR ("Failed to find match for field '%s'\n", cluster_field_name_.c_str ());
159  }
160 
161 
162  std::cout << "index: " << xyz_index << std::endl;
163 
164  std::string t = pcl::getFieldsList (point);
165  std::cout << "t: " << t << std::endl;
166 */
167 
168  //std::vector <pcl::PCLPointField> fields;
169  //pcl::getFieldIndex (*input_, "xyz", fields);
170 
171 
172  //std::cout << "field: " << fields[xyz_index].count << std::endl;
173 
174 
175 /*
176  for (size_t i = 0; i < fields[vfh_idx].count; ++i)
177  {
178 
179  //vfh.second[i] = point.points[0].histogram[i];
180 
181  }
182 */
183 
184 
185 
186  deinitCompute ();
187 }
188 
189 
190 
191 
192 #define PCL_INSTANTIATE_Kmeans(T) template class PCL_EXPORTS pcl::Kmeans<T>;
193 
194 #endif // PCL_KMEANS_HPP_
int getFieldIndex(const pcl::PCLPointCloud2 &cloud, const std::string &field_name)
Get the index of a specified field (i.e., dimension/channel)
Definition: io.h:59
K-means clustering.
Definition: kmeans.h:61
Kmeans(unsigned int num_points, unsigned int num_dimensions)
Empty constructor.
Definition: kmeans.hpp:53
~Kmeans()
This destructor destroys.
Definition: kmeans.hpp:60
PointCloud represents the base class in PCL for storing collections of 3D points.