网格上的布尔运算
Contents
[
Hide
]
{% alert color=“primary” %}
此功能由 25.3 或更高版本支持。
{% /alert %}
网格上的布尔运算
Aspose.3D for .NET 允许您通过 BooleanOperator 类对网格应用布尔运算。支持的运算类型由 BooleanOperation 枚举定义:
ADD——两个网格的并集INTERSECT——两个网格的公共体积SUB——第一个网格减去第二个网格
布尔运算的每个操作数可以是 Node 的实例、任何实现 IMeshConvertible 的实体(如 Mesh)或 HalfSpace。对于两个普通网格,Mesh.DoBoolean 静态方法直接应用该运算,并为每个操作数接受一个变换矩阵。
{% alert color=“primary” %}
布尔运算要求流形网格。您可以在应用运算之前使用 Mesh.IsManifold() 方法检查网格,请参阅验证和优化网格。
{% /alert %}
两个重叠网格的并集
此代码示例计算两个重叠盒体网格的并集:
// Create two overlapping box meshes
var a = new Box(1, 1, 1).ToMesh();
var b = new Box(1, 1, 1).ToMesh();
// Translate the second box by (0.25, 0, 0) so the two boxes overlap
var transform = new Matrix4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0.25, 0, 0, 1);
// Union of the two boxes
var union = Mesh.DoBoolean(BooleanOperation.Add, a, transform, b, null);
Console.WriteLine("The union mesh has {0} polygons", union.PolygonCount);
用半空间切割网格
HalfSpace 表示由平面划分的无限空间,由其 Normal 和平面上的 Position 定义。用半空间切割网格会保留网格位于平面正侧(法线指向的一侧)的部分。结合 HalfSpace 操作数使用 BooleanOperation.Sub 运算:
// Create a scene with a box mesh
var scene = new Scene();
var boxNode = scene.RootNode.CreateChildNode("box", new Box(2, 2, 2).ToMesh());
// Define a half space whose plane passes through (0, 0, 0.5)
// and whose normal points to +Z
var halfSpace = new HalfSpace(new Vector3(0, 0, 1), new Vector3(0, 0, 0.5));
// Cut the box, this keeps the part of the box above z = 0.5
var op = new BooleanOperator();
op.Operator = BooleanOperation.Sub;
op.First = BooleanOperand.Of(boxNode);
op.Second = BooleanOperand.Of(halfSpace);
Mesh cut = op.ToMesh();
Console.WriteLine("The cut mesh has {0} polygons", cut.PolygonCount);