Skip to content
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

CHANGE triangulation_earclip #1253

Merged
merged 10 commits into from
Jan 11, 2024
Merged

CHANGE triangulation_earclip #1253

merged 10 commits into from
Jan 11, 2024

Conversation

petrasvestartas
Copy link
Contributor

@petrasvestartas petrasvestartas commented Jan 9, 2024

The change of compas\src\compas\geometry\triangulation_earclip.py code to a new one.
The issue with the current algorithm is described in the following forum thread:
COMPAS FORUM

The function name stays the same:

if __name__ == "__main__":
    points = [
        [377.952174, -3452, 1500.484283],
        [369.348527, -3265, 1488.197003],
        [459.973604, -3252, 1617.623026],
        [595.337643, -3295, 1810.942908],
        [718.083001, -3144, 1986.241446],
        [458.252875, -3120, 1615.16557],
        [336.081094, -3052, 1440.686184],
        [277.576297, -3227, 1357.132676],
        [219.071501, -3154, 1273.579167],
        [74.530239, -3315, 1067.152852],
        [69.368051, -3436, 1059.780484],
        [86.575344, -3695, 1084.355045],
        [-2.329003, -3686, 957.386478],
        [-51.083001, -3865, 887.758554],
        [63.05871, -4035, 1050.769811],
        [136.476494, -3688, 1155.621273],
        [192.686985, -3362, 1235.898173],
        [332.639635, -3414, 1435.771272],
        [278.149874, -3619, 1357.951828],
        [186.377644, -3672, 1226.887501],
        [189.245526, -3981, 1230.983261],
        [339.522553, -4195, 1445.601096],
        [441.619158, -3998, 1591.41016],
        [448.502075, -3634, 1601.239985],
        [477.180897, -3578, 1642.197587],
        [505.286143, -3908, 1682.336037],
        [642.370911, -3534, 1878.113376],
        [422.117559, -3301, 1563.558991],
        [332.639635, -3824, 1435.771272],
    ]

    polygon = Polygon(points)
    triangles = earclip_polygon(polygon)
    print(triangles)

What type of change is this?

  • Bug fix in a backwards-compatible manner.
  • New feature in a backwards-compatible manner. - replacement of existing code.
  • Breaking change: bug fix or new feature that involve incompatible API changes.
  • Other (e.g. doc update, configuration, etc)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I added a line to the CHANGELOG.md file in the Unreleased section under the most fitting heading (e.g. Added, Changed, Removed).
  • I ran all tests on my computer and it's all green (i.e. invoke test).
  • I ran lint on my computer and there are no errors (i.e. invoke lint).
  • I added new functions/classes and made them available on a second-level import, e.g. compas.datastructures.Mesh.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if appropriate)

CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Member

@gonzalocasas gonzalocasas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good, I didn't check the math though. But it is missing tests, and since you mention that the previous algo failed in some cases, I would like to see those failing cases added as unit tests + some non-edge cases.

Comment on lines 5 to 29
"""Initialize an Ear instance.

Parameters
----------
points : list
List of points representing the polygon.
indexes : list
List of indices of the points representing the polygon.
ind : int
Index of the vertex of the Ear triangle.

Attributes
----------
index : int
Index of the vertex of the Ear triangle.
coords : list
Coordinates of the vertex of the Ear triangle.
next : int
Index of the next vertex of the Ear triangle.
prew : int
Index of the previous vertex of the Ear triangle.
neighbour_coords : list
Coordinates of the next and previous vertices of the Ear triangle.

"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add to class docstring


Returns
-------
bool : True, if the point is a vertex of the Ear triangle, False otherwise.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool
    True, if ...


Returns
-------
bool: True if the Ear triangle is valid, False otherwise.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


Returns
-------
bool: True if the Ear triangle is convex, False otherwise.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


Returns
-------
list: List of vertex indices forming the Ear triangle.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Comment on lines 128 to 148
"""Initialize an Earcut instance with the input points.

Parameters
----------
points : list
List of points representing the polygon.

Attributes
----------
vertices : list
List of points representing the polygon.
ears : list
List of Ear objects representing the Ears of the polygon.
neighbours : list
List of indices of the neighbouring vertices.
triangles : list
List of triangles forming the triangulation of the polygon.
length : int
Number of vertices of the polygon.

"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add to class docstring

@petrasvestartas
Copy link
Contributor Author

Thank you for a review, the changes have been made, including pytest, and pushed once more.

@tomvanmele
Copy link
Member

can you merge main back in please...

@petrasvestartas
Copy link
Contributor Author

petrasvestartas commented Jan 11, 2024

earclip_polygon method is merged back.

CHANGELOG.md Outdated
Comment on lines 85 to 88
* Changed `compas.geometry.earclip_polygon` algorithm because the current one does not work in several cases.
* Changed the docstrings in `compas.geometry.earclip_polygon` are corrected. The orientation to the XY Frame has been removed since it is handled by the `polygon.to_vertices_and_faces` method. Additionally, the test_polygon.py file has been modified with a new test for the ear_clip called `test_polygon_to_vertices_and_faces`. The `object` has been added to all `Ear` classes.
* Changed the docstrings in `compas.geometry.earclip_polygon` is merged back, meaning the polygon is transformed to XY frame.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to add tests and docstring stuff to the changelog, the changelog is only for users of the library that need to understand what has changed

self.add_ear(next_ear_new)
continue

return self.triangles


def earclip_polygon(polygon):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unit test added does not explicitly test the earclip algorithm, and actually I find it hard to see the link from the code. I would add an additiona tests/compas/geometry/test_triangulation_earclip.py that tests directly the relevant methods of this module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just made a new push to a new file: tests/compas/geometry/test_triangulation_earclip.py for testing.

I added one small detail to handle simple triangles.

CHANGELOG.md Outdated
@@ -82,6 +82,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Moved `compas.numerical.matrices` to `compas.topology.matrices`.
* Moved `compas.numerical.linalg` to `compas.geometry.linalg`.
* Changed `watchdog` dependency to be only required for platforms other than `emscripten`.
* Changed `compas.geometry.earclip_polygon` algorithm because the current one does handle several cases.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny typo ;)

Suggested change
* Changed `compas.geometry.earclip_polygon` algorithm because the current one does handle several cases.
* Changed `compas.geometry.earclip_polygon` algorithm because the current one does not handle several cases.

Copy link
Member

@gonzalocasas gonzalocasas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me! Besides the little typo, there's some linting required (the build is failing because of that). Once the build is green, I think this is good to go!

@tomvanmele
Copy link
Member

@petrasvestartas run invoke check to find all such errors locally...

@tomvanmele
Copy link
Member

to apply black to the src folder and fix things automatically black src

@tomvanmele tomvanmele merged commit 114eb12 into compas-dev:main Jan 11, 2024
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants