2014-03-05 5 views
2

Delphi XE5에서 삼각형 메쉬 구조를 만들고 싶습니다.Delphi에서 메쉬 클래스 구조를위한 가장 좋은 방법은 무엇입니까?

주요 TMyMesh 클래스는, 정점의 목록을 유지하는 일반적인 TObjectLists이있다면 등

이의 내가 메쉬의 모든 얼굴을 위해 뭔가를해야만 계산할 수 있다고 가정 해 봅시다. 나는 ListOfVertices에 TMyTriangleFace 클래스 액세스 권한을 부여 할 필요가

TMyVertex=class(TComponent) 
    Vertex: TPoint3D; 
    //other fields and methods 
end; 

TMyTriangleFace=class(TComponent) 
    Vertices: Array [0..2] of Integer; 
    procedure DoSomethingWithTheFace; 
    //other fields and methods 
end; 

TMyMesh=class(TComponent) 
    ListOfVertices: TObjectList<TMyVertex>; 
    ListOfTriangleFaces: TObjectList<TMyTriangleFace>; 
    procedure CreateListOfTriangleFaces; 
    procedure DoSomethingWithAllFaces; 
end; 

procedure TMyTriangleFace.DoSomethingWithTheFace; 
begin 
    AVertex:=TMyMesh(Owner).ListOfVertices[Vertices[0]]; 
    //do something 
end; 

procedure TMyMesh.CreateListOfTriangleFaces; 
begin 
    for i := 0 to NumberOfTriangleFaces-1 do 
    begin 
    ListOfTriangleFaces.Add(TMyTraingleFace.Add(Self)); 
    end; 
end; 

procedure TMyMesh.DoSomethingWithAllFaces; 
begin 
    for i := 0 to ListOfFaces.Count-1 do 
    begin 
    ListOfTriangleFaces[i].DoSomethingWithTheFace; 
    end; 
end; 

이 경우 :이의 TMyMesh 클래스 관리 보자 수 :

TMyVertex=class(TComponent) 
    Vertex: TPoint3D; 
    //other fields and methods 
end; 

TMyTriangleFace=class(TComponent) 
    Vertices: Array [0..2] of Integer; 
    //other fields and methods 
end; 

TMyMesh=class(TComponent) 
    ListOfVertices: TObjectList<TMyVertex>; 
    ListOfTriangleFaces: TObjectList<TMyTriangleFace>; 
    procedure CreateListOfTriangleFaces; 
    procedure DoSomethingWithTheFace(const FaceNumber: Integer); 
    procedure DoSomethingWithAllFaces; 
end; 

procedure TMyMesh.CreateListOfTriangleFaces; 
begin 
    for i := 0 to NumberOfTriangleFaces-1 do 
    begin 
    ListOfTriangleFaces.Add(TMyTraingleFace.Add(nil)); 
    end; 
end; 

procedure TMyMesh.DoSomethingWithTheFace(const AFaceNumber: Integer); 
begin 
    AVertex:=ListOfVertices[ListOfFaces[AFaceNumber].Vertices[0]]; 
    //do something 
end; 

procedure TMyMesh.DoSomethingWithAllFaces; 
begin 
    for i := 0 to ListOfFaces.Count-1 do 
    begin 
    DoSomethingWithTheFace(i); 
    end; 
end; 

을 또는 내가 TMyTriangleFace 클래스에 위임 할 수있다 . CreateListOfTriangleFaces 프로 시저에서 TMyMesh를 소유자로 전달하여이 작업을 수행 할 수 있습니다.

제 2 부분은 더 나은 코드 (Demeter의 법칙)이어야합니다. 그러나 TMyMesh를 소유자로 전달하는 것은 그렇게 좋지 않을 수 있습니다.

이 작업을 수행 할 수있는 가장 좋은 방법은 무엇입니까? 어쩌면 두 가지 해결책이 잘못된 방향으로 가고 있으며 훨씬 좋은 해결책이 있습니까?

대단히 감사합니다!

+1

가능한 한 값을 사용하고 싶습니다. 모든 것을 구성 요소로 만들면 위력이 저하됩니다. –

+0

이 질문은 코드 검토를 요구하기 때문에 주제가 아닌 것으로 보입니다. 이 질문은 코드 검토 스택 교환 사이트에서 요청하는 것이 좋습니다. –

+0

나에게 코드 검토처럼 보이지 않습니다. 물론 큰 코드 샘플이 있지만 본질적으로 OOP 접근과 데이터 저장에 관한 것입니다. OOP가 세밀하거나 간단한 접근 방법이 유익해야합니다. – Kromster

답변

3

모든 정점 및 삼각형에 대해 새 개체를 만드는 것은 모든 추가 초기화 오버 헤드와 개별 ​​메모리 할당 때문에 매우 비효율적입니다. 또한 데이터가 메모리에 희박 해져서 (델파이가 생성 한 객체 헤더와 인터리빙되어 있기 때문에) 액세스가 비효율적 일 수 있습니다.

David가 언급했듯이 정점과 인덱스가있는 레코드를 하나의 TMyMesh 클래스에 모두 포함하는 것이 훨씬 빠릅니다.

+0

레코드에 대한 추가 정보 및 데이터 저장소에 대한 클래스 : http://stackoverflow.com/questions/1876879/records-in-delphi – Kromster

+0

감사! 나는 그때 물건을 사용하지 않을 것이다. 내가 레코드의 간단한 동적 배열을 사용해야합니까, 아니면 TList 을 사용할 수 있습니까? – user3384674

+1

그것에 대해 질문을했습니다 : http://stackoverflow.com/questions/22215029/what-are-the-pros-and-cons-of-using-dynamic-array-of-records-vs-tlisttmyrecord – Kromster