def voronoi(s, ts=None, sp=[]):
    if ts == None:
        x0, x1, y0, y1 = setToRectangle(s)
        sp = [
            Vec2D(2 * x0 - x1, 2 * y0 - y1),
            Vec2D(2 * x1 - x0, 2 * y0 - y1),
            Vec2D(2 * x0 - x1, 2 * y1 - y0),
            Vec2D(2 * x1 - x0, 2 * y1 - y0)
        ]
        ts = delaunay(s + sp, False)
    else:
        for p in s:
            update_triangu(p, ts)
    cells = {}
    for t in ts:
        c = t.center
        for v in t.vertices():
            if v in cells:
                if c not in cells[v]:
                    cells[v].append(c)
            else:
                cells[v] = [c]
    to_remove = []
    for p in cells:
        if p in sp:
            to_remove.append(p)
        else:
            # print(p)
            cells[p] = convex_hull(cells[p])
    for p in to_remove:
        del cells[p]
    return cells
def delaunay(s, steps=False):
    x0, x1, y0, y1 = setToRectangle(s)
    W, H = x1 - x0, y1 - y0
    bigT = Triangle(Vec2D(x0 - W * 6 / 10, y0 - H / 20),
                    Vec2D(x1 + W * 6 / 10, y0 - H / 20),
                    Vec2D(x0 + W / 2, y1 + H * 11 / 10),
                    compute_cent=True)
    ts = {bigT}
    if steps:
        sts = []
    for p in s:
        update_triangu(p, ts)
        if steps:
            ts2 = ts.copy()
            remove_tri(ts2, bigT)
            sts.append(ts2)
    remove_tri(ts, bigT)
    if steps:
        return sts + [ts]
    return ts
def getCol(x, y, P, first=True):
    col, np = [0, 0, 0], 0
    if first or insideConv(Vec2D(x / w * rat, y / h), P):
        toSee[x][y] = False
        rgb = im.getpixel((x, h - 1 - y))
        for i in range(3):
            col[i] += rgb[i]
        np = 1
        for a, b in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
            if a >= 0 and a < w and b >= 0 and b < h and toSee[a][b]:
                c2, n2 = getCol(a, b, P, False)
                for i in range(3):
                    col[i] += c2[i]
                np += n2
    return col, np
def k_mean_step(centers, ps):
    ncs = len(centers)
    clus = [[] for _ in range(ncs)]
    sc = [Vec2D(0, 0) for _ in range(ncs)]
    for p in ps:
        i = -1
        d = float('inf')
        for j in range(ncs):
            nd = (p - centers[j]).norm22()
            if nd < d:
                i, d = j, nd
        clus[i].append(p)
        sc[i] += p
    for i in range(ncs):
        centers[i] = sc[i] / len(clus[i])
    return clus
import plot_opt
from base import Vec2D
import base
import pylab as pl
import delaunay
from PIL import Image

fig = pl.figure(figsize=(6, 6))

n = 6000
centers = base.pointSet(0, 1, 0, 1, n - 4) + [
    Vec2D(0.01, 0.01),
    Vec2D(0.99, 0.01),
    Vec2D(0.01, 0.99),
    Vec2D(0.99, 0.99)
]
square = [Vec2D(0, 0), Vec2D(1, 0), Vec2D(1, 1), Vec2D(0, 1), Vec2D(0, 0)]
im = Image.open("base_images/panda.jpg").convert("RGB")
w, h = im.width, im.height


def get_col(c):
    col = im.getpixel((int(c.x * w), int(h - c.y * h)))
    return tuple(col[i] / 255 for i in range(3))


cells = delaunay.voronoi(centers)

x0, y0 = base.setToLists(centers)
for i in range(n):
    c = centers[i]
Exemple #6
0
#!/usr/bin/env python3

from base import pointSetSpace, setToLists, Vec2D
from delaunay import toGraph
from graphs import dijkstra, plotGraph
import pylab as pl
import plot_opt
import matplotlib.animation as anim
import numpy as np

n = 180
V = [Vec2D(0.5, 0.5)] + pointSetSpace(0, 1, 0, 1, n - 1, 0.8 / n**0.5)
E = toGraph(V)
ds, pred = dijkstra(V, E, V[0])

fig = pl.figure()
pl.xlim(0, 1)
pl.ylim(0, 1)
plotGraph(V, E)

es = []
for v in V[1:]:
    es.append((ds[v], v))
es.sort()
es = [setToLists([e[1], pred[e[1]]]) for e in es]
ls = [pl.plot([], [], color='red', linewidth=1.6)[0] for _ in range(n - 1)]

nc = 7
dd = 1 / (nc - 1)
x, y = np.mgrid[slice(0, 1 + dd / 2, dd), slice(0, 1 + dd / 2, dd)]
z = [[[0, 0] for _ in range(nc)] for _ in range(nc)]
for ni in range(N):
	im = [vertl, t1, t2]
	x, y = setToLists(ps)
	for i in range(lenp):
		l = lines[i]
		x, y = setToLists(l)
		im.append(pl.plot(x, y, color='red')[0])
		lp = l[-1]
		c = (lp.x, abs(lp.x - lp.y), lp.y)
		im.append(pl.scatter([ps[i].x], [ps[i].y], color=c))
	if ni != N-1:
		np = []
		for l in lines:
			lp = l[-1]
			s = 0
			p = Vec2D(0, 0)
			for l2 in lines:
				if l == l2:
					continue
				k = math.exp(-(lp - l2[-1]).norm22() / sig2)
				s += k
				p += k * l2[-1]
			np.append((1 - inter)* lp + inter * (p / s))
		for i in range(len(lines)):
			lines[i].append(np[i])
	#####################################################
	if ni % (N // 7) == 0:
		if len(centers) < NC:
			clustering.add_uniform_center(ps, ds, centers)
		clusters = clustering.k_mean_step(centers, ps)
		k_mean_ims = []
#!/usr/bin/env python3
from base import pointSetSpace, setToLists, turnPositive, Vec2D, insideConv
import pylab as pl
import matplotlib.animation as anim
from delaunay import delaunay
from convexe import convex_hull
import plot_opt
from PIL import Image
import sys

sys.setrecursionlimit(10**4)

n = 1000
rat = 12 / 8
s = pointSetSpace(0, rat, 0, 1, n, 0.8 / n**0.5)
s += [Vec2D(-10, -10), Vec2D(10, -10), Vec2D(-10, 10), Vec2D(10, -10)]

fig = pl.figure()
pl.xlim(0, rat)
pl.ylim(0, 1)
xs, ys = setToLists(s)
# pl.scatter(xs, ys, linewidths=1, color='blue')
ts = delaunay(s)
cs = []
es = {}
vs = {}
for t in ts:
    #x, y = t.toPylab()
    #pl.plot(x, y, linewidth=0.8, color='green')
    c = t.center
    cs.append(c)