diff --git a/CHANGELOG.md b/CHANGELOG.md index 547b10d0cd..4d2e60da7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ been added to these forces to provide access to concrete path types (e.g., `updP `getGeometryPath`, `updGeometryPath`, etc., or a suitable alternative. - Fixed a minor memory leak when calling `OpenSim::CoordinateCouplerConstraint::setFunction` (#3541) - Increase the number of input dimensions supported by `MultivariatePolynomialFunction` to 6 (#3386) +- Added `Assertion.h` and associated `OPENSIM_ASSERT*` macros (#3531) +- Replaced uses of `assert` with `OPENSIM_ASSERT`, so that assertion may be configured via cmake in the future, and + so that OpenSim (esp. debug builds) throw instead of terminating the process (#3531) - Fixed mis-indexing into an `OpenSim::ObjectProperty` now throws an exception rather than segfaulting (#3347) - `PointToPointSpring` now throws an exception on finalizing connections if both sides of the spring are connected to the same base frame (#3485) diff --git a/OpenSim/Actuators/CoordinateActuator.cpp b/OpenSim/Actuators/CoordinateActuator.cpp index 143e7e5e19..17c473dfde 100644 --- a/OpenSim/Actuators/CoordinateActuator.cpp +++ b/OpenSim/Actuators/CoordinateActuator.cpp @@ -25,6 +25,7 @@ //============================================================================== // INCLUDES //============================================================================== +#include #include #include #include @@ -206,7 +207,7 @@ void CoordinateActuator::computeForce( const SimTK::State& s, double CoordinateActuator:: getSpeed( const SimTK::State& s) const { - assert(_coord); + OPENSIM_ASSERT_FRMOBJ(_coord != nullptr); return _coord->getSpeedValue(s); }; diff --git a/OpenSim/Auxiliary/auxiliaryTestMuscleFunctions.h b/OpenSim/Auxiliary/auxiliaryTestMuscleFunctions.h index e56c62d2aa..7076c6eb68 100644 --- a/OpenSim/Auxiliary/auxiliaryTestMuscleFunctions.h +++ b/OpenSim/Auxiliary/auxiliaryTestMuscleFunctions.h @@ -22,6 +22,7 @@ * limitations under the License. * * -------------------------------------------------------------------------- */ +#include "OpenSim/Common/Assertion.h" #include "OpenSim/Simulation/Model/ActivationFiberLengthMuscle.h" /** A debugging utility for investigating muscle equilibrium failures. @@ -40,7 +41,7 @@ void reportTendonAndFiberForcesAcrossFiberLengths(const T& muscle, { // should only be using this utility for equilibrium muscles // with a compliant tendon - assert(!muscle.get_ignore_tendon_compliance()); + OPENSIM_ASSERT(!muscle.get_ignore_tendon_compliance()); SimTK::State s = state; diff --git a/OpenSim/Common/AbstractProperty.cpp b/OpenSim/Common/AbstractProperty.cpp index 41b6cf8715..68ab67521b 100644 --- a/OpenSim/Common/AbstractProperty.cpp +++ b/OpenSim/Common/AbstractProperty.cpp @@ -24,6 +24,7 @@ // INCLUDES //============================================================================ #include "AbstractProperty.h" +#include "Assertion.h" #include "Object.h" #include @@ -205,7 +206,7 @@ void AbstractProperty::writeToXMLParentElement(Xml::Element& parent) const { if (!isOneObjectProperty()) { // Concrete property will be represented by an Xml element of // the form value(s) . - assert(!getName().empty()); + OPENSIM_ASSERT(!getName().empty()); Xml::Element propElement(getName()); writeToXMLElement(propElement); parent.insertNodeAfter(parent.node_end(), propElement); diff --git a/OpenSim/Common/AbstractProperty.h b/OpenSim/Common/AbstractProperty.h index 3853606275..1f2cc117b9 100644 --- a/OpenSim/Common/AbstractProperty.h +++ b/OpenSim/Common/AbstractProperty.h @@ -24,7 +24,7 @@ * -------------------------------------------------------------------------- */ // INCLUDES -#include +#include "Assertion.h" #include #include #include "osimCommonDLL.h" @@ -79,13 +79,13 @@ class OSIMCOMMON_API AbstractProperty { /** Require that the number of values n in the value list of this property be in the range aMin <= n <= aMax. */ void setAllowableListSize(int aMin, int aMax) - { assert(0 <= aMin && aMin <= aMax); + { OPENSIM_ASSERT(0 <= aMin && aMin <= aMax); _minListSize = aMin; _maxListSize = aMax; } /** Require that the number of values n in the value list of this property be exactly n=aNum values. **/ void setAllowableListSize(int aNum) - { assert(aNum >= 1); _minListSize = _maxListSize = aNum; } + { OPENSIM_ASSERT(aNum >= 1); _minListSize = _maxListSize = aNum; } // Default copy constructor and copy assignment operator. diff --git a/OpenSim/Common/Assertion.cpp b/OpenSim/Common/Assertion.cpp new file mode 100644 index 0000000000..aefb263dd5 --- /dev/null +++ b/OpenSim/Common/Assertion.cpp @@ -0,0 +1,41 @@ +/* ------------------------------------------------------------------------- * +* OpenSim: Assertion.cpp * +* -------------------------------------------------------------------------- * +* The OpenSim API is a toolkit for musculoskeletal modeling and simulation. * +* See http://opensim.stanford.edu and the NOTICE file for more information. * +* OpenSim is developed at Stanford University and supported by the US * +* National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA * +* through the Warrior Web program. * +* * +* Copyright (c) 2005-2023 Stanford University and the Authors * +* Author(s): Adam Kewley * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); you may * +* not use this file except in compliance with the License. You may obtain a * +* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +* -------------------------------------------------------------------------- */ + +#include "Assertion.h" + +#include + +void OpenSim::OnAssertionError( + char const* failingCode, + char const* failingFile, + char const* failingFunction, + unsigned int failingLine, + Object const* maybeSourceObject) +{ + if (maybeSourceObject) { + throw Exception{failingFile, failingLine, failingFunction, *maybeSourceObject, failingCode}; + } + else { + throw Exception{failingFile, failingLine, failingFunction, failingCode}; + } +} diff --git a/OpenSim/Common/Assertion.h b/OpenSim/Common/Assertion.h new file mode 100644 index 0000000000..5243c97114 --- /dev/null +++ b/OpenSim/Common/Assertion.h @@ -0,0 +1,74 @@ +#ifndef OPENSIM_ASSERTION_H_ +#define OPENSIM_ASSERTION_H_ + +/* ------------------------------------------------------------------------- * +* OpenSim: Assertion.h * +* -------------------------------------------------------------------------- * +* The OpenSim API is a toolkit for musculoskeletal modeling and simulation. * +* See http://opensim.stanford.edu and the NOTICE file for more information. * +* OpenSim is developed at Stanford University and supported by the US * +* National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA * +* through the Warrior Web program. * +* * +* Copyright (c) 2005-2023 Stanford University and the Authors * +* Author(s): Adam Kewley * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); you may * +* not use this file except in compliance with the License. You may obtain a * +* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +* -------------------------------------------------------------------------- */ + +#include "osimCommonDLL.h" + +namespace OpenSim { + + class Object; + + // a function that is called whenever an assertion fails + // + // callers may assume that the implementation halts forward progression of + // the caller by either throwing an exception or terminating the process + [[noreturn]] OSIMCOMMON_API void OnAssertionError( + char const* failingCode, + char const* failingFile, + char const* failingFunction, + unsigned int failingLine, + Object const* maybeSourceObject = nullptr + ); +} + +/** + * @name Macros to assert that expressions are true + * + * The purpose of these macros is to assert that preconditions are met within the + * source code of OpenSim. If the precondition is not met, and the macros are enabled, + * then the implementation will halt forward progression of the calling thread, either + * by throwing an exception or terminating the process. + */ + +// always ensures `expr` converts to `true` or otherwise halts forward progression of the calling thread +#define OPENSIM_ASSERT_ALWAYS(expr) \ + static_cast(expr) ? static_cast(0) : OpenSim::OnAssertionError(#expr, __FILE__, __func__, __LINE__) + +// always ensures `expr` converts to `true` or otherwise halts forward progression of the calling thread +// and passes `this` to the error handler (for better error messages) +#define OPENSIM_ASSERT_FRMOBJ_ALWAYS(expr) \ + static_cast(expr) ? static_cast(0) : OpenSim::OnAssertionError(#expr, __FILE__, __func__, __LINE__, this) + +// these macros behave identially to their `_ALWAYS` counterparts, but may be conditionally +// toggled by compile-time flags +#if defined(NDEBUG) +#define OPENSIM_ASSERT(expr) +#define OPENSIM_ASSERT_FRMOBJ(expr) +#else +#define OPENSIM_ASSERT(expr) OPENSIM_ASSERT_ALWAYS(expr) +#define OPENSIM_ASSERT_FRMOBJ(expr) OPENSIM_ASSERT_FRMOBJ_ALWAYS(expr) +#endif + +#endif // OPENSIM_ASSERTION_H_ diff --git a/OpenSim/Common/DataTable.h b/OpenSim/Common/DataTable.h index 27985b9a3d..b059508bf2 100644 --- a/OpenSim/Common/DataTable.h +++ b/OpenSim/Common/DataTable.h @@ -29,6 +29,7 @@ This file defines the DataTable_ class, which is used by OpenSim to provide an in-memory container for data access and manipulation. */ #include "AbstractDataTable.h" +#include "Assertion.h" #include "FileAdapter.h" #include "SimTKcommon/internal/BigMatrix.h" #include "SimTKcommon/internal/Quaternion.h" @@ -959,7 +960,7 @@ class DataTable_ : public AbstractDataTable { // get copy of labels auto labels = getColumnLabels(); - assert(labels.size() == _depData.ncol()); + OPENSIM_ASSERT(labels.size() == _depData.ncol()); // shift columns unless we're already at the last column for (size_t c = index; c < getNumColumns()-1; ++c) { diff --git a/OpenSim/Common/DebugUtilities.cpp b/OpenSim/Common/DebugUtilities.cpp index 8e2f0db69d..f14df2d52c 100644 --- a/OpenSim/Common/DebugUtilities.cpp +++ b/OpenSim/Common/DebugUtilities.cpp @@ -22,8 +22,8 @@ #include "DebugUtilities.h" #include "Logger.h" -#include #include +#include #include #include #include @@ -38,8 +38,7 @@ void Fatal_Error(const char* msg, const char* function, const char* file, "line = {})", msg, function, file, line); log_critical(str); throw std::runtime_error(str); - assert(false); - exit(1); + std::terminate(); } /** diff --git a/OpenSim/Common/Exception.cpp b/OpenSim/Common/Exception.cpp index 70d5491173..74131e110d 100644 --- a/OpenSim/Common/Exception.cpp +++ b/OpenSim/Common/Exception.cpp @@ -29,7 +29,6 @@ // INCLUDES #include #include -#include #include "osimCommonDLL.h" #include "Exception.h" #include "IO.h" @@ -58,12 +57,6 @@ exception() setMessage(aMsg); _file = aFile; _line = aLine; - -// make it assert false when debugging... -//#ifndef NDEBUG -// print(cout); -// assert(false); -//#endif } namespace { diff --git a/OpenSim/Common/Object.cpp b/OpenSim/Common/Object.cpp index a120053e6d..78623c8861 100644 --- a/OpenSim/Common/Object.cpp +++ b/OpenSim/Common/Object.cpp @@ -32,6 +32,7 @@ #include "Object.h" +#include "Assertion.h" #include "Exception.h" #include "IO.h" #include "Logger.h" @@ -1663,7 +1664,7 @@ void Object::setObjectIsUpToDateWithProperties() void Object::updateFromXMLDocument() { - assert(_document != nullptr); + OPENSIM_ASSERT_FRMOBJ(_document != nullptr); SimTK::Xml::Element e = _document->getRootDataElement(); IO::CwdChanger cwd = IO::CwdChanger::changeToParentOf(_document->getFileName()); diff --git a/OpenSim/Common/Object.h b/OpenSim/Common/Object.h index 45cae4c2b9..8618fa7279 100644 --- a/OpenSim/Common/Object.h +++ b/OpenSim/Common/Object.h @@ -35,13 +35,13 @@ // INCLUDES +#include "Assertion.h" #include "osimCommonDLL.h" #include "PropertySet.h" #include "PropertyTable.h" #include "Property.h" #include -#include // DISABLES MULTIPLE INSTANTIATION WARNINGS @@ -1345,7 +1345,7 @@ ObjectProperty::isEqualTo(const AbstractProperty& other) const { // Property_Deprecated implementation can't copy this flag right. if (this->getValueIsDefault() != other.getValueIsDefault()) return false; - assert(this->size() == other.size()); // base class checked + OPENSIM_ASSERT(this->size() == other.size()); // base class checked const ObjectProperty& otherO = ObjectProperty::getAs(other); for (int i=0; i::readFromXMLElement // Create an Object of the element tag's type. Object* object = Object::newInstanceOfType(objTypeTag); - assert(object); // we just checked above + OPENSIM_ASSERT(object); // we just checked above object->readObjectFromXMLNodeOrFile(*iter, versionNumber); T* objectT = dynamic_cast(object); - assert(objectT); // should have worked by construction + OPENSIM_ASSERT(objectT); // should have worked by construction adoptAndAppendValueVirtual(objectT); // don't copy } diff --git a/OpenSim/Common/Property.h b/OpenSim/Common/Property.h index d408e3bbb7..f0fe6eb7cf 100644 --- a/OpenSim/Common/Property.h +++ b/OpenSim/Common/Property.h @@ -25,6 +25,7 @@ // INCLUDES #include "AbstractProperty.h" +#include "Assertion.h" #include "Exception.h" #include "Logger.h" @@ -877,7 +878,7 @@ class SimpleProperty : public Property { // Property_Deprecated implementation can't copy this flag right. if (this->getValueIsDefault() != other.getValueIsDefault()) return false; - assert(this->size() == other.size()); // base class checked + OPENSIM_ASSERT(this->size() == other.size()); // base class checked const SimpleProperty& otherS = SimpleProperty::getAs(other); for (int i=0; i::TypeHelper::isEqual(values[i], otherS.values[i])) diff --git a/OpenSim/Common/PropertyDblVec.h b/OpenSim/Common/PropertyDblVec.h index cfbd2f3ea6..98e1f11a96 100644 --- a/OpenSim/Common/PropertyDblVec.h +++ b/OpenSim/Common/PropertyDblVec.h @@ -27,6 +27,7 @@ #pragma warning( disable : 4251 ) #endif +#include "Assertion.h" #include "osimCommonDLL.h" #include #include "Property_Deprecated.h" @@ -120,7 +121,7 @@ template class PropertyDblVec_ : public Property_Deprecated using Property_Deprecated::setValue; /** set value of this property from an array of doubles of equal or greater length */ void setValue(const Array &anArray) override { - assert(anArray.getSize() >= M); + OPENSIM_ASSERT(anArray.getSize() >= M); for(int i=0;i class PropertyDblVec_ : public Property_Deprecated const SimTK::Vec& getValueDblVec() const {return SimTK::Vec::getAs(&_dblvec[0]); }; /** set value from double array */ // to be used by the serialization code void setValue(int aSize, const double aArray[]) override { - assert(aSize == M); + OPENSIM_ASSERT(aSize == M); setValue(SimTK::Vec::getAs(aArray)); }; #ifndef SWIG diff --git a/OpenSim/Common/PropertyObj.h b/OpenSim/Common/PropertyObj.h index 77b8894fed..2e245868e1 100644 --- a/OpenSim/Common/PropertyObj.h +++ b/OpenSim/Common/PropertyObj.h @@ -29,6 +29,7 @@ // INCLUDES +#include "Assertion.h" #include "osimCommonDLL.h" #include #include "Object.h" @@ -74,11 +75,11 @@ class OSIMCOMMON_API PropertyObj : public Property_Deprecated bool isAcceptableObjectTag (const std::string& objectTypeTag) const override {return true;} const Object& getValueAsObject(int index=-1) const override - { assert(index <= 0); return getValueObj(); } + { OPENSIM_ASSERT(index <= 0); return getValueObj(); } Object& updValueAsObject(int index=-1) override - { assert(index <= 0); return getValueObj(); } + { OPENSIM_ASSERT(index <= 0); return getValueObj(); } void setValueAsObject(const Object& obj, int index=-1) override - { assert(index <= 0); delete _value; _value=obj.clone(); } + { OPENSIM_ASSERT(index <= 0); delete _value; _value=obj.clone(); } //-------------------------------------------------------------------------- // OPERATORS diff --git a/OpenSim/Common/PropertyObjPtr.h b/OpenSim/Common/PropertyObjPtr.h index 23115ffd76..1f5134a204 100644 --- a/OpenSim/Common/PropertyObjPtr.h +++ b/OpenSim/Common/PropertyObjPtr.h @@ -24,6 +24,7 @@ * -------------------------------------------------------------------------- */ // INCLUDES +#include "Assertion.h" #include "osimCommonDLL.h" #include #include "Object.h" @@ -88,11 +89,11 @@ template class PropertyObjPtr : public Property_Deprecated bool isAcceptableObjectTag (const std::string& objectTypeTag) const override {return true;} const Object& getValueAsObject(int index=-1) const override - { assert(index <= 0); return *_value; } + { OPENSIM_ASSERT(index <= 0); return *_value; } Object& updValueAsObject(int index=-1) override - { assert(index <= 0); return *_value; } + { OPENSIM_ASSERT(index <= 0); return *_value; } void setValueAsObject(const Object& obj, int index=-1) override - { assert(index <= 0); setValue(obj.clone()); } + { OPENSIM_ASSERT(index <= 0); setValue(obj.clone()); } //-------------------------------------------------------------------------- // OPERATORS diff --git a/OpenSim/Common/PropertyTable.cpp b/OpenSim/Common/PropertyTable.cpp index 11cd0f695c..cac2d40f55 100644 --- a/OpenSim/Common/PropertyTable.cpp +++ b/OpenSim/Common/PropertyTable.cpp @@ -25,6 +25,8 @@ //============================================================================ #include "PropertyTable.h" +#include "Assertion.h" + using namespace OpenSim; using namespace SimTK; @@ -77,12 +79,12 @@ bool PropertyTable::equals(const PropertyTable& other) const { int PropertyTable::adoptProperty(AbstractProperty* prop) { - assert(prop); + OPENSIM_ASSERT(prop != nullptr); const int nxtIndex = properties.size(); const std::string& name = prop->getName(); // Unnamed property should have had its Object class name used as its name. - assert(!name.empty()); + OPENSIM_ASSERT(!name.empty()); if (hasProperty(name)) throw OpenSim::Exception diff --git a/OpenSim/Common/PropertyTransform.cpp b/OpenSim/Common/PropertyTransform.cpp index 4d2841dd08..19be7b2638 100644 --- a/OpenSim/Common/PropertyTransform.cpp +++ b/OpenSim/Common/PropertyTransform.cpp @@ -30,6 +30,7 @@ //============================================================================ #include "PropertyTransform.h" +#include "Assertion.h" @@ -64,7 +65,7 @@ PropertyTransform(const string &aName, PropertyDblArray(aName, aArray) { setType(Transform); - assert(aArray.getSize()==6); + OPENSIM_ASSERT(aArray.getSize()==6); _transform.updR().setRotationToBodyFixedXYZ(SimTK::Vec3::getAs(&aArray[0])); _transform.updP() = SimTK::Vec3::getAs(&aArray[3]); setAllowableListSize(6); @@ -177,7 +178,7 @@ setValue(const SimTK::Transform &aTransform) void PropertyTransform:: setValue(int aSize,const double aArray[]) { - assert(aSize==6); + OPENSIM_ASSERT(aSize==6); PropertyDblArray::setValue(aSize, aArray); _transform.updR().setRotationToBodyFixedXYZ(SimTK::Vec3::getAs(&aArray[0])); _transform.updP() = SimTK::Vec3::getAs(&aArray[3]); diff --git a/OpenSim/Common/Property_Deprecated.h b/OpenSim/Common/Property_Deprecated.h index 70e5fd7b4e..884998e060 100644 --- a/OpenSim/Common/Property_Deprecated.h +++ b/OpenSim/Common/Property_Deprecated.h @@ -29,6 +29,7 @@ // INCLUDES +#include "Assertion.h" #include "osimCommonDLL.h" #include "AbstractProperty.h" #include "Exception.h" @@ -146,15 +147,15 @@ class OSIMCOMMON_API Property_Deprecated : public AbstractProperty virtual void readFromXMLElement (SimTK::Xml::Element& propertyElement, int versionNumber) override - {assert(!"Property_Deprecated::readFromXMLElement not implemented");} + {OPENSIM_ASSERT(!"Property_Deprecated::readFromXMLElement not implemented");} virtual void writeToXMLElement (SimTK::Xml::Element& propertyElement) const override - {assert(!"Property_Deprecated::writeToXMLElement not implemented");} + {OPENSIM_ASSERT(!"Property_Deprecated::writeToXMLElement not implemented");} // Override for array types. int getNumValues() const override {return 1;} - void clearValues() override {assert(!"implemented");} + void clearValues() override {OPENSIM_ASSERT(!"implemented");} bool isUnnamedProperty() const override {return false;} bool isObjectProperty() const override {return false;} diff --git a/OpenSim/Common/Storage.cpp b/OpenSim/Common/Storage.cpp index 9758ebad35..798f0da652 100644 --- a/OpenSim/Common/Storage.cpp +++ b/OpenSim/Common/Storage.cpp @@ -29,6 +29,7 @@ // INCLUDES #include "Storage.h" +#include "Assertion.h" #include "CommonUtilities.h" #include "GCVSpline.h" #include "GCVSplineSet.h" @@ -2675,7 +2676,7 @@ void Storage::interpolateAt(const Array &targetTimes) void Storage:: setOutputFileName(const std::string& aFileName) { - assert(_fileName==""); + OPENSIM_ASSERT_FRMOBJ(_fileName == ""); _fileName = aFileName; // OPEN THE FILE diff --git a/OpenSim/Common/Test/testAssertion.cpp b/OpenSim/Common/Test/testAssertion.cpp new file mode 100644 index 0000000000..cc5e8d3c18 --- /dev/null +++ b/OpenSim/Common/Test/testAssertion.cpp @@ -0,0 +1,84 @@ +/* ------------------------------------------------------------------------- * +* OpenSim: testAssertion.cpp * +* -------------------------------------------------------------------------- * +* The OpenSim API is a toolkit for musculoskeletal modeling and simulation. * +* See http://opensim.stanford.edu and the NOTICE file for more information. * +* OpenSim is developed at Stanford University and supported by the US * +* National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA * +* through the Warrior Web program. * +* * +* Copyright (c) 2005-2017 Stanford University and the Authors * +* Author(s): Frank C. Anderson * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); you may * +* not use this file except in compliance with the License. You may obtain a * +* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +* -------------------------------------------------------------------------- */ + +#include + +#include +#include +#define CATCH_CONFIG_MAIN +#include + +namespace TestAssertion { + + [[noreturn]] void SomeAssertingFunction() { + OPENSIM_ASSERT_ALWAYS(false && "i-am-in-the-error-msg"); + } + + class SomeAssertingObject : public OpenSim::Object { + OpenSim_DECLARE_CONCRETE_OBJECT(SomeAssertingObject, OpenSim::Object); + public: + SomeAssertingObject() + { + setName("name-of-the-object"); + OPENSIM_ASSERT_FRMOBJ(false && "and-i-am-also-in-the-error-msg"); + } + }; +} + +TEST_CASE("OPENSIM_ASSERT_ALWAYS throws an OpenSim::Exception on failure") +{ + CHECK_THROWS_AS(OPENSIM_ASSERT_ALWAYS(false), OpenSim::Exception); +} + +TEST_CASE("OPENSIM_ASSERT_ALWAYS exception contains expected information") +{ + // sorry: the line number in this source file that `OPENSIM_ASSERT_ALWAYS` + // appears on is hard-coded here because automating that is difficult + const char* lineNumberOfAssertionInThisFile = "34"; + + try { + TestAssertion::SomeAssertingFunction(); + } catch (const OpenSim::Exception& ex) { + REQUIRE_THAT(ex.what(), Catch::Matchers::Contains("SomeAssertingFunction")); + REQUIRE_THAT(ex.what(), Catch::Matchers::Contains("testAssertion.cpp")); + REQUIRE_THAT(ex.what(), Catch::Matchers::Contains(lineNumberOfAssertionInThisFile)); + REQUIRE_THAT(ex.what(), Catch::Matchers::Contains("i-am-in-the-error-msg")); + } +} + +TEST_CASE("OPENSIM_ASSERT_FRMOBJ_ALWAYS exception contains expected information") +{ + // sorry: the line number in this source file that `OPENSIM_ASSERT_FRMOBJ` + // appears on is hard-coded here because automating that is difficult + const char* lineNumberOfAssertionInThisFile = "43"; + + try { + TestAssertion::SomeAssertingObject throwsOnConstruction{}; + } catch (const OpenSim::Exception& ex) { + REQUIRE_THAT(ex.what(), Catch::Matchers::Contains("SomeAssertingObject")); + REQUIRE_THAT(ex.what(), Catch::Matchers::Contains("testAssertion.cpp")); + REQUIRE_THAT(ex.what(), Catch::Matchers::Contains(lineNumberOfAssertionInThisFile)); + REQUIRE_THAT(ex.what(), Catch::Matchers::Contains("name-of-the-object")); + REQUIRE_THAT(ex.what(), Catch::Matchers::Contains("and-i-am-also-in-the-error-msg")); + } +} diff --git a/OpenSim/Common/XMLDocument.cpp b/OpenSim/Common/XMLDocument.cpp index 6c8136f7a9..9bee9a7c4e 100644 --- a/OpenSim/Common/XMLDocument.cpp +++ b/OpenSim/Common/XMLDocument.cpp @@ -29,6 +29,7 @@ //----------------------------------------------------------------------------- // INCLUDES //----------------------------------------------------------------------------- +#include "Assertion.h" #include "XMLDocument.h" #include "Object.h" #include @@ -245,7 +246,7 @@ updateDocumentVersion() } // Validate >= 10500 and < latest as sanity check - assert(_documentVersion >= 10500 && _documentVersion <= LatestVersion); + OPENSIM_ASSERT(_documentVersion >= 10500 && _documentVersion <= LatestVersion); } //_____________________________________________________________________________ /** diff --git a/OpenSim/Common/osimCommon.h b/OpenSim/Common/osimCommon.h index f7309bcdc4..1c79481406 100644 --- a/OpenSim/Common/osimCommon.h +++ b/OpenSim/Common/osimCommon.h @@ -25,6 +25,7 @@ #include "About.h" #include "Adapters.h" +#include "Assertion.h" #include "CommonUtilities.h" #include "Constant.h" #include "DataTable.h" diff --git a/OpenSim/Moco/Archive/InverseMuscleSolver/INDYGOProblemInternal.h b/OpenSim/Moco/Archive/InverseMuscleSolver/INDYGOProblemInternal.h index 138d38d2e7..eef760e76a 100644 --- a/OpenSim/Moco/Archive/InverseMuscleSolver/INDYGOProblemInternal.h +++ b/OpenSim/Moco/Archive/InverseMuscleSolver/INDYGOProblemInternal.h @@ -25,6 +25,7 @@ #include "InverseMuscleSolverMotionData.h" #include +#include #include #include @@ -376,7 +377,7 @@ class INDYGOProblemInternal : public tropter::Problem { // Set control names. const auto& otherControlsLabels = mrsVars.other_controls.getColumnLabels(); - assert(_numCoordActuators == (int)otherControlsLabels.size()); + OPENSIM_ASSERT(_numCoordActuators == (int)otherControlsLabels.size()); for (int i_act = 0; i_act < _numCoordActuators; ++i_act) { vars.control_names[i_act] = otherControlsLabels[i_act] + "_control"; diff --git a/OpenSim/Moco/MocoBounds.cpp b/OpenSim/Moco/MocoBounds.cpp index efdf1ad2d2..ad94c33ac0 100644 --- a/OpenSim/Moco/MocoBounds.cpp +++ b/OpenSim/Moco/MocoBounds.cpp @@ -20,6 +20,8 @@ #include "MocoUtilities.h" +#include + using namespace OpenSim; MocoBounds::MocoBounds() { @@ -43,7 +45,7 @@ MocoBounds::MocoBounds(double lower, double upper) : MocoBounds() { } MocoBounds::MocoBounds(const Property& p) : MocoBounds() { - assert(p.size() <= 2); + OPENSIM_ASSERT_FRMOBJ(p.size() <= 2); if (p.size() >= 1) { OPENSIM_THROW_IF(SimTK::isNaN(p[0]), Exception, "NaN value detected. " "Please provide a non-NaN value for the bounds."); diff --git a/OpenSim/Moco/MocoCasADiSolver/MocoCasADiSolver.cpp b/OpenSim/Moco/MocoCasADiSolver/MocoCasADiSolver.cpp index bf9b920e04..82e9550190 100644 --- a/OpenSim/Moco/MocoCasADiSolver/MocoCasADiSolver.cpp +++ b/OpenSim/Moco/MocoCasADiSolver/MocoCasADiSolver.cpp @@ -18,6 +18,7 @@ #include "MocoCasADiSolver.h" +#include #include #ifdef OPENSIM_WITH_CASADI @@ -129,7 +130,7 @@ const MocoTrajectory& MocoCasADiSolver::getGuess() const { if (get_guess_file() != "" && m_guessFromFile.empty()) { // The API should make it impossible for both guessFromFile and // guessFromAPI to be non-empty. - assert(m_guessFromAPI.empty()); + OPENSIM_ASSERT_FRMOBJ(m_guessFromAPI.empty()); // No need to load from file again if we've already loaded it. MocoTrajectory guessFromFile(get_guess_file()); checkGuess(guessFromFile); diff --git a/OpenSim/Moco/MocoGoal/MocoAccelerationTrackingGoal.cpp b/OpenSim/Moco/MocoGoal/MocoAccelerationTrackingGoal.cpp index a2699458f5..a74ba15a45 100644 --- a/OpenSim/Moco/MocoGoal/MocoAccelerationTrackingGoal.cpp +++ b/OpenSim/Moco/MocoGoal/MocoAccelerationTrackingGoal.cpp @@ -20,6 +20,7 @@ #include +#include #include #include @@ -35,12 +36,12 @@ void MocoAccelerationTrackingGoal::initializeOnModelImpl( TimeSeriesTableVec3 accelerationTableToUse; // Should not be able to supply any two simultaneously. if (get_acceleration_reference_file() != "") { // acceleration ref file - assert(m_acceleration_table.getNumColumns() == 0); + OPENSIM_ASSERT_FRMOBJ(m_acceleration_table.getNumColumns() == 0); accelerationTableToUse = TimeSeriesTableVec3(get_acceleration_reference_file()); } else { // acceleration table - assert(get_acceleration_reference_file() == ""); + OPENSIM_ASSERT_FRMOBJ(get_acceleration_reference_file() == ""); accelerationTableToUse = m_acceleration_table; } diff --git a/OpenSim/Moco/MocoGoal/MocoAngularVelocityTrackingGoal.cpp b/OpenSim/Moco/MocoGoal/MocoAngularVelocityTrackingGoal.cpp index d5d7326789..f09224c8f0 100644 --- a/OpenSim/Moco/MocoGoal/MocoAngularVelocityTrackingGoal.cpp +++ b/OpenSim/Moco/MocoGoal/MocoAngularVelocityTrackingGoal.cpp @@ -20,6 +20,7 @@ #include +#include #include #include @@ -35,14 +36,14 @@ void MocoAngularVelocityTrackingGoal::initializeOnModelImpl( "") { // reference file provided TimeSeriesTableVec3 angularVelocityTableToUse; // Should not be able to supply any two simultaneously. - assert(get_states_reference().empty()); + OPENSIM_ASSERT_FRMOBJ(get_states_reference().empty()); if (get_angular_velocity_reference_file() != "") { // ang. vel. ref file - assert(m_angular_velocity_table.getNumColumns() == 0); + OPENSIM_ASSERT_FRMOBJ(m_angular_velocity_table.getNumColumns() == 0); angularVelocityTableToUse = TimeSeriesTableVec3( get_angular_velocity_reference_file()); } else { // ang. vel. table - assert(get_angular_velocity_reference_file() == ""); + OPENSIM_ASSERT_FRMOBJ(get_angular_velocity_reference_file() == ""); angularVelocityTableToUse = m_angular_velocity_table; } @@ -72,8 +73,8 @@ void MocoAngularVelocityTrackingGoal::initializeOnModelImpl( } } else { // states reference file or states reference provided - assert(get_angular_velocity_reference_file() != ""); - assert(m_angular_velocity_table.getNumColumns() == 0); + OPENSIM_ASSERT_FRMOBJ(get_angular_velocity_reference_file() != ""); + OPENSIM_ASSERT_FRMOBJ(m_angular_velocity_table.getNumColumns() == 0); // TODO: set relativeToDirectory properly. TimeSeriesTable statesTableToUse = get_states_reference().processAndConvertToRadians(model); diff --git a/OpenSim/Moco/MocoGoal/MocoOrientationTrackingGoal.cpp b/OpenSim/Moco/MocoGoal/MocoOrientationTrackingGoal.cpp index 59fc11d864..e8a0f6829d 100644 --- a/OpenSim/Moco/MocoGoal/MocoOrientationTrackingGoal.cpp +++ b/OpenSim/Moco/MocoGoal/MocoOrientationTrackingGoal.cpp @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -35,14 +36,14 @@ void MocoOrientationTrackingGoal::initializeOnModelImpl(const Model& model) get_rotation_reference_file() != "") { // reference file provided TimeSeriesTable_> rotationTableToUse; // Should not be able to supply any two simultaneously. - assert(get_states_reference().empty()); + OPENSIM_ASSERT_FRMOBJ(get_states_reference().empty()); if (get_rotation_reference_file() != "") { // rotation reference file - assert(m_rotation_table.getNumColumns() == 0); + OPENSIM_ASSERT_FRMOBJ(m_rotation_table.getNumColumns() == 0); rotationTableToUse = TimeSeriesTable_>( get_rotation_reference_file()); } else { // rotation table - assert(get_rotation_reference_file() == ""); + OPENSIM_ASSERT_FRMOBJ(get_rotation_reference_file() == ""); rotationTableToUse = m_rotation_table; } @@ -72,8 +73,8 @@ void MocoOrientationTrackingGoal::initializeOnModelImpl(const Model& model) } } else { // states reference file or states reference provided - assert(get_rotation_reference_file() == ""); - assert(m_rotation_table.getNumColumns() == 0); + OPENSIM_ASSERT_FRMOBJ(get_rotation_reference_file() == ""); + OPENSIM_ASSERT_FRMOBJ(m_rotation_table.getNumColumns() == 0); // TODO: set relativeToDirectory properly. TimeSeriesTable statesTableToUse = get_states_reference().processAndConvertToRadians(model); diff --git a/OpenSim/Moco/MocoGoal/MocoTranslationTrackingGoal.cpp b/OpenSim/Moco/MocoGoal/MocoTranslationTrackingGoal.cpp index 58408b48b7..17089bd48d 100644 --- a/OpenSim/Moco/MocoGoal/MocoTranslationTrackingGoal.cpp +++ b/OpenSim/Moco/MocoGoal/MocoTranslationTrackingGoal.cpp @@ -20,6 +20,7 @@ #include +#include #include #include @@ -34,14 +35,14 @@ void MocoTranslationTrackingGoal::initializeOnModelImpl(const Model& model) get_translation_reference_file() != "") { // reference file provided TimeSeriesTableVec3 translationTableToUse; // Should not be able to supply any two simultaneously. - assert(get_states_reference().empty()); + OPENSIM_ASSERT_FRMOBJ(get_states_reference().empty()); if (get_translation_reference_file() != "") { // translation ref file - assert(m_translation_table.getNumColumns() == 0); + OPENSIM_ASSERT_FRMOBJ(m_translation_table.getNumColumns() == 0); translationTableToUse = TimeSeriesTableVec3( get_translation_reference_file()); } else { // translation table - assert(get_translation_reference_file() == ""); + OPENSIM_ASSERT_FRMOBJ(get_translation_reference_file() == ""); translationTableToUse = m_translation_table; } @@ -73,8 +74,8 @@ void MocoTranslationTrackingGoal::initializeOnModelImpl(const Model& model) } } else { // states reference file or states reference provided - assert(get_translation_reference_file() != ""); - assert(m_translation_table.getNumColumns() == 0); + OPENSIM_ASSERT_FRMOBJ(get_translation_reference_file() != ""); + OPENSIM_ASSERT_FRMOBJ(m_translation_table.getNumColumns() == 0); // TODO: set relativeToDirectory properly. TimeSeriesTable statesTableToUse = get_states_reference().processAndConvertToRadians(model); diff --git a/OpenSim/Moco/MocoProblemRep.h b/OpenSim/Moco/MocoProblemRep.h index f169dd8973..bd64ca49eb 100644 --- a/OpenSim/Moco/MocoProblemRep.h +++ b/OpenSim/Moco/MocoProblemRep.h @@ -24,6 +24,7 @@ #include "MocoVariableInfo.h" #include "osimMocoDLL.h" +#include #include #include @@ -111,7 +112,7 @@ class OSIMMOCO_API MocoProblemRep { /// ModelDisabledConstraints. Some solvers may need to use 2 state objects /// at once; you can supply an index of 1 to get a second state object. SimTK::State& updStateDisabledConstraints(int index = 0) const { - assert(index <= 1); + OPENSIM_ASSERT(index <= 1); return m_state_disabled_constraints[index]; } /// This is a component inside ModelDisabledConstraints that you can use to diff --git a/OpenSim/Moco/MocoTrajectory.cpp b/OpenSim/Moco/MocoTrajectory.cpp index 1636b2ed07..75684af0b6 100644 --- a/OpenSim/Moco/MocoTrajectory.cpp +++ b/OpenSim/Moco/MocoTrajectory.cpp @@ -20,6 +20,7 @@ #include "MocoProblem.h" #include "MocoUtilities.h" +#include #include #include #include @@ -1346,7 +1347,7 @@ double MocoTrajectory::compareContinuousVariablesRMSInternal( } // Trapezoidal rule for uniform grid: // dt / 2 (f_0 + 2f_1 + 2f_2 + 2f_3 + ... + 2f_{N-1} + f_N) - assert(numTimes > 2); + OPENSIM_ASSERT(numTimes > 2); return timeInterval / 2.0 * (sumSquaredError.sum() + sumSquaredError(1, numTimes - 2).sum()); }; diff --git a/OpenSim/Moco/MocoTropterSolver.cpp b/OpenSim/Moco/MocoTropterSolver.cpp index b6558d2d73..4c3c5e6fd6 100644 --- a/OpenSim/Moco/MocoTropterSolver.cpp +++ b/OpenSim/Moco/MocoTropterSolver.cpp @@ -20,6 +20,7 @@ #include "MocoProblemRep.h" #include "MocoUtilities.h" +#include #include #ifdef OPENSIM_WITH_TROPTER @@ -285,7 +286,7 @@ const MocoTrajectory& MocoTropterSolver::getGuess() const { if (get_guess_file() != "" && m_guessFromFile.empty()) { // The API should make it impossible for both guessFromFile and // guessFromAPI to be non-empty. - assert(m_guessFromAPI.empty()); + OPENSIM_ASSERT_FRMOBJ(m_guessFromAPI.empty()); // No need to load from file again if we've already loaded it. MocoTrajectory guessFromFile(get_guess_file()); checkGuess(guessFromFile); diff --git a/OpenSim/Moco/tropter/TropterProblem.cpp b/OpenSim/Moco/tropter/TropterProblem.cpp index 904bb84c71..6c979e439b 100644 --- a/OpenSim/Moco/tropter/TropterProblem.cpp +++ b/OpenSim/Moco/tropter/TropterProblem.cpp @@ -17,6 +17,8 @@ * -------------------------------------------------------------------------- */ #include "TropterProblem.h" +#include + using namespace OpenSim; template @@ -36,7 +38,7 @@ convertIterateTropterToMoco(const tropIterateType& tropSol) const { const int numDerivatives = (int)tropSol.adjunct_names.size() - numMultipliers; - assert(numDerivatives >= 0); + OPENSIM_ASSERT(numDerivatives >= 0); std::vector derivative_names(numDerivatives); std::copy_n(tropSol.adjunct_names.begin() + numMultipliers, numDerivatives, derivative_names.begin()); diff --git a/OpenSim/Sandbox/Moco/sandboxContact.cpp b/OpenSim/Sandbox/Moco/sandboxContact.cpp index 320224be84..d8d4240e60 100644 --- a/OpenSim/Sandbox/Moco/sandboxContact.cpp +++ b/OpenSim/Sandbox/Moco/sandboxContact.cpp @@ -21,13 +21,14 @@ #include #include #include -#include #include //#include #include #include -#include +#include #include +#include +#include #include #include @@ -688,7 +689,7 @@ void slipSolveForForce(double rzvalue0 = 0, double rzspeed0 = 0) { springForceActualTime = SimTK::Vector((int)stdTime.size(), stdTime.data()); springForceActual.resize(X.size()); - assert(X.size() > 0); + OPENSIM_ASSERT(X.size() > 0); for (int i = 0; i < X.size(); ++i) { SimTK::Vec3 forceVec(X[i], Y[i], Z[i]); SimTK::Vec3 lineOfAction(pX[i], pY[i], pZ[i]); diff --git a/OpenSim/Sandbox/Moco/sandboxMocoTrack/sandboxMocoTrack.cpp b/OpenSim/Sandbox/Moco/sandboxMocoTrack/sandboxMocoTrack.cpp index fadcd88008..4e11547099 100644 --- a/OpenSim/Sandbox/Moco/sandboxMocoTrack/sandboxMocoTrack.cpp +++ b/OpenSim/Sandbox/Moco/sandboxMocoTrack/sandboxMocoTrack.cpp @@ -19,6 +19,7 @@ #include #include +#include #include using namespace OpenSim; @@ -84,7 +85,7 @@ void transformReactionToBodyFrame(const MocoStudy& study, model.initSystem(); const auto& ground = model.getGround(); auto statesTraj = iterate.exportToStatesTrajectory(study.getProblem()); - assert(statesTraj.getSize() == reactionTable.getNumRows()); + OPENSIM_ASSERT_FRMOBJ(statesTraj.getSize() == reactionTable.getNumRows()); for (int irow = 0; irow < reactionTable.getNumRows(); ++irow) { auto& row = reactionTable.updRowAtIndex(irow); diff --git a/OpenSim/Simulation/Model/ExternalForce.cpp b/OpenSim/Simulation/Model/ExternalForce.cpp index a89509be02..11b4576bb0 100644 --- a/OpenSim/Simulation/Model/ExternalForce.cpp +++ b/OpenSim/Simulation/Model/ExternalForce.cpp @@ -26,6 +26,7 @@ //============================================================================== #include #include +#include #include #include #include @@ -333,7 +334,7 @@ void ExternalForce::computeForce(const SimTK::State& state, { double time = state.getTime(); - assert(_appliedToBody!=nullptr); + OPENSIM_ASSERT_FRMOBJ(_appliedToBody != nullptr); if (_appliesForce) { Vec3 force = getForceAtTime(time); diff --git a/OpenSim/Simulation/Model/GeometryPath.cpp b/OpenSim/Simulation/Model/GeometryPath.cpp index c7db2882d7..3cea20e2b2 100644 --- a/OpenSim/Simulation/Model/GeometryPath.cpp +++ b/OpenSim/Simulation/Model/GeometryPath.cpp @@ -28,9 +28,11 @@ #include "ConditionalPathPoint.h" #include "MovingPathPoint.h" #include "PointForceDirection.h" -#include #include "Model.h" +#include +#include + //============================================================================= // STATICS //============================================================================= @@ -220,7 +222,7 @@ generateDecorations(bool fixed, const ModelDisplayHints& hints, const Array& pathPoints = getCurrentPath(state); - assert(pathPoints.size() > 1); + OPENSIM_ASSERT_FRMOBJ(pathPoints.size() > 1); const AbstractPathPoint* lastPoint = pathPoints[0]; MobilizedBodyIndex mbix(0); diff --git a/OpenSim/Simulation/Model/Ligament.cpp b/OpenSim/Simulation/Model/Ligament.cpp index d2f76678a3..b2a16c0f5f 100644 --- a/OpenSim/Simulation/Model/Ligament.cpp +++ b/OpenSim/Simulation/Model/Ligament.cpp @@ -26,6 +26,8 @@ //============================================================================= #include "Ligament.h" #include "PointForceDirection.h" + +#include #include //============================================================================= @@ -81,7 +83,7 @@ void Ligament::extendFinalizeFromProperties() Super::extendFinalizeFromProperties(); // Resting length must be greater than 0.0. - assert(get_resting_length() > 0.0); + OPENSIM_ASSERT_FRMOBJ(get_resting_length() > 0.0); updPath().setDefaultColor(DefaultLigamentColor); } diff --git a/OpenSim/Simulation/Model/ModelVisualizer.h b/OpenSim/Simulation/Model/ModelVisualizer.h index 67369cbfa7..f8d1178747 100644 --- a/OpenSim/Simulation/Model/ModelVisualizer.h +++ b/OpenSim/Simulation/Model/ModelVisualizer.h @@ -28,6 +28,7 @@ This file provides an OpenSim-oriented interface to the Simbody Visualizer that provides some visualization and user interaction when running a program that uses the OpenSim API. **/ +#include #include #include @@ -145,12 +146,12 @@ class OSIMSIMULATION_API ModelVisualizer { /** If you want access to the underlying Simbody SimTK::Visualizer, you can get a const reference here. **/ const SimTK::Visualizer& getSimbodyVisualizer() const - { assert(_viz); return *_viz; } + { OPENSIM_ASSERT(_viz); return *_viz; } /** If you want writable access to the underlying Simbody SimTK::Visualizer, you can get a non-const reference here, provided that you have non-const access to the %ModelVisualizer. **/ SimTK::Visualizer& updSimbodyVisualizer() - { assert(_viz); return *_viz; } + { OPENSIM_ASSERT(_viz); return *_viz; } /**@}**/ /** @name Miscellaneous utilities diff --git a/OpenSim/Simulation/Model/PathPoint.h b/OpenSim/Simulation/Model/PathPoint.h index 4c85fe1a94..1f4b7ad5b9 100644 --- a/OpenSim/Simulation/Model/PathPoint.h +++ b/OpenSim/Simulation/Model/PathPoint.h @@ -24,6 +24,7 @@ // INCLUDE +#include "OpenSim/Common/Assertion.h" #include "OpenSim/Simulation/Model/Station.h" #include "OpenSim/Simulation/Model/AbstractPathPoint.h" @@ -53,7 +54,7 @@ class OSIMSIMULATION_API PathPoint : public AbstractPathPoint { /** (Deprecated) Old PathPoint interface */ DEPRECATED_14("Use setLocation() instead.") void setLocationCoord(const SimTK::State&s, int aXYZ, double aValue) { - assert(aXYZ>=0 && aXYZ<=2); + OPENSIM_ASSERT_FRMOBJ(aXYZ>=0 && aXYZ<=2); updStation().upd_location()[aXYZ]=aValue; } diff --git a/OpenSim/Simulation/SimbodyEngine/ConstantCurvatureJoint.cpp b/OpenSim/Simulation/SimbodyEngine/ConstantCurvatureJoint.cpp index d456a872cc..0a4d66c5f7 100644 --- a/OpenSim/Simulation/SimbodyEngine/ConstantCurvatureJoint.cpp +++ b/OpenSim/Simulation/SimbodyEngine/ConstantCurvatureJoint.cpp @@ -33,6 +33,7 @@ #include #include +#include #include using namespace SimTK; @@ -55,7 +56,7 @@ Vec3 OpenSim::ConstantCurvatureJoint::clamp(const SimTK::Vec3& q) { "to unphysical behavior. Please adjust your model " "or simulation to avoid this state.", pos(0), bound)); - assert(false); + OPENSIM_ASSERT_FRMOBJ(false); pos(0) = bound; } if (pos(0) < -bound) { @@ -66,7 +67,7 @@ Vec3 OpenSim::ConstantCurvatureJoint::clamp(const SimTK::Vec3& q) { "to unphysical behavior. Please adjust your model " "or simulation to avoid this state.", pos(0), -bound)); - assert(false); + OPENSIM_ASSERT_FRMOBJ(false); pos(0) = -bound; } if (pos(1) > bound) { @@ -77,7 +78,7 @@ Vec3 OpenSim::ConstantCurvatureJoint::clamp(const SimTK::Vec3& q) { "to unphysical behavior. Please adjust your model " "or simulation to avoid this state.", pos(1), bound)); - assert(false); + OPENSIM_ASSERT_FRMOBJ(false); pos(1) = bound; } if (pos(1) < -bound) { @@ -88,7 +89,7 @@ Vec3 OpenSim::ConstantCurvatureJoint::clamp(const SimTK::Vec3& q) { "to unphysical behavior. Please adjust your model " "or simulation to avoid this state.", pos(1), -bound)); - assert(false); + OPENSIM_ASSERT_FRMOBJ(false); pos(1) = -bound; } if (pos(2) > bound) { @@ -99,7 +100,7 @@ Vec3 OpenSim::ConstantCurvatureJoint::clamp(const SimTK::Vec3& q) { "to unphysical behavior. Please adjust your model " "or simulation to avoid this state.", pos(2), bound)); - assert(false); + OPENSIM_ASSERT_FRMOBJ(false); pos(2) = bound; } if (pos(2) < -bound) { @@ -110,7 +111,7 @@ Vec3 OpenSim::ConstantCurvatureJoint::clamp(const SimTK::Vec3& q) { "to unphysical behavior. Please adjust your model " "or simulation to avoid this state.", pos(2), -bound)); - assert(false); + OPENSIM_ASSERT_FRMOBJ(false); pos(2) = -bound; } return pos; @@ -204,7 +205,7 @@ Mat33 OpenSim::ConstantCurvatureJoint::eulerXZYToMatrixGrad( ret(1, 2) = -(-sy) * sx + cx * (cy)*sz; ret(2, 2) = cx * (-sy) + sx * (cy)*sz; } else { - assert(false); + OPENSIM_ASSERT_FRMOBJ(false); } return ret; @@ -246,7 +247,7 @@ Mat63 OpenSim::ConstantCurvatureJoint::getEulerJacobian(const Vec3& q) { Mat63 OpenSim::ConstantCurvatureJoint::getEulerJacobianDerivWrtPos( const Vec3& q, int index) { - assert(index < 3); + OPENSIM_ASSERT_FRMOBJ(index < 3); Mat63 DJ_Dq; DJ_Dq.setToZero(); @@ -677,10 +678,10 @@ class ConstantCurvatureJointImpl OpenSim::ConstantCurvatureJoint::clamp(q + neutralPos), length); Vec6 result = J * Vec3(u[0], u[1], u[2]); - assert(!q.isNaN()); - assert(!J.isNaN()); - assert(!J.isInf()); - assert(!result.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!q.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!J.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!J.isInf()); + OPENSIM_ASSERT_FRMOBJ(!result.isNaN()); return SpatialVec(result.getSubVec<3>(0), result.getSubVec<3>(3)); } @@ -698,10 +699,10 @@ class ConstantCurvatureJointImpl Vec3 result = J.transpose() * rawSpatial; - assert(!q.isNaN()); - assert(!J.isNaN()); - assert(!J.isInf()); - assert(!result.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!q.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!J.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!J.isInf()); + OPENSIM_ASSERT_FRMOBJ(!result.isNaN()); Vec3::updAs(f) = result; } @@ -721,11 +722,11 @@ class ConstantCurvatureJointImpl dq, length); Vec6 result = dJ * Vec3(u[0], u[1], u[2]); - assert(!q.isNaN()); - assert(!dq.isNaN()); - assert(!dJ.isNaN()); - assert(!dJ.isInf()); - assert(!result.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!q.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!dq.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!dJ.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!dJ.isInf()); + OPENSIM_ASSERT_FRMOBJ(!result.isNaN()); return SpatialVec(result.getSubVec<3>(0), result.getSubVec<3>(3)); } @@ -747,24 +748,24 @@ class ConstantCurvatureJointImpl Vec3 result = dJ.transpose() * rawSpatial; - assert(!q.isNaN()); - assert(!dq.isNaN()); - assert(!dJ.isNaN()); - assert(!dJ.isInf()); - assert(!result.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!q.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!dq.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!dJ.isNaN()); + OPENSIM_ASSERT_FRMOBJ(!dJ.isInf()); + OPENSIM_ASSERT_FRMOBJ(!result.isNaN()); Vec3::updAs(f) = result; } void setQToFitTransform( const State&, const Transform& X_FM, int nq, Real* q) const { - assert(false); + OPENSIM_ASSERT_FRMOBJ(false); Vec3::updAs(q) = X_FM.p(); } void setUToFitVelocity( const State&, const SpatialVec& V_FM, int nu, Real* u) const { - assert(false); + OPENSIM_ASSERT_FRMOBJ(false); Vec3::updAs(u) = V_FM[1]; } diff --git a/OpenSim/Simulation/SimbodyEngine/Coordinate.cpp b/OpenSim/Simulation/SimbodyEngine/Coordinate.cpp index 6c010bf4bb..2ab33a92dd 100644 --- a/OpenSim/Simulation/SimbodyEngine/Coordinate.cpp +++ b/OpenSim/Simulation/SimbodyEngine/Coordinate.cpp @@ -26,6 +26,7 @@ //============================================================================= #include "Coordinate.h" #include "CoordinateCouplerConstraint.h" +#include #include #include #include "simbody/internal/Constraint.h" @@ -51,7 +52,7 @@ class ModifiableConstant : public SimTK::Function_{ } SimTK::Real calcValue(const SimTK::Vector& x) const override { - assert(x.size() == argumentSize); + OPENSIM_ASSERT(x.size() == argumentSize); return value; } diff --git a/OpenSim/Simulation/SimbodyEngine/CustomJoint.cpp b/OpenSim/Simulation/SimbodyEngine/CustomJoint.cpp index 8bafc8ca6e..cf33d9d4f0 100644 --- a/OpenSim/Simulation/SimbodyEngine/CustomJoint.cpp +++ b/OpenSim/Simulation/SimbodyEngine/CustomJoint.cpp @@ -27,6 +27,7 @@ #include "CustomJoint.h" #include "SpatialTransform.h" #include +#include #include #include #include "simbody/internal/MobilizedBody_FunctionBased.h" @@ -292,13 +293,13 @@ void CustomJoint::extendAddToSystem(SimTK::MultibodySystem& system) const SimTK_ASSERT1(numCoords <= 6, "%s cannot exceed 6 mobilities (dofs).", getConcreteClassName().c_str()); - assert(functions.size() == 6); + OPENSIM_ASSERT_FRMOBJ(functions.size() == 6); SimTK_ASSERT2(numCoords <= 6, "%s::%s must specify functions for complete spatial (6 axes) motion.", getConcreteClassName().c_str(), getSpatialTransform().getConcreteClassName().c_str()); - assert(coordinateIndices.size() == 6); + OPENSIM_ASSERT_FRMOBJ(coordinateIndices.size() == 6); SimTK_ASSERT2(axes.size() == 6, "%s::%s must specify 6 independent axes to span spatial motion.", @@ -373,7 +374,7 @@ updateFromXMLNode(SimTK::Xml::Element& aNode, int versionNumber) if (objectType == "TransformAxis"){ OpenSim::TransformAxis* readAxis = new OpenSim::TransformAxis(objElmt); - assert(nextAxis <=5); + OPENSIM_ASSERT_FRMOBJ(nextAxis <=5); bool isRotation = false; SimTK::Xml::element_iterator rotationNode = objElmt.element_begin("is_rotation"); @@ -406,8 +407,8 @@ updateFromXMLNode(SimTK::Xml::Element& aNode, int versionNumber) nextAxis++; } } - assert(rotationIndices.getSize() <=3); - assert(translationIndices.getSize() <=3); + OPENSIM_ASSERT_FRMOBJ(rotationIndices.getSize() <=3); + OPENSIM_ASSERT_FRMOBJ(translationIndices.getSize() <=3); //XMLNode::RemoveChildren(SpatialTransformAxesNode); int nRotations = rotationIndices.getSize(); int nTranslations = translationIndices.getSize(); diff --git a/OpenSim/Simulation/SimbodyEngine/Joint.cpp b/OpenSim/Simulation/SimbodyEngine/Joint.cpp index 8ba48f2f49..424886d9c7 100644 --- a/OpenSim/Simulation/SimbodyEngine/Joint.cpp +++ b/OpenSim/Simulation/SimbodyEngine/Joint.cpp @@ -25,6 +25,7 @@ // INCLUDES //============================================================================== #include "Joint.h" +#include #include #include #include @@ -479,7 +480,7 @@ SimTK::SpatialVec Joint::calcEquivalentSpatialForceForMobilizedBody(const SimTK: } else{ // should be the case where gen force is zero. - assert(f.norm() < SimTK::SignificantReal); + OPENSIM_ASSERT_FRMOBJ(f.norm() < SimTK::SignificantReal); } // The spatial forces above are expressed in the joint frame of the parent diff --git a/OpenSim/Simulation/SimbodyEngine/SimbodyEngine.cpp b/OpenSim/Simulation/SimbodyEngine/SimbodyEngine.cpp index 458359b728..96c102aa8d 100644 --- a/OpenSim/Simulation/SimbodyEngine/SimbodyEngine.cpp +++ b/OpenSim/Simulation/SimbodyEngine/SimbodyEngine.cpp @@ -24,6 +24,7 @@ //============================================================================= // INCLUDES //============================================================================= +#include #include #include #include @@ -409,9 +410,9 @@ void SimbodyEngine::computeReactions(const SimTK::State& s, Vector_& rForc // there may be more mobilized bodies than joint exposed in the OpenSim model // since joints and other components may use (massless) bodies internally - assert(_model->getMatterSubsystem().getNumBodies() >= nj); - assert(nj == rForces.size()); - assert(rForces.size() == rTorques.size()); + OPENSIM_ASSERT_FRMOBJ(_model->getMatterSubsystem().getNumBodies() >= nj); + OPENSIM_ASSERT_FRMOBJ(nj == rForces.size()); + OPENSIM_ASSERT_FRMOBJ(rForces.size() == rTorques.size()); SimTK::Vector_ reactionForces(nj); @@ -996,7 +997,7 @@ void SimbodyEngine::scaleRotationalDofColumns(TimeSeriesTable& table, */ void SimbodyEngine::convertDegreesToRadians(Storage &rStorage) const { - assert(rStorage.isInDegrees()); + OPENSIM_ASSERT_FRMOBJ(rStorage.isInDegrees()); scaleRotationalDofColumns(rStorage, SimTK_DEGREE_TO_RADIAN); rStorage.setInDegrees(false); } @@ -1010,7 +1011,7 @@ void SimbodyEngine::convertDegreesToRadians(Storage &rStorage) const */ void SimbodyEngine::convertRadiansToDegrees(Storage &rStorage) const { - assert(!rStorage.isInDegrees()); + OPENSIM_ASSERT_FRMOBJ(!rStorage.isInDegrees()); scaleRotationalDofColumns(rStorage, SimTK_RADIAN_TO_DEGREE); rStorage.setInDegrees(true); } diff --git a/OpenSim/Simulation/SimbodyEngine/TransformAxis.h b/OpenSim/Simulation/SimbodyEngine/TransformAxis.h index 6832acbf49..853bbb6ef5 100644 --- a/OpenSim/Simulation/SimbodyEngine/TransformAxis.h +++ b/OpenSim/Simulation/SimbodyEngine/TransformAxis.h @@ -25,6 +25,7 @@ * -------------------------------------------------------------------------- */ // INCLUDE +#include #include #include #include @@ -117,7 +118,7 @@ OpenSim_DECLARE_CONCRETE_OBJECT(TransformAxis, Object); void getAxis(SimTK::Vec3& axis) const {axis = getAxis();} /** Get one component (0,1, or 2) of the axis vector. **/ double getAxis(int which) const - { assert(0<=which && which<=2); return getAxis()[which]; } + { OPENSIM_ASSERT_FRMOBJ(0<=which && which<=2); return getAxis()[which]; } /** Determine whether a custom function has been specified to map between the generalized coordinate and the amount of transformation along the diff --git a/OpenSim/Tools/CMCTool.cpp b/OpenSim/Tools/CMCTool.cpp index 0d6bc89d95..547f3b93b0 100644 --- a/OpenSim/Tools/CMCTool.cpp +++ b/OpenSim/Tools/CMCTool.cpp @@ -26,6 +26,7 @@ #include "ActuatorForceTarget.h" #include "ActuatorForceTargetFast.h" #include "VectorFunctionForActuators.h" +#include #include #include #include @@ -674,7 +675,7 @@ bool CMCTool::run() // int nra = actSysZ.size(); // int nrm = modelZ.size(); - assert(actSysZ.size() == modelZ.size()); + OPENSIM_ASSERT_FRMOBJ(actSysZ.size() == modelZ.size()); actSysZ = modelZ; VectorFunctionForActuators *predictor = diff --git a/OpenSim/Tools/RRATool.cpp b/OpenSim/Tools/RRATool.cpp index ee1c9b910e..4de271b84f 100644 --- a/OpenSim/Tools/RRATool.cpp +++ b/OpenSim/Tools/RRATool.cpp @@ -27,6 +27,7 @@ #include "ActuatorForceTargetFast.h" #include "AnalyzeTool.h" #include "VectorFunctionForActuators.h" +#include #include #include #include @@ -659,7 +660,7 @@ bool RRATool::run() // int nra = actSysZ.size(); // int nrm = modelZ.size(); - assert(actSysZ.size() == modelZ.size()); + OPENSIM_ASSERT_FRMOBJ(actSysZ.size() == modelZ.size()); actSysZ = modelZ; VectorFunctionForActuators *predictor = @@ -1091,7 +1092,7 @@ string RRATool:: adjustCOMToReduceResiduals(const OpenSim::Array &aFAve,const OpenSim::Array &aMAve) { // CHECK SIZE - assert(aFAve.getSize()==3 && aMAve.getSize()==3); + OPENSIM_ASSERT_FRMOBJ(aFAve.getSize()==3 && aMAve.getSize()==3); // GRAVITY Vec3 g = _model->getGravity(); diff --git a/OpenSim/Utilities/simmToOpenSim/detools.c b/OpenSim/Utilities/simmToOpenSim/detools.c index 70520f79ea..58b9c75746 100644 --- a/OpenSim/Utilities/simmToOpenSim/detools.c +++ b/OpenSim/Utilities/simmToOpenSim/detools.c @@ -809,7 +809,6 @@ XForm* get_deform_xform (DeformObject* dfm, int deformMode) case DE_DEFORM_END_MODE: return &dfm->deform_end; } -/* assert(0);*/ return NULL; } @@ -834,7 +833,6 @@ XForm* get_deform_xform2 (DeformObject* dfm, int deformMode, double factor) else if (deformMode == DE_DEFORM_END_MODE) return &dfm->deform_end; } -/* assert(0);*/ return NULL; } @@ -866,10 +864,6 @@ XForm* get_deform_xform3 (DeformObject* dfm, int deformMode, double factor) return &xform; } } - /* - assert(0); - return NULL; - */ } /* -------------------------------------------------------------------------