summaryrefslogtreecommitdiff
path: root/CSXCAD/src/CSTransform.h
blob: 8293219c87680d1efdafa65fb9faa7f5d780e8e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
*	Copyright (C) 2011 Thorsten Liebig (Thorsten.Liebig@gmx.de)
*
*	This program is free software: you can redistribute it and/or modify
*	it under the terms of the GNU Lesser General Public License as published
*	by the Free Software Foundation, either version 3 of the License, or
*	(at your option) any later version.
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU Lesser General Public License for more details.
*
*	You should have received a copy of the GNU Lesser General Public License
*	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef CSTRANSFORM_H
#define CSTRANSFORM_H

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>

#include "ParameterObjects.h"

class CSXCAD_EXPORT CSTransform
{
public:
	CSTransform();
	CSTransform(CSTransform* transform);
	CSTransform(ParameterSet* paraSet);
	virtual ~CSTransform();

	void SetParameterSet(ParameterSet* paraset) {m_ParaSet=paraset;}

	enum TransformType
	{
		SCALE, SCALE3, TRANSLATE, ROTATE_ORIGIN, ROTATE_X, ROTATE_Y, ROTATE_Z, MATRIX
	}; //Keep this in sync with GetNameByType and GetTypeByName and TransformByType methods!!!

	double* Transform(const double inCoords[3], double outCoords[3]) const;
	double* InvertTransform(const double inCoords[3], double outCoords[3]) const;

	void Invert();

	double* GetMatrix() {return m_TMatrix;}

	//! Apply a matrix directly
	void SetMatrix(const double matrix[16], bool concatenate=true);
	bool SetMatrix(std::string matrix, bool concatenate=true);

	//! Create and apply a translation matrix
	void Translate(const double translate[3], bool concatenate=true);
	bool Translate(std::string translate, bool concatenate=true);

	//! Create and apply a rotation matrix around the given vector and angle
	void RotateOrigin(const double vector[3], double angle, bool concatenate=true);
	bool RotateOrigin(std::string XYZ_A, bool concatenate=true);

	void RotateX(double angle, bool concatenate=true);
	bool RotateX(std::string angle, bool concatenate=true);

	void RotateY(double angle, bool concatenate=true);
	bool RotateY(std::string angle, bool concatenate=true);

	void RotateZ(double angle, bool concatenate=true);
	bool RotateZ(std::string angle, bool concatenate=true);

	void RotateXYZ(int dir, double angle, bool concatenate=true);
	bool RotateXYZ(int dir, std::string angle, bool concatenate=true);

	void Scale(double scale, bool concatenate=true);
	void Scale(const double scale[3], bool concatenate=true);
	bool Scale(std::string scale, bool concatenate=true);

	bool TransformByString(std::string operation, std::string argument, bool concatenate=true);

	void TransformByType(TransformType type, std::vector<double> args, bool concatenate=true);
	void TransformByType(TransformType type, const double* args, bool concatenate=true);
	bool TransformByType(TransformType type, std::string args, bool concatenate=true);

	void Reset();

	//! All subsequent operations will be occur before the previous operations (not the default).
	void SetPreMultiply() {m_PostMultiply=false;}
	//! All subsequent operations will be after the previous operations (default).
	void SetPostMultiply() {m_PostMultiply=true;}

	void SetAngleDegree() {m_AngleRadian=false;}
	void SetAngleRadian() {m_AngleRadian=true;}

	double* MakeUnitMatrix(double matrix[16]) const;

	std::string GetNameByType(TransformType type) const;
	std::string GetNameByType(TransformType type, unsigned int &numArgs) const;
	int GetTypeByName(std::string name, unsigned int &numArgs) const;

	void PrintMatrix(std::ostream& stream);

	void PrintTransformations(std::ostream& stream, std::string prefix="");

	//! Write this transformations to a xml-node. \param parameterised Use false if parameters should be written as values. Parameters are lost!
	virtual bool Write2XML(TiXmlNode* root, bool parameterised=true, bool sparse=false);
	//! Read transformations from xml-node. \return Successful read-operation.
	virtual bool ReadFromXML(TiXmlNode* root);

	static CSTransform* New(TiXmlNode* root, ParameterSet* paraSet=NULL);
	static CSTransform* New(CSTransform* cst, ParameterSet* paraSet=NULL);

protected:
	//transform matrix
	double m_TMatrix[16];
	//inverse transform matrix
	double m_Inv_TMatrix[16];

	void UpdateInverse();

	bool m_PostMultiply;
	bool m_AngleRadian;

	ParameterSet* m_ParaSet;

	void ApplyMatrix(const double matrix[16], bool concatenate);

	bool RotateOriginMatrix(double matrix[16], const double XYZ_A[4]);
	bool ScaleMatrix(double matrix[16], double scale);
	bool ScaleMatrix(double matrix[16], const double scale[3]);
	bool TranslateMatrix(double matrix[16], const double translate[3]);

	void AppendList(TransformType type, const double* args, size_t numArgs );
	void AppendList(TransformType type, const ParameterScalar* args, size_t numArgs );
	std::vector<TransformType> m_TransformList;
	std::vector<std::vector <ParameterScalar> > m_TransformArguments;
};

#endif // CSTRANSFORM_H