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