{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "##
Meshing a triangular surface of equation $z=f(x,y)$
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " We define and plot a surface S, as the graph of a function defined on a 2D triangular region, $\\Delta$. \n", " \n", "To draw such a surface as a PlotlyJS mesh3d trace, we triangulate the region $\\Delta$, by generating a regular grid in barycentric coordinates with respect to the boundary triangle, and converting these coordinates to cartesian ones. Then we map this triangulation onto the surface, via the function f, and project it onto a plane z=h, as well." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using StaticArrays\n", "using Delaunay\n", "using PlotlyJS\n", "include(\"src/plotlyju.jl\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "function gen_trimesh_points(T₁, T₂, T₃, n=21)\n", " # T_1, T_2, T_3 are the vertices of a triangle \n", " # it generates (n+1)(n+2)/2 points as a triangular grid over the triangle T_1T_2T_3\n", " \n", " ## vector of barycentric coordinates \n", " B = [[i/n, j/n, (n-(i+j))/n] for i in n:-1:0 for j in n-i:-1:0]\n", " #conversion to cartesian coordinates\n", " pts_vect = [SVector{2, Float64}(b[1]*T₁+b[2]*T₂ +b[3]*T₃) for b in B] \n", " pts_matr = reshape(reinterpret(Float64, pts_vect), (2, length(pts_vect)))'\n", " return pts_matr\n", "end \n", "\n", "f(x,y) = -cos(x)*sin(x*y);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "T₁, T₂, T₃ = [-2.0, -2*sqrt(3)/3], [2.0, -2*sqrt(3)/3], [0.0, 4*sqrt(3)/3] \n", "points = gen_trimesh_points(T₁, T₂, T₃) #N=22*23/2=253 points\n", "mesh = delaunay(Matrix(points))\n", "triangles = mesh.simplices .- 1; \n", "x, y = points[:, 1], points[:, 2]\n", "z = f.(x,y);\n", "\n", "triangular_surf = mesh3d(x=x, y=y, \n", " z=z, \n", " i=triangles[:, 1], \n", " j=triangles[:, 2], \n", " k=triangles[:, 3],\n", " intensity=z,\n", " colorbar_len=0.7\n", " )\n", "data = AbstractTrace[]\n", "push!(data, triangular_surf)\n", "\n", "#project surf onto the plane z=h:\n", "h = -2\n", "z_proj = h * ones(size(z)) \n", "\n", "proj_surf = mesh3d(x=x, y=y, z=z_proj,\n", " i=triangles[:, 1], \n", " j=triangles[:, 2], \n", " k=triangles[:, 3],\n", " intensity=z,\n", " showscale=false)\n", "push!(data, proj_surf)\n", "layout = Layout(width=600, height=600, \n", " #margin=attr(l=50, t=10, r=50, b=10),\n", " scene_camera_eye=attr(x=1.75, y=1.75, z=0.9))\n", "pl = Plot(data, layout, style=plotlyju)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![tri-surf](images/triangular-surf.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "kernelspec": { "display_name": "Julia 1.6.1", "language": "julia", "name": "julia-1.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.6.1" } }, "nbformat": 4, "nbformat_minor": 4 }