You are viewing the documentation for Deformable v0.2.0, an older version. Read the latest, v0.9.0
C SDK
Getting started with C
Download the C SDK, compile against deformable.h with a compiler, make or CMake, and run your first simulation from C.
On this page
The C SDK brings the Tangerina Deformable engine into your own C or C++ tools. It is libdeformable, a shared and a
static library, and one header, deformable.h. The header is plain C99, and it wraps its declarations in extern "C",
so C++ code can include it too. The library has the same scene, mesh tools and render surfaces as the Rust and Python
SDKs. What they do, and why, is in the Engine guide, starting with
How the engine works. This guide shows how to use them from C;
the C API Reference documents every type, constant and function.
Requirements
- A C99 compiler, such as Clang, GCC or MSVC.
- Optional:
make, or CMake 3.18 or later. - macOS on Apple silicon, Linux x64 or Windows x64.
Download the C SDK
Download the C SDK zip for your platform from the download page, for
example deformable-c-v0.1.0-macos-arm64.zip, and unzip it. It holds one folder, named like the zip:
| Path | What it is |
|---|---|
include/deformable.h |
The header. Every type, constant and function of the C SDK. |
lib/ |
The shared and the static library. See the table below. |
lib/cmake/deformable/ |
The CMake package. find_ gives the targets deformable and deformable. |
README.txt |
A short guide to the SDK. |
LICENSE.md |
The license. |
The lib/ folder holds these files:
| Platform | Shared library | Static library |
|---|---|---|
| macOS | libdeformable.dylib |
libdeformable.a |
| Linux | libdeformable.so |
libdeformable.a |
| Windows | deformable.dll, with the import library deformable.dll.lib |
deformable.lib |
The commands on this page name the unzipped folder $DEFORMABLE. Set it once in your shell:
export DEFORMABLE=/path/to/deformable-c-v0.1.0-macos-arm64On macOS, clear the download mark from the folder once. A browser marks a downloaded zip, unzipping copies the mark
onto each file, and macOS will not load a marked library that is not notarized, so without this step a program linked
with libdeformable.dylib never starts:
xattr -dr com.apple.quarantine "$DEFORMABLE"Your first simulation
This program builds a closed unit cube, fills it with tetrahedrons, and lets it fall under gravity for one second.
Save it as falling_cube.c.
#include <stdio.h>
#include "deformable.h"
/* Prints the last error of this thread and returns 1, for main to return. */
static int fail(const char *call) {
fprintf(stderr, "%s failed: %s\n", call, deformable_last_error_message());
return 1;
}
int main(void) {
/* A closed unit cube: 8 corners and 12 outward triangles. */
DeformableVector3 corners[8];
for (int corner = 0; corner < 8; ++corner) {
corners[corner].x = (double)(corner & 1);
corners[corner].y = (double)((corner >> 1) & 1);
corners[corner].z = (double)(corner >> 2);
}
const uint32_t triangles[12][3] = {
{0, 2, 3}, {0, 3, 1}, {4, 5, 7}, {4, 7, 6}, {0, 1, 5}, {0, 5, 4},
{2, 6, 7}, {2, 7, 3}, {0, 4, 6}, {0, 6, 2}, {1, 3, 7}, {1, 7, 5},
};
DeformableTriangleMeshView cage = {corners, 8, triangles, 12};
/* Fill the cage with tetrahedrons. */
DeformableTetrahedralizeParameters parameters = {0};
parameters.maximum_edge_length = 0.5;
deformable_anisotropy_default(¶meters.anisotropy[0]);
deformable_anisotropy_default(¶meters.anisotropy[1]);
DeformableTetrahedralization *tetrahedralization = NULL;
if (deformable_tetrahedralize(&cage, ¶meters, NULL, &tetrahedralization) != DEFORMABLE_STATUS_OK) {
return fail("deformable_tetrahedralize");
}
DeformableTetrahedralizationView result;
deformable_tetrahedralization_view(tetrahedralization, &result);
DeformableScene *scene = NULL;
if (deformable_scene_create(&scene) != DEFORMABLE_STATUS_OK) {
deformable_tetrahedralization_destroy(tetrahedralization);
return fail("deformable_scene_create");
}
DeformableMaterialParameters material_parameters;
DeformableObjectOptions options;
DeformableTetrahedronMaterials materials = {0};
uint32_t cube = 0;
deformable_material_parameters_default(&material_parameters);
deformable_object_options_default(&options);
materials.assignment = DEFORMABLE_MATERIAL_ASSIGNMENT_UNIFORM;
if (deformable_scene_add_material(scene, &material_parameters, &materials.material) !=
DEFORMABLE_STATUS_OK ||
deformable_scene_add_object(scene, &result.mesh, &materials, &options, &cube) !=
DEFORMABLE_STATUS_OK) {
int code = fail("adding the cube");
deformable_scene_destroy(scene);
deformable_tetrahedralization_destroy(tetrahedralization);
return code;
}
/* The scene copied the mesh, so the tetrahedralization can go. */
deformable_tetrahedralization_destroy(tetrahedralization);
/* 24 frames per second, 10 substeps per frame. The first call only records the time. */
for (int frame = 0; frame <= 24; ++frame) {
DeformableStepReport report;
if (deformable_scene_step(scene, frame / 24.0, 10, NULL, &report) != DEFORMABLE_STATUS_OK) {
int code = fail("deformable_scene_step");
deformable_scene_destroy(scene);
return code;
}
}
const DeformableVector3 *positions = NULL;
size_t count = 0;
deformable_scene_node_positions(scene, cube, &positions, &count);
printf("deformable %s: %zu nodes, first node at y = %.3f\n", deformable_version(), count, positions[0].y);
deformable_scene_destroy(scene);
return 0;
}Compile and run
Run these commands in the folder that holds falling_cube.c.
Shared library. Point the compiler at the header and the linker at the library. The run path lets the program find the library in the SDK folder when it starts:
cc -std=c99 -I "$DEFORMABLE/include" falling_cube.c -L "$DEFORMABLE/lib" -ldeformable -Wl,-rpath,"$DEFORMABLE/lib" -o falling_cube
./falling_cubedeformable 0.1.0: 9 nodes, first node at y = -4.925On Windows, with MSVC, link the import library and put deformable.dll next to the program:
cl /I %DEFORMABLE%\include falling_cube.c %DEFORMABLE%\lib\deformable.dll.libStatic library. Pass the archive itself. The library is written in Rust, so the program also needs the system libraries that Rust’s standard library uses. On macOS this is only the math library:
cc -std=c99 -I "$DEFORMABLE/include" falling_cube.c "$DEFORMABLE/lib/libdeformable.a" -lm -o falling_cubeThe CMake target deformable::static lists the system libraries for its platform, so with CMake you need not know
them. The static library needs nothing at run time: the program carries the library inside it.
Build with make
This Makefile builds the same program with the built-in rules of make. Set DEFORMABLE to the unzipped SDK
folder:
DEFORMABLE = /path/to/deformable-c-v0.1.0-macos-arm64
CFLAGS = -std=c99 -Wall -Wextra -I $(DEFORMABLE)/include
LDFLAGS = -L $(DEFORMABLE)/lib -Wl,-rpath,$(DEFORMABLE)/lib
LDLIBS = -ldeformable
falling_cube: falling_cube.cmake
./falling_cubeBuild with CMake
The SDK holds a CMake package with two imported targets. deformable::deformable links the shared library, and
deformable::static links the static library with the system libraries it needs. Both add the header folder to the
include path. Find the package and link a target:
cmake_minimum_required(VERSION 3.18)
project(falling_cube LANGUAGES C)
find_package(deformable 0.1 CONFIG REQUIRED)
add_executable(falling_cube falling_cube.c)
target_link_libraries(falling_cube PRIVATE deformable::deformable)Add the SDK folder to CMAKE_PREFIX_PATH so CMake finds the package:
cmake -S . -B build -DCMAKE_PREFIX_PATH="$DEFORMABLE"
cmake --build build
./build/falling_cubefind_package refuses an SDK whose version does not match the one you ask for.
Ship the shared library
A program you give to others needs the shared library next to it. Copy the library next to the program, and give the program a run path that points there:
| Platform | File | Link with |
|---|---|---|
| macOS | libdeformable.dylib |
-Wl,-rpath,@executable_. The library’s install name is @rpath/libdeformable.dylib. |
| Linux | libdeformable.so |
-Wl,-rpath,'$ORIGIN' |
| Windows | deformable.dll |
Nothing: Windows looks in the executable’s folder first. |
On macOS:
mkdir -p app
cp "$DEFORMABLE/lib/libdeformable.dylib" app/
cc -std=c99 -I "$DEFORMABLE/include" falling_cube.c -L "$DEFORMABLE/lib" -ldeformable -Wl,-rpath,@executable_path -o app/falling_cube
./app/falling_cubeThe static library needs none of this: the program carries the library inside it.
The Standard trial and the Standard license belong to each computer that runs the program. On a colleague’s computer, a server or a render farm node, it starts its own Standard trial and needs its own seat to keep running after 7 days. To ship the engine inside an application you give to others, you need a Developer license: contact us. Your application then activates its key at every start, offline, and its users need no activation, network or seat. See Developer license.
What the program does
deformable_tetrahedralizefills the closed triangle cage with tetrahedrons. It writes a new handle, aDeformableTetrahedralization *.deformable_tetrahedralization_viewlends its arrays.deformable_scene_createmakes an empty scene with gravity (0, -9.81, 0).deformable_scene_add_materialanddeformable_scene_add_objectwrite a material handle and an object handle. Both areuint32_tvalues that count up from 0.- The scene copies the mesh, so the program destroys the tetrahedralization right after
deformable_scene_add_object. deformable_scene_stepadvances the scene to a time in seconds. The first call only records the start time.deformable_scene_node_positionslends the current node positions. The scene owns the array.deformable_scene_destroyfrees the scene and every array it lent.
The cube falls about 4.9 units in one second. Nothing stops it, because the scene has no floor. Scene adds one.
The cage has only 12 triangles, and deformable_tetrahedralize keeps the cage triangles as the boundary of the
mesh, so the cube gets only 12 tetrahedrons. Tools shows how to triangulate the cage
first for a finer mesh.
License
The C SDK works with both licenses. It comes with the free 7-day Standard trial: every feature, up to 2,500
tetrahedrons per scene. The first call that does engine work, such as deformable_tetrahedralize above, starts the
trial with our licensing service, so the computer must be online that one time. After that it works offline, but a
Standard license renews at least once every 7 days of use, by itself whenever the program runs online. The Standard
trial and license are shared with the plugins and the other SDKs on the same computer. An application you ship to
others uses a Developer license instead: it activates the Developer key at every start, offline, as its first Deformable
call, before status and refresh. Licensing has the whole story.
Five functions read and change the license state. This program starts the Standard trial, or renews the Standard license, when that is due, and prints the status line:
#include <stdio.h>
#include "deformable.h"
int main(void) {
DeformableLicenseStatus status;
if (deformable_license_refresh(15000, &status) != DEFORMABLE_STATUS_OK) {
fprintf(stderr, "%s\n", deformable_last_error_message());
return 1;
}
printf("%s\n", status.message);
return 0;
}Standard trial: 7 days left| Function | What it does |
|---|---|
deformable_ |
Fills a Deformable: the state, days_, tetrahedron_ (0 without a limit), usable, needs_, has_ and the status line message. It never goes online. |
deformable_ |
Starts the Standard trial, or renews the Standard license, when that is due. On a Standard license, call it when your program starts. A program on a Developer license never needs it: it activates its key first. |
deformable_ |
Turns a license key on. A Standard key, such as "TGDF-XXXX-XXXX-XXXX", is activated on this computer and needs internet. A Developer key, "TGDF-<payload>.<signature>", is checked offline and holds for this run of the program, so call it at every start. |
deformable_ |
Frees this computer’s Standard seat. Needs internet. |
deformable_ |
With 0, keeps the program off the network: nothing is sent, and the license works only as far as it works offline. |
The timeouts are in milliseconds: one above 60,000 counts as 60,000, and 0 gives DEFORMABLE_STATUS_INVALID_ARGUMENT.
When the license does not allow a call, it returns a
status such as DEFORMABLE_STATUS_LICENSE_REQUIRED or DEFORMABLE_STATUS_TRIAL_LIMIT_EXCEEDED, and
deformable_last_error_message gives the status line, such as Standard trial ended — Buy license. See
Memory and errors.
Conventions
These rules hold for every function. Memory and errors covers them in full.
- Status. A function that can fail returns a
DeformableStatus:DEFORMABLE_STATUS_OK(0) on success, a positive code otherwise.deformable_last_error_messagethen describes the failure. - Handles. A
_createfunction or a tool makes a handle; the matching_destroyfunction frees it. - Arrays. You pass arrays as a pointer and a count, borrowed for the call. The library lends its own arrays; you never free them.
- Booleans and enumerations. Booleans are
uint8_t, 0 for false and anything else for true. Enumerations areint32_tcodes withDEFORMABLE_constants. - Units. Lengths are in your units. Time, forces, gravity, masses and material values are in simulation units. The scene’s scale converts your lengths to simulation lengths. See Units and axes.
- License. Engine calls check the trial or license of this computer, and return a license status when it does
not allow them. The
deformable_license_functions read and change it. - Threads. A scene takes one call at a time. Tools, embeddings and cancel flags work from several threads at once. The last error is per thread.
Next steps
- How the engine works: the ideas the plugins and every SDK share, explained once in the Engine guide.
- Scene: materials, objects, stepping and outputs.
- Tools: the mesh tools that prepare a scene’s inputs.
- Render surfaces: skin a render mesh on a simulated object.
- Memory and errors: ownership, handles, status codes and threads.
- C API Reference: every type, constant and function of
deformable.h.