简体   繁体   English

Delphi中网格类结构的最佳方法是什么?

[英]What is the best way for a mesh class structure in Delphi?

I want to create a triangluar mesh structure in Delphi XE5. 我想在Delphi XE5中创建一个三角网格结构。

The main TMyMesh class has generic TObjectLists to hold the list of vertices, faces, etc. TMyMesh主类具有通用的TObjectList,用于保存顶点,面等的列表。

Let's say I have to calculate somthing for every face of the mesh. 假设我必须为网格的每个面计算东西。 I could let take the TMyMesh class care of this: 我可以这样处理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;

Or I could delegate it to the TMyTriangleFace class: 或者我可以将其委托给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;

In this case I would need to give the TMyTriangleFace class access to the ListOfVertices. 在这种情况下,我需要为TMyTriangleFace类提供对ListOfVertices的访问权限。 I could do this by passing the TMyMesh as owner in the procedure CreateListOfTriangleFaces. 我可以通过在过程CreateListOfTriangleFaces中将TMyMesh作为所有者传递来实现。

In my understanding the second part should be the better code (Law of Demeter). 以我的理解,第二部分应该是更好的代码(Demeter法则)。 But passing on TMyMesh as the owner is maybe not so nice. 但是,以TMyMesh作为所有者来传递可能不是很好。

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! 非常感谢你!

Creating a new object for every vertex and triangle is very inefficient because of all the extra initialization overhead and individual memory allocations. 由于所有额外的初始化开销和单独的内存分配,为每个顶点和三角形创建一个新对象的效率非常低。 Also access will be inefficient too due to data being sparse in memory (interleaved with objects headers Delphi creates?) and functions calls. 由于数据在内存中稀疏(与Delphi创建的对象标头交错)和函数调用,因此访问也将效率低下。

As David comments, it would be much faster to have everything in a single TMyMesh class with vertices and indices as array of records. 正如David所评论的,将所有内容都放在单个TMyMesh类中,并将顶点和索引作为记录数组会更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM