Skip to content

Commit

Permalink
Merge pull request #5 from VirtualPlantLab/new_mesh
Browse files Browse the repository at this point in the history
New mesh
  • Loading branch information
AleMorales authored Dec 17, 2024
2 parents 1b17440 + 3737a77 commit 122a68b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 59 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PlantViz"
uuid = "358bd95d-d12c-439f-94b7-04b17e500c7f"
authors = ["Alejandro Morales Sierra <alejandro.moralessierra@wur.nl> and contributors"]
version = "0.0.7"
version = "0.0.8"

[deps]
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
Expand All @@ -19,7 +19,7 @@ FileIO = "1.16.1"
GeometryBasics = "0.4.7"
LinearAlgebra = "1.11"
Makie = "0.21"
PlantGeomPrimitives = "0.0.4"
PlantRayTracer = "0.0.7"
PlantGeomPrimitives = "0.0.5"
PlantRayTracer = "0.0.8"
Unrolled = "0.1.5"
julia = "1.11"
2 changes: 1 addition & 1 deletion src/PlantViz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ColorTypes as CT
import FileIO
import Unrolled: @unroll
import PlantGeomPrimitives as PGP
import PlantRayTracer as VT
import PlantRayTracer as PRT

export render, render!, export_scene

Expand Down
51 changes: 11 additions & 40 deletions src/Render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,6 @@
##### Meshes #####
##################

"""
render(m::Mesh; kwargs...)
Render a mesh. This will create a new visualization (see Documentation for
details). Keyword arguments are passed to the `render(scene::Scene)` method
and any unmatched keywords will be passed along to `Makie.mesh()`.
"""
function render(m::PGP.Mesh; kwargs...)
render(PGP.GLMesh(m); kwargs...)
end

"""
render!(m::Mesh; kwargs...)
Add a mesh to the visualization currently active. This will create a new
visualization (see Documentation for details). Keyword arguments are passed to
the `render!(scene::Scene)` method and any unmatched keywords will be passed
along to `Makie.mesh!()`.
"""
function render!(m::PGP.Mesh; kwargs...)
render!(PGP.GLMesh(m); kwargs...)
end

# Basic rendering of a triangular mesh that is already in the right format
function render(
m::GeometryBasics.Mesh;
Expand Down Expand Up @@ -54,27 +31,21 @@ function render!(
scene_additions!(m, normals, wireframe)
end



##################
##### Scenes #####
##################

"""
render(scene::Scene; normals::Bool = false, wireframe::Bool = false, kwargs...)
render(mesh::Mesh; normals::Bool = false, wireframe::Bool = false, kwargs...)
Render a `Scene` object. This will create a new visualization (see
Render a `Mesh` object. This will create a new visualization (see
Documentation for details). `normals = true` will draw arrows in the direction
of the normal vector for each triangle in the mesh, `wireframe = true` will draw
the edges of each triangle with black lines. Keyword arguments are passed to
`Makie.mesh()`. The actual color of each triangle depends on the illumination of the scene
but it is possible to turn this off by passing `shading = false`. This will use the exact
colors specified in the `Scene` object.
"""
function render(scene::PGP.Scene; normals::Bool = false, wireframe::Bool = false, kwargs...)
function render(mesh::PGP.Mesh; normals::Bool = false, wireframe::Bool = false, kwargs...)
render(
PGP.mesh(scene);
color = PGP.colors(scene),
PGP.GLMesh(mesh);
color = repeat(PGP.properties(mesh)[:colors], inner = 3),
normals = normals,
wireframe = wireframe,
kwargs...,
Expand All @@ -101,11 +72,11 @@ of the light source is rendered along with the normal vector at that point
version, `point = true` is only possible for directional light sources.
"""
function render!(
sources::Vector{VT.Source{G,A,nw}};
sources::Vector{PRT.Source{G,A,nw}};
n = 20,
alpha = 0.2,
scale = 0.2,
) where {G<:VT.Directional,A<:VT.FixedSource,nw}
) where {G<:PRT.Directional,A<:PRT.FixedSource,nw}
FT = eltype(sources[1].geom.xmin)
# Compute point and arrow for each light source
temp = compute_dir_p.(sources)
Expand Down Expand Up @@ -136,9 +107,9 @@ end


function render!(
sources::VT.Source{G,A,nw};
sources::PRT.Source{G,A,nw};
kwargs...,
) where {G<:VT.Directional,A<:VT.FixedSource,nw}
) where {G<:PRT.Directional,A<:PRT.FixedSource,nw}
render!([sources]; kwargs...)
end

Expand All @@ -148,11 +119,11 @@ end
Add a mesh representing the bounding boxes of the grid cloner to a 3D scene,
where `alpha` represents the transparency of each box.
"""
function render!(grid::VT.GridCloner; alpha = 0.2)
function render!(grid::PRT.GridCloner; alpha = 0.2)
leaf_nodes = filter(x -> x.leaf, grid.nodes.data)
AABBs = getfield.(leaf_nodes, :box)
mesh = PGP.Mesh([PGP.BBox(box.min, box.max) for box in AABBs])
render!(mesh, color = CT.RGBA(0.0, 0.0, 0.0, alpha), transparency = true)
render!(PGP.GLMesh(mesh), color = CT.RGBA(0.0, 0.0, 0.0, alpha), transparency = true)
end

#######################
Expand Down
27 changes: 27 additions & 0 deletions test/test_primitives.jl
Original file line number Diff line number Diff line change
@@ -1,86 +1,113 @@
using PlantViz
using PlantGeomPrimitives
import GLMakie # import native backend for rendering -> NOT "using"!
import ColorTypes: RGB

let

# Triangle
t = Triangle(length = 2.0, width = 2.0)
add_property!(t, :colors, RGB(1, 0, 0))
render(t, wireframe = true, normals = true)
t = Triangle(length = 2.0f0, width = 2.0f0)
add_property!(t, :colors, RGB(1, 0, 0))
render(t, wireframe = true, normals = true)


# Rectangle
r = Rectangle(length = 2.0, width = 1.0);
add_property!(r, :colors, RGB(1, 0, 0))
render(r, wireframe = true, normals = true)
r = Rectangle(length = 2.0f0, width = 1.0f0);
add_property!(r, :colors, RGB(1, 0, 0))
render(r, wireframe = true, normals = true)

# Trapezoid
t = Trapezoid(length = 2.0, width = 1.0, ratio = 0.5);
add_property!(t, :colors, RGB(1, 0, 0))
render(t, wireframe = true, normals = true)
t = Trapezoid(length = 2.0f0, width = 1.0f0, ratio = 0.5f0);
add_property!(t, :colors, RGB(1, 0, 0))
render(t, wireframe = true, normals = true)

# Ellipse
e = Ellipse(length = 2.0, width = 2.0, n = 20);
add_property!(e, :colors, RGB(1, 0, 0))
render(e, wireframe = true, normals = true)
e = Ellipse(length = 2.0f0, width = 2.0f0, n = 20);
add_property!(e, :colors, RGB(1, 0, 0))
render(e, wireframe = true, normals = true)

# BBox
b = BBox(Vec(0.0, 0.0, 0.0), Vec(1.0, 1.0, 1.0));
add_property!(b, :colors, RGB(1, 0, 0))
render(b, wireframe = true, normals = true)
b = BBox(Vec(0.0f0, 0.0f0, 0.0f0), Vec(1.0f0, 1.0f0, 1.0f0));
add_property!(b, :colors, RGB(1, 0, 0))
render(b, wireframe = true, normals = true)

# Solid cone
c = SolidCone(length = 2.0, width = 1.0, height = 1.0, n = 40);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)
c = SolidCone(length = 2.0f0, width = 1.0f0, height = 1.0f0, n = 40);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)

# Solid cube
c = SolidCube(length = 1.0, width = 1.0, height = 1.0);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)
c = SolidCube(length = 1.0f0, width = 1.0f0, height = 1.0f0);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)

# Solid cylinder
c = SolidCylinder(length = 2.0, width = 1.0, height = 1.0, n = 80);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)
c = SolidCylinder(length = 2.0f0, width = 1.0f0, height = 1.0f0, n = 80);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)

# Solid frustum
f = SolidFrustum(length = 2.0, width = 1.0, height = 1.0, ratio = 0.5, n = 80);
add_property!(f, :colors, RGB(1, 0, 0))
render(f, wireframe = true, normals = true)
f = SolidFrustum(length = 2.0f00, width = 1.0f00, height = 1.0f00, ratio = 0.5f0, n = 80);
add_property!(f, :colors, RGB(1, 0, 0))
render(f, wireframe = true, normals = true)

# Hollow cone
c = HollowCone(length = 2.0, width = 1.0, height = 1.0, n = 20);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)
c = HollowCone(length = 2.0f0, width = 1.0f0, height = 1.0f0, n = 20);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)

# Hollow cube
c = HollowCube(length = 1.0, width = 1.0, height = 1.0);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)
c = HollowCube(length = 1.0f0, width = 1.0f0, height = 1.0f0);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)

# Hollow cylinder
c = HollowCylinder(length = 2.0, width = 1.0, height = 1.0, n = 40);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)
c = HollowCylinder(length = 2.0f0, width = 1.0f0, height = 1.0f0, n = 40);
add_property!(c, :colors, RGB(1, 0, 0))
render(c, wireframe = true, normals = true)

# Hollow frustum
f = HollowFrustum(length = 2.0, width = 1.0, height = 1.0, ratio = 0.5, n = 40);
add_property!(f, :colors, RGB(1, 0, 0))
render(f, wireframe = true, normals = true)
f = HollowFrustum(length = 2.0f0, width = 1.0f0, height = 1.0f0, ratio = 0.5f0, n = 40);
add_property!(f, :colors, RGB(1, 0, 0))
render(f, wireframe = true, normals = true)

end
26 changes: 11 additions & 15 deletions test/test_scenes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using PlantGeomPrimitives
import PlantGeomPrimitives as Geom
using PlantGeomTurtle
using PlantRayTracer
import PlantRayTracer as RT
import PlantRayTracer as PRT
using Test
import ColorTypes: RGB
using PlantViz
Expand Down Expand Up @@ -84,27 +84,23 @@ let
end
rule = Rule(sn.E64, rhs = Kochsnowflake)
Koch = Graph(axiom = axiom, rules = Tuple(rule))
scene = Scene(Koch)
render(scene)
mesh = Mesh(Koch)
render(mesh)

# Intersection of a rectangle from a directional light source downwards
nrays = 100_000
radiosity = 1.0
rect = Rectangle(length = 1.0, width = 1.0)
rotatey!(rect, -π / 4)
mesh = Rectangle(length = 1.0, width = 1.0)
rotatey!(mesh, -π / 4)
mat = Black()
ext_mat = [mat, mat]
scene = Scene(
mesh = rect,
materials = ext_mat,
colors = RGB(1, 0, 0),
)
gbox = RT.AABB(scene)
add_property!(mesh, :materials, [mat for _ in 1:2])
add_property!(mesh, :colors, [RGB(1, 0, 0) for _ in 1:2])
gbox = PRT.AABB(mesh)
source = DirectionalSource(gbox, θ = 0.0, Φ = 0.0, radiosity = radiosity, nrays = nrays)
settings = RTSettings(pkill = 1.0, maxiter = 1, dx = sqrt(0.5), dy = 1.0)
rtobj = RayTracer(scene, source, settings = settings, acceleration = Naive)
render(scene)
rtobj = RayTracer(mesh, source, settings = settings, acceleration = Naive)
render(mesh)
render!(source)
render!(rtobj.scene.grid)
render!(rtobj.mesh.grid)

end

4 comments on commit 122a68b

@AleMorales
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/121534

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.0.8 -m "<description of version>" 122a68be62581a0cf4886b4c7964629e1cd9af24
git push origin v0.0.8

@AleMorales
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/121534

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.0.8 -m "<description of version>" 122a68be62581a0cf4886b4c7964629e1cd9af24
git push origin v0.0.8

Please sign in to comment.