Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
histogram_visualizer.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: histogram_visualizer.hpp 6161 2012-07-05 17:37:29Z rusu $
37  *
38  */
39 
40 #ifndef PCL_PCL_HISTOGRAM_VISUALIZER_IMPL_H_
41 #define PCL_PCL_HISTOGRAM_VISUALIZER_IMPL_H_
42 
44 template <typename PointT> bool
46  const pcl::PointCloud<PointT> &cloud, int hsize,
47  const std::string &id, int win_width, int win_height)
48 {
49  RenWinInteractMap::iterator am_it = wins_.find (id);
50  if (am_it != wins_.end ())
51  {
52  PCL_WARN ("[addFeatureHistogram] A window with id <%s> already exists! Please choose a different id and retry.\n", id.c_str ());
53  return (false);
54  }
55 
56  vtkSmartPointer<vtkDoubleArray> xy_array = vtkSmartPointer<vtkDoubleArray>::New ();
57  xy_array->SetNumberOfComponents (2);
58  xy_array->SetNumberOfTuples (hsize);
59 
60  // Parse the cloud data and store it in the array
61  double xy[2];
62  for (int d = 0; d < hsize; ++d)
63  {
64  xy[0] = d;
65  xy[1] = cloud.points[0].histogram[d];
66  xy_array->SetTuple (d, xy);
67  }
68  RenWinInteract renwinint;
69  createActor (xy_array, renwinint, id, win_width, win_height);
70 
71  // Save the pointer/ID pair to the global window map
72  wins_[id] = renwinint;
73 #if ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION <= 4))
74  resetStoppedFlag ();
75 #endif
76  return (true);
77 }
78 
80 template <typename PointT> bool
82  const pcl::PointCloud<PointT> &cloud,
83  const std::string &field_name,
84  const int index,
85  const std::string &id, int win_width, int win_height)
86 {
87  if (index < 0 || index >= cloud.points.size ())
88  {
89  PCL_ERROR ("[addFeatureHistogram] Invalid point index (%d) given!\n", index);
90  return (false);
91  }
92 
93  // Get the fields present in this cloud
94  std::vector<sensor_msgs::PointField> fields;
95  // Check if our field exists
96  int field_idx = pcl::getFieldIndex<PointT> (cloud, field_name, fields);
97  if (field_idx == -1)
98  {
99  PCL_ERROR ("[addFeatureHistogram] The specified field <%s> does not exist!\n", field_name.c_str ());
100  return (false);
101  }
102 
103  RenWinInteractMap::iterator am_it = wins_.find (id);
104  if (am_it != wins_.end ())
105  {
106  PCL_WARN ("[addFeatureHistogram] A window with id <%s> already exists! Please choose a different id and retry.\n", id.c_str ());
107  return (false);
108  }
109 
110  vtkSmartPointer<vtkDoubleArray> xy_array = vtkSmartPointer<vtkDoubleArray>::New ();
111  xy_array->SetNumberOfComponents (2);
112  xy_array->SetNumberOfTuples (fields[field_idx].count);
113 
114  // Parse the cloud data and store it in the array
115  double xy[2];
116  for (int d = 0; d < fields[field_idx].count; ++d)
117  {
118  xy[0] = d;
119  //xy[1] = cloud.points[index].histogram[d];
120  float data;
121  memcpy (&data, reinterpret_cast<const char*> (&cloud.points[index]) + fields[field_idx].offset + d * sizeof (float), sizeof (float));
122  xy[1] = data;
123  xy_array->SetTuple (d, xy);
124  }
125  RenWinInteract renwinint;
126  createActor (xy_array, renwinint, id, win_width, win_height);
127 
128  // Save the pointer/ID pair to the global window map
129  wins_[id] = renwinint;
130 #if ((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION <= 4))
131  resetStoppedFlag ();
132 #endif
133  return (true);
134 }
135 
137 template <typename PointT> bool
139  const pcl::PointCloud<PointT> &cloud, int hsize,
140  const std::string &id)
141 {
142  RenWinInteractMap::iterator am_it = wins_.find (id);
143  if (am_it == wins_.end ())
144  {
145  PCL_WARN ("[updateFeatureHistogram] A window with id <%s> does not exists!.\n", id.c_str ());
146  return (false);
147  }
148  RenWinInteract* renwinupd = &wins_[id];
149 
150  vtkSmartPointer<vtkDoubleArray> xy_array = vtkSmartPointer<vtkDoubleArray>::New ();
151  xy_array->SetNumberOfComponents (2);
152  xy_array->SetNumberOfTuples (hsize);
153 
154  // Parse the cloud data and store it in the array
155  double xy[2];
156  for (int d = 0; d < hsize; ++d)
157  {
158  xy[0] = d;
159  xy[1] = cloud.points[0].histogram[d];
160  xy_array->SetTuple (d, xy);
161  }
162  reCreateActor (xy_array, renwinupd, hsize);
163  return (true);
164 }
165 
167 template <typename PointT> bool
169  const pcl::PointCloud<PointT> &cloud, const std::string &field_name, const int index,
170  const std::string &id)
171 {
172  if (index < 0 || index >= cloud.points.size ())
173  {
174  PCL_ERROR ("[updateFeatureHistogram] Invalid point index (%d) given!\n", index);
175  return (false);
176  }
177 
178  // Get the fields present in this cloud
179  std::vector<sensor_msgs::PointField> fields;
180  // Check if our field exists
181  int field_idx = pcl::getFieldIndex<PointT> (cloud, field_name, fields);
182  if (field_idx == -1)
183  {
184  PCL_ERROR ("[updateFeatureHistogram] The specified field <%s> does not exist!\n", field_name.c_str ());
185  return (false);
186  }
187 
188  RenWinInteractMap::iterator am_it = wins_.find (id);
189  if (am_it == wins_.end ())
190  {
191  PCL_WARN ("[updateFeatureHistogram] A window with id <%s> does not exists!.\n", id.c_str ());
192  return (false);
193  }
194  RenWinInteract* renwinupd = &wins_[id];
195 
196  vtkSmartPointer<vtkDoubleArray> xy_array = vtkSmartPointer<vtkDoubleArray>::New ();
197  xy_array->SetNumberOfComponents (2);
198  xy_array->SetNumberOfTuples (fields[field_idx].count);
199 
200  // Parse the cloud data and store it in the array
201  double xy[2];
202  for (int d = 0; d < fields[field_idx].count; ++d)
203  {
204  xy[0] = d;
205  //xy[1] = cloud.points[index].histogram[d];
206  float data;
207  memcpy (&data, reinterpret_cast<const char*> (&cloud.points[index]) + fields[field_idx].offset + d * sizeof (float), sizeof (float));
208  xy[1] = data;
209  xy_array->SetTuple (d, xy);
210  }
211 
212  reCreateActor (xy_array, renwinupd, cloud.fields[field_idx].count - 1);
213  return (true);
214 }
215 
216 #endif
217