Subject: CGAL users discussion list
List archive
[cgal-discuss] CGAL::difference for 2d polygon return a incorrect result polygon
Chronological Thread
- From: Luoyu <>
- To:
- Subject: [cgal-discuss] CGAL::difference for 2d polygon return a incorrect result polygon
- Date: Sun, 26 Apr 2020 05:14:05 -0500 (CDT)
- Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None ; spf=SoftFail ; spf=Pass
- Ironport-phdr: 9a23:G5FXGxQzv1coxpnypTEwEVIypdpsv+yvbD5Q0YIujvd0So/mwa69ZhGN2/xhgRfzUJnB7Loc0qyK6v2mBDNLu8rJmUtBWaQEbwUCh8QSkl5oK+++Imq/EsTXaTcnFt9JTl5v8iLzG0FUHMHjew+a+SXqvnYdFRrlKAV6OPn+FJLMgMSrzeCy/IDYbxlViDanbr5+MRW7oR/Ru8QUjoduNKk8wQbVr3VVfOhb2XlmLk+JkRbm4cew8p9j8yBOtP8k6sVNT6b0cbkmQLJBFDgpPHw768PttRnYUAuA/WAcXXkMkhpJGAfK8hf3VYrsvyTgt+p93C6aPdDqTb0xRD+v4btnRAPuhSwaMTMy7WPZhdFqjK9DrhyvpwJxzY3Jbo6aKPVwcbjQfc8YSGZdQspdSzBNDp26YoASD+QBJ+FYr4zlqlUIsBu+AgmtBP7ywTJPhn/22bA23/oiHA3Y0wEtH9MDvXTUodj1L6oSVv21zLXMzTXEaPNW2i3x55TPchAkuPyBW697f8TWyUkqDQzFj1OQpJTjPzyPzesCqGyb4PR6We2zjG4nrgd8qSWsyMc0koTFm4YYx1Te+Sh3w4s5P961RU9hbdOlEZZdsTyROZFsTcM4WW5ovT43yr0Ytp6/eygH0JInyhHFZ/yBaYeH+QnsVOKPLjtimH1lf7e/iw6z8Uim1OL8StG53EtOoydBiNXBuHMA2wbQ58WGUPdw/0as1S6K1w/J6+FEJU40lbDcK54k2rMwlp4TvVndEiL1hkn7g6+be0Qk9+Wo6+nqebLmpoKAN49wjQH+NacultajDuQ/NwgCR3Kb9vik1L3/4U35R61HgeE5kqbDtJDWPNkUpq+iAwBJz4Yj8A2/Aiy90NUYmHkHNEhKdAiGj4jvIVHOIer3Ae2xg1S2w39XwKXNMbTlR5nMNXPei6zJfLBn6kcaxhBg48pY4sdwF7YQPbqnWkuo7oyCUkJnblHp6+H6FNF60YBYUmWKVPzKeJjOuEOFs7p8a9KHY5UY7W6kdqoVosX2hHp8omczOKmk2ZxONSK+QrJgKk+TZXeqidAEQz5T4lgOCdfygVjHagZ9Im6oVvtntDYmFIahCoSFTYeo0uTYjXWLW6ZOb2UDMWiiVHLhdoGKQfAJMXnAJdJ9ljsDVv6qTIpzjhw=
Hi,
I'm newer to use cgal, I want to split the given polygon by another
polygon. Then I choose the CGAL::difference function. But in some cases,
it's function return incorrect result polygon for me.
Here is my code:
// TArray is a template for an array, FVector2D is a struct for a 2d point,
use float number type.
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Boolean_set_operations_2.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Polygon_set_2.h>
#include <list>
typedef CGAL::Exact_predicates_exact_constructions_kernel exactKernel;
typedef exactKernel::Point_2 exactPoint_2;
typedef CGAL::Polygon_2<exactKernel> exactPolygon_2;
typedef CGAL::Polygon_with_holes_2<exactKernel>
exactPolygon_with_holes_2;
typedef CGAL::Polygon_set_2<exactKernel>
exactPolygon_set_2;
typedef exactKernel::Segment_2 exactSegment_2;
typedef exactKernel::Point_2
exactPoint_2;
typedef exactKernel::Vector_2
exactVector_2;
void UPaveMethodFunctionLibrary::FiltrationPolygon(const
TArray<FPolygonData>& InHolesPolygon, TArray<FVector2D>&
OutFiltrationPolygon)
{
if (OutFiltrationPolygon.Num() < 3 || InHolesPolygon.Num() == 0){
return;
}
exactPolygon_2 targetPolygon;
std::list<exactPolygon_2> holesPolygon;
for (int32 filtrationPolygonIndex = 0; filtrationPolygonIndex <
OutFiltrationPolygon.Num(); ++filtrationPolygonIndex)
{
targetPolygon.push_back(exactPoint_2(OutFiltrationPolygon[filtrationPolygonIndex].X,
OutFiltrationPolygon[filtrationPolygonIndex].Y));
}
if (targetPolygon.is_clockwise_oriented()) {
targetPolygon.reverse_orientation();
}
for (int32 holePolygonIndex = 0; holePolygonIndex <
InHolesPolygon.Num();
++holePolygonIndex)
{
exactPolygon_2 currentHolePolygon;
for (int32 holePointIndex = 0; holePointIndex <
InHolesPolygon[holePolygonIndex].Polygon.Num(); ++holePointIndex)
{
currentHolePolygon.push_back(exactPoint_2(InHolesPolygon[holePolygonIndex].Polygon[holePointIndex].X,
InHolesPolygon[holePolygonIndex].Polygon[holePointIndex].Y));
}
if (currentHolePolygon.is_clockwise_oriented()) {
currentHolePolygon.reverse_orientation();
}
holesPolygon.push_back(currentHolePolygon);
}
bool IsExistInList = false;
std::list<exactPolygon_2> tempholesPolygon;
for (std::list<exactPolygon_2>::const_iterator holePolygonListIt =
holesPolygon.cbegin(); holePolygonListIt != holesPolygon.cend();
++holePolygonListIt)
{
for (exactPolygon_2::Edge_const_iterator holePolygonIt =
holePolygonListIt->edges_begin(); holePolygonIt !=
holePolygonListIt->edges_end(); ++holePolygonIt)
{
for (exactPolygon_2::Edge_const_iterator
targetPolygonIt =
targetPolygon.edges_begin(); targetPolygonIt != targetPolygon.edges_end();
++targetPolygonIt)
{
if
(targetPolygonIt->collinear_has_on(holePolygonIt->start()) &&
targetPolygonIt->collinear_has_on(holePolygonIt->end()))
{
IsExistInList = true;
tempholesPolygon.push_back(*holePolygonListIt);
break;
}
}
if (IsExistInList){
break;
}
}
IsExistInList = false;
}
if (tempholesPolygon.size() == 0){
return;
}
for (exactPolygon_2 currentHolePolygon : tempholesPolygon)
{
std::list<exactPolygon_with_holes_2> differencePolygons;
CGAL::difference(targetPolygon, currentHolePolygon,
std::back_inserter(differencePolygons));
if (differencePolygons.size() != 1) {
return;
}
targetPolygon = differencePolygons.front().outer_boundary();
if (targetPolygon.is_clockwise_oriented()) {
targetPolygon.reverse_orientation();
}
}
if (targetPolygon.size() < 3 || !targetPolygon.is_simple()){
return;
}
OutFiltrationPolygon.Empty();
for (exactPolygon_2::Vertex_const_iterator it =
targetPolygon.vertices_begin(); it != targetPolygon.vertices_end(); it++)
{
double pointX = CGAL::to_double(it->x());
double pointY = CGAL::to_double(it->y());
OutFiltrationPolygon.AddUnique(FVector2D(pointX, pointY));
}
}
--
Sent from: http://cgal-discuss.949826.n4.nabble.com/
- [cgal-discuss] CGAL::difference for 2d polygon return a incorrect result polygon, Luoyu, 04/26/2020
Archive powered by MHonArc 2.6.18.