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