-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add cotmatrix, grad, cut_mesh, face_components, rdp #41
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
Changes from all commits
0241dee
f50a6c7
846ae44
6b8f59e
3cc1f51
19a9217
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import numpy as np | ||
| from compas.plugins import plugin | ||
|
|
||
| from compas_libigl import _cotmatrix | ||
|
|
||
|
|
||
| @plugin(category="trimesh") | ||
| def trimesh_cotmatrix(M): | ||
| """Compute the cotangent Laplacian matrix of a triangle mesh. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| M : tuple[list[list[float]], list[list[int]]] | ||
| A mesh represented by a tuple of (vertices, faces) | ||
| where vertices are 3D points and faces are triangles | ||
|
|
||
| Returns | ||
| ------- | ||
| scipy.sparse.csc_matrix | ||
| The cotangent Laplacian matrix in sparse format. | ||
| """ | ||
| V, F = M | ||
| V = np.asarray(V, dtype=np.float64) | ||
| F = np.asarray(F, dtype=np.int32) | ||
| return _cotmatrix.trimesh_cotmatrix(V, F) | ||
|
|
||
|
|
||
| @plugin(category="trimesh") | ||
| def trimesh_cotmatrix_entries(M): | ||
| """Compute cotangent values for each edge in each triangle. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| M : tuple[list[list[float]], list[list[int]]] | ||
| A mesh represented by a tuple of (vertices, faces) | ||
| where vertices are 3D points and faces are triangles | ||
|
|
||
| Returns | ||
| ------- | ||
| numpy.ndarray | ||
| A matrix of shape (F, 3) containing cotangent values per edge. | ||
| For each face, contains cotan of angles opposite to edges (1,2), (2,0), (0,1). | ||
| """ | ||
| V, F = M | ||
| V = np.asarray(V, dtype=np.float64) | ||
| F = np.asarray(F, dtype=np.int32) | ||
| return _cotmatrix.trimesh_cotmatrix_entries(V, F) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import numpy as np | ||
| from compas.plugins import plugin | ||
|
|
||
| from compas_libigl import _grad | ||
|
|
||
|
|
||
| @plugin(category="trimesh") | ||
| def trimesh_grad(M): | ||
| """Compute the gradient operator for a triangle mesh. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| M : tuple[list[list[float]], list[list[int]]] | ||
| A mesh represented by a tuple of (vertices, faces) | ||
| where vertices are 3D points and faces are triangles | ||
|
|
||
| Returns | ||
| ------- | ||
| scipy.sparse.csc_matrix | ||
| The gradient operator as a sparse matrix of size (3*F, V). | ||
| When multiplied by a scalar field on vertices, produces gradient | ||
| vectors per face (stacked as Gx, Gy, Gz). | ||
| """ | ||
| V, F = M | ||
| V = np.asarray(V, dtype=np.float64) | ||
| F = np.asarray(F, dtype=np.int32) | ||
| return _grad.trimesh_grad(V, F) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import numpy as np | ||
| from compas.plugins import plugin | ||
|
|
||
| from compas_libigl import _simplify | ||
|
|
||
|
|
||
| @plugin(category="polyline") | ||
| def ramer_douglas_peucker(points, threshold): | ||
| """Simplify a polyline using Ramer-Douglas-Peucker algorithm. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| points : list[list[float]] | ||
| A list of 3D points representing the polyline. | ||
| threshold : float | ||
| Maximum distance threshold for simplification. | ||
| Points that deviate less than this from the simplified line are removed. | ||
|
|
||
| Returns | ||
| ------- | ||
| tuple[list[list[float]], list[int], list[list[float]]] | ||
| A tuple containing | ||
| * the simplified polyline points (S), | ||
| * indices in original polyline that were kept (J), | ||
| * for each original point, the corresponding point on the simplified curve (Q). | ||
| """ | ||
| P = np.asarray(points, dtype=np.float64) | ||
| S, J, Q = _simplify.ramer_douglas_peucker(P, float(threshold)) | ||
| return S.tolist(), J.tolist(), Q.tolist() |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,39 @@ | ||||||||
| #include "cotmatrix.hpp" | ||||||||
|
|
||||||||
| Eigen::SparseMatrix<double> | ||||||||
| trimesh_cotmatrix( | ||||||||
| Eigen::Ref<const compas::RowMatrixXd> V, | ||||||||
| Eigen::Ref<const compas::RowMatrixXi> F | ||||||||
| ) { | ||||||||
| Eigen::SparseMatrix<double> L; | ||||||||
| igl::cotmatrix(V, F, L); | ||||||||
| return L; | ||||||||
| } | ||||||||
|
|
||||||||
| compas::RowMatrixXd | ||||||||
| trimesh_cotmatrix_entries( | ||||||||
| Eigen::Ref<const compas::RowMatrixXd> V, | ||||||||
| Eigen::Ref<const compas::RowMatrixXi> F | ||||||||
| ) { | ||||||||
| Eigen::MatrixXd C; | ||||||||
| igl::cotmatrix_entries(V, F, C); | ||||||||
| compas::RowMatrixXd C_row = C; | ||||||||
| return C_row; | ||||||||
| } | ||||||||
|
|
||||||||
| NB_MODULE(_cotmatrix, m) { | ||||||||
|
||||||||
| NB_MODULE(_cotmatrix, m) { | |
| NB_MODULE(_cotmatrix, m) { | |
| m.doc() = "Cotangent matrix computation functions using libigl"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #pragma once | ||
|
|
||
| #include "compas.hpp" | ||
| #include <igl/cotmatrix.h> | ||
| #include <igl/cotmatrix_entries.h> | ||
| #include <Eigen/Core> | ||
| #include <Eigen/Sparse> | ||
|
|
||
| /** | ||
| * Compute the cotangent Laplacian matrix of a triangle mesh. | ||
| * | ||
| * @param V The vertex positions of the mesh. | ||
| * @param F The face indices of the mesh. | ||
| * @return The cotangent Laplacian as a sparse matrix. | ||
| */ | ||
| Eigen::SparseMatrix<double> trimesh_cotmatrix( | ||
| Eigen::Ref<const compas::RowMatrixXd> V, | ||
| Eigen::Ref<const compas::RowMatrixXi> F | ||
| ); | ||
|
|
||
| /** | ||
| * Compute the cotangent values for each edge in each triangle of a mesh. | ||
| * | ||
| * @param V The vertex positions of the mesh. | ||
| * @param F The face indices of the mesh. | ||
| * @return A matrix of size F x 3 containing cotangent values per edge. | ||
| */ | ||
| compas::RowMatrixXd trimesh_cotmatrix_entries( | ||
| Eigen::Ref<const compas::RowMatrixXd> V, | ||
| Eigen::Ref<const compas::RowMatrixXi> F | ||
| ); |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,21 @@ | ||||||||
| #include "grad.hpp" | ||||||||
|
|
||||||||
| Eigen::SparseMatrix<double> | ||||||||
| trimesh_grad( | ||||||||
| Eigen::Ref<const compas::RowMatrixXd> V, | ||||||||
| Eigen::Ref<const compas::RowMatrixXi> F | ||||||||
| ) { | ||||||||
| Eigen::SparseMatrix<double> G; | ||||||||
| igl::grad(V, F, G); | ||||||||
| return G; | ||||||||
| } | ||||||||
|
|
||||||||
| NB_MODULE(_grad, m) { | ||||||||
|
||||||||
| NB_MODULE(_grad, m) { | |
| NB_MODULE(_grad, m) { | |
| m.doc() = "Gradient operator computation functions using libigl"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #pragma once | ||
|
|
||
| #include "compas.hpp" | ||
| #include <igl/grad.h> | ||
| #include <Eigen/Core> | ||
| #include <Eigen/Sparse> | ||
|
|
||
| /** | ||
| * Compute the gradient operator for a triangle mesh. | ||
| * | ||
| * @param V The vertex positions of the mesh. | ||
| * @param F The face indices of the mesh. | ||
| * @return The gradient operator as a sparse matrix of size 3F x V. | ||
| */ | ||
| Eigen::SparseMatrix<double> trimesh_grad( | ||
| Eigen::Ref<const compas::RowMatrixXd> V, | ||
| Eigen::Ref<const compas::RowMatrixXi> F | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary unpacking: The variable
Vis extracted fromMbut never used. Sincetrimesh_face_componentsonly requires face connectivity (not vertex positions), you should either remove the unpacking ofVor only unpackFwith_, F = Mfor clarity.