I want to create a triangluar mesh structure in Delphi XE5.
The main TMyMesh class has generic TObjectLists to hold the list of vertices, faces, etc.
Let's say I have to calculate somthing for every face of the mesh. I could let take the TMyMesh class care of this:
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;
Or I could delegate it to the TMyTriangleFace class:
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;
In this case I would need to give the TMyTriangleFace class access to the ListOfVertices. I could do this by passing the TMyMesh as owner in the procedure CreateListOfTriangleFaces.
In my understanding the second part should be the better code (Law of Demeter). But passing on TMyMesh as the owner is maybe not so nice.
What is the best practice to do this? Maybe both solutions are going in the wrong direction and there is a much better solution?
Thank you very much!