New MeshWidth() comand
Blitz3D Forums/Blitz3D Programming/New MeshWidth() comand
| ||
This adds a global parameter! :DFunction 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 |
| ||
Josh, Thanks - you rock! IPete2. |
| ||
This is very neat, I will definitly use this one. |
| ||
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 |
| ||
how about an entitywidth, entityheight, and entitydepth? that would be useful also. |
| ||
There's an entity scale x,y,z() lib function I posted...somewhere. |
| ||
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. |
| ||
How does an entity have "width"? |
| ||
Size along the X axis. |
| ||
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? |
| ||
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. |
| ||
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() |
| ||
Easy enough - Even lets you get the global scale. |
| ||
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. |
| ||
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. |
| ||
Yeah. DJWoodgate's functions are about 2x as fast as mine as well. ohwell. |