11/11/2015

Surface Algorithm with Python scripts


When we generate the curves from Processing, the curves are defined with a random order. So, here is a code for connecting those curves in order. The code can generate contours and random connections between two adjacent lines. The contours can be used to generate pipes, surfaces or meshes.


Webby Dome


Generated contours
Piping of contours
Random connections
Generated dome


# ========== CODES DESCRIPTION: Virtual Engagement - Surface    ========== #
# ========== CODE AUTHOR      : Tzu-Chieh Kurt Hong             ========== #
# ========== DATE             : 2015/10/12                      ========== #
# ========== VERSION          : 5                               ========== #

import rhinoscriptsyntax as rs
import random as rnd
import math

seg = 100 # Numbers of Agent

input_curves = []
point_calculate = {}

input_curves = rs.GetObjects('select curves', rs.filter.curve);
join_curves = rs.JoinCurves(input_curves, True, 0.00001)

rs.EnableRedraw(False)

for i in range (len(join_curves)):
    
    point = rs.DivideCurve(join_curves[i],1,False,True)
    point_calculate[(i)] = point[1]

# central point

sum_x = 0
sum_y = 0
sum_z = 0

for i in range (len(join_curves)):
    sum_x = sum_x + point_calculate[i][0] 
    sum_y = sum_y + point_calculate[i][1]
    sum_z = sum_z + point_calculate[i][2]

center_x = sum_x / len(join_curves)
center_y = sum_y / len(join_curves)
center_z = sum_z / len(join_curves)

center_point = rs.AddPoint([center_x,center_y,center_z])

lineSet = {}
lineSet_rnd = {}
pointSet1 = {}
pointSet2 = {}

# Sorting
# ======================================================================== #
for i in range (len(join_curves)):

    vec_center = rs.VectorCreate(center_point, point_calculate[(i)])

    # group the points ?
    GroupA = []
    GroupB = []
    
    print vec_center
    
    for j in range (len(join_curves)): 
        if i != j : 
            vec_group = rs.VectorCreate(center_point, point_calculate[(j)])
            # 1st
            if vec_group[1] <= vec_group[0]*(vec_center[1]/vec_center[0]) and (vec_center[1]/vec_center[0])>0 and vec_center[1] > 0:
                GroupA.append(j)
            # 2nd
            elif vec_group[1] >= vec_group[0]*(vec_center[1]/vec_center[0]) and (vec_center[1]/vec_center[0])<0 and vec_center[1] > 0:
                GroupA.append(j)
            # 3rd
            elif vec_group[1] >= vec_group[0]*(vec_center[1]/vec_center[0]) and (vec_center[1]/vec_center[0])>0 and vec_center[1] < 0:
                GroupA.append(j)
            # 4th
            elif vec_group[1] <= vec_group[0]*(vec_center[1]/vec_center[0]) and (vec_center[1]/vec_center[0])<0 and vec_center[1] < 0:
                GroupA.append(j)
            else:
                GroupB.append(j)
    surface_group = []
    dtemp = 10
    for k in range (len(GroupA)):
        d = rs.Distance(point_calculate[(i)] , point_calculate[(GroupA[k])])
        if d != 0 and d < dtemp:
            dtemp = d
            index_A = GroupA[k]
        else :
            dtemp = dtemp
# ======================================================================== #

# Gnerate Spider web
# ======================================================================== #

    pointSet1[(i)] = rs.DivideCurve(join_curves[index_A],seg,False, True)
    pointSet2[(i)] = rs.DivideCurve(join_curves[i],seg,False, True)
    for m in range (0,seg):
        rndK = rnd.randint(seg-m,seg)
        rndM = rnd.randint(seg-m,seg)
        if m == 0 :
            lineSet_rnd[(i,m)] = rs.AddLine(pointSet1[i][rndK],pointSet2[i][rndM])
            MidLine_temp = rs.AddLine(pointSet1[i][rndK],pointSet2[i][rndM])
        elif m%2 == 1 :
            lineSet_rnd[(i,m)] = rs.AddLine(pointSet1[i][rndK],pointSet2[i][rndM])
            MidPoint_temp = rs.CurveMidPoint(MidLine_temp)
            MidLine_temp = rs.AddLine(MidPoint_temp, pointSet2[i][rndM])
            PipeSet_rnd = rs.AddPipe(MidLine_temp, 0, 0.2 , 0, 1)
            rs.MoveObject(PipeSet_rnd, (600,0,0))
        elif m%2 == 0 :
            lineSet_rnd[(i,m)] = rs.AddLine(pointSet1[i][rndK],pointSet2[i][rndM])
            MidPoint_temp = rs.CurveMidPoint(MidLine_temp)
            MidLine_temp = rs.AddLine(MidPoint_temp, pointSet1[i][rndK])
            PipeSet_rnd = rs.AddPipe(MidLine_temp, 0, 0.2 , 0, 1)
            rs.MoveObject(PipeSet_rnd, (600,0,0))
        lineSet[(i,m)] = rs.AddLine(pointSet1[i][m],pointSet2[i][m])
line_H = {}
final_point_set = {}
# ======================================================================== #

# ======================================================================== #
# Generate contours
PipeSet_rnd = {}
for n in range (0,seg):
    line_H = []
    final_point_set = []
    for k in range (len(join_curves)+1):
        
        if k < len(join_curves):
            line_H.append(lineSet[(k,n)])
            
        else :
            line_H.append(lineSet[(0,n)])
    line_H_join = rs.JoinCurves(line_H)
    final_point_set = rs.DivideCurve(line_H_join, 30, False, True)
    final_line = rs.AddInterpCurve(final_point_set,3,0)
    final_pt_set = rs.DivideCurve(final_line,1000,False,True)
    rs.MoveObject(line_H_join,(200,0,0))
    rs.MoveObject(final_line,(400,0,0))
    rs.DeleteObjects(line_H)
# ======================================================================== #
rs.DeleteObject(center_point)
rs.MoveObjects(join_curves,(200,0,0))

rs.EnableRedraw(True)




No comments:

Post a Comment