四面体面的大型数据结构,大部分为空 [英] Large, mostly empty data structure for tetrahedron faces
问题描述
我正在尝试将四面体中每个面的节点ID与对应的四面体ID链接起来.
I'm trying to link the node id of every face in a tetrahedron with it's corresponding tetra id.
tetras = [1 2 3 4 % Tetra 1
5 6 7 8] % Tetra 2
对于四边形1,有四个面:
For tetra 1, there are four faces:
faces = [1 2 3; 1 2 4; 1 3 4; 2 3 4] % Notice these are sorted
然后,我想将它们存储在数据结构中:
Then I'd like to store these in a data structure:
tet_for_face = cell(8,8,8) % 8 allows for the maximum node id
tet_for_face{1,2,3} = 1;
tet_for_face{1,2,4} = 1;
tet_for_face{1,3,4} = 1;
tet_for_face{2,3,4} = 1;
这意味着我可以在 O(1)中找到任何特定面孔的四边形ID:
This means that I can find the tetra ID of any particular face in O(1):
tet_for_face{2,3,3}
ans = []
tet_for_face{2,3,4}
ans = 1
此方法的问题在于它需要连续的内存.随着网格的变大,我的内存不足:
The problem with this approach is that it requires contiguous memory. As my mesh gets larger, I run out of memory:
cell(1000, 1000, 1000)
??? Error using ==> cell
Out of memory. Type HELP MEMORY for your options.
我还玩过使用嵌套单元格的方法:
I've also played around with using nested cells:
tet = cell(num_nodes, 1);
tet2 = cellfun(@(x) cell(num_nodes, 1), tet, 'UniformOutput', 0);
tet3 = cellfun(@(x) cellfun(@(y) cell(num_nodes, 1), x, 'UniformOutput', 0), tet2, 'UniformOutput', 0);
tet3{2}{3}{4} = 1;
...
尽管这适用于较小的网格,并且不需要连续内存(AFAIK),但它有一个使N = 1000崩溃的MATLAB的讨厌习惯.
Although this works for small meshes, and doesn't require contiguous memory (AFAIK), it has a nasty habit of crashing MATLAB with N=1000.
有什么想法吗?
推荐答案
在使用了稀疏数组(只能是1D或2D,不能是3D)后,又没有到达任何地方,我决定使用容器.地图(HashMap).
After a bit of playing with sparse arrays (which can only be 1D or 2D, not 3D), and not getting anywhere, I decided to go with containers.Map (HashMap).
我使用了字符串键,而我发现产生它们的最快方法是使用sprintf(而不是int2str或mat2str)
I used string keys, and the fastest way I found of producing them I found was using sprintf (rather than int2str or mat2str)
示例代码:
tet = containers.Map;
for tetra_id in tetras
for face in faces
face_key = sprintf('%d ', face);
tet(face_key) = tetra_id;
这给了我这样的地图:
tet('1 2 3') = 1
这篇关于四面体面的大型数据结构,大部分为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!