Skip to Content.
Sympa Menu

cgal-discuss - [cgal-discuss] using a Point class from another library

Subject: CGAL users discussion list

List archive

[cgal-discuss] using a Point class from another library


Chronological Thread 
  • From: "Tomislav Maric" <>
  • To:
  • Subject: [cgal-discuss] using a Point class from another library
  • Date: Tue, 24 Apr 2012 10:01:00 +0200

I have managed to make Polyhedron work with my Point class, but when I want
to read it from an .off file,

this function:

template < class Point> inline
Point&
file_scan_vertex( File_scanner_OFF& scanner, Point& p) {
   typedef typename Point::R R;
   typedef typename R::RT    RT;
   double x, y, z, w;
   scanner.scan_vertex( x, y, z, w);
   if ( w == 1)
       p = Point( RT(x), RT(y), RT(z));
   else
       p = Point( RT(x), RT(y), RT(z), RT(w));
   return p;
}

from the file "File_scanner_OFF.h" expects my point to have a constructor
with 4 arguments. For a simple
example Point class that I wrote, I have added this without a problem, but if
a Point class is coming from another
library, it is a problem. I cannot change the library Point class. What
should I do?

Option 1) Inherit from the Point class of the other library and add a dummy
constructor, counting on
         implicit casting every time i call a method on the Point to cast it
up?
      2) Change the .off File_scanner and adapt it somehow?

      ?) ?

I don't need homogeneous coordinate representation of points, so this kind of
constructor is not necessary..

Working code is attached.

Thank you!

Tomislav
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polyhedron_3.h>
#include <iostream>

#include "point.hpp"

#include<fstream>
#include <CGAL/IO/Polyhedron_iostream.h>

// A face type with a color member variable.
template <class Refs>
struct My_face : public CGAL::HalfedgeDS_face_base<Refs> {
    CGAL::Color color;
};

template <class Refs, class T, class Point>
struct My_vertex: public CGAL::HalfedgeDS_vertex_base<Refs, T, Point> 
{
    double x;

    My_vertex()
    :
        CGAL::HalfedgeDS_vertex_base<Refs, T, Point> ()
    {};

    My_vertex(const Point& p)
    :
        CGAL::HalfedgeDS_vertex_base<Refs, T, Point>(p)
    {
    };

    My_vertex(const Point& p, double X)
    :
        CGAL::HalfedgeDS_vertex_base<Refs, T, Point>(p), 
        x(X)
    {};

};


// An items type using my face.
struct My_items : public CGAL::Polyhedron_items_3 {

    template <class Refs, class Traits>
    struct Face_wrapper {
        typedef My_face<Refs> Face;
    };

    template <class Refs, class Traits>
    struct Vertex_wrapper {
        typedef typename Traits::Point_3 Point;
        typedef My_vertex<Refs, CGAL::Tag_true, Point> Vertex;
    };
};


// Define the PolyhedronTraits_3 concept and take the Point directly from point.hpp
template <class Kernel>
struct My_Polyhedron_traits_3
:
    public CGAL::Polyhedron_traits_3<Kernel>
{
    // Redefine Point_3 to be my Point<double>
    typedef Point<double> Point_3;
    typedef typename Kernel::Plane_3 Plane_3;

    My_Polyhedron_traits_3()
    :
        CGAL::Polyhedron_traits_3<Kernel>()
    {};

    My_Polyhedron_traits_3(Kernel kernel)
    :
        CGAL::Polyhedron_traits_3<Kernel>(kernel)
    {};

    My_Polyhedron_traits_3(const My_Polyhedron_traits_3& r)
    :
        CGAL::Polyhedron_traits_3<Kernel>(r)
    {}

    My_Polyhedron_traits_3& operator=(const My_Polyhedron_traits_3& r)
    {
        CGAL::Polyhedron_traits_3<Kernel>::operator=(r);
    }
};

    


typedef CGAL::Simple_cartesian<double>        Kernel;
//typedef CGAL::Polyhedron_3<Kernel, My_items>  Polyhedron;
typedef CGAL::Polyhedron_3<My_Polyhedron_traits_3<Kernel>, My_items>  Polyhedron;
typedef Polyhedron::Halfedge_handle           Halfedge_handle;

//typedef CGAL::Simple_cartesian<double>               Kernel;
typedef Kernel::Point_3                              Point_3;
//typedef CGAL::Polyhedron_3<Kernel>                   Polyhedron;
typedef Polyhedron::Facet_iterator                   Facet_iterator;
typedef Polyhedron::Halfedge_around_facet_circulator Halfedge_facet_circulator;

int main() {

    // Kernel::Point_3
    //Point_3 p( 0.0, 0.0, 0.0);
    //Point_3 q( 1.0, 0.0, 0.0);
    //Point_3 r( 0.0, 1.0, 0.0);
    //Point_3 s( 0.0, 0.0, 1.0);
    
    typedef Point<double> Point;

    Point p( 0.0, 0.0, 0.0);
    Point q( 1.0, 0.0, 0.0);
    Point r( 0.0, 1.0, 0.0);
    Point s( 0.0, 0.0, 1.0);

    //Polyhedron P;
    //P.make_tetrahedron( p, q, r, s);
    
    Polyhedron P;

    std::ifstream is ("input.off");
    is >> P;


    // Write polyhedron in Object File Format (OFF).
    //CGAL::set_ascii_mode( std::cout);
    std::cout << "OFF" << std::endl << P.size_of_vertices() << ' '
              << P.size_of_facets() << " 0" << std::endl;
    std::copy( P.points_begin(), P.points_end(),
               std::ostream_iterator< Point >( std::cout, "\n"));
    for (  Facet_iterator i = P.facets_begin(); i != P.facets_end(); ++i) {
        Halfedge_facet_circulator j = i->facet_begin();
        // Facets in polyhedral surfaces are at least triangles.
        CGAL_assertion( CGAL::circulator_size(j) >= 3);
        std::cout << CGAL::circulator_size(j) << ' ';
        do {
            std::cout << ' ' << std::distance(P.vertices_begin(), j->vertex());
        } while ( ++j != i->facet_begin());
        std::cout << std::endl;
    }
    return 0;
}
#ifndef POINT_HPP
#define POINT_HPP

template <class T>
class Point;

template<class T>
std::ostream& operator << (std::ostream& os, const Point<T>& v);
template<class T>
std::istream& operator >> (std::istream& is, const Point<T>& v);

template<class T>
class Point 
{
    private:
        T v_[3];

    public:

        struct R
        {
            typedef T RT;
        };

        Point()
            :
                v_()
        {

        }

        Point(T c1, T c2, T c3=0)
        {

            v_[0] = c1;
            v_[1] = c2;
            v_[2] = c3;
        }

        Point(T c1, T c2, T c3, T c4)
        {
            // Homogeneous coordinates.
        }

        Point(const Point& v)
        {
            v_[0] = v.v_[0];
            v_[1] = v.v_[1];
            v_[2] = v.v_[2];
        }
        
        const Point& operator = (const Point& v)
        {

            v_[0] = v.v_[0];
            v_[1] = v.v_[1];
            v_[2] = v.v_[2];

            return *this;
        }

        const Point& operator + (const Point& v)
        {
            v_[0] = v_[0]+ v.v_[0];
            v_[1] = v_[1]+ v.v_[1];
            v_[2] = v_[2]+ v.v_[2];

            return *this;
        }

        const Point& operator += (const Point& v)
        {
            return this->operator+(v);
        }

        const Point& operator - (const Point& v)
        {
            v_[0] = v_[0]- v.v_[0];
            v_[1] = v_[1]- v.v_[1];
            v_[2] = v_[2]- v.v_[2];

            return *this;
        }

        const Point& operator -= (const Point& v)
        {
            return this->operator-(v);
        }

        friend std::ostream& operator << <> (std::ostream& , 
                                             const Point<T>&);

        friend std::istream& operator >> <> (std::istream& , 
                                             const Point<T>&);
};

template<class T>
std::ostream& operator << (std::ostream& os, const Point<T>& v)
{
    os  << v.v_[0] << " " << v.v_[1] << " " << v.v_[2];
    return os;
}

template<class T>
std::istream& operator >> (std::istream& is, const Point<T>& v)
{
    is  >> v.v_[0] >> v.v_[1] >> v.v_[2];
    return is;
}


#endif



Archive powered by MHonArc 2.6.16.

Top of Page