Merge main and preserve sweep history grips

This commit is contained in:
Hakan Seven committed 2026-09-02 17:50:30 +03:00
commit d93be37d35
38 files changed
+2691 -396

No files matched your search

+10
View File
@@ -274,6 +274,16 @@ impl MeshLodSet {
.map_or(self.lods.as_slice(), |source| source.lods.as_slice())
}
pub fn geometry_edges(&self) -> (&[[f32; 3]], &[[f32; 3]]) {
self.instance_source.as_ref().map_or(
(self.edge_verts.as_slice(), self.edge_verts_low.as_slice()),
|source| (
source.edge_verts.as_slice(),
source.edge_verts_low.as_slice(),
),
)
}
pub fn entity_handle(&self) -> Option<acadrust::Handle> {
self.instance_handle.or_else(|| {
self.lods
+463 -7
View File
@@ -24,6 +24,8 @@ pub const GRIP_SIDES: usize = 10_007;
pub const GRIP_MAJOR_RADIUS: usize = 10_008;
pub const GRIP_MINOR_RADIUS: usize = 10_009;
pub const GRIP_TOP_RADIUS: usize = 10_010;
pub const GRIP_DRAFT: usize = 10_012;
pub const GRIP_PROFILE_FIRST: usize = 10_200;
pub const GRIP_SWEEP_PROFILE_FIRST: usize = 20_000;
pub const GRIP_SWEEP_PATH_FIRST: usize = 30_000;
pub const GRIP_BOX_CORNER_FIRST: usize = 10_100;
@@ -60,6 +62,8 @@ pub const PROP_BANK: &str = "solid_history_bank";
pub const PROP_TWIST_ALONG_PATH: &str = "solid_history_twist_along_path";
pub const PROP_SCALE_ALONG_PATH: &str = "solid_history_scale_along_path";
pub const PROP_SWEEP_LENGTH: &str = "solid_history_sweep_length";
pub const PROP_EXTRUSION_HEIGHT: &str = "solid_history_extrusion_height";
pub const PROP_TAPER_ANGLE: &str = "solid_history_taper_angle";
pub const PROP_PYRAMID_TYPE: &str = "solid_history_pyramid_type";
pub const PROP_HISTORY: &str = "solid_history_record";
pub const PROP_SHOW_HISTORY: &str = "solid_history_show";
@@ -113,6 +117,7 @@ pub fn has_specialized_primitive_properties(
| SolidHistoryOperation::Torus(_)
| SolidHistoryOperation::Pyramid(_)
| SolidHistoryOperation::Sweep(_)
| SolidHistoryOperation::Extrusion(_)
)
)
}
@@ -128,7 +133,7 @@ pub fn reference_point(operation: &SolidHistoryOperation) -> Option<glam::DVec3>
}
SolidHistoryOperation::Cone(value) => world_point(value.base.transform, [0.0; 3]),
SolidHistoryOperation::Cylinder(value) => world_point(value.base.transform, [0.0; 3]),
SolidHistoryOperation::Sweep(value) => world_point(
SolidHistoryOperation::Sweep(value) | SolidHistoryOperation::Extrusion(value) => world_point(
value.base.transform,
[
value.reference_point.x,
@@ -142,6 +147,100 @@ pub fn reference_point(operation: &SolidHistoryOperation) -> Option<glam::DVec3>
}
}
fn extrusion_properties(
document: &acadrust::CadDocument,
handle: acadrust::Handle,
value: &SolidHistorySweep,
) -> Vec<PropSection> {
let position = world_point(
value.base.transform,
[
value.reference_point.x,
value.reference_point.y,
value.reference_point.z,
],
)
.unwrap_or(glam::DVec3::ZERO);
let (record_history, object_show_history, show_history_mode) =
history_flags(document, handle).unwrap_or((false, false, 1));
let (show_history, show_history_editable) =
displayed_history_state(object_show_history, show_history_mode);
let path_length = sweep_path_length(value);
let height_value = path_length.unwrap_or(value.end_draft_distance);
let height = if value.path_entity.is_some() {
PropValue::ReadOnly(crate::entities::common::format_length(height_value))
} else {
PropValue::EditText(crate::entities::common::format_length(height_value))
};
let taper = if value.path_entity.is_some() {
PropValue::ReadOnly(crate::entities::common::format_angle(value.draft_angle))
} else {
PropValue::EditText(crate::entities::common::format_angle(value.draft_angle))
};
vec![
PropSection {
title: t!("Geometry").into_owned(),
props: vec![
Property {
label: t!("Solid type").into_owned(),
field: "solid_history_type",
value: PropValue::ReadOnly(t!("Extrusion").into_owned()),
},
crate::entities::common::edit_prop(
t!("Position X").as_ref(),
PROP_POSITION_X,
position.x,
),
crate::entities::common::edit_prop(
t!("Position Y").as_ref(),
PROP_POSITION_Y,
position.y,
),
crate::entities::common::edit_prop(
t!("Position Z").as_ref(),
PROP_POSITION_Z,
position.z,
),
Property {
label: t!("Height").into_owned(),
field: PROP_EXTRUSION_HEIGHT,
value: height,
},
Property {
label: t!("Taper angle").into_owned(),
field: PROP_TAPER_ANGLE,
value: taper,
},
],
},
PropSection {
title: t!("Solid History").into_owned(),
props: vec![
Property {
label: t!("History").into_owned(),
field: PROP_HISTORY,
value: PropValue::Choice {
selected: if record_history { "Record" } else { "None" }.to_string(),
options: vec!["None".to_string(), "Record".to_string()],
},
},
Property {
label: t!("Show History").into_owned(),
field: PROP_SHOW_HISTORY,
value: if show_history_editable {
PropValue::Choice {
selected: if show_history { "Yes" } else { "No" }.to_string(),
options: vec!["No".to_string(), "Yes".to_string()],
}
} else {
PropValue::ReadOnly(if show_history { "Yes" } else { "No" }.to_string())
},
},
],
},
]
}
fn sweep_properties(
document: &acadrust::CadDocument,
handle: acadrust::Handle,
@@ -229,7 +328,30 @@ fn sweep_path_length(value: &SolidHistorySweep) -> Option<f64> {
use acadrust::entities::EmbeddedEntity;
use acadrust::EntityType;
let entity = match value.path_entity.as_ref()? {
let path_entity = value.path_entity.as_ref()?;
if let EmbeddedEntity::Spline(path) = path_entity {
if path.degree == 1 && path.control_points.len() >= 2 && path.weights.is_empty() {
let transform = glam::DMat4::from_cols_array(&value.path_entity_transform);
if !transform.is_finite() || transform.determinant().abs() <= 1e-12 {
return None;
}
return Some(
path.control_points
.windows(2)
.map(|pair| {
let start = transform.transform_point3(glam::DVec3::new(
pair[0].x, pair[0].y, pair[0].z,
));
let end = transform.transform_point3(glam::DVec3::new(
pair[1].x, pair[1].y, pair[1].z,
));
end.distance(start)
})
.sum(),
);
}
}
let entity = match path_entity {
EmbeddedEntity::Line(value) => EntityType::Line(value.clone()),
EmbeddedEntity::Arc(value) => EntityType::Arc(value.clone()),
EmbeddedEntity::Circle(value) => EntityType::Circle(value.clone()),
@@ -892,6 +1014,9 @@ pub fn primitive_properties(
SolidHistoryOperation::Pyramid(value) => pyramid_properties(document, handle, value),
SolidHistoryOperation::Torus(value) => torus_properties(document, handle, value),
SolidHistoryOperation::Sweep(value) => sweep_properties(document, handle, value),
SolidHistoryOperation::Extrusion(value) => {
extrusion_properties(document, handle, value)
}
_ => Vec::new(),
}
}
@@ -922,6 +1047,8 @@ pub fn is_primitive_property(field: &str) -> bool {
| PROP_PROFILE_ROTATION
| PROP_TWIST_ALONG_PATH
| PROP_SCALE_ALONG_PATH
| PROP_EXTRUSION_HEIGHT
| PROP_TAPER_ANGLE
| PROP_PYRAMID_TYPE
)
}
@@ -936,6 +1063,81 @@ pub fn is_specialized_property(field: &str) -> bool {
|| is_history_choice(field)
}
fn apply_extrusion_geometry_property(
value: &mut SolidHistorySweep,
field: &str,
text: &str,
) -> Option<bool> {
if value.path_entity.is_some()
&& matches!(field, PROP_EXTRUSION_HEIGHT | PROP_TAPER_ANGLE)
{
return Some(false);
}
match field {
PROP_POSITION_X | PROP_POSITION_Y | PROP_POSITION_Z => {
let axis = match field {
PROP_POSITION_X => 0,
PROP_POSITION_Y => 1,
_ => 2,
};
let target = crate::entities::common::parse_length(text)?;
if !target.is_finite() {
return Some(false);
}
let current = matrix(value.base.transform)?;
let reference = current.transform_point3(glam::DVec3::new(
value.reference_point.x,
value.reference_point.y,
value.reference_point.z,
));
if !reference.is_finite() || (target - reference[axis]).abs() <= 1e-12 {
return Some(false);
}
let mut delta = glam::DVec3::ZERO;
delta[axis] = target - reference[axis];
let updated = glam::DMat4::from_translation(delta) * current;
if !updated.is_finite() || updated.determinant().abs() <= 1e-12 {
return Some(false);
}
value.base.transform = updated.to_cols_array();
Some(true)
}
PROP_EXTRUSION_HEIGHT => {
let height = crate::entities::common::parse_length(text)?;
if !height.is_finite() || height <= 1e-9 {
return Some(false);
}
let direction = glam::DVec3::new(
value.direction.x,
value.direction.y,
value.direction.z,
);
let Some(unit) = direction.try_normalize() else {
return Some(false);
};
let updated = unit * height;
if (updated - direction).length_squared() <= 1e-24 {
return Some(false);
}
value.direction = acadrust::types::Vector3::new(updated.x, updated.y, updated.z);
value.end_draft_distance = height;
Some(true)
}
PROP_TAPER_ANGLE => {
let angle = crate::entities::common::parse_angle(text)?;
if !angle.is_finite()
|| angle.abs() >= std::f64::consts::FRAC_PI_2
|| (angle - value.draft_angle).abs() <= 1e-12
{
return Some(false);
}
value.draft_angle = angle;
Some(true)
}
_ => None,
}
}
pub fn apply_history_choice(
document: &mut acadrust::CadDocument,
handle: acadrust::Handle,
@@ -1365,6 +1567,13 @@ pub fn apply_primitive_property(
return applied;
}
}
if let SolidHistoryOperation::Extrusion(extrusion_value) = operation {
if let Some(applied) =
apply_extrusion_geometry_property(extrusion_value, field, value)
{
return applied;
}
}
if let SolidHistoryOperation::Torus(torus_value) = operation {
if let Some(applied) = apply_torus_position_property(torus_value, field, value) {
return applied;
@@ -1644,7 +1853,7 @@ fn local_point(transform: [f64; 16], point: glam::DVec3) -> Option<glam::DVec3>
Some(matrix(transform)?.inverse().transform_point3(point))
}
fn sweep_embedded_entity(value: &EmbeddedEntity) -> Option<EntityType> {
fn embedded_entity(value: &EmbeddedEntity) -> Option<EntityType> {
Some(match value {
EmbeddedEntity::Point(value) => EntityType::Point(value.clone()),
EmbeddedEntity::Line(value) => EntityType::Line(value.clone()),
@@ -1659,7 +1868,7 @@ fn sweep_embedded_entity(value: &EmbeddedEntity) -> Option<EntityType> {
})
}
fn into_sweep_embedded_entity(value: EntityType) -> Option<EmbeddedEntity> {
fn into_embedded_entity(value: EntityType) -> Option<EmbeddedEntity> {
Some(match value {
EntityType::Point(value) => EmbeddedEntity::Point(value),
EntityType::Line(value) => EmbeddedEntity::Line(value),
@@ -1674,13 +1883,101 @@ fn into_sweep_embedded_entity(value: EntityType) -> Option<EmbeddedEntity> {
})
}
fn profile_matrix(value: &SolidHistorySweep) -> Option<glam::DMat4> {
Some(matrix(value.base.transform)? * matrix(value.sweep_entity_transform)?)
}
fn extrusion_profile_grips(value: &SolidHistorySweep) -> (Vec<GripDef>, Option<glam::DVec3>) {
let Some(profile) = value.sweep_entity.as_ref().and_then(embedded_entity) else {
return (Vec::new(), None);
};
let Some(transform) = profile_matrix(value) else {
return (Vec::new(), None);
};
let source = profile.grips();
if source.is_empty() {
return (Vec::new(), None);
}
let mut center = glam::DVec3::ZERO;
let mut count = 0.0;
let grips = source
.into_iter()
.enumerate()
.filter_map(|(index, grip)| {
let world = transform.transform_point3(grip.world);
if !world.is_finite() {
return None;
}
center += world;
count += 1.0;
let transform_vector = |vector: glam::DVec3| {
let vector = transform.transform_vector3(vector);
(vector.length_squared() > 1e-12).then(|| vector.normalize())
};
Some(GripDef {
id: GRIP_PROFILE_FIRST + index,
world,
is_midpoint: grip.is_midpoint,
shape: grip.shape,
dir: grip.dir.and_then(transform_vector),
axis: grip.axis.and_then(transform_vector),
})
})
.collect::<Vec<_>>();
let center = (count > 0.0).then_some(center / count);
(grips, center)
}
fn apply_extrusion_profile_grip(
value: &mut SolidHistorySweep,
index: usize,
apply: GripApply,
) -> bool {
let Some(mut profile) = value.sweep_entity.as_ref().and_then(embedded_entity) else {
return false;
};
let Some(source_grip) = profile.grips().get(index).cloned() else {
return false;
};
let Some(transform) = profile_matrix(value) else {
return false;
};
let inverse = transform.inverse();
if !inverse.is_finite() {
return false;
}
let local_apply = match apply {
GripApply::Absolute(world) => {
let local = inverse.transform_point3(world);
if !local.is_finite() {
return false;
}
GripApply::Absolute(local)
}
GripApply::Translate(world_delta) => {
let local_delta = inverse.transform_vector3(world_delta);
if !local_delta.is_finite() || local_delta.length_squared() <= 1e-24 {
return false;
}
GripApply::Translate(local_delta)
}
};
profile.apply_grip(source_grip.id, local_apply);
let Some(profile) = into_embedded_entity(profile) else {
return false;
};
value.sweep_entity = Some(profile);
true
}
fn sweep_embedded_grips(
value: Option<&EmbeddedEntity>,
base_transform: [f64; 16],
entity_transform: [f64; 16],
first_id: usize,
) -> Vec<GripDef> {
let Some(entity) = value.and_then(sweep_embedded_entity) else {
let Some(entity) = value.and_then(embedded_entity) else {
return Vec::new();
};
let (Some(base), Some(entity_placement)) =
@@ -1721,7 +2018,7 @@ fn apply_sweep_embedded_grip(
index: usize,
apply: GripApply,
) -> bool {
let Some(mut entity) = value.as_ref().and_then(sweep_embedded_entity) else {
let Some(mut entity) = value.as_ref().and_then(embedded_entity) else {
return false;
};
let Some(source_grip) = entity.grips().get(index).cloned() else {
@@ -1753,13 +2050,103 @@ fn apply_sweep_embedded_grip(
}
};
entity.apply_grip(source_grip.id, apply);
let Some(entity) = into_sweep_embedded_entity(entity) else {
let Some(entity) = into_embedded_entity(entity) else {
return false;
};
*value = Some(entity);
true
}
fn extrusion_draft_grip(
value: &SolidHistorySweep,
) -> Option<(glam::DVec3, glam::DVec3, glam::DVec3, f64, f64)> {
if value.path_entity.is_some() {
return None;
}
let (profile_grips, center) = extrusion_profile_grips(value);
let center = center?;
let transform = profile_matrix(value)?;
let inverse = transform.inverse();
if !inverse.is_finite() {
return None;
}
let profile = value.sweep_entity.as_ref().and_then(embedded_entity)?;
let normal = crate::entities::curve::entity_curve(&profile)?.plane.normal()?;
let normal = transform
.transform_vector3(glam::DVec3::from_array(normal))
.try_normalize()?;
let center_local = inverse.transform_point3(center);
let anchor = profile_grips
.into_iter()
.filter_map(|grip| {
let local = inverse.transform_point3(grip.world);
let radius = (grip.world - center).length_squared();
(local.is_finite() && radius > 1e-12).then_some((grip.world, local))
})
.min_by(|(_, a), (_, b)| {
a.x.total_cmp(&b.x)
.then_with(|| {
(a.y - center_local.y)
.abs()
.total_cmp(&(b.y - center_local.y).abs())
})
.then_with(|| {
(a.z - center_local.z)
.abs()
.total_cmp(&(b.z - center_local.z).abs())
})
})?
.0;
let radial = anchor - center;
let radial_length = radial.length();
if !radial_length.is_finite() || radial_length <= 1e-6 {
return None;
}
let radial = radial / radial_length;
let base = matrix(value.base.transform)?;
let direction = base.transform_vector3(glam::DVec3::new(
value.direction.x,
value.direction.y,
value.direction.z,
));
let height = direction.dot(normal).abs();
if !direction.is_finite() || !height.is_finite() || height <= 1e-6 {
return None;
}
let draft_offset = -height * value.draft_angle.tan();
if !draft_offset.is_finite() {
return None;
}
let base_top = anchor + direction;
let world = base_top + radial * draft_offset;
world
.is_finite()
.then_some((world, base_top, radial, radial_length, height))
}
fn apply_extrusion_draft_grip(value: &mut SolidHistorySweep, apply: GripApply) -> bool {
let GripApply::Absolute(world) = apply else {
return false;
};
let Some((_, base_top, radial, profile_radius, height)) = extrusion_draft_grip(value) else {
return false;
};
let top_offset = (world - base_top).dot(radial);
if !top_offset.is_finite() {
return false;
}
let limit = 89.0_f64.to_radians();
let inward_limit = ((profile_radius - 1e-6).max(1e-6) / height)
.atan()
.min(limit);
let angle = (-top_offset).atan2(height).clamp(-limit, inward_limit);
if !angle.is_finite() || (angle - value.draft_angle).abs() <= 1e-12 {
return false;
}
value.draft_angle = angle;
true
}
fn grip(
id: usize,
world: glam::DVec3,
@@ -1995,6 +2382,32 @@ pub fn primitive_grips(
GRIP_SWEEP_PATH_FIRST,
));
}
SolidHistoryOperation::Extrusion(value) => {
let (profile_grips, profile_center) = extrusion_profile_grips(value);
grips.extend(profile_grips);
let direction = [value.direction.x, value.direction.y, value.direction.z];
if value.path_entity.is_none() {
if let (Some(center), Some(base)) = (profile_center, matrix(value.base.transform)) {
let direction = base.transform_vector3(glam::DVec3::from_array(direction));
if direction.length_squared() > 1e-12 {
grips.push(grip(
GRIP_HEIGHT,
center + direction,
GripShape::Triangle,
Some(direction.normalize()),
));
}
}
if let Some((world, _, radial, _, _)) = extrusion_draft_grip(value) {
grips.push(grip(
GRIP_DRAFT,
world,
GripShape::Triangle,
Some(radial),
));
}
}
}
_ => {}
}
grips
@@ -2028,6 +2441,14 @@ pub fn apply_primitive_grip(
);
}
}
if let SolidHistoryOperation::Extrusion(value) = operation {
if let Some(index) = grip_id.checked_sub(GRIP_PROFILE_FIRST) {
return apply_extrusion_profile_grip(value, index, apply);
}
if grip_id == GRIP_DRAFT {
return apply_extrusion_draft_grip(value, apply);
}
}
let GripApply::Absolute(world) = apply else {
return false;
};
@@ -2161,6 +2582,41 @@ pub fn apply_primitive_grip(
}
_ => return false,
},
SolidHistoryOperation::Extrusion(value) => match grip_id {
GRIP_HEIGHT => {
if value.path_entity.is_some() {
return false;
}
let (_, Some(profile_center)) = extrusion_profile_grips(value) else {
return false;
};
let Some(current) = matrix(value.base.transform) else {
return false;
};
let direction = current
.inverse()
.transform_vector3(world - profile_center);
let height = direction.length();
if !direction.is_finite() || !height.is_finite() || height <= 1e-6 {
return false;
}
let current = glam::DVec3::new(
value.direction.x,
value.direction.y,
value.direction.z,
);
if (direction - current).length_squared() <= 1e-24 {
return false;
}
value.direction = acadrust::types::Vector3::new(
direction.x,
direction.y,
direction.z,
);
value.end_draft_distance = height;
}
_ => return false,
},
_ => return false,
}
true
+415 -22
View File
@@ -3,7 +3,7 @@
use cadkernel::brep::{self, Body};
use cadkernel::geom2d::{Arc, Curve, EllipseArc, Line};
use cadkernel::space::{PlanarCurve, Plane, Vec3};
use acadrust::entities::{EmbeddedEntity, LwPolyline, LwVertex};
use acadrust::entities::{EmbeddedEntity, LwPolyline, LwVertex, Spline};
use acadrust::objects::{
SolidHistoryNodeBase, SolidHistoryOperation, SolidHistorySweep,
};
@@ -28,30 +28,185 @@ pub struct Profile {
/// is handed over as the arcs it is made of rather than as one curve with
/// nowhere for the sweep to start.
pub fn profile_of(entity: &EntityType) -> Option<Profile> {
let planar: PlanarCurve = entity_curve(entity)?;
let planar = entity_curve(entity).or_else(|| planar_polygon_entity(entity))?;
if !planar.curve.is_closed() {
return None;
}
let pieces = match &planar.curve {
Curve::Polyline(_) => planar.curve.segments(),
Curve::Circle(circle) => quarters(circle.centre, circle.radius),
Curve::Arc(arc) => split_arc(*arc),
Curve::Ellipse(arc) => split_ellipse(*arc),
Curve::Nurbs(curve) => (0..4)
.map(|part| {
curve
.trimmed(part as f64 / 4.0, (part + 1) as f64 / 4.0)
.map(Curve::Nurbs)
})
.collect::<Option<Vec<_>>>()?,
_ => return None,
};
let pieces = profile_pieces(&planar.curve, true)?;
(pieces.len() >= 3).then_some(Profile {
plane: planar.plane,
pieces,
})
}
/// An entity-level planar curve suitable for either a solid profile or an
/// open surface profile.
pub fn extrusion_profile_of(entity: &EntityType) -> Option<(Profile, bool)> {
let planar = entity_curve(entity).or_else(|| planar_polygon_entity(entity))?;
let closed = planar.curve.is_closed();
let pieces = profile_pieces(&planar.curve, closed)?;
(!pieces.is_empty()).then_some((
Profile {
plane: planar.plane,
pieces,
},
closed,
))
}
fn profile_pieces(curve: &Curve, closed: bool) -> Option<Vec<Curve>> {
let pieces = match curve {
Curve::Polyline(_) => curve.segments(),
Curve::Circle(circle) => quarters(circle.centre, circle.radius),
Curve::Arc(arc) if closed => split_arc(*arc),
Curve::Arc(arc) => vec![Curve::Arc(*arc)],
Curve::Ellipse(arc) if closed => split_ellipse(*arc),
Curve::Ellipse(arc) => vec![Curve::Ellipse(*arc)],
Curve::Line(line) => vec![Curve::Line(*line)],
Curve::Nurbs(nurbs) if closed => (0..4)
.map(|part| {
nurbs
.trimmed(part as f64 / 4.0, (part + 1) as f64 / 4.0)
.map(Curve::Nurbs)
})
.collect::<Option<Vec<_>>>()?,
Curve::Nurbs(nurbs) => vec![Curve::Nurbs(nurbs.clone())],
_ => return None,
};
Some(pieces)
}
fn planar_polygon_entity(entity: &EntityType) -> Option<PlanarCurve> {
let (points, closed) = match entity {
EntityType::Polyline3D(value) => (
value
.vertices
.iter()
.map(|vertex| [vertex.position.x, vertex.position.y, vertex.position.z])
.collect::<Vec<_>>(),
value.is_closed(),
),
EntityType::Face3D(value) => (
value
.corners()
.into_iter()
.map(|point| [point.x, point.y, point.z])
.collect::<Vec<_>>(),
true,
),
EntityType::Solid(value) => (
value
.boundary_corners()
.into_iter()
.map(|point| [point.x, point.y, point.z])
.collect::<Vec<_>>(),
true,
),
EntityType::Region(value) => (
value
.wires
.first()?
.points
.iter()
.map(|point| [point.x, point.y, point.z])
.collect::<Vec<_>>(),
true,
),
_ => return None,
};
let mut points = points;
if closed && points.len() > 2 && points.first() == points.last() {
points.pop();
}
if points.len() < if closed { 3 } else { 2 } {
return None;
}
let origin = Vec3::from(points[0]);
let first = Vec3::from(points[1]) - origin;
let normal = points[2..]
.iter()
.find_map(|point| first.cross(Vec3::from(*point) - origin).normalize())
.or_else(|| {
(!closed).then(|| first.cross(Vec3::Z).normalize().unwrap_or(Vec3::Y))
})?;
let plane = Plane::orthonormal(points[0], first.to_array(), normal.to_array())?;
let scale = points
.iter()
.flatten()
.map(|value| value.abs())
.fold(1.0_f64, f64::max);
if points
.iter()
.any(|point| !plane.contains(*point, scale * 1e-9))
{
return None;
}
let vertices = points
.iter()
.map(|point| {
plane.project(*point).map(|position| cadkernel::geom2d::PolylineVertex {
position,
bulge: 0.0,
})
})
.collect::<Option<Vec<_>>>()?;
Some(PlanarCurve::new(
plane,
Curve::Polyline(cadkernel::geom2d::Polyline { vertices, closed }),
))
}
fn region_profiles(entity: &EntityType) -> Option<(Plane, Vec<Vec<Curve>>)> {
let EntityType::Region(region) = entity else {
return None;
};
let plane = planar_polygon_entity(entity)?.plane;
let mut profiles = Vec::with_capacity(region.wires.len());
for wire in &region.wires {
let mut points = wire
.points
.iter()
.map(|point| [point.x, point.y, point.z])
.collect::<Vec<_>>();
if points.len() > 2 && points.first() == points.last() {
points.pop();
}
if points.len() < 3 {
return None;
}
let scale = points
.iter()
.flatten()
.map(|value| value.abs())
.fold(1.0_f64, f64::max);
if points
.iter()
.any(|point| !plane.contains(*point, scale * 1e-9))
{
return None;
}
let vertices = points
.iter()
.map(|point| {
plane
.project(*point)
.map(|position| cadkernel::geom2d::PolylineVertex {
position,
bulge: 0.0,
})
})
.collect::<Option<Vec<_>>>()?;
profiles.push(
Curve::Polyline(cadkernel::geom2d::Polyline {
vertices,
closed: true,
})
.segments(),
);
}
(!profiles.is_empty()).then_some((plane, profiles))
}
/// A circle as four arcs.
fn quarters(centre: [f64; 2], radius: f64) -> Vec<Curve> {
use std::f64::consts::FRAC_PI_2;
@@ -103,10 +258,112 @@ fn split_ellipse(arc: EllipseArc) -> Vec<Curve> {
pub fn extruded(entity: &EntityType, height: f64) -> Option<Body> {
let profile = profile_of(entity)?;
let normal = profile.plane.normal()?;
brep::extrude(
extruded_direction(
entity,
[normal[0] * height, normal[1] * height, normal[2] * height],
0.0,
)
}
pub fn extruded_direction(
entity: &EntityType,
direction: [f64; 3],
taper_angle: f64,
) -> Option<Body> {
if let Some((plane, profiles)) =
region_profiles(entity).filter(|(_, profiles)| profiles.len() > 1)
{
if taper_angle.abs() > 1e-12 {
return None;
}
return brep::extrude_region(plane, &profiles, direction);
}
let profile = profile_of(entity)?;
brep::extrude_tapered(profile.plane, &profile.pieces, direction, taper_angle)
}
pub fn extruded_surface(
entity: &EntityType,
direction: [f64; 3],
taper_angle: f64,
) -> Option<Body> {
if matches!(entity, EntityType::Region(region) if region.wires.len() > 1) {
return None;
}
let (profile, _) = extrusion_profile_of(entity)?;
brep::extrude_surface_tapered(
profile.plane,
&profile.pieces,
[normal[0] * height, normal[1] * height, normal[2] * height],
direction,
taper_angle,
)
}
pub fn extruded_along_path(
entity: &EntityType,
path: &EntityType,
taper_angle: f64,
) -> Option<Body> {
if matches!(entity, EntityType::Region(region) if region.wires.len() > 1) {
if taper_angle.abs() > 1e-12 {
return None;
}
return extruded_direction(entity, straight_path_direction(path)?, 0.0);
}
let profile = profile_of(entity)?;
if let EntityType::Polyline3D(path) = path {
let points = path
.vertices
.iter()
.map(|vertex| [vertex.position.x, vertex.position.y, vertex.position.z])
.collect::<Vec<_>>();
if taper_angle.abs() > 1e-12 {
if points.len() != 2 {
return None;
}
return brep::extrude_tapered(
profile.plane,
&profile.pieces,
(Vec3::from(points[1]) - Vec3::from(points[0])).to_array(),
taper_angle,
);
}
return brep::sweep_along_polyline3d(profile.plane, &profile.pieces, &points);
}
if taper_angle.abs() <= 1e-12 {
return swept(entity, path);
}
let curve = entity_curve(path)?;
let Curve::Line(line) = curve.curve else {
return None;
};
let direction = Vec3::from(curve.plane.point_at(line.end))
- Vec3::from(curve.plane.point_at(line.start));
brep::extrude_tapered(
profile.plane,
&profile.pieces,
direction.to_array(),
taper_angle,
)
}
pub fn straight_path_direction(path: &EntityType) -> Option<[f64; 3]> {
if let EntityType::Polyline3D(path) = path {
if path.vertices.len() != 2 {
return None;
}
let start = &path.vertices[0].position;
let end = &path.vertices[1].position;
return Some([end.x - start.x, end.y - start.y, end.z - start.z]);
}
let curve = entity_curve(path)?;
let Curve::Line(line) = curve.curve else {
return None;
};
Some(
(Vec3::from(curve.plane.point_at(line.end))
- Vec3::from(curve.plane.point_at(line.start)))
.to_array(),
)
}
@@ -142,13 +399,40 @@ pub fn revolved(
/// SWEEP as an exact B-rep along straight and circular path pieces.
pub fn swept(profile: &EntityType, path: &EntityType) -> Option<Body> {
let profile = profile_of(profile)?;
let path = entity_curve(path)?;
let mut path = entity_curve(path)?;
let pieces = match &path.curve {
Curve::Line(line) => vec![Curve::Line(*line)],
Curve::Arc(arc) => vec![Curve::Arc(*arc)],
Curve::Circle(circle) => quarters(circle.centre, circle.radius),
Curve::Polyline(_) => path.curve.segments(),
Curve::Ellipse(_) | Curve::Nurbs(_) => {
let scale = path
.curve
.point_at(0.0)
.into_iter()
.chain(path.curve.point_at(1.0))
.map(f64::abs)
.fold(1.0_f64, f64::max);
path.curve
.tessellate_within(scale * 1e-4)
.windows(2)
.filter(|pair| pair[0] != pair[1])
.map(|pair| Curve::Line(Line {
start: pair[0],
end: pair[1],
}))
.collect()
}
_ => return None,
};
let profile_center = profile
.pieces
.iter()
.map(|piece| Vec3::from(profile.plane.point_at(piece.point_at(0.0))))
.fold(Vec3::ZERO, |sum, point| sum + point)
/ profile.pieces.len() as f64;
let path_start = Vec3::from(path.plane.point_at(path.curve.point_at(0.0)));
path.plane.origin = (Vec3::from(path.plane.origin) + profile_center - path_start).to_array();
brep::sweep_along(profile.plane, &profile.pieces, path.plane, &pieces)
}
@@ -174,7 +458,7 @@ pub enum PolysolidJustification {
Right,
}
fn embedded_path(entity: &EntityType) -> Option<EmbeddedEntity> {
pub fn embedded_path(entity: &EntityType) -> Option<EmbeddedEntity> {
match entity {
EntityType::Line(value) => Some(EmbeddedEntity::Line(value.clone())),
EntityType::Arc(value) => Some(EmbeddedEntity::Arc(value.clone())),
@@ -182,24 +466,133 @@ fn embedded_path(entity: &EntityType) -> Option<EmbeddedEntity> {
EntityType::Ellipse(value) => Some(EmbeddedEntity::Ellipse(value.clone())),
EntityType::LwPolyline(value) => Some(EmbeddedEntity::LwPolyline(value.clone())),
EntityType::Spline(value) => Some(EmbeddedEntity::Spline(value.clone())),
EntityType::Polyline3D(value) if !value.is_closed() && value.vertices.len() >= 2 => {
let mut spline = Spline::from_control_points(
1,
value
.vertices
.iter()
.map(|vertex| vertex.position)
.collect(),
);
spline.flags.linear = true;
spline.flags.planar = false;
Some(EmbeddedEntity::Spline(spline))
}
_ => None,
}
}
fn embedded_planar_entity(entity: &EntityType) -> Option<(EmbeddedEntity, [f64; 16])> {
if matches!(entity, EntityType::Region(region) if region.wires.len() > 1) {
return None;
}
if !matches!(entity, EntityType::Polyline3D(_)) {
if let Some(embedded) = embedded_path(entity) {
return Some((embedded, glam::DMat4::IDENTITY.to_cols_array()));
}
}
let (profile, closed) = extrusion_profile_of(entity)?;
let mut polyline = LwPolyline::new();
for piece in &profile.pieces {
let Curve::Line(line) = piece else {
return None;
};
polyline
.vertices
.push(LwVertex::new(Vector2::new(line.start[0], line.start[1])));
}
if !closed {
let Curve::Line(last) = profile.pieces.last()? else {
return None;
};
polyline
.vertices
.push(LwVertex::new(Vector2::new(last.end[0], last.end[1])));
}
polyline.is_closed = closed;
let normal = profile.plane.normal()?;
let transform = glam::DMat4::from_cols(
glam::DVec4::new(
profile.plane.x_axis[0],
profile.plane.x_axis[1],
profile.plane.x_axis[2],
0.0,
),
glam::DVec4::new(
profile.plane.y_axis[0],
profile.plane.y_axis[1],
profile.plane.y_axis[2],
0.0,
),
glam::DVec4::new(normal[0], normal[1], normal[2], 0.0),
glam::DVec4::new(
profile.plane.origin[0],
profile.plane.origin[1],
profile.plane.origin[2],
1.0,
),
)
.to_cols_array();
Some((EmbeddedEntity::LwPolyline(polyline), transform))
}
pub fn extrusion_history(
profile: &EntityType,
path: Option<&EntityType>,
direction: [f64; 3],
taper_angle: f64,
reference_point: [f64; 3],
) -> Option<SolidHistoryOperation> {
let signed_height = Vec3::from(direction).length();
let mut base = SolidHistoryNodeBase::new(1);
base.transform = glam::DMat4::IDENTITY.to_cols_array();
let (sweep_entity, sweep_entity_transform) = embedded_planar_entity(profile)?;
let (path_entity, path_entity_transform) = match path {
Some(path) => {
let (entity, transform) = embedded_planar_entity(path)?;
(Some(entity), transform)
}
None => (None, glam::DMat4::IDENTITY.to_cols_array()),
};
Some(SolidHistoryOperation::Extrusion(SolidHistorySweep {
base,
operation_major: 1,
direction: Vector3::new(direction[0], direction[1], direction[2]),
sweep_entity: Some(sweep_entity),
path_entity,
draft_angle: taper_angle,
start_draft_distance: 0.0,
end_draft_distance: signed_height,
scale_factor: 1.0,
sweep_entity_transform,
path_entity_transform,
reference_point: Vector3::new(
reference_point[0],
reference_point[1],
reference_point[2],
),
..SolidHistorySweep::default()
}))
}
pub fn sweep_history(
profile: &EntityType,
path: &EntityType,
taper_angle: f64,
reference_point: [f64; 3],
) -> Option<SolidHistoryOperation> {
let mut base = SolidHistoryNodeBase::new(1);
base.transform = glam::DMat4::IDENTITY.to_cols_array();
let (sweep_entity, sweep_entity_transform) = embedded_planar_entity(profile)?;
Some(SolidHistoryOperation::Sweep(SolidHistorySweep {
base,
operation_major: 1,
sweep_entity: Some(embedded_path(profile)?),
sweep_entity: Some(sweep_entity),
path_entity: Some(embedded_path(path)?),
draft_angle: taper_angle,
scale_factor: 1.0,
sweep_entity_transform: glam::DMat4::IDENTITY.to_cols_array(),
sweep_entity_transform,
path_entity_transform: glam::DMat4::IDENTITY.to_cols_array(),
reference_point: Vector3::new(
reference_point[0],