1 #ifndef OPENSIM_PIECEWISE_CONSTANT_FUNCTION_H_
2 #define OPENSIM_PIECEWISE_CONSTANT_FUNCTION_H_
3 /* -------------------------------------------------------------------------- *
4  *                   OpenSim:  PiecewiseConstantFunction.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): Peter Loan                                                      *
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 
27 // INCLUDES
28 #include "osimCommonDLL.h"
29 #include <string>
30 #include "Function.h"
31 #include "PropertyDblArray.h"
32 
33 
34 //=============================================================================
35 //=============================================================================
36 namespace OpenSim {
37 
38 template <class T> class Array;
39 
40 /**
41  * A class implementing a step function.
42  *
43  * This class inherits from Function and so can be used as input to
44  * any class requiring a Function as input.
45  *
46  * @author Peter Loan
47  * @version 1.0
48  */
49 class OSIMCOMMON_API PiecewiseConstantFunction : public Function {
50 OpenSim_DECLARE_CONCRETE_OBJECT(PiecewiseConstantFunction, Function);
51 
52 //=============================================================================
53 // MEMBER VARIABLES
54 //=============================================================================
55 protected:
56        // PROPERTIES
57        /** Array of values for the independent variables (i.e., the knot
58        sequence).  This array must be monotonically increasing. */
59        PropertyDblArray _propX;
60        Array<double> &_x;
61 
62        /** Y values. */
63        PropertyDblArray _propY;
64        Array<double> &_y;
65 
66 //=============================================================================
67 // METHODS
68 //=============================================================================
69 public:
70     //--------------------------------------------------------------------------
71     // CONSTRUCTION
72     //--------------------------------------------------------------------------
73     PiecewiseConstantFunction();
74     PiecewiseConstantFunction(int aN,const double *aTimes,const double *aValues,
75             const std::string &aName="");
76     PiecewiseConstantFunction(const PiecewiseConstantFunction &aFunction);
77     virtual ~PiecewiseConstantFunction();
78 
79     void init(Function* aFunction) override;
80 
81 private:
82     void setNull();
83     void setupProperties();
84     void setEqual(const PiecewiseConstantFunction &aFunction);
85 
86     //--------------------------------------------------------------------------
87     // OPERATORS
88     //--------------------------------------------------------------------------
89 public:
90 #ifndef SWIG
91     PiecewiseConstantFunction& operator=(const PiecewiseConstantFunction &aFunction);
92 #endif
93     //--------------------------------------------------------------------------
94     // SET AND GET
95     //--------------------------------------------------------------------------
96 public:
97     int getSize() const;
98     const Array<double>& getX() const;
99     const Array<double>& getY() const;
100     virtual const double* getXValues() const;
101     virtual const double* getYValues() const;
getNumberOfPoints()102     virtual int getNumberOfPoints() const { return _x.getSize(); }
103     virtual double getX(int aIndex) const;
104     virtual double getY(int aIndex) const;
getZ(int aIndex)105     virtual double getZ(int aIndex) const { return 0.0; }
106     virtual void setX(int aIndex, double aValue);
107     virtual void setY(int aIndex, double aValue);
108     virtual bool deletePoint(int aIndex);
109     virtual bool deletePoints(const Array<int>& indices);
110     virtual int addPoint(double aX, double aY);
111 
112     //--------------------------------------------------------------------------
113     // EVALUATION
114     //--------------------------------------------------------------------------
115     virtual double evaluateTotalFirstDerivative(double aX,double aDxdt) const;
116     virtual double evaluateTotalSecondDerivative(double aX,double aDxdt,double aD2xdt2) const;
117     double calcValue(const SimTK::Vector& x) const override;
118     double calcDerivative(const std::vector<int>& derivComponents, const SimTK::Vector& x) const override;
119     int getArgumentSize() const override;
120     int getMaxDerivativeOrder() const override;
121     SimTK::Function* createSimTKFunction() const override;
122 
123 //=============================================================================
124 };     // END class PiecewiseConstantFunction
125 
126 }; //namespace
127 //=============================================================================
128 //=============================================================================
129 
130 #endif // OPENSIM_PIECEWISE_CONSTANT_FUNCTION_H_
131 
132