Code archives/3D Graphics - Mesh/Centre Mesh

This code has been declared by its author to be Public Domain code.

Download source code

Centre Mesh by John Blackledge2006
Centres a mesh around its centre point
;--------------------
Function CentreAnimMesh(mesh,mode=0)
;--------------------
; mode=0 : centre on 3 axes
; mode=1 : centre on x,z; align to y bottom
Local origx#,origy#,origz#
Local allx#,ally#,allz#
Local newx#,newy#,newz#
Local cc,c,childent,mult

	; get orig position
	origx# = EntityX#(mesh)
	origy# = EntityY#(mesh)
	origz# = EntityZ#(mesh)
	
	; get 'all' vertices positions
	allx# = MeshAllX#(mesh)
	If mode=0 Then ally# = MeshAllY#(mesh)
	If mode=1 Then ally# = MeshAllYlowest#(mesh)
	allz# = MeshAllZ#(mesh)

	; find difference
	newx# = (origx# - allx#)
	newy# = (origy# - ally#)
	newz# = (origz# - allz#)
	
	; move one or all meshes
	cc = CountChildren(mesh)
	If cc = 0
		PositionMesh mesh, newx#,newy#,newz#
	Else
		For c = 1 To cc
			childent = GetChild(mesh,c)
			mult = 2
			PositionMesh childent, mult * newx#,mult * newy#,mult * newz#
		Next
	EndIf

End Function

;--------------------
Function MeshAllX#(parent)
;--------------------
Local LeftX#,RightX#
Local cc,c,childent,vx#,x#

	cc = CountChildren(parent)
	If cc = 0
		x# = MeshX#(parent)
	Else
		For c = 1 To cc
			childent = GetChild(parent,c)
			vx# = MeshX#(childent)
			If vx#<LeftX# Then LeftX#=vx#
			If vx#>RightX# Then RightX#=vx#
		Next
		x# = LeftX# + ( (RightX# - LeftX#) / Float(2) )
	EndIf

Return x#
End Function

;--------------------
Function MeshAllY#(parent)
;--------------------
Local BottomY#,TopY#
Local cc,c,childent,vy#,y#

	cc = CountChildren(parent)
	If cc = 0
		y# = MeshY#(parent)
	Else
		For c = 1 To cc
			childent = GetChild(parent,c)
			vy# = MeshY#(childent)
			If vy#<BottomY# Then BottomY#=vy#
	    If vy#>TopY# Then TopY#=vy#
		Next
		y# = BottomY# + ( (TopY# - BottomY#) / Float(2) )
	EndIf

Return y#
End Function

;--------------------
Function MeshAllYlowest#(parent)
;--------------------
Local BottomY#,TopY#
Local cc,c,childent,vy#,y#

	cc = CountChildren(parent)
	If cc = 0
		y# = BottomMost#(parent)
	Else
		For c = 1 To cc
			childent = GetChild(parent,c)
			vy# = BottomMost#(childent)
			If vy#<BottomY# Then BottomY#=vy#
	    If vy#>TopY# Then TopY#=vy#
		Next
		y# = BottomY# + ( (TopY# - BottomY#) / Float(2) )
	EndIf

Return y#
End Function

;--------------------
Function MeshAllZ#(parent)
;--------------------
Local BackZ#,FrontZ#
Local cc,c,childent,vz#,z#

	cc = CountChildren(parent)
	If cc = 0
		z# = MeshZ#(parent)
	Else
		For c = 1 To cc
			childent = GetChild(parent,c)
			vz# = MeshZ#(childent)
			If vz#<BackZ# Then BackZ#=vz#
			If vz#>FrontZ# Then FrontZ#=vz#
		Next
		z# = BackZ# + (FrontZ# - BackZ#)/ 2
	EndIf

Return z#
End Function

;------------------------------------------------------------------------------------
;--------------------
Function MeshX#(mesh)
;--------------------
Local LeftX#,RightX#
Local s,su,v,vx#,x#

	For s = 1 To CountSurfaces(mesh)
		su = GetSurface(mesh,s)
		For v = 0  To CountVertices(su)-1
			vx# = VertexX#(su,v)
			If vx#<LeftX# Then LeftX#=vx#
			If vx#>RightX# Then RightX#=vx#
		Next
	Next
  x# = LeftX# + ( (RightX# - LeftX#) / Float(2) )

Return x#
End Function

;--------------------
Function MeshY#(mesh)
;--------------------
Local BottomY#,TopY#
Local s,su,v,vy#,y#

	For s = 1 To CountSurfaces(mesh)
		su = GetSurface(mesh,s)
		For v = 0  To CountVertices(su)-1
			vy# = VertexY#(su,v)
			If vy#<BottomY# Then BottomY#=vy#
	    If vy#>TopY# Then TopY#=vy#
		Next
	Next
	y# = BottomY# + ( (TopY# - BottomY#) / Float(2) )

Return y#
End Function

;--------------------
Function MeshZ#(mesh)
;--------------------
Local BackZ#,FrontZ#
Local s,su,v,vz#,z#

	For s = 1 To CountSurfaces(mesh)
		su = GetSurface(mesh,s)
		For v = 0  To CountVertices(su)-1
			vz# = VertexZ#(su,v)
			If vz#<BackZ# Then BackZ#=vz#
			If vz#>FrontZ# Then FrontZ#=vz#
		Next
	Next
	z# = BackZ# + (FrontZ# - BackZ#) / 2

Return z#
End Function

;--------------------
Function BottomMost#(mesh)
;--------------------
Local BottomY#,TopY#
Local s,su,v,vy#

	For s = 1 To CountSurfaces(mesh)
		su = GetSurface(mesh,s)
		For v = 0  To CountVertices(su)-1
			vy# = VertexY#(su,v)
			If vy#<BottomY# Then BottomY#=vy#
			If vy#>TopY# Then TopY#=vy#
		Next
	Next

Return BottomY#
End Function

Comments

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


Stevie G2006
Agree completely and I mentioned this in the original post. I'll stick with my own mesh center function ;)

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 Blackledge2006
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_2009
This is Extremely useful!
Especially when combining meshes with AddMesh :D


John Blackledge2009
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.


Code Archives Forum