1 // Copyright 2009,2014 Max-Planck-Institute Saarbruecken (Germany).
2 // All rights reserved.
3 //
4 // This file is part of CGAL (www.cgal.org).
5 //
6 // $URL$
7 // $Id$
8 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
9 //
10 //
11 // author(s)     : Waqar Khan <wkhan@mpi-inf.mpg.de>
12 
13 
14 /* Usage
15  *
16  *  This example converts arbitrary-precision arrangment into fixed-precision using Snap Rounding and by using INPUT DATA FROM A USER SPECIFIED FILE.
17  *  <Argument 1> (Mandatory) path to the input file containing the arrangment information.
18  *  <Argument 2> (Optional)  path to the output file where the results of snap rounding will be stored.
19  *                                                        Not providing this argument will print the result on standard output.
20  *
21  *  Input file format
22  *  Line # 1:                 Number of line-segments present in the file.
23  *  Line # 2 to N+1:        segment_start_point_x <space> segment_start_point_y <space> segment_end_point_x <space> segment_end_point_y
24  *
25  *  Each line should contain information about just one segment.
26 */
27 
28 #include <CGAL/Cartesian.h>
29 #include <CGAL/Quotient.h>
30 #include <CGAL/MP_Float.h>
31 #include <CGAL/Snap_rounding_traits_2.h>
32 #include <CGAL/Snap_rounding_2.h>
33 #include <fstream>
34 
35 typedef CGAL::Quotient<CGAL::MP_Float>           Number_type;
36 typedef CGAL::Cartesian<Number_type>             Kernel;
37 typedef CGAL::Snap_rounding_traits_2<Kernel>     Traits;
38 typedef Kernel::Segment_2                        Segment_2;
39 typedef Kernel::Point_2                          Point_2;
40 typedef std::list<Segment_2>                     Segment_list_2;
41 typedef std::list<Point_2>                       Polyline_2;
42 typedef std::list<Polyline_2>                    Polyline_list_2;
43 
main(int argc,char * argv[])44 int main(int argc, char* argv[])
45 {
46         //if(argc > 3 || argc < 2)
47         if(argc > 3)
48         {
49                 std::cout<< "Incorrect input. <Arg 1> path to the INPUT file. <Arg 2> (optional) path to the OUTPUT file. No arguments to choose the default data file" << std::endl;
50                 return -1;
51         }
52 
53         Segment_list_2 seg_list;
54         Polyline_list_2 output_list;
55 
56         std::ifstream my_read_file;
57         std::ofstream my_write_file;
58 
59         if(argc > 1)
60                 my_read_file.open(argv[1]);
61         else
62                 my_read_file.open("data/snap_rounding_data");
63 
64         if(!my_read_file.is_open())
65         {
66                 std::cout<< "Error opening the input file"<< std::endl;
67                 return -1;
68         }
69 
70         if(argc==3)
71         {
72                 my_write_file.open(argv[2]);
73 
74                 if(!my_read_file.is_open())
75                 {
76                         std::cout<< "Error opening the output file"<< std::endl;
77                         return -1;
78                 }
79         }
80 
81         unsigned int number_of_lines = 0;
82         my_read_file >> number_of_lines;
83 
84         for(unsigned int i=0; i<number_of_lines; i++)
85         {
86                 double point_start_x, point_start_y, point_end_x, point_end_y;
87                 my_read_file >> point_start_x;
88                 my_read_file >> point_start_y;
89                 my_read_file >> point_end_x;
90                 my_read_file >> point_end_y;
91 
92                 seg_list.push_back(Segment_2(Point_2(point_start_x, point_start_y), Point_2(point_end_x, point_end_y)));
93         }
94 
95         // Generate an iterated snap-rounding representation, where the centers of
96         // the hot pixels bear their original coordinates, using 1 kd trees:
97         CGAL::snap_rounding_2<Traits,Segment_list_2::const_iterator,Polyline_list_2>
98                                                                                   (seg_list.begin(), seg_list.end(), output_list, 1.0, true, false, 1);
99 
100          int counter = 0;
101         Polyline_list_2::const_iterator iter1;
102 
103         if(argc == 3) //output to the file
104         {
105                 for (iter1 = output_list.begin(); iter1 != output_list.end(); ++iter1)
106                 {
107                     my_write_file << "Polyline number " << ++counter << ":\n";
108                     Polyline_2::const_iterator iter2;
109 
110                     for (iter2 = iter1->begin(); iter2 != iter1->end(); ++iter2)
111                       my_write_file << "    (" << iter2->x() << ":" << iter2->y() << ")\n";
112                 }
113 
114                 my_write_file.close();
115         }
116         else //output to std output
117         {
118                 for (iter1 = output_list.begin(); iter1 != output_list.end(); ++iter1)
119                 {
120                     std::cout << "Polyline number " << ++counter << ":\n";
121                     Polyline_2::const_iterator iter2;
122 
123                     for (iter2 = iter1->begin(); iter2 != iter1->end(); ++iter2)
124                       std::cout << "    (" << iter2->x() << ":" << iter2->y() << ")\n";
125                 }
126         }
127 
128         my_read_file.close();
129 
130         return(0);
131 }
132