home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / OTTSPAR1.DMS / in.adf / LWMmacros / Fragment.lwm next >
Encoding:
Text File  |  1994-05-22  |  9.8 KB  |  362 lines

  1. /* Breakup your model into many models. Very cool to use with SPARKS       */
  2. /* Enter x y z slice and the volume select will break apart and save each  */
  3. /* Then use SPARKS to bulk load and reposition the models for the start    */
  4. /* Save source will center the fragments & write a source to position them */
  5. /* No save source needs v2.056 or better to write a move pivot into scene  */
  6. /* this is an alternate method but objects dont bounce well because of the */
  7. /* offset pivot point fools the groundplane detection routine              */
  8. /* © march 25 1994 Jon Tindall. This macro is not public domain.           */
  9. /* many thanks for not passing it around                                   */
  10.  
  11.  
  12. NUMERIC DIGITS 6
  13. libadd = addlib("LWModelerARexx.port",0)
  14. signal on error
  15. signal on syntax
  16.  
  17. RexxMathLib = "rexxmathlib.library"
  18. IF POS(RexxMathLib , SHOW('Libraries')) = 0 THEN
  19.   IF ~ADDLIB(RexxMathLib , 0 , -30 , 0) THEN DO
  20.     call notify(1,"!Can't find rexxmathlib.library")
  21.     exit
  22.     end
  23.  
  24. version = 'v1.2'
  25. sysnam = 'BreakUp' version
  26. filnam = 'ENV:Breakup.state'
  27. fnam = "ram:"
  28. div = "3 3 3"
  29. cnt = 1
  30. home = CURLAYER()
  31. scratch = Word(EMPTYLAYERS(),1)
  32. source = 1
  33. explosioncenter="0 0 0"
  34. pi=3.14159265358
  35. DegreesPerRadian=180/pi
  36. extent = 2
  37. reversex= 1
  38. reversey= 2
  39. reversez= 1
  40. random = 1
  41. primary = 1
  42. inside = 1
  43. voltype = 1
  44.  
  45.  
  46. Do Forever
  47.     call req_begin sysnam
  48.     id_mes = req_addcontrol("",'T',"Break-up 1 model into many models")
  49.     id_div = req_addcontrol("X, Y, Z subdivision", 'V')
  50.     id_explosioncenter = req_addcontrol("Explosion center",'V')
  51.     id_extent = req_addcontrol("Maximum explosion radius",'n')
  52.     id_mes2 = req_addcontrol("",'T',"Reset fragment origins by")
  53.     id_source = req_addcontrol("Centering mode",'CH',"MovePivotPoint  MakeSourceFile")
  54.     id_reversex = req_addcontrol("Fragment ordering",'CH'," forward reverseX ")
  55.     id_reversey = req_addcontrol("",'CH'," forward reverseY ")
  56.     id_reversez = req_addcontrol("",'CH'," forward reverseZ ")
  57.     id_primary = req_addcontrol("Primary breakup axis",'CH',"X Y Z")
  58.     id_random = req_addcontrol("Random ordering",'CH'," no yes")
  59.     id_inside = req_addcontrol("Create inner geometry?",'CH',"no yes")
  60.     id_voltype = req_addcontrol("Volume select type",'CH',"include exclude")
  61.  
  62.     call req_setval id_div, div
  63.     call req_setval id_explosioncenter, explosioncenter
  64.     call req_setval id_source, source
  65.     call req_setval id_extent, extent
  66.     call req_setval id_reversex, reversex
  67.     call req_setval id_reversey, reversey
  68.     call req_setval id_reversez, reversez
  69.     call req_setval id_primary, primary
  70.     call req_setval id_random, random
  71.     call req_setval id_inside, inside
  72.     call req_setval id_voltype, voltype
  73.  
  74. If (~req_post()) then do
  75.     call req_end
  76.     exit
  77. end
  78.  
  79.     div = req_getval(id_div)
  80.     explosioncenter = req_getval(id_explosioncenter)
  81.     source = req_getval(id_source)
  82.     extent = req_getval(id_extent)
  83.     reversex = req_getval(id_reversex)
  84.     reversey = req_getval(id_reversey)
  85.     reversez = req_getval(id_reversez)
  86.     primary = req_getval(id_primary)
  87.     random = req_getval(id_random)
  88.     inside = req_getval(id_inside)
  89.     voltype = req_getval(id_voltype)
  90.  
  91.  
  92. call SEL_MODE('global')
  93. Box=BoundingBox()
  94. parse var box n x1 x2 y1 y2 z1 z2
  95. If n=0 then do
  96.     call notify(1,"!Need data on this layer!")
  97.     exit
  98. end
  99. fnam = getfilename("Base save name",fnam)
  100. if fnam='(none)' then do 
  101.     call req_end
  102.     exit
  103. end
  104.  
  105. If voltype=1 then voltype="volincl"
  106. else voltype="volexcl"
  107. xslice = Word(div,1) 
  108. yslice = Word(div,2) 
  109. zslice = Word(div,3) 
  110. cx = Word(explosioncenter,1)
  111. cy = Word(explosioncenter,2)
  112. cz = Word(explosioncenter,3)
  113. say xslice yslice zslice
  114.  
  115. /* find cell size */
  116. xsize = x2-x1
  117. If xsize=0 then xsize=.01
  118. ysize = y2-y1
  119. If ysize=0 then ysize=.01
  120. zsize = z2-z1
  121. If zsize=0 then zsize=.01
  122. say xsize ysize zsize
  123.  
  124.  
  125. If random = 1 then do
  126. xover = xsize*.005  /* .05% bigger to insure capturing all polys */
  127. yover = ysize*.005  /* along the coplanar edges  */
  128. zover = zsize*.005 
  129. xstart = x1-(xover*.5) 
  130. ystart = y1-(yover*.5)  
  131. zstart = z1-(zover*.5) 
  132. say xstart ystart zstart
  133. x = (xsize+xover)/xslice    /* cell size */
  134. y = (ysize+yover)/yslice
  135. z = (zsize+zover)/zslice
  136.     If reversex=1 then do
  137.         xlo = 0
  138.         xhi = xslice-1
  139.         xc = 1
  140.     end
  141.     If reversex=2 then do
  142.         xhi = 0
  143.         xlo = xslice-1
  144.         xc = -1
  145.     end    
  146.  
  147.     If reversey=1 then do
  148.         ylo = 0
  149.         yhi = yslice-1
  150.         yc = 1
  151.     end
  152.     If reversey=2 then do
  153.         yhi = 0
  154.         ylo = yslice-1
  155.         yc = -1
  156.     end    
  157.  
  158.     If reversez=1 then do
  159.         zlo = 0
  160.         zhi = zslice-1
  161.         zc = 1
  162.     end
  163.     If reversez=2 then do
  164.         zhi = 0
  165.         zlo = zslice-1
  166.         zc = -1
  167.     end    
  168.     say xlo xhi ylo yhi zlo zhi
  169.     If primary = 1 then do
  170.         prim.1 ='Do i = ' xlo ' to ' xhi ' by ' xc
  171.         prim.2 ='Do j = ' ylo ' to ' yhi ' by ' yc
  172.         prim.3 ='Do k = ' zlo ' to ' zhi ' by ' zc
  173.     end
  174.    If primary = 2 then do
  175.         prim.3 = 'Do i = ' xlo ' to ' xhi ' by ' xc
  176.         prim.1 = 'Do j = ' ylo ' to ' yhi ' by ' yc
  177.         prim.2 = 'Do k = ' zlo ' to ' zhi ' by ' zc
  178.     end
  179.    If primary = 3 then do
  180.         prim.2 = 'Do i = ' xlo ' to ' xhi ' by ' xc
  181.         prim.3 = 'Do j = ' ylo ' to ' yhi ' by ' yc
  182.         prim.1 = 'Do k = ' zlo ' to ' zhi ' by ' zc
  183.     end
  184.         primaryaxis = prim.1 d2c(10) prim.2 d2c(10) prim.3 d2c(10)
  185.         say primaryaxis
  186.  
  187.                 /* say nx1 xstart x i j k */
  188.                 prim2='nx1 = '||xstart||'+('||x||'*i)'||D2C(10)
  189.                 prim2=prim2||'ny1 = '||ystart||'+('||y||'*j)'||D2C(10)
  190.                 prim2=prim2||'nz1 = '||zstart||'+('||z||'*k)'||D2C(10)
  191.                 prim2=prim2||'nx2 = nx1 + '||x||d2c(10)
  192.                 prim2=prim2||'ny2 = ny1 + '||y||d2c(10)
  193.                 prim2=prim2||'nz2 = nz1 + '||z||d2c(10)
  194.                 /* say i j k */
  195.                 /* say nx1 ny2 nz1 nx2 ny1 nz2  */
  196.                 prim2=prim2||'call SEL_MODE("'"user"'")'||d2c(10)
  197.                 prim2=prim2||'call SEL_POLYGON("'"clear"'")'||d2c(10)
  198.                 prim2=prim2||'call SEL_POLYGON("'"set"'","'"volincl"'",nx1 ny1 nz1, nx2 ny2 nz2)'||d2c(10)
  199.                 prim2=prim2||'call CUT()'||d2c(10)
  200.                 prim2=prim2||'If Word(BOUNDINGBOX(),1)~= 0 then call nibble' || d2c(10)       
  201.                 prim2=prim2||'else call PASTE()'||d2c(10)   
  202.                 prim2=prim2||'call SETLAYER(home)'||d2c(10)
  203.              prim2=prim2||'end'||d2c(10)
  204.         prim2=prim2||'end'||d2c(10)
  205.     prim2=prim2||'end'
  206.  
  207. primaryaxis = primaryaxis prim2
  208. say primaryaxis
  209. interpret primaryaxis
  210. end
  211.  
  212.  
  213. If random = 2 then do
  214.  
  215. xstart = x1 
  216. ystart = y1  
  217. zstart = z1 
  218.  
  219. x = xsize/xslice    /* cell size */
  220. y = ysize/yslice
  221. z = zsize/zslice
  222.  
  223.     list=''
  224.     do i = 1 to (xslice*yslice*zslice)
  225.             list = list||i||' '
  226.      end
  227.   c=1
  228.   Do i = 0 to xslice-1          
  229.         Do j = 0 to yslice-1
  230.             Do k = 0 to zslice-1
  231.                 nx1 = (xstart+(x*i))-(x*.1)
  232.                 ny1 = (ystart+(y*j))-(y*.1)
  233.                 nz1 = (zstart+(z*k))-(z*.1)
  234.                 rand.c=nx1 ny1 nz1
  235.                 nx2 = nx1 + x + (x*.2)
  236.                 ny2 = ny1 + y + (y*.2)
  237.                 nz2 = nz1 + z + (z*.2)
  238.                 rand2.c=nx2 ny2 nz2
  239.                 c=c+1
  240.             end
  241.         end
  242.     end
  243.  
  244.     Do in = 1 to (xslice*yslice*zslice)
  245.         if words(list)>1 then t=random(1,words(list),Time('S'))
  246.         else t=1
  247.         i=word(list,t)
  248.         say "i=" i list
  249.         list=strip(delword(list,t,1))
  250.         call SEL_MODE('user')
  251.         call SEL_POLYGON('clear')
  252.         call SEL_POLYGON('set',voltype , rand.i, rand2.i)
  253.         call CUT()
  254.         If Word(BOUNDINGBOX(),1)~= 0 then call nibble         
  255.         else call PASTE()  /*  put it back, cut got it all  */ 
  256.         call SETLAYER(home)
  257.     end
  258. end
  259.  
  260.  
  261. Box=BoundingBox()
  262. parse var box n 
  263. If n~=0 then do
  264.     call CUT()
  265.     call nibble
  266. end
  267. call SETLAYER(home)
  268. call writecenter
  269. call writetarget
  270.     qty = Trunc(xslice*yslice*zslice)
  271.     call notify(1,"!saved " cnt-1 " fragments out of " qty)
  272. exit
  273. call end_all  
  274. end /* forever */
  275.  
  276.  
  277. nibble:
  278.    call SETLAYER(scratch)
  279.    call PASTE()
  280.    call sel_mode('USER') 
  281.    c=xfrm_begin()
  282.    sx=0; sy=0; sz=0
  283.    do n=1 to c
  284.      v=xfrm_getpos(n)
  285.      sx=sx+word(v,1)
  286.      sy=sy+word(v,2)
  287.      sz=sz+word(v,3)
  288.      v=sx/c sy/c sz/c
  289.    end
  290.    call xfrm_end()
  291.    If source = 2 then do
  292.      v1 = -1*Word(v,1) 
  293.      v2 = -1*Word(v,2) 
  294.      v3 = -1*Word(v,3) 
  295.      call Move(v1 v2 v3)    /* center object */
  296.    end
  297.    If inside = 2 then call inr
  298.    call SAVE(fnam||Right(cnt,3,0))
  299.    call CUT()
  300.    pp.cnt = v         
  301.    cnt = cnt + 1
  302.    return 
  303.  
  304. inr:
  305.     call copy()
  306.     call changesurface("inside")
  307.     call flip()
  308.     call paste()
  309.     call mergepoints()
  310.     call surface("default")
  311.     return
  312.  
  313. writecenter:   /* file to move pivot point only, needs SPARKS v2.056 or < */
  314.     If (open(state, "t:pp.dat", 'W')) then do i = 1 to cnt-1
  315.         call writeln state, pp.i    
  316.         end i
  317.         call close state
  318.       /* source file to set up SPARKS origins */
  319.       If (open(state, "t:source", 'W')) then do i = 1 to cnt-1
  320.         call writeln state, pp.i
  321.       end i
  322.       call close state
  323.  
  324.     return 
  325.  
  326. writetarget:
  327.        do i = 1 to cnt-1
  328.          dx = Word(pp.i,1) - cx
  329.          dy = Word(pp.i,2) - cy
  330.          dz = Word(pp.i,3) - cz
  331.          r = sqrt(dx * dx + dy * dy + dz * dz)
  332.          If r<extent then do
  333.              factor = (1-(r/extent))+1
  334.              tx = cx + dx * factor
  335.              ty = cy + dy * factor
  336.              tz = cz + dz * factor
  337.          end
  338.  
  339.          If r>=extent then do
  340.             tx = cx + dx * 1
  341.             ty = cy + dy * 1
  342.             tz = cz + dz * 1
  343.          end
  344.          target.i = tx ty tz
  345.        end
  346.  
  347.        If (open(state, "t:target", 'W')) then do i = 1 to cnt-1
  348.          call writeln state, target.i
  349.        end i
  350.        call close state
  351.       
  352.     return 
  353.  
  354.  
  355.  
  356.  
  357. syntax:
  358. error:
  359.    call end_all
  360.    t=Notify(1,'!Rexx Script Error','@'ErrorText(rc),'Line 'SIGL||sourceline(SIGL))
  361.    exit
  362.