download studio_utility_1_0.py
studio_utility_1_0.py ユーティリティモジュールを更新しました。いくつか機能追加です。クラスタリングの関数がありますが、これはユークリッド距離によるものなので、スタジオで必要な”ネットワーク距離でのクラスタリング”はもう一ひねり必要です。それらは後ほど。
クラスタリングは k-means法(クラスタ数指定)、k-means ++ 法(クラスタ数指定)、ward法(ステップ指定)、ward法(クラスタ数指定) となっています。それぞれググればアルゴリズムは調べられると思います。
Rhino-Pythonでのサンプルコード GH-Pythonでのサンプルはこちら
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import random
import studio_utility_1_0 as su
class Unit:
def __init__(self,pos):
self.pos = pos
self.group = 0
self.w = 0
units = []
pts = []
random.seed(1)
for i in range(100):
x = random.randint(0,100)
y = random.randint(0,100)
pos = rg.Point3d(x,y,0)
u = Unit(pos)
units.append(u)
pt = rs.AddPoint(pos)
pts.append(pt)
wps = []
#wps = su.k_means(units,5)
#wps = su.k_meansPP(units,10)
#wps = su.wardMethodStep(units,15)
wps = su.wardMethodCNum(units,10)
#ptss = su.k_meansRGP(pts,5)
#ptss = su.k_meansPPRGP(pts,5)
#ptss = su.wardMethodStepRGP(pts,25)
#ptss = su.wardMethodCNumRGP(pts,5)
#for pts in ptss:
# if len(ptss) > 1:
# rs.AddPolyline(pts)
#draw result
for u in units:
for i in range(len(wps)):
if u.group == i:
if u.pos != wps[i][0]:
rs.AddLine(u.pos,wps[i][0])
for wp in wps:
rs.AddCircle(wp[0],5)
以下実装関数:
SetBoundary(site_surf)
"""
site_surf: rhino surface object for boundary
"""
checkBrep(brep,breps):
"""
brep: rhino brep object to check
if it is in boundary if it not collide with other
breps specified breps
breps: hino brep objects to check
return:
True: if it is in boundary, and not collide with others
False: if it is not in boundary, or is colliedes with others
"""
checkPoint(p,pts,buffer = 0):
"""
p: rhino.Geometry point to check
if it is in boundary if it not closer with others than
specified buffer
pts: rhino.Geometry point list to check
buffer: buffer distance
return:
True: if it is in boundary, and not closer than buffer with others
False: if it is not in boundary, or is too close with others
"""
checkPoint(p,pts,buffer = 0):
"""
p: rhino.Geometry point to check
if it is in boundary if it not closer with others than
specified buffer
pts: rhino.Geometry point list to check
buffer: buffer distance
return:
True: if it is in boundary, and not closer than buffer with others
False: if it is not in boundary, or is too close with others
"""
w_rand_choice(units):
"""
units: list of instance
1 instance should have member
.w
which express weight
return:
1 insutance from list "units"
selected randomly weighted possibility of ".w"
"""
k_means(units, clus):
"""
units: list of instance
1 instance should have 2 member
.group (integer)
.pos (Rhino.Geometry.Point3d)
clus: expected cluster number (integer)
return:
list of [rg.Point3d, num]
[0]center point of cluster
[1]cluster group id
"""
k_meansRGP(pts,clus):
"""
clustering by k-means
pts: list of Rhino.Geometry.Point3d
clus: expected cluster number (integer)
return:
list of clusters [list of Point3d]
"""
k_meansPP(units, clus):
"""
units: list of instance
1 instance should have 3 member
.group (integer)
.pos (Rhino.Geometry.Point3d)
.w (double)
clus: expected cluster number (integer)
return:
list of [rg.Point3d, num]
[0]center point of cluster
[1]cluster group id
"""
k_meansPPRGP(pts,clus):
"""
clustering by k-means++
pts: list of Rhino.Geometry.Point3d
clus: expected cluster number (integer)
return:
list of clusters [list of Point3d]
"""
wardMethodStep(units,steps):
"""
units: list of instance
1 instance should have 2 member
.group (integer)
.pos (Rhino.Geometry.Point3d)
steps: clustering steps (integer)
return:
list of [rg.Point3d, num]
[0]center point of cluster
[1]cluster group id
"""
wardMethodStepRGP(pts,steps):
"""
clustering by ward method
pts: list of Rhino.Geometry.Point3d
steps: number of steps (integer)
return:
list of clusters [list of Point3d]
"""
wardMethodCNum(units,clus):
"""
units: list of instance
1 instance should have 2 member
.group (integer)
.pos (Rhino.Geometry.Point3d)
clus: cluster number (integer)
return:
list of [rg.Point3d, num]
[0]center point of cluster
[1]cluster group id
"""
wardMethodCNumRGP(pts,clus):
"""
clustering by ward method
pts: list of Rhino.Geometry.Point3d
clus: expected cluster number (integer)
return:
list of clusters [list of Point3d]
"""

Comments are closed.