Skip to content

Commit

Permalink
add dowload_thingi10k and compute_cartesian_desc to src
Browse files Browse the repository at this point in the history
  • Loading branch information
pmartorell committed Oct 18, 2022
1 parent 64a86b9 commit d61ab8b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 53 deletions.
18 changes: 1 addition & 17 deletions examples/SubTriangulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,7 @@ using Test
using Gridap
using STLCutters

import Downloads

function download(id;path="")
url = "https://www.thingiverse.com/download:$id"
r = Downloads.request(url)
if 200 r.status 399
_,ext = splitext(r.url)
ext, = split(ext,'?')
mkpath(path)
filename = joinpath(path,"$id$ext")
Downloads.download(url,filename)
else
@warn "$id is no longer available on Thingiverse."
filename = nothing
end
filename
end
download = download_thingi10k

function main(filename;n=20=0.2,output=nothing)

Expand Down
4 changes: 4 additions & 0 deletions src/STLCutters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module STLCutters
import MeshIO
import Plots
import GraphRecipes
import Downloads

using Gridap
using Gridap.Arrays
Expand Down Expand Up @@ -47,12 +48,15 @@ import GridapEmbedded.CSG: similar_geometry

import Plots: plot

import Base: split

export STLGeometry
export STLCutter

export subtriangulate
export get_bounding_box
export check_requisites
export download_thingi10k

include("SimplexFaces.jl")
include("STLs.jl")
Expand Down
60 changes: 60 additions & 0 deletions src/STLs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -991,3 +991,63 @@ function bisector_plane_cache(stl::STL,d::Integer)
fc = get_dface_cache(stl,d)
c,fc
end

# Testing helpers

"""
download_thingi10k(id;path"")
Dowloads surface model from [thingiverse](https://www.thingiverse.com)
indexed by FileID at [Thingi10K](https://ten-thousand-models.appspot.com)
"""

function download_thingi10k(id;path="")
url = "https://www.thingiverse.com/download:$id"
r = Downloads.request(url)
if 200 r.status 399
_,ext = splitext(r.url)
ext, = split(ext,'?')
mkpath(path)
filename = joinpath(path,"$id$ext")
Downloads.download(url,filename)
else
@warn "$id is no longer available on Thingiverse."
filename = nothing
end
filename
end

"""
compute_cartesian_descriptor(pmin,pmax;nmin,nmax)
Compute `CartesianDescriptor` in a bounding box
with the same cell size (h) in all directions:
h = min( max((pmin-pmax)/nmax), min((pmax-pmin)/nmin) )
"""

function compute_cartesian_descriptor(pmin::Point,pmax::Point;kwargs...)
CartesianDescriptor( _compute_cartesian_description(pmin,pmax;kwargs...) ... )
end

function _compute_cartesian_description(
pmin::Point{D},
pmax::Point{D};
nmin=10,
nmax=100) where D

s = pmax - pmin
s = Tuple(s)
h = maximum(s)/nmax
p = ( s./maximum(s) ) .* nmax
p = ceil.(p)
if minimum(p) < nmin
p = ( s./minimum(s) ) .* nmin
h = minimum(s) / nmin
end
origin = pmin
sizes = tfill(h,Val{D}())
partition = Int.(ceil.(p))
origin,sizes,partition
end

41 changes: 5 additions & 36 deletions test/SampleGeometriesTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,11 @@ using Gridap.Arrays
using Gridap.Helpers
using STLCutters

import Downloads

using STLCutters: volumes
using STLCutters: volume, surface
using STLCutters: compute_cartesian_descriptor

function download(id;path="")
url = "https://www.thingiverse.com/download:$id"
r = Downloads.request(url)
if 200 r.status 399
_,ext = splitext(r.url)
ext, = split(ext,'?')
mkpath(path)
filename = joinpath(path,"$id$ext")
Downloads.download(url,filename)
else
@warn "$id is no longer available on Thingiverse."
filename = nothing
end
filename
end

function compute_sizes(pmin::Point{D},pmax::Point{D};nmin=10,nmax=100) where D
s = pmax - pmin
s = Tuple(s)
h = maximum(s)/nmax
p = ( s./maximum(s) ) .* nmax
p = ceil.(p)
if minimum(p) < nmin
p = ( s./minimum(s) ) .* nmin
h = minimum(s) / nmin
end
origin = pmin
sizes = tfill(h,Val{D}())
partition = Int.(ceil.(p))
origin,sizes,partition
end
download = download_thingi10k

function main(filename;
nmin=10,nmax=100=0.2,tolfactor=1000,kdtree=false,output=nothing)
Expand All @@ -52,8 +21,8 @@ function main(filename;

pmin,pmax = get_bounding_box(stl)
Δ = (pmax-pmin)*δ
origin,sizes,partition = compute_sizes(pmin-Δ,pmax+Δ;nmin,nmax)
model = CartesianDiscreteModel(origin,sizes,partition)
desc = compute_cartesian_descriptor(pmin-Δ,pmax+Δ;nmin,nmax)
model = CartesianDiscreteModel(desc)

@test check_requisites(stl,model)

Expand Down Expand Up @@ -88,7 +57,7 @@ function main(filename;


println("TIME: $(t.time)")
println("Background partition: $partition")
println("Background partition: $(desc.partition)")
println("Num background cells: $(num_cells(model))")
println("Num subcells: $(num_cells(subcells))")
println("ϵ_V = $volume_error")
Expand Down

0 comments on commit d61ab8b

Please sign in to comment.