Are polyshape vertices preserved reliably without floating point errors?
1 view (last 30 days)
Show older comments
Suppose I have two polyshapes p1 and p2 and I want to do operations on them (intersection, unions) that in theory should preserve some of the vertices of p1 and p2. My question is, can I always count on this to occur without floating point error from the underlying software algorithm?
In the following example, it seems to be true. The union of p1 and p2 is a quadrilateral whos vertices ought to be the union of the vertices of p1 and p2 separately,
load Data
figure; plot([p1,p2]);axis equal
and this is indeed shown to be the case without the need to apply a floating point error tolerance,
Union=union(p1,p2);
all(ismember(p1.Vertices,Union.Vertices,'rows'))
all(ismember(p2.Vertices,Union.Vertices,'rows'))
The same thing appears to be true with intersections:
figure; plot([p2,p3]);axis equal
Intersection=intersect(p2,p3);
nnz(ismember(p3.Vertices,Intersection.Vertices,'rows'))
But there's no reason this had to be the case, right? There must be something about the underlying union() and intersect() algorithms that ensure this.
0 Comments
Accepted Answer
Paul
on 5 May 2024
Hi Matt,
The doc pages for intersect, union, subtract, and xor all show a second output (ShapeID) that indicates if a vertex in polyout comes from poly1, poly2, or is created from the boolean operation itself.
In my opinion it would very peculiar if the ShapeID indicates a vertex in polyout came from poly1 (poly2) and it wasn't exactly the same as the corresponding vertex in poly1 (poly2).
I'd hope that any tolerances are applied to determine whether or not keep a particular vertex, but once a determination is made to keep it, the vertex data themselves should not be changed.
1 Comment
More Answers (1)
John D'Errico
on 5 May 2024
Edited: John D'Errico
on 5 May 2024
Um, probably, yes. But potentially no.
Yes, because on most operations, you will just be indexing into the original set of vertices. And that MUST preserve the exact numbers.
Potentially no because consider an intersection, where one of the vertices from one of the shapes just happens to fall along an edge of the other. Now there will be floating point operations applied, where the least significant bits could be wrong. Can I say this would never happen? No. But I'd not trust it, at least not without some significant testing. Anyway, that is where I would be looking. Anything else seems unlikely to cause a problem.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!