Skip to main content

An extension of the Flexible Collision Library

Project description

Coal — An extension of the Flexible Collision Library

Pipeline status Documentation Coverage report Conda Downloads Conda Version PyPI version black ruff

FCL was forked in 2015, creating a new project called HPP-FCL. Since then, a large part of the code has been rewritten or removed (unused and untested code), and new features have been introduced (see below). Due to these major changes, it was decided in 2024 to rename the HPP-FCL project to Coal.

If you use Coal in your projects and research papers, we would appreciate it if you would cite it.

New features

Compared to the original FCL library, the main new features are:

  • dedicated and efficient implementations of the GJK and the EPA algorithms (we do not rely on libccd)
  • the support of safety margins for collision detection
  • an accelerated version of collision detection à la Nesterov, which leads to increased performance (up to a factor of 2). More details are available in this paper
  • the computation of a lower bound of the distance between two objects when collision checking is performed, and no collision is found
  • the implementation of Python bindings for easy code prototyping
  • the support of new geometries such as height fields, capsules, ellipsoids, etc.
  • enhance reliability with the fix of a myriad of bugs
  • efficient computation of contact points and contact patches between objects
  • full support of object serialization via Boost.Serialization

Note: the broad phase was reintroduced by Justin Carpentier in 2022, based on the FCL version 0.7.0.

This project is now used in several robotics frameworks such as Pinocchio, an open-source library which implements efficient and versatile rigid-body dynamics algorithms, the Humanoid Path Planner, an open-source library for Motion and Manipulation Planning. Coal has recently also been used to develop Simple, a new (differentiable) and efficient simulator for robotics and beyond.

A high-performance library

Unlike the original FCL library, Coal implements the well-established GJK algorithm and its variants for collision detection and distance computation. These implementations lead to state-of-the-art performance, as shown in the figures below.

On the one hand, we have benchmarked Coal against major state-of-the-art software alternatives:

  1. the Bullet simulator,
  2. the original FCL library (used in the Drake framework),
  3. the libccd library (used in MuJoCo).

The results are depicted in the following figure, which notably shows that the accelerated variants of GJK largely outperform by a large margin (from 5x up to 15x times faster). Please notice that the y-axis is in log scale.

Coal vs the rest of the world

On the other hand, why do we care about dedicated collision detection solvers like GJK for the narrow phase? Why can't we simply formulate the collision detection problem as a quadratic problem and call an off-the-shelf optimization solver like ProxQP)? Here is why:

Coal vs generic QP solvers

One can observe that GJK-based approaches largely outperform solutions based on classic optimization solvers (e.g., QP solver like ProxQP), notably for large geometries composed of tens or hundreds of vertices.

Open-source projects relying on Coal

  • Pinocchio A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives.
  • IfcOpenShell Open source IFC library and geometry engine.
  • Crocoddyl A software to realize model predictive control for complex robotics platforms.
  • TSID A software that implements a Task Space Inverse Dynamics QP.
  • HPP A SDK that implements motion planners for humanoids and other robots.
  • Jiminy A simulator based on Pinocchio.
  • ocs2 A toolbox for Optimal Control for Switched Systems (OCS2)

Installation

Conda

Coal can be installed from the conda-forge channel:

conda install coal -c conda-forge

Docker

docker run --rm -it ghcr.io/coal-library/coal:devel

Build

You can find build instruction here.

C++ example

Both the C++ library and the python bindings can be installed as simply as conda -c conda-forge install coal. The .so library, include files and python bindings will then be installed under $CONDA_PREFIX/lib, $CONDA_PREFIX/include and $CONDA_PREFIX/lib/python3.XX/site-packages.

Here is an example of using Coal in C++:

#include "coal/math/transform.h"
#include "coal/mesh_loader/loader.h"
#include "coal/BVH/BVH_model.h"
#include "coal/collision.h"
#include "coal/collision_data.h"
#include <iostream>
#include <memory>

// Function to load a convex mesh from a `.obj`, `.stl` or `.dae` file.
//
// This function imports the object inside the file as a BVHModel, i.e. a point cloud
// which is hierarchically transformed into a tree of bounding volumes.
// The leaves of this tree are the individual points of the point cloud
// stored in the `.obj` file.
// This BVH can then be used for collision detection.
//
// For better computational efficiency, we sometimes prefer to work with
// the convex hull of the point cloud. This insures that the underlying object
// is convex and thus very fast collision detection algorithms such as
// GJK or EPA can be called with this object.
// Consequently, after creating the BVH structure from the point cloud, this function
// also computes its convex hull.
std::shared_ptr<coal::ConvexBase> loadConvexMesh(const std::string& file_name) {
  coal::NODE_TYPE bv_type = coal::BV_AABB;
  coal::MeshLoader loader(bv_type);
  coal::BVHModelPtr_t bvh = loader.load(file_name);
  bvh->buildConvexHull(true, "Qt");
  return bvh->convex;
}

int main() {
  // Create the coal shapes.
  // Coal supports many primitive shapes: boxes, spheres, capsules, cylinders, ellipsoids, cones, planes,
  // halfspace and convex meshes (i.e. convex hulls of clouds of points).
  // It also supports BVHs (bounding volumes hierarchies), height-fields and octrees.
  std::shared_ptr<coal::Ellipsoid> shape1 = std::make_shared<coal::Ellipsoid>(0.7, 1.0, 0.8);
  std::shared_ptr<coal::ConvexBase> shape2 = loadConvexMesh("../path/to/mesh/file.obj");

  // Define the shapes' placement in 3D space
  coal::Transform3s T1;
  T1.setQuatRotation(coal::Quaternion3f::UnitRandom());
  T1.setTranslation(coal::Vec3s::Random());
  coal::Transform3s T2 = coal::Transform3s::Identity();
  T2.setQuatRotation(coal::Quaternion3f::UnitRandom());
  T2.setTranslation(coal::Vec3s::Random());

  // Define collision requests and results.
  //
  // The collision request allows to set parameters for the collision pair.
  // For example, we can set a positive or negative security margin.
  // If the distance between the shapes is less than the security margin, the shapes
  // will be considered in collision.
  // Setting a positive security margin can be usefull in motion planning,
  // i.e to prevent shapes from getting too close to one another.
  // In physics simulation, allowing a negative security margin may be usefull to stabilize the simulation.
  coal::CollisionRequest col_req;
  col_req.security_margin = 1e-1;
  // A collision result stores the result of the collision test (signed distance between the shapes,
  // witness points location, normal etc.)
  coal::CollisionResult col_res;

  // Collision call
  coal::collide(shape1.get(), T1, shape2.get(), T2, col_req, col_res);

  // We can access the collision result once it has been populated
  std::cout << "Collision? " << col_res.isCollision() << "\n";
  if (col_res.isCollision()) {
    coal::Contact contact = col_res.getContact(0);
    // The penetration depth does **not** take into account the security margin.
    // Consequently, the penetration depth is the true signed distance which separates the shapes.
    // To have the distance which takes into account the security margin, we can simply add the two together.
    std::cout << "Penetration depth: " << contact.penetration_depth << "\n";
    std::cout << "Distance between the shapes including the security margin: " << contact.penetration_depth + col_req.security_margin << "\n";
    std::cout << "Witness point on shape1: " << contact.nearest_points[0].transpose() << "\n";
    std::cout << "Witness point on shape2: " << contact.nearest_points[1].transpose() << "\n";
    std::cout << "Normal: " << contact.normal.transpose() << "\n";
  }

  // Before calling another collision test, it is important to clear the previous results stored in the collision result.
  col_res.clear();

  return 0;
}

Python example

Here is the C++ example from above translated in python using the python bindings of Coal:

import numpy as np
import coal
# Optional:
# The Pinocchio library is a rigid body algorithms library and has a handy SE3 module.
# It can be installed as simply as `conda -c conda-forge install pinocchio`.
# Installing pinocchio also installs coal.
import pinocchio as pin

def loadConvexMesh(file_name: str):
    loader = coal.MeshLoader()
    bvh: coal.BVHModelBase = loader.load(file_name)
    bvh.buildConvexHull(True, "Qt")
    return bvh.convex

if __name__ == "__main__":
    # Create coal shapes
    shape1 = coal.Ellipsoid(0.7, 1.0, 0.8)
    shape2 = loadConvexMesh("../path/to/mesh/file.obj")

    # Define the shapes' placement in 3D space
    T1 = coal.Transform3s()
    T1.setTranslation(pin.SE3.Random().translation)
    T1.setRotation(pin.SE3.Random().rotation)
    T2 = coal.Transform3s();
    # Using np arrays also works
    T1.setTranslation(np.random.rand(3))
    T2.setRotation(pin.SE3.Random().rotation)

    # Define collision requests and results
    col_req = coal.CollisionRequest()
    col_res = coal.CollisionResult()

    # Collision call
    coal.collide(shape1, T1, shape2, T2, col_req, col_res)

    # Accessing the collision result once it has been populated
    print("Is collision? ", {col_res.isCollision()})
    if col_res.isCollision():
        contact: coal.Contact = col_res.getContact(0)
        print("Penetration depth: ", contact.penetration_depth)
        print("Distance between the shapes including the security margin: ", contact.penetration_depth + col_req.security_margin)
        print("Witness point shape1: ", contact.getNearestPoint1())
        print("Witness point shape2: ", contact.getNearestPoint2())
        print("Normal: ", contact.normal)

    # Before running another collision call, it is important to clear the old one
    col_res.clear()

Acknowledgments

The development of Coal is actively supported by the Gepetto team @LAAS-CNRS, the Willow team @INRIA and, to some extent, Eureka Robotics.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libcoal-3.0.2.tar.gz (1.5 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

libcoal-3.0.2-0-py3-none-manylinux_2_28_x86_64.whl (2.3 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

libcoal-3.0.2-0-py3-none-manylinux_2_28_aarch64.whl (2.3 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

libcoal-3.0.2-0-py3-none-macosx_11_0_arm64.whl (1.5 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

libcoal-3.0.2-0-py3-none-macosx_10_9_x86_64.whl (1.7 MB view details)

Uploaded Python 3macOS 10.9+ x86-64

File details

Details for the file libcoal-3.0.2.tar.gz.

File metadata

  • Download URL: libcoal-3.0.2.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for libcoal-3.0.2.tar.gz
Algorithm Hash digest
SHA256 1d48cfdce1157d4b89cf6a7215fc1b1e120d54c4a8d975cd9f45f2c8cedec275
MD5 f0daa7ab6d7ee1c1ca90097360df89fc
BLAKE2b-256 cc51cb68b16abd786e3ebb5e7e64036894a6f69ea8fe45c04a433e6d5462d60e

See more details on using hashes here.

Provenance

The following attestation bundles were made for libcoal-3.0.2.tar.gz:

Publisher: release.yml on cmake-wheel/libcoal

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libcoal-3.0.2-0-py3-none-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libcoal-3.0.2-0-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c4ca3fec02386e5c8ccc81030c44e74a546b06059886ce04bb6c16fe4628e9ba
MD5 7df3f03efebad834b8cb0f79684454ce
BLAKE2b-256 3c49c3bec783144c226b5ef3728ed66d7fc2d08c553922a3892591958284801a

See more details on using hashes here.

Provenance

The following attestation bundles were made for libcoal-3.0.2-0-py3-none-manylinux_2_28_x86_64.whl:

Publisher: release.yml on cmake-wheel/libcoal

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libcoal-3.0.2-0-py3-none-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for libcoal-3.0.2-0-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ed45722c07a3d23a346211f837856549dce11167928743eb6c73bcf17a369dd6
MD5 fcf7b6894120474e0014504d2d8999b9
BLAKE2b-256 e2e55b9605496e48a0437152196e5f200433d3904e59c899cab799a3c27bcd4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for libcoal-3.0.2-0-py3-none-manylinux_2_28_aarch64.whl:

Publisher: release.yml on cmake-wheel/libcoal

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libcoal-3.0.2-0-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for libcoal-3.0.2-0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 527c710c6215936f1a4b99ca1d01b4bb15c6b52980fa96cfa5f1fd1a7ef12393
MD5 ae84846c606d1b2b77864f467143bcff
BLAKE2b-256 bab03480197ba40cf9c6de71ca7f7a81a7504a40ca77a2b8604cbcc068f8f7ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for libcoal-3.0.2-0-py3-none-macosx_11_0_arm64.whl:

Publisher: release.yml on cmake-wheel/libcoal

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libcoal-3.0.2-0-py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for libcoal-3.0.2-0-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 16702fdd13942080c42c9565eb1b692618ce4456192a5bc497c585f9142138e5
MD5 c291cbfdbcac41d2c1e12fc606717cf8
BLAKE2b-256 e3ea6aa65497d00ec494bf1c5e121b59ad8cf6da308e0cf01271a9d7c614752c

See more details on using hashes here.

Provenance

The following attestation bundles were made for libcoal-3.0.2-0-py3-none-macosx_10_9_x86_64.whl:

Publisher: release.yml on cmake-wheel/libcoal

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page