CGAL 6.2 - Shape Detection

Loading...
Searching...
No Matches
Shape_detection/include/efficient_RANSAC_with_custom_shape.h
#ifndef MY_PLANE_SHAPE_H
#define MY_PLANE_SHAPE_H
#include <CGAL/number_utils.h>
#include <CGAL/Shape_detection/Efficient_RANSAC.h>
// My_Plane is derived from Shape_base. The plane is represented by
// its normal vector and distance to the origin.
template <class Traits>
class My_Plane : public CGAL::Shape_detection::Shape_base<Traits> {
public:
typedef typename Traits::FT FT; // number type
typedef typename Traits::Point_3 Point; // point type
typedef typename Traits::Vector_3 Vector; // vector type
My_Plane() :
CGAL::Shape_detection::Shape_base<Traits>()
{ }
// Compute squared Euclidean distance from query point to the shape.
virtual FT squared_distance(const Point& p) const {
const FT sd = (this->constr_vec(m_point_on_primitive, p)) * m_normal;
return sd * sd;
}
Vector plane_normal() const {
return m_normal;
}
FT d() const {
return m_d;
}
// Return a string with shape parameters.
virtual std::string info() const {
std::stringstream sstr;
sstr << "Type: plane (" << this->get_x(m_normal) << ", "
<< this->get_y(m_normal) << ", " << this->get_z(m_normal) << ")x - " <<
m_d << " = 0" << " #Pts: " << this->m_indices.size();
return sstr.str();
}
protected:
// Construct shape base on a minimal set of samples from the input data.
virtual void create_shape(const std::vector<std::size_t>& indices) {
const Point p1 = this->point(indices[0]);
const Point p2 = this->point(indices[1]);
const Point p3 = this->point(indices[2]);
m_normal = this->cross_pdct(p1 - p2, p1 - p3);
m_normal = m_normal * (1.0 / sqrt(this->sqlen(m_normal)));
m_d = -(p1[0] * m_normal[0] + p1[1] * m_normal[1] + p1[2] * m_normal[2]);
m_point_on_primitive = p1;
this->m_is_valid = true;
}
// Compute squared Euclidean distance from a set of points.
virtual void squared_distance(
const std::vector<std::size_t>& indices,
std::vector<FT>& dists) const {
for (std::size_t i = 0; i < indices.size(); ++i) {
const FT sd = (this->point(indices[i]) - m_point_on_primitive) * m_normal;
dists[i] = sd * sd;
}
}
// Compute the normal deviation between a shape and
// a set of points with normals.
virtual void cos_to_normal(
const std::vector<std::size_t>& indices,
std::vector<FT>& angles) const {
for (std::size_t i = 0; i < indices.size(); ++i)
angles[i] = CGAL::abs(this->normal(indices[i]) * m_normal);
}
// Return the number of required samples for construction.
virtual std::size_t minimum_sample_size() const {
return 3;
}
private:
Point m_point_on_primitive;
Vector m_normal;
FT m_d;
};
#endif // MY_PLANE_SHAPE_H
Efficient_RANSAC.h
A convenience header that includes all classes related to the efficient RANSAC algorithm.
CGAL::Shape_detection::Shape_base
Base class for shape types that defines an interface to construct a shape from a set of points and to...
Definition: Shape_base.h:49
CGAL::Shape_detection::Shape_base::point
boost::property_traits< typenameTraits::Point_map >::reference point(std::size_t i) const
Retrieves the point location from its index.
Definition: Shape_base.h:376
CGAL::Shape_detection::Shape_base::cos_to_normal
virtual void cos_to_normal(const std::vector< std::size_t > &indices, std::vector< FT > &angles) const =0
Computes the deviation of the point normal from the surface normal at the projected point in form of ...
CGAL::Shape_detection::Shape_base::normal
boost::property_traits< typenameTraits::Normal_map >::reference normal(std::size_t i) const
Retrieves the normal vector from its index.
Definition: Shape_base.h:384
CGAL::Shape_detection::Shape_base::create_shape
virtual void create_shape(const std::vector< std::size_t > &indices)=0
Constructs the shape based on a minimal set of samples from the input data.
CGAL::Shape_detection::Shape_base::FT
Traits::FT FT
Number type.
Definition: Shape_base.h:69
CGAL::Shape_detection::Shape_base::squared_distance
virtual FT squared_distance(const Point_3 &p) const =0
Computes the squared Euclidean distance from the query point p to the shape.
CGAL::Shape_detection::Shape_base::info
virtual std::string info() const
Returns a string containing the shape type and the numerical parameters.
Definition: Shape_base.h:96
CGAL::Shape_detection::Shape_base::minimum_sample_size
virtual std::size_t minimum_sample_size() const =0
Returns minimal number of sample points required for construction.
CGAL::abs
NT abs(const NT &x)
CGAL::sqrt
NT sqrt(const NT &x)