Point Cloud Library (PCL)  1.14.1-dev
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 #pragma once
41 
42 #include <pcl/ml/kmeans.h>
43 
44 //#include <pcl/common/io.h>
45 
46 //#include <stdio.h>
47 //#include <stdlib.h>
48 //#include <time.h>
49 
50 namespace pcl {
51 template <typename PointT>
52 Kmeans<PointT>::Kmeans() : cluster_field_name_("")
53 {}
54 
55 template <typename PointT>
56 void
57 Kmeans<PointT>::cluster(std::vector<PointIndices>& clusters)
58 {
59  if (!initCompute() || (input_ != 0 && input_->points.empty()) ||
60  (indices_ != 0 && indices_->empty())) {
61  clusters.clear();
62  return;
63  }
64 
66  std::vector<pcl::PCLPointField> fields;
67 
68  // if no cluster field name is set, check for X Y Z
69  if (strcmp(cluster_field_name_.c_str(), "") == 0) {
70  int x_index = -1;
71  int y_index = -1;
72  int z_index = -1;
73  x_index = pcl::getFieldIndex<PointT>("x", fields);
74  if (y_index != -1)
75  y_index = pcl::getFieldIndex<PointT>("y", fields);
76  if (z_index != -1)
77  z_index = pcl::getFieldIndex<PointT>("z", fields);
78 
79  if (x_index == -1 && y_index == -1 && z_index == -1) {
80  PCL_ERROR("Failed to find match for field 'x y z'\n");
81  return;
82  }
83 
84  PCL_INFO("Use X Y Z as input data\n");
85  // create input data
86  /*
87  for (std::size_t i = 0; i < input_->size (); i++)
88  {
89  DataPoint data (3);
90  data[0] = (*input_)[i].data[0];
91 
92 
93 
94  }
95  */
96 
97  std::cout << "x index: " << x_index << std::endl;
98 
99  float x = 0.0;
100  memcpy(&x, &(*input_)[0] + fields[x_index].offset, sizeof(float));
101 
102  std::cout << "xxx: " << x << std::endl;
103 
104  // memcpy (&x, reinterpret_cast<float*> (&(*input_)[0]) + x_index, sizeof
105  // (float));
106 
107  // int rgba_index = 1;
108 
109  // pcl::RGB rgb;
110  // memcpy (&rgb, reinterpret_cast<const char*>
111  // (&(*input_)[index_vector[i].cloud_point_index]) + rgba_index, sizeof (RGB));
112  }
113  // if cluster field name is set, check if field name is valid
114  else {
115  int user_index = pcl::getFieldIndex<PointT>(cluster_field_name_.c_str(), fields);
116 
117  if (user_index == -1) {
118  PCL_ERROR("Failed to find match for field '%s'\n", cluster_field_name_.c_str());
119  return;
120  }
121  }
122 
123  /*
124  int xyz_index = -1;
125  pcl::PointCloud <PointT> point;
126  xyz_index = pcl::getFieldIndex<PointT> ("r", fields);
127 
128 
129  if (xyz_index == -1 && strcmp (cluster_field_name_.c_str (), "") == 0)
130  {
131  PCL_ERROR ("Failed to find match for field '%s'\n", cluster_field_name_.c_str ());
132  }
133 
134 
135  std::cout << "index: " << xyz_index << std::endl;
136 
137  std::string t = pcl::getFieldsList (point);
138  std::cout << "t: " << t << std::endl;
139  */
140 
141  // std::vector <pcl::PCLPointField> fields;
142  // pcl::getFieldIndex (*input_, "xyz", fields);
143 
144  // std::cout << "field: " << fields[xyz_index].count << std::endl;
145 
146  /*
147  for (std::size_t i = 0; i < fields[vfh_idx].count; ++i)
148  {
149 
150  //vfh.second[i] = point[0].histogram[i];
151 
152  }
153  */
154 
155  deinitCompute();
156 }
157 } // namespace pcl
158 
159 #define PCL_INSTANTIATE_Kmeans(T) template class PCL_EXPORTS pcl::Kmeans<T>;
K-means clustering.
Definition: kmeans.h:55
Kmeans(unsigned int num_points, unsigned int num_dimensions)
Empty constructor.
Definition: kmeans.hpp:52
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173