Skip to content

Instantly share code, notes, and snippets.

@mschauer
Created May 29, 2018 13:59
Show Gist options
  • Select an option

  • Save mschauer/36d8c70c1cfc51266763de03bf7a7927 to your computer and use it in GitHub Desktop.

Select an option

Save mschauer/36d8c70c1cfc51266763de03bf7a7927 to your computer and use it in GitHub Desktop.
Plot picture with distorted delaunay mesh in Makie
using VoronoiDelaunay
using StaticArrays
using Makie, GeometryTypes, GLVisualize#, GLWindow
srand(10)
const Point = SArray{Tuple{2},Float64,1,2} # point in 2
q0 = rand(Point, 20)
qT = q0 + 0.2 .*rand(Point, 20)
module IdPoints
using VoronoiDelaunay
import VoronoiDelaunay: getx, gety
export getx, gety, getid, IdPoint
immutable IdPoint <: AbstractPoint2D
_x::Float64
_y::Float64
index::Int64
end
IdPoint(x, y) = IdPoint(x, y, -1)
getx(p::IdPoint) = p._x
gety(p::IdPoint) = p._y
getid(p::IdPoint) = p.index
end
using IdPoints
function scale12(qs)
xl, xu = extrema(first.(qs))
yl, yu = extrema(last.(qs))
pts = [IdPoint(1.0 + eps() + (1-3eps())*(qs[i][1]-xl)/(xu-xl),
1.0 + eps() + (1-3eps())*(qs[i][2]-yl)/(yu-yl), i) for i in 1:length(qs)]
# the first for indices are reserved for the corners
pts, (xl, xu, yl, yu)
end
tessel = DelaunayTessellation2D{IdPoint}()
pts0, scal = scale12(q0)
ptsT, scal = scale12(qT)
for pt in pts0
push!(tessel, pt)
end
faces = GeometryTypes.Face{3,GeometryTypes.OffsetInteger{-1,UInt32}}[]
for tr in tessel
# println(getx(tr._a), ",", getx(tr._b), ",", getx(tr._c) )
# println(gety(tr._a), ",", gety(tr._b), ",", gety(tr._c) )
# println(getid(tr._a), ",", getid(tr._b), ",", getid(tr._c))
push!(faces, GeometryTypes.Face{3,GeometryTypes.OffsetInteger{-1,UInt32}}(getid(tr._a), getid(tr._b), getid(tr._c)))
end
#corners = [IdPoint(1.0, 1.0, 2), IdPoint(2.0, 1.0, 2), IdPoint(1.0, 2.0, 3), IdPoint(2.0, 2.0, 4)]
vertices = [Point3f0(getx(pt)-1, gety(pt)-1, 0) for pt in ptsT]
uv = [UV{Float32}(getx(pt)-1, gety(pt)-1) for pt in pts0]
scene = Scene(resolution = (500, 500))
img = loadasset("doge.png");
mesh = GLNormalUVMesh(vertices = vertices, texturecoordinates = uv, faces = faces)
Makie.mesh(mesh, color = img, shading = false)
wireframe(mesh)
scatter(vertices, markersize=0.01f0)
#scatter(uv, markersize=0.01f0)
center!(scene)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment