Skip to content

Commit

Permalink
warp wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sjkelly committed Jun 13, 2024
1 parent e25f520 commit 0b64993
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 11 deletions.
4 changes: 3 additions & 1 deletion examples/2d.jl
Original file line number Diff line number Diff line change
@@ -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],
Expand All @@ -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

Expand All @@ -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)
4 changes: 2 additions & 2 deletions examples/Manifest.toml
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
2 changes: 2 additions & 0 deletions examples/Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/csg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion examples/example001.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ example001();
"""

using Descartes: Cylinder, Sphere
using Descartes: Cylinder, Sphere, rotate
using GeometryBasics: Mesh

function example001()
Expand Down
40 changes: 35 additions & 5 deletions examples/triangle_wave.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

65 changes: 65 additions & 0 deletions examples/warp.jl
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions src/frep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions src/hyperrectangles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
7 changes: 6 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0b64993

Please sign in to comment.