New MeshWidth() comand

Blitz3D Forums/Blitz3D Programming/New MeshWidth() comand

JoshK(Posted 2004) [#1]
This adds a global parameter! :D

Function MeshWidth#(mesh,globl=False)
start=True
For s=1 To CountSurfaces(mesh)
	surf=GetSurface(mesh,s)
	For v=0 To CountVertices(surf)-1
		x#=VertexX(surf,v)
		y#=VertexY(surf,v)
		z#=VertexZ(surf,v)
		If globl
			TFormPoint x,y,z,mesh,0
			x=TFormedX()
			y=TFormedY()
			z=TFormedZ()
			EndIf
		If x<minx# Or start=True minx=x
		If y<miny# Or start=True miny=y
		If z<minz# Or start=True minz=z
		If x>maxx# Or start=True maxx=x
		If y>maxy# Or start=True maxy=y
		If z>maxz# Or start=True maxz=z
		start=False
		Next
	Next
w#=maxx-minx
h#=maxy-miny
d#=maxz-minz
Return w
End Function

Function MeshHeight#(mesh,globl=False)
start=True
For s=1 To CountSurfaces(mesh)
	surf=GetSurface(mesh,s)
	For v=0 To CountVertices(surf)-1
		x#=VertexX(surf,v)
		y#=VertexY(surf,v)
		z#=VertexZ(surf,v)
		If globl
			TFormPoint x,y,z,mesh,0
			x=TFormedX()
			y=TFormedY()
			z=TFormedZ()
			EndIf
		If x<minx# Or start=True minx=x
		If y<miny# Or start=True miny=y
		If z<minz# Or start=True minz=z
		If x>maxx# Or start=True maxx=x
		If y>maxy# Or start=True maxy=y
		If z>maxz# Or start=True maxz=z
		start=False
		Next
	Next
w#=maxx-minx
h#=maxy-miny
d#=maxz-minz
Return h
End Function

Function MeshDepth#(mesh,globl=False)
start=True
For s=1 To CountSurfaces(mesh)
	surf=GetSurface(mesh,s)
	For v=0 To CountVertices(surf)-1
		x#=VertexX(surf,v)
		y#=VertexY(surf,v)
		z#=VertexZ(surf,v)
		If globl
			TFormPoint x,y,z,mesh,0
			x=TFormedX()
			y=TFormedY()
			z=TFormedZ()
			EndIf
		If x<minx# Or start=True minx=x
		If y<miny# Or start=True miny=y
		If z<minz# Or start=True minz=z
		If x>maxx# Or start=True maxx=x
		If y>maxy# Or start=True maxy=y
		If z>maxz# Or start=True maxz=z
		start=False
		Next
	Next
w#=maxx-minx
h#=maxy-miny
d#=maxz-minz
Return d
End Function



IPete2(Posted 2004) [#2]
Josh,

Thanks - you rock!

IPete2.


xmlspy(Posted 2004) [#3]
This is very neat, I will definitly use this one.


JoshK(Posted 2004) [#4]
Here is a better version. This is recursive, and skips non-mesh entities. It also includes commands to return a mesh's extents on each axis:

Function MeshWidth#(mesh,globl=False,recursive=False)
min#=meshminx(mesh,globl,recursive)
max#=meshmaxx(mesh,globl,recursive)
Return (max-min)/2.0+min
End Function

Function MeshHeight#(mesh,globl=False,recursive=False)
min#=meshminy(mesh,globl,recursive)
max#=meshmaxy(mesh,globl,recursive)
Return (max-min)/2.0+min
End Function

Function MeshDepth#(mesh,globl=False,recursive=False)
min#=meshminz(mesh,globl,recursive)
max#=meshmaxz(mesh,globl,recursive)
Return (max-min)/2.0+min
End Function

Function MeshMaxX#(m,globl=False,recursive=False)
start=True
If EntityClass(m)="Mesh"
	For s=1 To CountSurfaces(m)
		surf=GetSurface(m,s)
		For v=0 To CountVertices(surf)-1
			x#=VertexX(surf,v)
			y#=VertexY(surf,v)
			z#=VertexZ(surf,v)
			If globl
				TFormPoint(x,y,z,m,0)
				x=TFormedX()
				y=TFormedY()
				z=TFormedZ()
				EndIf
			If x>maxx# Or start=True maxx=x
			start=False
			Next
		Next
	EndIf
If recursive
	For c=1 To CountChildren(m)
		x=meshmaxx#(GetChild(m,c),globl,True)
		If x>maxx# Or start=True maxx=x
		start=False
		Next
	EndIf
Return maxx
End Function

Function MeshMinX#(m,globl=False,recursive=False)
start=True
If EntityClass(m)="Mesh"
	For s=1 To CountSurfaces(m)
		surf=GetSurface(m,s)
		For v=0 To CountVertices(surf)-1
			x#=VertexX(surf,v)
			y#=VertexY(surf,v)
			z#=VertexZ(surf,v)
			If globl
				TFormPoint(x,y,z,m,0)
				x=TFormedX()
				y=TFormedY()
				z=TFormedZ()
				EndIf
			If x<minx# Or start=True minx=x
			start=False
			Next
		Next
	EndIf
If recursive
	For c=1 To CountChildren(m)
		x=meshminx#(GetChild(m,c),globl,True)
		If x<minx# Or start=True minx=x
		start=False
		Next
	EndIf
Return minx
End Function

Function MeshMaxY#(m,globl=False,recursive=False)
start=True
If EntityClass(m)="Mesh"
	For s=1 To CountSurfaces(m)
		surf=GetSurface(m,s)
		For v=0 To CountVertices(surf)-1
			x#=VertexX(surf,v)
			y#=VertexY(surf,v)
			z#=VertexZ(surf,v)
			If globl
				TFormPoint(x,y,z,m,0)
				x=TFormedX()
				y=TFormedY()
				z=TFormedZ()
				EndIf
			If y>maxy# Or start=True maxy=y
			start=False
			Next
		Next
	EndIf
If recursive
	For c=1 To CountChildren(m)
		y=meshmaxy#(GetChild(m,c),globl,True)
		If y>maxy# Or start=True maxy=y
		start=False
		Next
	EndIf
Return maxy
End Function

Function MeshMinY#(m,globl=False,recursive=False)
start=True
If EntityClass(m)="Mesh"
	For s=1 To CountSurfaces(m)
		surf=GetSurface(m,s)
		For v=0 To CountVertices(surf)-1
			x#=VertexX(surf,v)
			y#=VertexY(surf,v)
			z#=VertexZ(surf,v)
			If globl
				TFormPoint(x,y,z,m,0)
				x=TFormedX()
				y=TFormedY()
				z=TFormedZ()
				EndIf
			If y<miny# Or start=True miny=y
			start=False
			Next
		Next
	EndIf
If recursive
	For c=1 To CountChildren(m)
		y=meshminy#(GetChild(m,c),globl,True)
		If y<miny# Or start=True miny=y
		start=False
		Next
	EndIf
Return miny
End Function

Function MeshMaxZ#(m,globl=False,recursive=False)
start=True
If EntityClass(m)="Mesh"
	For s=1 To CountSurfaces(m)
		surf=GetSurface(m,s)
		For v=0 To CountVertices(surf)-1
			x#=VertexX(surf,v)
			y#=VertexY(surf,v)
			z#=VertexZ(surf,v)
			If globl
				TFormPoint(x,y,z,m,0)
				x=TFormedX()
				y=TFormedY()
				z=TFormedZ()
				EndIf
			If z>maxz# Or start=True maxz=z
			start=False
			Next
		Next
	EndIf
If recursive
	For c=1 To CountChildren(m)
		z=meshmaxz#(GetChild(m,c),globl,True)
		If z>maxz# Or start=True maxz=z
		start=False
		Next
	EndIf
Return maxz
End Function

Function MeshMinZ#(m,globl=False,recursive=False)
start=True
If EntityClass(m)="Mesh"
	For s=1 To CountSurfaces(m)
		surf=GetSurface(m,s)
		For v=0 To CountVertices(surf)-1
			x#=VertexX(surf,v)
			y#=VertexY(surf,v)
			z#=VertexZ(surf,v)
			If globl
				TFormPoint(x,y,z,m,0)
				x=TFormedX()
				y=TFormedY()
				z=TFormedZ()
				EndIf
			If z<minz# Or start=True minz=z
			start=False
			Next
		Next
	EndIf
If recursive
	For c=1 To CountChildren(m)
		z=meshminz#(GetChild(m,c),globl,True)
		If z<minz# Or start=True minz=z
		start=False
		Next
	EndIf
Return minz
End Function



xmlspy(Posted 2004) [#5]
how about an entitywidth, entityheight, and entitydepth? that would be useful also.


JoshK(Posted 2004) [#6]
There's an entity scale x,y,z() lib function I posted...somewhere.


DJWoodgate(Posted 2004) [#7]
Maybe something along the lines of

Function Entitywidth#(Mesh, globl=False)
If globl Then parent=0 Else parent=getparent(mesh)
TFormVector MeshWidth(mesh),0,0,mesh,parent
Return Sqr(TFormedX()*TFormedX()+TFormedY()*TFormedY()+TFormedZ()*TFormedZ())
End Function

would work? And similarly for EntityHeight and Depth with the tformvector function adjusted accordingly. I can't think of any use for it at the moment though!

[edit]
Similarly, for entity scale:

Function EntityScaleX#(entity, globl=False)
If globl Then parent=0 Else parent=getparent(entity)
TFormVector 1,0,0,entity,parent
Return Sqr(TFormedX()*TFormedX()+TFormedY()*TFormedY()+TFormedZ()*TFormedZ())
End Function

Function EntityScaleY#(entity, globl=False)
If globl Then parent=0 Else parent=getparent(entity)
TFormVector 0,1,0,entity,parent
Return Sqr(TFormedX()*TFormedX()+TFormedY()*TFormedY()+TFormedZ()*TFormedZ())
End Function

Function EntityScaleZ#(entity, globl=False)
If globl Then parent=0 Else parent=getparent(entity)
TFormVector 0,0,1,entity,parent
Return Sqr(TFormedX()*TFormedX()+TFormedY()*TFormedY()+TFormedZ()*TFormedZ())
End Function

In this context if the global flag is false then the scale returned will be from the entities viewpoint in local space and should correspond to the scale applied by ScaleEntity(). If the Global flag is true then the scale returned for each axis will depend on the entities orientation in the scaled space from an observers viewpoint in world space.


JoshK(Posted 2004) [#8]
How does an entity have "width"?


Warren(Posted 2004) [#9]
Size along the X axis.


JoshK(Posted 2004) [#10]
How does an entity have a "size"? It is a point in 3D space.

Let me put it this way, what would the difference be between a MeshWidth() command and an EntityWidth() command?


Warren(Posted 2004) [#11]
You can scale entities seperately from their meshes. So a function that took the entity scaling into account along with the associated mesh would probably be what the person was asking about.


sswift(Posted 2004) [#12]
If you have a mesh that has a scale of 100 units wide, and you use scaleentity on a particular instance of it, then the resulting entity might be 200 units wide. You can also scale it differently on each axis. The mesh scale is only the base scale of the entity if it has not been changed in size from the default of 1.0.

The differnece between a meshwidth command and an entitywidth command is that entitywidth returns meshwidth() * entityscalex()


Bot Builder(Posted 2004) [#13]
Easy enough -


Even lets you get the global scale.


JoshK(Posted 2004) [#14]
I also have a lib somewhere that returns the actual scale values stored in the entity structure.

A global mesh width is just the MeshWidth() command, using the global flag.


DJWoodgate(Posted 2004) [#15]
I have updated my post above with functions that return entityscale to my liking though granted you can get blitz to cough them up more directly by peeking about. Thinking about it peeking the values from the blitz entity structure is probably a better way of getting the absolute scale factors, particularly if they are going to be used to plug back into ScaleEntity as might sometimes be the case. Thats from a numerical stability point of view anyway.


Bot Builder(Posted 2004) [#16]
Yeah. DJWoodgate's functions are about 2x as fast as mine as well. ohwell.