Skip to content

Instantly share code, notes, and snippets.

@damhiya
Created October 21, 2023 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damhiya/6c12e8b0edd9a7e9e9f6c6e53ecbd1a0 to your computer and use it in GitHub Desktop.
Save damhiya/6c12e8b0edd9a7e9e9f6c6e53ecbd1a0 to your computer and use it in GitHub Desktop.
# Tetrahedron
# volume = 1/6
# area = (3 + sqrt 3)/2
tetrahedronMesh =
let vmap = [ 0. 1. 0. 0.
; 0. 0. 1. 0.
; 0. 0. 0. 1.
]
fmap = [ 0 0 0 1
; 2 1 3 2
; 1 3 2 3
]
(vmap, fmap)
end
# Torus
# volume = 2π²ab²
# area = 4π²ab
torus(a,b,ϕ,θ) = [ a * cos(ϕ) + b * cos(ϕ) * cos(θ)
, a * sin(ϕ) + b * sin(ϕ) * cos(θ)
, b * sin(θ)
]
torusMesh(a,b,m,n) =
let ϕ(i) = 2π * (i/m),
θ(j) = 2π * (j/n),
ι(i,j) = n * (i%m) + (j%n),
vmap = hcat((torus(a,b,ϕ(i),θ(j)) for i=0:m-1 for j=0:n-1)...),
fmap = hcat(([ι(i,j+p), ι(i+1,j), ι(i+p,j+1)] for i=0:m-1 for j=0:n-1 for p=0:1)...)
(vmap, fmap)
end
# Hemisphere
# area = 2πr²
# perimeter = 2πr
hemisphere(r,θ,ϕ) = [ r * sin(θ) * cos(ϕ)
, r * sin(θ) * sin(ϕ)
, r * cos(θ)
]
hemisphereMesh(r,n) =
let θ(i) = 0.5π / n * i,
ϕ(j) = 2π / n * j,
ι(i,j) = (i == 0) ? 0 : (1 + n*(i-1) + j%n),
vmap = hcat(hemisphere(r,θ(0),ϕ(0)), (hemisphere(r,θ(i),ϕ(j)) for i=1:n for j=0:n-1)...)
fmap = hcat( ([ι(0,j+1), ι(1 ,j), ι(1 ,j+1)] for j=0:n-1)...
, ([ι(i,j+p), ι(i+1,j), ι(i+p,j+1)] for i=1:n-1 for j=0:n-1 for p=0:1)...
)
(vmap, fmap)
end
# Möbius strip
# area = 6.35327
# perimeter = 13.0067544662
moebius(u,v) = [ (1 + u * cos(v/2)) * cos(v)
, (1 + u * cos(v/2)) * sin(v)
, u * sin(v/2)
]
moebiusMesh(n) =
let u(i) = -0.5 + 1 / n * i,
v(j) = 2π / n * j,
ι(i,j) = j == n ? n*(n-i) : (n*i + j),
vmap = hcat((moebius(u(i), v(j)) for i=0:n for j=0:n-1)...),
fmap = hcat(([ι(i,j+p), ι(i+1,j), ι(i+p,j+1)] for i=0:n-1 for j=0:n-1 for p=0:1)...)
(vmap, fmap)
end
#### interface ####
function examples(id)
if id == "tetrahedron"
return tetrahedronMesh
elseif id == "torus"
return torusMesh(10.0,1.0,100,100)
elseif id == "hemisphere"
return hemisphereMesh(1.0,100)
elseif id == "moebius"
return moebiusMesh(100)
else
println("No such example : $id")
exit(-1)
end
end
function main()
id = ARGS[1]
(vmap, fmap) = examples(id)
vs = eachcol(vmap)
fs = eachcol(fmap)
println("$(length(vs))")
for v in vs
println("($(v[1]), $(v[2]), $(v[3]))")
end
println("$(length(fs))")
for f in fs
println("($(f[1]), $(f[2]), $(f[3]))")
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment