/*
    QGL Transform
    Ivan Pelipenko peri4ko@yandex.ru

    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 GLTRANSFORM_H
#define GLTRANSFORM_H

#include "qglengine_core_export.h"

#include <QMatrix4x4>
#include <QQuaternion>
#include <QVector3D>
#include <chunkstream.h>


class QGLENGINE_CORE_EXPORT Transform {
	friend QDataStream & operator>>(QDataStream & s, Transform & v);

public:
	Transform();
	Transform & operator=(const Transform & t);

	float scale() const;
	QVector3D scale3D() const;
	QVector3D rotation() const;
	QVector3D translation() const;
	QMatrix4x4 matrix() const;
	QMatrix4x4 matrixRotate() const;
	QMatrix4x4 matrixScale() const;
	QMatrix4x4 matrixRotateScale() const;
	QVector3D direction() const;

	float rotationX() const;
	float rotationY() const;
	float rotationZ() const;

	void setScale(float s) { setScale(QVector3D(s, s, s)); }
	void setScale(const QVector3D & s);
	void setScaleX(float s);
	void setScaleY(float s);
	void setScaleZ(float s);

	void setRotation(const QVector3D & r);
	void setRotationX(float r);
	void setRotationY(float r);
	void setRotationZ(float r);

	void setTranslation(const QVector3D & t);
	void setTranslationX(float t);
	void setTranslationY(float t);
	void setTranslationZ(float t);

	void setMatrix(const QMatrix4x4 & matrix);
	void setDirty(bool yes = true) { m_matrixDirty = yes; }


	static QQuaternion fromAxisAndAngle(const QVector3D & axis, float angle);
	static QQuaternion fromAxisAndAngle(float x, float y, float z, float angle);

	static QQuaternion fromAxesAndAngles(const QVector3D & axis1, float angle1, const QVector3D & axis2, float angle2);
	static QQuaternion
	fromAxesAndAngles(const QVector3D & axis1, float angle1, const QVector3D & axis2, float angle2, const QVector3D & axis3, float angle3);
	static QQuaternion fromAxes(const QVector3D & xAxis, const QVector3D & yAxis, const QVector3D & zAxis);

	static QVector3D fromDirection(QVector3D d, float pitch = 0.f);
	static QVector3D fromRotationMatrix(const QMatrix3x3 & m);
	static QMatrix3x3 toRotationMatrix(const QVector3D & r);

	static QMatrix4x4 rotateAround(const QVector3D & point, float angle, const QVector3D & axis);
	static QMatrix4x4 rotateFromAxes(const QVector3D & xAxis, const QVector3D & yAxis, const QVector3D & zAxis);

protected:
	void buildMatrix() const;

	QVector3D m_scale;
	QVector3D m_translation;
	QVector3D m_eulerRotationAngles;

	mutable QMatrix4x4 m_matrix, m_matrixWT, m_matrixR, m_matrixS;
	mutable bool m_matrixDirty;
};


inline QDataStream & operator<<(QDataStream & s, const Transform & v) {
	s << v.matrix();
	return s;
}
inline QDataStream & operator>>(QDataStream & s, Transform & v) {
	QMatrix4x4 m;
	s >> m;
	v.setMatrix(m);
	v.m_matrixDirty = true;
	return s;
}

#endif // QT3DCORE_QTRANSFORM_H
