Skip to content

Commit

Permalink
loop and sqrt3
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanmele committed Nov 19, 2023
1 parent d87c7a8 commit 40a3d27
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
30 changes: 27 additions & 3 deletions src/compas_cgal/subdivision.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def mesh_subdivide_catmull_clark(mesh: VerticesFaces, k=1) -> VerticesFacesNumpy:
"""Subdivide a mesh withe the Catmull Clark scheme.
"""Subdivide a mesh with the Catmull Clark scheme.
Parameters
----------
Expand Down Expand Up @@ -38,7 +38,7 @@ def mesh_subdivide_catmull_clark(mesh: VerticesFaces, k=1) -> VerticesFacesNumpy


def mesh_subdivide_loop(mesh: VerticesFaces, k=1) -> VerticesFacesNumpy:
"""Subdivide a mesh withe the Loop scheme.
"""Subdivide a mesh with the Loop scheme.
Parameters
----------
Expand All @@ -52,4 +52,28 @@ def mesh_subdivide_loop(mesh: VerticesFaces, k=1) -> VerticesFacesNumpy:
:attr:`compas_cgal.types.VerticesFacesNumpy`
"""
raise NotImplementedError
V, F = mesh
V = np.asarray(V, dtype=np.float64)
F = np.asarray(F, dtype=np.int32)
return subdivision.subd_loop(V, F, k)


def mesh_subdivide_sqrt3(mesh: VerticesFaces, k=1) -> VerticesFacesNumpy:
"""Subdivide a mesh with the Sqrt3 scheme.
Parameters
----------
mesh : :attr:`compas_cgal.types.VerticesFaces`
The mesh to remesh.
k : int, optional
The number of subdivision steps.
Returns
-------
:attr:`compas_cgal.types.VerticesFacesNumpy`
"""
V, F = mesh
V = np.asarray(V, dtype=np.float64)
F = np.asarray(F, dtype=np.int32)
return subdivision.subd_sqrt3(V, F, k)
56 changes: 50 additions & 6 deletions src/subdivision.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "subdivision.h"
#include <CGAL/subdivision_method_3.h>


std::tuple<compas::RowMatrixXd, compas::RowMatrixXi>
subd_catmullclark(
compas::RowMatrixXd & V,
std::vector< std::vector<int> > & faces,
compas::RowMatrixXd &V,
std::vector<std::vector<int>> &faces,
unsigned int k)
{
Mesh mesh = compas::ngon_from_vertices_and_faces(V, faces);
Expand All @@ -18,15 +17,60 @@ subd_catmullclark(
return R;
};

std::tuple<compas::RowMatrixXd, compas::RowMatrixXi>
subd_loop(
compas::RowMatrixXd &V,
compas::RowMatrixXi &F,
unsigned int k)
{
Mesh mesh = compas::mesh_from_vertices_and_faces(V, F);

CGAL::Subdivision_method_3::Loop_subdivision(mesh, CGAL::parameters::number_of_iterations(k));

mesh.collect_garbage();

std::tuple<compas::RowMatrixXd, compas::RowMatrixXi> R = compas::mesh_to_vertices_and_faces(mesh);
return R;
};

std::tuple<compas::RowMatrixXd, compas::RowMatrixXi>
subd_sqrt3(
compas::RowMatrixXd &V,
compas::RowMatrixXi &F,
unsigned int k)
{
Mesh mesh = compas::mesh_from_vertices_and_faces(V, F);

void init_subdivision(pybind11::module & m) {
CGAL::Subdivision_method_3::Sqrt3_subdivision(mesh, CGAL::parameters::number_of_iterations(k));

mesh.collect_garbage();

std::tuple<compas::RowMatrixXd, compas::RowMatrixXi> R = compas::mesh_to_vertices_and_faces(mesh);
return R;
};

void init_subdivision(pybind11::module &m)
{
pybind11::module submodule = m.def_submodule("subdivision");

submodule.def(
"subd_catmullclark",
&subd_catmullclark,
pybind11::arg("V").noconvert(),
pybind11::arg("faces").noconvert(),
pybind11::arg("k")
);
pybind11::arg("k"));

submodule.def(
"subd_loop",
&subd_loop,
pybind11::arg("V").noconvert(),
pybind11::arg("F").noconvert(),
pybind11::arg("k"));

submodule.def(
"subd_sqrt3",
&subd_sqrt3,
pybind11::arg("V").noconvert(),
pybind11::arg("F").noconvert(),
pybind11::arg("k"));
};
16 changes: 13 additions & 3 deletions src/subdivision.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@

#include <compas.h>


std::tuple<compas::RowMatrixXd, compas::RowMatrixXi>
subd_catmullclark(
compas::RowMatrixXd & V,
std::vector< std::vector<int> > & faces,
compas::RowMatrixXd &V,
std::vector<std::vector<int>> &faces,
unsigned int k);

std::tuple<compas::RowMatrixXd, compas::RowMatrixXi>
subd_loop(
compas::RowMatrixXd &V,
compas::RowMatrixXi &F,
unsigned int k);

std::tuple<compas::RowMatrixXd, compas::RowMatrixXi>
subd_sqrt3(
compas::RowMatrixXd &V,
compas::RowMatrixXi &F,
unsigned int k);

#endif /* COMPAS_SUBD_H */

0 comments on commit 40a3d27

Please sign in to comment.