我想在Delphi XE5中創建一個三角形網格結構。Delphi中的mesh類結構的最佳方式是什麼?
主要TMyMesh類具有通用TObjectLists持有頂點列表,面等
比方說,我要計算財產以後爲網格的每個人臉上。我可以讓乘坐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類:
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;
在這種情況下,我需要給TMyTriangleFace類訪問ListOfVertices 。我可以通過在CreateListOfTriangleFaces過程中將TMyMesh作爲所有者來傳遞。
在我的理解第二部分應該是更好的代碼(德米特法)。但傳遞TMyMesh作爲所有者可能不太好。
這樣做的最佳做法是什麼?也許這兩種解決方案都走錯了方向,還有更好的解決方案?
非常感謝!
您想要儘可能使用值。把所有東西都作爲一個組件將導致可怕的性能。 –
此問題似乎無關緊要,因爲它要求進行代碼審查。 Code Review堆棧交換站點最好問這個問題。 –
看起來不像代碼審查給我。確實存在大量代碼示例,但本質上它是關於OOP方法與數據存儲的關係。如果面向對象應該是粒度或簡單的方法是有益的。 – Kromster