Browse Source

Extend lines at infinity

master
Taylor Bockman 4 years ago
parent
commit
be0dc761d1
  1. BIN
      example.PNG
  2. 5
      main_window.py
  3. 63
      voronoiview/ui/opengl_widget.py

BIN
example.PNG

Binary file not shown.

Before

Width:  |  Height:  |  Size: 92 KiB

After

Width:  |  Height:  |  Size: 120 KiB

5
main_window.py

@ -36,14 +36,11 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.setupUi(self)
# Size of point for drawing
self._point_size = 8
self._point_size = 4 * self.devicePixelRatio()
self._viewport_width = self.opengl_widget.width() * self.devicePixelRatio()
self._viewport_height = self.opengl_widget.height() * self.devicePixelRatio()
print(self.height())
print(self.width())
# PointManager is a class that is filled with static methods
# designed for managing state.
PointManager.point_set = PointSet(self._point_size,

63
voronoiview/ui/opengl_widget.py

@ -13,6 +13,7 @@ It should be split up into a few more separate files eventually...
Probably even into it's own module folder.
"""
import numpy as np
from OpenGL.GL import (glBegin, glClearColor, glColor3f, glColor4f,
glEnable, glEnd, GL_LINES, GL_LINE_LOOP, GL_LINE_SMOOTH,
GL_POINTS, glPointSize, glVertex3f,
@ -403,22 +404,62 @@ def draw_voronoi_diagram():
results = PointManager.voronoi_results
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]
print(results.regions)
#
# Code for these loops taken from the scipy documentation on plotting and adapted to OpenGL.
#
for region_indices in results.regions:
glBegin(GL_LINE_LOOP)
for idx in region_indices:
vertex = vertices[idx]
# Simple case - plot the non-infinity lines
for simplex in ridge_vertices:
glBegin(GL_LINES)
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:
continue
p1 = vertices[i, 0]
p2 = far_point[0]
p3 = vertices[i, 1]
p4 = far_point[1]
glBegin(GL_LINES)
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()

Loading…
Cancel
Save