Repository navigation
crash with CSG operations #290
|
The program below will crash geogram. Note that if you change the radius to 0.5 then it won't crash. Please look into this issue. It says that it's a numeric issue, but I don't think it is that. Also it spits out ZZ on the command line some number of times, which it probably shouldn't do. #include <geogram/mesh/mesh_CSG.h>
#include <geogram/mesh/mesh_io.h>
int main() {
using namespace GEO;
double x = 100.0; // length (X-axis)
double y = 10.0; // thickness (Y-axis)
double z = 50.0; // width (Z-axis)
initialize(GEOGRAM_INSTALL_ALL);
CSGBuilder B;
B.set_verbose(true);
auto board = B.cube({ x, y, z }, true);
GEO::mat4 positionCyl({
{1, 0, 0, 4.5},
{0, 0, -1, y + 1},
{0, 1, 0, 9.5},
{0, 0, 0, 1}
});
const auto radius = 2.5; // set this to 0.5 and it won't crash
const auto cyl1 = B.cylinder(y + 2, radius, radius, false);
const auto cyl1t = B.multmatrix(positionCyl, {cyl1});
positionCyl(0, 3) = 2.0 + 4.5;
const auto cyl2 = B.cylinder(y + 2, radius, radius, false);
const auto cyl2t = B.multmatrix(positionCyl, {cyl2});
auto rect = B.cube({2.0, y + 2, radius * 2}, false);
GEO::mat4 positionRect({
{1, 0, 0, 4.5},
{0, 1, 0, -1},
{0, 0, 1, 9.5 - radius},
{0, 0, 0, 1}
});
rect = B.multmatrix(positionRect, {rect});
auto result = B.difference({board, rect, cyl1t});
auto board_with_hole = B.difference({result, cyl2t});
// Save the resulting mesh to a file
mesh_save(*board_with_hole, "board_with_hole.obj");
return 0;
} |
Replies: 2 comments
|
Hello,
Where does the problem come from ?
In your CSG tree, there is a hole made by the union of two cylinders and a rectangle, forming a polygonal approximation of an oblong shape. The corresponding intersections are degenerate, in the sense that some vertices of the cylinders correspond to vertices of the rectangle. They probably do not match exactly, in a way that makes the open-source arithmetic kernel overflow (causing the ZZZZ messages you have seen on the console). Now there is also a solution with the open-source kernel The idea is to do all the delicate computations in 2D, then extrude the shape in 3D. In 2D, the computation depth is much lower, and overflow is very unlikely to occur. x = 100.0;
y = 10.0;
z = 50.0;
radius = 2.5;
difference() {
cube([x,z,y], true);
linear_extrude(y+2,false)
union() {
multmatrix(
[[1, 0, 0, 4.5],
[0, 1, 0, 9.5 - radius],
[0, 0, 1, 0],
[0, 0, 0, 1]]
) {
square([2.0, radius*2], false);
};
multmatrix(
[[1, 0, 0, 4.5],
[0, 1, 0, 9.5],
[0, 0, 1, 0 ],
[0, 0, 0, 1 ]]
) {
circle(r=radius);
};
multmatrix(
[[1, 0, 0, 2.0 + 4.5],
[0, 1, 0, 9.5 ],
[0, 0, 1, 0 ],
[0, 0, 0, 1 ]]
) {
circle(r=radius);
};
}
}To test it, save it to difference() {
cube(size = [100, 50, 10], center = true);
linear_extrude(height = 12, center = false, convexity = 1, scale = [1, 1], $fn = 0, $fa = 12, $fs = 2) {
union() {
multmatrix([[1, 0, 0, 4.5], [0, 1, 0, 7], [0, 0, 1, 0], [0, 0, 0, 1]]) {
square(size = [2, 5], center = false);
}
multmatrix([[1, 0, 0, 4.5], [0, 1, 0, 9.5], [0, 0, 1, 0], [0, 0, 0, 1]]) {
circle($fn = 0, $fa = 12, $fs = 2, r = 2.5);
}
multmatrix([[1, 0, 0, 6.5], [0, 1, 0, 9.5], [0, 0, 1, 0], [0, 0, 0, 1]]) {
circle($fn = 0, $fa = 12, $fs = 2, r = 2.5);
}
}
}
}save it to It seems that |
|
P.S. if you and others use the Builder API from C++, I'll add some translate(), rotate(), scale() functions to it (entering all 4x4 coefficients of an homogeneous xform matrix is not very convenient...) |

Hello,
geogram has two arithmetic kernels (more details in this publication),
Where does the problem come from ?
In your CSG tree, there is a hole made by the union of two cylinders and a rectangle, forming a polygonal approximation of an oblong shape. The corresponding intersections are degener…