|
| 1 | +# RayCaster.jl |
| 2 | + |
| 3 | +```@setup raycaster |
| 4 | +using Bonito |
| 5 | +Bonito.Page() |
| 6 | +``` |
| 7 | + |
| 8 | +```@example raycaster |
| 9 | +using RayCaster, GeometryBasics, LinearAlgebra |
| 10 | +using WGLMakie, FileIO |
| 11 | +
|
| 12 | +function LowSphere(radius, contact=Point3f(0); ntriangles=10) |
| 13 | + return Tesselation(Sphere(contact .+ Point3f(0, 0, radius), radius), ntriangles) |
| 14 | +end |
| 15 | +
|
| 16 | +ntriangles = 10 |
| 17 | +s1 = LowSphere(0.5f0, Point3f(-0.5, 0.0, 0); ntriangles) |
| 18 | +s2 = LowSphere(0.3f0, Point3f(1, 0.5, 0); ntriangles) |
| 19 | +s3 = LowSphere(0.3f0, Point3f(-0.5, 1, 0); ntriangles) |
| 20 | +s4 = LowSphere(0.4f0, Point3f(0, 1.0, 0); ntriangles) |
| 21 | +l = 0.5 |
| 22 | +floor = Rect3f(-l, -l, -0.01, 2l, 2l, 0.01) |
| 23 | +cat = load(Makie.assetpath("cat.obj")) |
| 24 | +bvh = RayCaster.BVHAccel([s1, s2, s3, s4, cat]); |
| 25 | +world_mesh = GeometryBasics.Mesh(bvh) |
| 26 | +f, ax, pl = Makie.mesh(world_mesh; color=:teal) |
| 27 | +center!(ax.scene) |
| 28 | +viewdir = normalize(ax.scene.camera.view_direction[]) |
| 29 | +
|
| 30 | +@time "hitpoints" hitpoints, centroid = RayCaster.get_centroid(bvh, viewdir) |
| 31 | +@time "illum" illum = RayCaster.get_illumination(bvh, viewdir) |
| 32 | +@time "viewf_matrix" viewf_matrix = RayCaster.view_factors(bvh, rays_per_triangle=1000) |
| 33 | +viewfacts = map(i-> Float32(sum(view(viewf_matrix, :, i))), 1:length(bvh.primitives)) |
| 34 | +world_mesh = GeometryBasics.Mesh(bvh) |
| 35 | +N = length(world_mesh.faces) |
| 36 | +areas = map(i-> area(world_mesh.position[world_mesh.faces[i]]), 1:N) |
| 37 | +# View factors |
| 38 | +f, ax, pl = mesh(world_mesh, color=:teal, figure=(; size=(800, 600)), axis=(; show_axis=false)) |
| 39 | +per_face_vf = FaceView((viewfacts), [GLTriangleFace(i) for i in 1:N]) |
| 40 | +viewfact_mesh = GeometryBasics.mesh(world_mesh, color=per_face_vf) |
| 41 | +pl = Makie.mesh(f[1, 2], |
| 42 | + viewfact_mesh, colormap=[:black, :red], axis=(; show_axis=false), |
| 43 | + shading=false, highclip=:red, lowclip=:black, colorscale=sqrt,) |
| 44 | +
|
| 45 | +# Centroid |
| 46 | +cax, pl = Makie.mesh(f[2, 1], world_mesh, color=(:blue, 0.5), axis=(; show_axis=false), transparency=true) |
| 47 | +
|
| 48 | +eyepos = cax.scene.camera.eyeposition[] |
| 49 | +depth = map(x-> norm(x .- eyepos), hitpoints) |
| 50 | +meshscatter!(cax, hitpoints, color=depth, colormap=[:gray, :black], markersize=0.01) |
| 51 | +meshscatter!(cax, centroid, color=:red, markersize=0.05) |
| 52 | +
|
| 53 | +# Illum |
| 54 | +pf = FaceView(100f0 .* (illum ./ areas), [GLTriangleFace(i) for i in 1:N]) |
| 55 | +illum_mesh = GeometryBasics.mesh(world_mesh, color=pf) |
| 56 | +
|
| 57 | +Makie.mesh(f[2, 2], illum_mesh, colormap=[:black, :yellow], colorscale=sqrt, shading=false, axis=(; show_axis=false)) |
| 58 | +
|
| 59 | +Label(f[0, 1], "Scene ($(length(bvh.primitives)) triangles)", tellwidth=false, fontsize=20) |
| 60 | +Label(f[0, 2], "Viewfactors", tellwidth=false, fontsize=20) |
| 61 | +Label(f[3, 1], "Centroid", tellwidth=false, fontsize=20) |
| 62 | +Label(f[3, 2], "Illumination", tellwidth=false, fontsize=20) |
| 63 | +
|
| 64 | +f |
| 65 | +``` |
| 66 | + |
| 67 | +## Overview |
| 68 | + |
| 69 | + |
| 70 | +```@autodocs |
| 71 | +Modules = [RayCaster] |
| 72 | +Order = [:module, :constant, :type, :function, :macro] |
| 73 | +Public = true |
| 74 | +Private = false |
| 75 | +``` |
| 76 | + |
| 77 | +## Private Functions |
| 78 | + |
| 79 | +```@autodocs |
| 80 | +Modules = [RayCaster] |
| 81 | +Order = [:module, :constant, :type, :function, :macro] |
| 82 | +Public = false |
| 83 | +Private = true |
| 84 | +``` |
0 commit comments