forked from nortikin/sverchok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_Voronoi2D.py
152 lines (122 loc) · 5.59 KB
/
node_Voronoi2D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
from node_s import *
from util import *
from types import *
from voronoi import Site,computeVoronoiDiagram,computeDelaunayTriangulation
class Voronoi2DNode(Node, SverchCustomTreeNode):
''' Voronoi 2d line '''
bl_idname = 'Voronoi2DNode'
bl_label = 'Voronoi'
bl_icon = 'OUTLINER_OB_EMPTY'
clip = bpy.props.FloatProperty(name = 'clip', description='Clipping Distance', default=1.0, min=0, options={'ANIMATABLE'}, update=updateNode)
def init(self, context):
self.inputs.new('VerticesSocket', "Vertices", "Vertices")
# self.inputs.new('StringsSocket', "Clipping", "Clipping")
self.outputs.new('VerticesSocket', "Vertices", "Vertices")
self.outputs.new('StringsSocket', "Edges", "Edges")
# self.outputs.new('StringsSocket', "Polygons", "Polygons")
# Polygon output does not work right now. Should be fixed
def draw_buttons(self, context, layout):
layout.prop(self, "clip", text="Clipping")
def update(self):
# inputs
if 'Edges' in self.outputs and self.outputs['Edges'].links or \
'Vertices' in self.outputs and self.outputs['Vertices'].links:
if 'Vertices' in self.inputs and self.inputs['Vertices'].links:
points_in = SvGetSocketAnyType(self,self.inputs['Vertices'])
pts_out = []
# polys_out = []
edges_out = []
for obj in points_in:
pt_list = []
x_max = obj[0][0]
x_min = obj[0][0]
y_min = obj[0][1]
y_max = obj[0][1]
#creates points in format for voronoi library, throwing away z
for pt in obj:
x,y = pt[0],pt[1]
x_max = max(x,x_max)
x_min = min(x,x_min)
y_max = max(y,y_max)
y_min = min(x,x_min)
pt_list.append(Site(pt[0],pt[1]))
res = computeVoronoiDiagram(pt_list)
edges = res[2]
delta = self.clip
x_max = x_max + delta
y_max = y_max + delta
x_min = x_min - delta
y_min = y_min - delta
#clipping box to bounding box.
pts_tmp = []
for pt in res[0]:
x,y=pt[0],pt[1]
if x < x_min:
x = x_min
if x > x_max:
x = x_max
if y < y_min:
y = y_min
if y > y_max:
y = y_max
pts_tmp.append((x,y,0))
pts_out.append(pts_tmp)
edges_out.append([ (edge[1], edge[2]) for edge in edges if -1 not in edge ])
# outputs
if 'Vertices' in self.outputs and self.outputs['Vertices'].links:
SvSetSocketAnyType(self, 'Vertices', pts_out)
if 'Edges' in self.outputs and self.outputs['Edges'].links:
SvSetSocketAnyType(self,'Edges',edges_out)
def update_socket(self, context):
self.update()
#computeDelaunayTriangulation
class DelaunayTriangulation2DNode(Node, SverchCustomTreeNode):
''' DelaunayTriangulation '''
bl_idname = 'DelaunayTriangulation2DNode'
bl_label = 'Delaunay 2D'
bl_icon = 'OUTLINER_OB_EMPTY'
def init(self, context):
self.inputs.new('VerticesSocket', "Vertices", "Vertices")
# self.outputs.new('StringsSocket', "Edges", "Edges")
self.outputs.new('StringsSocket', "Polygons", "Polygons")
def update(self):
points_in = []
if not ('Polygons' in self.outputs and self.outputs['Polygons'].links):
return
if 'Vertices' in self.inputs and self.inputs['Vertices'].links:
points_in = SvGetSocketAnyType(self,self.inputs['Vertices'])
tris_out=[]
for obj in points_in:
pt_list = [Site(pt[0],pt[1]) for pt in obj]
res = computeDelaunayTriangulation(pt_list)
tris_out.append([tri for tri in res if -1 not in tri] )
if 'Polygons' in self.outputs and self.outputs['Polygons'].links:
SvSetSocketAnyType(self,'Polygons',tris_out)
def update_socket(self, context):
self.update()
def register():
bpy.utils.register_class(Voronoi2DNode)
bpy.utils.register_class(DelaunayTriangulation2DNode)
def unregister():
bpy.utils.unregister_class(Voronoi2DNode)
bpy.utils.unregister_class(DelaunayTriangulation2DNode)
if __name__ == "__main__":
register()