Skip to content

Commit

Permalink
Merge pull request #60 from JuliaGeometry/sjk/warp3
Browse files Browse the repository at this point in the history
sjk/warp3
  • Loading branch information
sjkelly authored Jul 7, 2024
2 parents 50f993a + 073d7e2 commit 6ea3dd4
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 64 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
Aqua = "0.8"
CoordinateTransformations = "0.6"
Combinatorics = "1"
CoordinateTransformations = "0.6"
FileIO = "1"
ForwardDiff = "0.10"
GeometryBasics = "0.4"
LinearAlgebra = "1"
Meshing = "0.5"
MeshIO = "0.4"
Meshing = "0.7"
Revise = "3"
Rotations = "1"
StaticArrays = "1"
Expand Down
70 changes: 34 additions & 36 deletions examples/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

julia_version = "1.10.4"
manifest_format = "2.0"
project_hash = "45f0ef80ca12ef806f32247cd0d5651dd69dc4de"
project_hash = "5711fc8d77eed703586d928736ce229a69d0c5a2"

[[deps.ADTypes]]
git-tree-sha1 = "fa0822e5baee6e23081c2685ae27265dabee23d8"
git-tree-sha1 = "ae44a0c3d68a420d4ac0fa1f7e0c034ccecb6dc5"
uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
version = "1.4.0"
version = "1.5.2"

[deps.ADTypes.extensions]
ADTypesChainRulesCoreExt = "ChainRulesCore"
Expand Down Expand Up @@ -139,14 +139,14 @@ uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
version = "1.5.0"

[[deps.Bijections]]
git-tree-sha1 = "c9b163bd832e023571e86d0b90d9de92a9879088"
git-tree-sha1 = "95f5c7e2d177b7ba1a240b0518038b975d72a8c0"
uuid = "e2ed5e7c-b2de-5872-ae92-c73ca462fb04"
version = "0.1.6"
version = "0.1.7"

[[deps.BitFlags]]
git-tree-sha1 = "2dc09997850d68179b69dafb58ae806167a32b1b"
git-tree-sha1 = "0691e34b3bb8be9307330f88d1a3c3f25466c24d"
uuid = "d1d4a3ce-64b1-5f1a-9ba4-7e7e69966f35"
version = "0.1.8"
version = "0.1.9"

[[deps.Bonito]]
deps = ["Base64", "CodecZlib", "Colors", "Dates", "Deno_jll", "HTTP", "Hyperscript", "LinearAlgebra", "Markdown", "MsgPack", "Observables", "RelocatableFolders", "SHA", "Sockets", "Tables", "ThreadPools", "URIs", "UUIDs", "WidgetsBase"]
Expand Down Expand Up @@ -258,6 +258,11 @@ git-tree-sha1 = "7b8a93dba8af7e3b42fecabf646260105ac373f7"
uuid = "bbf7d656-a473-5ed7-a52c-81e309532950"
version = "0.3.0"

[[deps.CommonWorldInvalidations]]
git-tree-sha1 = "ae52d1c52048455e85a387fbee9be553ec2b68d0"
uuid = "f70d9fcc-98c5-4d4a-abd7-e4cdeebd8ca8"
version = "1.0.0"

[[deps.Compat]]
deps = ["TOML", "UUIDs"]
git-tree-sha1 = "b1c55339b7c6c350ee89f2c1604299660525b248"
Expand Down Expand Up @@ -582,22 +587,16 @@ version = "0.1.6"

[[deps.GeoInterface]]
deps = ["Extents"]
git-tree-sha1 = "801aef8228f7f04972e596b09d4dba481807c913"
git-tree-sha1 = "9fff8990361d5127b770e3454488360443019bb3"
uuid = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
version = "1.3.4"
version = "1.3.5"

[[deps.GeometryBasics]]
deps = ["EarCut_jll", "Extents", "GeoInterface", "IterTools", "LinearAlgebra", "StaticArrays", "StructArrays", "Tables"]
git-tree-sha1 = "b62f2b2d76cee0d61a2ef2b3118cd2a3215d3134"
uuid = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
version = "0.4.11"

[[deps.GeometryTypes]]
deps = ["ColorTypes", "FixedPointNumbers", "LinearAlgebra", "StaticArrays"]
git-tree-sha1 = "d796f7be0383b5416cd403420ce0af083b0f9b28"
uuid = "4d00f742-c7ba-57c2-abde-4428a4b178cb"
version = "0.8.5"

[[deps.Gettext_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "XML2_jll"]
git-tree-sha1 = "9b02998aba7bf074d14de89f9d37ca24a1a0b046"
Expand Down Expand Up @@ -1024,10 +1023,9 @@ uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
version = "2.28.2+1"

[[deps.Meshing]]
deps = ["GeometryBasics", "GeometryTypes", "StaticArrays"]
git-tree-sha1 = "b32d34f3e3ca44391ca7261ca4eb96af71e022b9"
path = "/Users/sjkelly/.julia/dev/Meshing"
uuid = "e6723b4c-ebff-59f1-b4b7-d97aa5274f73"
version = "0.5.7"
version = "0.7.0"

[[deps.Missings]]
deps = ["DataAPI"]
Expand Down Expand Up @@ -1435,9 +1433,9 @@ version = "0.3.8"

[[deps.SciMLStructures]]
deps = ["ArrayInterface"]
git-tree-sha1 = "6ab4beaf88dcdd2639bead916f2347f81dcacd0e"
git-tree-sha1 = "cfdd1200d150df1d3c055cc72ee6850742e982d7"
uuid = "53ae85a6-f571-4167-b2af-e1d143709226"
version = "1.3.0"
version = "1.4.1"

[[deps.Scratch]]
deps = ["Dates"]
Expand Down Expand Up @@ -1525,9 +1523,9 @@ version = "0.1.1"

[[deps.StaticArrays]]
deps = ["LinearAlgebra", "PrecompileTools", "Random", "StaticArraysCore"]
git-tree-sha1 = "6e00379a24597be4ae1ee6b2d882e15392040132"
git-tree-sha1 = "20833c5b7f7edf0e5026f23db7f268e4f23ec577"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.9.5"
version = "1.9.6"
weakdeps = ["ChainRulesCore", "Statistics"]

[deps.StaticArrays.extensions]
Expand Down Expand Up @@ -1603,15 +1601,15 @@ version = "0.2.1"

[[deps.SymbolicUtils]]
deps = ["AbstractTrees", "Bijections", "ChainRulesCore", "Combinatorics", "ConstructionBase", "DataStructures", "DocStringExtensions", "DynamicPolynomials", "IfElse", "LabelledArrays", "LinearAlgebra", "MultivariatePolynomials", "NaNMath", "Setfield", "SparseArrays", "SpecialFunctions", "StaticArrays", "SymbolicIndexingInterface", "TermInterface", "TimerOutputs", "Unityper"]
git-tree-sha1 = "79813de27af70906d223fbd89ad90dba3d88a2b0"
git-tree-sha1 = "cc049913a449be8c7ac00979831f26891e01add2"
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
version = "2.0.2"
version = "2.1.0"

[[deps.Symbolics]]
deps = ["ADTypes", "ArrayInterface", "Bijections", "ConstructionBase", "DataStructures", "DiffRules", "Distributions", "DocStringExtensions", "DomainSets", "DynamicPolynomials", "ForwardDiff", "IfElse", "LaTeXStrings", "LambertW", "Latexify", "Libdl", "LinearAlgebra", "LogExpFunctions", "MacroTools", "Markdown", "NaNMath", "PrecompileTools", "RecipesBase", "Reexport", "Requires", "RuntimeGeneratedFunctions", "SciMLBase", "Setfield", "SparseArrays", "SpecialFunctions", "StaticArrays", "SymbolicIndexingInterface", "SymbolicLimits", "SymbolicUtils", "TermInterface"]
git-tree-sha1 = "3669f8eba2720c595e16b038f33cb880c12ef7f7"
deps = ["ADTypes", "ArrayInterface", "Bijections", "CommonWorldInvalidations", "ConstructionBase", "DataStructures", "DiffRules", "Distributions", "DocStringExtensions", "DomainSets", "DynamicPolynomials", "ForwardDiff", "IfElse", "LaTeXStrings", "LambertW", "Latexify", "Libdl", "LinearAlgebra", "LogExpFunctions", "MacroTools", "Markdown", "NaNMath", "PrecompileTools", "RecipesBase", "Reexport", "Requires", "RuntimeGeneratedFunctions", "SciMLBase", "Setfield", "SparseArrays", "SpecialFunctions", "StaticArraysCore", "SymbolicIndexingInterface", "SymbolicLimits", "SymbolicUtils", "TermInterface"]
git-tree-sha1 = "8539a734b448ca27707709c62420b1bf115ed7d9"
uuid = "0c5d862f-8b57-4792-8d23-62f2024744c7"
version = "5.30.3"
version = "5.33.0"

[deps.Symbolics.extensions]
SymbolicsGroebnerExt = "Groebner"
Expand Down Expand Up @@ -1681,9 +1679,9 @@ uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
version = "0.5.24"

[[deps.TranscodingStreams]]
git-tree-sha1 = "a947ea21087caba0a798c5e494d0bb78e3a1a3a0"
git-tree-sha1 = "d73336d81cafdc277ff45558bb7eaa2b04a8e472"
uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
version = "0.10.9"
version = "0.10.10"
weakdeps = ["Random", "Test"]

[deps.TranscodingStreams.extensions]
Expand Down Expand Up @@ -1749,15 +1747,15 @@ version = "1.0.0"

[[deps.XML2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Zlib_jll"]
git-tree-sha1 = "52ff2af32e591541550bd753c0da8b9bc92bb9d9"
git-tree-sha1 = "d9717ce3518dc68a99e6b96300813760d887a01d"
uuid = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a"
version = "2.12.7+0"
version = "2.13.1+0"

[[deps.XSLT_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgcrypt_jll", "Libgpg_error_jll", "Libiconv_jll", "Pkg", "XML2_jll", "Zlib_jll"]
git-tree-sha1 = "91844873c4085240b95e795f692c4cec4d805f8a"
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgcrypt_jll", "Libgpg_error_jll", "Libiconv_jll", "XML2_jll", "Zlib_jll"]
git-tree-sha1 = "a54ee957f4c86b526460a720dbc882fa5edcbefc"
uuid = "aed1982a-8fda-507f-9586-7b0439959a61"
version = "1.1.34+0"
version = "1.1.41+0"

[[deps.Xorg_libX11_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Xorg_libxcb_jll", "Xorg_xtrans_jll"]
Expand Down Expand Up @@ -1797,9 +1795,9 @@ version = "0.1.1+0"

[[deps.Xorg_libxcb_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "XSLT_jll", "Xorg_libXau_jll", "Xorg_libXdmcp_jll", "Xorg_libpthread_stubs_jll"]
git-tree-sha1 = "b4bfde5d5b652e22b9c790ad00af08b6d042b97d"
git-tree-sha1 = "bcd466676fef0878338c61e655629fa7bbc69d8e"
uuid = "c7cfdc94-dc32-55de-ac96-5a1b8d977c5b"
version = "1.15.0+0"
version = "1.17.0+0"

[[deps.Xorg_xtrans_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
Expand Down
1 change: 1 addition & 0 deletions examples/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
HCubature = "19dc6840-f33b-545b-b366-655c7e3ffd49"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Meshing = "e6723b4c-ebff-59f1-b4b7-d97aa5274f73"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand Down
12 changes: 12 additions & 0 deletions examples/gyroid.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Revise
using Descartes: Gyroid, Cuboid, PolarWarp, Cylinder, FRep
using GeometryBasics: GeometryBasics, mesh
using Meshing: MarchingTetrahedra

p = intersect(PolarWarp(Gyroid(0.5), 200), Cuboid([50,50,50]))

@time m = mesh(p; samples=(100,100,100), algorithm=MarchingTetrahedra());

using WGLMakie
WGLMakie.activate!(resize_to=:body)
WGLMakie.mesh(m)
4 changes: 2 additions & 2 deletions examples/piping.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Descartes: Piping
using GeometryBasics: Mesh
using GeometryBasics: mesh

p = Piping(1.0, [[0,0,0],
[10,0,0],
[10,10,0],
[10,10,10],
[5,5,5]])

m = Mesh(p)
m = mesh(p)

using WGLMakie
mesh(m)
Expand Down
6 changes: 3 additions & 3 deletions examples/triangle_wave.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Revise
using Descartes: Descartes, TriangleWave, FRep, Grid, PolarWarp, Cuboid, union
using Descartes: Descartes, TriangleWave, FRep, PolarWarp, Cuboid, union
using WGLMakie
using CoordinateTransformations
using StaticArrays
Expand All @@ -19,7 +19,7 @@ y2 = [FRep(t2, e) for e in x]

lines(x,y2)

g = union(TriangleWave(1., 1), TriangleWave(2., 2))
g = union(TriangleWave(5., 1), TriangleWave(1., 2))

x_vals = -10:0.1:10
y_vals = -10:0.1:10
Expand All @@ -33,7 +33,7 @@ heatmap(x_vals, y_vals, z, alpha=0.6)
FRep(g, (x, y))

# Apply warp
g_polar = PolarWarp(g, 8)
g_polar = PolarWarp(g, 2)

#FRep(g_polar, (x, y))

Expand Down
4 changes: 3 additions & 1 deletion src/Descartes.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module Descartes

import Base: *, union, diff, intersect
using GeometryBasics: GeometryBasics
import GeometryBasics: AbstractMesh,
HyperRectangle,
Vec,
Mesh
mesh


using StaticArrays,
Meshing,
Expand Down
10 changes: 8 additions & 2 deletions src/frep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ function FRep(u::CSGIntersect, v)
f
end

_gyroid(v) = cos(v[1])*sin(v[2])+cos(v[2])*sin(v[3])+cos(v[3])*sin(v[1])

function FRep(p::Gyroid, v)
max(_gyroid(v)-p.width,-_gyroid(v)-p.width)
end

function FRep(p::Sphere,v)
norm(v) - p.radius
end
Expand Down Expand Up @@ -124,10 +130,10 @@ function FRep(p::PolarWarp, v)
end

function inner_warp(p::AbstractPrimitive, w, v)
T(a) = SVector(hypot(a...), atan(a[2], a[1])*w/2π) #cartesian to polar
T(a) = SVector(hypot(a[1], a[2]), atan(a[2], a[1])*w/2π, a[3]) #cartesian to polar
hr = gradient(a -> FRep(p, T(a)), SVector(v...))
mr, mt = T(v)
FRep(p, SVector(mr, mt))/norm(hr)
FRep(p, SVector(mr, mt, v[3]))/norm(hr)
end

function inner_warp(p::CSGUnion, w, v)
Expand Down
16 changes: 12 additions & 4 deletions src/hyperrectangles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function HyperRectangle(p::Circle{T}) where {T}
HyperRectangle{2,T}(Vec(-p.radius,-p.radius), Vec(p.radius*2,p.radius*2))
end

function HyperRectangle(p::PolarWarp)
nothing
end

function HyperRectangle(p::Piping{T}) where {T}
maxx, maxy, maxz = typemin(Float64), typemin(Float64), typemin(Float64)
minx, miny, minz = typemax(Float64), typemax(Float64), typemax(Float64)
Expand All @@ -42,7 +46,7 @@ end
function HyperRectangle(csg::CSGUnion)
h = HyperRectangle(csg.left)
for r in csg.right
union(h, HyperRectangle(r))
h = union(h, HyperRectangle(r))
end
h
end
Expand All @@ -54,7 +58,7 @@ end
function HyperRectangle(csg::CSGIntersect)
h = HyperRectangle(csg.left)
for r in csg.right
intersect(h, HyperRectangle(r))
h = intersect(h, HyperRectangle(r))
end
h
end
Expand Down Expand Up @@ -91,16 +95,20 @@ diff(h1::HyperRectangle, h2::HyperRectangle) = h1
"""
Perform a intersection between two HyperRectangles.
"""
function intersect(h1::HyperRectangle, h2::HyperRectangle)
function Base.intersect(h1::HyperRectangle, h2::HyperRectangle)
m = max.(minimum(h1), minimum(h2))
mm = min.(maximum(h1), maximum(h2))
HyperRectangle(m, mm - m)
end

function intersect(::Nothing, h1::HyperRectangle)
function Base.intersect(::Nothing, h1::HyperRectangle)
h1
end

function Base.intersect(h1::HyperRectangle, ::Nothing)
h1
end

function HyperRectangle(g::Gyroid)
nothing
end
34 changes: 29 additions & 5 deletions src/meshes.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
function (::Type{MT})(primitives::AbstractPrimitive...;
function GeometryBasics.mesh(primitives::AbstractPrimitive...;
samples=(128,128,128),
algorithm=MarchingCubes(),
adf=false) where {MT <: AbstractMesh}
algorithm=MarchingCubes())

f(x) = FRep(primitives[1], x)
meshes = Vector{MT}(undef, length(primitives))
meshes = Vector{GeometryBasics.Mesh}(undef, length(primitives))
sdf_arr = Array{Float64}(undef, samples)

for i = 1:length(primitives)
b = HyperRectangle(primitives[i])
meshes[i] = MT(x -> FRep(primitives[i], x), b, samples, algorithm)
rng = range.(b.origin, b.origin.+ b.widths)

@info "Sampling SDF"
xp = LinRange(first(rng[1]), last(rng[1]), samples[1])
yp = LinRange(first(rng[2]), last(rng[2]), samples[2])
zp = LinRange(first(rng[3]), last(rng[3]), samples[3])
sdf(v) = FRep(primitives[i], SVector(v...))
sdf_normal(v) = gradient(sdf, SVector(v...))

@time Threads.@threads for x in eachindex(xp)
for y in eachindex(yp), z in eachindex(zp)
sdf_arr[x,y,z] = sdf((xp[x],yp[y],zp[z]))
end
end

@info "generating mesh"
@time vts, fcs = isosurface(sdf_arr, algorithm, rng[1], rng[2], rng[3])
@info "remapping data types"
@time _points = map(GeometryBasics.Point, vts)
@time _faces = map(v -> GeometryBasics.TriangleFace{GeometryBasics.OneIndex}(v), fcs)
@info "evaluating normals"
@time normals = map(v -> GeometryBasics.Vec3f(sdf_normal(v)), vts)
@info "remapping mesh"
@time meshes[i] = GeometryBasics.Mesh(GeometryBasics.meta(_points; normals=normals), _faces)
end
return merge(meshes)
end
Expand Down
Loading

0 comments on commit 6ea3dd4

Please sign in to comment.