# Export P1 function on a mesh to Mathematica

**URL:** <https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485>\
**Category:** Questions\
**Tags:** graphics-and-visualization\
**Created:** [January 23, 2022, 10:21am UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485 "2022-01-23T10:21:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophetroestler](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@christophetroestler](https://community.wolfram.com/u/christophetroestler)\
**Post date:** [January 23, 2022, 10:21am UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/1 "2022-01-23T10:21:00Z")

</div>

Hi,

I’m writing a library (in Rust) to handle triangular meshes and P1 functions on them and would like to offer an export to Mathematica, in particular to visualize the functions. There used to be `TriangularSurfacePlot` for that but that [functionality is now built in](https://reference.wolfram.com/language/ComputationalGeometry/ref/TriangularSurfacePlot.html) and I could not find

- what Mathematica datastructures I should use to export the mesh (coordinates of the points, list of triangles or adjacency matrix) and the P1 function (values at each point of the mesh);

- what function(s) I should call to produce a nice graphic (similar to Matlab `trisurf`, shown below).

Thanks!

 ![graph](https://global.discourse-cdn.com/wolfram/original/3X/6/8/68102c055ce45bf312131753a85505bb84b1b544.png)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [January 23, 2022, 3:44pm UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/2 "2022-01-23T15:44:52Z")

</div>

You get something similar with the `Mesh` and `MeshStyle` options:

```
ListPlot3D[Table[1/(x^2 + y^2 + 1), {x, -2, 2, 1/2}, {y, -2, 2, 1/2}],
  Mesh -> All, MeshStyle -> Thick]

```

---

<div class="post-metadata">

**Author:** ![christophetroestler](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@christophetroestler](https://community.wolfram.com/u/christophetroestler)\
**Post date:** [January 23, 2022, 4:08pm UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/3 "2022-01-23T16:08:06Z")

</div>

Thanks but `Table` defines a _rectangular_ mesh while, in my case, my data is defined on a _triangular_ one. The picture was just an example. Attached is an example of what I really want.

[E2\_ball.pdf](https://community.wolfram.com/uploads/short-url/oV2mnAykLGbqFjOuWY931GDFuDy.pdf) (62.4 KB)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [January 23, 2022, 4:34pm UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/4 "2022-01-23T16:34:05Z")

</div>

You can use `Graphics3D` and `Polygon`:

```
Graphics3D[Table[Polygon[RandomReal[{0, 1}, {3, 3}]], {10}]]

```

If your triangles share vertices, it may be more computationally efficient to use `GraphicsComplex` rather than listing each `Polygon` by itself.

---

<div class="post-metadata">

**Author:** ![christophetroestler](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@christophetroestler](https://community.wolfram.com/u/christophetroestler)\
**Post date:** [January 23, 2022, 8:32pm UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/5 "2022-01-23T20:32:36Z")

</div>

Thanks for your help. I also used `GraphicsComplex` to store the coordinate triplets and `Polygon` structure “separately”. I now get the test image below. Any way to have a color gradient according to the height?

 ![R1.png](https://global.discourse-cdn.com/wolfram/original/3X/c/3/c39a49e9f6e5a023e4160dc5a64d92ed6ff2df16.png)

---

<div class="post-metadata">

**Author:** ![wolframcommunity](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wolframcommunity/32/8335_2.png) [@wolframcommunity](https://community.wolfram.com/u/wolframcommunity)\
**Post date:** [January 24, 2022, 6:21pm UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/6 "2022-01-24T18:21:29Z")

</div>

Welcome to Wolfram Community!  
Please provide your code to make it easier for other members to help.  
Please read the forum rules for more information on how to include code to your post: [http://wolfr.am/READ-1ST](http://wolfr.am/READ-1ST)

---

<div class="post-metadata">

**Author:** ![christophetroestler](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@christophetroestler](https://community.wolfram.com/u/christophetroestler)\
**Post date:** [January 24, 2022, 8:35pm UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/7 "2022-01-24T20:35:50Z")

</div>

Sure, here it is (as said earlier, it is generated code). The complete file is attached.

```
xyz = {{1.16*^2,3.72*^2,2.56*^2}, {1.27*^2,2.02*^2,7.5*^1}, {4.29*^2,2.38*^2,-1.91*^2}, {2.54*^2,1.99*^2,-5.5*^1}, ...};
triangles = {{235,116,4}, {4,232,235}, {116,43,4}, {232,236,235}, ...};
Graphics3D[GraphicsComplex[xyz, Polygon[triangles]]]

```

[test.m](https://community.wolfram.com/uploads/short-url/6zGNppzh0gKVFzg6j8VSwr8BeEv.m) (13.6 KB)

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [January 24, 2022, 9:39pm UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/8 "2022-01-24T21:39:42Z")

</div>

All your points appear to lie in a plane. So - it does not look like the first picture you showed.

> Any way to have a color gradient according to the height?

Does this do what you have in mind?

```
ListPlot3D[xyz,ColorFunction->"Rainbow",Mesh->All]

```

 ![disk.png](https://global.discourse-cdn.com/wolfram/original/3X/f/a/fa137d67a2343256b608c466e181d9ff5da87d42.png)

---

<div class="post-metadata">

**Author:** ![christophetroestler](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@christophetroestler](https://community.wolfram.com/u/christophetroestler)\
**Post date:** [January 24, 2022, 10:29pm UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/9 "2022-01-24T22:29:59Z")

</div>

Yes thanks. The picture is only a small test, the library is still under development. I attached a modification of it. With `ListPlot3D[xyz,ColorFunction->"Rainbow",Mesh->All]` I indeed produced the picture below. However, since the `triangles` are not used, Mathematica reconstructs a mesh from the points. I would prefer if possible that it uses mine. Any way to tell it to do so?

 ![T1.png](https://global.discourse-cdn.com/wolfram/original/3X/8/9/89f90991c417626d198dbc54a9e8ec80660e61dd.png)

[test.m](https://community.wolfram.com/uploads/short-url/964oA9Bgtdz6R4h759w3WvvrEfY.m) (17.2 KB)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [January 25, 2022, 10:36am UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/10 "2022-01-25T10:36:21Z")

</div>

You can use `VertexColors` inside `GraphicsComplex` to give colors to vertices:

```
pts = RandomReal[{0, 1}, {5, 3}];
Graphics3D[
 GraphicsComplex[pts, Polygon[{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}], 
  VertexColors -> 
   Map[Function[pt, ColorData["Rainbow"][pt[[3]]]], pts]]]

```

You may have to simulate `ColorFunctionScaling`.

---

<div class="post-metadata">

**Author:** ![christophetroestler](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@christophetroestler](https://community.wolfram.com/u/christophetroestler)\
**Post date:** [January 25, 2022, 12:38pm UTC](https://community.wolfram.com/t/export-p1-function-on-a-mesh-to-mathematica/19485/11 "2022-01-25T12:38:48Z")

</div>

Aha! Thanks a lot for your help!

 ![T2.png](https://global.discourse-cdn.com/wolfram/original/2X/6/6a962fddeb4354558a56bd5c1228c1d031eea6fb.png)
