Boolean Intersection- PathsD - Better Precision #522
-
When using PathsD for Boolean Intersection is there a way to increase a resolution/precision? bool get_intersection_between_two_polylines(CGAL_Polyline &polyline0, CGAL_Polyline &polyline1, IK::Plane_3 &plane, CGAL_Polyline &intersection_result, int intersection_type, bool include_triangles)
{
//printf("get_intersection_between_two_polylines\n");
/////////////////////////////////////////////////////////////////////////////////////
// Orient from 3D to 2D
/////////////////////////////////////////////////////////////////////////////////////
if (polyline0.size() > polyline0.max_size())
return false;
if (polyline1.size() > polyline1.max_size())
return false;
CGAL_Polyline a = polyline0;
CGAL_Polyline b = polyline1;
/////////////////////////////////////////////////////////////////////////////////////
// Create Transformation
/////////////////////////////////////////////////////////////////////////////////////
CGAL::Aff_transformation_3<IK> xform_to_xy = internal::plane_to_xy(polyline0[0], plane);
CGAL::Aff_transformation_3<IK> xform_to_xy_Inv = xform_to_xy.inverse();
for (auto it = a.begin(); it != a.end(); ++it)
*it = it->transform(xform_to_xy);
for (auto it = b.begin(); it != b.end(); ++it)
*it = it->transform(xform_to_xy);
// Transform(a, xform_to_xy);
// Transform(b, xform_to_xy);
/////////////////////////////////////////////////////////////////////////////////////
// Convert to Clipper
/////////////////////////////////////////////////////////////////////////////////////
printf("\n");
std::vector<Clipper2Lib::PointD> pathA(a.size() - 1);
std::vector<Clipper2Lib::PointD> pathB(b.size() - 1);
for (int i = 0; i < a.size() - 1; i++)
{
printf("%f %f\n", a[i].x(), a[i].y());
pathA[i] = Clipper2Lib::PointD((int)(a[i].x() ), (int)(a[i].y() ));
}
printf("\n");
for (int i = 0; i < b.size() - 1; i++)
{
printf("%f %f\n", b[i].x(), b[i].y());
pathB[i] = Clipper2Lib::PointD((int)(b[i].x()), (int)(b[i].y()));
}
Clipper2Lib::PathsD pathA_;
Clipper2Lib::PathsD pathB_;
pathA_.emplace_back(pathA);
pathB_.emplace_back(pathB);
Clipper2Lib::PathsD C;
if (intersection_type == 0)
C = Clipper2Lib::Intersect(pathA_, pathB_, Clipper2Lib::FillRule::NonZero);
else if (intersection_type == 1)
C = Clipper2Lib::Union(pathA_, pathB_, Clipper2Lib::FillRule::NonZero);
else if (intersection_type == 2)
C = Clipper2Lib::Difference(pathA_, pathB_, Clipper2Lib::FillRule::NonZero);
else if (intersection_type == 3)
C = Clipper2Lib::Xor(pathA_, pathB_, Clipper2Lib::FillRule::NonZero);
if (C.size() > 0)
{
//printf("Area_0 %f Area_0 %f\n", Area(C[0]),Area(pathA) );
// include triangles based on user input
bool is_not_triangle = C[0].size() != 3;
if (!is_not_triangle)
is_not_triangle = include_triangles;
if (C[0].size() > 2 && is_not_triangle && std::abs(Area(C[0])) >= std::abs(Area(pathA) ))//* wood_globals::CLIPPER_AREA
{ // skip triangles and very small polygons
intersection_result.resize(C[0].size() + 1);
for (int i = 0; i < C[0].size(); i++)
{
IK::Point_3 p(C[0][i].x , C[0][i].y , 0);
p = p.transform(xform_to_xy_Inv); // Rotate back to 3D
intersection_result[i] = p;
}
intersection_result[C[0].size()] = intersection_result[0]; // Close
}
else
{
return false;
}
}
else
{
return false;
} Example dataset:
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
I don't see you taking advantage of the precision parameter in the calls. You can increase this to improve accuracy (up to 8 decimal places). |
Beta Was this translation helpful? Give feedback.
-
What do you mean by precision parameter?
|
Beta Was this translation helpful? Give feedback.
-
Yes, so I was hoping that the additional resolution would solve your case. It was needed in some of mine to avoid similar problems in the results. Do you have a simple test case that is runnable? |
Beta Was this translation helpful? Give feedback.
-
Oh, it was because I was casting double to int. |
Beta Was this translation helpful? Give feedback.
-
Probably worth marking this as answered to help others in future :) |
Beta Was this translation helpful? Give feedback.
I don't see you taking advantage of the precision parameter in the calls. You can increase this to improve accuracy (up to 8 decimal places).