Assembly: convert if-else to switch.

This commit is contained in:
Jacob Oursland
2025-04-16 14:12:00 -07:00
committed by Benjamin Nauck
parent f65896d403
commit c432b28ec2

View File

@@ -1091,77 +1091,82 @@ void Assembly::AssemblyObject::create_mbdSimulationParameters(App::DocumentObjec
std::shared_ptr<ASMTJoint> AssemblyObject::makeMbdJointOfType(App::DocumentObject* joint,
JointType type)
{
if (type == JointType::Fixed) {
if (bundleFixed) {
return nullptr;
}
return CREATE<ASMTFixedJoint>::With();
}
else if (type == JointType::Revolute) {
return CREATE<ASMTRevoluteJoint>::With();
}
else if (type == JointType::Cylindrical) {
return CREATE<ASMTCylindricalJoint>::With();
}
else if (type == JointType::Slider) {
return CREATE<ASMTTranslationalJoint>::With();
}
else if (type == JointType::Ball) {
return CREATE<ASMTSphericalJoint>::With();
}
else if (type == JointType::Distance) {
return makeMbdJointDistance(joint);
}
else if (type == JointType::Parallel) {
return CREATE<ASMTParallelAxesJoint>::With();
}
else if (type == JointType::Perpendicular) {
return CREATE<ASMTPerpendicularJoint>::With();
}
else if (type == JointType::Angle) {
double angle = fabs(Base::toRadians(getJointDistance(joint)));
if (fmod(angle, 2 * std::numbers::pi) < Precision::Confusion()) {
switch (type) {
case JointType::Fixed:
if (bundleFixed) {
return nullptr;
}
return CREATE<ASMTFixedJoint>::With();
case JointType::Revolute:
return CREATE<ASMTRevoluteJoint>::With();
case JointType::Cylindrical:
return CREATE<ASMTCylindricalJoint>::With();
case JointType::Slider:
return CREATE<ASMTTranslationalJoint>::With();
case JointType::Ball:
return CREATE<ASMTSphericalJoint>::With();
case JointType::Distance:
return makeMbdJointDistance(joint);
case JointType::Parallel:
return CREATE<ASMTParallelAxesJoint>::With();
}
else {
case JointType::Perpendicular:
return CREATE<ASMTPerpendicularJoint>::With();
case JointType::Angle: {
double angle = fabs(Base::toRadians(getJointDistance(joint)));
if (fmod(angle, 2 * std::numbers::pi) < Precision::Confusion()) {
return CREATE<ASMTParallelAxesJoint>::With();
}
auto mbdJoint = CREATE<ASMTAngleJoint>::With();
mbdJoint->theIzJz = angle;
return mbdJoint;
}
}
else if (type == JointType::RackPinion) {
auto mbdJoint = CREATE<ASMTRackPinionJoint>::With();
mbdJoint->pitchRadius = getJointDistance(joint);
return mbdJoint;
}
else if (type == JointType::Screw) {
int slidingIndex = slidingPartIndex(joint);
if (slidingIndex == 0) { // invalid this joint needs a slider
case JointType::RackPinion: {
auto mbdJoint = CREATE<ASMTRackPinionJoint>::With();
mbdJoint->pitchRadius = getJointDistance(joint);
return mbdJoint;
}
case JointType::Screw: {
int slidingIndex = slidingPartIndex(joint);
if (slidingIndex == 0) { // invalid this joint needs a slider
return nullptr;
}
if (slidingIndex != 1) {
swapJCS(joint); // make sure that sliding is first.
}
auto mbdJoint = CREATE<ASMTScrewJoint>::With();
mbdJoint->pitch = getJointDistance(joint);
return mbdJoint;
}
case JointType::Gears: {
auto mbdJoint = CREATE<ASMTGearJoint>::With();
mbdJoint->radiusI = getJointDistance(joint);
mbdJoint->radiusJ = getJointDistance2(joint);
return mbdJoint;
}
case JointType::Belt: {
auto mbdJoint = CREATE<ASMTGearJoint>::With();
mbdJoint->radiusI = getJointDistance(joint);
mbdJoint->radiusJ = -getJointDistance2(joint);
return mbdJoint;
}
default:
return nullptr;
}
if (slidingIndex != 1) {
swapJCS(joint); // make sure that sliding is first.
}
auto mbdJoint = CREATE<ASMTScrewJoint>::With();
mbdJoint->pitch = getJointDistance(joint);
return mbdJoint;
}
else if (type == JointType::Gears) {
auto mbdJoint = CREATE<ASMTGearJoint>::With();
mbdJoint->radiusI = getJointDistance(joint);
mbdJoint->radiusJ = getJointDistance2(joint);
return mbdJoint;
}
else if (type == JointType::Belt) {
auto mbdJoint = CREATE<ASMTGearJoint>::With();
mbdJoint->radiusI = getJointDistance(joint);
mbdJoint->radiusJ = -getJointDistance2(joint);
return mbdJoint;
}
return nullptr;
}
std::shared_ptr<ASMTJoint> AssemblyObject::makeMbdJointDistance(App::DocumentObject* joint)
@@ -1173,186 +1178,154 @@ std::shared_ptr<ASMTJoint> AssemblyObject::makeMbdJointDistance(App::DocumentObj
auto* obj1 = getLinkedObjFromRef(joint, "Reference1");
auto* obj2 = getLinkedObjFromRef(joint, "Reference2");
if (type == DistanceType::PointPoint) {
// Point to point distance, or ball joint if distance=0.
double distance = getJointDistance(joint);
if (distance < Precision::Confusion()) {
return CREATE<ASMTSphericalJoint>::With();
switch (type) {
case DistanceType::PointPoint: {
// Point to point distance, or ball joint if distance=0.
double distance = getJointDistance(joint);
if (distance < Precision::Confusion()) {
return CREATE<ASMTSphericalJoint>::With();
}
auto mbdJoint = CREATE<ASMTSphSphJoint>::With();
mbdJoint->distanceIJ = distance;
return mbdJoint;
}
auto mbdJoint = CREATE<ASMTSphSphJoint>::With();
mbdJoint->distanceIJ = distance;
return mbdJoint;
}
// Edge - edge cases
else if (type == DistanceType::LineLine) {
auto mbdJoint = CREATE<ASMTRevCylJoint>::With();
mbdJoint->distanceIJ = getJointDistance(joint);
return mbdJoint;
}
else if (type == DistanceType::LineCircle) {
auto mbdJoint = CREATE<ASMTRevCylJoint>::With();
mbdJoint->distanceIJ = getJointDistance(joint) + getEdgeRadius(obj2, elt2);
return mbdJoint;
}
else if (type == DistanceType::CircleCircle) {
auto mbdJoint = CREATE<ASMTRevCylJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getEdgeRadius(obj1, elt1) + getEdgeRadius(obj2, elt2);
return mbdJoint;
}
// TODO : other cases od edge-edge : Ellipse, parabola, hyperbola...
// Edge - edge cases
case DistanceType::LineLine: {
auto mbdJoint = CREATE<ASMTRevCylJoint>::With();
mbdJoint->distanceIJ = getJointDistance(joint);
return mbdJoint;
}
// Face - Face cases
else if (type == DistanceType::PlanePlane) {
auto mbdJoint = CREATE<ASMTPlanarJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
else if (type == DistanceType::PlaneCylinder) {
auto mbdJoint = CREATE<ASMTLineInPlaneJoint>::With();
mbdJoint->offset = getJointDistance(joint) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
else if (type == DistanceType::PlaneSphere) {
auto mbdJoint = CREATE<ASMTPointInPlaneJoint>::With();
mbdJoint->offset = getJointDistance(joint) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
else if (type == DistanceType::PlaneCone) {
// TODO
}
else if (type == DistanceType::PlaneTorus) {
auto mbdJoint = CREATE<ASMTPlanarJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
else if (type == DistanceType::CylinderCylinder) {
auto mbdJoint = CREATE<ASMTRevCylJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getFaceRadius(obj1, elt1) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
else if (type == DistanceType::CylinderSphere) {
auto mbdJoint = CREATE<ASMTCylSphJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getFaceRadius(obj1, elt1) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
else if (type == DistanceType::CylinderCone) {
// TODO
}
else if (type == DistanceType::CylinderTorus) {
auto mbdJoint = CREATE<ASMTRevCylJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getFaceRadius(obj1, elt1) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
else if (type == DistanceType::ConeCone) {
// TODO
}
else if (type == DistanceType::ConeTorus) {
// TODO
}
else if (type == DistanceType::ConeSphere) {
// TODO
}
else if (type == DistanceType::TorusTorus) {
auto mbdJoint = CREATE<ASMTPlanarJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
else if (type == DistanceType::TorusSphere) {
auto mbdJoint = CREATE<ASMTCylSphJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getFaceRadius(obj1, elt1) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
else if (type == DistanceType::SphereSphere) {
auto mbdJoint = CREATE<ASMTSphSphJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getFaceRadius(obj1, elt1) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
case DistanceType::LineCircle: {
auto mbdJoint = CREATE<ASMTRevCylJoint>::With();
mbdJoint->distanceIJ = getJointDistance(joint) + getEdgeRadius(obj2, elt2);
return mbdJoint;
}
// Point - Face cases
else if (type == DistanceType::PointPlane) {
auto mbdJoint = CREATE<ASMTPointInPlaneJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
else if (type == DistanceType::PointCylinder) {
auto mbdJoint = CREATE<ASMTCylSphJoint>::With();
mbdJoint->distanceIJ = getJointDistance(joint) + getFaceRadius(obj1, elt1);
return mbdJoint;
}
else if (type == DistanceType::PointSphere) {
auto mbdJoint = CREATE<ASMTSphSphJoint>::With();
mbdJoint->distanceIJ = getJointDistance(joint) + getFaceRadius(obj1, elt1);
return mbdJoint;
}
else if (type == DistanceType::PointCone) {
// TODO
}
else if (type == DistanceType::PointTorus) {
// TODO
}
case DistanceType::CircleCircle: {
auto mbdJoint = CREATE<ASMTRevCylJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getEdgeRadius(obj1, elt1) + getEdgeRadius(obj2, elt2);
return mbdJoint;
}
// Edge - Face cases
else if (type == DistanceType::LinePlane) {
auto mbdJoint = CREATE<ASMTLineInPlaneJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
else if (type == DistanceType::LineCylinder) {
// TODO
}
else if (type == DistanceType::LineSphere) {
// TODO
}
else if (type == DistanceType::LineCone) {
// TODO
}
else if (type == DistanceType::LineTorus) {
// TODO
}
// Face - Face cases
case DistanceType::PlanePlane: {
auto mbdJoint = CREATE<ASMTPlanarJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
else if (type == DistanceType::CurvePlane) {
// TODO
}
else if (type == DistanceType::CurveCylinder) {
// TODO
}
else if (type == DistanceType::CurveSphere) {
// TODO
}
else if (type == DistanceType::CurveCone) {
// TODO
}
else if (type == DistanceType::CurveTorus) {
// TODO
}
case DistanceType::PlaneCylinder: {
auto mbdJoint = CREATE<ASMTLineInPlaneJoint>::With();
mbdJoint->offset = getJointDistance(joint) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
// Point - Edge cases
else if (type == DistanceType::PointLine) {
auto mbdJoint = CREATE<ASMTCylSphJoint>::With();
mbdJoint->distanceIJ = getJointDistance(joint);
return mbdJoint;
}
else if (type == DistanceType::PointCurve) {
// For other curves we do a point in plane-of-the-curve.
// Maybe it would be best tangent / distance to the conic?
// For arcs and circles we could use ASMTRevSphJoint. But is it better than pointInPlane?
auto mbdJoint = CREATE<ASMTPointInPlaneJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
case DistanceType::PlaneSphere: {
auto mbdJoint = CREATE<ASMTPointInPlaneJoint>::With();
mbdJoint->offset = getJointDistance(joint) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
case DistanceType::PlaneTorus: {
auto mbdJoint = CREATE<ASMTPlanarJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
// by default we make a planar joint.
auto mbdJoint = CREATE<ASMTPlanarJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
case DistanceType::CylinderCylinder: {
auto mbdJoint = CREATE<ASMTRevCylJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getFaceRadius(obj1, elt1) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
case DistanceType::CylinderSphere: {
auto mbdJoint = CREATE<ASMTCylSphJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getFaceRadius(obj1, elt1) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
case DistanceType::CylinderTorus: {
auto mbdJoint = CREATE<ASMTRevCylJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getFaceRadius(obj1, elt1) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
case DistanceType::TorusTorus: {
auto mbdJoint = CREATE<ASMTPlanarJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
case DistanceType::TorusSphere: {
auto mbdJoint = CREATE<ASMTCylSphJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getFaceRadius(obj1, elt1) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
case DistanceType::SphereSphere: {
auto mbdJoint = CREATE<ASMTSphSphJoint>::With();
mbdJoint->distanceIJ =
getJointDistance(joint) + getFaceRadius(obj1, elt1) + getFaceRadius(obj2, elt2);
return mbdJoint;
}
// Point - Face cases
case DistanceType::PointPlane: {
auto mbdJoint = CREATE<ASMTPointInPlaneJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
case DistanceType::PointCylinder: {
auto mbdJoint = CREATE<ASMTCylSphJoint>::With();
mbdJoint->distanceIJ = getJointDistance(joint) + getFaceRadius(obj1, elt1);
return mbdJoint;
}
case DistanceType::PointSphere: {
auto mbdJoint = CREATE<ASMTSphSphJoint>::With();
mbdJoint->distanceIJ = getJointDistance(joint) + getFaceRadius(obj1, elt1);
return mbdJoint;
}
// Edge - Face cases
case DistanceType::LinePlane: {
auto mbdJoint = CREATE<ASMTLineInPlaneJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
// Point - Edge cases
case DistanceType::PointLine: {
auto mbdJoint = CREATE<ASMTCylSphJoint>::With();
mbdJoint->distanceIJ = getJointDistance(joint);
return mbdJoint;
}
case DistanceType::PointCurve: {
// For other curves we do a point in plane-of-the-curve.
// Maybe it would be best tangent / distance to the conic?
// For arcs and circles we could use ASMTRevSphJoint. But is it better than
// pointInPlane?
auto mbdJoint = CREATE<ASMTPointInPlaneJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
default: {
// by default we make a planar joint.
auto mbdJoint = CREATE<ASMTPlanarJoint>::With();
mbdJoint->offset = getJointDistance(joint);
return mbdJoint;
}
}
}
std::vector<std::shared_ptr<MbD::ASMTJoint>>