October 19, 2021, 10:57:50

Author Topic: [bb] Centre Mesh by John Blackledge [ 1+ years ago ]  (Read 765 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Centre Mesh by John Blackledge [ 1+ years ago ]
« on: June 29, 2017, 00:28:38 »
Title : Centre Mesh
Author : John Blackledge
Posted : 1+ years ago

Description : Centres a mesh around its centre point

Code :
Code: BlitzBasic
  1. ;--------------------
  2. Function CentreAnimMesh(mesh,mode=0)
  3. ;--------------------
  4. ; mode=0 : centre on 3 axes
  5. ; mode=1 : centre on x,z; align to y bottom
  6. Local origx#,origy#,origz#
  7. Local allx#,ally#,allz#
  8. Local newx#,newy#,newz#
  9. Local cc,c,childent,mult
  10.  
  11.         ; get orig position
  12.         origx# = EntityX#(mesh)
  13.         origy# = EntityY#(mesh)
  14.         origz# = EntityZ#(mesh)
  15.        
  16.         ; get 'all' vertices positions
  17.         allx# = MeshAllX#(mesh)
  18.         If mode=0 Then ally# = MeshAllY#(mesh)
  19.         If mode=1 Then ally# = MeshAllYlowest#(mesh)
  20.         allz# = MeshAllZ#(mesh)
  21.  
  22.         ; find difference
  23.         newx# = (origx# - allx#)
  24.         newy# = (origy# - ally#)
  25.         newz# = (origz# - allz#)
  26.        
  27.         ; move one or all meshes
  28.         cc = CountChildren(mesh)
  29.         If cc = 0
  30.                 PositionMesh mesh, newx#,newy#,newz#
  31.         Else
  32.                 For c = 1 To cc
  33.                         childent = GetChild(mesh,c)
  34.                         mult = 2
  35.                         PositionMesh childent, mult * newx#,mult * newy#,mult * newz#
  36.                 Next
  37.         EndIf
  38.  
  39. End Function
  40.  
  41. ;--------------------
  42. Function MeshAllX#(parent)
  43. ;--------------------
  44. Local LeftX#,RightX#
  45. Local cc,c,childent,vx#,x#
  46.  
  47.         cc = CountChildren(parent)
  48.         If cc = 0
  49.                 x# = MeshX#(parent)
  50.         Else
  51.                 For c = 1 To cc
  52.                         childent = GetChild(parent,c)
  53.                         vx# = MeshX#(childent)
  54.                         If vx#<LeftX# Then LeftX#=vx#
  55.                         If vx#>RightX# Then RightX#=vx#
  56.                 Next
  57.                 x# = LeftX# + ( (RightX# - LeftX#) / Float(2) )
  58.         EndIf
  59.  
  60. Return x#
  61. End Function
  62.  
  63. ;--------------------
  64. Function MeshAllY#(parent)
  65. ;--------------------
  66. Local BottomY#,TopY#
  67. Local cc,c,childent,vy#,y#
  68.  
  69.         cc = CountChildren(parent)
  70.         If cc = 0
  71.                 y# = MeshY#(parent)
  72.         Else
  73.                 For c = 1 To cc
  74.                         childent = GetChild(parent,c)
  75.                         vy# = MeshY#(childent)
  76.                         If vy#<BottomY# Then BottomY#=vy#
  77.             If vy#>TopY# Then TopY#=vy#
  78.                 Next
  79.                 y# = BottomY# + ( (TopY# - BottomY#) / Float(2) )
  80.         EndIf
  81.  
  82. Return y#
  83. End Function
  84.  
  85. ;--------------------
  86. Function MeshAllYlowest#(parent)
  87. ;--------------------
  88. Local BottomY#,TopY#
  89. Local cc,c,childent,vy#,y#
  90.  
  91.         cc = CountChildren(parent)
  92.         If cc = 0
  93.                 y# = BottomMost#(parent)
  94.         Else
  95.                 For c = 1 To cc
  96.                         childent = GetChild(parent,c)
  97.                         vy# = BottomMost#(childent)
  98.                         If vy#<BottomY# Then BottomY#=vy#
  99.             If vy#>TopY# Then TopY#=vy#
  100.                 Next
  101.                 y# = BottomY# + ( (TopY# - BottomY#) / Float(2) )
  102.         EndIf
  103.  
  104. Return y#
  105. End Function
  106.  
  107. ;--------------------
  108. Function MeshAllZ#(parent)
  109. ;--------------------
  110. Local BackZ#,FrontZ#
  111. Local cc,c,childent,vz#,z#
  112.  
  113.         cc = CountChildren(parent)
  114.         If cc = 0
  115.                 z# = MeshZ#(parent)
  116.         Else
  117.                 For c = 1 To cc
  118.                         childent = GetChild(parent,c)
  119.                         vz# = MeshZ#(childent)
  120.                         If vz#<BackZ# Then BackZ#=vz#
  121.                         If vz#>FrontZ# Then FrontZ#=vz#
  122.                 Next
  123.                 z# = BackZ# + (FrontZ# - BackZ#)/ 2
  124.         EndIf
  125.  
  126. Return z#
  127. End Function
  128.  
  129. ;------------------------------------------------------------------------------------
  130. ;--------------------
  131. Function MeshX#(mesh)
  132. ;--------------------
  133. Local LeftX#,RightX#
  134. Local s,su,v,vx#,x#
  135.  
  136.         For s = 1 To CountSurfaces(mesh)
  137.                 su = GetSurface(mesh,s)
  138.                 For v = 0  To CountVertices(su)-1
  139.                         vx# = VertexX#(su,v)
  140.                         If vx#<LeftX# Then LeftX#=vx#
  141.                         If vx#>RightX# Then RightX#=vx#
  142.                 Next
  143.         Next
  144.   x# = LeftX# + ( (RightX# - LeftX#) / Float(2) )
  145.  
  146. Return x#
  147. End Function
  148.  
  149. ;--------------------
  150. Function MeshY#(mesh)
  151. ;--------------------
  152. Local BottomY#,TopY#
  153. Local s,su,v,vy#,y#
  154.  
  155.         For s = 1 To CountSurfaces(mesh)
  156.                 su = GetSurface(mesh,s)
  157.                 For v = 0  To CountVertices(su)-1
  158.                         vy# = VertexY#(su,v)
  159.                         If vy#<BottomY# Then BottomY#=vy#
  160.             If vy#>TopY# Then TopY#=vy#
  161.                 Next
  162.         Next
  163.         y# = BottomY# + ( (TopY# - BottomY#) / Float(2) )
  164.  
  165. Return y#
  166. End Function
  167.  
  168. ;--------------------
  169. Function MeshZ#(mesh)
  170. ;--------------------
  171. Local BackZ#,FrontZ#
  172. Local s,su,v,vz#,z#
  173.  
  174.         For s = 1 To CountSurfaces(mesh)
  175.                 su = GetSurface(mesh,s)
  176.                 For v = 0  To CountVertices(su)-1
  177.                         vz# = VertexZ#(su,v)
  178.                         If vz#<BackZ# Then BackZ#=vz#
  179.                         If vz#>FrontZ# Then FrontZ#=vz#
  180.                 Next
  181.         Next
  182.         z# = BackZ# + (FrontZ# - BackZ#) / 2
  183.  
  184. Return z#
  185. End Function
  186.  
  187. ;--------------------
  188. Function BottomMost#(mesh)
  189. ;--------------------
  190. Local BottomY#,TopY#
  191. Local s,su,v,vy#
  192.  
  193.         For s = 1 To CountSurfaces(mesh)
  194.                 su = GetSurface(mesh,s)
  195.                 For v = 0  To CountVertices(su)-1
  196.                         vy# = VertexY#(su,v)
  197.                         If vy#<BottomY# Then BottomY#=vy#
  198.                         If vy#>TopY# Then TopY#=vy#
  199.                 Next
  200.         Next
  201.  
  202. Return BottomY#
  203. End Function


Comments :


Vorderman(Posted 1+ years ago)

 Does FITMESH -0.5,-0.5,-0.5,1,1,1 not do this already?


Stevie G(Posted 1+ years ago)

 Agree completely and I mentioned this in the original post.  I'll stick with my own mesh center function ;)
Code: [Select]
Function MESHcenter( Mesh )

  W# = meshwidth( Mesh )
  H# = meshheight( Mesh )
  D# = meshdepth( Mesh )
  fitmesh - W*.5, -H*.5, -D*.5, W, H, D

end function
Stevie


John Blackledge(Posted 1+ years ago)

 Not if the 'point of origin' (as with many 3DS and Autocad models) is far away - no it doesn't.I've had this situation with many client's models; and yes, I started with your code, but ended up with mine. ;)


_PJ_(Posted 1+ years ago)

 This is Extremely useful!Especially when combining meshes with AddMesh :D


John Blackledge(Posted 1+ years ago)

 Thanks. Glad someone finally got some use from this.It came about because I had a project that required the loading of the user's meshes, and a lot were created in Autocad which usually results in the model being created (in Blitz terms) miles from the origin point.And then they would winge 'I can't see my model!' to which I would reply, 'That's because you created it 5 miles from 0,0,0.'But of course it became my problem to solve. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal