# Makie bug with tetrahedral meshes

**URL:** https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834
**Category:** Visualization
**Created:** [June 22, 2020, 2:02am UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834 "2020-06-22T02:02:36Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [June 22, 2020, 2:02am UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/1 "2020-06-22T02:02:36Z")

</div>

In Makie.jl there is a bug with displaying tetrahedral meshes. Using the example from [TetGen.jl](https://github.com/JuliaGeometry/TetGen.jl)

 ![Screenshot_2020-06-21_21-56-56](https://global.discourse-cdn.com/julialang/original/3X/f/a/fa3a233cfc469e364333b3af1115db5c710fb46d.png)

Note that at the corners there are missing edges and faces in the plot.

However, I have verified that those edges/faces/simplices are part of the mesh data.

Therefore, it’s a bug with Makie for displaying the tetrahedral mesh, @sdanisch ?

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [June 22, 2020, 9:36am UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/2 "2020-06-22T09:36:30Z")

</div>

Looks correct to me:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/7/379d375fab8188327fbbde37de60cae54e2ac1e8.png)  
I might have fixed this issue “accidentally” in the newest release of GeometryBasics, so make sure you’re on the newest version of that 😉

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [June 22, 2020, 9:38am UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/3 "2020-06-22T09:38:36Z")

</div>

Oh, nvm it’s only a few that are hard to find:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/8/48b7dd5e5d175321f9ce59e59bcfe22cde42e4f0.png)

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [June 22, 2020, 11:01am UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/4 "2020-06-22T11:01:46Z")

</div>

How did you verify, that those exist?

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [June 22, 2020, 3:11pm UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/5 "2020-06-22T15:11:23Z")

</div>

Verified the existence with method I described in [Grassmann.jl A\b 3x faster than Julia's StaticArrays.jl](https://discourse.julialang.org/t/grassmann-jl-a-b-3x-faster-than-julias-staticarrays-jl/41451)

First, you need to convert the mesh into `Grassmann` format:

```nohighlight
using Grassmann
function mesh_init(m::GeometryBasics.Mesh)
    c,f = coordinates(m),faces(m)
    s = size(eltype(f))[1]; V = SubManifold(ℝ^s)
    p = ChainBundle([Chain{V,1}(1.0,k...) for k ∈ c])
    t = ChainBundle([Chain{p,1}(SVector(k)) for k ∈ f])
    return (p,t)
end

```

Then define the following `checkpoint` for determining the tetrahedron containing a point:

```nohighlight
function checkpoint(P::Chain{V,1},t) where V
    p = Manifold(t)
    for i ∈ 1:length(t)
        P ∈ Chain{V,1}(p[value(t[i])]) && (return i)
    end
    return 0
end

```

Then you can determine whether there exists a tetrahedron with point `P` or not (returns 0 if not).

```nohighlight
julia> p,t = mesh_init(result) # result from TetGen
(Λ¹⟨++++⟩×211, Λ¹Λ¹⟨++++⟩×211×566)

julia> checkpoint(Chain{SubManifold(ℝ^4),1}(1,2,2,2),t)
122

```

Note that I am using homogeneous coordinates with (1,x,y,z) coordinates for the points, so you will have to use 4 dimensional points with an additional 1 coordinate.

You will find that indeed the mesh data contains simplices having points in the problematic regions.

PS, it would be great if `Makie` could natively support `Grassmann` so I don’t need `GeometryBasics` as a compatibility layer. Instead of `GeometryBasics.Point` I prefer using `Grassmann.Chain{V,1}`, because I need that type in order to do my calculations. As you can see, I am using homogeneous coordinates with `Chain{V,1}` types in `Grassmann` to represent mesh data. Currently, I need to convert this mesh data back into `GeometryBasics.Point` for plotting (using cache), which works fine, but I would much prefer being able to work with `Grassmann.Chain{V,1}` directly without conversions.

```nohighlight
GLMakie.mesh(t, color=(:blue, 0.1), transparency=true)
wireframe!(ans[end][1])

```

As you can see, it’s possible to use `mesh` function on `Grassmann` data as well, but it requires the conversion process with caching I mentioned above. I’d prefer to have `Makie` work with `Grassmann` point data directly instead of converting to `GeometryTypes.Point`.

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [June 22, 2020, 5:58pm UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/6 "2020-06-22T17:58:32Z")

</div>

Interesting… Can you try the same with:

```julia
tetrahedral_mesh = ...
triangular_mesh = triangle_mesh(tetrahedral_mesh)
... figure out if triangle is present ...

```

Makie needs to triangulate the mesh before displaying. Since there aren’t many places that could just ignore a few triangles after that step, I’m pretty sure the triangulation is where triangles get skipped.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [June 22, 2020, 9:55pm UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/7 "2020-06-22T21:55:54Z")

</div>

Will try that out, but need to modify and extend my programming a bit for embedded manifolds.

Taking a closer look though, I am noticing a few more problems with the triangulation displayed.

There is definitely an issue with the `triangle_mesh` algorithm, I’ll investigate it a bit further later.

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [June 23, 2020, 8:28am UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/8 "2020-06-23T08:28:17Z")

</div>

Thank you 🙂  
Yeah, this shouldn’t really come as a surprise… This is backed by some old generic algorithms, and you’re probably one of the first to really use it^^  
This is where the magic should happen:  
[https://github.com/JuliaGeometry/GeometryBasics.jl/blob/master/src/geometry\_primitives.jl#L24](https://github.com/JuliaGeometry/GeometryBasics.jl/blob/master/src/geometry_primitives.jl#L24)

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [June 23, 2020, 8:34am UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/9 "2020-06-23T08:34:25Z")

</div>

There likely needs to be a specialization for `SimplexFace` of that method, since it needs to create a closed mesh from a simplex, while NgonFace tries to create an n-vert polygon.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [June 24, 2020, 9:38pm UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/10 "2020-06-24T21:38:29Z")

</div>

Alright, I made a new commit to `Grassmann` which allows me to check if a point is inside of an embedded simplex of a higher dimensional manifold (e.g. check if point is inside affine triangle).

Using this method, I found that indeed the triangles are missing from the `triangulate_mesh` result.

Not only are triangles missing, some of them are duplicates! There are triangles which appear twice.

Most likely, I believe that your algorithm has an issue with the orientation of simplices. I have made an algorithm for triangulation of a simplicial complex in `Grassmann` but it’s only for local geometries. It is one of my tasks to generalize this triangulation to global mesh geometries also. Therefore, I will also spend some more time later to investigate the issues with your algorithm to see what’s wrong.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [June 30, 2020, 5:27pm UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/11 "2020-06-30T17:27:05Z")

</div>

Got an algorithm which directly computes all the edges from a simplex mesh (of any dimension \> 1)

```plaintext
column(t,i) = getindex.(value(t),i)
columns(t) = column.(Ref(value(t)),Grassmann.list(1,ndims(t)))
function edges(t,cols=columns(t))
    np,N = length(points(t)),ndims(Manifold(t))
    A,M = spzeros(np,np),points(t)(Grassmann.list(N-1,N)...)
    for c ∈ Grassmann.combo(N,2)
        A += sparse(cols[c[1]],cols[c[2]],1,np,np)
    end
    f = findall(x->x>0,LinearAlgebra.triu(A+transpose(A)))
    [Chain{M,1}(SVector{2,Int}(f[n].I)) for n ∈ 1:length(f)]
end

```

Resulting in the correct line segments for the mesh

 ![Screenshot_2020-06-30_13-20-30](https://global.discourse-cdn.com/julialang/original/3X/a/0/a0b43436765e01d99b4e208efc65f0701e8376f3.jpeg)

However, your method involves first computing all the triangles first, and then the line segments. So this algorithm doesn’t directly translate over into your implementation. When I come up with a method similar to yours based on first getting the triangles, I’ll make a pull request.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [June 30, 2020, 6:11pm UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/12 "2020-06-30T18:11:27Z")

</div>

MeshCore can derive these adjacency relationships. Feel free to borrow.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [July 2, 2020, 8:15pm UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/13 "2020-07-02T20:15:03Z")

</div>

Anyone know how to obtain the boundary triangles from the `GeometryTypes.Mesh` or `TetGen`?

---

<div class="post-metadata">

### Author: ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)
#### Post date: [July 3, 2020, 3:27am UTC](https://discourse.julialang.org/t/makie-bug-with-tetrahedral-meshes/41834/14 "2020-07-03T03:27:52Z")

</div>

How have I missed the rather wonderful progress you have made with Finite Element methods in Julia?  
Last time I looked at FE in Julia things were at quite an early stage.  
👏 👏
