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