Skip to content

Commit

Permalink
Merge pull request #58 from JuliaGeometry/sjk/warp2
Browse files Browse the repository at this point in the history
sjk/warp2
  • Loading branch information
sjkelly authored Jun 19, 2024
2 parents e25f520 + e8f9f59 commit c746002
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 59 deletions.
12 changes: 4 additions & 8 deletions examples/2d.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Revise
using Descartes: Circle, Square, LinearExtrude, translate
using GeometryBasics: Mesh
using Descartes: Circle, Square, LinearExtrude, translate, Grid
using GeometryBasics: Mesh, HyperRectangle

function beam(;beam_size = [50,10,10],
hole_ct = 5,
Expand All @@ -15,16 +15,12 @@ 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 = Grid(c, 1)
LinearExtrude(c, beam_size[3])
end

beam(;hole_ct=3)
beam(;hole_ct=5)

@assert typeof(beam(;hole_ct=3)) == typeof(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
7 changes: 4 additions & 3 deletions 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 All @@ -37,6 +37,7 @@ c = translate([24,0,0])diff(

m = Mesh(a,b,c)
@show typeof(m)
using WGLMakie

scene = WGLMakie.mesh(coordinates(m), faces(m); shading=true, color=:yellow)
using WGLMakie
WGLMakie.activate!(resize_to=:body)
scene = WGLMakie.mesh(m; shading=true, color=:yellow)
5 changes: 5 additions & 0 deletions examples/differentiability.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Descartes
using StaticArrays
using ForwardDiff
using HCubature

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
21 changes: 0 additions & 21 deletions examples/fea.jl

This file was deleted.

6 changes: 3 additions & 3 deletions examples/radiused_union.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Descartes
using Descartes: rotate, Cuboid, Cylinder, Shell, translate, RadiusedCSGUnion
using GeometryBasics: Mesh

c2 = Cuboid([4,4.0,4])
Expand All @@ -9,9 +9,9 @@ u2 = diff(u, Cuboid([2,2,2]))

m = Mesh(u2)

#Descartes.visualize(u2)

save("radiused_union.ply",m)
#save("radiused_union.ply",m)

using WGLMakie
WGLMakie.activate!(resize_to=:body)
mesh(m)
42 changes: 36 additions & 6 deletions examples/triangle_wave.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
using Revise
using Descartes: Descartes, TriangleWave, FRep, Grid
using WGLMakie

using Descartes: Descartes, TriangleWave, FRep, Grid, PolarWarp, Cuboid
using WGLMakie, Makie
using CoordinateTransformations
using StaticArrays
using ForwardDiff
using LinearAlgebra
t = TriangleWave(2.)

x = 1:0.1:10
y = [FRep(t, e) for e in x]

lines(x,y)

g = Grid(1.0)
g = Grid(Square([10, 10]), 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)

66 changes: 66 additions & 0 deletions examples/warp.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# https://www.desmos.com/calculator/mcjscmo6fq

using WGLMakie
WGLMakie.activate!(resize_to=:body)
#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
24 changes: 12 additions & 12 deletions test/examples.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
@testset "examples" begin
dir = "../examples"
files = [ #"csg.jl",
"example001.jl",
#"piping.jl",
"radiused_union.jl",
"fea.jl",
"2d.jl"]
for file in files
@testset "$file" begin
include("$dir/$file")
end
end
#dir = "../examples"
#dir = "../examples"
#files = [ "csg.jl",
# "example001.jl",
# #"piping.jl",
# "radiused_union.jl",
# "2d.jl"]
#for file in files
# @testset "$file" begin
# include("$dir/$file")
# end
#end
end
18 changes: 18 additions & 0 deletions test/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,22 @@
op(p...)
end
end

function looped_op(;ct = 5, op = diff)

hole_interval = 10/(ct + 1)

c = Square([10,10])

for i = 1:ct
h = translate([hole_interval*i, 10/2])Circle(1)
c = op(c, h)
end
end

# test type stability of setops in a loop
@test typeof(looped_op(;ct=3, op=diff)) == typeof(looped_op(;ct=5, op=diff))
@test typeof(looped_op(;ct=3, op=union)) == typeof(looped_op(;ct=5, op=union))
@test typeof(looped_op(;ct=3, op=intersect)) == typeof(looped_op(;ct=5, op=intersect))

end
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Descartes: Sphere, Cuboid, Cylinder, CSGUnion, CSGIntersect, CSGDiff, MapContainer, Piping, FRep, Square, translate, rotate, Shell, RadiusedCSGUnion
using Descartes: Sphere, Cuboid, Cylinder, CSGUnion, CSGIntersect, CSGDiff, MapContainer, Piping, FRep, Square, Circle, translate, rotate, Shell, RadiusedCSGUnion, LinearExtrude
using StaticArrays
using Test
using Combinatorics
using FileIO
using MeshIO

import GeometryBasics: Mesh, HyperRectangle
using GeometryBasics: Mesh, HyperRectangle

primitives_array = [Sphere(1), Cuboid([1,2,3]), Cylinder(3,4)]
operations_array = [CSGUnion, CSGIntersect, CSGDiff]
Expand Down

0 comments on commit c746002

Please sign in to comment.