英文:
Union of 3D shapes created by a loop
问题
这是您提供的代码的翻译部分:
我有这个模块来创建四面体:
module draw_tetrahedra(tetrahedron_table)
{
for (i = [0:len(tetrahedron_table) - 1])
{
indices = tetrahedron_table[i];
echo("**RESULT tetrahedron ", i, " indices: ", indices);
p0 = edges_and_corners[indices[0]];
p1 = edges_and_corners[indices[1]];
p2 = edges_and_corners[indices[2]];
p3 = edges_and_corners[indices[3]];
color([ 1, 0, 0, 0.5 ])
polyhedron(points = [ p0, p1, p2, p3 ], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
}
}
我这样调用它:
tetrahedron_table = [
[ 1, 15, 3, 19 ],
[ 1, 14, 15, 19 ],
[ 1, 14, 19, 18 ],
[ 1, 18, 19, 17 ],
[ 1, 3, 9, 17 ],
[ 17, 19, 3, 16 ],
[ 9, 3, 8, 17 ],
[ 8, 17, 3, 16 ],
];
draw_tetrahedra(tetrahedron_table);
它创建了这样的四面体:
您如何获得循环创建的所有四面体的并集?
更新
我只是简单地将整个循环包装在union()
函数内,但它没有将它们合并:
module draw_tetrahedra(tetrahedron_table)
{
union()
{
for (i = [0:len(tetrahedron_table) - 1])
{
indices = tetrahedron_table[i];
echo("**RESULT tetrahedron ", i, " indices: ", indices);
p0 = edges_and_corners[indices[0]];
p1 = edges_and_corners[indices[1]];
p2 = edges_and_corners[indices[2]];
p3 = edges_and_corners[indices[3]];
color([ 1, 0, 0, 0.5 ])
polyhedron(points = [ p0, p1, p2, p3 ], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
}
}
}
这是您要求的翻译部分。
<details>
<summary>英文:</summary>
I have this module to create tetrahedra:
```openscad
module draw_tetrahedra(tetrahedron_table)
{
for (i = [0:len(tetrahedron_table) - 1])
{
indices = tetrahedron_table[i];
echo("**RESULT tetrahedron ", i, " indices: ", indices);
p0 = edges_and_corners[indices[0]];
p1 = edges_and_corners[indices[1]];
p2 = edges_and_corners[indices[2]];
p3 = edges_and_corners[indices[3]];
color([ 1, 0, 0, 0.5 ])
polyhedron(points = [ p0, p1, p2, p3 ], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
}
}
I call it like this:
tetrahedron_table = [
[ 1, 15, 3, 19 ],
[ 1, 14, 15, 19 ],
[ 1, 14, 19, 18 ],
[ 1, 18, 19, 17 ],
[ 1, 3, 9, 17 ],
[ 17, 19, 3, 16 ],
[ 9, 3, 8, 17 ],
[ 8, 17, 3, 16 ],
];
draw_tetrahedra(tetrahedron_table);
It creates tetrahedra like this:
How can I get a union of all the tetrahedra created by the loop?
Update
I just simply did wrap the entire loop inside the union()
function like this, but it didn't unify them:
module draw_tetrahedra(tetrahedron_table)
{
union()
{
for (i = [0:len(tetrahedron_table) - 1])
{
indices = tetrahedron_table[i];
echo("**RESULT tetrahedron ", i, " indices: ", indices);
p0 = edges_and_corners[indices[0]];
p1 = edges_and_corners[indices[1]];
p2 = edges_and_corners[indices[2]];
p3 = edges_and_corners[indices[3]];
color([ 1, 0, 0, 0.5 ])
polyhedron(points = [ p0, p1, p2, p3 ], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
}
}
}
答案1
得分: 1
我无法查看它(因为你没有包括 edges_and_corners
),但我认为你在更新中所做的是正确的。然而,如果多面体的面的点按错误顺序给出(参见此处),则对其进行的布尔运算可能会混乱。
如果这不能解决问题,请添加 edges_and_corners
,以便我可以尝试我的猜测。
更新
最后一个面的方向不对:将 faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 0, 1, 2 ] ]
替换为 faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 3, 2, 1 ] ]
。
英文:
I can't check it by myself (because you didn't include edges_and_corners
) but I think what you did in your update was right. However if the points of the faces of a polyhedron are given in the wrong order (see here), the boolean operations you do on it can be messed up.
If it doesn't solve the problem, add edges_and_corners
so I can try my guesses.
UPDATE
The last face was misoriented : replace faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]
by faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 3, 2, 1 ] ]
.
答案2
得分: 0
I just computed the vertices/points by a loop:
function tetrahedra_points(tetrahedron_table) =
[for (i = [0:len(tetrahedron_table) -
1])[edges_and_corners[tetrahedron_table[i][0]], edges_and_corners[tetrahedron_table[i][1]],
edges_and_corners[tetrahedron_table[i][2]], edges_and_corners[tetrahedron_table[i][3]]]];
and then did a union
of the tetrahedra without any loop:
tetrahedron_table = [
[ 1, 15, 3, 19 ],
[ 1, 14, 15, 19 ],
[ 1, 14, 19, 18 ],
[ 1, 18, 19, 17 ],
[ 1, 3, 9, 17 ],
[ 17, 19, 3, 16 ],
[ 9, 3, 8, 17 ],
[ 8, 17, 3, 16 ],
];
points = tetrahedra_points(tetrahedron_table = tetrahedron_table);
union()
{
polyhedron(points = points[0], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[1], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[2], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[3], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[4], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[5], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[6], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[7], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
}
英文:
Eventually, I just computed the vertices/points by a loop:
function tetrahedra_points(tetrahedron_table) =
[for (i = [0:len(tetrahedron_table) -
1])[edges_and_corners[tetrahedron_table[i][0]], edges_and_corners[tetrahedron_table[i][1]],
edges_and_corners[tetrahedron_table[i][2]], edges_and_corners[tetrahedron_table[i][3]]]];
and then did a union
of the tetrahedra without any loop:
tetrahedron_table = [
[ 1, 15, 3, 19 ],
[ 1, 14, 15, 19 ],
[ 1, 14, 19, 18 ],
[ 1, 18, 19, 17 ],
[ 1, 3, 9, 17 ],
[ 17, 19, 3, 16 ],
[ 9, 3, 8, 17 ],
[ 8, 17, 3, 16 ],
];
points = tetrahedra_points(tetrahedron_table = tetrahedron_table);
union()
{
polyhedron(points = points[0], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[1], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[2], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[3], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[4], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[5], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[6], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
polyhedron(points = points[7], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论