# Function to re-orient mesh faces

**URL:** <https://discourse.julialang.org/t/function-to-re-orient-mesh-faces/6499>\
**Category:** Visualization\
**Created:** [October 17, 2017, 11:02am UTC](https://discourse.julialang.org/t/function-to-re-orient-mesh-faces/6499 "2017-10-17T11:02:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaakkor2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaakkor2/32/2540_2.png) [@jaakkor2](https://discourse.julialang.org/u/jaakkor2)\
**Post date:** [October 17, 2017, 11:02am UTC](https://discourse.julialang.org/t/function-to-re-orient-mesh-faces/6499/1 "2017-10-17T11:02:54Z")

</div>

Tried to find Julia implementation of re-orienting faces of a mesh. Does an implementation exist?

Geometries from external sources not necessary have their faces oriented consistently (even if possible). If visualized in GLVisualize.jl, coloring vertices of faces not oriented consistently does not come out nicely.

Example Julia code using GLVisualize with a screenshot linked with non-consistent mesh [https://i.stack.imgur.com/N9lS1.png](https://i.stack.imgur.com/N9lS1.png) .

```julia
using GLVisualize, GeometryTypes, GLWindow, GLAbstraction, Colors

vertices = [
 0.0 0.0 0.0
10.0 0.0 0.0
10.0 20.0 0.0
 0.0 20.0 0.0
 0.0 0.0 5.0
10.0 0.0 5.0
10.0 20.0 5.0
 0.0 20.0 5.0
]

faces = [
7 6 5 # 5 6 7 for consistent orientation
5 7 8
1 4 3 
1 3 2
1 2 6
1 6 5
2 3 7
2 7 6
3 4 8
3 8 7
4 1 5
4 5 8
]

v = [Point{3,Float32}( vertices[i,1:3] ) for i=1:size(vertices,1) ]
f = [Face{3,UInt32}( faces[i,3:-1:1] ) for i=1:size(faces,1) ]

mesh = GLNormalAttributeMesh(
            vertices=v, faces=f,
            attributes=RGBA{Float32}[RGBA{Float32}( 0.0, 1.0, 0.0, 1.0 )], attribute_id=zeros( length( v ) )
        )

window = glscreen()
_view( visualize( mesh ), window )

```

I could write a slow brute-force algorithm, which would be slow. Better to ask first.

A recent article suggests the algorithm can be O(N), see [https://arxiv.org/pdf/1512.02137.pdf](https://arxiv.org/pdf/1512.02137.pdf)

Note: one should be careful not to look at source implementation of mesh re-orientation in software with non-compatible licenses.

Note2: copied the topic from [mesh - Function to re-orient faces in Julia? - Stack Overflow](https://stackoverflow.com/questions/46759661/function-to-re-orient-faces-in-julia) based on the suggestion there.

---

<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:** [October 17, 2017, 4:07pm UTC](https://discourse.julialang.org/t/function-to-re-orient-mesh-faces/6499/2 "2017-10-17T16:07:09Z")

</div>

There is an open-source implementation this algorithm (using a stack) in my BESS library: [http://hogwarts.ucsd.edu/~pkrysl/software/BESS.tar.gz](http://hogwarts.ucsd.edu/~pkrysl/software/BESS.tar.gz)

---

<div class="post-metadata">

**Author:** ![jaakkor2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaakkor2/32/2540_2.png) [@jaakkor2](https://discourse.julialang.org/u/jaakkor2)\
**Post date:** [October 18, 2017, 7:51am UTC](https://discourse.julialang.org/t/function-to-re-orient-mesh-faces/6499/4 "2017-10-18T07:51:17Z")

</div>

Thanks Petr for link.

I’ll have a look. Based on a very quick browse, is it in `bess.c` function `start_manifold_s` ?

---

<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:** [October 18, 2017, 2:30pm UTC](https://discourse.julialang.org/t/function-to-re-orient-mesh-faces/6499/5 "2017-10-18T14:30:51Z")

</div>

I would start with `smesh_orient()` in the file `smesh.c`. The description of the algorithm is in our paper [[http://onlinelibrary.wiley.com/doi/10.1002/nme.94/abstract](http://onlinelibrary.wiley.com/doi/10.1002/nme.94/abstract)]. The relevant algorithm is this

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/9/b900f9a2b06a6e470dc2aebc92ec22fecbb400d1.png)  
mf = mesh face, me = mesh edge

---

<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:** [October 18, 2017, 2:51pm UTC](https://discourse.julialang.org/t/function-to-re-orient-mesh-faces/6499/6 "2017-10-18T14:51:12Z")

</div>

To compare with the arxiv paper:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/a/1ac3059f901d7b61fb4c50581c2d84e1ad3ca958.jpeg)
