|
|
@ -13,6 +13,7 @@ It should be split up into a few more separate files eventually... |
|
|
|
Probably even into it's own module folder. |
|
|
|
Probably even into it's own module folder. |
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import numpy as np |
|
|
|
from OpenGL.GL import (glBegin, glClearColor, glColor3f, glColor4f, |
|
|
|
from OpenGL.GL import (glBegin, glClearColor, glColor3f, glColor4f, |
|
|
|
glEnable, glEnd, GL_LINES, GL_LINE_LOOP, GL_LINE_SMOOTH, |
|
|
|
glEnable, glEnd, GL_LINES, GL_LINE_LOOP, GL_LINE_SMOOTH, |
|
|
|
GL_POINTS, glPointSize, glVertex3f, |
|
|
|
GL_POINTS, glPointSize, glVertex3f, |
|
|
@ -403,22 +404,62 @@ def draw_voronoi_diagram(): |
|
|
|
results = PointManager.voronoi_results |
|
|
|
results = PointManager.voronoi_results |
|
|
|
|
|
|
|
|
|
|
|
vertices = results.vertices |
|
|
|
vertices = results.vertices |
|
|
|
|
|
|
|
ridge_vertices = results.ridge_vertices |
|
|
|
|
|
|
|
ridge_points = results.ridge_points |
|
|
|
|
|
|
|
points = results.points |
|
|
|
|
|
|
|
center = points.mean(axis=0) |
|
|
|
|
|
|
|
|
|
|
|
color = COLOR_TO_RGBA[Color.BLACK] |
|
|
|
color = COLOR_TO_RGBA[Color.BLACK] |
|
|
|
|
|
|
|
|
|
|
|
print(results.regions) |
|
|
|
# |
|
|
|
|
|
|
|
# Code for these loops taken from the scipy documentation on plotting and adapted to OpenGL. |
|
|
|
|
|
|
|
# |
|
|
|
|
|
|
|
|
|
|
|
for region_indices in results.regions: |
|
|
|
# Simple case - plot the non-infinity lines |
|
|
|
glBegin(GL_LINE_LOOP) |
|
|
|
for simplex in ridge_vertices: |
|
|
|
for idx in region_indices: |
|
|
|
glBegin(GL_LINES) |
|
|
|
vertex = vertices[idx] |
|
|
|
simplex = np.asarray(simplex) |
|
|
|
|
|
|
|
if np.all(simplex >= 0): |
|
|
|
|
|
|
|
glColor3f(color[0], color[1], color[2]) |
|
|
|
|
|
|
|
p1 = vertices[simplex, 0] |
|
|
|
|
|
|
|
p2 = vertices[simplex, 1] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glVertex3f(__clamp_x(p1[0]), |
|
|
|
|
|
|
|
__clamp_y(p2[0]), |
|
|
|
|
|
|
|
0.0) |
|
|
|
|
|
|
|
glVertex3f(__clamp_x(p1[1]), |
|
|
|
|
|
|
|
__clamp_y(p2[1]), |
|
|
|
|
|
|
|
0.0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glEnd() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Hard case - points at infinity |
|
|
|
|
|
|
|
for pointidx, simplex in zip(ridge_points, ridge_vertices): |
|
|
|
|
|
|
|
simplex = np.asarray(simplex) |
|
|
|
|
|
|
|
if np.any(simplex < 0): |
|
|
|
|
|
|
|
i = simplex[simplex >= 0][0] # finite end Voronoi vertex |
|
|
|
|
|
|
|
t = points[pointidx[1]] - points[pointidx[0]] # tangent |
|
|
|
|
|
|
|
t = t / np.linalg.norm(t) |
|
|
|
|
|
|
|
n = np.array([-t[1], t[0]]) # normal |
|
|
|
|
|
|
|
midpoint = points[pointidx].mean(axis=0) |
|
|
|
|
|
|
|
far_point = vertices[i] + np.sign(np.dot(midpoint - center, n)) * n * 100 |
|
|
|
|
|
|
|
|
|
|
|
if -1 in region_indices: |
|
|
|
p1 = vertices[i, 0] |
|
|
|
continue |
|
|
|
p2 = far_point[0] |
|
|
|
|
|
|
|
p3 = vertices[i, 1] |
|
|
|
|
|
|
|
p4 = far_point[1] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glBegin(GL_LINES) |
|
|
|
|
|
|
|
|
|
|
|
glColor3f(color[0], color[1], color[2]) |
|
|
|
glColor3f(color[0], color[1], color[2]) |
|
|
|
glVertex3f(__clamp_x(vertex[0]), |
|
|
|
|
|
|
|
__clamp_y(vertex[1]), |
|
|
|
|
|
|
|
0.0) # Z is currently fixed to 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glEnd() |
|
|
|
glVertex3f(__clamp_x(p1), |
|
|
|
|
|
|
|
__clamp_y(p3), |
|
|
|
|
|
|
|
0.0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# x2000 scaling factor so the far points run off screen |
|
|
|
|
|
|
|
glVertex3f(__clamp_x(p2) * 2000, |
|
|
|
|
|
|
|
__clamp_y(p4) * 2000, |
|
|
|
|
|
|
|
0.0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glEnd() |
|
|
|