1 /*
2 *	Copyright (C) 2008,2009,2010 Thorsten Liebig (Thorsten.Liebig@gmx.de)
3 *
4 *	This program is free software: you can redistribute it and/or modify
5 *	it under the terms of the GNU Lesser General Public License as published
6 *	by the Free Software Foundation, either version 3 of the License, or
7 *	(at your option) any later version.
8 *
9 *	This program is distributed in the hope that it will be useful,
10 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *	GNU Lesser General Public License for more details.
13 *
14 *	You should have received a copy of the GNU Lesser General Public License
15 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #pragma once
19 
20 /*
21  * Author:	Thorsten Liebig
22  * Date:	03-12-2008
23  * Lib:		CSXCAD
24  * Version:	0.1a
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string>
30 #include <vector>
31 #include <math.h>
32 #include "CSXCAD_Global.h"
33 
34 class Parameter;
35 class LinearParameter;
36 class ParameterSet;
37 class ParameterScalar;
38 class TiXmlNode;
39 class TiXmlElement;
40 
41 bool ReadTerm(ParameterScalar &PS, TiXmlElement &elem, const char* attr, double val=0.0);
42 void WriteTerm(ParameterScalar &PS, TiXmlElement &elem, const char* attr, bool mode, bool scientific=true);
43 
44 bool ReadVectorTerm(ParameterScalar PS[3], TiXmlElement &elem, std::string attr, double val=0.0, const char delimiter=',');
45 bool ReadVectorTerm(ParameterScalar PS[3], TiXmlElement &elem, const char* attr, double val=0.0, const char delimiter=',');
46 void WriteVectorTerm(ParameterScalar PS[3], TiXmlElement &elem, std::string attr, bool mode, bool scientific=true, const char delimiter=',');
47 void WriteVectorTerm(ParameterScalar PS[3], TiXmlElement &elem, const char* attr, bool mode, bool scientific=true, const char delimiter=',');
48 
49 class CSXCAD_EXPORT Parameter
50 {
51 public:
52 	Parameter();
53 	Parameter(const std::string Paraname, double val);
Parameter(const Parameter * parameter)54 	Parameter(const Parameter* parameter) {sName=std::string(parameter->sName);dValue=parameter->dValue;bModified=true;Type=parameter->Type;bSweep=parameter->bSweep;}
55 	virtual ~Parameter();
56 	enum ParameterType
57 	{
58 		Const, Linear
59 	};
GetType()60 	ParameterType GetType() {return Type;}
61 
GetName()62 	const std::string GetName() {return sName;}
SetName(const std::string Paraname)63 	void SetName(const std::string Paraname) {sName=std::string(Paraname);bModified=true;}
64 
GetValue()65 	virtual double GetValue() {return dValue;}
SetValue(double val)66 	virtual void SetValue(double val) {dValue=val;bModified=true;}
67 
GetModified()68 	bool GetModified() {return bModified;}
SetModified(bool mod)69 	void SetModified(bool mod) {bModified=mod;}
70 
GetSweep()71 	virtual bool GetSweep() {return false;}  ///const parameter can't sweep
SetSweep(bool mod)72 	void SetSweep(bool mod) {bSweep=mod;}
73 
InitSweep()74 	virtual void InitSweep() {}
Save()75 	void Save() {dValueSaved=dValue;}
Restore()76 	void Restore() {dValue=dValueSaved;}
77 
IncreaseStep()78 	virtual bool IncreaseStep() {return false;} ///return false if no more sweep step available
CountSteps()79 	virtual int CountSteps() {return 1;}
80 
81 	virtual void PrintSelf(FILE* out=stdout);
82 
83 	virtual bool Write2XML(TiXmlNode& root);
84 	virtual bool ReadFromXML(TiXmlNode &root);
85 	Parameter* GetParameterFromXML(TiXmlNode &root);
86 
Clone()87 	virtual Parameter* Clone() {return new Parameter(this);}
88 
ToConst()89 	Parameter* ToConst() { return ( this && Type == Const ) ? this : 0; } /// Cast Parameter to a more defined type. Will return null if not of the requested type.
ToLinear()90 	LinearParameter* ToLinear() { return ( this && Type == Linear ) ? (LinearParameter*) this : 0; } /// Cast Parameter to a more defined type. Will return null if not of the requested type.
91 
92 protected:
93 	std::string sName;
94 	double dValue;
95 	double dValueSaved;
96 	bool bModified;
97 	bool bSweep;
98 	ParameterType Type;
99 };
100 
101 class CSXCAD_EXPORT LinearParameter :  public Parameter
102 {
103 public:
104 	LinearParameter();
105 	LinearParameter(const std::string Paraname, double val, double min, double max, double step);
106 	//copy-constructor
107 	LinearParameter(const LinearParameter *parameter);
~LinearParameter(void)108 	virtual ~LinearParameter(void) {}
109 
110 	virtual void SetValue(double val);
111 
GetSweep()112 	virtual bool GetSweep() {return bSweep;}
113 
InitSweep()114 	virtual void InitSweep() {dValue=dMin;}
115 
116 	virtual bool IncreaseStep();
CountSteps()117 	virtual int CountSteps() {return (int)((dMax-dMin)/dStep)+1;}
118 
GetMin()119 	double GetMin() {return dMin;}
SetMin(double min)120 	void SetMin(double min) {dMin=min; if (dMax<dMin) dMax=dMin; SetValue(dValue);}
121 
GetMax()122 	double GetMax() {return dMax;}
SetMax(double max)123 	void SetMax(double max) {dMax=max; if (dMax<dMin) dMax=dMin; SetValue(dValue);}
124 
GetStep()125 	double GetStep() {return dStep;}
SetStep(double step)126 	void SetStep(double step) {dStep=step; if (dStep<0) dStep=0; SetValue(dValue);}
127 
128 	void PrintSelf(FILE* out=stdout);
129 
130 	virtual bool Write2XML(TiXmlNode& root);
131 	virtual bool ReadFromXML(TiXmlNode &root);
132 
Clone()133 	virtual Parameter* Clone() {return new LinearParameter(this);}
134 
135 protected:
136 	double dMin;
137 	double dMax;
138 	double dStep;
139 };
140 
141 
142 class CSXCAD_EXPORT ParameterSet
143 {
144 public:
145 	//! Create an empty Parameter-Set
146 	ParameterSet(void);
147 	//! Delete the Parameter-Set, including all parameter
148 	virtual ~ParameterSet(void);
149 
150 	//! This will create a clone of the parameter and insert it into the ParameterSet, caller keeps ownership of original parameter \sa LinkParameter \return number of current parameter
InsertParameter(Parameter * newPara)151 	virtual size_t InsertParameter(Parameter* newPara) {return LinkParameter(newPara->Clone());}
152 	//! This will add/link the given parameter into the ParameterSet and take ownership \return number of current parameter
153 	virtual size_t LinkParameter(Parameter* newPara);
154 	//! Same as LinkParameter \sa LinkParameter \return number of current parameter
AddParameter(Parameter * newPara)155 	virtual size_t AddParameter(Parameter* newPara) {return LinkParameter(newPara);}
156 	//! Delete a Parameter at given index \return number of current parameter
157 	virtual size_t DeleteParameter(size_t index);
158 	//! Delete a given Parameter \return number of current parameter
159 	virtual size_t DeleteParameter(Parameter* para);
160 	//! Get the Parameter at the given index
GetParameter(size_t index)161 	Parameter* GetParameter(size_t index) {if (index<vParameter.size()) return vParameter.at(index); else return NULL;}
162 
163 	//! Check whether the ParameterSet or any Parameter has been modified
164 	bool GetModified();
165 	//! Set this ParameterSet's modfication status, including all Parameter
166 	virtual void SetModified(bool mod=true);
167 
168 	//! Check whether the ParameterSet has been modified (will not check the Parameter) \sa GetModified
GetParaSetModified()169 	bool GetParaSetModified() {return bModified;}
170 	//! Set the ParameterSet's modfication status \sa SetModified
SetParaSetModified(bool val)171 	void SetParaSetModified(bool val) {bModified=val;}
172 
173 	//! Get the string of all parameter separated by the given spacer
174 	const std::string GetParameterString(const std::string spacer=",");
175 	//! Get a string of all parameter and values or only the values separated by the given spacer
176 	const std::string GetParameterValueString(const std::string spacer=",", bool ValuesOnly=false);
177 
178 	//! Get the number of parameters in this Parameter-Set
GetQtyParameter()179 	size_t GetQtyParameter() {return vParameter.size();}
180 	//! Fill a given array with the parameter values
181 	double* GetValueArray(double *array);
182 
183 	//! Get the number of necessary sweep steps for the given mode (1: full sweep, 2: sweep independently)
184 	int CountSweepSteps(int SweepMode);
185 	//! Init a sweep, will set all sweep-enabled Parameter to there initial value
186 	void InitSweep();
187 	//! This will restore all Parameter values as prior to InitSweep \sa InitSweep
188 	void EndSweep();
189 	//! Move sweep to the next step \sa InitSweep \sa CountSweepSteps
190 	bool NextSweepPos(int SweepMode);
191 
192 	//! Clear this ParameterSet, this will delete all Parameter
193 	virtual void clear();
194 
195 	void PrintSelf(FILE* out=stdout);
196 	//! Write this ParameterSet into a xml-node
197 	bool Write2XML(TiXmlNode& root);
198 	//! Read the ParameterSet from a xml-node
199 	bool ReadFromXML(TiXmlNode &root);
200 
201 protected:
202 	std::vector<Parameter* > vParameter;
203 	bool bModified;
204 	int SweepPara;
205 };
206 
207 void PSErrorCode2Msg(int code, std::string* msg);
208 std::string PSErrorCode2Msg(int code);
209 
210 class CSXCAD_EXPORT ParameterScalar
211 {
212 public:
213 	enum EvaluateErrorType
214 	{
215 		PS_NO_ERROR
216 	};
217 	ParameterScalar();
218 	ParameterScalar(ParameterSet* ParaSet, double value);
219 	ParameterScalar(ParameterSet* ParaSet, const std::string value);
220 	ParameterScalar(ParameterScalar* ps);
221 	~ParameterScalar();
222 
223 	void SetParameterSet(ParameterSet *paraSet);
224 
225 	int SetValue(const std::string value, bool Eval=true); ///returns eval-error-code
226 	void SetValue(double value);
227 
GetMode()228 	bool GetMode() const {return ParameterMode;}
GetString()229 	const std::string GetString() const {return sValue;}
230 
231 	double GetValue() const;
232 
233 	const std::string GetValueString() const;
234 
235 	//returns error-code
236 	int Evaluate();
237 
238 	double GetEvaluated(double* ParaValues, int &EC);
239 
240 	// Copy all values and parameter from ps to this.
241 	void Copy(ParameterScalar* ps);
242 
243 protected:
244 	ParameterSet* clParaSet;
245 	bool bModified;
246 	bool ParameterMode;
247 	std::string sValue;
248 	double dValue;
249 };
250