New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect triangulation of polygons with many holes #1331
Comments
The best solution to this would be to find a replacement for the current "earclip_triangulation" function. Unfortunately, this does not seem to work well for polygons with too many holes. I have previously looked for a more robust library in python, and if you can find one do let me know. I can take a look at this example and try to fix it more directly, but I'm hesitant to make any promises. |
One solution here would be to find a more robust polygon triangulation library written in C, not python, and use that. |
This may be helpful: https://github.com/SebLague/Ear-Clipping-Triangulation/tree/master Furthermore, I think earclip_triangulation itself can not resolve this issue. It is needed to tell how many holes in the object and the topology among the holes so that the vertices data can be correctly established. To include general Chinese characters, it is required to find a way to find the pattern about the topology of holes among different parts of each Chinese character. For TonyCrane’s SquareTris example, the issue I think is function get_triangulation in vectorized_mobject.py. It treats the inner square with one hole as a convex polygon. Correctly, it should have 8 triangles, however the code gives 2 triangles. |
Am I wrong in saying https://trimsh.org/ would handle this? |
Okay, I think I may have fixed it with this commit. Let me know if that works for you. |
Maybe I'm missing something, but it looks like trimesh is useful for working with and manipulating meshes of 3d objects once you already have them, but I'm not seeing where it would be useful for finding the triangulation of a 2d polygon with many holes. |
Great, it works now. |
Can you attach the relevant svg? When I render that character in other fonts it looks fine, but I don't currently have the font from the example above installed. |
Here: d17bf5c77d8120cd.txt (change its extension to |
Okay, I'll look into it. |
Alright, I believe this change should actually fix it in a more robust way. It works on the svg you passed, and it seems to work on all the Chinese text I try, let me know how it looks @TonyCrane. |
But there may be still a bug. When I tried to run the class SquaresTri(Scene):
def construct(self):
squares = VGroup(*[
Square(i) for i in np.arange(7, 0, -1)
])
self.add(squares)
square = VMobject()
square.set_fill(opacity=0.3)
for index, mob in enumerate(squares):
if index % 2:
square.append_points(mob.get_points()[::-1])
else:
square.append_points(mob.get_points())
self.add(square) |
Fascinating. Unless I am mistaken, I believe this is an error with the mapbox_earcut library, and not with the wrapper I wrote around it. In some sense, this is an "unstable" exception. What I mean that is that if you perturb the squares slightly, e.g.
The wrapper is just there to turn a polygon with holes into a continuous polygon by connecting the holes, analogous to how the limit of a keyhole contour is a discontinuous contour with a hole in it. The way it connects up indices in the perturbed example is the same as how it does in the faulty centered example, which leads me to believe it's not an issue with that connecting process. From there, it's up to the mapbox_earcut library to fill in the single continuous polygon correctly. Perhaps this means the ultimate solution really does lie with finding a better triangulation library. If it's alright, I'll go ahead and close this issue since it seems like the exception involving holes within holes within holes may be pathological enough not to come up in practice. But of course, feel free to reopen it if it does. |
The Chinese character shown in svg is not correctly displayed. From previous codes, it is perfectly displayed. ChineseCh.mp4 |
Okay, maybe not as pathological as I suspect. I'll look into if this can be fixed without changing the triangulation library, otherwise, that's probably what we'll need to do. |
You offered another two versions about this issue, they all work. But this one, it fails. |
Great, at least for 99.9% scenarios, it is enough. If possible, I think it is a great idea to have a video lecture about earcut algorithm and how to cut and paste different parts by some examples like the aforementioned to work with earcut algorithm, I think it will be interesting and very useful. |
I tried to use
Text
to write Chinese, but there were many problems.For example, the simplest one is to write the word "回". But the result is this (contains unnecessary connections):
I lowered its opacity and showed the point set and its indices. The result is this:
The position and order of the point set are correct. So I suspect that there may be an error in the triangulation, so I displayed the inner triangles in the triangulation, and the result is this:
Basically it is certain that there is a problem, but I don't know how to solve it.
In addition, this is the svg file of this Chinese character, you can use
SVGMobject
to test it.d17bf5c77d8120cd.txt (change its extension to
.svg
, because GitHub does not support uploading svg files here)And it this way, not only
Text
will have problems, butSVGMobject
and even other mobjects may also have such problems.So, @3b1b please take a look at this.
The text was updated successfully, but these errors were encountered: