From f0571dbfc2ca0a38ce29c57e74c841ebd001e5f9 Mon Sep 17 00:00:00 2001 From: azv Date: Mon, 7 Sep 2015 15:22:37 +0300 Subject: [PATCH] Improve performance of calculation of multi-translation constraint (issue #893) --- src/SketchSolver/SketchSolver_Constraint.h | 2 +- .../SketchSolver_ConstraintMulti.cpp | 4 ++ .../SketchSolver_ConstraintMulti.h | 13 +++++- .../SketchSolver_ConstraintMultiRotation.cpp | 10 ++++- ...ketchSolver_ConstraintMultiTranslation.cpp | 4 ++ src/SketchSolver/SketchSolver_Group.cpp | 41 ++++++++++++------- src/SketchSolver/SketchSolver_Group.h | 4 ++ 7 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/SketchSolver/SketchSolver_Constraint.h b/src/SketchSolver/SketchSolver_Constraint.h index 8589dc19b..084e17410 100644 --- a/src/SketchSolver/SketchSolver_Constraint.h +++ b/src/SketchSolver/SketchSolver_Constraint.h @@ -46,7 +46,7 @@ public: virtual bool remove(ConstraintPtr theConstraint = ConstraintPtr()); /// \brief Update SketchPlugin attributes using the data obtained from SolveSpace entities - void refresh(); + virtual void refresh(); /// \brief Returns the type of constraint virtual int getType() const = 0; diff --git a/src/SketchSolver/SketchSolver_ConstraintMulti.cpp b/src/SketchSolver/SketchSolver_ConstraintMulti.cpp index 92a79eb04..e68a881be 100644 --- a/src/SketchSolver/SketchSolver_ConstraintMulti.cpp +++ b/src/SketchSolver/SketchSolver_ConstraintMulti.cpp @@ -136,6 +136,9 @@ void SketchSolver_ConstraintMulti::addFeature(FeaturePtr theFeature) void SketchSolver_ConstraintMulti::adjustConstraint() { + if (myAdjusted) + return; // constraint already adjusted, don't do it once again + double aRelCoord[2] = {0.0, 0.0}; // relative coordinates of point double anAbsCoord[2] = {0.0, 0.0}; // absolute coordinates of point @@ -232,4 +235,5 @@ void SketchSolver_ConstraintMulti::adjustConstraint() } myPointsJustUpdated.clear(); + myAdjusted = true; } diff --git a/src/SketchSolver/SketchSolver_ConstraintMulti.h b/src/SketchSolver/SketchSolver_ConstraintMulti.h index cf2f16d94..bbc4992be 100644 --- a/src/SketchSolver/SketchSolver_ConstraintMulti.h +++ b/src/SketchSolver/SketchSolver_ConstraintMulti.h @@ -22,7 +22,8 @@ public: SketchSolver_ConstraintMulti(ConstraintPtr theConstraint) : SketchSolver_Constraint(theConstraint), myNumberOfObjects(0), - myNumberOfCopies(0) + myNumberOfCopies(0), + myAdjusted(false) {} virtual int getType() const @@ -38,6 +39,14 @@ public: /// \brief Adds a feature to constraint and create its analogue in SolveSpace virtual void addFeature(FeaturePtr theFeature); + /// \brief Update SketchPlugin attributes using the data obtained from SolveSpace entities + virtual void refresh() + { + myAdjusted = false; + SketchSolver_Constraint::refresh(); + } + + protected: /// \brief Converts SketchPlugin constraint to a list of SolveSpace constraints virtual void process() @@ -78,6 +87,8 @@ protected: std::set myPointsJustUpdated; ///< list of points touched by user std::set myInitialPoints; ///< list of points containing initial objects + + bool myAdjusted; ///< the constraint is already adjusted (to not do it several times) }; #endif diff --git a/src/SketchSolver/SketchSolver_ConstraintMultiRotation.cpp b/src/SketchSolver/SketchSolver_ConstraintMultiRotation.cpp index da083caf7..ed1550d41 100644 --- a/src/SketchSolver/SketchSolver_ConstraintMultiRotation.cpp +++ b/src/SketchSolver/SketchSolver_ConstraintMultiRotation.cpp @@ -126,15 +126,19 @@ void SketchSolver_ConstraintMultiRotation::process() mySlvsConstraints.push_back(aConstraint.h); } + myAdjusted = false; processEntities(anEntitiesAndCopies); adjustConstraint(); } void SketchSolver_ConstraintMultiRotation::updateLocal() { - // update angle value - myAngle = std::dynamic_pointer_cast( + double aValue = std::dynamic_pointer_cast( myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value(); + if (fabs(myAngle - aValue) > tolerance) + myAdjusted = false; + // update angle value + myAngle = aValue; } void SketchSolver_ConstraintMultiRotation::adjustConstraint() @@ -143,6 +147,8 @@ void SketchSolver_ConstraintMultiRotation::adjustConstraint() myStorage->setNeedToResolve(false); return; } + if (myAdjusted) + return; std::list aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT); std::list::const_iterator aCoIt; diff --git a/src/SketchSolver/SketchSolver_ConstraintMultiTranslation.cpp b/src/SketchSolver/SketchSolver_ConstraintMultiTranslation.cpp index 816aa8fcc..381c8641b 100644 --- a/src/SketchSolver/SketchSolver_ConstraintMultiTranslation.cpp +++ b/src/SketchSolver/SketchSolver_ConstraintMultiTranslation.cpp @@ -138,12 +138,16 @@ void SketchSolver_ConstraintMultiTranslation::process() } } + myAdjusted = false; processEntities(anEntitiesAndCopies); adjustConstraint(); } void SketchSolver_ConstraintMultiTranslation::adjustConstraint() { + if (myAdjusted) + return; + Slvs_Entity aTranslationLine = myStorage->getEntity(myTranslationLine); Slvs_hConstraint aFixed; // temporary variable // Set the translation line unchanged during constraint recalculation diff --git a/src/SketchSolver/SketchSolver_Group.cpp b/src/SketchSolver/SketchSolver_Group.cpp index 589b6a320..9371607d2 100644 --- a/src/SketchSolver/SketchSolver_Group.cpp +++ b/src/SketchSolver/SketchSolver_Group.cpp @@ -285,6 +285,29 @@ bool SketchSolver_Group::changeConstraint( } +void SketchSolver_Group::updateConstraints() +{ + std::set aPostponed; // postponed constraints Multi-Rotation and Multi-Translation + + ConstraintConstraintMap::iterator anIt = myConstraints.begin(); + for (; anIt != myConstraints.end(); ++anIt) { + if (myChangedConstraints.find(anIt->first) == myChangedConstraints.end()) + continue; + if (anIt->first->getKind() == SketchPlugin_MultiRotation::ID() || + anIt->first->getKind() == SketchPlugin_MultiTranslation::ID()) + aPostponed.insert(anIt->second); + else + anIt->second->update(); + } + + // Update postponed constraints + std::set::iterator aSCIter = aPostponed.begin(); + for (; aSCIter != aPostponed.end(); ++aSCIter) + (*aSCIter)->update(); + + myChangedConstraints.clear(); +} + bool SketchSolver_Group::updateFeature(std::shared_ptr theFeature) { if (!checkFeatureValidity(theFeature)) @@ -295,7 +318,6 @@ bool SketchSolver_Group::updateFeature(std::shared_ptr the if (aConstraints.empty()) return false; std::set::iterator aCIter = aConstraints.begin(); - std::set aPostponed; // postponed constraints Multi-Rotation and Multi-Translation for (; aCIter != aConstraints.end(); aCIter++) { ConstraintConstraintMap::iterator aSolConIter = myConstraints.find(*aCIter); if (aSolConIter == myConstraints.end() || !aSolConIter->first->data() || @@ -303,20 +325,8 @@ bool SketchSolver_Group::updateFeature(std::shared_ptr the continue; myFeatureStorage->changeFeature(theFeature, aSolConIter->first); - if (aSolConIter->first->getKind() == SketchPlugin_MultiRotation::ID() || - aSolConIter->first->getKind() == SketchPlugin_MultiTranslation::ID()) { - aPostponed.insert(aSolConIter->second); - continue; - } aSolConIter->second->addFeature(theFeature); - aSolConIter->second->update(); - } - - // Update postponed constraints - std::set::iterator aSCIter = aPostponed.begin(); - for (; aSCIter != aPostponed.end(); ++aSCIter) { - (*aSCIter)->addFeature(theFeature); - (*aSCIter)->update(); + myChangedConstraints.insert(aSolConIter->first); } return true; } @@ -454,6 +464,9 @@ bool SketchSolver_Group::updateWorkplane() // ============================================================================ bool SketchSolver_Group::resolveConstraints() { + if (!myChangedConstraints.empty()) + updateConstraints(); + bool aResolved = false; if (myStorage->isNeedToResolve() && !isEmpty()) { myConstrSolver.setGroupID(myID); diff --git a/src/SketchSolver/SketchSolver_Group.h b/src/SketchSolver/SketchSolver_Group.h index e75accd08..3197bd6da 100644 --- a/src/SketchSolver/SketchSolver_Group.h +++ b/src/SketchSolver/SketchSolver_Group.h @@ -160,12 +160,16 @@ private: /// \brief Verifies is the feature valid bool checkFeatureValidity(FeaturePtr theFeature); + /// \brief Update just changed constraints + void updateConstraints(); + private: Slvs_hGroup myID; ///< Index of the group Slvs_hEntity myWorkplaneID; ///< Index of workplane, the group is based on CompositeFeaturePtr mySketch; ///< Sketch is equivalent to workplane ConstraintConstraintMap myConstraints; ///< List of constraints std::set myTempConstraints; ///< List of temporary constraints + std::set myChangedConstraints; ///< List of just updated constraints StoragePtr myStorage; ///< Container for the set of SolveSpace constraints and their entities FeatureStoragePtr myFeatureStorage; ///< Container for the set of SketchPlugin features and their dependencies -- 2.39.2