From 0b64993fab86094522baf076c618aa497d70119c Mon Sep 17 00:00:00 2001 From: Steve Kelly Date: Thu, 13 Jun 2024 17:10:50 -0400 Subject: [PATCH] warp wip --- examples/2d.jl | 4 ++- examples/Manifest.toml | 4 +-- examples/Project.toml | 2 ++ examples/csg.jl | 2 +- examples/example001.jl | 2 +- examples/triangle_wave.jl | 40 +++++++++++++++++++++--- examples/warp.jl | 65 +++++++++++++++++++++++++++++++++++++++ src/frep.jl | 6 ++++ src/hyperrectangles.jl | 8 +++++ src/types.jl | 7 ++++- 10 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 examples/warp.jl diff --git a/examples/2d.jl b/examples/2d.jl index f6f6060..6b67f36 100644 --- a/examples/2d.jl +++ b/examples/2d.jl @@ -1,5 +1,5 @@ using Revise -using Descartes: Circle, Square, LinearExtrude, translate +using Descartes: Circle, Square, LinearExtrude, translate, Grid using GeometryBasics: Mesh function beam(;beam_size = [50,10,10], @@ -15,6 +15,7 @@ function beam(;beam_size = [50,10,10], h = translate([hole_interval*i, beam_size[2]/2])Circle(hole_d/2) c = diff(c, h) end + c = intersect(c, Grid(1)) LinearExtrude(c, beam_size[3]) end @@ -26,5 +27,6 @@ beam(;hole_ct=5) m = Mesh(beam()) using WGLMakie +WGLMakie.activate!(resize_to=:body) mesh(m) #save("2d_beam.ply",m) diff --git a/examples/Manifest.toml b/examples/Manifest.toml index bcf7e69..87fbb4e 100644 --- a/examples/Manifest.toml +++ b/examples/Manifest.toml @@ -1,8 +1,8 @@ # This file is machine-generated - editing it directly is not advised -julia_version = "1.10.3" +julia_version = "1.10.4" manifest_format = "2.0" -project_hash = "307f054144a276e094fb1ddb88ac6864dbbb8dee" +project_hash = "79f437763e6a1eb62c3106094973df8666180632" [[deps.AbstractFFTs]] deps = ["LinearAlgebra"] diff --git a/examples/Project.toml b/examples/Project.toml index aaddec7..3b09e3d 100644 --- a/examples/Project.toml +++ b/examples/Project.toml @@ -1,9 +1,11 @@ [deps] BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" +CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298" Descartes = "ea4ead7c-22ac-5e77-afc5-cd927cdc0b20" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326" HCubature = "19dc6840-f33b-545b-b366-655c7e3ffd49" +Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc" Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" diff --git a/examples/csg.jl b/examples/csg.jl index 31229e0..b4575da 100644 --- a/examples/csg.jl +++ b/examples/csg.jl @@ -20,7 +20,7 @@ translate([24,0,0]) { """ -using Descartes: Cuboid, Sphere +using Descartes: Cuboid, Sphere, translate using GeometryBasics a = translate([-24,0,0])union( diff --git a/examples/example001.jl b/examples/example001.jl index 124fd99..c833a12 100644 --- a/examples/example001.jl +++ b/examples/example001.jl @@ -28,7 +28,7 @@ example001(); """ -using Descartes: Cylinder, Sphere +using Descartes: Cylinder, Sphere, rotate using GeometryBasics: Mesh function example001() diff --git a/examples/triangle_wave.jl b/examples/triangle_wave.jl index 69de053..beb86d4 100644 --- a/examples/triangle_wave.jl +++ b/examples/triangle_wave.jl @@ -1,7 +1,10 @@ using Revise -using Descartes: Descartes, TriangleWave, FRep, Grid -using WGLMakie - +using Descartes: Descartes, TriangleWave, FRep, Grid, PolarWarp +using WGLMakie, Makie +using CoordinateTransformations +using StaticArrays +using ForwardDiff +using LinearAlgebra t = TriangleWave(2.) x = 1:0.1:10 @@ -11,9 +14,36 @@ lines(x,y) g = Grid(1.0) -x_vals = 1:0.1:10 -y_vals = 1:0.1:10 +x_vals = -10:0.1:10 +y_vals = -10:0.1:10 +# basic Grid z = [FRep(g, (x, y)) for x in x_vals, y in y_vals] +extrema(z) +heatmap(x_vals, y_vals, z, alpha=0.6) + + +# Apply warp +g_polar = PolarWarp(g, 8) + +z = [min(FRep(g_polar, (x, y))...) for x in x_vals, y in y_vals] +extrema(z) +heatmap(x_vals, y_vals, z, alpha=0.6) + + + +polar_f(v) = FRep(g_polar, v) +polar_f([1,2]) +polar_f_min(v) = min(polar_f(v)...) +polar_f_min([1,2]) + +polar_f_jac(v) = ForwardDiff.jacobian(polar_f, v) +z = [polar_f_jac([x, y]) for x in x_vals, y in y_vals] + +# TODO : Correction factor +exper1(v) = norm(inv(polar_f_jac(v))*ForwardDiff.gradient(polar_f_min, v)) + +z = [exper1([x, y]) for x in x_vals, y in y_vals] +extrema(z) heatmap(x_vals, y_vals, z, alpha=0.6) diff --git a/examples/warp.jl b/examples/warp.jl new file mode 100644 index 0000000..c04bf51 --- /dev/null +++ b/examples/warp.jl @@ -0,0 +1,65 @@ +# https://www.desmos.com/calculator/mcjscmo6fq + +using WGLMakie +#Plots.plotly() +# Parameters (TODO: make const or propogate through functions for better performance) +p = 1 +t = 0.5 +w0 = 8 + +w = 2π / (p * w0) + +x_vals = range(-10, 10, length=1200) +y_vals = range(-10, 10, length=1200) + +""" +TriangleWave function +""" +function f_x(x) + abs(mod(-2 * p * x, p)) +end + +""" +Grid function +""" +function f(v) + minimum(f_x.(v)) +end + +z = [f((x, y)) - t/2 < 0 for x in x_vals, y in y_vals] +heatmap(x_vals, y_vals, z, alpha=0.6) + +function h(v) + mr = hypot(v...) + mt = atan(v[2], v[1])*w + min(f_x(mr), f_x(mt)) +end + +z = [h((x, y)) - t/2 <= 0 for x in x_vals, y in y_vals] +heatmap(x_vals, y_vals, z, alpha=0.6) + +function j(v) + w / hypot(v...) +end + +z = [h((x, y))/j((x,y)) - t/2 <= 0 for x in x_vals, y in y_vals] +heatmap(x_vals, y_vals, z, alpha=0.6, title="Grid Function", xlabel="x", ylabel="y", legend=false, aspect_ratio=:equal) + +using ForwardDiff + +function h_g(v) + g = ForwardDiff.gradient(h, [v...]) # TODO: make this faster + hypot(g...) +end + +z = [h((x, y))/h_g((x,y)) - t/2 <= 0 for x in x_vals, y in y_vals] +heatmap(x_vals, y_vals, z, alpha=0.6, title="Grid Function", xlabel="x", ylabel="y", legend=false, aspect_ratio=:equal) + +function h_norm(v) + mr = hypot(v...) + mt = atan(v[2], v[1])*w + min(f_x(mr), f_x(mt)/j(v)) +end + +z = [h_norm((x, y)) - t/2 <= 0 for x in x_vals, y in y_vals] +heatmap(x_vals, y_vals, z, alpha=0.6) diff --git a/src/frep.jl b/src/frep.jl index c7ffae4..096246c 100644 --- a/src/frep.jl +++ b/src/frep.jl @@ -121,3 +121,9 @@ end function FRep(g::Grid, v) minimum(FRep(TriangleWave(g.period), e) for e in v) end + +function FRep(p::PolarWarp, v) + mr = hypot(v...) + mt = atan(v[2], v[1])*p.w/2π + [FRep(p.primitive, mr), FRep(p.primitive, mt)] +end diff --git a/src/hyperrectangles.jl b/src/hyperrectangles.jl index 8881d2c..95f9d55 100644 --- a/src/hyperrectangles.jl +++ b/src/hyperrectangles.jl @@ -59,6 +59,10 @@ function HyperRectangle(csg::CSGIntersect) h end +function HyperRectangle(g::Grid{T}) where T + HyperRectangle(Vec(-100,100), Vec(200,200)) +end + function HyperRectangle(csg::CSGDiff) HyperRectangle(csg.left) end @@ -96,3 +100,7 @@ function intersect(h1::HyperRectangle, h2::HyperRectangle) mm = min.(maximum(h1), maximum(h2)) HyperRectangle(m, mm - m) end + +function Base.intersect(h1::HyperRectangle, ::Nothing) + h1 +end \ No newline at end of file diff --git a/src/types.jl b/src/types.jl index e4a7130..8003eb2 100644 --- a/src/types.jl +++ b/src/types.jl @@ -95,6 +95,11 @@ end A simple grid structure with a `period` field of type `T`. """ -struct Grid{T} +struct Grid{T} <: AbstractPrimitive{2, T} period::T end + +struct PolarWarp{T} <: AbstractPrimitive{2, T} + primitive + w::T +end \ No newline at end of file