2015-01-06 13 views
0

현재 3 차원 직교 좌표계에 정점의 ArrayList가 있습니다. 다각형은 무작위입니다. 그것은 자동차, 컵 또는 용 일 수 있습니다.임의의 3D 폴리곤 (obj 파일 또는 stl 파일)의 중심입니다.

밀도가 변하지 않는다고 가정하면이 3D 오브젝트의 무게 중심 (x, y, z)을 계산하는 방법은 무엇입니까?

나는 얼굴과 정점을 ArrayList에 저장합니다.

public ArrayList<stlFace> StlFaces = new ArrayList<stlFace>(); 
public ArrayList<VertexGeometric> VertexList = new ArrayList<VertexGeometric>(); 
+1

질량의 모멘트의 적분을 계산하고이를 전체 질량으로 나눌 필요가 있습니다. 이것은 인터넷에 잘 설명되어 있습니다. Google을위한 시간 ;-) 그리고 당신이 너무 깊이 파고 들기 전에, 당신을 위해 도서관 기능을 찾을 수있을 것입니다. – Bathsheba

+0

@ Bathsheba 감사합니다! 좋은 링크가 있습니까? 나는 통합이 올바른 방향이어야한다고 생각합니다. –

답변

0

I 각면 또는 삼각형의 질량에 비례하는 표면을 계산 this를 사용 하였다. 그리고 각 삼각형의 질량 중심과 전체 물체 중심을 계산하기 위해서 나는 this을 사용하고있었습니다. 도우미 메서드 getCenter() 및 getSurface()를 Face 클래스에 추가하여 하나의 얼굴/삼각형에 한정된 계산을 캡슐화했습니다.

public static class Vertex { 

    public float x = 0; 
    public float y = 0; 
    public float z = 0; 

    public Vertex(float x, float y, float z) { 
     this.x = x; 
     this.y = y; 
     this.z = z; 
    } 
} 

public static class Face { 

    public Vertex v1; 
    public Vertex v2; 
    public Vertex v3; 

    public Face(Vertex v1, Vertex v2, Vertex v3) { 
     this.v1 = v1; 
     this.v2 = v2; 
     this.v3 = v3; 
    } 

    public Vertex getCenter() { 
     Vertex triangleCenter = new Vertex(0, 0, 0); 
     triangleCenter.x += v1.x; 
     triangleCenter.x += v2.x; 
     triangleCenter.x += v3.x; 
     triangleCenter.y += v1.y; 
     triangleCenter.y += v2.y; 
     triangleCenter.y += v3.y; 
     triangleCenter.z += v1.z; 
     triangleCenter.z += v2.z; 
     triangleCenter.z += v3.z; 
     triangleCenter.x /= 3; 
     triangleCenter.y /= 3; 
     triangleCenter.z /= 3; 
     return triangleCenter; 
    } 

    public float getSurface() { 
     float x1 = v1.x - v2.x; 
     float x2 = v1.y - v2.y; 
     float x3 = v1.z - v2.z; 
     float y1 = v1.x - v3.x; 
     float y2 = v1.y - v3.y; 
     float y3 = v1.z - v3.z; 
     return (float) Math.sqrt(
       Math.pow(x2 * y3 - x3 * y2, 2) + 
       Math.pow(x3 * y1 - x1 * y3, 2) + 
       Math.pow(x1 * y2 - x2 * y1, 2) 
      )/2f; 
    } 
} 

public static Vertex calculateMassCenter(List<Face> faces) { 
    Vertex massCenter = new Vertex(0, 0, 0); 
    float mass = 0; 
    for (Face face : faces) { 
     Vertex triangleCenter = face.getCenter(); 
     float faceMass = face.getSurface(); 
     mass += faceMass; 
     massCenter.x += faceMass * triangleCenter.x; 
     massCenter.y += faceMass * triangleCenter.y; 
     massCenter.z += faceMass * triangleCenter.z; 
    } 
    massCenter.x /= mass; 
    massCenter.y /= mass; 
    massCenter.z /= mass; 
    return massCenter; 
}