M4.1 — 工业格式 (Agent #0): - JT parser: ISO 14306 Segment→Part→Mesh/LOD, XT B-Rep decode - Parasolid XT: text/binary parser, Body→BrepModel mapping - ACIS SAT: text format parser with version handling - IFC: SPF parser, IfcWall/Slab/Beam/Column entities - STEP AP242: PMI annotation + tolerance export - 47 tests covering all formats + edge cases M4.2 — GPU 加速 (Agent #1): - CUDA kernels: mc_kernel, qem_cost_kernel, tri_intersect_kernel - __constant__ memory for edge tables, atomic triangle collection - CPU fallback when CUDA unavailable (seamless degradation) - VDE_USE_CUDA CMake option, .cu compilation support - 9 tests with CPU path validation M4.3 — Python 绑定 (Agent #2): - vde_brep: BrepModel, make_*, boolean, STEP/IGES, heal, validate - vde_sdf: primitives, operations, to_mesh, gradient descent - vde_cam: roughing/finishing/drilling, Tool, ToolLibrary - vde_assembly: Assembly, AssemblyNode, interference, explode - Version: 3.3.0 synced across pyproject/setup/__init__ 13 files, ~5200 lines, 56+ tests
358 lines
13 KiB
C++
358 lines
13 KiB
C++
#include <pybind11/pybind11.h>
|
|
#include <pybind11/eigen.h>
|
|
#include <pybind11/stl.h>
|
|
|
|
#include "vde/brep/assembly.h"
|
|
#include "vde/brep/interference_check.h"
|
|
#include "vde/brep/explode_view.h"
|
|
|
|
namespace py = pybind11;
|
|
using namespace vde::brep;
|
|
|
|
void bind_assembly(py::module& m) {
|
|
auto assy = m.def_submodule("assembly",
|
|
R"pbdoc(
|
|
Assembly modeling — hierarchical part and subassembly management.
|
|
|
|
Represents a tree structure where each node can be either:
|
|
- A part: contains a BrepModel with a local transform
|
|
- A subassembly: contains child nodes with a local transform
|
|
|
|
Classes:
|
|
Assembly: the root of an assembly tree
|
|
AssemblyNode: a node in the assembly tree (part or subassembly)
|
|
|
|
Operations:
|
|
interference_check: detect collisions between parts
|
|
explode_view: generate exploded assembly view
|
|
)pbdoc");
|
|
|
|
// ── AssemblyNode ────────────────────────────────────
|
|
py::class_<AssemblyNode, std::unique_ptr<AssemblyNode, py::nodelete>>(
|
|
assy, "AssemblyNode",
|
|
R"pbdoc(
|
|
A node in an assembly tree.
|
|
|
|
Can be either:
|
|
- A part node: has a BrepModel and no children
|
|
- A subassembly node: has children and no BrepModel
|
|
|
|
Attributes:
|
|
name: node name
|
|
local_transform: local transform relative to parent (Transform3D)
|
|
|
|
Methods:
|
|
is_part(): True if this node is a leaf part
|
|
add_part(name, model, transform): add a part child
|
|
add_subassembly(name, transform): add a subassembly child
|
|
|
|
Example:
|
|
>>> assy = assembly.Assembly("robot")
|
|
>>> base = assy.root.add_part("base", brep.make_box(10, 10, 2))
|
|
>>> arm = assy.root.add_subassembly("arm", core.translate(0, 0, 2))
|
|
>>> arm.add_part("segment1", brep.make_cylinder(0.5, 8))
|
|
)pbdoc")
|
|
.def_property_readonly("name", [](const AssemblyNode& n) -> const std::string& {
|
|
return n.name;
|
|
})
|
|
.def_readwrite("local_transform", &AssemblyNode::local_transform,
|
|
"Local transform matrix (Transform3D)")
|
|
.def("is_part", &AssemblyNode::is_part,
|
|
"True if this node is a leaf part (has a BrepModel)")
|
|
.def("add_part", &AssemblyNode::add_part,
|
|
py::arg("name"), py::arg("model"),
|
|
py::arg("transform") = core::Transform3D::Identity(),
|
|
py::return_value_policy::reference,
|
|
R"pbdoc(
|
|
Add a part (leaf node with BrepModel) as a child.
|
|
|
|
Args:
|
|
name: part name
|
|
model: BrepModel for this part
|
|
transform: local transform (default identity)
|
|
|
|
Returns:
|
|
AssemblyNode*: the newly created child node
|
|
)pbdoc")
|
|
.def("add_subassembly", &AssemblyNode::add_subassembly,
|
|
py::arg("name"),
|
|
py::arg("transform") = core::Transform3D::Identity(),
|
|
py::return_value_policy::reference,
|
|
R"pbdoc(
|
|
Add a subassembly (container node) as a child.
|
|
|
|
Args:
|
|
name: subassembly name
|
|
transform: local transform (default identity)
|
|
|
|
Returns:
|
|
AssemblyNode*: the newly created child node (empty, ready for adding parts)
|
|
)pbdoc")
|
|
.def_property_readonly("children", [](const AssemblyNode& n) -> const std::vector<std::unique_ptr<AssemblyNode>>& {
|
|
return n.children;
|
|
}, py::return_value_policy::reference)
|
|
.def("__repr__", [](const AssemblyNode& n) {
|
|
return "AssemblyNode(name=\"" + n.name
|
|
+ "\", is_part=" + std::string(n.is_part() ? "True" : "False") + ")";
|
|
});
|
|
|
|
// ── Assembly ───────────────────────────────────────
|
|
py::class_<Assembly>(assy, "Assembly",
|
|
R"pbdoc(
|
|
Assembly model — hierarchical tree of parts and subassemblies.
|
|
|
|
The root node is accessed via `assembly.root`. Parts and subassemblies
|
|
are added through the root node's add_part() and add_subassembly().
|
|
|
|
Attributes:
|
|
name: assembly name
|
|
version: version string (default "1.0")
|
|
root: root AssemblyNode
|
|
|
|
Methods:
|
|
part_count(): total number of leaf parts
|
|
node_count(): total number of tree nodes
|
|
|
|
Example:
|
|
>>> assy = assembly.Assembly("engine")
|
|
>>> assy.version = "2.0"
|
|
>>> block = assy.root.add_part("engine_block", brep.make_box(20, 15, 10))
|
|
>>> head = assy.root.add_subassembly("cylinder_head",
|
|
... core.translate(0, 7.5, 0))
|
|
>>> head.add_part("head_gasket", brep.make_box(20, 1, 10))
|
|
>>> print(assy.part_count()) # 2
|
|
)pbdoc")
|
|
.def(py::init<const std::string&>(),
|
|
py::arg("name"),
|
|
"Create an assembly with given name")
|
|
.def_readwrite("name", &Assembly::name, "Assembly name")
|
|
.def_readwrite("version", &Assembly::version, "Version string")
|
|
.def_readwrite("root", &Assembly::root, "Root AssemblyNode (add parts here)")
|
|
.def("part_count", &Assembly::part_count, "Total number of leaf parts")
|
|
.def("node_count", &Assembly::node_count, "Total number of tree nodes (including subassemblies)")
|
|
.def("visit_parts", [](const Assembly& a) {
|
|
// Collect all parts as tuples (name, model, world_transform)
|
|
py::list result;
|
|
a.visit_parts([&result](const std::string& name,
|
|
const BrepModel& model,
|
|
const core::Transform3D& xform) {
|
|
result.append(py::make_tuple(name, model, xform));
|
|
});
|
|
return result;
|
|
}, R"pbdoc(
|
|
Visit all leaf parts in the assembly.
|
|
|
|
Returns:
|
|
list of (name, BrepModel, world_transform) tuples
|
|
)pbdoc")
|
|
.def("__repr__", [](const Assembly& a) {
|
|
return "Assembly(name=\"" + a.name
|
|
+ "\", parts=" + std::to_string(a.part_count()) + ")";
|
|
});
|
|
|
|
// ── Interference check ─────────────────────────────
|
|
py::class_<InterferencePair>(assy, "InterferencePair",
|
|
R"pbdoc(
|
|
Interference result for a single part pair.
|
|
|
|
Attributes:
|
|
part_a, part_b: part names
|
|
interfering: True if parts collide
|
|
penetration: estimated penetration depth (mm)
|
|
overlap_volume: approximate overlap volume (mm³)
|
|
)pbdoc")
|
|
.def_readonly("part_a", &InterferencePair::part_a)
|
|
.def_readonly("part_b", &InterferencePair::part_b)
|
|
.def_readonly("interfering", &InterferencePair::interfering)
|
|
.def_readonly("penetration", &InterferencePair::penetration)
|
|
.def_readonly("overlap_volume", &InterferencePair::overlap_volume)
|
|
.def("__repr__", [](const InterferencePair& ip) {
|
|
return "InterferencePair(" + ip.part_a + " ↔ " + ip.part_b
|
|
+ ", interfering=" + std::string(ip.interfering ? "True" : "False")
|
|
+ ", pen=" + std::to_string(ip.penetration) + ")";
|
|
});
|
|
|
|
py::class_<InterferenceResult>(assy, "InterferenceResult",
|
|
R"pbdoc(
|
|
Complete interference check result.
|
|
|
|
Attributes:
|
|
has_interference: True if any pair interferes
|
|
total_pairs_checked: number of part pairs checked
|
|
interfering_pairs: number of interfering pairs
|
|
pairs: all checked pair details
|
|
conflicts: only interfering pair details (convenience)
|
|
)pbdoc")
|
|
.def_readonly("has_interference", &InterferenceResult::has_interference)
|
|
.def_readonly("total_pairs_checked", &InterferenceResult::total_pairs_checked)
|
|
.def_readonly("interfering_pairs", &InterferenceResult::interfering_pairs)
|
|
.def_readonly("pairs", &InterferenceResult::pairs)
|
|
.def_readonly("conflicts", &InterferenceResult::conflicts)
|
|
.def("summary", &InterferenceResult::summary,
|
|
"Get a human-readable summary of the interference check")
|
|
.def("__repr__", [](const InterferenceResult& r) {
|
|
return "InterferenceResult(interference="
|
|
+ std::string(r.has_interference ? "True" : "False")
|
|
+ ", conflicts=" + std::to_string(r.interfering_pairs) + ")";
|
|
});
|
|
|
|
assy.def("check_interference", &check_interference,
|
|
py::arg("assembly"), py::arg("coarse_only") = false,
|
|
R"pbdoc(
|
|
Check for interference/collisions in the entire assembly.
|
|
|
|
Uses AABB pre-filtering followed by GJK/EPA precise detection.
|
|
|
|
Args:
|
|
assembly: Assembly to check
|
|
coarse_only: if True, only do AABB coarse check (faster but may have false positives)
|
|
|
|
Returns:
|
|
InterferenceResult with detailed collision info
|
|
|
|
Example:
|
|
>>> result = assembly.check_interference(my_assy)
|
|
>>> if result.has_interference:
|
|
... for c in result.conflicts:
|
|
... print(f"{c.part_a} collides with {c.part_b}, pen={c.penetration:.3f}")
|
|
)pbdoc");
|
|
|
|
assy.def("check_pair", &check_pair,
|
|
py::arg("model_a"), py::arg("model_b"),
|
|
py::arg("transform_a"), py::arg("transform_b"),
|
|
R"pbdoc(
|
|
Check interference between two B-Rep models with world transforms.
|
|
|
|
Args:
|
|
model_a, model_b: BrepModel for each part
|
|
transform_a, transform_b: world-space transforms
|
|
|
|
Returns:
|
|
InterferencePair
|
|
)pbdoc");
|
|
|
|
assy.def("penetration_depth", &penetration_depth,
|
|
py::arg("model_a"), py::arg("model_b"),
|
|
py::arg("transform_a"), py::arg("transform_b"),
|
|
R"pbdoc(
|
|
Compute penetration depth between two interfering models (EPA).
|
|
|
|
Only call when models are confirmed interfering.
|
|
|
|
Args:
|
|
model_a, model_b: BrepModel
|
|
transform_a, transform_b: world transforms
|
|
|
|
Returns:
|
|
float: penetration depth (>= 0)
|
|
)pbdoc");
|
|
|
|
// ── Explode view ───────────────────────────────────
|
|
py::class_<ExplodeConfig>(assy, "ExplodeConfig",
|
|
R"pbdoc(
|
|
Explode view configuration.
|
|
|
|
Attributes:
|
|
factor: explosion distance multiplier (default 1.0, larger = more spread)
|
|
direction: global explosion direction (zero vector = auto-compute)
|
|
use_constraint_dirs: whether to explode along constraint directions
|
|
preserve_subassemblies: whether to keep internal subassembly structure
|
|
)pbdoc")
|
|
.def(py::init<>())
|
|
.def_readwrite("factor", &ExplodeConfig::factor,
|
|
"Explosion distance coefficient")
|
|
.def_readwrite("direction", &ExplodeConfig::direction,
|
|
"Global explosion direction (Vector3D)")
|
|
.def_readwrite("use_constraint_dirs", &ExplodeConfig::use_constraint_dirs,
|
|
"Explode along constraint directions")
|
|
.def_readwrite("preserve_subassemblies", &ExplodeConfig::preserve_subassemblies,
|
|
"Preserve subassembly internal structure")
|
|
.def("__repr__", [](const ExplodeConfig& c) {
|
|
return "ExplodeConfig(factor=" + std::to_string(c.factor) + ")";
|
|
});
|
|
|
|
py::class_<ExplodeStep>(assy, "ExplodeStep",
|
|
R"pbdoc(
|
|
Single explode step (for animation).
|
|
|
|
Attributes:
|
|
part_name: part name
|
|
original_transform: transform before explosion
|
|
exploded_transform: transform after explosion
|
|
displacement: displacement vector
|
|
)pbdoc")
|
|
.def_readonly("part_name", &ExplodeStep::part_name)
|
|
.def_readonly("original_transform", &ExplodeStep::original_transform)
|
|
.def_readonly("exploded_transform", &ExplodeStep::exploded_transform)
|
|
.def_readonly("displacement", &ExplodeStep::displacement)
|
|
.def("__repr__", [](const ExplodeStep& s) {
|
|
return "ExplodeStep(\"" + s.part_name
|
|
+ "\", disp=" + std::to_string(s.displacement.norm()) + ")";
|
|
});
|
|
|
|
py::class_<ExplodeResult>(assy, "ExplodeResult",
|
|
R"pbdoc(
|
|
Explode view result.
|
|
|
|
Attributes:
|
|
steps: list of ExplodeStep for each part (for animation)
|
|
original_bounds: AABB3D before explosion
|
|
exploded_bounds: AABB3D after explosion
|
|
)pbdoc")
|
|
.def_readonly("steps", &ExplodeResult::steps)
|
|
.def_readonly("original_bounds", &ExplodeResult::original_bounds)
|
|
.def_readonly("exploded_bounds", &ExplodeResult::exploded_bounds)
|
|
.def("max_displacement", &ExplodeResult::max_displacement,
|
|
"Get maximum displacement distance")
|
|
.def("__repr__", [](const ExplodeResult& r) {
|
|
return "ExplodeResult(steps=" + std::to_string(r.steps.size()) + ")";
|
|
});
|
|
|
|
assy.def("explode_view", &explode_view,
|
|
py::arg("assembly"), py::arg("config") = ExplodeConfig{},
|
|
R"pbdoc(
|
|
Generate an exploded view of the assembly.
|
|
|
|
Spreads parts outward along the assembly hierarchy direction.
|
|
The assembly's part transforms are modified in place.
|
|
|
|
Args:
|
|
assembly: Assembly (modified in place)
|
|
config: ExplodeConfig (optional)
|
|
|
|
Returns:
|
|
ExplodeResult with displacement information
|
|
|
|
Example:
|
|
>>> config = assembly.ExplodeConfig()
|
|
>>> config.factor = 2.0
|
|
>>> result = assembly.explode_view(my_assy, config)
|
|
>>> for step in result.steps:
|
|
... print(f"{step.part_name}: displacement {step.displacement.norm():.3f}")
|
|
)pbdoc");
|
|
|
|
assy.def("un_explode", &un_explode,
|
|
py::arg("assembly"), py::arg("result"),
|
|
R"pbdoc(
|
|
Undo an explode view, restoring original transforms.
|
|
|
|
Args:
|
|
assembly: Assembly (modified in place)
|
|
result: ExplodeResult from previous explode_view() call
|
|
)pbdoc");
|
|
|
|
assy.def("compute_explode_displacement", &compute_explode_displacement,
|
|
py::arg("node"), py::arg("parent_tf"), py::arg("factor"),
|
|
R"pbdoc(
|
|
Compute explosion displacement vector for a single node.
|
|
|
|
Args:
|
|
node: AssemblyNode
|
|
parent_tf: parent world transform (Transform3D)
|
|
factor: explosion coefficient
|
|
|
|
Returns:
|
|
Vector3D: displacement vector
|
|
)pbdoc");
|
|
}
|