The code below plots a mesh defined by vertices and faces. Is there a way to have automatic normals? By “automatic normals” I mean for example the per-vertex normals obtained by averaging the normals of the incident faces of the vertices.
using GLMakie
function HopfTorusMeshHelper(u, cos_v, sin_v, nlobes, A)
B = pi/2 - (pi/2 - A)*cos(u*nlobes)
C = u + A*sin(2*u*nlobes)
y1 = 1 + cos(B)
y23 = sin(B) .* [cos(C), sin(C)]
y2 = y23[1]
y3 = y23[2]
x1 = y3 .* cos_v + y2 .* sin_v
x2 = y2 .* cos_v - y3 .* sin_v
x3 = y1 .* sin_v
x4 = y1 .* cos_v
yden = sqrt(2*y1)
return vcat(x1 ./ (yden .- x4), x2 ./ (yden .- x4), x3 ./ (yden .- x4))
end
function HopfTorusMesh(nu, nv; nlobes = 3, A = 0.44)
vs = Array{Float64}(undef, nu*nv, 3)
tris1 = Array{Int64}(undef, nu*nv, 3)
tris2 = Array{Int64}(undef, nu*nv, 3)
u_ = range(0, stop = 2*pi, length = nu+1)[2:(nu+1)]
v_ = range(0, stop = 2*pi, length = nv+1)[2:(nv+1)]
cos_v = cos.(v_)
sin_v = sin.(v_)
jp1_ = [(2:nv)..., 1]
j_ = [(1:nv)...]
for i in 1:(nu-1)
i_nv = i*nv
vs[(i_nv - nv + 1):i_nv, :] =
HopfTorusMeshHelper(u_[i], cos_v, sin_v, nlobes, A)
k1 = i_nv - nv
k_ = k1 .+ j_
l_ = k1 .+ jp1_
m_ = i_nv .+ j_
tris1[k_, :] = hcat(k_, l_, m_)
tris2[k_, :] = hcat(l_, i_nv .+ jp1_, m_)
end
i_nv = nu*nv
vs[(i_nv - nv + 1):i_nv, :] =
HopfTorusMeshHelper(2*pi, cos_v, sin_v, nlobes, A)
k1 = i_nv - nv
k_ = k1 .+ j_
l_ = k1 .+ jp1_
tris1[k_, :] = hcat(k_, l_, j_)
tris2[k_, :] = hcat(l_, jp1_, j_)
return (vertices = vs, faces = vcat(tris1, tris2))
end
ht = HopfTorusMesh(200, 200)
mesh(ht.vertices, ht.faces)