ply2ply.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2007-2012, Ares Lagae
00006  *  Copyright (c) 2012, Willow Garage, Inc.
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 Willow Garage, Inc. 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  * $Id$
00037  *
00038  */
00039 
00040 #include <cstdlib>
00041 #include <cstring>
00042 #include <fstream>
00043 #include <iostream>
00044 
00045 #include <pcl/io/boost.h>
00046 #include <pcl/io/ply/ply_parser.h>
00047 
00056 class ply_to_ply_converter
00057 {
00058   public:
00059     typedef int format_type;
00060     enum format
00061     {
00062       same_format,
00063       ascii_format,
00064       binary_format,
00065       binary_big_endian_format,
00066       binary_little_endian_format
00067     };
00068   
00069     ply_to_ply_converter(format_type format) : 
00070       format_(format), input_format_(), output_format_(), 
00071       bol_ (), ostream_ () {}
00072 
00073     bool 
00074     convert (const std::string &filename, std::istream& istream, std::ostream& ostream);
00075 
00076   private:
00077     void
00078     info_callback(const std::string& filename, std::size_t line_number, const std::string& message);
00079     
00080     void
00081     warning_callback(const std::string& filename, std::size_t line_number, const std::string& message);
00082     
00083     void
00084     error_callback(const std::string& filename, std::size_t line_number, const std::string& message);
00085     
00086     void
00087     magic_callback();
00088     
00089     void
00090     format_callback(pcl::io::ply::format_type format, const std::string& version);
00091     
00092     void
00093     element_begin_callback();
00094     
00095     void
00096     element_end_callback();
00097 
00098     boost::tuple<boost::function<void()>, boost::function<void()> > 
00099     element_definition_callback(const std::string& element_name, std::size_t count);
00100 
00101     template <typename ScalarType> void
00102     scalar_property_callback(ScalarType scalar);
00103 
00104     template <typename ScalarType> boost::function<void (ScalarType)> 
00105     scalar_property_definition_callback(const std::string& element_name, const std::string& property_name);
00106 
00107     template <typename SizeType, typename ScalarType> void
00108     list_property_begin_callback(SizeType size);
00109 
00110     template <typename SizeType, typename ScalarType> void
00111     list_property_element_callback(ScalarType scalar);
00112 
00113     template <typename SizeType, typename ScalarType> void
00114     list_property_end_callback();
00115 
00116     template <typename SizeType, typename ScalarType> boost::tuple<boost::function<void (SizeType)>, 
00117                                                                       boost::function<void (ScalarType)>, 
00118                                                                       boost::function<void ()> > 
00119     list_property_definition_callback(const std::string& element_name, const std::string& property_name);
00120     
00121     void
00122     comment_callback(const std::string& comment);
00123     
00124     void
00125     obj_info_callback(const std::string& obj_info);
00126 
00127     bool 
00128     end_header_callback();
00129 
00130     format_type format_;
00131     pcl::io::ply::format_type input_format_, output_format_;
00132     bool bol_;
00133     std::ostream* ostream_;
00134 };
00135 
00136 void
00137 ply_to_ply_converter::info_callback(const std::string& filename, std::size_t line_number, const std::string& message)
00138 {
00139   std::cerr << filename << ": " << line_number << ": " << "info: " << message << std::endl;
00140 }
00141 
00142 void
00143 ply_to_ply_converter::warning_callback(const std::string& filename, std::size_t line_number, const std::string& message)
00144 {
00145   std::cerr << filename << ": " << line_number << ": " << "warning: " << message << std::endl;
00146 }
00147 
00148 void
00149 ply_to_ply_converter::error_callback(const std::string& filename, std::size_t line_number, const std::string& message)
00150 {
00151   std::cerr << filename << ": " << line_number << ": " << "error: " << message << std::endl;
00152 }
00153 
00154 void
00155 ply_to_ply_converter::magic_callback()
00156 {
00157   (*ostream_) << "ply" << "\n";
00158 }
00159 
00160 void
00161 ply_to_ply_converter::format_callback(pcl::io::ply::format_type format, const std::string& version)
00162 {
00163   input_format_ = format;
00164 
00165   switch (format_) {
00166     case same_format:
00167       output_format_ = input_format_;
00168       break;
00169     case ascii_format:
00170       output_format_ = pcl::io::ply::ascii_format;
00171       break;
00172     case binary_format:
00173       output_format_ = pcl::io::ply::host_byte_order == pcl::io::ply::little_endian_byte_order ? pcl::io::ply::binary_little_endian_format : pcl::io::ply::binary_big_endian_format;
00174       break;
00175     case binary_big_endian_format:
00176       output_format_ = pcl::io::ply::binary_big_endian_format;
00177       break;
00178     case binary_little_endian_format:
00179       output_format_ = pcl::io::ply::binary_little_endian_format;
00180       break;
00181   };
00182 
00183   (*ostream_) << "format ";
00184   switch (output_format_) {
00185     case pcl::io::ply::ascii_format:
00186       (*ostream_) << "ascii";
00187       break;
00188     case pcl::io::ply::binary_little_endian_format:
00189       (*ostream_) << "binary_little_endian";
00190       break;
00191     case pcl::io::ply::binary_big_endian_format:
00192       (*ostream_) << "binary_big_endian";
00193       break;
00194   }
00195   (*ostream_) << " " << version << "\n";
00196 }
00197 
00198 void
00199 ply_to_ply_converter::element_begin_callback()
00200 {
00201   if (output_format_ == pcl::io::ply::ascii_format) {
00202     bol_ = true;
00203   }
00204 }
00205 
00206 void
00207 ply_to_ply_converter::element_end_callback()
00208 {
00209   if (output_format_ == pcl::io::ply::ascii_format) {
00210     (*ostream_) << "\n";
00211   }
00212 }
00213 
00214 boost::tuple<boost::function<void()>, boost::function<void()> > ply_to_ply_converter::element_definition_callback(const std::string& element_name, std::size_t count)
00215 {
00216   (*ostream_) << "element " << element_name << " " << count << "\n";
00217   return boost::tuple<boost::function<void()>, boost::function<void()> >(
00218     boost::bind(&ply_to_ply_converter::element_begin_callback, this),
00219     boost::bind(&ply_to_ply_converter::element_end_callback, this)
00220   );
00221 }
00222 
00223 template <typename ScalarType>
00224 void
00225 ply_to_ply_converter::scalar_property_callback(ScalarType scalar)
00226 {
00227   if (output_format_ == pcl::io::ply::ascii_format) {
00228     using namespace pcl::io::ply::io_operators;
00229     if (bol_) {
00230       bol_ = false;
00231       (*ostream_) << scalar;
00232     }
00233     else {
00234       (*ostream_) << " " << scalar;
00235     }
00236   }
00237   else {
00238     if (((pcl::io::ply::host_byte_order == pcl::io::ply::little_endian_byte_order) && (output_format_ == pcl::io::ply::binary_big_endian_format))
00239       || ((pcl::io::ply::host_byte_order == pcl::io::ply::big_endian_byte_order) && (output_format_ == pcl::io::ply::binary_little_endian_format))) {
00240       pcl::io::ply::swap_byte_order(scalar);
00241     }
00242     ostream_->write(reinterpret_cast<char*>(&scalar), sizeof(scalar));
00243   }
00244 }
00245 
00247 template <typename ScalarType> boost::function<void (ScalarType)> 
00248 ply_to_ply_converter::scalar_property_definition_callback (const std::string&, const std::string& property_name)
00249 {
00250   (*ostream_) << "property " << pcl::io::ply::type_traits<ScalarType>::old_name() << " " << property_name << "\n";
00251   return boost::bind(&ply_to_ply_converter::scalar_property_callback<ScalarType>, this, _1);
00252 }
00253 
00255 template <typename SizeType, typename ScalarType> void
00256 ply_to_ply_converter::list_property_begin_callback (SizeType size)
00257 {
00258   if (output_format_ == pcl::io::ply::ascii_format) 
00259   {
00260     using namespace pcl::io::ply::io_operators;
00261     if (bol_) 
00262     {
00263       bol_ = false;
00264       (*ostream_) << size;
00265     }
00266     else 
00267       (*ostream_) << " " << size;
00268   }
00269   else 
00270   {
00271     if (((pcl::io::ply::host_byte_order == pcl::io::ply::little_endian_byte_order) && (output_format_ == pcl::io::ply::binary_big_endian_format))
00272       || ((pcl::io::ply::host_byte_order == pcl::io::ply::big_endian_byte_order) && (output_format_ == pcl::io::ply::binary_little_endian_format))) {
00273       pcl::io::ply::swap_byte_order(size);
00274     }
00275     ostream_->write(reinterpret_cast<char*>(&size), sizeof(size));
00276   }
00277 }
00278 
00280 template <typename SizeType, typename ScalarType> void
00281 ply_to_ply_converter::list_property_element_callback (ScalarType scalar)
00282 {
00283   if (output_format_ == pcl::io::ply::ascii_format) 
00284   {
00285     using namespace pcl::io::ply::io_operators;
00286     (*ostream_) << " " << scalar;
00287   }
00288   else 
00289   {
00290     if (((pcl::io::ply::host_byte_order == pcl::io::ply::little_endian_byte_order) && (output_format_ == pcl::io::ply::binary_big_endian_format)) || 
00291         ((pcl::io::ply::host_byte_order == pcl::io::ply::big_endian_byte_order) && (output_format_ == pcl::io::ply::binary_little_endian_format)))
00292       pcl::io::ply::swap_byte_order(scalar);
00293 
00294     ostream_->write(reinterpret_cast<char*>(&scalar), sizeof(scalar));
00295   }
00296 }
00297 
00299 template <typename SizeType, typename ScalarType> void
00300 ply_to_ply_converter::list_property_end_callback() {}
00301 
00303 template <typename SizeType, typename ScalarType> boost::tuple<boost::function<void (SizeType)>, 
00304                                                                   boost::function<void (ScalarType)>, 
00305                                                                   boost::function<void ()> > 
00306 ply_to_ply_converter::list_property_definition_callback (const std::string&, const std::string& property_name)
00307 {
00308   (*ostream_) << "property list " << pcl::io::ply::type_traits<SizeType>::old_name() << " " << pcl::io::ply::type_traits<ScalarType>::old_name() << " " << property_name << "\n";
00309   return boost::tuple<boost::function<void (SizeType)>, boost::function<void (ScalarType)>, boost::function<void ()> >(
00310     boost::bind(&ply_to_ply_converter::list_property_begin_callback<SizeType, ScalarType>, this, _1),
00311     boost::bind(&ply_to_ply_converter::list_property_element_callback<SizeType, ScalarType>, this, _1),
00312     boost::bind(&ply_to_ply_converter::list_property_end_callback<SizeType, ScalarType>, this)
00313   );
00314 }
00315 
00317 void
00318 ply_to_ply_converter::comment_callback(const std::string& comment)
00319 {
00320   (*ostream_) << comment << "\n";
00321 }
00322 
00324 void
00325 ply_to_ply_converter::obj_info_callback(const std::string& obj_info)
00326 {
00327   (*ostream_) << obj_info << "\n";
00328 }
00329 
00331 bool 
00332 ply_to_ply_converter::end_header_callback()
00333 {
00334   (*ostream_) << "end_header" << "\n";
00335   return true;
00336 }
00337 
00339 bool 
00340 ply_to_ply_converter::convert (const std::string &ifilename, std::istream&, std::ostream& ostream)
00341 {
00342   pcl::io::ply::ply_parser::flags_type ply_parser_flags = 0;
00343 
00344   pcl::io::ply::ply_parser ply_parser(ply_parser_flags);
00345 
00346   ply_parser.info_callback(boost::bind(&ply_to_ply_converter::info_callback, this, boost::ref(ifilename), _1, _2));
00347   ply_parser.warning_callback(boost::bind(&ply_to_ply_converter::warning_callback, this, boost::ref(ifilename), _1, _2));
00348   ply_parser.error_callback(boost::bind(&ply_to_ply_converter::error_callback, this, boost::ref(ifilename), _1, _2));
00349 
00350   ply_parser.magic_callback(boost::bind(&ply_to_ply_converter::magic_callback, this));
00351   ply_parser.format_callback(boost::bind(&ply_to_ply_converter::format_callback, this, _1, _2));
00352   ply_parser.element_definition_callback(boost::bind(&ply_to_ply_converter::element_definition_callback, this, _1, _2));
00353 
00354   pcl::io::ply::ply_parser::scalar_property_definition_callbacks_type scalar_property_definition_callbacks;
00355 
00356   pcl::io::ply::at<pcl::io::ply::int8>(scalar_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::scalar_property_definition_callback<pcl::io::ply::int8>, this, _1, _2);
00357   pcl::io::ply::at<pcl::io::ply::int16>(scalar_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::scalar_property_definition_callback<pcl::io::ply::int16>, this, _1, _2);
00358   pcl::io::ply::at<pcl::io::ply::int32>(scalar_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::scalar_property_definition_callback<pcl::io::ply::int32>, this, _1, _2);
00359   pcl::io::ply::at<pcl::io::ply::uint8>(scalar_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::scalar_property_definition_callback<pcl::io::ply::uint8>, this, _1, _2);
00360   pcl::io::ply::at<pcl::io::ply::uint16>(scalar_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::scalar_property_definition_callback<pcl::io::ply::uint16>, this, _1, _2);
00361   pcl::io::ply::at<pcl::io::ply::uint32>(scalar_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::scalar_property_definition_callback<pcl::io::ply::uint32>, this, _1, _2);
00362   pcl::io::ply::at<pcl::io::ply::float32>(scalar_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::scalar_property_definition_callback<pcl::io::ply::float32>, this, _1, _2);
00363   pcl::io::ply::at<pcl::io::ply::float64>(scalar_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::scalar_property_definition_callback<pcl::io::ply::float64>, this, _1, _2);
00364 
00365   ply_parser.scalar_property_definition_callbacks(scalar_property_definition_callbacks);
00366 
00367   pcl::io::ply::ply_parser::list_property_definition_callbacks_type list_property_definition_callbacks;
00368 
00369   pcl::io::ply::at<pcl::io::ply::uint8, pcl::io::ply::int8>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint8, pcl::io::ply::int8>, this, _1, _2);
00370   pcl::io::ply::at<pcl::io::ply::uint8, pcl::io::ply::int16>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint8, pcl::io::ply::int16>, this, _1, _2);
00371   pcl::io::ply::at<pcl::io::ply::uint8, pcl::io::ply::int32>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint8, pcl::io::ply::int32>, this, _1, _2);
00372   pcl::io::ply::at<pcl::io::ply::uint8, pcl::io::ply::uint8>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint8, pcl::io::ply::uint8>, this, _1, _2);
00373   pcl::io::ply::at<pcl::io::ply::uint8, pcl::io::ply::uint16>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint8, pcl::io::ply::uint16>, this, _1, _2);
00374   pcl::io::ply::at<pcl::io::ply::uint8, pcl::io::ply::uint32>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint8, pcl::io::ply::uint32>, this, _1, _2);
00375   pcl::io::ply::at<pcl::io::ply::uint8, pcl::io::ply::float32>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint8, pcl::io::ply::float32>, this, _1, _2);
00376   pcl::io::ply::at<pcl::io::ply::uint8, pcl::io::ply::float64>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint8, pcl::io::ply::float64>, this, _1, _2);
00377 
00378   pcl::io::ply::at<pcl::io::ply::uint16, pcl::io::ply::int8>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint16, pcl::io::ply::int8>, this, _1, _2);
00379   pcl::io::ply::at<pcl::io::ply::uint16, pcl::io::ply::int16>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint16, pcl::io::ply::int16>, this, _1, _2);
00380   pcl::io::ply::at<pcl::io::ply::uint16, pcl::io::ply::int32>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint16, pcl::io::ply::int32>, this, _1, _2);
00381   pcl::io::ply::at<pcl::io::ply::uint16, pcl::io::ply::uint8>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint16, pcl::io::ply::uint8>, this, _1, _2);
00382   pcl::io::ply::at<pcl::io::ply::uint16, pcl::io::ply::uint16>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint16, pcl::io::ply::uint16>, this, _1, _2);
00383   pcl::io::ply::at<pcl::io::ply::uint16, pcl::io::ply::uint32>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint16, pcl::io::ply::uint32>, this, _1, _2);
00384   pcl::io::ply::at<pcl::io::ply::uint16, pcl::io::ply::float32>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint16, pcl::io::ply::float32>, this, _1, _2);
00385   pcl::io::ply::at<pcl::io::ply::uint16, pcl::io::ply::float64>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint16, pcl::io::ply::float64>, this, _1, _2);
00386 
00387   pcl::io::ply::at<pcl::io::ply::uint32, pcl::io::ply::int8>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint32, pcl::io::ply::int8>, this, _1, _2);
00388   pcl::io::ply::at<pcl::io::ply::uint32, pcl::io::ply::int16>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint32, pcl::io::ply::int16>, this, _1, _2);
00389   pcl::io::ply::at<pcl::io::ply::uint32, pcl::io::ply::int32>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint32, pcl::io::ply::int32>, this, _1, _2);
00390   pcl::io::ply::at<pcl::io::ply::uint32, pcl::io::ply::uint8>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint32, pcl::io::ply::uint8>, this, _1, _2);
00391   pcl::io::ply::at<pcl::io::ply::uint32, pcl::io::ply::uint16>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint32, pcl::io::ply::uint16>, this, _1, _2);
00392   pcl::io::ply::at<pcl::io::ply::uint32, pcl::io::ply::uint32>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint32, pcl::io::ply::uint32>, this, _1, _2);
00393   pcl::io::ply::at<pcl::io::ply::uint32, pcl::io::ply::float32>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint32, pcl::io::ply::float32>, this, _1, _2);
00394   pcl::io::ply::at<pcl::io::ply::uint32, pcl::io::ply::float64>(list_property_definition_callbacks) = boost::bind(&ply_to_ply_converter::list_property_definition_callback<pcl::io::ply::uint32, pcl::io::ply::float64>, this, _1, _2);
00395 
00396   ply_parser.list_property_definition_callbacks(list_property_definition_callbacks);
00397 
00398   ply_parser.comment_callback(boost::bind(&ply_to_ply_converter::comment_callback, this, _1));
00399   ply_parser.obj_info_callback(boost::bind(&ply_to_ply_converter::obj_info_callback, this, _1));
00400   ply_parser.end_header_callback(boost::bind(&ply_to_ply_converter::end_header_callback, this));
00401 
00402   ostream_ = &ostream;
00403   return ply_parser.parse(ifilename);
00404 }
00405 
00407 int 
00408 main(int argc, char* argv[])
00409 {
00410   ply_to_ply_converter::format_type ply_to_ply_converter_format = ply_to_ply_converter::same_format;
00411 
00412   int argi;
00413   for (argi = 1; argi < argc; ++argi) {
00414 
00415     if (argv[argi][0] != '-') {
00416       break;
00417     }
00418     if (argv[argi][1] == 0) {
00419       ++argi;
00420       break;
00421     }
00422     char short_opt, *long_opt, *opt_arg;
00423     if (argv[argi][1] != '-') {
00424       short_opt = argv[argi][1];
00425       opt_arg = &argv[argi][2];
00426       long_opt = &argv[argi][2];
00427       while (*long_opt != '\0') {
00428         ++long_opt;
00429       }
00430     }
00431     else {
00432       short_opt = 0;
00433       long_opt = &argv[argi][2];
00434       opt_arg = long_opt;
00435       while ((*opt_arg != '=') && (*opt_arg != '\0')) {
00436         ++opt_arg;
00437       }
00438       if (*opt_arg == '=') {
00439         *opt_arg++ = '\0';
00440       }
00441     }
00442 
00443     if ((short_opt == 'h') || (std::strcmp(long_opt, "help") == 0)) {
00444       std::cout << "Usage: ply2ply [OPTION] [[INFILE] OUTFILE]\n";
00445       std::cout << "Parse an PLY file.\n";
00446       std::cout << "\n";
00447       std::cout << "  -h, --help           display this help and exit\n";
00448       std::cout << "  -v, --version        output version information and exit\n";
00449       std::cout << "  -f, --format=FORMAT  set format\n";
00450       std::cout << "\n";
00451       std::cout << "FORMAT may be one of the following: ascii, binary, binary_big_endian,\n";
00452       std::cout << "binary_little_endian.\n";
00453       std::cout << "If no format is given, the format of INFILE is kept.\n";
00454       std::cout << "\n";
00455       std::cout << "With no INFILE/OUTFILE, or when INFILE/OUTFILE is -, read standard input/output.\n";
00456       std::cout << "\n";
00457       std::cout << "Report bugs to <www.pointclouds.org/issues>.\n";
00458       return EXIT_SUCCESS;
00459     }
00460 
00461     else if ((short_opt == 'v') || (std::strcmp(long_opt, "version") == 0)) {
00462       std::cout << "ply2ply\n";
00463       std::cout << " Point Cloud Library (PCL) - www.pointclouds.org\n";
00464       std::cout << " Copyright (c) 2007-2012, Ares Lagae\n";
00465       std::cout << " Copyright (c) 2012, Willow Garage, Inc.\n";
00466       std::cout << " All rights reserved.\n";
00467       std::cout << " Redistribution and use in source and binary forms, with or without\n";
00468       std::cout << " modification, are permitted provided that the following conditions\n";
00469       std::cout << " are met:\n";
00470       std::cout << "  * Redistributions of source code must retain the above copyright\n";
00471       std::cout << "    notice, this list of conditions and the following disclaimer.\n";
00472       std::cout << "  * Redistributions in binary form must reproduce the above\n";
00473       std::cout << "    copyright notice, this list of conditions and the following\n";
00474       std::cout << "    disclaimer in the documentation and/or other materials provided\n";
00475       std::cout << "    with the distribution.\n";
00476       std::cout << "  * Neither the name of Willow Garage, Inc. nor the names of its\n";
00477       std::cout << "    contributors may be used to endorse or promote products derived\n";
00478       std::cout << "    from this software without specific prior written permission.\n";
00479       std::cout << " THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n";
00480       std::cout << " \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n";
00481       std::cout << " LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\n";
00482       std::cout << " FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\n";
00483       std::cout << " COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\n";
00484       std::cout << " INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\n";
00485       std::cout << " BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n";
00486       std::cout << " LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n";
00487       std::cout << " CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n";
00488       std::cout << " LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\n";
00489       std::cout << " ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n";
00490       std::cout << " POSSIBILITY OF SUCH DAMAGE.\n";
00491       return EXIT_SUCCESS;
00492     }
00493 
00494     else if ((short_opt == 'f') || (std::strcmp(long_opt, "format") == 0)) {
00495       if (strcmp(opt_arg, "ascii") == 0) {
00496         ply_to_ply_converter_format = ply_to_ply_converter::ascii_format;
00497       }
00498       else if (strcmp(opt_arg, "binary") == 0) {
00499         ply_to_ply_converter_format = ply_to_ply_converter::binary_format;
00500       }
00501       else if (strcmp(opt_arg, "binary_little_endian") == 0) {
00502         ply_to_ply_converter_format = ply_to_ply_converter::binary_little_endian_format;
00503       }
00504       else if (strcmp(opt_arg, "binary_big_endian") == 0) {
00505         ply_to_ply_converter_format = ply_to_ply_converter::binary_big_endian_format;
00506       }
00507       else {
00508         std::cerr << "ply2ply: " << "invalid option `" << argv[argi] << "'" << "\n";
00509         std::cerr << "Try `" << argv[0] << " --help' for more information.\n";
00510         return EXIT_FAILURE;
00511       }
00512     }
00513 
00514     else {
00515       std::cerr << "ply2ply: " << "invalid option `" << argv[argi] << "'" << "\n";
00516       std::cerr << "Try `" << argv[0] << " --help' for more information.\n";
00517       return EXIT_FAILURE;
00518     }
00519   }
00520 
00521   int parc = argc - argi;
00522   char** parv = argv + argi;
00523   if (parc > 2) {
00524     std::cerr << "ply2ply: " << "too many parameters" << "\n";
00525     std::cerr << "Try `" << argv[0] << " --help' for more information.\n";
00526     return EXIT_FAILURE;
00527   }
00528 
00529   std::ifstream ifstream;
00530   const char* ifilename = "";
00531   if (parc > 0) {
00532     ifilename = parv[0];
00533     if (std::strcmp(ifilename, "-") != 0) {
00534       ifstream.open(ifilename);
00535       if (!ifstream.is_open()) {
00536         std::cerr << "ply2ply: " << ifilename << ": " << "no such file or directory" << "\n";
00537         return EXIT_FAILURE;
00538       }
00539     }
00540   }
00541 
00542   std::ofstream ofstream;
00543   const char* ofilename = "";
00544   if (parc > 1) {
00545     ofilename = parv[1];
00546     if (std::strcmp(ofilename, "-") != 0) {
00547       ofstream.open(ofilename);
00548       if (!ofstream.is_open()) {
00549         std::cerr << "ply2ply: " << ofilename << ": " << "could not open file" << "\n";
00550         return EXIT_FAILURE;
00551       }
00552     }
00553   }
00554 
00555   std::istream& istream = ifstream.is_open() ? ifstream : std::cin;
00556   std::ostream& ostream = ofstream.is_open() ? ofstream : std::cout;
00557 
00558   class ply_to_ply_converter ply_to_ply_converter(ply_to_ply_converter_format);
00559   return ply_to_ply_converter.convert (ifilename, istream, ostream);
00560 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:28:18