对于在 CGAL4.2 中成功编译的代码,CGAL4.6 中的编译错误
compile error in CGAL4.6 for a code that successfully compiles in CGAL4.2
我使用 CGAL4.2 成功编译并执行了以下代码。
cgal_intersection.h
typedef struct {double x; double y;} coordinate;
int cgal();
extern "C" int find_intersections_of_curves(
coordinate *curve_1, long curve_1_size,
coordinate *curve_2, long curve_2_size,
coordinate *intersections, int max_num_of_intersections);
cgal_intersection.cpp
#include <CGAL/Cartesian.h>
#include <CGAL/Arr_segment_traits_2.h>
#include <CGAL/Arr_polyline_traits_2.h>
#include <CGAL/Sweep_line_2_algorithms.h>
#include <vector>
#include <list>
#include "cgal_intersection.h"
typedef CGAL::Cartesian<double> Kernel;
typedef CGAL::Arr_segment_traits_2<Kernel> Segment_traits;
typedef CGAL::Arr_polyline_traits_2<Segment_traits> Traits;
typedef Traits::Point_2 Point;
typedef Traits::Curve_2 Polyline;
extern "C" int find_intersections_of_curves(
coordinate *curve_1, long curve_1_size,
coordinate *curve_2, long curve_2_size,
coordinate *intersections, int max_num_of_intersections)
{
std::vector<Point> points_1(curve_1_size);
for (long i_1 = 0; i_1 < curve_1_size; i_1++)
points_1[i_1] = Point(curve_1[i_1].x, curve_1[i_1].y);
Polyline polyline_1(points_1.begin(), points_1.end());
std::vector<Point> points_2(curve_2_size);
for (long i_2 = 0; i_2 < curve_2_size; i_2++)
points_2[i_2] = Point(curve_2[i_2].x, curve_2[i_2].y);
Polyline polyline_2(points_2.begin(), points_2.end());
Polyline polylines[] = {polyline_1, polyline_2};
std::list<Point> intersection_points;
CGAL::compute_intersection_points(polylines, polylines + 2,
std::back_inserter(intersection_points));
int num_of_intersections = intersection_points.size();
Point intersection_point;
for (int j = 0; j < num_of_intersections; j++){
if (j == max_num_of_intersections)
break;
intersection_point = intersection_points.front();
intersections[j].x = intersection_point.x();
intersections[j].y = intersection_point.y();
intersection_points.pop_front();
}
return num_of_intersections;
}
但是在使用CGAL4.6时,出现如下编译错误:
Scanning dependencies of target cgal_intersection
[100%] Building CXX object CMakeFiles/cgal_intersection.dir/cgal_intersection.cpp.o
In file included from /home/chan/mose/mose/cgal_intersection/cgal_intersection.cpp:5:0:
/usr/include/CGAL/Sweep_line_2_algorithms.h: In instantiation of ‘OutputIterator CGAL::compute_intersection_points(CurveInputIterator, CurveInputIterator, OutputIterator, bool) [with CurveInputIterator = CGAL::polyline::Polyline_2<CGAL::Arr_segment_2<CGAL::Cartesian<double> >, CGAL::Point_2<CGAL::Cartesian<double> > >*; OutputIterator = std::back_insert_iterator<std::list<CGAL::Point_2<CGAL::Cartesian<double> > > >]’:
/home/chan/mose/mose/cgal_intersection/cgal_intersection.cpp:36:78: required from here
/usr/include/CGAL/Sweep_line_2_algorithms.h:134:48: error: no type named ‘Traits’ in ‘struct CGAL::Default_arr_traits<CGAL::polyline::Polyline_2<CGAL::Arr_segment_2<CGAL::Cartesian<double> >, CGAL::Point_2<CGAL::Cartesian<double> > > >’
typename Default_arr_traits<Curve>::Traits traits;
^
/usr/include/CGAL/Sweep_line_2_algorithms.h:134:48: error: no type named ‘Traits’ in ‘struct CGAL::Default_arr_traits<CGAL::polyline::Polyline_2<CGAL::Arr_segment_2<CGAL::Cartesian<double> >, CGAL::Point_2<CGAL::Cartesian<double> > > >’
make[2]: *** [CMakeFiles/cgal_intersection.dir/cgal_intersection.cpp.o] Error 1
make[1]: *** [CMakeFiles/cgal_intersection.dir/all] Error 2
make: *** [all] Error 2
我查看了头文件,但不明白为什么它在 CGAL4.2 中有效但在 CGAL4.6 中失败。如有任何帮助,我将不胜感激。
经过两次修改,我成功编译了代码。
原来是Arr_polyline_traits_2
的API从CGAL4.2
变成了CGAL4.4
明显变了。例如,当使用CGAL4.6.1
时,应该使用Construct_curve_2
构造折线,如示例代码Arrangement_on_surface_2/polylines.cpp
.
中所述
compute_intersection_points
有两个重载定义。在这里,应该明确指定正在使用哪个特征,即应该使用 API 描述的:http://doc.cgal.org/latest/Sweep_line_2/group__PkgIntersectionOfCurves2.html#ga69bf75d5c190cf566c1f6eba93a20a29.
合并上述修改后的代码如下:
// Computing intersection points among curves using the sweep line.
#include <CGAL/Cartesian.h>
#include <CGAL/Arr_segment_traits_2.h>
#include <CGAL/Arr_polyline_traits_2.h>
#include <CGAL/Sweep_line_2_algorithms.h>
#include <vector>
#include <list>
#include "cgal_intersection.h"
typedef CGAL::Cartesian<double> Kernel;
typedef CGAL::Arr_segment_traits_2<Kernel> Segment_traits;
typedef CGAL::Arr_polyline_traits_2<Segment_traits> Polyline_traits;
typedef Polyline_traits::Point_2 Point;
typedef Polyline_traits::Curve_2 Polyline;
extern "C" int find_intersections_of_curves(
coordinate *curve_1, long curve_1_size,
coordinate *curve_2, long curve_2_size,
coordinate *intersections, int max_num_of_intersections)
{
Polyline_traits polyline_traits;
Polyline_traits::Construct_curve_2 construct_polyline =
polyline_traits.construct_curve_2_object();
int num_of_curves = 2;
coordinate *curves[] = {curve_1, curve_2};
long size_of_curve[] = {curve_1_size, curve_2_size};
std::vector<Polyline> polylines;
for(int n = 0; n < num_of_curves; n++){
std::vector<Point> points;
Point p_0 = Point(curves[n][0].x, curves[n][0].y);
points.push_back(p_0);
for(long i = 1; i < size_of_curve[n]; i++){
Point p_i = Point(curves[n][i].x, curves[n][i].y);
if(p_i != points.back()){
points.push_back(p_i);
}
}
polylines.push_back(construct_polyline(points.begin(), points.end()));
}
std::list<Point> intersection_points;
CGAL::compute_intersection_points(polylines.begin(), polylines.end(),
std::back_inserter(intersection_points),
false, polyline_traits);
int num_of_intersections = intersection_points.size();
Point intersection_point;
for (int j = 0; j < num_of_intersections; j++){
if (j == max_num_of_intersections)
break;
intersection_point = intersection_points.front();
intersections[j].x = intersection_point.x();
intersections[j].y = intersection_point.y();
intersection_points.pop_front();
}
return num_of_intersections;
}
我使用 CGAL4.2 成功编译并执行了以下代码。
cgal_intersection.h
typedef struct {double x; double y;} coordinate;
int cgal();
extern "C" int find_intersections_of_curves(
coordinate *curve_1, long curve_1_size,
coordinate *curve_2, long curve_2_size,
coordinate *intersections, int max_num_of_intersections);
cgal_intersection.cpp
#include <CGAL/Cartesian.h>
#include <CGAL/Arr_segment_traits_2.h>
#include <CGAL/Arr_polyline_traits_2.h>
#include <CGAL/Sweep_line_2_algorithms.h>
#include <vector>
#include <list>
#include "cgal_intersection.h"
typedef CGAL::Cartesian<double> Kernel;
typedef CGAL::Arr_segment_traits_2<Kernel> Segment_traits;
typedef CGAL::Arr_polyline_traits_2<Segment_traits> Traits;
typedef Traits::Point_2 Point;
typedef Traits::Curve_2 Polyline;
extern "C" int find_intersections_of_curves(
coordinate *curve_1, long curve_1_size,
coordinate *curve_2, long curve_2_size,
coordinate *intersections, int max_num_of_intersections)
{
std::vector<Point> points_1(curve_1_size);
for (long i_1 = 0; i_1 < curve_1_size; i_1++)
points_1[i_1] = Point(curve_1[i_1].x, curve_1[i_1].y);
Polyline polyline_1(points_1.begin(), points_1.end());
std::vector<Point> points_2(curve_2_size);
for (long i_2 = 0; i_2 < curve_2_size; i_2++)
points_2[i_2] = Point(curve_2[i_2].x, curve_2[i_2].y);
Polyline polyline_2(points_2.begin(), points_2.end());
Polyline polylines[] = {polyline_1, polyline_2};
std::list<Point> intersection_points;
CGAL::compute_intersection_points(polylines, polylines + 2,
std::back_inserter(intersection_points));
int num_of_intersections = intersection_points.size();
Point intersection_point;
for (int j = 0; j < num_of_intersections; j++){
if (j == max_num_of_intersections)
break;
intersection_point = intersection_points.front();
intersections[j].x = intersection_point.x();
intersections[j].y = intersection_point.y();
intersection_points.pop_front();
}
return num_of_intersections;
}
但是在使用CGAL4.6时,出现如下编译错误:
Scanning dependencies of target cgal_intersection
[100%] Building CXX object CMakeFiles/cgal_intersection.dir/cgal_intersection.cpp.o
In file included from /home/chan/mose/mose/cgal_intersection/cgal_intersection.cpp:5:0:
/usr/include/CGAL/Sweep_line_2_algorithms.h: In instantiation of ‘OutputIterator CGAL::compute_intersection_points(CurveInputIterator, CurveInputIterator, OutputIterator, bool) [with CurveInputIterator = CGAL::polyline::Polyline_2<CGAL::Arr_segment_2<CGAL::Cartesian<double> >, CGAL::Point_2<CGAL::Cartesian<double> > >*; OutputIterator = std::back_insert_iterator<std::list<CGAL::Point_2<CGAL::Cartesian<double> > > >]’:
/home/chan/mose/mose/cgal_intersection/cgal_intersection.cpp:36:78: required from here
/usr/include/CGAL/Sweep_line_2_algorithms.h:134:48: error: no type named ‘Traits’ in ‘struct CGAL::Default_arr_traits<CGAL::polyline::Polyline_2<CGAL::Arr_segment_2<CGAL::Cartesian<double> >, CGAL::Point_2<CGAL::Cartesian<double> > > >’
typename Default_arr_traits<Curve>::Traits traits;
^
/usr/include/CGAL/Sweep_line_2_algorithms.h:134:48: error: no type named ‘Traits’ in ‘struct CGAL::Default_arr_traits<CGAL::polyline::Polyline_2<CGAL::Arr_segment_2<CGAL::Cartesian<double> >, CGAL::Point_2<CGAL::Cartesian<double> > > >’
make[2]: *** [CMakeFiles/cgal_intersection.dir/cgal_intersection.cpp.o] Error 1
make[1]: *** [CMakeFiles/cgal_intersection.dir/all] Error 2
make: *** [all] Error 2
我查看了头文件,但不明白为什么它在 CGAL4.2 中有效但在 CGAL4.6 中失败。如有任何帮助,我将不胜感激。
经过两次修改,我成功编译了代码。
原来是
Arr_polyline_traits_2
的API从CGAL4.2
变成了CGAL4.4
明显变了。例如,当使用CGAL4.6.1
时,应该使用Construct_curve_2
构造折线,如示例代码Arrangement_on_surface_2/polylines.cpp
. 中所述
compute_intersection_points
有两个重载定义。在这里,应该明确指定正在使用哪个特征,即应该使用 API 描述的:http://doc.cgal.org/latest/Sweep_line_2/group__PkgIntersectionOfCurves2.html#ga69bf75d5c190cf566c1f6eba93a20a29.
合并上述修改后的代码如下:
// Computing intersection points among curves using the sweep line.
#include <CGAL/Cartesian.h>
#include <CGAL/Arr_segment_traits_2.h>
#include <CGAL/Arr_polyline_traits_2.h>
#include <CGAL/Sweep_line_2_algorithms.h>
#include <vector>
#include <list>
#include "cgal_intersection.h"
typedef CGAL::Cartesian<double> Kernel;
typedef CGAL::Arr_segment_traits_2<Kernel> Segment_traits;
typedef CGAL::Arr_polyline_traits_2<Segment_traits> Polyline_traits;
typedef Polyline_traits::Point_2 Point;
typedef Polyline_traits::Curve_2 Polyline;
extern "C" int find_intersections_of_curves(
coordinate *curve_1, long curve_1_size,
coordinate *curve_2, long curve_2_size,
coordinate *intersections, int max_num_of_intersections)
{
Polyline_traits polyline_traits;
Polyline_traits::Construct_curve_2 construct_polyline =
polyline_traits.construct_curve_2_object();
int num_of_curves = 2;
coordinate *curves[] = {curve_1, curve_2};
long size_of_curve[] = {curve_1_size, curve_2_size};
std::vector<Polyline> polylines;
for(int n = 0; n < num_of_curves; n++){
std::vector<Point> points;
Point p_0 = Point(curves[n][0].x, curves[n][0].y);
points.push_back(p_0);
for(long i = 1; i < size_of_curve[n]; i++){
Point p_i = Point(curves[n][i].x, curves[n][i].y);
if(p_i != points.back()){
points.push_back(p_i);
}
}
polylines.push_back(construct_polyline(points.begin(), points.end()));
}
std::list<Point> intersection_points;
CGAL::compute_intersection_points(polylines.begin(), polylines.end(),
std::back_inserter(intersection_points),
false, polyline_traits);
int num_of_intersections = intersection_points.size();
Point intersection_point;
for (int j = 0; j < num_of_intersections; j++){
if (j == max_num_of_intersections)
break;
intersection_point = intersection_points.front();
intersections[j].x = intersection_point.x();
intersections[j].y = intersection_point.y();
intersection_points.pop_front();
}
return num_of_intersections;
}