# Calling C++ library (CGAL) from Julia

**URL:** <https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629>\
**Category:** General Usage\
**Created:** [March 8, 2019, 11:46am UTC](https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629 "2019-03-08T11:46:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![timmm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timmm/32/6760_2.png) [@timmm](https://discourse.julialang.org/u/timmm)\
**Post date:** [March 8, 2019, 11:46am UTC](https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629/1 "2019-03-08T11:46:31Z")

</div>

Hi,

What would be the best route for me to call the C++ library CGAL ([https://www.cgal.org/](https://www.cgal.org/)) from Julia? Does anyone have experience calling this specific library? I know there are a few different packages to call C++ from Julia but I am not sure which is best? Suggestions of which packages to use for this would be great.

I would be nice if it worked across all platforms too, I saw that in the help of [GitHub - JuliaInterop/Cxx.jl: The Julia C++ Interface](https://github.com/Keno/Cxx.jl) only works on specific Linux distributions.

It also looks like there is a Python way of calling CGAL so I could get to this through PyCall? I would prefer calling C++ straight from Julia if possible.

Cheers,  
Tim

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [March 8, 2019, 12:18pm UTC](https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629/2 "2019-03-08T12:18:25Z")

</div>

Hi,

CGAL is a very big library, what specific part(s) are you interested in? I would certainly recommend calling C++ directly, the parts they wrap in Python should also be easy to wrap using CxxWrap.jl or Cxx.jl.

Cheers,

Bart

---

<div class="post-metadata">

**Author:** ![timmm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timmm/32/6760_2.png) [@timmm](https://discourse.julialang.org/u/timmm)\
**Post date:** [March 8, 2019, 12:46pm UTC](https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629/3 "2019-03-08T12:46:09Z")

</div>

Great, sounds good. I am interested in the Advancing front surface reconstruction algorithm (used it before in a package and its amazing) and some of the mesh simplification/resampling algorithms.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 8, 2019, 1:01pm UTC](https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629/4 "2019-03-08T13:01:07Z")

</div>

If you’re interested in a relatively small number of functions, you might want to consider writing a thin C wrapper around those functions (using `extern "C"` in your C/C++ code). This has the disadvantage of requiring some manual work, vut the benefit that calling a C shared library from Julia is extremely easy (using the built-in `ccall`) and works everywhere.

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [March 8, 2019, 2:42pm UTC](https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629/5 "2019-03-08T14:42:13Z")

</div>

> [@timmm](#):
>
> only works on specific Linux distributions.

did you read [issue #390](https://github.com/Keno/Cxx.jl/issues/390) and tried the solutions discussed in the comments? at least the binary-build solution should work fine on macOS and any Linux distributions that Julia is running on.

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [March 8, 2019, 4:32pm UTC](https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629/6 "2019-03-08T16:32:09Z")

</div>

> [@timmm](#):
>
> Great, sounds good. I am interested in the Advancing front surface reconstruction algorithm (used it before in a package and its amazing) and some of the mesh simplification/resampling algorithms.

I looked briefly at the interface for the Advancing front reconstruction, and I think it would be quite doable using CxxWrap and a custom C++ class to encapsulate the point set (e.g. adding iterators to a Julia array containing the points). Possibly a nicer way would be to store the points as an array of tuples or even fixed size arrays in Julia, but converting that without copying would take some adjustments in CxxWrap (which I am willing to do).

Either way, I think your final decision on how to wrap this will probably depend on how you want to store the input data on the Julia side.

---

<div class="post-metadata">

**Author:** ![timmm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timmm/32/6760_2.png) [@timmm](https://discourse.julialang.org/u/timmm)\
**Post date:** [March 8, 2019, 7:03pm UTC](https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629/7 "2019-03-08T19:03:37Z")

</div>

OK, thanks for the comments, it looks like I have a number of options. Currently my input data in Julia is just a 3\*n array of the points (X,Y,Z) locations. e.g.

```julia
xyz=(Float64[1 1 0; 1 0 0;5 7 0;8 9 0;1 2 0])
5×3 Array{Float64,2}:
 1.0 1.0 0.0
 1.0 0.0 0.0
 5.0 7.0 0.0
 8.0 9.0 0.0
 1.0 2.0 0.0

```

To check i have understood, to use C++ wrap I now need to write a custom C++ class that defines these points?

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [March 8, 2019, 10:21pm UTC](https://discourse.julialang.org/t/calling-c-library-cgal-from-julia/21629/8 "2019-03-08T22:21:58Z")

</div>

No, I mean a class that can wrap any array like that, for example:

```nohighlight
class PointList
{
public:
  PointList(jlcxx::ArrayRef<double,2> points) : m_points(points) {}

  void print_points()
  {
    const std::size_t nb_points = m_points.size() / 3; // assuming 3D points
    for(std::size_t i = 0; i != nb_points; ++i)
    {
      std::cout << m_points[i] << ", " << m_points[i+nb_points] << ", " << m_points[i+2*nb_points] << std::endl;
    }
  }

private:
  jlcxx::ArrayRef<double,2> m_points;
};

```

In CxxWrap this is wrapped as:

```nohighlight
  mod.add_type<PointList>("PointList")
    .constructor<ArrayRef<double,2>>()
    .method("print_points", &PointList::print_points);

```

And then used in Julia as:

```julia
xyz= Float64[1 1 0; 1 0 0;5 7 0;8 9 0;1 2 0]
pointlist = PointList(xyz)
print_points(pointlist)

```

Instead of the toy `print_points` function, the `PointList` class needs a C++ iterator function, so its begin and end iterator can be passed to the first two arguments of `CGAL::advancing_front_surface_reconstruction`. The advantage of this approach is that the points are not copied, the downside is that writing iterators in C++ is quite painful.

A simpler but more wasteful option would be to just copy the points from an `ArrayRef` to a `std::vector<Point_3>`.
