Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
point_types_conversion.h
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: point_types_conversion.h 6126 2012-07-03 20:19:58Z aichim $
37  */
38 
39 #ifndef PCL_TYPE_CONVERSIONS_H
40 #define PCL_TYPE_CONVERSIONS_H
41 
42 namespace pcl
43 {
44  // r,g,b, i values are from 0 to 1
45  // h = [0,360]
46  // s, v values are from 0 to 1
47  // if s = 0 > h = -1 (undefined)
48 
53  inline void
55  PointXYZI& out)
56  {
57  out.x = in.x; out.y = in.y; out.z = in.z;
58  out.intensity = 0.299f * in.r + 0.587f * in.g + 0.114f * in.b;
59  }
60 
61 
66  inline void
68  PointXYZHSV& out)
69  {
70  float min;
71 
72  out.x = in.x; out.y = in.y; out.z = in.z;
73 
74  out.v = std::max (in.r, std::max (in.g, in.b));
75  min = std::min (in.r, std::min (in.g, in.b));
76 
77  if (out.v != 0)
78  out.s = (out.v - min) / out.v;
79  else
80  {
81  out.s = 0;
82  out.h = -1;
83  return;
84  }
85 
86  if (in.r == out.v)
87  out.h = static_cast<float> (in.g - in.b) / (out.v - min);
88  else if (in.g == out.v)
89  out.h = static_cast<float> (2 + (in.b - in.r) / (out.v - min));
90  else
91  out.h = static_cast<float> (4 + (in.r - in.g) / (out.v - min));
92  out.h *= 60;
93  if (out.h < 0)
94  out.h += 360;
95  }
96 
97 
102  inline void
104  PointXYZRGB& out)
105  {
106  if (in.s == 0)
107  {
108  out.r = out.g = out.b = static_cast<uint8_t> (in.v);
109  return;
110  }
111  float a = in.h / 60;
112  int i = static_cast<int> (floorf (a));
113  float f = a - static_cast<float> (i);
114  float p = in.v * (1 - in.s);
115  float q = in.v * (1 - in.s * f);
116  float t = in.v * (1 - in.s * (1 - f));
117 
118  switch (i)
119  {
120  case 0:
121  {
122  out.r = static_cast<uint8_t> (255 * in.v);
123  out.g = static_cast<uint8_t> (255 * t);
124  out.b = static_cast<uint8_t> (255 * p);
125  break;
126  }
127  case 1:
128  {
129  out.r = static_cast<uint8_t> (255 * q);
130  out.g = static_cast<uint8_t> (255 * in.v);
131  out.b = static_cast<uint8_t> (255 * p);
132  break;
133  }
134  case 2:
135  {
136  out.r = static_cast<uint8_t> (255 * p);
137  out.g = static_cast<uint8_t> (255 * in.v);
138  out.b = static_cast<uint8_t> (255 * t);
139  break;
140  }
141  case 3:
142  {
143  out.r = static_cast<uint8_t> (255 * p);
144  out.g = static_cast<uint8_t> (255 * q);
145  out.b = static_cast<uint8_t> (255 * in.v);
146  break;
147  }
148  case 4:
149  {
150  out.r = static_cast<uint8_t> (255 * t);
151  out.g = static_cast<uint8_t> (255 * p);
152  out.b = static_cast<uint8_t> (255 * in.v);
153  break;
154  }
155  default:
156  {
157  out.r = static_cast<uint8_t> (255 * in.v);
158  out.g = static_cast<uint8_t> (255 * p);
159  out.b = static_cast<uint8_t> (255 * q);
160  break;
161  }
162  }
163  }
164 
165 
170  inline void
173  {
174  out.width = in.width;
175  out.height = in.height;
176  for (size_t i = 0; i < in.points.size (); i++)
177  {
178  PointXYZHSV p;
179  PointXYZRGBtoXYZHSV (in.points[i], p);
180  out.points.push_back (p);
181  }
182  }
187  inline void
190  {
191  out.width = in.width;
192  out.height = in.height;
193  for (size_t i = 0; i < in.points.size (); i++)
194  {
195  PointXYZI p;
196  PointXYZRGBtoXYZI (in.points[i], p);
197  out.points.push_back (p);
198  }
199  }
200 }
201 
202 #endif //#ifndef PCL_TYPE_CONVERSIONS_H
203