Rhino3d Image Reader Script

Screen shot 2009-09-09 at 8.13.54 PM

Image reader tool for rhino3d from David Mans @ neoarchaic

  1. Option Explicit
  2. 'Script written by <David Mans>
  3. 'adapted from work by Che Wei Wang
  4. 'www.cwwang.com
  5. 'Script copyrighted by <NeoArchaic Studio>
  6. 'Script version Tuesday, March 18, 2008 7:40:18 AM
  7. Call Main()
  8. Sub Main()
  9. Dim rows, cols, tol, height,unit
  10. Dim arrItems, arrValues, arrResults
  11. arrItems = array("columns","rows","tolerance","maximum_height","unit_width")
  12. arrValues = array(10,10,0,10,10)
  13. arrResults = Rhino.PropertyListBox (arrItems, arrValues ,,"Image Parameters")
  14. cols = CDbl(arrResults(0))
  15. rows = CDbl(arrResults(1))
  16. If CDbl(arrResults(2)) > 1 Then
  17. tol = 1
  18. Else
  19. tol = CDbl(arrResults(2))
  20. End If
  21. height = CDbl(arrResults(3))
  22. unit = CDbl(arrResults(4))
  23. Dim arrImg, arrExist, strInput
  24. arrImg = arrImageSample(cols,rows)
  25. ' image outputs (0)red(1)green(2)blue(3)alpha(4)hue(5)saturation(6)luminance
  26. strInput = Rhino.GetString("Select Image Reading Method","Normalized_Pannel",array("Banding","Segments","Cylinders","Horizontal_Plates","Faceted","Normalized_Pannel","Surface","PointCloud"))
  27. If isNull(strInput) Then Exit Sub
  28. Call Rhino.EnableRedraw(False)
  29. If CStr(strInput) = "Banding" Then
  30. arrExist = banding(arrImg(6),tol,height,unit)
  31. End If
  32. If CStr(strInput) = "Segments" Then
  33. arrExist = segments(arrImg(6),tol,height,unit)
  34. End If
  35. If CStr(strInput) = "Cylinders" Then
  36. arrExist = cylinders(arrImg(6),tol,height,unit)
  37. End If
  38. If CStr(strInput) = "Horizontal_Plates" Then
  39. arrExist = plates(arrImg(6),tol,height,unit)
  40. End If
  41. If CStr(strInput) = "Faceted" Then
  42. arrExist = loftPannels(arrImg(6),tol,height,unit)
  43. End If
  44. If CStr(strInput) = "Normalized_Pannel" Then
  45. arrExist = uniformPannels(arrImg(6),tol,height,unit)
  46. End If
  47. If CStr(strInput) = "Surface" Then
  48. arrExist = surface(arrImg(6),height,unit)
  49. End If
  50. If CStr(strInput) = "PointCloud" Then
  51. arrExist = cloud(arrImg(0),arrImg(1),arrImg(2),arrImg(6),height,unit)
  52. End If
  53. Call Rhino.EnableRedraw(True)
  54. End Sub
  55. Function arrImageSample(cols, rows)
  56. arrImageSample = Null
  57. 'Instantiate the RhPicture Object
  58. Dim RhPicture : Set RhPicture = Rhino.GetPlugInObject("RhPicture")
  59. If IsNull(RhPicture) Then Exit Function
  60. 'Load an arbitrary image
  61. If Not RhPicture.LoadImage() Then
  62. Call Rhino.Print("Image not loaded")
  63. Exit Function
  64. End If
  65. 'Get the width and height
  66. Dim w : w = RhPicture.Width()
  67. Dim h : h = RhPicture.Height()
  68. If IsNull(w) Or IsNull(h) Then
  69. Call Rhino.Print("No valid image data")
  70. Exit Function
  71. End If
  72. Dim x, y, i,j
  73. Dim r, g, b, a, hu, s, u
  74. ReDim r(rows), g(rows), b(rows), a(rows), hu(rows), s(rows), u(rows)
  75. Dim rValSet, gValSet, bValSet, aValSet, hValSet, sValSet, uValSet
  76. ReDim rValSet(cols), gValSet(cols), bValSet(cols), aValSet(cols), hValSet(cols), sValSet(cols), uValSet(cols)
  77. 'Sample Image returning all values between zero and one
  78. For i = 0 To cols Step 1
  79. For j = 0 To rows Step 1
  80. x = int(w/cols)*i
  81. y = int(h/rows)*j
  82. If x>w Then
  83. x = w
  84. End If
  85. If y>h Then
  86. y = h
  87. End If
  88. r(j) = RhPicture.Red(x,y)/255
  89. g(j) = RhPicture.Green(x,y)/255
  90. b(j) = RhPicture.Blue(x,y)/255
  91. a(j) = RhPicture.Alpha(x,y)/255
  92. hu(j) = RhPicture.Hue(x,y)/360
  93. s(j) = RhPicture.Saturation(x,y)
  94. u(j) = RhPicture.Luminance(x,y)
  95. Next
  96. rValSet(i) = r
  97. gValSet(i) = g
  98. bValSet(i) = b
  99. aValSet(i) = a
  100. hValSet(i) = hu
  101. sValSet(i) = s
  102. uValSet(i) = u
  103. Next
  104. Set RhPicture = Nothing
  105. ' image outputs (0)red(1)green(2)blue(3)alpha(4)hue(5)saturation(6)luminance
  106. arrImageSample = array(rValSet,gValSet,bValSet,aValSet,hValSet,sValSet,uValSet)
  107. End Function
  108. Function plates(arrInput,min,max,spacing)
  109. plates = Null
  110. Dim i,j,r,cols,rows
  111. cols = uBound(arrInput)
  112. rows = uBound(arrInput(0))
  113. Dim mvPlane,plate()
  114. r = 0
  115. ReDim plate(r)
  116. For i = 0 To cols-1 Step 1
  117. For j = 0 To rows Step 1
  118. If arrInput(i)(j) > min Then
  119. mvPlane = Rhino.MovePlane(Rhino.WorldXYPlane(),array(spacing*i,spacing*j,max*arrInput(i)(j)))
  120. ReDim Preserve plate(r)
  121. plate(r) = Rhino.AddPlaneSurface(mvPlane, spacing, spacing)
  122. r =r+1
  123. End If
  124. Next
  125. Next
  126. plates = plate
  127. End Function
  128. Function cylinders(arrInput,min,max,spacing)
  129. cylinders = Null
  130. Dim i,j,r,cols,rows
  131. cols = uBound(arrInput)
  132. rows = uBound(arrInput(0))
  133. Dim mvPlane,plate()
  134. r = 0
  135. ReDim plate(r)
  136. For i = 0 To cols-1 Step 1
  137. For j = 0 To rows Step 1
  138. If arrInput(i)(j) > min Then
  139. mvPlane = Rhino.MovePlane(Rhino.WorldXYPlane(),array(spacing*i,spacing*j,0))
  140. ReDim Preserve plate(r)
  141. plate(r) = Rhino.AddCylinder(mvPlane(0), array(mvPlane(0)(0),mvPlane(0)(1),mvPlane(0)(2)+max*arrInput(i)(j)), spacing*.5)
  142. r =r+1
  143. End If
  144. Next
  145. Next
  146. cylinders = plate
  147. End Function
  148. Function banding(arrInput,min,max,spacing)
  149. banding = Null
  150. Dim i,j,r,cols,rows
  151. cols = uBound(arrInput)
  152. rows = uBound(arrInput(0))
  153. Dim mvPlane, pSet(),band()
  154. ReDim band(cols)
  155. For i = 0 To cols-1 Step 1
  156. r = 0
  157. ReDim pSet(r)
  158. For j = 0 To rows Step 1
  159. If arrInput(i)(j) > min Then
  160. mvPlane = Rhino.MovePlane(Rhino.WorldXYPlane(),array(spacing*i,spacing*j,max*arrInput(i)(j)))
  161. ReDim Preserve pSet(r)
  162. pSet(r) = mvPlane(0)
  163. r =r+1
  164. End If
  165. Next
  166. band(i) = Rhino.AddInterpCurve(pSet)
  167. Next
  168. banding = band
  169. End Function
  170. Function surface(arrInput,max,spacing)
  171. surface = Null
  172. Dim i,j,r,cols,rows
  173. cols = uBound(arrInput)
  174. rows = uBound(arrInput(0))
  175. Dim mvPlane, pSet()
  176. r = 0
  177. ReDim pSet(r)
  178. For i = 0 To cols-1 Step 1
  179. For j = 0 To rows-1 Step 1
  180. mvPlane = Rhino.MovePlane(Rhino.WorldXYPlane(),array(spacing*i,spacing*j,max*arrInput(i)(j)))
  181. ReDim Preserve pSet(r)
  182. pSet(r) = mvPlane(0)
  183. r =r+1
  184. Next
  185. Next
  186. Call Rhino.AddSrfPtGrid(array(cols,rows),pSet,array(3,3))
  187. surface = array()
  188. End Function
  189. Function segments(arrInput,min,max,spacing)
  190. segments = Null
  191. Dim i,j,k,r,s,cols,rows
  192. cols = uBound(arrInput)
  193. rows = uBound(arrInput(0))
  194. Dim mvPlane, pSet(),band()
  195. Dim trFa(),arrTrFa()
  196. ReDim band(cols),trFa(rows),arrTrFa(cols)
  197. For i = 0 To cols-1 Step 1
  198. r=0
  199. For j = 0 To rows Step 1
  200. If arrInput(i)(j) > min Then
  201. trFa(j) = True
  202. ReDim pSet(r)
  203. pSet(r) = array(i*spacing,j*spacing,max*arrInput(i)(j))
  204. r=r+1
  205. Else
  206. trFa(j) = False
  207. End If
  208. Next
  209. arrTrFa(i) = trFa
  210. Next
  211. Dim ptGroup(),ptSet(),arrPts(),crvVal(),crvBln
  212. ReDim arrPts(cols),crvVal(cols)
  213. For i = 0 To cols-1 Step 1
  214. r = 0
  215. s = 0
  216. If arrTrFa(i)(0) = True And arrTrFa(i)(1) = True Then
  217. ReDim Preserve ptGroup(r)
  218. ptGroup(r) = array(i*spacing,0*spacing,max*arrInput(i)(0))
  219. r=r+1
  220. End If
  221. For j = 1 To rows-1 Step 1
  222. If arrTrFa(i)(j) = True And arrTrFa(i)(j+1) = True And arrTrFa(i)(j-1) = False Then
  223. ReDim Preserve ptGroup(r)
  224. ptGroup(r) = array(i*spacing,j*spacing,max*arrInput(i)(j))
  225. r=r+1
  226. End If
  227. If arrTrFa(i)(j) = True And arrTrFa(i)(j-1) = True Then
  228. ReDim Preserve ptGroup(r)
  229. ptGroup(r) = array(i*spacing,j*spacing,max*arrInput(i)(j))
  230. r = r+1
  231. End If
  232. If arrTrFa(i)(j) = True And arrTrFa(i)(j+1) = False Then
  233. r = 0
  234. ReDim Preserve ptSet(s)
  235. ptSet(s) = ptGroup
  236. s = s+1
  237. End If
  238. If s = 0 Then
  239. crvBln = False
  240. Else
  241. crvBln = True
  242. End If
  243. Next
  244. arrPts(i) = ptSet
  245. crvVal(i) = crvBln
  246. Next
  247. Dim cntA,bandSet()
  248. s=0
  249. For i = 0 To cols-1 Step 1
  250. r=0
  251. If crvVal(i) = True Then
  252. cntA = uBound(arrPts(i))
  253. For j = 0 To cntA Step 1
  254. ReDim band(r)
  255. band(r) = Rhino.AddCurve(arrPts(i)(j))
  256. r = r+1
  257. Next
  258. ReDim bandSet(s)
  259. bandSet(s) = band
  260. s = s+1
  261. End If
  262. Next
  263. segments = bandSet
  264. End Function
  265. Function uniformPannels(arrInput,min,max,spacing)
  266. uniformPannels = Null
  267. Dim i,j,k,r,s,cols,rows
  268. cols = uBound(arrInput)
  269. rows = uBound(arrInput(0))
  270. Dim pSet()
  271. Dim trFa(),arrTrFa()
  272. ReDim band(cols),trFa(rows),arrTrFa(cols)
  273. For i = 0 To cols-1 Step 1
  274. r=0
  275. For j = 0 To rows Step 1
  276. If arrInput(i)(j) > min Then
  277. trFa(j) = True
  278. ReDim pSet(r)
  279. pSet(r) = array(i*spacing,j*spacing,max*arrInput(i)(j))
  280. r=r+1
  281. Else
  282. trFa(j) = False
  283. End If
  284. Next
  285. arrTrFa(i) = trFa
  286. Next
  287. Dim tempSrf,srfPlane()
  288. tempSrf = Rhino.AddSrfPt(array(array(-spacing*.5,-spacing*.5,0),array(-spacing*.5,spacing*.5,0),array(spacing*.5,spacing*.5,0),array(spacing*.5,-spacing*.5,0)))
  289. r = 0
  290. For i = 1 To cols-1 Step 1
  291. For j = 1 To rows-1 Step 1
  292. If arrTrFa(i-1)(j) = True And arrTrFa(i)(j) = True And arrTrFa(i)(j-1) = True Then
  293. ReDim Preserve srfPlane(r)
  294. srfPlane(r)= Rhino.OrientObject (tempSrf, array(array(0,0,0),array(1,0,0),array(0,1,0)), array(array(i*spacing,j*spacing,max*arrInput(i)(j)), array((i-1)*spacing,j*spacing,max*arrInput(i-1)(j)), array(i*spacing,(j-1)*spacing,max*arrInput(i)(j-1))), 1)
  295. r = r+1
  296. End If
  297. Next
  298. Next
  299. Call Rhino.DeleteObject(tempSrf)
  300. uniformPannels = srfPlane
  301. End Function
  302. Function loftPannels(arrInput,min,max,spacing)
  303. loftPannels = Null
  304. Dim i,j,k,r,s,cols,rows
  305. cols = uBound(arrInput)
  306. rows = uBound(arrInput(0))
  307. Dim pSet()
  308. Dim trFa(),arrTrFa()
  309. ReDim band(cols),trFa(rows),arrTrFa(cols)
  310. For i = 0 To cols-1 Step 1
  311. r=0
  312. For j = 0 To rows Step 1
  313. If arrInput(i)(j) > min Then
  314. trFa(j) = True
  315. ReDim pSet(r)
  316. pSet(r) = array(i*spacing,j*spacing,max*arrInput(i)(j))
  317. r=r+1
  318. Else
  319. trFa(j) = False
  320. End If
  321. Next
  322. arrTrFa(i) = trFa
  323. Next
  324. Dim srfOutput()
  325. r = 0
  326. For i = 1 To cols-1 Step 1
  327. For j = 1 To rows-1 Step 1
  328. If arrTrFa(i-1)(j) = True And arrTrFa(i-1)(j-1) = True And arrTrFa(i)(j) = True And arrTrFa(i)(j-1) = True Then
  329. ReDim Preserve srfOutput(r)
  330. srfOutput(r) = Rhino.AddSrfPt(array(array((i-1)*spacing,j*spacing,max*arrInput(i-1)(j)),array((i-1)*spacing,(j-1)*spacing,max*arrInput(i-1)(j-1)),array(i*spacing,(j-1)*spacing,max*arrInput(i)(j-1)),array(i*spacing,j*spacing,max*arrInput(i)(j))))
  331. r=r+1
  332. End If
  333. Next
  334. Next
  335. loftPannels = srfOutput
  336. End Function
  337. Function cloud(arrInputX,arrInputY,arrInputZ,arrInputR,spacing,rad)
  338. cloud = Null
  339. Dim i,j,r,cols,rows
  340. cols = uBound(arrInputX)
  341. rows = uBound(arrInputX(0))
  342. Dim mvPlane, arrbln, pSet()
  343. arrbln = Rhino.GetBoolean("Type of Data Representation",array("Representation","points","spheres"),array(False))
  344. r = 0
  345. For i = 0 To cols-1 Step 1
  346. For j = 0 To rows-1 Step 1
  347. ReDim Preserve pSet(r)
  348. pSet(r)    = array(arrInputX(i)(j)*spacing,arrInputY(i)(j)*spacing,arrInputZ(i)(j)*spacing)
  349. If arrbln(0) = True Then
  350. Call Rhino.addsphere(pSet(r),arrInputR(i)(j)*rad)
  351. Else
  352. Call Rhino.AddPoint(pSet(r))
  353. End If
  354. r =r+1
  355. Next
  356. Next
  357. cloud = pSet
  358. End Function

Screen shot 2009-09-09 at 8.42.13 PM

Screen shot 2009-09-09 at 8.13.43 PM

Leave a comment

You must be logged in to post a comment.