Point Cloud Library (PCL)  1.10.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 #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  // if no cluster field name is set, check for X Y Z
77  if (strcmp(cluster_field_name_.c_str(), "") == 0) {
78  int x_index = -1;
79  int y_index = -1;
80  int z_index = -1;
81  x_index = pcl::getFieldIndex<PointT>("x", fields);
82  if (y_index != -1)
83  y_index = pcl::getFieldIndex<PointT>("y", fields);
84  if (z_index != -1)
85  z_index = pcl::getFieldIndex<PointT>("z", fields);
86 
87  if (x_index == -1 && y_index == -1 && z_index == -1) {
88  PCL_ERROR("Failed to find match for field 'x y z'\n");
89  return;
90  }
91 
92  PCL_INFO("Use X Y Z as input data\n");
93  // create input data
94  /*
95  for (std::size_t i = 0; i < input_->points.size (); i++)
96  {
97  DataPoint data (3);
98  data[0] = input_->points[i].data[0];
99 
100 
101 
102  }
103  */
104 
105  std::cout << "x index: " << x_index << std::endl;
106 
107  float x = 0.0;
108  memcpy(&x, &input_->points[0] + fields[x_index].offset, sizeof(float));
109 
110  std::cout << "xxx: " << x << std::endl;
111 
112  // memcpy (&x, reinterpret_cast<float*> (&input_->points[0]) + x_index, sizeof
113  // (float));
114 
115  // int rgba_index = 1;
116 
117  // pcl::RGB rgb;
118  // memcpy (&rgb, reinterpret_cast<const char*>
119  // (&input_->points[index_vector[i].cloud_point_index]) + rgba_index, sizeof (RGB));
120  }
121  // if cluster field name is set, check if field name is valid
122  else {
123  int user_index = pcl::getFieldIndex<PointT>(cluster_field_name_.c_str(), fields);
124 
125  if (user_index == -1) {
126  PCL_ERROR("Failed to find match for field '%s'\n", cluster_field_name_.c_str());
127  return;
128  }
129  }
130 
131  /*
132  int xyz_index = -1;
133  pcl::PointCloud <PointT> point;
134  xyz_index = pcl::getFieldIndex<PointT> ("r", fields);
135 
136 
137  if (xyz_index == -1 && strcmp (cluster_field_name_.c_str (), "") == 0)
138  {
139  PCL_ERROR ("Failed to find match for field '%s'\n", cluster_field_name_.c_str ());
140  }
141 
142 
143  std::cout << "index: " << xyz_index << std::endl;
144 
145  std::string t = pcl::getFieldsList (point);
146  std::cout << "t: " << t << std::endl;
147  */
148 
149  // std::vector <pcl::PCLPointField> fields;
150  // pcl::getFieldIndex (*input_, "xyz", fields);
151 
152  // std::cout << "field: " << fields[xyz_index].count << std::endl;
153 
154  /*
155  for (std::size_t i = 0; i < fields[vfh_idx].count; ++i)
156  {
157 
158  //vfh.second[i] = point.points[0].histogram[i];
159 
160  }
161  */
162 
163  deinitCompute();
164 }
165 
166 #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