mbsim  4.0.0
MBSim Kernel
bounded_function.h
1/* Copyright (C) 2004-2014 MBSim Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 * Contact: martin.o.foerg@gmail.com
18 */
19
20#ifndef _BOUNDED_FUNCTION_H_
21#define _BOUNDED_FUNCTION_H_
22
23#include "mbsim/functions/function.h"
24#include "mbsim/utils/utils.h"
25#include <limits>
26
27namespace MBSim {
28
29 template<typename Sig> class BoundedFunction;
30
31 template<typename Ret, typename Arg>
32 class BoundedFunction<Ret(Arg)> : public Function<Ret(Arg)> {
33 private:
34 double lowerBound, upperBound;
35 public:
36 BoundedFunction() : lowerBound(-std::numeric_limits<double>::max()), upperBound(std::numeric_limits<double>::max()) { }
37 void setLowerBound(double lowerBound_) { lowerBound = lowerBound_; }
38 void setUpperBound(double upperBound_) { upperBound = upperBound_; }
39 int getArgSize() const override { return 1; }
40 std::pair<int, int> getRetSize() const override { return std::make_pair(1,1); }
41 Ret operator()(const Arg &x_) override {
42 double x = ToDouble<Arg>::cast(x_);
43 if(x<lowerBound)
44 x = lowerBound;
45 if(x>upperBound)
46 x = upperBound;
47 return FromDouble<Ret>::cast(x);
48 }
49 void initializeUsingXML(xercesc::DOMElement *element) override {
50 xercesc::DOMElement *e;
51 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"lowerBound");
52 setLowerBound(MBXMLUtils::E(e)->getText<double>());
53 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"upperBound");
54 setUpperBound(MBXMLUtils::E(e)->getText<double>());
55 }
56 };
57
58}
59
60#endif
Definition: bounded_function.h:29
Definition: utils.h:89
Definition: function.h:53
Definition: utils.h:61
namespace MBSim
Definition: bilateral_constraint.cc:30