Option Strict Off Option Explicit On Imports RMA.OpenNurbs Imports RMA.Rhino Imports GrassHopper Imports GrassHopper.Kernel Imports System Imports System.IO Imports System.Xml Imports System.Math Imports System.Data Imports System.Drawing Imports System.Reflection Imports System.Collections Imports System.Collections.Generic Imports System.Windows.Forms Imports Microsoft.VisualBasic 'Code generated by Grasshopper™ 'Copyright © 2008 - Robert McNeel & Associates _ Public Class Script_Runtime_Instance Implements IEH_ScriptInstance 'Declare err and out list members Private __err As New List(Of String) Private __out As New List(Of String) 'Declare doc and app members Private doc As MRhinoDoc = RMA.Rhino.RhUtil.RhinoApp().ActiveDoc() Private app As MRhinoApp = RMA.Rhino.RhUtil.RhinoApp() 'Define utility functions Private Sub Print(ByVal S As String) __out.Add(S) End Sub Private Sub Print(ByVal S As String, <[ParamArray]()> ByVal args As Object()) __out.Add(String.Format(S, args)) End Sub Private Sub Reflect(ByVal d As Object) If (d Is Nothing) Then __out.Add("null-object") Return End If Dim stream As String = String.Format("{0} {{", d.GetType().Name) Dim proc_info As MethodInfo() = d.GetType().GetMethods() If (proc_info Is Nothing) Then stream &= "this type exports no methods…" Else stream &= "methods: " Dim methods As New List(Of String) For Each method As MethodInfo In proc_info Dim m_name As String = String.Format("'{0}'", method.Name) If (Not methods.Contains(m_name)) Then methods.Add(m_name) Next stream &= String.Join(", ", methods.ToArray()) End If stream &= "}" __out.Add(stream) End Sub Private Sub Reflect(ByVal d As Object, ByVal proc_name As String) If (String.IsNullOrEmpty(proc_name)) Then Return If (d Is Nothing) Then __out.Add("null-object") Return End If Dim stream As String = String.Format("{0}.{1} {{", d.GetType().Name, proc_name) Dim proc_info As MethodInfo() = d.GetType().GetMethods() If (proc_info Is Nothing) Then stream &= "{this type exports no methods…}" Else Dim methods As New List(Of String) For Each method As MethodInfo In proc_info If (method.Name.Equals(proc_name, StringComparison.OrdinalIgnoreCase)) Then Dim overload_signature As String = method.Name & "(" Dim params As ParameterInfo() = method.GetParameters() If (params IsNot Nothing) Then Dim parameters As New List(Of String) For Each param As ParameterInfo In params Dim op_tag As String = String.Empty If (param.IsOptional) Then op_tag = "Optional " parameters.Add(String.Format("{0}{1} As {2}", op_tag, param.Name, param.ParameterType.Name)) Next overload_signature &= String.Join(", ", parameters.ToArray()) End If overload_signature &= ") As " & method.ReturnType.Name methods.Add(overload_signature) End If Next stream &= String.Join(", ", methods.ToArray()) End If stream &= "}" __out.Add(stream) End Sub Public Function RunScript(ByVal __data_in As System.Collections.Generic.List(Of Object), _ ByVal __data_out As System.Collections.Generic.List(Of Grasshopper.Kernel.IEH_Param)) As Boolean _ Implements IEH_ScriptInstance.RunScript 'Prepare for a new run... '1. Reset lists __out.Clear() __err.Clear() '2. Assign input values Dim pointSet As List(Of On3dPoint) = Nothing If (__data_in(0) IsNot Nothing) Then pointSet = New List(Of On3dPoint)(100) For Each __item As System.Object In __data_in(0) If (__item Is Nothing) Then pointSet.Add(Nothing) Else pointSet.Add(CType(__item, On3dPoint)) End If Next End If Dim boundary As List(Of On3dPoint) = Nothing If (__data_in(1) IsNot Nothing) Then boundary = New List(Of On3dPoint)(100) For Each __item As System.Object In __data_in(1) If (__item Is Nothing) Then boundary.Add(Nothing) Else boundary.Add(CType(__item, On3dPoint)) End If Next End If '3. Declare output values Dim A As System.Object = Nothing Dim B As System.Object = Nothing Dim C As System.Object = Nothing Dim D As System.Object = Nothing Try 'Custom code not written and not copyrighted by Robert McNeel & Associates ' ''' ' 'Script by Dimitrie Stefanescu ' 'Based on David Rutten's algorithm ' 'Released under 'Creative Commons Attribution-Noncommercial-Share Alike 3.0 Licence. 'http://creativecommons.org/licenses/by-nc-sa/3.0/us/ ' ' 'Version .9f - code ain't clean A = New List (Of OnLine) B = New List (Of onLine) Dim _A As New List (Of onLine) Dim __A As New List (Of on3dPoint) Dim _C As New List (Of onPolyline) 'Dim ptarr() As Double 'ptarr = New Double() Dim k As Int16 : k = 0 Dim i As On3dPoint Dim j As On3dPoint For Each i In pointset Dim vvv As vCell vvv = New vCell(i, boundary) For Each j In pointset If i <> j Then vvv.slice(j) End If Next __A = vvv.getVertices() For k = 1 To __A.count - 1 _A.Add(New OnLine(__A.item(k - 1), __A.item(k))) Next _A.Add(New OnLine(__A.item(0), __A.item(__A.count - 1))) Next 'B = drawBoundary(boundary) A = _A ''' ' 'Assign result values... If (A IsNot Nothing) Then If (Grasshopper.Kernel.EH_DataConverter.TreatAsCollection(A)) Then 'iterate over collection and assign items individually Dim __num_A As IEnumerable = DirectCast(A, IEnumerable) For Each __obj_A As System.Object In __num_A __data_out(1).AddVolatileData(__obj_A) Next Else 'assign direct __data_out(1).AddVolatileData(A) End If End If If (B IsNot Nothing) Then If (Grasshopper.Kernel.EH_DataConverter.TreatAsCollection(B)) Then 'iterate over collection and assign items individually Dim __num_B As IEnumerable = DirectCast(B, IEnumerable) For Each __obj_B As System.Object In __num_B __data_out(2).AddVolatileData(__obj_B) Next Else 'assign direct __data_out(2).AddVolatileData(B) End If End If If (C IsNot Nothing) Then If (Grasshopper.Kernel.EH_DataConverter.TreatAsCollection(C)) Then 'iterate over collection and assign items individually Dim __num_C As IEnumerable = DirectCast(C, IEnumerable) For Each __obj_C As System.Object In __num_C __data_out(3).AddVolatileData(__obj_C) Next Else 'assign direct __data_out(3).AddVolatileData(C) End If End If If (D IsNot Nothing) Then If (Grasshopper.Kernel.EH_DataConverter.TreatAsCollection(D)) Then 'iterate over collection and assign items individually Dim __num_D As IEnumerable = DirectCast(D, IEnumerable) For Each __obj_D As System.Object In __num_D __data_out(4).AddVolatileData(__obj_D) Next Else 'assign direct __data_out(4).AddVolatileData(D) End If End If Catch ex As Exception __err.Add(String.Format("Script exception: {0}", ex.Message)) Finally 'Add error messages... If (Me.__err IsNot Nothing) Then For Each msg As String in Me.__err __data_out(0).AddVolatileData(msg) Next End If 'Add output messages... If (Me.__out IsNot Nothing) Then For Each msg As String in Me.__out __data_out(0).AddVolatileData(msg) Next End If End Try End Function ' 'GENERAL USE FUNCTIONS' 'Draws the boundary rectangle Function drawBoundary(ByVal pts As List (Of on3dPoint)) As List(Of onLine) Dim l As New List (Of onLine) Dim p As int16 : p = 0 For p = 1 To pts.count - 1 Step 1 l.Add(New OnLine(pts.item(p), pts.item(p - 1))) Next l.add(New OnLine(pts.item(0), pts.item(pts.count - 1))) Return l End Function 'VORONOI VERTEX CLASS' Public Class vVertex Public loc As On3dPoint Public isNew As Boolean Public deleteme As Boolean Public Sub New(ByVal _loc As on3dPoint) loc = _loc isNew = True deleteme = False End Sub Public Sub New(ByVal _loc As on3dPoint, ByVal _isNew As Boolean) loc = _loc isNew = _isNew deleteme = False End Sub Public Function distance(ByVal pt As on3dPoint) As Double 'modifiy to return just the square of the distance, makes it faster Return loc.DistanceTo(pt) End Function End Class 'VORONOI CELL CLASS' Public Class vCell Public vertices As New List (Of vVertex) Public origin As On3dPoint 'constructor; Public Sub New(ByVal _origin As on3dPoint) origin = _origin End Sub Public Sub New(ByVal _origin As on3dPoint, ByVal _b As List (Of on3dPoint)) origin = _origin Dim pt As On3dPoint Dim vert As vVertex For Each pt In _b vert = New vVertex(pt, False) vertices.Add(vert) Next End Sub 'calculates teh perpendicular bisector between two points Public Function bisectorAt(ByVal a As on3dPoint, ByVal b As on3dPoint) As OnLine Dim le As On3dPoint, ls As on3dPoint ls = (a + b) / 2 le = New On3dPoint(0, 0, 0) le.x = ls.x - (b.y - a.y) le.y = ls.y + (b.x - a.x) Return (New OnLine(ls, le)) End Function 'Intersects two lines 9th grade math style Public Function intersectL(ByVal l1 As OnLine, ByVal l2 As OnLine) As Double Dim p As Double : p = 0 Dim denom, enom As Double Dim x1, x2, x3, x4 As Double Dim y1, y2, y3, y4 As Double x1 = l1.from.x x2 = l1.To.x x3 = l2.from.x x4 = l2.To.x y1 = l1.from.y y2 = l1.To.y y3 = l2.from.y y4 = l2.To.y denom = (y4 - y3) * (x2 - x1) - (y2 - y1) * (x4 - x3) If(denom = 0) Then Return -1 End If enom = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3) p = enom / denom If((p < 0) Or (p >= 1)) Then Return -1 End If Return p End Function Public Sub addVertex(ByVal vertex As vVertex, ByVal index As int16) vertices.Insert(index, vertex) End Sub Public Sub purgeVList() Dim _vertices As New List (Of vVertex) Dim _v As vVertex For Each _v In vertices If(_v.deleteme = False) Then _vertices.add(_v) End If Next vertices = _vertices End Sub Public Sub slice(ByVal pttt As On3dPoint) Dim bis As OnLine bis = bisectorAt(origin, pttt) Dim i As int16, j As Int16, n As Int16 Dim p As Double i = -1 n = 0 Do i = i + 1 j = (i + 1) Mod vertices.Count p = intersectL(New OnLine(vertices.item(i).loc, vertices.Item(j).loc), bis) If p >= 0 Then n = n + 1 If p = 0 Then vertices.Item(i).isNew = True Else Dim nvert As vVertex Dim newpt As New On3dPoint(0, 0, 0) newpt.x = vertices.item(i).loc.x + p * (vertices.item(j).loc.x - vertices.item(i).loc.x) newpt.y = vertices.item(i).loc.y + p * (vertices.item(j).loc.y - vertices.item(i).loc.y) nvert = New vVertex(newpt, True) vertices.Insert(i + 1, nvert) i = i + 1 End If If n = 2 Then Exit Do End If End If Loop Until i >= vertices.Count - 1 Dim k As Int16 For k = 0 To vertices.Count - 1 If(vertices.Item(k).distance(origin) < vertices.Item(k).distance(pttt)) Then i = k Exit For End If Next Dim signal As Boolean signal = False For k = 0 To vertices.Count - 1 i = (i + 1) Mod vertices.Count If(vertices.Item(i).isnew = True) Then vertices.Item(i).isnew = False signal = Not signal Else If (signal = True) Then vertices.Item(i).deleteme = True End If End If Next purgeVList() End Sub Public Function getVertices() As List (Of On3dPoint) Dim ret As New List (Of on3dPoint) Dim vertex As vVertex For Each vertex In vertices ret.Add(vertex.loc) Next Return ret End Function End Class ' End Class