这是一个功能,使用了几天前我问过的问题答案的建议。让我们在Mathematica中创建一个Graphics3D对象。我使用来自here的3D几何数据。
cd = Import[NotebookDirectory[] <> "withwake.obj"];
vertices = cd[[1, 2, 1]];
polygons = Flatten[cd[[1, 2, 2, 1]] /. Polygon -> List, 2];
Graphics3D[GraphicsComplex[vertices, Polygon[polygons]]]
对于每个多边形,我们创建一个指向其顶点的数字的列表,然后是指向每个边连接到的多边形的数字。重要的是要注意表面正常;定义面板的节点顺序应为逆时针。通过右手规则,如果手指弯曲以跟随编号,拇指将显示应指向“向外”几何体的法线向量。
如果Graphics3D
对象中的所有多边形都是三角形,这是一个创建这样一个列表的函数。
EdgeSorting[vertices_, polygons_] :=
Block[{triangleEdges, singleEdges, edgesNeighbors, relations, n, n1,
n2, trires, triangleNeigbours, TriangleMaker, polygonArea, tring},
(*Split every triangle in 3 edges,with nodes in each edge sorted*)
triangleEdges = (Sort /@ Subsets[#, {2}]) & /@ polygons;
(*Generate a list of edges*)
singleEdges = Union[Flatten[triangleEdges, 1]];
(*Define a function which,given an edge (node number list),
returns the bordering*)
(*triangle numbers.It's done by working through each of the \
triangles' edges*)
edgesNeighbors[_] = {};
MapIndexed[(edgesNeighbors[#1[[1]]] =
Flatten[{edgesNeighbors[#1[[1]]], #2[[1]]}];
edgesNeighbors[#1[[2]]] =
Flatten[{edgesNeighbors[#1[[2]]], #2[[1]]}];
edgesNeighbors[#1[[3]]] =
Flatten[{edgesNeighbors[#1[[3]]], #2[[1]]}];) &, triangleEdges];
(*Build a triangle relation table.Each'1' indicates a triangle \
relation*)
relations =
ConstantArray[
0, {triangleEdges // Length, triangleEdges // Length}];
Scan[(n = edgesNeighbors[##];
If[Length[n] == 2, {n1, n2} = n;
relations[[n1, n2]] = 1; relations[[n2, n1]] = 1];) &,
singleEdges];
Print[MatrixPlot[relations]];
(*Build a neighborhood list*)
triangleNeigbours =
Table[Flatten[Position[relations[[i]], 1]], {i,
triangleEdges // Length}];
trires =
Table[Flatten[{polygons[[i]], triangleNeigbours[[i]]}], {i, 1,
Length@polygons}];
TriangleMaker[{a_, b_, c_}] := {vertices[[a]], vertices[[b]],
vertices[[c]]};
{trires}
];
我不完全理解这个功能的工作方式。我无法理解如何实现以下目标。
答案 0 :(得分:5)
要创建所有多边形及其邻居的列表,您可以执行以下操作:
neighbours[polygons_] := {#,
Flatten@Position[polygons,
a_List /; Length[Intersection[a, #]] == 2]} & /@ polygons;
然后neighbours[polygons]
创建一个列表,其中i
条目由polygons[[i]]
和polygons[[i]]
的邻居的索引组成。
对于问题的第二部分,您可以执行类似
的操作wake[polygons_] :=
Module[{edges, boundaries, wakelist, body},
edges[polylist_] := Flatten[Map[Partition[#, 2, 1, 1] &, polylist], 1];
boundaries = Cases[Tally[
edges[polygons], (Sort[#1] == Sort[#2]) &], {a_, b_} /; b == 1 :> a];
wakelist =
DeleteDuplicates[
Map[Cases[polygons, a_ /; (Length[Intersection[a, #]] == 2)][[1]] &,
boundaries]];
{#, Flatten@Position[polygons, a_List /; (Length[Intersection[#, a]] == 2 &&
Not[MemberQ[wakelist, a]])]} & /@ wakelist]
在wake
中,如果多边形包含没有任何相邻多边形的边,则将其视为尾迹面板。我不知道这是否总是有效,但它似乎适用于问题中的例子。
修改强> 要分割尾流和体多边形中的多边形的完整列表,您可以执行类似
的操作split[polygons_] := Module[{edges, boundaries, wakelist},
edges[polylist_] := Flatten[Map[Partition[#, 2, 1, 1] &, polygons], 1];
boundaries = Cases[Tally[edges[polygons],
(Sort[#1] == Sort[#2]) &], {a_, b_} /; b == 1 :> a];
wakelist = DeleteDuplicates[Map[Cases[polygons,
a_ /; (Length[Intersection[a, #]] == 2)][[1]] &, boundaries]];
{wakelist, Complement[polygons, wakelist]}];
然后split[polygons]
将生成两个子列表的列表。第一个子列表包含属于唤醒的所有多边形,第二个子列表包含属于主体的所有多边形。由于split
已经将唤醒与身体分开,我们可以根据
wake
wake2[wakelist_, bodylist_] := {#, Flatten@Position[bodylist,
a_List /; (Length[Intersection[#, a]] == 2)]} & /@ wakelist
然后找到身体多边形的列表加上它们的邻居的索引,以及唤醒多边形的列表加上你可以做的邻近身体多边形的索引
{wakepols, bodypols} = split[polygons];
bodylist = neighbours[bodypols];
wakelist = wake[wakepols, bodypols];
请注意,bodylist
和wakelist
中多边形的索引现在引用bodypols
中的多边形,而不是完整列表polygons
中的多边形。