00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Point Cloud Library (PCL) - www.pointclouds.org 00005 * Copyright (c) 2013-, Open Perception, Inc. 00006 * 00007 * All rights reserved. 00008 * 00009 * Redistribution and use in source and binary forms, with or without 00010 * modification, are permitted provided that the following conditions 00011 * are met: 00012 * 00013 * * Redistributions of source code must retain the above copyright 00014 * notice, this list of conditions and the following disclaimer. 00015 * * Redistributions in binary form must reproduce the above 00016 * copyright notice, this list of conditions and the following 00017 * disclaimer in the documentation and/or other materials provided 00018 * with the distribution. 00019 * * Neither the name of the copyright holder(s) nor the names of its 00020 * contributors may be used to endorse or promote products derived 00021 * from this software without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00024 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00025 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00026 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00027 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00028 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00029 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00032 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00033 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00034 * POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 */ 00037 00038 #ifndef PCL_POINT_CLOUD_IMAGE_EXTRACTORS_IMPL_HPP_ 00039 #define PCL_POINT_CLOUD_IMAGE_EXTRACTORS_IMPL_HPP_ 00040 00041 #include <map> 00042 #include <ctime> 00043 #include <cstdlib> 00044 00045 #include <pcl/common/io.h> 00046 00047 /////////////////////////////////////////////////////////////////////////////////////////// 00048 template <typename PointT> bool 00049 pcl::io::PointCloudImageExtractorFromNormalField<PointT>::extract (const PointCloud& cloud, pcl::PCLImage& img) const 00050 { 00051 if (!cloud.isOrganized () || cloud.points.size () != cloud.width * cloud.height) 00052 return (false); 00053 00054 std::vector<pcl::PCLPointField> fields; 00055 int field_x_idx = pcl::getFieldIndex (cloud, "normal_x", fields); 00056 int field_y_idx = pcl::getFieldIndex (cloud, "normal_y", fields); 00057 int field_z_idx = pcl::getFieldIndex (cloud, "normal_z", fields); 00058 if (field_x_idx == -1 || field_y_idx == -1 || field_z_idx == -1) 00059 return (false); 00060 const size_t offset_x = fields[field_x_idx].offset; 00061 const size_t offset_y = fields[field_y_idx].offset; 00062 const size_t offset_z = fields[field_z_idx].offset; 00063 00064 img.encoding = "rgb8"; 00065 img.width = cloud.width; 00066 img.height = cloud.height; 00067 img.step = img.width * sizeof (unsigned char) * 3; 00068 img.data.resize (img.step * img.height); 00069 00070 for (size_t i = 0; i < cloud.points.size (); ++i) 00071 { 00072 float x; 00073 float y; 00074 float z; 00075 pcl::getFieldValue<PointT, float> (cloud.points[i], offset_x, x); 00076 pcl::getFieldValue<PointT, float> (cloud.points[i], offset_y, y); 00077 pcl::getFieldValue<PointT, float> (cloud.points[i], offset_z, z); 00078 img.data[i * 3 + 0] = static_cast<unsigned char>((x + 1.0) * 127); 00079 img.data[i * 3 + 1] = static_cast<unsigned char>((y + 1.0) * 127); 00080 img.data[i * 3 + 2] = static_cast<unsigned char>((z + 1.0) * 127); 00081 } 00082 00083 return (true); 00084 } 00085 00086 /////////////////////////////////////////////////////////////////////////////////////////// 00087 template <typename PointT> bool 00088 pcl::io::PointCloudImageExtractorFromRGBField<PointT>::extract (const PointCloud& cloud, pcl::PCLImage& img) const 00089 { 00090 if (!cloud.isOrganized () || cloud.points.size () != cloud.width * cloud.height) 00091 return (false); 00092 00093 std::vector<pcl::PCLPointField> fields; 00094 int field_idx = pcl::getFieldIndex (cloud, "rgb", fields); 00095 if (field_idx == -1) 00096 { 00097 field_idx = pcl::getFieldIndex (cloud, "rgba", fields); 00098 if (field_idx == -1) 00099 return (false); 00100 } 00101 const size_t offset = fields[field_idx].offset; 00102 00103 img.encoding = "rgb8"; 00104 img.width = cloud.width; 00105 img.height = cloud.height; 00106 img.step = img.width * sizeof (unsigned char) * 3; 00107 img.data.resize (img.step * img.height); 00108 00109 for (size_t i = 0; i < cloud.points.size (); ++i) 00110 { 00111 uint32_t val; 00112 pcl::getFieldValue<PointT, uint32_t> (cloud.points[i], offset, val); 00113 img.data[i * 3 + 0] = (val >> 16) & 0x0000ff; 00114 img.data[i * 3 + 1] = (val >> 8) & 0x0000ff; 00115 img.data[i * 3 + 2] = (val) & 0x0000ff; 00116 } 00117 00118 return (true); 00119 } 00120 00121 /////////////////////////////////////////////////////////////////////////////////////////// 00122 template <typename PointT> bool 00123 pcl::io::PointCloudImageExtractorFromLabelField<PointT>::extract (const PointCloud& cloud, pcl::PCLImage& img) const 00124 { 00125 if (!cloud.isOrganized () || cloud.points.size () != cloud.width * cloud.height) 00126 return (false); 00127 00128 std::vector<pcl::PCLPointField> fields; 00129 int field_idx = pcl::getFieldIndex (cloud, "label", fields); 00130 if (field_idx == -1) 00131 return (false); 00132 const size_t offset = fields[field_idx].offset; 00133 00134 switch (color_mode_) 00135 { 00136 case COLORS_MONO: 00137 { 00138 img.encoding = "mono16"; 00139 img.width = cloud.width; 00140 img.height = cloud.height; 00141 img.step = img.width * sizeof (unsigned short); 00142 img.data.resize (img.step * img.height); 00143 unsigned short* data = reinterpret_cast<unsigned short*>(&img.data[0]); 00144 for (size_t i = 0; i < cloud.points.size (); ++i) 00145 { 00146 uint32_t val; 00147 pcl::getFieldValue<PointT, uint32_t> (cloud.points[i], offset, val); 00148 data[i] = static_cast<unsigned short>(val); 00149 } 00150 break; 00151 } 00152 case COLORS_RGB_RANDOM: 00153 { 00154 img.encoding = "rgb8"; 00155 img.width = cloud.width; 00156 img.height = cloud.height; 00157 img.step = img.width * sizeof (unsigned char) * 3; 00158 img.data.resize (img.step * img.height); 00159 00160 std::srand(std::time(0)); 00161 std::map<uint32_t, size_t> colormap; 00162 00163 for (size_t i = 0; i < cloud.points.size (); ++i) 00164 { 00165 uint32_t val; 00166 pcl::getFieldValue<PointT, uint32_t> (cloud.points[i], offset, val); 00167 if (colormap.count (val) == 0) 00168 { 00169 colormap[val] = i * 3; 00170 img.data[i * 3 + 0] = static_cast<uint8_t> ((std::rand () % 256)); 00171 img.data[i * 3 + 1] = static_cast<uint8_t> ((std::rand () % 256)); 00172 img.data[i * 3 + 2] = static_cast<uint8_t> ((std::rand () % 256)); 00173 } 00174 else 00175 { 00176 memcpy (&img.data[i * 3], &img.data[colormap[val]], 3); 00177 } 00178 } 00179 break; 00180 } 00181 } 00182 00183 return (true); 00184 } 00185 00186 /////////////////////////////////////////////////////////////////////////////////////////// 00187 template <typename PointT> bool 00188 pcl::io::PointCloudImageExtractorWithScaling<PointT>::extract (const PointCloud& cloud, pcl::PCLImage& img) const 00189 { 00190 if (!cloud.isOrganized () || cloud.points.size () != cloud.width * cloud.height) 00191 return (false); 00192 00193 std::vector<pcl::PCLPointField> fields; 00194 int field_idx = pcl::getFieldIndex (cloud, field_name_, fields); 00195 if (field_idx == -1) 00196 return (false); 00197 const size_t offset = fields[field_idx].offset; 00198 00199 img.encoding = "mono16"; 00200 img.width = cloud.width; 00201 img.height = cloud.height; 00202 img.step = img.width * sizeof (unsigned short); 00203 img.data.resize (img.step * img.height); 00204 unsigned short* data = reinterpret_cast<unsigned short*>(&img.data[0]); 00205 00206 float scaling_factor = scaling_factor_; 00207 float data_min = 0.0f; 00208 if (scaling_method_ == SCALING_FULL_RANGE) 00209 { 00210 float min = std::numeric_limits<float>::infinity(); 00211 float max = -std::numeric_limits<float>::infinity(); 00212 for (size_t i = 0; i < cloud.points.size (); ++i) 00213 { 00214 float val; 00215 pcl::getFieldValue<PointT, float> (cloud.points[i], offset, val); 00216 if (val < min) 00217 min = val; 00218 if (val > max) 00219 max = val; 00220 } 00221 scaling_factor = min == max ? 0 : std::numeric_limits<unsigned short>::max() / (max - min); 00222 data_min = min; 00223 } 00224 00225 for (size_t i = 0; i < cloud.points.size (); ++i) 00226 { 00227 float val; 00228 pcl::getFieldValue<PointT, float> (cloud.points[i], offset, val); 00229 if (scaling_method_ == SCALING_NO) 00230 { 00231 data[i] = val; 00232 } 00233 else if (scaling_method_ == SCALING_FULL_RANGE) 00234 { 00235 data[i] = (val - data_min) * scaling_factor; 00236 } 00237 else if (scaling_method_ == SCALING_FIXED_FACTOR) 00238 { 00239 data[i] = val * scaling_factor; 00240 } 00241 } 00242 00243 return (true); 00244 } 00245 00246 #endif // PCL_POINT_CLOUD_IMAGE_EXTRACTORS_IMPL_HPP_ 00247
Except where otherwise noted, the PointClouds.org web pages are licensed under Creative Commons Attribution 3.0.
Pages generated on Wed Mar 25 00:24:30 2015