How to Split Mesh by HalfSpace in Aspose.3D

Splitting Meshes by HalfSpace in Aspose.3D

This tutorial demonstrates how to use Aspose.3D to perform mesh splitting operations using HalfSpace planes. This technique is useful for extracting specific portions of a 3D model based on spatial criteria.

Understanding HalfSpace Operations

A HalfSpace represents an infinite space divided by a plane. When combined with Aspose.3D’s boolean operations, it allows you to extract specific portions of a mesh that exist on one side of the defined plane.

Key Concepts:

  • HalfSpace: Represents an infinite space divided by a plane, defined by its Normal and a Position on the plane
  • Boolean Operations: Used to extract mesh portions relative to the HalfSpace
  • Positive Side: The portion of space where the plane normal points to

Code Example: Splitting a Mesh with HalfSpace

The following C# code demonstrates how to create a simple box mesh and split it using a HalfSpace plane:

using Aspose.ThreeD;
using Aspose.ThreeD.Entities;
using Aspose.ThreeD.Utilities;

// Create a new 3D scene
Scene scene = new Scene();

// Create a box mesh
Mesh boxMesh = new Box(2, 2, 2).ToMesh();
Node boxNode = scene.RootNode.CreateChildNode("Box", boxMesh);

// Create a half space whose plane passes through (0, 0, 0.5)
// and whose normal points to +Z
HalfSpace halfSpace = new HalfSpace(new Vector3(0, 0, 1), new Vector3(0, 0, 0.5));

// Cut the box with the half space, this keeps the part of the box
// on the positive side of the plane — the part above z = 0.5
BooleanOperator op = new BooleanOperator();
op.Operator = BooleanOperation.Sub;
op.First = BooleanOperand.Of(boxNode);
op.Second = BooleanOperand.Of(halfSpace);
Mesh splitResult = op.ToMesh();

// Add the result to the scene and save
scene.RootNode.CreateChildNode("SplitResult", splitResult);
scene.Save("halfspace_split_result.obj", FileFormat.WavefrontOBJ);

Console.WriteLine("Mesh split using HalfSpace completed successfully.");

Code Explanation

Namespace Requirements

using Aspose.ThreeD;
using Aspose.ThreeD.Entities;  // Contains HalfSpace and BooleanOperator classes
using Aspose.ThreeD.Utilities; // Contains Vector3 utilities

Defining the Cutting Plane

  1. HalfSpace Definition:
    HalfSpace halfSpace = new HalfSpace(new Vector3(0, 0, 1), new Vector3(0, 0, 0.5));
    
    • The first parameter is the plane normal, (0, 0, 1) points to +Z
    • The second parameter is a point on the plane, so the plane is z = 0.5
    • The positive side of the plane is where z > 0.5

Performing the Operation

op.Operator = BooleanOperation.Sub;
op.First = BooleanOperand.Of(boxNode);
op.Second = BooleanOperand.Of(halfSpace);
Mesh splitResult = op.ToMesh();
  • The Sub operation with a HalfSpace operand returns the portion of the mesh on the positive side of the plane
  • Only the kept fragment is returned as a new mesh, the original mesh is not modified

Saving the Result

scene.Save("halfspace_split_result.obj", FileFormat.WavefrontOBJ);
  • Supported formats include OBJ, STL, FBX, glTF, and more

Advanced Usage

Getting the Other Side of the Cut

// Flip the plane normal to keep the part below z = 0.5 instead
HalfSpace halfSpace = new HalfSpace(new Vector3(0, 0, -1), new Vector3(0, 0, 0.5));

Adjusting the Cutting Plane

// Different orientation - angled cut at 45 degrees in the XZ plane
HalfSpace halfSpace = new HalfSpace(new Vector3(0.7071, 0, 0.7071), new Vector3(0, 0, 1));

This implementation demonstrates the core functionality of Aspose.3D’s mesh splitting capabilities using HalfSpace planes, allowing for precise extraction of 3D geometry based on spatial criteria.