HyperspaceExplorer 0.7.1
|
00001 /* 00002 Hyperspace Explorer - vizualizing higher-dimensional geometry 00003 Copyright (C) 2010 Lene Preuss <lene.preuss@gmail.com> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License along 00016 with this program; if not, write to the Free Software Foundation, Inc., 00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00018 (Seriously, has anyone ever done this?) 00019 00020 */ 00021 00022 #ifndef VECTORFUNCTION_H 00023 #define VECTORFUNCTION_H 00024 00025 #include "ParameterMap.h" 00026 #include "Vector.h" 00027 00028 #include <memory> 00029 00031 00054 template <unsigned N, unsigned P, typename NUM = double> 00055 class ParametricFunction { 00056 00057 public: 00058 00060 typedef VecMath::Vector<P, NUM> argument_type; 00062 typedef VecMath::Vector<N, NUM> return_type; 00063 00064 ParametricFunction(): 00065 _function_name(), _function_description(), 00066 _parameters(), 00067 _default_x_min(-1.), _default_x_max(1.) { } 00068 00069 virtual ~ParametricFunction() { } 00070 00072 00076 virtual return_type f(const argument_type &x) = 0; 00077 00079 std::string getName() const { return _function_name; } 00080 00082 std::string getDescription() const { return _function_description; } 00083 00085 unsigned getNumParameters() const { return _parameters.size(); } 00086 00088 ParameterMap getParameterMap() const { return _parameters; } 00089 00091 FunctionParameter::parameter_ptr_type getParameter(const std::string &name) { 00092 return FunctionParameter::parameter_ptr_type(_parameters.getParameter(name)); 00093 } 00094 00096 FunctionParameter::value_ptr_type getParameterValue(const std::string &name) { 00097 return FunctionParameter::value_ptr_type(_parameters.getValue(name)); 00098 } 00099 00101 argument_type getDefaultXMin() const { return _default_x_min; } 00103 argument_type getDefaultXMax() const { return _default_x_max; } 00104 00105 protected: 00106 00108 void setName(const std::string &newName) { _function_name = newName; } 00109 00111 void setDescription(const std::string &newDescription) { _function_description = newDescription; } 00112 00114 00116 template <typename T> void declareParameter(const std::string ¶meter_name, 00117 const T ¶meter_default_value); 00118 00120 00122 template <typename T> void declareParameter(const std::string ¶meter_name, 00123 const T ¶meter_default_value, 00124 const T ¶meter_value); 00125 00127 void setDefaultBoundaries(const ParametricFunction::argument_type &x_min, 00128 const ParametricFunction::argument_type &x_max); 00129 00130 private: 00131 00133 std::string _function_name; 00135 std::string _function_description; 00137 ParameterMap _parameters; 00139 argument_type _default_x_min; 00141 argument_type _default_x_max; 00142 }; 00143 00147 template <unsigned N, unsigned P, typename NUM> 00148 inline 00149 void ParametricFunction<N, P, NUM>::setDefaultBoundaries( 00150 const ParametricFunction<N, P, NUM>::argument_type& x_min, 00151 const ParametricFunction<N, P, NUM>::argument_type& x_max) { 00152 _default_x_min = x_min; 00153 _default_x_max = x_max; 00154 } 00155 00159 template <unsigned N, unsigned P, typename NUM> 00160 template<typename T> 00161 inline 00162 void ParametricFunction<N, P, NUM>::declareParameter( 00163 const std::string ¶meter_name, 00164 const T ¶meter_default_value) { 00165 if (_parameters.find(parameter_name) != _parameters.end()) return; 00166 00167 _parameters.insertByDefault(parameter_name, parameter_default_value); 00168 } 00169 00174 template <unsigned N, unsigned P, typename NUM> 00175 template<typename T> 00176 inline 00177 void ParametricFunction<N, P, NUM>::declareParameter( 00178 const std::string& parameter_name, 00179 const T& parameter_default_value, 00180 const T& parameter_value) { 00181 declareParameter(parameter_name, parameter_default_value); 00182 _parameters[parameter_name]->setValue(new FunctionParameterValue<T>(parameter_value)); 00183 } 00184 00185 #endif // VECTORFUNCTION_H