1 #pragma once
2 
3 #ifndef Y_PARAMS_H
4 #define Y_PARAMS_H
5 
6 #include <yafray_constants.h>
7 #include "matrix4.h"
8 #include "color.h"
9 #include <map>
10 #include <string>
11 
12 __BEGIN_YAFRAY
13 
14 
15 #define TYPE_INT    1
16 #define TYPE_BOOL   2
17 #define TYPE_FLOAT  3
18 #define TYPE_STRING 4
19 #define TYPE_POINT  5
20 #define TYPE_COLOR  6
21 #define TYPE_NONE   -1
22 
23 /*! a class that can hold exactly one value of a range types.
24 */
25 class YAFRAYCORE_EXPORT parameter_t
26 {
27 	public:
parameter_t(const std::string & s)28 		parameter_t(const std::string &s): used(false), vtype(TYPE_STRING) { str=s; }
parameter_t(int i)29 		parameter_t(int i): used(false), vtype(TYPE_INT) { ival=i; }
parameter_t(bool b)30 		parameter_t(bool b): used(false), vtype(TYPE_BOOL) { bval=b; }
parameter_t(float f)31 		parameter_t(float f): used(false), vtype(TYPE_FLOAT) { fval=f; }
parameter_t(double f)32 		parameter_t(double f): used(false), vtype(TYPE_FLOAT) { fval=f; }
parameter_t(const colorA_t & c)33 		parameter_t(const colorA_t &c): used(false), vtype(TYPE_COLOR) { C[0]=c.R, C[1]=c.G, C[2]=c.B, C[3]=c.A; }
parameter_t(const point3d_t & p)34 		parameter_t(const point3d_t &p): used(false), vtype(TYPE_POINT){ P[0]=p.x, P[1]=p.y, P[2]=p.z; }
35 //		parameter_t(const parameter_t &p);
parameter_t()36 		parameter_t(): used(false), vtype(TYPE_NONE) {};
~parameter_t()37 		~parameter_t(){};
38 
39 		// return parameter value in reference parameter, return true if
40 		// the parameter type matches, false otherwise.
getVal(std::string & s)41 		bool getVal(std::string &s) const {used=true; if(vtype==TYPE_STRING){s=str; return true;}return false;};
getVal(const std::string * & s)42 		bool getVal(const std::string *&s) const {used=true; if(vtype==TYPE_STRING){s=&str; return true;}return false;};
getVal(int & i)43 		bool getVal(int &i)			const {used=true; if(vtype==TYPE_INT)  {i=ival; return true;} return false;};
getVal(bool & b)44 		bool getVal(bool &b)		const {used=true; if(vtype==TYPE_BOOL)  {b=bval; return true;} return false;};
getVal(float & f)45 		bool getVal(float &f)		const {used=true; if(vtype==TYPE_FLOAT){f=(float)fval; return true;} return false;};
getVal(double & f)46 		bool getVal(double &f)		const {used=true; if(vtype==TYPE_FLOAT){f=fval; return true;} return false;};
getVal(point3d_t & p)47 		bool getVal(point3d_t &p)	const {used=true; if(vtype==TYPE_POINT){p.x=P[0], p.y=P[1], p.z=P[2]; return true;} return false;};
getVal(color_t & c)48 		bool getVal(color_t &c)		const {used=true; if(vtype==TYPE_COLOR){c.R=C[0], c.G=C[1], c.B=C[2]; return true;} return false;};
getVal(colorA_t & c)49 		bool getVal(colorA_t &c)	const {used=true; if(vtype==TYPE_COLOR){c.R=C[0], c.G=C[1], c.B=C[2], c.A=C[3]; return true;} return false;};
50 		//! return the type of the parameter_t
type()51 		int type()const { return vtype; }
52 		// operator= assigns new value, be aware that this may change the parameter type!
53 		parameter_t &operator = (const std::string &s) { vtype=TYPE_STRING; str=s; return *this; }
54 		parameter_t &operator = (int i) { vtype=TYPE_INT; ival=i; return *this; }
55 		parameter_t &operator = (bool b) { vtype=TYPE_BOOL; bval=b; return *this; }
56 		parameter_t &operator = (float f) { vtype=TYPE_FLOAT; fval=f; return *this; }
57 		parameter_t &operator = (const point3d_t &p) { vtype=TYPE_POINT; P[0]=p.x, P[1]=p.y, P[2]=p.z; return *this; }
58 		parameter_t &operator = (const colorA_t &c) { vtype=TYPE_COLOR; C[0]=c.R, C[1]=c.G, C[2]=c.B, C[3]=c.A; return *this; }
59 
60 		mutable bool used; //! indicate whether the parameter was read (sucessfully) before
61 	protected:
62 		std::string str;
63 		//point3d_t P;
64 		//colorA_t C;
65 		union
66 		{
67 			int ival;
68 			double fval;
69 			bool bval;
70 			float C[4];
71 			float P[4];
72 			//float matrix[4][4];
73 		};
74 		int vtype; //!< type of the stored value
75 };
76 
77 class YAFRAYCORE_EXPORT paraMap_t
78 {
79 	public:
paraMap_t()80 		paraMap_t(){}
81 		//! template function to get a value, available types are those of parameter_t::getVal()
82 		template <class T>
getParam(const std::string & name,T & val)83 		bool getParam(const std::string &name, T &val) const
84 		{
85 			auto i=dicc.find(name);
86 			if(i != dicc.end() ) return i->second.getVal(val);
87 			return false;
88 		}
89 
getMatrix(const std::string & name,matrix4x4_t & m)90 		bool getMatrix(const std::string &name, matrix4x4_t &m) const
91 		{
92 			auto i=mdicc.find(name);
93 			if(i != mdicc.end() ){ m = i->second; return true; }
94 			return false;
95 		}
96 //		virtual bool includes(const std::string &label,int type)const;
97 //		virtual void checkUnused(const std::string &env)const;
98 		parameter_t & operator [] (const std::string &key){ return dicc[key]; };
setMatrix(const std::string & key,const matrix4x4_t & m)99 		void setMatrix(const std::string &key, const matrix4x4_t &m){ mdicc[key] = m; }
100 
clear()101 		void clear() { dicc.clear(); mdicc.clear(); }
102 		//! get the actualy parameter dictionary;
103 		/*	Usefull e.g. to dump it to file, since we don't provide iterators etc (yet)... */
getDict()104 		const std::map<std::string,parameter_t>* getDict() const{ return &dicc; }
getMDict()105 		const std::map<std::string,matrix4x4_t>* getMDict() const{ return &mdicc; }
~paraMap_t()106 		virtual ~paraMap_t(){};
107 	protected:
108 		std::map<std::string,parameter_t> dicc;
109 		std::map<std::string,matrix4x4_t> mdicc;
110 };
111 
112 
113 __END_YAFRAY
114 #endif // Y_PARAMS_H
115