Skip to content

Commit

Permalink
C bindings to facilitate static FFI bindings (elalish#292)
Browse files Browse the repository at this point in the history
* + c bindings to facilitate static FFI bindings

Squashed commit of the following:

commit a05f9b0
Author: Geoff deRosenroll <[email protected]>
Date:   Sat Dec 10 16:21:47 2022 -0800

    Add flag (default on) for python bindings

    Signed-off-by: Geoff deRosenroll <[email protected]>

commit ce5b601
Author: Geoff deRosenroll <[email protected]>
Date:   Fri Dec 9 23:45:29 2022 -0800

    conv function naming consistency

commit 1b50cf0
Author: Geoff deRosenroll <[email protected]>
Date:   Tue Dec 6 17:42:18 2022 -0800

    manifold_level_set_seq to C API (force sequential)

commit 77e06da
Author: Geoff deRosenroll <[email protected]>
Date:   Tue Dec 6 17:24:10 2022 -0800

    Use an optional executionpolicy param in LevelSet

    This allows bindings to avoid breaking their runtime locks while passing
    in function pointers without introducing another compile flag.

commit 12c62b1
Author: Geoff deRosenroll <[email protected]>
Date:   Mon Dec 5 15:09:41 2022 -0800

    Better section comments

commit 24f1d51
Author: Geoff deRosenroll <[email protected]>
Date:   Fri Dec 2 12:01:50 2022 -0800

    Add missing management functions for Components

commit a7d5aae
Author: Geoff deRosenroll <[email protected]>
Date:   Thu Dec 1 23:45:45 2022 -0800

    Break Decompose up to avoid repetition in C binds

commit a650a15
Author: Geoff deRosenroll <[email protected]>
Date:   Sun Nov 27 22:41:38 2022 -0800

    Add manifold_get_properties

commit 97a5839
Author: Geoff deRosenroll <[email protected]>
Date:   Sat Nov 26 22:53:42 2022 -0800

    circular segment functions corrected to static

commit c9c1ca7
Author: Geoff deRosenroll <[email protected]>
Date:   Thu Nov 24 21:19:11 2022 -0800

    polygons from dbl ptr of SimplePolygon

commit 8e5390a
Author: Geoff deRosenroll <[email protected]>
Date:   Wed Nov 23 12:16:44 2022 -0800

    compose and decompose switched to double ptrs

commit 45c78c3
Author: Geoff deRosenroll <[email protected]>
Date:   Tue Nov 22 11:40:32 2022 -0800

    Add manifold copy and empty constructors

commit cfc06fe
Author: Geoff deRosenroll <[email protected]>
Date:   Mon Nov 21 15:03:00 2022 -0800

    Add MANIFOLD_SEQ_SDF to avoid runtime lock

commit e0f9d09
Author: Geoff deRosenroll <[email protected]>
Date:   Sun Nov 20 16:56:50 2022 -0800

    Add missing export related contructors.

commit 0cbaa4a
Author: Geoff deRosenroll <[email protected]>
Date:   Sun Nov 20 16:03:43 2022 -0800

    Add separate destruct functions

commit fd6c1c2
Author: Geoff deRosenroll <[email protected]>
Date:   Sat Nov 19 17:29:12 2022 -0800

    More consistent array handling

commit ec1eef8
Author: Geoff deRosenroll <[email protected]>
Date:   Sat Nov 19 16:52:07 2022 -0800

    Replace array args with pointers

commit 9079cbf
Author: Geoff deRosenroll <[email protected]>
Date:   Sat Nov 19 15:00:47 2022 -0800

    Revert "Switch back to funptr struct args for simplicity"

    This reverts commit dc78841.

commit 62c1078
Author: Geoff deRosenroll <[email protected]>
Date:   Sat Nov 19 14:52:37 2022 -0800

    Switch back to funptr struct args for simplicity

commit d30992d
Author: Geoff deRosenroll <[email protected]>
Date:   Sat Nov 19 14:06:12 2022 -0800

    Avoid needing to pass in vec struct for funptrs

commit 2ba7bec
Author: Geoff deRosenroll <[email protected]>
Date:   Sat Nov 19 00:32:09 2022 -0800

    Use template spec to avoid fun ptr to cuda

commit 1faf58b
Author: Geoff deRosenroll <[email protected]>
Date:   Fri Nov 18 21:54:56 2022 -0800

    Make fPIC more conditional.

commit 6215f89
Author: Geoff deRosenroll <[email protected]>
Date:   Fri Nov 18 21:35:09 2022 -0800

    Avoid passing dynamic lambda to GPU

commit 30fa3dc
Author: Geoff deRosenroll <[email protected]>
Date:   Fri Nov 18 18:02:04 2022 -0800

    note and temporary hack RE cuda compilation

commit bb4dfe9
Author: Geoff deRosenroll <[email protected]>
Date:   Fri Nov 18 17:21:25 2022 -0800

    Set graphlite library to OBJECT

commit e803da5
Author: Geoff deRosenroll <[email protected]>
Date:   Fri Nov 18 16:04:28 2022 -0800

    Convert sublibraries to OBJECT to avoid archives

commit d0e9d17
Author: Geoff deRosenroll <[email protected]>
Date:   Fri Nov 18 15:28:15 2022 -0800

    Add missing polygon size and deletion funcs

commit 7db7284
Author: Geoff deRosenroll <[email protected]>
Date:   Tue Nov 15 22:26:17 2022 -0800

    Remove static flag (control with shared flag)

commit 5ae33f3
Author: Geoff deRosenroll <[email protected]>
Date:   Tue Nov 15 22:16:27 2022 -0800

    Add options to avoid python bindings

commit 4181a58
Author: Geoff deRosenroll <[email protected]>
Date:   Mon Nov 14 22:02:10 2022 -0800

    Rename to manifoldc

commit c4b14d6
Author: Geoff deRosenroll <[email protected]>
Date:   Mon Nov 14 21:51:45 2022 -0800

    Cleanup unused typedefs and simplify names

commit a282c90
Author: Geoff deRosenroll <[email protected]>
Date:   Mon Nov 14 21:23:07 2022 -0800

    Placeholder (terrible) decompose binding

commit 9409e81
Author: Geoff deRosenroll <[email protected]>
Date:   Mon Nov 14 20:37:02 2022 -0800

    Turn on export in CLI instead

commit 9dad24b
Author: Geoff deRosenroll <[email protected]>
Date:   Mon Nov 14 20:33:17 2022 -0800

    "Rebased" WIP cbindings

* Turn on -fPIC whenever shared libs are being built

* Add parens to disambiguate sdf policy logic

* Bump public std from 14 to 17

* Add binding flags to workflow builds

* Add export when building cbindings

* Bump ubuntu version (PBR material deprecation)

* Select no-param overload of Decompose

* Add missing const to decompose sig overload

* Re-organize

* variable capitalization consistency

* Drop to std14 to test for regression fix

* Attempted old libassimp compatibility hack

* Rebump to std_17

* Temporarily disabled

TODO: investigate why this is failing on Mac, but not other backends.

* Remove unused tuple include

* Remove uneccesary auto-includes

* Properly disable Boolean_Close

* rename discrete -> numComponents + early return

* Rename discrete -> numComponents

* Rename conv.hh -> conv.h

* Prune auto-includes

* Note on level_set vs level_set_seq

* Set export on for c-bindings

* Adding C binding tests.

* Separate and don't require CBIND build/test

* Remove bad flag

* Take const char ptr for filename

* Remove testc executable

* Add tests from removed testc
  • Loading branch information
geoffder authored Dec 18, 2022
1 parent 8705950 commit a7b457d
Show file tree
Hide file tree
Showing 32 changed files with 1,626 additions and 65 deletions.
40 changes: 33 additions & 7 deletions .github/workflows/manifold.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
Expand All @@ -16,7 +16,7 @@ jobs:
matrix:
cuda_support: [ON, OFF]
parallel_backend: [NONE, OMP, TBB]
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
if: github.event.pull_request.draft == false
container:
image: docker://nvidia/cuda:11.6.0-devel-ubuntu20.04
Expand All @@ -35,7 +35,7 @@ jobs:
git apply thrust.diff
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DMANIFOLD_DEBUG=ON -DMANIFOLD_PAR=${{matrix.parallel_backend}} -DMANIFOLD_USE_CUDA=${{matrix.cuda_support}} .. && make
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DMANIFOLD_DEBUG=ON -DMANIFOLD_PYBIND=ON -DMANIFOLD_PAR=${{matrix.parallel_backend}} -DMANIFOLD_USE_CUDA=${{matrix.cuda_support}} .. && make
- name: Test ${{matrix.parallel_backend}} with CUDA ${{matrix.cuda_support}}
# note that the test for CUDA backend does not really test CUDA, as we
# don't have CUDA GPU on GitHub Action
Expand All @@ -52,7 +52,7 @@ jobs:
if: matrix.parallel_backend == 'NONE' && matrix.cuda_support == 'OFF'
run: |
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DMANIFOLD_DEBUG=OFF -DMANIFOLD_PAR=${{matrix.parallel_backend}} -DMANIFOLD_USE_CUDA=${{matrix.cuda_support}} -DCODE_COVERAGE=ON .. && make
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DMANIFOLD_DEBUG=OFF -DMANIFOLD_PYBIND=ON -DMANIFOLD_PAR=${{matrix.parallel_backend}} -DMANIFOLD_USE_CUDA=${{matrix.cuda_support}} -DCODE_COVERAGE=ON .. && make
lcov --capture --initial --directory . --output-file ./code_coverage_init.info
cd test
./manifold_test
Expand All @@ -67,6 +67,33 @@ jobs:
fail_ci_if_error: false
name: ${{matrix.parallel_backend}}-${{matrix.cuda_support}}

build_cbind:
runs-on: ubuntu-22.04
if: github.event.pull_request.draft == false
container:
image: docker://nvidia/cuda:11.6.0-devel-ubuntu20.04
steps:
- name: Install dependencies
run: |
apt-get -y update
DEBIAN_FRONTEND=noninteractive apt install -y libomp-dev libassimp-dev git libtbb-dev pkg-config libpython3-dev python3 python3-distutils python3-pip
- uses: actions/checkout@v3
with:
submodules: true
- uses: jwlawson/[email protected]
- name: Build C bindings with OMP and CUDA
run: |
git apply thrust.diff
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DMANIFOLD_DEBUG=ON -DMANIFOLD_CBIND=ON -DMANIFOLD_PAR=OMP -DMANIFOLD_USE_CUDA=ON .. && make
- name: Test ${{matrix.parallel_backend}} with CUDA ${{matrix.cuda_support}}
# note that the test for CUDA backend does not really test CUDA, as we
# don't have CUDA GPU on GitHub Action
run: |
cd build/test
./manifold_test --gtest_filter=CBIND.*
build_wasm:
runs-on: ubuntu-20.04
if: github.event.pull_request.draft == false
Expand Down Expand Up @@ -144,7 +171,7 @@ jobs:
- name: Build ${{matrix.backend}}
shell: powershell
run: |
cmake . -DCMAKE_BUILD_TYPE=Release -B build -DMANIFOLD_DEBUG=ON -DMANIFOLD_PAR=${{matrix.parallel_backend}} -DMANIFOLD_USE_CUDA=${{matrix.cuda_support}} -A x64
cmake . -DCMAKE_BUILD_TYPE=Release -B build -DMANIFOLD_DEBUG=ON -DMANIFOLD_PYBIND=ON -DMANIFOLD_PAR=${{matrix.parallel_backend}} -DMANIFOLD_USE_CUDA=${{matrix.cuda_support}} -A x64
cd build
cmake --build . --target ALL_BUILD --config Release
- name: Test ${{matrix.parallel_backend}} with CUDA ${{matrix.cuda_support}}
Expand Down Expand Up @@ -175,7 +202,7 @@ jobs:
git apply thrust.diff
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DMANIFOLD_DEBUG=ON -DMANIFOLD_PAR=${{matrix.parallel_backend}} .. && make
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DMANIFOLD_DEBUG=ON -DMANIFOLD_PYBIND=ON -DMANIFOLD_PAR=${{matrix.parallel_backend}} .. && make
- name: Test
run: |
cd build/test
Expand All @@ -191,4 +218,3 @@ jobs:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v15
- run: nix build -L '.?submodules=1#manifold-${{matrix.variant}}'

27 changes: 20 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ project(manifold LANGUAGES CXX)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(MANIFOLD_EXPORT off)
option(MANIFOLD_DEBUG off)
option(MANIFOLD_USE_CUDA off)
option(MANIFOLD_EXPORT OFF)
option(MANIFOLD_DEBUG OFF)
option(MANIFOLD_USE_CUDA OFF)
option(MANIFOLD_PYBIND ON)
option(MANIFOLD_CBIND OFF)
set(MANIFOLD_PAR "NONE" CACHE STRING "Parallel backend, either \"TBB\" or \"OpenMP\" or \"NONE\"")

if(EMSCRIPTEN)
Expand All @@ -29,15 +31,26 @@ if(EMSCRIPTEN)
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} -sALLOW_MEMORY_GROWTH=1)
endif()

option(PYBIND11_FINDPYTHON on)
option(PYBIND11_FINDPYTHON ON)

option(BUILD_TEST_CGAL off)
option(BUILD_TEST_CGAL OFF)

option(BUILD_SHARED_LIBS off)
option(BUILD_SHARED_LIBS OFF)
set(GLM_INC_DIR ${PROJECT_SOURCE_DIR}/src/third_party/glm)
set(PYBIND11_DIR ${PROJECT_SOURCE_DIR}/bindings/python/third_party/pybind11)
set(THRUST_INC_DIR ${PROJECT_SOURCE_DIR}/src/third_party/thrust)

if(MANIFOLD_CBIND)
set(MANIFOLD_EXPORT ON)
endif()

if(BUILD_SHARED_LIBS OR MANIFOLD_CBIND)
# Allow shared libraries to be relocatable (add Place Independent Code flag).
# Also include when statically linking C bindings to avoid issues with bundling
# artefacts in host languages using them for FFI.
add_compile_options(-fPIC)
endif()

if(MANIFOLD_USE_CUDA)
enable_language(CUDA)
find_package(CUDA REQUIRED)
Expand Down Expand Up @@ -72,4 +85,4 @@ add_subdirectory(src)
add_subdirectory(samples)
add_subdirectory(test)
add_subdirectory(extras)
add_subdirectory(bindings)
add_subdirectory(bindings)
6 changes: 5 additions & 1 deletion bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

if(NOT EMSCRIPTEN)
if(NOT EMSCRIPTEN AND MANIFOLD_CBIND)
add_subdirectory(c)
endif()

if(NOT EMSCRIPTEN AND MANIFOLD_PYBIND)
add_subdirectory(python)
endif()

Expand Down
18 changes: 18 additions & 0 deletions bindings/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
project(manifoldc)


add_library(manifoldc manifoldc.cpp conv.cpp)

if(MANIFOLD_USE_CUDA)
set_source_files_properties(manifoldc.cpp conv.cpp PROPERTIES LANGUAGE CUDA)
set_property(TARGET manifoldc PROPERTY CUDA_ARCHITECTURES 61)
endif()

target_link_libraries(
manifoldc
PRIVATE manifold sdf meshIO graphlite
)

target_include_directories(manifoldc PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_compile_options(manifoldc PRIVATE ${MANIFOLD_FLAGS})
target_compile_features(manifoldc PRIVATE cxx_std_17)
94 changes: 94 additions & 0 deletions bindings/c/box.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <conv.h>
#include <manifold.h>
#include <public.h>

#include "types.h"

ManifoldBox *manifold_box(void *mem, float x1, float y1, float z1, float x2,
float y2, float z2) {
auto p1 = glm::vec3(x1, y1, z1);
auto p2 = glm::vec3(x2, y2, z2);
auto box = new (mem) Box(p1, p2);
return to_c(box);
}

ManifoldVec3 manifold_box_min(ManifoldBox *b) { return to_c((*from_c(b)).min); }

ManifoldVec3 manifold_box_max(ManifoldBox *b) { return to_c((*from_c(b)).max); }

ManifoldVec3 manifold_box_dimensions(ManifoldBox *b) {
auto box = *from_c(b);
auto v = box.Size();
return {v.x, v.y, v.z};
}

ManifoldVec3 manifold_box_center(ManifoldBox *b) {
auto box = *from_c(b);
auto v = box.Center();
return {v.x, v.y, v.z};
}

float manifold_box_scale(ManifoldBox *b) {
auto box = *from_c(b);
return box.Scale();
}

int manifold_box_contains_pt(ManifoldBox *b, float x, float y, float z) {
auto box = *from_c(b);
auto p = glm::vec3(x, y, z);
return box.Contains(p);
}

int manifold_box_contains_box(ManifoldBox *a, ManifoldBox *b) {
auto outer = *from_c(a);
auto inner = *from_c(b);
return outer.Contains(inner);
}

void manifold_box_include_pt(ManifoldBox *b, float x, float y, float z) {
auto box = *from_c(b);
auto p = glm::vec3(x, y, z);
box.Union(p);
}

ManifoldBox *manifold_box_union(void *mem, ManifoldBox *a, ManifoldBox *b) {
auto box = (*from_c(a)).Union(*from_c(b));
return to_c(new (mem) Box(box));
}

ManifoldBox *manifold_box_transform(void *mem, ManifoldBox *b, float x1,
float y1, float z1, float x2, float y2,
float z2, float x3, float y3, float z3,
float x4, float y4, float z4) {
auto mat = glm::mat4x3(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4);
auto transformed = (*from_c(b)).Transform(mat);
return to_c(new (mem) Box(transformed));
}

ManifoldBox *manifold_box_translate(void *mem, ManifoldBox *b, float x, float y,
float z) {
auto box = *from_c(b);
auto p = glm::vec3(x, y, z);
auto translated = (*from_c(b)) + p;
return to_c(new (mem) Box(translated));
}

ManifoldBox *manifold_box_mul(void *mem, ManifoldBox *b, float x, float y,
float z) {
auto box = *from_c(b);
auto p = glm::vec3(x, y, z);
auto scaled = (*from_c(b)) * p;
return to_c(new (mem) Box(scaled));
}

int manifold_box_does_overlap_pt(ManifoldBox *b, float x, float y, float z) {
auto box = *from_c(b);
auto p = glm::vec3(x, y, z);
return box.DoesOverlap(p);
}

int manifold_box_does_overlap_box(ManifoldBox *a, ManifoldBox *b) {
return (*from_c(a)).DoesOverlap(*from_c(b));
}

int manifold_box_is_finite(ManifoldBox *b) { return (*from_c(b)).IsFinite(); }
Loading

0 comments on commit a7b457d

Please sign in to comment.