Get a Mesh’s Triangles

Software: Away3D 4.x

Alex.R.Hogue, Newbie
Posted: 08 July 2011 09:55 PM   Total Posts: 3

Has anyone figured out how to get a collection of a mesh’s triangles?  I think it has something to do with the “getIndexBuffer” function but I’m not sure.

   

Stephen Hopkins, Sr. Member
Posted: 09 July 2011 04:19 AM   Total Posts: 110   [ # 1 ]

I’ve only done the reverse(putting together a mesh from triangles using updateVertex/IndexData), but i think you can do this:

For SubGeometry there is

.vertexData
.indexData

Can put the 2 together to construct triangles
Something like this, I just put this together really quick, should check over it yourself

public static function getTriangles(subgeometry:SubGeometry):Vector.<Triangle>{
 
var indexData:Vector.<uint> = subgeometry.indexData;
 var 
vertexData:Vector.<Number> = subgeometry.vertexData;
 var 
triangles:Vector.<Triangle> = new Vector.<Triangle>();
 for(var 
i:int 0i  indexData.length 3; ++i){
  
var v0index:int indexData[i 3];
  var 
v1index:int indexData[i 1];
  var 
v2index:int indexData[i 2];
  var 
v0:Vector3D = new Vector3D(
   
vertexData[v0index],//x
   
vertexData[v0index+1],//y
   
vertexData[v0index+2]);//z
  
var v1:Vector3D = new Vector3D(
   
vertexData[v1index],//x
   
vertexData[v1index+1],//y
   
vertexData[v1index+2]);//z
  
var v2:Vector3D = new Vector3D(
   
vertexData[v2index],//x
   
vertexData[v2index+1],//y
   
vertexData[v2index+2]);//z
  
triangles.push(new Triangle(v0v1v2));//this class doesn't exist
 
}
 
return triangles;

 

 Signature 

http://www-scf.usc.edu/~shopkins

   

Alex.R.Hogue, Newbie
Posted: 13 July 2011 09:31 PM   Total Posts: 3   [ # 2 ]

Hey Stephen,
Thanks a lot.  I implemented your method and it is good start but I don’t think it is working properly.  I added spheres to the view at each v0, v1 and v2 expecting that the cubes would appear at the 8 vertices of the mesh I was testing but this was not the case.  A few were correct while others were either in the middle of the mesh or off to the sides. Any ideas?

EDIT:
Here is some sample output.  I am tracing the 3 vectors of each triangle of the cube.

Triangle 1Vector3D(24.1613, -24.1613, -24.1613) , Vector3D(-24.1613, -24.1613, -24.1613) , Vector3D(-24.1613, -24.1613, -24.1613)
Triangle 2Vector3D(24.1613, -24.1613, -24.1613) , Vector3D(-24.1613, -24.1613, -24.1613) , Vector3D(-24.1613, -24.1613, -24.1613)
Triangle 3Vector3D(-24.1613, -24.1613, -24.1613) , Vector3D(-24.1613, -24.161324.1613) , Vector3D(-24.161324.1613, -24.1613)
Triangle 4Vector3D(-24.1613, -24.1613, -24.1613) , Vector3D(-24.161324.1613, -24.1613) , Vector3D(24.1613, -24.161324.1613)
Triangle 5Vector3D(-24.161324.161324.1613) , Vector3D(24.161324.1613, -24.1613) , Vector3D(24.1613, -24.161324.1613)
Triangle 6Vector3D(-24.161324.161324.1613) , Vector3D(24.1613, -24.161324.1613) , Vector3D(-24.161324.161324.1613)
Triangle 7Vector3D(24.161324.1613, -24.1613) , Vector3D(24.1613, -24.1613, -24.1613) , Vector3D(-24.1613, -24.161324.1613)
Triangle 8Vector3D(24.161324.1613, -24.1613) , Vector3D(-24.1613, -24.161324.1613) , Vector3D(-24.161324.1613, -24.1613)
Triangle 9Vector3D(24.1613, -24.1613, -24.1613) , Vector3D(-24.1613, -24.161324.1613) , Vector3D(-24.161324.161324.1613)
Triangle 10Vector3D(24.1613, -24.1613, -24.1613) , Vector3D(-24.161324.161324.1613) , Vector3D(24.161324.161324.1613)
Triangle 11Vector3D(24.161324.161324.1613) , Vector3D(24.161324.161324.1613) , Vector3D(24.161324.161324.1613)
Triangle 12Vector3D(24.161324.161324.1613) , Vector3D(24.161324.161324.1613) , Vector3D(24.161324.161324.1613

Note how some triangles (11 and 12 for example) have the same 3 points.  Also I need to find the normals of these triangles as well.  Any ideas?

 

   

Stephen Hopkins, Sr. Member
Posted: 14 July 2011 09:58 PM   Total Posts: 110   [ # 3 ]

Hm I was going to try and test it but apparently, installing flash player 11 beta uninstalled incubator, and I can’t run broomstick anymore with the current bugs because of the new api

Ah I think I forgot to multiply the vertex indices by 3. There’s 3 numbers per vertex, so the indices need to be * 3

public static function getTriangles(subgeometry:SubGeometry):Vector.<Triangle>{
 
var indexData:Vector.<uint> = subgeometry.indexData;
 var 
vertexData:Vector.<Number> = subgeometry.vertexData;
 var 
triangles:Vector.<Triangle> = new Vector.<Triangle>();
 for(var 
i:int 0i  indexData.length 3; ++i){//each i is one triangle
  
var v0index:int indexData[i 3] 3;//3 numbers per vertex, so index 1, starts at index 3 in the vector
  
var v1index:int indexData[i 1] 3;
  var 
v2index:int indexData[i 2] 3;
  var 
v0:Vector3D = new Vector3D(
   
vertexData[v0index],//x
   
vertexData[v0index+1],//y
   
vertexData[v0index+2]);//z
  
var v1:Vector3D = new Vector3D(
   
vertexData[v1index],//x
   
vertexData[v1index+1],//y
   
vertexData[v1index+2]);//z
  
var v2:Vector3D = new Vector3D(
   
vertexData[v2index],//x
   
vertexData[v2index+1],//y
   
vertexData[v2index+2]);//z
  
triangles.push(new Triangle(v0v1v2));//this class doesn't exist
 
}
 
return triangles;

To find the normals, do normal = (v0 - v1).cross(v2 - v1), might need to switch the order depending on whether broomstick uses ccw or cw order.

 Signature 

http://www-scf.usc.edu/~shopkins

   

Alex.R.Hogue, Newbie
Posted: 14 July 2011 10:01 PM   Total Posts: 3   [ # 4 ]

That did it. Thanks so much.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X