Subject: CGAL users discussion list
List archive
- From: Nader SALMAN <>
- To:
- Subject: Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set
- Date: Fri, 11 Dec 2009 16:23:15 +0100
Dear Tina, sorry for the little late reply, i just corrected the lack of Property_maps compatibility in our code, and corrected a small bug in your example code. It works now :-) So please try it and let me know. You will find attached to this mail the corrected versions of grid_simplify_point_set.h as well as grid_simplification_example.cpp (just replace the old files with the new ones) Thanks again! Yours, Nad. Nader SALMAN Geometrica Lab, Byron Y308 INRIA Sophia Antipolis (FRANCE) Tel: +33(0)4 9238 7161 Fax: +33(0)4 9715 5395 http://www-sop.inria.fr/members/Nader.Salman tina rumi a écrit : Thank you Nader! |
#include <CGAL/property_map.h>
#include <CGAL/remove_outliers.h>
#include <CGAL/IO/read_xyz_points.h>
#include <CGAL/grid_simplify_point_set.h>
#include <CGAL/jet_smooth_point_set.h>
#include <CGAL/pca_estimate_normals.h>
#include <CGAL/mst_orient_normals.h>
#include <CGAL/property_map.h>
#include <CGAL/IO/write_xyz_points.h>
#include <vector>
#include <fstream>
#include <utility> // defines std::pair
#include <list>
// types
// kernel
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
// Simple geometric types
typedef Kernel::FT FT;
typedef Kernel::Point_3 Point;
typedef Kernel::Vector_3 Vector;
// Point with normal vector stored in a std::pair.
typedef std::pair<Point, Vector> PointVectorPair;
typedef std::vector<PointVectorPair> PointList;
int main(void)
{
PointList points; // Type of input point set
// Reads a .xyz point set file in points[].
// The Dereference_property_map property map can be omitted here as it is
the default value.
//std::list<PointVectorPair> points;
std::ifstream stream("data/oni.xyz");
if (!stream || !CGAL::read_xyz_points(stream, std::back_inserter(points),
CGAL::First_of_pair_property_map<PointVectorPair>()))
{
std::cerr << "Error: cannot read file data" << std::endl;
return EXIT_FAILURE;
}
// Removes outliers using erase-remove idiom.
// The Dereference_property_map property map can be omitted here as it is
the default value.
const double removed_percentage = 5.0; // percentage of points to remove
const int nb_neighbors = 24; // considers 24 nearest neighbor points
points.erase(CGAL::remove_outliers(points.begin(), points.end(),
CGAL::Dereference_property_map<Point>(),
nb_neighbors, removed_percentage),
points.end());
// // Optional: after erase(), use Scott Meyer's "swap trick" to trim
excess capacity
// PointList(points).swap(points);
// simplification by clustering using erase-remove idiom
double cell_size = 0.001;
PointList::iterator unwanted_points_begin =
CGAL::grid_simplify_point_set(points.begin(), points.end(),
CGAL::First_of_pair_property_map<PointVectorPair>(), cell_size);
// delete unwanted points
points.erase(unwanted_points_begin , points.end());
// Optional: after erase(), use Scott Meyer's "swap trick" to trim excess
capacity
PointList(points).swap(points);
// Smoothing.
const unsigned int nb_neighbors2 = 8; // default is 24 for real-life point
sets
CGAL::jet_smooth_point_set(points.begin(), points.end(),
CGAL::First_of_pair_property_map<PointVectorPair>(), nb_neighbors2);
// Estimates normals direction.
// Note: pca_estimate_normals() requires an iterator over points
// as well as property maps to access each point's position and normal.
const int nb_neighbors3 = 18; // K-nearest neighbors = 3 rings
CGAL::pca_estimate_normals(points.begin(), points.end(),
CGAL::First_of_pair_property_map<PointVectorPair>(),
CGAL::Second_of_pair_property_map<PointVectorPair>(),
nb_neighbors3);
// Orients normals.
// Note: mst_orient_normals() requires an iterator over points
// as well as property maps to access each point's position and normal.
PointList::iterator unoriented_points_begin =
CGAL::mst_orient_normals(points.begin(), points.end(),
CGAL::First_of_pair_property_map<PointVectorPair>(),
CGAL::Second_of_pair_property_map<PointVectorPair>(),
nb_neighbors);
// Optional: delete points with an unoriented normal
// if you plan to call a reconstruction algorithm that expects oriented
normals.
points.erase(unoriented_points_begin, points.end());
// Optional: after erase(), use Scott Meyer's "swap trick" to trim excess
capacity
PointList(points).swap(points);
// Saves point set.
// Note: write_xyz_points_and_normals() requires an output iterator
// over points as well as property maps to access each
// point position and normal.
std::ofstream out("out_total.xyz");
if (!out ||
!CGAL::write_xyz_points_and_normals(
out, points.begin(), points.end(),
CGAL::First_of_pair_property_map<PointVectorPair>(),
CGAL::Second_of_pair_property_map<PointVectorPair>()))
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
//#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
//#include <CGAL/grid_simplify_point_set.h>
//#include <CGAL/IO/read_xyz_points.h>
//
//#include <vector>
//#include <fstream>
//
//// types
//typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
//typedef Kernel::Point_3 Point;
//
//int main(void)
//{
// // Reads a .xyz point set file in points[].
// std::vector<Point> points;
// std::ifstream stream("data/oni.xyz");
// if (!stream ||
// !CGAL::read_xyz_points(stream, std::back_inserter(points)))
// {
// std::cerr << "Error: cannot read file data/oni.xyz" << std::endl;
// return EXIT_FAILURE;
// }
//
// // simplification by clustering using erase-remove idiom
// double cell_size = 0.001;
// points.erase(CGAL::grid_simplify_point_set(points.begin(), points.end(),
cell_size),
// points.end());
//
// // Optional: after erase(), use Scott Meyer's "swap trick" to trim excess
capacity
// std::vector<Point>(points).swap(points);
//
// return EXIT_SUCCESS;
//}
//
//#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
//#include <CGAL/grid_simplify_point_set.h>
//#include <CGAL/IO/read_xyz_points.h>
//
//#include <vector>
//#include <fstream>
//
//// types
//typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
//typedef Kernel::Point_3 Point;
//
//int main(void)
//{
// // Reads a .xyz point set file in points[].
// std::vector<Point> points;
// std::ifstream stream("data/oni.xyz");
// if (!stream ||
// !CGAL::read_xyz_points(stream, std::back_inserter(points)))
// {
// std::cerr << "Error: cannot read file data/oni.xyz" << std::endl;
// return EXIT_FAILURE;
// }
//
// // simplification by clustering using erase-remove idiom
// double cell_size = 0.001;
// points.erase(CGAL::grid_simplify_point_set(points.begin(), points.end(),
cell_size),
// points.end());
//
// // Optional: after erase(), use Scott Meyer's "swap trick" to trim excess
capacity
// std::vector<Point>(points).swap(points);
//
// return EXIT_SUCCESS;
//}
//
// Copyright (c) 2007-09 INRIA Sophia-Antipolis (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you may redistribute point_it
under
// the terms of the Q Public License version 1.0.
// See the file LICENSE.QPL distributed with CGAL.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the
software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL:
svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Point_set_processing_3/include/CGAL/grid_simplify_point_set.h
$
// $Id: grid_simplify_point_set.h 49943 2009-06-17 07:49:35Z lsaboret $
//
// Author(s) : Nader Salman and Laurent Saboret
#ifndef CGAL_GRID_SIMPLIFY_POINT_SET_H
#define CGAL_GRID_SIMPLIFY_POINT_SET_H
#include <CGAL/property_map.h>
#include <CGAL/point_set_processing_assertions.h>
#include <iterator>
#include <set>
#include <deque>
#include <algorithm>
#include <cmath>
CGAL_BEGIN_NAMESPACE
//
----------------------------------------------------------------------------
// Private section
//
----------------------------------------------------------------------------
namespace CGALi {
/// Utility class for grid_simplify_point_set():
/// Less_epsilon_points_3 defines a 3D points order / 2 points are equal
/// iff they belong to the same cell of a grid of cell size = epsilon.
template <class Point_3, class PointPMap>
struct Less_epsilon_points_3
{
private:
double m_epsilon;
PointPMap point_pmap;
public:
Less_epsilon_points_3 (double epsilon, PointPMap p_pmap) : m_epsilon
(epsilon), point_pmap(p_pmap)
{
CGAL_point_set_processing_precondition(epsilon > 0);
}
bool operator() (const Point_3& a, const Point_3& b) const
{
typedef typename boost::property_traits<PointPMap>::value_type Point;
// Round points to multiples of m_epsilon, then compare.
Point a_n = get(point_pmap,&a);
Point b_n = get(point_pmap,&b);
Point rounded_a(round_epsilon(a_n.x(), m_epsilon),
round_epsilon(a_n.y(), m_epsilon),
round_epsilon(a_n.z(), m_epsilon));
Point rounded_b(round_epsilon(b_n.x(), m_epsilon),
round_epsilon(b_n.y(), m_epsilon),
round_epsilon(b_n.z(), m_epsilon));
return (rounded_a < rounded_b);
}
private:
// Round number to multiples of epsilon
static inline double round_epsilon(double value, double epsilon)
{
return std::floor(value/epsilon) * epsilon;
}
};
} /* namespace CGALi */
//
----------------------------------------------------------------------------
// Public section
//
----------------------------------------------------------------------------
/// Utility class for grid_simplify_point_set():
/// 3D points set which allows at most 1 point per cell
/// of a grid of cell size = epsilon.
///
/// Warning:
/// This class is a container sorted wrt points position
/// => you should not modify directly the order or the position of points.
template <class Point_3, class PointPMap>
class Epsilon_point_set_3 : public std::set<Point_3,
CGALi::Less_epsilon_points_3<Point_3, PointPMap> >
{
private:
// superclass
typedef std::set<Point_3, CGALi::Less_epsilon_points_3<Point_3,
PointPMap> > Base;
public:
Epsilon_point_set_3 (double epsilon, PointPMap point_pmap) : Base(
CGALi::Less_epsilon_points_3<Point_3,
PointPMap>(epsilon, point_pmap) )
{
CGAL_point_set_processing_precondition(epsilon > 0);
}
// default copy constructor, operator =() and destructor are fine.
};
/// Merges points which belong to the same cell of a grid of cell size =
epsilon.
///
/// This method modifies the order of input points so as to pack all
remaining points first,
/// and returns an iterator over the first point to remove (see erase-remove
idiom).
/// For this reason it should not be called on sorted containers.
///
/// @commentheading Precondition: epsilon > 0.
///
/// @commentheading Template Parameters:
/// @param ForwardIterator iterator over input points.
/// @param PointPMap is a model of boost::ReadablePropertyMap with a
value_type = Point_3<Kernel>.
/// It can be omitted if ForwardIterator value_type is convertible to
Point_3<Kernel>.
/// @param Kernel Geometric traits class.
/// It can be omitted and deduced automatically from PointPMap
value_type.
///
/// @return iterator over the first point to remove.
// This variant requires all parameters.
template <typename ForwardIterator,
typename PointPMap,
typename Kernel>
ForwardIterator grid_simplify_point_set(ForwardIterator first, ///< iterator
over the first input point.
ForwardIterator beyond, ///<
past-the-end iterator over the input points.
PointPMap point_pmap, ///< property
map ForwardIterator -> Point_3
double epsilon, ///<
tolerance value when merging 3D points.
const Kernel& kernel) ///<
geometric traits.
{
// actual type of input points
typedef typename std::iterator_traits<ForwardIterator>::value_type
Enriched_point;
CGAL_point_set_processing_precondition(epsilon > 0);
// Merges points which belong to the same cell of a grid of cell size =
epsilon.
// points_to_keep[] will contain 1 point per cell; the others will be in
points_to_remove[].
Epsilon_point_set_3<Enriched_point, PointPMap> points_to_keep(epsilon,
point_pmap);
std::deque<Enriched_point> points_to_remove;
for (ForwardIterator it=first ; it != beyond ; it++)
{
std::pair<typename Epsilon_point_set_3<Enriched_point,
PointPMap>::iterator,bool> result;
result = points_to_keep.insert(*it);
if (!result.second) // if not inserted
points_to_remove.push_back(*it);
}
// Replaces [first, beyond) range by the content of points_to_keep, then
points_to_remove.
ForwardIterator first_point_to_remove = std::copy(points_to_keep.begin(),
points_to_keep.end(), first);
std::copy(points_to_remove.begin(),
points_to_remove.end(), first_point_to_remove);
return first_point_to_remove;
}
/// @cond SKIP_IN_MANUAL
// This variant deduces the kernel from the iterator type.
template <typename ForwardIterator,
typename PointPMap
>
ForwardIterator
grid_simplify_point_set(
ForwardIterator first, ///< iterator over the first input point
ForwardIterator beyond, ///< past-the-end iterator
PointPMap point_pmap, ///< property map ForwardIterator -> Point_3
double epsilon) ///< tolerance value when merging 3D points
{
typedef typename boost::property_traits<PointPMap>::value_type Point;
typedef typename Kernel_traits<Point>::Kernel Kernel;
return grid_simplify_point_set(
first,beyond,
point_pmap,
epsilon,
Kernel());
}
/// @endcond
/// @cond SKIP_IN_MANUAL
// This variant creates a default point property map =
Dereference_property_map.
template <typename ForwardIterator
>
ForwardIterator
grid_simplify_point_set(
ForwardIterator first, ///< iterator over the first input point
ForwardIterator beyond, ///< past-the-end iterator
double epsilon) ///< tolerance value when merging 3D points
{
return grid_simplify_point_set( first,beyond,
make_dereference_property_map(first), epsilon);
}
/// @endcond
CGAL_END_NAMESPACE
#endif // CGAL_GRID_SIMPLIFY_POINT_SET_H
//// Copyright (c) 2007-09 INRIA Sophia-Antipolis (France).
//// All rights reserved.
////
//// This file is part of CGAL (www.cgal.org); you may redistribute point_it
under
//// the terms of the Q Public License version 1.0.
//// See the file LICENSE.QPL distributed with CGAL.
////
//// Licensees holding a valid commercial license may use this file in
//// accordance with the commercial license agreement provided with the
software.
////
//// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
//// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
////
//// $URL:
svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Point_set_processing_3/include/CGAL/grid_simplify_point_set.h
$
//// $Id: grid_simplify_point_set.h 49943 2009-06-17 07:49:35Z lsaboret $
////
//// Author(s) : Laurent Saboret and Nader Salman
//
//#ifndef CGAL_GRID_SIMPLIFY_POINT_SET_H
//#define CGAL_GRID_SIMPLIFY_POINT_SET_H
//
//#include <CGAL/property_map.h>
//#include <CGAL/point_set_processing_assertions.h>
//
//#include <iterator>
//#include <set>
//#include <deque>
//#include <algorithm>
//#include <cmath>
//
//CGAL_BEGIN_NAMESPACE
//
//
////
----------------------------------------------------------------------------
//// Private section
////
----------------------------------------------------------------------------
//namespace CGALi {
//
//
///// Utility class for grid_simplify_point_set():
///// Less_epsilon_points_3 defines a 3D points order / 2 points are equal
///// iff they belong to the same cell of a grid of cell size = epsilon.
//template <class Point_3>
//struct Less_epsilon_points_3
//{
//private:
//
// double m_epsilon;
//
//public:
//
// Less_epsilon_points_3 (double epsilon)
// : m_epsilon (epsilon)
// {
// CGAL_point_set_processing_precondition(epsilon > 0);
// }
//
// bool operator() (const Point_3& a, const Point_3& b) const
// {
// // Round points to multiples of m_epsilon, then compare.
// Point_3 rounded_a(round_epsilon(a.x(), m_epsilon),
// round_epsilon(a.y(), m_epsilon),
// round_epsilon(a.z(), m_epsilon));
// Point_3 rounded_b(round_epsilon(b.x(), m_epsilon),
// round_epsilon(b.y(), m_epsilon),
// round_epsilon(b.z(), m_epsilon));
// return (rounded_a < rounded_b);
// }
//
//private:
//
// // Round number to multiples of epsilon
// static inline double round_epsilon(double value, double epsilon)
// {
// return std::floor(value/epsilon) * epsilon;
// }
//};
//
//
//} /* namespace CGALi */
//
//
////
----------------------------------------------------------------------------
//// Public section
////
----------------------------------------------------------------------------
//
//
///// Utility class for grid_simplify_point_set():
///// 3D points set which allows at most 1 point per cell
///// of a grid of cell size = epsilon.
/////
///// Warning:
///// This class is a container sorted wrt points position
///// => you should not modify directly the order or the position of points.
//
//template <class Point_3>
//class Epsilon_point_set_3
// : public std::set<Point_3, CGALi::Less_epsilon_points_3<Point_3> >
//{
//private:
//
// // superclass
// typedef std::set<Point_3, CGALi::Less_epsilon_points_3<Point_3> > Base;
//
//public:
//
// Epsilon_point_set_3 (double epsilon)
// : Base( CGALi::Less_epsilon_points_3<Point_3>(epsilon) )
// {
// CGAL_point_set_processing_precondition(epsilon > 0);
// }
//
// // default copy constructor, operator =() and destructor are fine.
//};
//
//
///// Merges points which belong to the same cell of a grid of cell size =
epsilon.
/////
///// This method modifies the order of input points so as to pack all
remaining points first,
///// and returns an iterator over the first point to remove (see
erase-remove idiom).
///// For this reason it should not be called on sorted containers.
/////
///// @commentheading Precondition: epsilon > 0.
/////
///// @commentheading Template Parameters:
///// @param ForwardIterator iterator over input points.
///// @param PointPMap is a model of boost::ReadablePropertyMap with a
value_type = Point_3<Kernel>.
///// It can be omitted if ForwardIterator value_type is convertible
to Point_3<Kernel>.
///// @param Kernel Geometric traits class.
///// It can be omitted and deduced automatically from PointPMap
value_type.
/////
///// @return iterator over the first point to remove.
//
//// This variant requires all parameters.
//template <typename ForwardIterator,
// typename PointPMap,
// typename Kernel
//>
//ForwardIterator
//grid_simplify_point_set(
// ForwardIterator first, ///< iterator over the first input point.
// ForwardIterator beyond, ///< past-the-end iterator over the input points.
// PointPMap point_pmap, ///< property map ForwardIterator -> Point_3
// double epsilon, ///< tolerance value when merging 3D points.
// const Kernel& kernel) ///< geometric traits.
//{
// // actual type of input points
// typedef typename std::iterator_traits<ForwardIterator>::value_type
Enriched_point;
//
// CGAL_point_set_processing_precondition(epsilon > 0);
//
// // Merges points which belong to the same cell of a grid of cell size =
epsilon.
// // points_to_keep[] will contain 1 point per cell; the others will be in
points_to_remove[].
// Epsilon_point_set_3<Enriched_point> points_to_keep(epsilon);
// std::deque<Enriched_point> points_to_remove;
// for (ForwardIterator it=first ; it != beyond ; it++)
// {
// std::pair<typename
Epsilon_point_set_3<Enriched_point>::iterator,bool> result;
// result = points_to_keep.insert(*it);
// if (!result.second) // if not inserted
// points_to_remove.push_back(*it);
// }
//
// // Replaces [first, beyond) range by the content of points_to_keep, then
points_to_remove.
// ForwardIterator first_point_to_remove =
// std::copy(points_to_keep.begin(), points_to_keep.end(), first);
// std::copy(points_to_remove.begin(), points_to_remove.end(),
first_point_to_remove);
//
// return first_point_to_remove;
//}
//
///// @cond SKIP_IN_MANUAL
//// This variant deduces the kernel from the iterator type.
//template <typename ForwardIterator,
// typename PointPMap
//>
//ForwardIterator
//grid_simplify_point_set(
// ForwardIterator first, ///< iterator over the first input point
// ForwardIterator beyond, ///< past-the-end iterator
// PointPMap point_pmap, ///< property map ForwardIterator -> Point_3
// double epsilon) ///< tolerance value when merging 3D points
//{
// typedef typename boost::property_traits<PointPMap>::value_type Point;
// typedef typename Kernel_traits<Point>::Kernel Kernel;
// return grid_simplify_point_set(
// first,beyond,
// point_pmap,
// epsilon,
// Kernel());
//}
///// @endcond
//
///// @cond SKIP_IN_MANUAL
//// This variant creates a default point property map =
Dereference_property_map.
//template <typename ForwardIterator
//>
//ForwardIterator
//grid_simplify_point_set(
// ForwardIterator first, ///< iterator over the first input point
// ForwardIterator beyond, ///< past-the-end iterator
// double epsilon) ///< tolerance value when merging 3D points
//{
// return grid_simplify_point_set(
// first,beyond,
// make_dereference_property_map(first),
// epsilon);
//}
///// @endcond
//
//
//CGAL_END_NAMESPACE
//
//#endif // CGAL_GRID_SIMPLIFY_POINT_SET_H
//
- [cgal-discuss] error in using CGAL::grid_simplify_point_set, tina rumi, 12/08/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, Nader SALMAN, 12/08/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, Nader SALMAN, 12/08/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, tina rumi, 12/08/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, tina rumi, 12/08/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, Laurent Saboret, 12/09/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, Nader Salman, 12/09/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, tina rumi, 12/09/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, Nader SALMAN, 12/10/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, tina rumi, 12/10/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, Nader SALMAN, 12/11/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, tina rumi, 12/11/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, tina rumi, 12/09/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, Nader Salman, 12/09/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, tina rumi, 12/08/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, Nader SALMAN, 12/08/2009
- Re: [cgal-discuss] error in using CGAL::grid_simplify_point_set, Nader SALMAN, 12/08/2009
Archive powered by MHonArc 2.6.16.