Triangulated surfaces in GR: ray-tracing

I’m looking to use GR along with Interact.jl for a 3D visualization in IJulia, and I noticed that GR.trisurface seems to have some difficulties with overlapping polygons (as matplotlib does).

Is there currently a way to achieve a ray-traced surface plot in Julia? I have been using Asymptote to render such figures statically, and it does a terrific job, but I would prefer a Julia-based solution.

import GR
GR.inline("png")
f(x,y) = x^2+y^2 < 1e-3 ? 0 : x*y/(x^2+y^2)
rad = 1
rv = linspace(0,rad,5)
θv = linspace(0,2π,12)
xv = [r*cos(θ) for r=rv, θ=θv]
yv = [r*sin(θ) for r=rv, θ=θv]
zv = [f(r*cos(θ),r*sin(θ)) for r=rv, θ=θv];
xv, yv, zv = map(a->vcat(a...),(xv,yv,zv))
GR.setviewport(0.1, 0.95, 0.1, 0.95)
GR.setwindow(-2, 2, -2, 2)
GR.setspace(-2, 2, 85, 35)
GR.setmarkersize(0.1)
GR.trisurface(xv,yv,zv)
GR.polyline3d([0,0],[0,0],[0,2])
GR.polyline3d([0,0],[0,2],[0,0])
GR.polyline3d([0,2],[0,0],[0,0])
GR.show()

With GLVisualize you can do something like:

using GLVisualize, GeometryTypes, Colors, GLAbstraction
f(x,y) = x^2+y^2 < 1e-3 ? 0 : x*y/(x^2+y^2)
rad = 1
rv = linspace(0,rad,5)
θv = linspace(0,2π,12)
xv = Float32[r*cos(θ) for r=rv, θ=θv]
yv = Float32[r*sin(θ) for r=rv, θ=θv]
zv = Float32[f(r*cos(θ),r*sin(θ)) for r = rv, θ = θv]


w = glscreen(); @async renderloop(w)
x = visualize(
    (xv, yv, zv), :surface,
    color_map = GLVisualize.default(Vector{RGBA}, Style(:default)),
    wireframe = true, color = nothing, color_norm = Vec2f0(-0.4, 0.4),
)
x2 = copy(x).children[].children[]
x2[:model] = translationmatrix(Vec3f0(2, 0, 0)) * rotationmatrix_x(0.5f0*pi)
x3 = copy(x).children[].children[]
x3[:model] = translationmatrix(Vec3f0(4, 0, 0)) * rotationmatrix_z(0.5f0*pi)

_view(x)
_view(x2)
_view(x3)

If you manage to create a dense volume of your function, you could also ray trace that with GLVisualize.
Or if you can create the triangles (not the one being on a surface), you could also visualize those.

The current GLVisualize API is a bit messy as you can see, but I’m working on a much nicer API right now and will release it soon (MakiE.jl)…

2 Likes