Constraints not recreated after writing / reading to / from file

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Constraints not recreated after writing / reading to / from file

malcolm
Hi,
I am using the file_output and file_input methods to write the and read the
triangulation data from a file. After the triangulation is read from the
file, I see the vertices are recreated but the constraints are not.

Here's my code:

#include <CGAL\Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Constrained_Delaunay_triangulation_2.h>
#include <CGAL/Triangulation_vertex_base_with_info_2.h>

#include <fstream>

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Exact_predicates_tag Itag;
typedef CGAL::Triangulation_vertex_base_2<Kernel> Vb;
typedef CGAL::Constrained_triangulation_face_base_2<Kernel> Fb;
typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds;
typedef CGAL::Constrained_Delaunay_triangulation_2<Kernel, Tds, Itag>
Delaunay;
typedef Kernel::Point_2 CGAL_Point;

int main()
{
        Delaunay T;
        auto v1 = T.insert(CGAL_Point(10, 13));
        auto v2 = T.insert(CGAL_Point(5, 43));
        auto v3 = T.insert(CGAL_Point(24, 3));

        T.insert_constraint(v1, v2);
        T.insert_constraint(v2, v3);

        // write triangulation data to file
        const char* filename = "Data.txt";
        std::filebuf fb;
        fb.open(filename, std::ios::out);
        std::ostream oFile(&fb);
        T.file_output(oFile);
        fb.close();

        // clear triangulation
        T.clear();

        //read triangulation data from the earlier file
        fb.open(filename, std::ios::in);
        std::istream iFile(&fb);
        T.file_input(iFile);
        fb.close();

        // print the vertex coordinates, this works fine
        for (auto itr = T.finite_vertices_begin();
                itr != T.finite_vertices_end();
                itr++)
        {
                printf("\nPoint: %f, %f", itr->point().x(), itr->point().y());
        }

        // the loop below is never entered
        for (auto itr = T.constrained_edges_begin();
                itr != T.constrained_edges_end();
                ++itr)
        {
                auto face = itr->first;
                auto sec = itr->second;
                auto v1 = face->vertex(face->ccw(sec));
                auto v2 = face->vertex(face->cw(sec));

                printf("\nConstraint point1: %f, %f", v1->point().x(), v1->point().y());
                printf("\nConstraint point2: %f, %f", v2->point().x(), v2->point().y());
        }
        return 0;
}


I have also checked the contents of the file. It reads:
4 4 2
10 13
5 43
24 3

0 2 3
2 1 3
1 0 3
1 2 0

1 2 3
2 0 3
0 1 3
0 2 1
C N N
N C C
N N N
N N C

Clearly the constrained edged are written to the file.

What am I missing? Why are these not recreated in the triangulation?



--
Sent from: http://cgal-discuss.949826.n4.nabble.com/

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss


Reply | Threaded
Open this post in threaded view
|

Re: Constraints not recreated after writing / reading to / from file

andreas.fabri

Hello,

You use undocumented functions.

You should use operator<<() and operator>>() instead.

Note that for Constrained_triangulation_plus_2 reading from a file does
not restore the hierarchy of constraints.

Best,

Andreas


On 4/22/2019 4:01 PM, malcolm wrote:
Hi,
I am using the file_output and file_input methods to write the and read the
triangulation data from a file. After the triangulation is read from the
file, I see the vertices are recreated but the constraints are not.

Here's my code:

#include <CGAL\Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Constrained_Delaunay_triangulation_2.h>
#include <CGAL/Triangulation_vertex_base_with_info_2.h>

#include <fstream>

typedef CGAL::Exact_predicates_inexact_constructions_kernel			Kernel;
typedef CGAL::Exact_predicates_tag								Itag;
typedef CGAL::Triangulation_vertex_base_2<Kernel>					Vb;
typedef CGAL::Constrained_triangulation_face_base_2<Kernel>			Fb;
typedef CGAL::Triangulation_data_structure_2<Vb, Fb>				Tds;
typedef CGAL::Constrained_Delaunay_triangulation_2<Kernel, Tds, Itag>
Delaunay;
typedef Kernel::Point_2										CGAL_Point;

int main()
{
	Delaunay T;
	auto v1 = T.insert(CGAL_Point(10, 13));
	auto v2 = T.insert(CGAL_Point(5, 43));
	auto v3 = T.insert(CGAL_Point(24, 3));

	T.insert_constraint(v1, v2);
	T.insert_constraint(v2, v3);

	// write triangulation data to file
	const char* filename = "Data.txt";
	std::filebuf fb;
	fb.open(filename, std::ios::out);
	std::ostream oFile(&fb);
	T.file_output(oFile);
	fb.close();

	// clear triangulation
	T.clear();

	//read triangulation data from the earlier file
	fb.open(filename, std::ios::in);
	std::istream iFile(&fb);
	T.file_input(iFile);
	fb.close();

	// print the vertex coordinates, this works fine
	for (auto itr = T.finite_vertices_begin();
		itr != T.finite_vertices_end();
		itr++)
	{
		printf("\nPoint: %f, %f", itr->point().x(), itr->point().y());
	}

	// the loop below is never entered
	for (auto itr = T.constrained_edges_begin();
		itr != T.constrained_edges_end();
		++itr)
	{
		auto face = itr->first;
		auto sec = itr->second;
		auto v1 = face->vertex(face->ccw(sec));
		auto v2 = face->vertex(face->cw(sec));

		printf("\nConstraint point1: %f, %f", v1->point().x(), v1->point().y());
		printf("\nConstraint point2: %f, %f", v2->point().x(), v2->point().y());
	}
	return 0;
}


I have also checked the contents of the file. It reads:
4 4 2
10 13
5 43
24 3

0 2 3 
2 1 3 
1 0 3 
1 2 0 

1 2 3 
2 0 3 
0 1 3 
0 2 1 
C N N
N C C
N N N
N N C

Clearly the constrained edged are written to the file.

What am I missing? Why are these not recreated in the triangulation?



--
Sent from: http://cgal-discuss.949826.n4.nabble.com/

-- 
Andreas Fabri, PhD
Chief Officer, GeometryFactory
Editor, The CGAL Project

phone: +33.492.954.912    skype: andreas.fabri
Reply | Threaded
Open this post in threaded view
|

Re: Constraints not recreated after writing / reading to / from file

malcolm
Thanks, Andreas. That works.

Cheers,
Malcolm



--
Sent from: http://cgal-discuss.949826.n4.nabble.com/

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss