Skip to content

Commit 97b4bc2

Browse files
committed
Added a C# binder to the project
1 parent 66f918a commit 97b4bc2

29 files changed

Lines changed: 197 additions & 24 deletions

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ doc/*.pdf
5454

5555

5656
.vscode/settings.json
57+
58+
# C# example
59+
examples/csharp/ezc3d_csharp_wrapper/
60+
examples/csharp/obj/
61+
examples/csharp/ezc3d_CSharp.*

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ option(BUILD_EXAMPLE "Build a C++ example" ON)
1111
option(BUILD_DOC "Build documentation" OFF)
1212
option(GET_OFFICIAL_DOCUMENTATION "Automatically download the C3D documentation" OFF)
1313
option(BUILD_TESTS "Build all tests." OFF) # Makes boolean 'test' available.
14-
option(BINDER_PYTHON3 "Build Python SWIG module" OFF)
14+
option(BINDER_CSHARP "Build CSharp SWIG module" OFF)
1515
option(BINDER_MATLAB "Build Matlab module" OFF)
16+
option(BINDER_PYTHON3 "Build Python SWIG module" OFF)
1617

1718
set(EZC3D_NAME ${PROJECT_NAME})
1819
set(EZC3D_ROOT_FOLDER ${PROJECT_SOURCE_DIR})
@@ -155,6 +156,9 @@ add_subdirectory(binding)
155156
# Examples
156157
if (BUILD_EXAMPLE)
157158
add_subdirectory(examples)
159+
if (BINDER_CSHARP)
160+
add_subdirectory(examples/csharp)
161+
endif()
158162
endif()
159163

160164

binding/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Add subdirectories for each language if desired
2-
if (BINDER_PYTHON3)
3-
add_subdirectory(python3)
2+
if (BINDER_CSHARP)
3+
add_subdirectory(csharp)
44
endif()
55

66
if (BINDER_MATLAB)
77
add_subdirectory(matlab)
88
endif()
9+
10+
if (BINDER_PYTHON3)
11+
add_subdirectory(python3)
12+
endif()
13+

binding/csharp/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
if(POLICY CMP0086)
2+
cmake_policy(SET CMP0086 NEW)
3+
endif()
4+
if(POLICY CMP0078)
5+
cmake_policy(SET CMP0078 NEW)
6+
endif()
7+
if(POLICY CMP0122)
8+
cmake_policy(SET CMP0122 NEW)
9+
endif()
10+
11+
project(${EZC3D_NAME}_CSharp)
12+
set(CSHARP_PROJECT_NAME "${PROJECT_NAME}" CACHE INTERNAL "Name of the C# swig interface project")
13+
14+
find_package(SWIG REQUIRED)
15+
include(${SWIG_USE_FILE})
16+
17+
# Add swig module
18+
set(CMAKE_SWIG_FLAGS "")
19+
set(I_SOURCE_FILE "${EZC3D_NAME}_csharp.i")
20+
21+
set_property(SOURCE "${I_SOURCE_FILE}"
22+
PROPERTY CPLUSPLUS ON
23+
)
24+
25+
# Create an output directory for the generated C# files
26+
set(CSHARP_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "Directory where the generated C# files will be created")
27+
set(CSHARP_GENERATED_FOLDER_NAME "${EZC3D_NAME}_csharp_wrapper" CACHE INTERNAL "Name of the folder where the generated C# files will be created")
28+
file(MAKE_DIRECTORY ${CSHARP_BUILD_DIR}/${CSHARP_GENERATED_FOLDER_NAME})
29+
set(CMAKE_SWIG_OUTDIR ${CSHARP_BUILD_DIR}/${CSHARP_GENERATED_FOLDER_NAME})
30+
31+
# Generate the wrapper
32+
SWIG_ADD_LIBRARY(${PROJECT_NAME}
33+
TYPE MODULE
34+
LANGUAGE "csharp"
35+
SOURCES "${I_SOURCE_FILE}"
36+
)
37+
swig_link_libraries(${PROJECT_NAME}
38+
"${EZC3D_NAME}"
39+
)
40+
41+
# Add headers
42+
set_target_properties(${PROJECT_NAME} PROPERTIES
43+
SWIG_INCLUDE_DIRECTORIES
44+
"${CMAKE_CURRENT_SOURCE_DIR}/../../include;${CMAKE_CURRENT_SOURCE_DIR}/../../include/ezc3d;${EZC3D_BINARY_DIR}/include;${EZC3D_BINARY_DIR}/include/ezc3d"
45+
)

binding/csharp/ezc3d_csharp.i

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
%{
3+
#define SWIG_FILE_WITH_INIT
4+
5+
#include "ezc3d/ezc3d.h"
6+
#include "ezc3d/Header.h"
7+
#include "ezc3d/Data.h"
8+
#include "ezc3d/Parameters.h"
9+
#include "ezc3d/RotationsInfo.h"
10+
%}
11+
12+
// Rename the "lock" and "unlock" methods to avoid conflict with C# keywords
13+
%rename(LockGroup) ezc3d::ParametersNS::GroupNS::Group::lock;
14+
%rename(UnlockGroup) ezc3d::ParametersNS::GroupNS::Group::unlock;
15+
%rename(LockParameter) ezc3d::ParametersNS::GroupNS::Parameter::lock;
16+
%rename(UnlockParameter) ezc3d::ParametersNS::GroupNS::Parameter::unlock;
17+
18+
%module ezc3d_csharp
19+
%include ../ezc3d.i

binding/ezc3d.i

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// File : ezc3d.i
2-
%module ezc3d
32
%{
43
#include "ezc3d_all.h"
54
#include "modules/ForcePlatforms.h"
@@ -8,7 +7,6 @@
87
// Instantiate from standard library
98
%include <std_vector.i>
109
%include <std_string.i>
11-
%include <std_iostream.i>
1210
%include <std_except.i>
1311

1412
// Instantiate templates

binding/python3/ezc3d_python.i

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,5 @@ PyArrayObject *helper_getPyArrayObject( PyObject *input, int type) {
514514
};
515515
}
516516

517+
%module ezc3d
517518
%include ../ezc3d.i
518-
519-
520-

examples/csharp/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copy the generated DLL to the C# project directory after building
2+
project(${EZC3D_NAME}_CSharpExample LANGUAGES CSharp)
3+
4+
add_custom_target(
5+
CopyCSharpExampleFiles ALL
6+
DEPENDS ${CSHARP_PROJECT_NAME} ${EZC3D_NAME}
7+
)
8+
9+
# Paths
10+
set(SWIG_GENERATED_OUTPUT_DIR "${CSHARP_BUILD_DIR}/${CSHARP_GENERATED_FOLDER_NAME}")
11+
set(SWIG_LIBRARY_OUTPUT_DIR "${CSHARP_BUILD_DIR}/$<CONFIG>")
12+
set(EXAMPLE_OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
13+
14+
# Copy generated C# files
15+
add_custom_command(TARGET CopyCSharpExampleFiles
16+
DEPENDS ${CSHARP_PROJECT_NAME} ${EZC3D_NAME}
17+
COMMAND ${CMAKE_COMMAND} -E copy_directory
18+
"${SWIG_GENERATED_OUTPUT_DIR}"
19+
"${EXAMPLE_OUTPUT_DIR}/${CSHARP_GENERATED_FOLDER_NAME}"
20+
COMMENT "Copying SWIG-generated C# files..."
21+
)
22+
23+
# Copy compiled library
24+
add_custom_command(TARGET CopyCSharpExampleFiles
25+
DEPENDS ${CSHARP_PROJECT_NAME} ${EZC3D_NAME}
26+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
27+
"${SWIG_LIBRARY_OUTPUT_DIR}/${CSHARP_PROJECT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}"
28+
"${EXAMPLE_OUTPUT_DIR}"
29+
COMMENT "Copying compiled library..."
30+
)

examples/csharp/Ezc3dExample.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
class Ezc3dExample {
4+
static void Main() {
5+
var file = new c3d("../../test/c3dFiles/ezc3d-testFiles-master/ezc3d-testFiles-master/Vicon.c3d");
6+
7+
// Showcasing how to get header information
8+
Console.WriteLine("Header information:");
9+
Console.WriteLine("- Number of points: " + file.header().nb3dPoints());
10+
11+
// Showcasing how to get parameters
12+
Console.WriteLine("Available parameters:");
13+
for (uint i = 0; i < file.parameters().nbGroups(); i++) {
14+
Console.WriteLine("- " + file.parameters().group(i).name());
15+
}
16+
17+
var used = file.parameters().group("POINT").parameter("USED").valuesAsInt();
18+
Console.WriteLine("Number of points: " + used[0]);
19+
20+
// Showcasing how to get data
21+
Console.WriteLine("First point of the first frame:");
22+
var point = file.data().frame(0).points().point(0);
23+
Console.WriteLine("- X: " + point.x());
24+
Console.WriteLine("- Y: " + point.y());
25+
Console.WriteLine("- Z: " + point.z());
26+
}
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

0 commit comments

Comments
 (0)