找回密码
 注册
搜索
查看: 97894|回复: 48

[工作记录] 马赛克【附代码】

 火 [复制链接]
发表于 2010-6-22 16:11:24 | 显示全部楼层 |阅读模式
060817MosiacCat01[1].jpg (57.06 KB, 下载次数: 8)
代码未经过完全测试,可能个别情况下会出错。
发现bug请指出,谢谢。
'========================================

Option Explicit
run
SUB run
makeMosiac Rhino.GetObject("请选择物体", 0),Rhino.IntegerBox("最短方向的方块数",6),Rhino.RealBox("方块缩小比例",0.9)
END SUB
'--------------------------------------------
' nDensity:最小boundingBox边长的分段数
' rShrink:方块缩小比例
'--------------------------------------------
FUNCTION makeMosiac(obj,nDensity,rShrink)
DIM objs : objs=array(obj)
DIM pBox, boundingBox : pBox=Rhino.BoundingBox(objs)
DIM nX,nY,nZ  '包围盒三边的分段数
DIM minSide  '最短边代码
DIM lSide(2)  '包围盒三边长
DIM objTemp
DIM lCube   '马赛克边长
DIM iX,iY,iZ  '三个边的截线数
'计算最短边、马赛克边长、三个方向的马赛克数
lSide(0)=pBox(1)(0)-pBox(0)(0)
lSide(1)=pBox(2)(1)-pBox(1)(1)
lSide(2)=pBox(4)(2)-pBox(0)(2)
IF lSide(0)<=lSide(1) AND lSide(0)<=lSide(2) THEN
  minSide=0
  lCube=lSide(minSide)/nDensity
  iX=nDensity
  iY=int(lSide(1)/lCube+0.5) '四舍五入
  iZ=int(lSide(2)/lCube+0.5)
END IF
IF lSide(1)<=lSide(0) AND lSide(1)<=lSide(2) THEN
  minSide=1
  lCube=lSide(minSide)/nDensity
  iX=int(lSide(0)/lCube+0.5)
  iY=nDensity
  iZ=int(lSide(2)/lCube+0.5)
END IF
IF lSide(2)<=lSide(0) AND lSide(2)<=lSide(1) THEN
  minSide=2
  lCube=lSide(minSide)/nDensity
  iX=int(lSide(0)/lCube+0.5)
  iY=int(lSide(1)/lCube+0.5)
  iZ=nDensity
END IF
'定义马赛克空间, 并初始化为零(该空间没有马赛克)
DIM i,j,k,m,s
DIM p() : REDIM p((iX+1)*(iY+1)*(iZ+1))
FOR i=0 TO iX
  FOR j=0 TO iY
   FOR k=0 TO iZ
    p(i*(iY+1)*(iZ+1)+j*(iZ+1)+k)=0
   NEXT
  NEXT
NEXT
DIM iRoundUp '圆整后的坐标序号
DIM pCube(7)
'拆分物体
DIM nObjs : nObjs=UBound(objs)  '选择要马赛克的物体的数目
DIM nSfs  : nSfs=0 '打散后的NURBS曲面总数
DIM sfs()  : REDIM sfs(nSfs) '打散后的NURBS曲面
DIM sfsInEach,sfsInEachPlane,pVertice,pMeshFaces
DIM arrVerticeNum,nMeshFace '顶点序号;Mesh内面片数 '
FOR i=0 TO nObjs '对每一个所选的物体
  IF Rhino.IsSurface(objs(i))=vbTRUE AND Rhino.IsSurfaceTrimmed(objs(i))=vbFALSE THEN  '是单曲面,且不是裁剪曲面
   sfs(UBound(sfs))=objs(i)
   REDIM PRESERVE sfs(UBound(sfs)+1)
  ELSE  '是复合曲面或裁剪曲面
   Rhino.SelectObject objs(i)   
   Rhino.Command "-Mesh PolygonDensity 12  Enter Enter "  '数字12表示离散后的mesh精度,越大越精细,计算量也越大
   objTemp=Rhino.FirstObject(vbTRUE)   'Mesh面
   pMeshFaces=Rhino.MeshFaces(objTemp, vbTRUE)  'Mesh内所有顶点的集合
   arrVerticeNum=Rhino.MeshFaceVertices(objTemp)  'Mesh内各面角点序号集合
   nMeshFace=UBound(arrVerticeNum)  '面片总数
   Rhino.DeleteObject objTemp  '删除Mesh
   FOR j=0 TO nMeshFace '为每一个Mesh面片建立曲面
    objTemp=Rhino.AddSrfPt(Array(pMeshFaces(j*4+0),pMeshFaces(j*4+1),pMeshFaces(j*4+2),pMeshFaces(j*4+3)))
    sfs(UBound(sfs))=objTemp
    REDIM PRESERVE sfs(UBound(sfs)+1)
   NEXT
  END IF
NEXT
Rhino.UnSelectAllObjects
REDIM PRESERVE sfs(UBound(sfs)-1)
nSfs=UBound(sfs)
DIM pNorm,pStart,pEnd,sectLine,pSect,nSectPoints
'第一轮:平行Z轴
dim pTemp() :redim pTemp(0)
FOR i=0 TO iX
  FOR j=0 TO iY
   pStart=Array(pBox(0)(0)+i*lCube, pBox(0)(1)+j*lCube, pBox(0)(2))
   pEnd=Array(pBox(0)(0)+i*lCube, pBox(0)(1)+j*lCube, pBox(4)(2))
   sectLine=Rhino.AddLine(pStart,pEnd)
   FOR m=0 TO nSfs
    pSect=Rhino.CurveSurfaceIntersection(sectLine, sfs(m))
    IF IsArray(pSect)=vbTRUE THEN  '有交叉点,(此句费解,去掉IF后pSect就不是数组了)
     nSectPoints=UBound(pSect,1) '交叉点个数
     FOR k=0 TO nSectPoints
      iRoundUp=int((pSect(k,1)(2)-pStart(2))/lCube+0.5) '把交叉点位置圆整到马赛克中心
      p(i*(iY+1)*(iZ+1)+j*(iZ+1)+iRoundUp)=1 '该位置的马赛克状态为1(有马赛克)
      pTemp(ubound(pTemp))=Rhino.AddPoint(pSect(k,1))
      redim preserve pTemp(ubound(pTemp)+1)
     NEXT
    END IF
   NEXT
   Rhino.DeleteObject sectLine
  NEXT
NEXT
'第二轮:平行X轴
FOR k=0 TO iZ
  FOR j=0 TO iY
   pStart=Array(pBox(0)(0), pBox(0)(1)+j*lCube, pBox(0)(2)+k*lCube)
   pEnd=Array(pBox(2)(0), pBox(0)(1)+j*lCube, pBox(0)(2)+k*lCube)
   sectLine=Rhino.AddLine(pStart,pEnd)
   FOR m=0 TO nSfs
    pSect=Rhino.CurveSurfaceIntersection(sectLine, sfs(m))
    IF IsArray(pSect) THEN '有交叉点
     nSectPoints=UBound(pSect,1) '交叉点个数
     FOR i=0 TO nSectPoints
      iRoundUp=int((pSect(i,1)(0)-pStart(0))/lCube+0.5)
      p(iRoundUp*(iY+1)*(iZ+1)+j*(iZ+1)+k)=1
      pTemp(ubound(pTemp))=Rhino.AddPoint(pSect(i,1))
      redim preserve pTemp(ubound(pTemp)+1)
     NEXT
    END IF
   NEXT
   Rhino.DeleteObject sectLine
  NEXT
NEXT
'第三轮:平行Y轴
FOR k=0 TO iZ
  FOR i=0 TO iX
   pStart=Array(pBox(0)(0)+i*lCube, pBox(0)(1), pBox(0)(2)+k*lCube)
   pEnd=Array(pBox(0)(0)+i*lCube, pBox(3)(1), pBox(0)(2)+k*lCube)
   sectLine=Rhino.AddLine(pStart,pEnd)
   FOR m=0 TO nSfs
    pSect=Rhino.CurveSurfaceIntersection(sectLine, sfs(m))
    IF IsArray(pSect) THEN '有交叉点
     nSectPoints=UBound(pSect,1) '交叉点个数
     FOR j=0 TO nSectPoints
      iRoundUp=int((pSect(j,1)(1)-pStart(1))/lCube+0.5)
      p(i*(iY+1)*(iZ+1)+iRoundUp*(iZ+1)+k)=1
      pTemp(ubound(pTemp))=Rhino.AddPoint(pSect(j,1))
      redim preserve pTemp(ubound(pTemp)+1)
     NEXT
    END IF
   NEXT
   Rhino.DeleteObject sectLine
  NEXT
NEXT
redim preserve pTemp(ubound(pTemp)-1)
'根据记录的方块属性画方块
dim lCube2  '缩后的Cube边长
lCube2=rShrink*lCube
FOR i=0 TO iX
  FOR j=0 TO iY
   FOR k=0 TO iZ
    IF p(i*(iY+1)*(iZ+1)+j*(iZ+1)+k)=1  THEN
     pCube(0)=Array(pBox(0)(0)+i*lCube-lCube2/2,pBox(0)(1)+j*lCube-lCube2/2,pBox(0)(2)+k*lCube-lCube2/2)
     pCube(1)=Array(pBox(0)(0)+i*lCube+lCube2/2,pBox(0)(1)+j*lCube-lCube2/2,pBox(0)(2)+k*lCube-lCube2/2)
     pCube(2)=Array(pBox(0)(0)+i*lCube+lCube2/2,pBox(0)(1)+j*lCube+lCube2/2,pBox(0)(2)+k*lCube-lCube2/2)
     pCube(3)=Array(pBox(0)(0)+i*lCube-lCube2/2,pBox(0)(1)+j*lCube+lCube2/2,pBox(0)(2)+k*lCube-lCube2/2)
     pCube(4)=Array(pBox(0)(0)+i*lCube-lCube2/2,pBox(0)(1)+j*lCube-lCube2/2,pBox(0)(2)+k*lCube+lCube2/2)
     pCube(5)=Array(pBox(0)(0)+i*lCube+lCube2/2,pBox(0)(1)+j*lCube-lCube2/2,pBox(0)(2)+k*lCube+lCube2/2)
     pCube(6)=Array(pBox(0)(0)+i*lCube+lCube2/2,pBox(0)(1)+j*lCube+lCube2/2,pBox(0)(2)+k*lCube+lCube2/2)
     pCube(7)=Array(pBox(0)(0)+i*lCube-lCube2/2,pBox(0)(1)+j*lCube+lCube2/2,pBox(0)(2)+k*lCube+lCube2/2)
     Rhino.AddBox pCube
    END IF
   NEXT
  NEXT
NEXT
Rhino.DeleteObjects sfs '删除原始物体
Rhino.DeleteObjects pTemp '删除点
makeMosiac=sfs
END FUNCTION

评分

参与人数 3强度 +7 照度 +79 收起 理由
ememery + 1 + 1 楼主太棒了
skywoolf + 3 + 30 原创内容
tony_tsang + 3 + 48 很有启发

查看全部评分

 楼主| 发表于 2010-6-22 16:13:11 | 显示全部楼层
这是个简化版,是为了方便初学者阅读。
还有个效率更高的版本,代码长度有这个三倍长。
回复

使用道具 举报

发表于 2010-6-22 17:00:39 | 显示全部楼层
唉,我还不能上升到简化的程度,只求能做出来,呵呵
回复

使用道具 举报

发表于 2010-6-22 17:29:06 | 显示全部楼层
简化版和那个版本效果一样??差别在那??
回复

使用道具 举报

 楼主| 发表于 2010-6-22 19:56:15 | 显示全部楼层

功能都一样,不打折扣,只是高效版本的速度要快的多。当然,作为代价,程序复杂了许多。
回复

使用道具 举报

发表于 2010-6-22 21:30:56 | 显示全部楼层
楼主犀利。进来看看。
回复

使用道具 举报

发表于 2010-6-22 21:43:05 | 显示全部楼层
就单纯做一个类似的效果而言 这种算法显然过于冗长
使用Rhino.CurveSurfaceIntersection
这个就很耗时间

为什么不用判定点是否在brep内部那个命令呢?
那个命令就可以判定点是否在brep表面
完全被有必要拆分brep 再一个个面去求
回复

使用道具 举报

 楼主| 发表于 2010-6-22 22:25:01 | 显示全部楼层
本帖最后由 夜神 于 2010-6-22 22:28 编辑
就单纯做一个类似的效果而言 这种算法显然过于冗长
使用Rhino.CurveSurfaceIntersection
这个就很耗时间

为什么不用判定点是否在brep内部那个命令呢?
那个命令就可以判定点是否在brep表面
完全被有必要拆分 ...
panhao1 发表于 2010-6-22 21:43

有这个命令吗?晕!
我在另一个程序里写过这个算法,就是为了避开使用Rhino.CurveSurfaceIntersection
回复

使用道具 举报

发表于 2010-6-23 00:05:07 | 显示全部楼层
只能路过看看。。。。
回复

使用道具 举报

发表于 2010-6-23 00:17:09 | 显示全部楼层
爬进来学习 lz大好人啊
回复

使用道具 举报

发表于 2010-6-24 02:30:38 | 显示全部楼层
本帖最后由 uuuf8 于 2010-6-24 02:36 编辑

确实没必要拆分

ps:虽然做的东西类似 但是彼此的逻辑还是有很大不同啊  这点很有意思
回复

使用道具 举报

发表于 2010-6-24 17:57:15 | 显示全部楼层
好有趣,想学
回复

使用道具 举报

发表于 2010-6-25 12:40:59 | 显示全部楼层
嗯 学习了
回复

使用道具 举报

发表于 2010-6-26 12:42:12 | 显示全部楼层
感谢楼主分享啊。。。。。。。。
回复

使用道具 举报

发表于 2010-6-29 19:05:47 | 显示全部楼层
看着就好复杂
回复

使用道具 举报

发表于 2010-10-23 17:21:54 | 显示全部楼层
感谢楼主分享啊
回复

使用道具 举报

发表于 2010-10-26 10:12:45 | 显示全部楼层
谢谢楼主分享  好东西大家学习
回复

使用道具 举报

发表于 2010-11-17 15:03:33 | 显示全部楼层
还是不错的说!
回复

使用道具 举报

发表于 2010-11-17 17:00:30 | 显示全部楼层
啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷
甚好甚好甚好甚好甚好甚好甚好甚好甚好甚好甚好甚好甚好
这货是什么?! lz 我爱你!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
回复

使用道具 举报

发表于 2010-11-17 17:02:05 | 显示全部楼层
强烈要求斑竹再给他加n分!
喵了个咪 真是赞哉!  哦喵~
回复

使用道具 举报

NCFZ微信公众号

手机版|小黑屋|NCF参数化建筑论坛 ( 浙ICP备2020044100号-2 ) 辽公网安备21021102000973号

GMT+8, 2026-11-23 17:30 , Processed in 0.060257 second(s), 29 queries , Gzip On.

Powered by Discuz! X5.0 Licensed

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表