1 /*
2  * SymmetryCalculator.h
3  *
4  * Copyright Notice: see copyright.txt
5  *
6  * Date: 3/8/2012 (D/M/Y)
7  * Author: Dmitrij Lioubartsev
8  * Email: dmitrijl42@gmail.com
9  *
10  * A symmetry is a string that holds mathematic operations for
11  * three coordinates separated by commas. Eg. "x+1/2,y+1/2,z+1/2".
12  * x,y,z are the current atom's coordinates. The three expressions give
13  * a new value for each coordinate. This class calculates each expression.
14  *
15  * Esentially all common expressions using +,-,*,/,(,) are allowed. Syntactic
16  * sugar as '3x' or 'x(1+2)4' is allowed as well. The parsing process will simply add *
17  * to compensate.
18  *
19  * How to use this class:
20  *
21  * 1. Use the constructor (does nothing)
22  * 2. setSymmetry(Symmetry string)
23  * 			This loads and parses the symmetry string
24  * 3. loadAtom - input atom coordinates
25  * 4. call calculate();
26  * 5. Use getx(), gety(), getz() to fetch calculated values.
27  *
28  */
29 
30 #ifndef SYMMETRYCALCULATOR_H_
31 #define SYMMETRYCALCULATOR_H_
32 
33 #include <string>
34 using namespace std;
35 
36 class SymmetryCalculator {
37 private:
38 	string xyz[3]; //the strings of the symmetry
39 	double xyzNVal[3]; //the new atom values, after symmetry is applied.
40 	double xyzOVal[3]; //the old atom values, before symmetry is applied.
41 	struct res {
42 		double result;
43 		int ip;
44 	};
45 
46 public:
47 	SymmetryCalculator();
48 	virtual ~SymmetryCalculator();
49 
50 	bool setSymmetry(const string& symmetry);
51 	void loadAtom(double x, double y, double z);
52 	bool calculate();
53 
54 	//getters
55 	double getx();
56 	double gety();
57 	double getz();
58 
59 private:
60 	//here follows the private intern calculation methods.
61 	//this calculates all terms (+ and -)
62 	res calculateTerms(string& expr, int expp);
63 	//this calculates all products (* and /)
64 	res calculateProducts(string& expr, int expp);
65 	//this fetches a value. If its a parenthesis expression, uses recursion
66 	res getValue(string& expr, int expp);
67 
68 };
69 
70 #endif /* SYMMETRYCALCULATOR_H_ */
71