// Copyright (c) 2007-09 INRIA Sophia-Antipolis (France). // All rights reserved. // // This file is part of CGAL (www.cgal.org). // // $URL: https://github.com/CGAL/cgal/blob/v5.1/Point_set_processing_3/include/CGAL/IO/read_xyz_points.h $ // $Id: read_xyz_points.h c253679 2020-04-18T16:27:58+02:00 Sébastien Loriot // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial // // Author(s) : Pierre Alliez and Laurent Saboret #ifndef CGAL_READ_XYZ_POINTS_H #define CGAL_READ_XYZ_POINTS_H #include #include #include #include #include #include #include #include #include #include #include namespace CGAL { /** \ingroup PkgPointSetProcessing3IO Reads points (positions + normals, if available) from a .xyz ASCII stream. The function expects for each point a line with the x y z position, optionally followed by the nx ny nz normal. The first line may contain the number of points in the file. Empty lines and comments starting by # character are allowed. \tparam OutputIteratorValueType type of objects that can be put in `OutputIterator`. It is default to `value_type_traits::%type` and can be omitted when the default is fine. \tparam OutputIterator iterator over output points. \param stream input stream. \param output output iterator over points. \param np an optional sequence of \ref bgl_namedparameters "Named Parameters" among the ones listed below \cgalNamedParamsBegin \cgalParamNBegin{point_map} \cgalParamDescription{a property map associating points to the elements of the point range} \cgalParamType{a model of `WritablePropertyMap` with value type `geom_traits::Point_3`} \cgalParamDefault{`CGAL::Identity_property_map`} \cgalParamNEnd \cgalParamNBegin{normal_map} \cgalParamDescription{a property map associating normals to the elements of the poing range} \cgalParamType{a model of `ReadWritePropertyMap` with value type `geom_traits::Vector_3`} \cgalParamDefault{If this parameter is omitted, normals in the input stream are ignored.} \cgalParamNEnd \cgalParamNBegin{geom_traits} \cgalParamDescription{an instance of a geometric traits class} \cgalParamType{a model of `Kernel`} \cgalParamDefault{a \cgal Kernel deduced from the point type, using `CGAL::Kernel_traits`} \cgalParamNEnd \cgalNamedParamsEnd \return true on success. */ template bool read_xyz_points( std::istream& stream, OutputIterator output, #ifdef DOXYGEN_RUNNING const NamedParameters& np) #else const CGAL_BGL_NP_CLASS& np) #endif { using parameters::choose_parameter; using parameters::get_parameter; typedef Point_set_processing_3::Fake_point_range PointRange; // basic geometric types typedef typename CGAL::GetPointMap::type PointMap; typedef typename Point_set_processing_3::GetNormalMap::type NormalMap; typedef typename Point_set_processing_3::GetK::Kernel Kernel; bool has_normals = !(boost::is_same::NoMap>::value); PointMap point_map = choose_parameter(get_parameter(np, internal_np::point_map)); NormalMap normal_map = choose_parameter(get_parameter(np, internal_np::normal_map)); // value_type_traits is a workaround as back_insert_iterator's value_type is void //typedef typename value_type_traits::type Enriched_point; typedef OutputIteratorValueType Enriched_point; typedef typename Kernel::FT FT; typedef typename Kernel::Point_3 Point; typedef typename Kernel::Vector_3 Vector; if(!stream) { std::cerr << "Error: cannot open file" << std::endl; return false; } // scan points long pointsCount; // number of points in file int lineNumber = 0; // line counter std::string line; // line buffer std::istringstream iss; while(getline(stream,line)) { // position + normal FT x,y,z; FT nx,ny,nz; lineNumber++; // Trims line buffer line.erase(line.find_last_not_of (" ")+1); line.erase(0, line.find_first_not_of (" ")); // Skips comment or empty line... if (line.length() == 0 || line[0] == '#') { continue; } // ...or reads position... else { iss.clear(); iss.str(line); if (iss >> iformat(x) >> iformat(y) >> iformat(z)) { Point point(x,y,z); Vector normal = CGAL::NULL_VECTOR; // ... + normal... if (iss >> iformat(nx)) { // In case we could read one number, we expect that there are two more if(iss >> iformat(ny) >> iformat(nz)){ normal = Vector(nx,ny,nz); } else { std::cerr << "Error line " << lineNumber << " of file" << std::endl; return false; } } Enriched_point pwn; put(point_map, pwn, point); // point_map[pwn] = point if (has_normals) put(normal_map, pwn, normal); // normal_map[pwn] = normal *output++ = pwn; continue; } } // ...or skips number of points on first line (optional) if (lineNumber == 1 && std::istringstream(line) >> pointsCount) { continue; } else // if wrong file format { std::cerr << "Error line " << lineNumber << " of file" << std::endl; return false; } } return true; } /// \cond SKIP_IN_MANUAL // variant with default NP template bool read_xyz_points( std::istream& stream, ///< input stream. OutputIterator output) ///< output iterator over points. { return read_xyz_points (stream, output, CGAL::parameters::all_default()); } // variant with default output iterator value type template bool read_xyz_points( std::istream& stream, ///< input stream. OutputIterator output, const CGAL_BGL_NP_CLASS& np) { return read_xyz_points::type> (stream, output, np); } // variant with default NP and output iterator value type template bool read_xyz_points( std::istream& stream, ///< input stream. OutputIterator output) { return read_xyz_points::type> (stream, output, CGAL::parameters::all_default()); } #ifndef CGAL_NO_DEPRECATED_CODE // deprecated API template CGAL_DEPRECATED_MSG("you are using the deprecated V1 API of CGAL::read_xyz_points_and_normals(), please update your code") bool read_xyz_points_and_normals( std::istream& stream, ///< input stream. OutputIterator output, ///< output iterator over points. PointPMap point_map, ///< property map: value_type of OutputIterator -> Point_3. NormalPMap normal_map, ///< property map: value_type of OutputIterator -> Vector_3. const Kernel& /*kernel*/) ///< geometric traits. { return read_xyz_points (stream, output, CGAL::parameters::point_map (point_map). normal_map (normal_map). geom_traits (Kernel())); } // deprecated API template CGAL_DEPRECATED_MSG("you are using the deprecated V1 API of CGAL::read_xyz_points_and_normals(), please update your code") bool read_xyz_points_and_normals( std::istream& stream, ///< input stream. OutputIterator output, ///< output iterator over points. PointPMap point_map, ///< property map: value_type of OutputIterator -> Point_3. NormalPMap normal_map, ///< property map: value_type of OutputIterator -> Vector_3. const Kernel& kernel) ///< geometric traits. { return read_xyz_points::type> (stream, output, CGAL::parameters::point_map (point_map). normal_map (normal_map). geom_traits (kernel)); } // deprecated API template CGAL_DEPRECATED_MSG("you are using the deprecated V1 API of CGAL::read_xyz_points_and_normals(), please update your code") bool read_xyz_points_and_normals( std::istream& stream, ///< input stream. OutputIterator output, ///< output iterator over points. PointPMap point_map, ///< property map: value_type of OutputIterator -> Point_3. NormalPMap normal_map) ///< property map: value_type of OutputIterator -> Vector_3. { return read_xyz_points (stream, output, CGAL::parameters::point_map (point_map). normal_map (normal_map)); } // deprecated API template CGAL_DEPRECATED_MSG("you are using the deprecated V1 API of CGAL::read_xyz_points_and_normals(), please update your code") bool read_xyz_points_and_normals( std::istream& stream, ///< input stream. OutputIterator output, ///< output iterator over points. PointPMap point_map, ///< property map: value_type of OutputIterator -> Point_3. NormalPMap normal_map) ///< property map: value_type of OutputIterator -> Vector_3. { return read_xyz_points::type> (stream, output, CGAL::parameters::point_map (point_map). normal_map (normal_map)); } // deprecated API template CGAL_DEPRECATED_MSG("you are using the deprecated V1 API of CGAL::read_xyz_points_and_normals(), please update your code") bool read_xyz_points_and_normals( std::istream& stream, ///< input stream. OutputIterator output, ///< output iterator over points. NormalPMap normal_map) ///< property map: value_type of OutputIterator -> Vector_3. { return read_xyz_points (stream, output, CGAL::parameters::normal_map (normal_map)); } // deprecated API template CGAL_DEPRECATED_MSG("you are using the deprecated V1 API of CGAL::read_xyz_points_and_normals(), please update your code") bool read_xyz_points_and_normals( std::istream& stream, ///< input stream. OutputIterator output, ///< output iterator over points. NormalPMap normal_map) ///< property map: value_type of OutputIterator -> Vector_3. { return read_xyz_points::type> (stream, output, CGAL::parameters::normal_map (normal_map)); } // deprecated API template CGAL_DEPRECATED_MSG("you are using the deprecated V1 API of CGAL::read_xyz_points(), please update your code") bool read_xyz_points( std::istream& stream, ///< input stream. OutputIterator output, ///< output iterator over points. PointPMap point_map, ///< property map: value_type of OutputIterator -> Point_3. const Kernel& kernel) ///< geometric traits. { return read_xyz_points (stream, output, CGAL::parameters::point_map (point_map). geom_traits (kernel)); } // deprecated API template CGAL_DEPRECATED_MSG("you are using the deprecated V1 API of CGAL::read_xyz_points(), please update your code") bool read_xyz_points( std::istream& stream, ///< input stream. OutputIterator output, ///< output iterator over points. PointPMap point_map, ///< property map: value_type of OutputIterator -> Point_3. const Kernel& kernel) ///< geometric traits. { return read_xyz_points::type> (stream, output, CGAL::parameters::point_map (point_map). geom_traits (kernel)); } // deprecated API template CGAL_DEPRECATED_MSG("you are using the deprecated V1 API of CGAL::read_xyz_points(), please update your code") bool read_xyz_points( std::istream& stream, ///< input stream. OutputIterator output, ///< output iterator over points. PointPMap point_map) ///< property map: value_type of OutputIterator -> Point_3. { return read_xyz_points (stream, output, CGAL::parameters::point_map (point_map)); } // deprecated API template CGAL_DEPRECATED_MSG("you are using the deprecated V1 API of CGAL::read_xyz_points(), please update your code") bool read_xyz_points( std::istream& stream, ///< input stream. OutputIterator output, ///< output iterator over points. PointPMap point_map) ///< property map: value_type of OutputIterator -> Point_3. { return read_xyz_points::type> (stream, output, CGAL::parameters::point_map (point_map)); } #endif // CGAL_NO_DEPRECATED_CODE /// \endcond } //namespace CGAL #endif // CGAL_READ_XYZ_POINTS_H