From 6cbca9e9d060bf1f3c0b202030b52064c879440b Mon Sep 17 00:00:00 2001 From: ffreyer Date: Sun, 12 Mar 2023 19:55:32 +0100 Subject: [PATCH] add example --- docs/documentation/cameras.md | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/docs/documentation/cameras.md b/docs/documentation/cameras.md index f935c92c29c..236764434cc 100644 --- a/docs/documentation/cameras.md +++ b/docs/documentation/cameras.md @@ -30,6 +30,59 @@ Note that this camera is not used by `Axis`. It is used, by default, for 2D `LSc {{doc Camera3D}} +## Example - Visualizing the cameras view box + +```julia +using GeometryBasics, LinearAlgebra + +function frustum_snapshot(cam) + r = Rect3f(Point3f(-1, -1, -1), Vec3f(2, 2, 2)) + rect_ps = coordinates(r) .|> Point3f + insert!(rect_ps, 13, Point3f(1, -1, 1)) # fix bad line + + inv_pv = inv(cam.projectionview[]) + return map(rect_ps) do p + p = inv_pv * to_ndim(Point4f, p, 1) + return p[Vec(1,2,3)] / p[4] + end +end + + +ex = Point3f(1,0,0) +ey = Point3f(0,1,0) +ez = Point3f(0,0,1) + +fig = Figure() +scene = LScene(fig[1, 1]) +cc = Makie.Camera3D(scene.scene, projectiontype = Makie.Perspective, far = 3.0) + +linesegments!(scene, Rect3f(Point3f(-1), Vec3f(2)), color = :black) +linesegments!(scene, + [-ex, ex, -ey, ey, -ez, ez], + color = [:red, :red, :green, :green, :blue, :blue] +) +center!(scene.scene) + +cam = scene.scene.camera +eyeposition = cc.eyeposition +lookat = cc.lookat +frustum = map(pv -> frustum_snapshot(cam), cam.projectionview) + +scene = LScene(fig[1, 2]) +_cc = Makie.Camera3D(scene.scene, projectiontype = Makie.Orthographic) +lines!(scene, frustum, color = :blue, linestyle = :dot) +scatter!(scene, eyeposition, color = :black) +scatter!(scene, lookat, color = :black) + +linesegments!(scene, + [-ex, ex, -ey, ey, -ez, ez], + color = [:red, :red, :green, :green, :blue, :blue] +) +linesegments!(scene, Rect3f(Point3f(-1), Vec3f(2)), color = :black) + +fig +``` + ## General Remarks To force a plot to be visualized in 3D, you can set the limits to have a nonzero \(z\)-axis interval, or ensure that a 3D camera type is used.