1 #ifndef OPENSIM_PROPERTY_DBL_VEC_H_ 2 #define OPENSIM_PROPERTY_DBL_VEC_H_ 3 /* -------------------------------------------------------------------------- * 4 * OpenSim: PropertyDblVec.h * 5 * -------------------------------------------------------------------------- * 6 * The OpenSim API is a toolkit for musculoskeletal modeling and simulation. * 7 * See http://opensim.stanford.edu and the NOTICE file for more information. * 8 * OpenSim is developed at Stanford University and supported by the US * 9 * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA * 10 * through the Warrior Web program. * 11 * * 12 * Copyright (c) 2005-2017 Stanford University and the Authors * 13 * Author(s): Ayman Habib, Ajay Seth, Michael A. Sherman * 14 * * 15 * Licensed under the Apache License, Version 2.0 (the "License"); you may * 16 * not use this file except in compliance with the License. You may obtain a * 17 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * 18 * * 19 * Unless required by applicable law or agreed to in writing, software * 20 * distributed under the License is distributed on an "AS IS" BASIS, * 21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 22 * See the License for the specific language governing permissions and * 23 * limitations under the License. * 24 * -------------------------------------------------------------------------- */ 25 26 #ifdef _WIN32 27 #pragma warning( disable : 4251 ) 28 #endif 29 30 #include "osimCommonDLL.h" 31 #include <string> 32 #include "Property_Deprecated.h" 33 #include "OpenSim/Common/Array.h" 34 35 //============================================================================= 36 //============================================================================= 37 namespace OpenSim { 38 39 /** 40 * Class PropertyDblVec_ extends class Property. It consists of a small 41 * vector of doubles (i.e., SimTK::Vec<M>) and the methods for accessing 42 * and modifying this Vec<M>. 43 * 44 * @author Ayman Habib, Ajay Seth, Michael Sherman 45 */ 46 template<int M> class PropertyDblVec_ : public Property_Deprecated 47 { 48 49 //============================================================================= 50 // DATA 51 //============================================================================= 52 private: 53 // Store in an Array for serialization; we'll fake up the Vec<M> when 54 // we need it using the Array's storage. 55 Array<double> _dblvec; 56 57 //============================================================================= 58 // METHODS 59 //============================================================================= 60 //-------------------------------------------------------------------------- 61 // CONSTRUCTION 62 //-------------------------------------------------------------------------- 63 public: 64 /** Default constructor */ PropertyDblVec_()65 PropertyDblVec_() : Property_Deprecated(DblVec, "DblVec_PropertyName") 66 { setAllowableListSize(M); 67 _dblvec.setSize(M); 68 } 69 /** Construct from name and value */ PropertyDblVec_(const std::string & aName,const SimTK::Vec<M> & aVec)70 PropertyDblVec_(const std::string &aName, const SimTK::Vec<M>& aVec) 71 : Property_Deprecated(DblVec, aName) 72 { setAllowableListSize(M); 73 _dblvec.setSize(M); 74 setValue(aVec); 75 } 76 /** Construct from name and value as an Array<double> */ PropertyDblVec_(const std::string & aName,const Array<double> & anArray)77 PropertyDblVec_(const std::string &aName, const Array<double> &anArray) 78 : Property_Deprecated(DblVec, aName) 79 { setAllowableListSize(M); 80 _dblvec.setSize(M); 81 setValue(anArray); 82 } 83 84 // default destructor, copy constructor, copy assignment 85 86 /* Return a copy of this property */ clone()87 PropertyDblVec_* clone() const override { 88 PropertyDblVec_* prop = new PropertyDblVec_<M>(*this); 89 return prop; 90 } 91 assign(const AbstractProperty & that)92 void assign(const AbstractProperty& that) override { 93 try { 94 *this = dynamic_cast<const PropertyDblVec_&>(that); 95 } catch(const std::bad_cast&) { 96 OPENSIM_THROW(InvalidArgument, 97 "Unsupported type. Expected: " + this->getTypeName() + 98 " | Received: " + that.getTypeName()); 99 } 100 } 101 102 public: 103 104 //-------------------------------------------------------------------------- 105 // GET AND SET 106 //-------------------------------------------------------------------------- 107 public: 108 /** Get the type of this property as a string. */ getTypeName()109 std::string getTypeName() const override { 110 return "double"; 111 } 112 113 // VALUE 114 /** set value of property from an equivalently sized Vec */ setValue(const SimTK::Vec<M> & aVec)115 void setValue(const SimTK::Vec<M> &aVec) { 116 SimTK::Vec<M>::updAs(&_dblvec[0])=aVec; 117 } 118 // This helps avoid the -Woverloaded-virtual warning with Clang (the method 119 // above otherwise hides the virtual setValue() methods in the base class). 120 using Property_Deprecated::setValue; 121 /** set value of this property from an array of doubles of equal or greater length */ setValue(const Array<double> & anArray)122 void setValue(const Array<double> &anArray) override { 123 assert(anArray.getSize() >= M); 124 for(int i=0;i<M; i++) 125 _dblvec[i] = anArray[i]; 126 } 127 /** get writable reference to the value */ getValueDblVec()128 SimTK::Vec<M>& getValueDblVec() {return SimTK::Vec<M>::updAs(&_dblvec[0]); } 129 /** get const (read-only) reference to the value */ getValueDblVec()130 const SimTK::Vec<M>& getValueDblVec() const {return SimTK::Vec<M>::getAs(&_dblvec[0]); }; 131 /** set value from double array */ // to be used by the serialization code setValue(int aSize,const double aArray[])132 void setValue(int aSize, const double aArray[]) override { 133 assert(aSize == M); 134 setValue(SimTK::Vec<M>::getAs(aArray)); 135 }; 136 #ifndef SWIG 137 /** get value as double array */ getValueDblArray()138 const Array<double>& getValueDblArray() const override {return _dblvec;} 139 #endif 140 /** Nonconst version of accessor for use by GUI. */ getValueDblArray()141 Array<double>& getValueDblArray() override {return _dblvec;} 142 143 /** Get a constant String representing the value of this property. */ toString()144 std::string toString() const override { 145 std::string str = "("; 146 char dbl[256]; 147 for(int i=0; i < M; i++){ 148 snprintf(dbl, 256, "%g", _dblvec[i]); 149 str += (i>0?" ":"") + std::string(dbl); 150 } 151 str += ")"; 152 return str; 153 }; 154 // SIZE getArraySize()155 int getArraySize() const override { return M; } 156 isArrayProperty()157 bool isArrayProperty() const override {return true;} 158 getNumValues()159 int getNumValues() const override {return M;} 160 //============================================================================= 161 }; // END of class PropertyDblVec_ 162 163 }; //OpenSim namespace 164 //============================================================================= 165 //============================================================================= 166 167 #endif // OPENSIM_PROPERTY_DBL_VEC_H_ 168