Skip to content

Commit

Permalink
Merge pull request #79 from VEZY/compathelper/new_version/2024-10-18-…
Browse files Browse the repository at this point in the history
…00-20-13-202-03192803601

CompatHelper: bump compat for Meshes to 0.52, (keep existing compat)
  • Loading branch information
VEZY authored Nov 28, 2024
2 parents a100cae + b17bbef commit 3787146
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 2,254 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/Manifest.toml
/docs/build/
test/Manifest.toml
9 changes: 2 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa"
MultiScaleTreeGraph = "dd4a991b-8a45-4075-bede-262ee62d5583"
Observables = "510215fc-4207-5dde-b226-833fc4488ee2"
Expand All @@ -19,18 +20,12 @@ TransformsBase = "28dd2a49-a57a-4bfb-84ca-1a49db9b96b8"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[weakdeps]
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"

[extensions]
PlantGeomMakie = "Makie"

[compat]
ColorSchemes = "3"
Colors = "0.12"
EzXML = "1"
Makie = "0.21"
Meshes = "0.44"
Meshes = "0.52"
MultiScaleTreeGraph = "0.14"
Observables = "0.5"
RecipesBase = "1"
Expand Down
14 changes: 9 additions & 5 deletions ext/PlantGeomMakie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import Unitful

MeshesMakieExt = Base.get_extension(Meshes, :MeshesMakieExt)

include("makie_recipes/RefMeshes_recipes.jl")
include("makie_recipes/opf/plot_opf.jl")
include("makie_recipes/opf_recipe.jl")
include("makie_recipes/mtg_tree_recipe.jl")
include("makie_recipes/colorbar.jl")
if isnothing(MeshesMakieExt)
println("Couldn't import the MeshesMakieExt extension, please load the Meshes package before loading PlantGeom")
else
include("makie_recipes/RefMeshes_recipes.jl")
include("makie_recipes/opf/plot_opf.jl")
include("makie_recipes/opf_recipe.jl")
include("makie_recipes/mtg_tree_recipe.jl")
include("makie_recipes/colorbar.jl")
end

export viz, viz!
export colorbar
Expand Down
11 changes: 11 additions & 0 deletions src/PlantGeom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,15 @@ export colorbar

export get_transformation_matrix


# Code that should be moved to PlantGeomMakie:
import Makie
MeshesMakieExt = Base.get_extension(Meshes, :MeshesMakieExt)

include("../ext/makie_recipes/RefMeshes_recipes.jl")
include("../ext/makie_recipes/opf/plot_opf.jl")
include("../ext/makie_recipes/opf_recipe.jl")
include("../ext/makie_recipes/mtg_tree_recipe.jl")
include("../ext/makie_recipes/colorbar.jl")

end
25 changes: 9 additions & 16 deletions src/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ end

function normals(mesh::RefMesh{S,ME,M,N,T}) where {S,ME<:Meshes.SimpleMesh,M,N,T}
if length(mesh.normals) == 0
return SVector{length(Meshes.topology(mesh.mesh).connec)}(
Meshes.Point(Meshes.normal(Meshes.Triangle(Meshes.vertices(mesh.mesh)[[tri.indices...]]))) for tri in Meshes.topology(mesh.mesh).connec
)
return SVector{length(mesh.mesh)}(Meshes.normal(tri) for tri in mesh.mesh)
else
return mesh.normals
end
Expand All @@ -38,23 +36,18 @@ Compute per vertex normals and return them as a `StaticArrays.SVector`.
# https://stackoverflow.com/questions/45477806/general-method-for-calculating-smooth-vertex-normals-with-100-smoothness?noredirect=1&lq=1
"""
function normals_vertex(mesh::RefMesh)
vertex_normals = fill(Meshes.Vec(0.0, 0.0, 0.0), Meshes.nvertices(mesh))
for (i, tri) in enumerate(Meshes.topology(mesh.mesh).connec)
vertex_normals[tri.indices[1]] = mesh.normals[i]
vertex_normals[tri.indices[2]] = mesh.normals[i]
vertex_normals[tri.indices[3]] = mesh.normals[i]
end

return SVector{length(vertex_normals)}(vertex_normals)
normals_vertex(mesh.mesh)
end

function normals_vertex(mesh::Meshes.SimpleMesh)
vertex_normals = fill(Meshes.Vec(0.0, 0.0, 0.0), Meshes.nvertices(mesh))
for (i, tri) in enumerate(Meshes.topology(mesh).connec)
tri_norm = Meshes.normal(Meshes.Triangle(mesh.vertices[[tri.indices...]]...))
vertex_normals[tri.indices[1]] = tri_norm
vertex_normals[tri.indices[2]] = tri_norm
vertex_normals[tri.indices[3]] = tri_norm
tri_normals = [Meshes.normal(tri) for tri in mesh]

for (i, tri) in enumerate(Meshes.topology(mesh))
tri_indices = Meshes.indices(tri)
vertex_normals[tri_indices[1]] = tri_normals[i]
vertex_normals[tri_indices[2]] = tri_normals[i]
vertex_normals[tri_indices[3]] = tri_normals[i]
end

return SVector{length(vertex_normals)}(vertex_normals)
Expand Down
2 changes: 1 addition & 1 deletion src/meshes/summary_coordinates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ Apply function `f` over the mesh coordinates `coord`.
Values for `coord` can be 1 for x, 2 for y and 3 for z.
"""
function map_coord(f, mesh, coord)
f([Meshes.to(i)[coord] for i in Meshes.vertices(mesh)])
f([Meshes.to(i)[coord] for i in Meshes.eachvertex(mesh)])
end
2 changes: 1 addition & 1 deletion src/opf/refmesh_to_mesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function refmesh_to_mesh(node)
end

function apply_transformation(transformation, ref_mesh)
Meshes.SimpleMesh([transformation(p) for p in Meshes.vertices(ref_mesh)], Meshes.topology(ref_mesh))
Meshes.SimpleMesh([transformation(p) for p in Meshes.eachvertex(ref_mesh)], Meshes.topology(ref_mesh))
end

function refmesh_to_mesh!(node)
Expand Down
21 changes: 11 additions & 10 deletions src/opf/write_opf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ function write_opf(file, mtg)
addelement!(
mesh_elm,
"points",
string("\n", join(Iterators.flatten(Unitful.ustrip.(Unitful.uconvert.(u"cm", Meshes.to(p))) for p in Meshes.vertices(mesh_.mesh)), "\t"), "\n")
string("\n", join(Iterators.flatten(Unitful.ustrip.(u"cm", Meshes.to(p)) for p in Meshes.eachvertex(mesh_.mesh)), "\t"), "\n")
)

# p |> LengthUnit(u"cm")
if length(mesh_.normals) == Meshes.nelements(mesh_) && length(mesh_.normals) != Meshes.nvertices(mesh_)
# If the normals are per triangle, re-compute them per vertex:
vertex_normals = normals_vertex(mesh_)
Expand All @@ -60,7 +60,7 @@ function write_opf(file, mtg)
norm_elm = addelement!(
mesh_elm,
"normals",
string("\n", join(Iterators.flatten(Unitful.ustrip.(Unitful.uconvert.(u"cm", p)) for p in vertex_normals), "\t"), "\n")
string("\n", join(Iterators.flatten(Unitful.ustrip.(u"cm", p) for p in vertex_normals), "\t"), "\n")
)


Expand All @@ -69,18 +69,19 @@ function write_opf(file, mtg)
norm_elm = addelement!(
mesh_elm,
"textureCoords",
string("\n", join(Iterators.flatten(Unitful.ustrip.(Unitful.uconvert.(u"cm", Meshes.to(p))) for p in mesh_.texture_coords), "\t"), "\n")
string("\n", join(Iterators.flatten(Unitful.ustrip.(u"cm", Meshes.to(p)) for p in mesh_.texture_coords), "\t"), "\n")
)
end

faces_elm = addelement!(mesh_elm, "faces")


face_id = [0]
for i in firstindex(mesh_.mesh):lastindex(mesh_.mesh)
for tri in Meshes.elements(Meshes.topology(mesh_.mesh))
face_elm = addelement!(
faces_elm,
"face",
string("\n", join(Meshes.topology(mesh_.mesh).connec[i].indices .- 1, "\t"), "\n")
string("\n", join(Meshes.indices(tri) .- 1, "\t"), "\n")
)
#? NB: we remove one because face index are 0-based in the opf
face_elm["Id"] = face_id[1]
Expand Down Expand Up @@ -297,22 +298,22 @@ end

function get_transformation_matrix(trans::Affine)
A, b = parameters(trans)
b = Unitful.ustrip.(Unitful.uconvert.(u"cm", b))
b = Unitful.ustrip.(u"cm", b)
vcat(hcat(A, b), [0 0 0 1])
end

function get_transformation_matrix(trans::Translate{3,T}) where {T}
x, y, z = Unitful.ustrip.(Unitful.uconvert.(u"cm", trans.offsets))
x, y, z = Unitful.ustrip.(u"cm", trans.offsets)
[1.0 0.0 0.0 x; 0.0 1.0 0.0 y; 0.0 0.0 1.0 z; 0.0 0.0 0.0 1.0]
end

function get_transformation_matrix(trans::Translate{2,T}) where {T}
x, y = Unitful.ustrip.(Unitful.uconvert.(u"cm", trans.offsets))
x, y = Unitful.ustrip.(u"cm", trans.offsets)
[1.0 0.0 0.0 x; 0.0 1.0 0.0 y; 0.0 0.0 1.0 0.0; 0.0 0.0 0.0 1.0]
end

function get_transformation_matrix(trans::Translate{1,T}) where {T}
x = Unitful.ustrip.(Unitful.uconvert(u"cm", trans.offsets[1]))
x = Unitful.ustrip.(u"cm", trans.offsets[1])
[1.0 0.0 0.0 x; 0.0 1.0 0.0 0.0; 0.0 0.0 1.0 0.0; 0.0 0.0 0.0 1.0]
end

Expand Down
6 changes: 3 additions & 3 deletions src/ops/write_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ write_ops("scene.ops", scene_dimensions, object_table)
"""
function write_ops(file, scene_dimensions, object_table)
dims = join([
Unitful.ustrip.(Unitful.uconvert.(u"m", Meshes.to(scene_dimensions[1])))...,
Unitful.ustrip.(Unitful.uconvert.(u"m", Meshes.to(scene_dimensions[2])))...
Unitful.ustrip.(u"m", Meshes.to(scene_dimensions[1]))...,
Unitful.ustrip.(u"m", Meshes.to(scene_dimensions[2]))...
][1:end-1], " ")
ops_lines = vcat("# T xOrigin yOrigin zOrigin xSize ySize flat", string("T ", dims..., " flat"))

Expand All @@ -55,7 +55,7 @@ function write_ops(file, scene_dimensions, object_table)
"#[Archimed] $current_group",
"#sceneId plantId plantFileName x y z scale inclinationAzimut inclinationAngle stemTwist")
end
x, y, z = Unitful.ustrip.(Unitful.uconvert.(u"m", Meshes.to(row.pos)))
x, y, z = Unitful.ustrip.(u"m", Meshes.to(row.pos))
plant_scale = haskey(row, :scale) ? row.scale : 1.0
plant_rotation = haskey(row, :rotation) ? row.rotation : 0.0
plant_inclinationAzimut = haskey(row, :inclinationAzimut) ? row.inclinationAzimut : 0.0
Expand Down
2 changes: 1 addition & 1 deletion src/structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ RefMesh type. Stores all information about a Mesh:
The reference meshes are then transformed on each node of the MTG using a transformation matrix
to match the actual mesh.
"""
struct RefMesh{S<:String,ME<:Meshes.Mesh{3},M<:Union{Material,Colorant},N<:AbstractVector,T<:Union{AbstractVector,Nothing}}
struct RefMesh{S<:String,ME<:Meshes.Mesh{<:Meshes.𝔼{3}},M<:Union{Material,Colorant},N<:AbstractVector,T<:Union{AbstractVector,Nothing}}
name::S
mesh::ME
normals::N
Expand Down
Loading

0 comments on commit 3787146

Please sign in to comment.