2010-03-01 23:08:26 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-03-01 23:08:26 +00:00
|
|
|
|
|
|
|
#ifndef GFX_3DMATRIX_H
|
|
|
|
#define GFX_3DMATRIX_H
|
|
|
|
|
|
|
|
#include <gfxTypes.h>
|
2014-08-29 18:47:30 +00:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2011-10-06 21:22:18 +00:00
|
|
|
#include <gfxQuad.h>
|
2010-03-01 23:08:26 +00:00
|
|
|
|
2014-06-25 11:59:46 +00:00
|
|
|
class gfxMatrix;
|
2013-08-23 17:22:49 +00:00
|
|
|
|
2010-03-01 23:08:26 +00:00
|
|
|
/**
|
|
|
|
* This class represents a 3D transformation. The matrix is laid
|
|
|
|
* out as follows:
|
|
|
|
*
|
|
|
|
* _11 _12 _13 _14
|
|
|
|
* _21 _22 _23 _24
|
|
|
|
* _31 _32 _33 _34
|
|
|
|
* _41 _42 _43 _44
|
|
|
|
*
|
|
|
|
* This matrix is treated as row-major. Assuming we consider our vectors row
|
|
|
|
* vectors, this matrix type will be identical in memory to the OpenGL and D3D
|
|
|
|
* matrices. OpenGL matrices are column-major, however OpenGL also treats
|
|
|
|
* vectors as column vectors, the double transposition makes everything work
|
|
|
|
* out nicely.
|
|
|
|
*/
|
2013-05-29 21:59:24 +00:00
|
|
|
class gfx3DMatrix
|
2010-03-01 23:08:26 +00:00
|
|
|
{
|
2014-08-29 18:47:30 +00:00
|
|
|
typedef mozilla::gfx::Point3D Point3D;
|
2014-08-29 18:47:30 +00:00
|
|
|
typedef mozilla::gfx::Point4D Point4D;
|
2010-03-01 23:08:26 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create matrix.
|
|
|
|
*/
|
2011-07-22 22:28:33 +00:00
|
|
|
gfx3DMatrix(void);
|
2010-03-01 23:08:26 +00:00
|
|
|
|
2014-08-27 22:38:42 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& stream, const gfx3DMatrix& m) {
|
|
|
|
if (m.IsIdentity()) {
|
|
|
|
return stream << "[ I ]";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.Is2D()) {
|
|
|
|
return stream << "["
|
|
|
|
<< m._11 << " " << m._12 << "; "
|
|
|
|
<< m._21 << " " << m._22 << "; "
|
|
|
|
<< m._41 << " " << m._42
|
|
|
|
<< "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream << "["
|
|
|
|
<< m._11 << " " << m._12 << " " << m._13 << " " << m._14 << "; "
|
|
|
|
<< m._21 << " " << m._22 << " " << m._23 << " " << m._24 << "; "
|
|
|
|
<< m._31 << " " << m._32 << " " << m._33 << " " << m._34 << "; "
|
|
|
|
<< m._41 << " " << m._42 << " " << m._43 << " " << m._44
|
|
|
|
<< "]";
|
|
|
|
}
|
|
|
|
|
2010-03-01 23:08:26 +00:00
|
|
|
/**
|
|
|
|
* Matrix multiplication.
|
|
|
|
*/
|
2011-07-22 22:28:33 +00:00
|
|
|
gfx3DMatrix operator*(const gfx3DMatrix &aMatrix) const;
|
|
|
|
gfx3DMatrix& operator*=(const gfx3DMatrix &aMatrix);
|
2010-03-01 23:08:26 +00:00
|
|
|
|
2014-08-29 18:47:30 +00:00
|
|
|
Point4D& operator[](int aIndex)
|
2011-08-27 00:06:03 +00:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(aIndex >= 0 && aIndex <= 3, "Invalid matrix array index");
|
2014-08-29 18:47:30 +00:00
|
|
|
return *reinterpret_cast<Point4D*>((&_11)+4*aIndex);
|
2011-08-27 00:06:03 +00:00
|
|
|
}
|
2014-08-29 18:47:30 +00:00
|
|
|
const Point4D& operator[](int aIndex) const
|
2011-08-27 00:06:03 +00:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(aIndex >= 0 && aIndex <= 3, "Invalid matrix array index");
|
2014-08-29 18:47:30 +00:00
|
|
|
return *reinterpret_cast<const Point4D*>((&_11)+4*aIndex);
|
2011-08-27 00:06:03 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 20:35:49 +00:00
|
|
|
/**
|
|
|
|
* Return true if this matrix and |aMatrix| are the same matrix.
|
|
|
|
*/
|
2011-07-22 22:28:33 +00:00
|
|
|
bool operator==(const gfx3DMatrix& aMatrix) const;
|
2012-08-29 05:47:18 +00:00
|
|
|
bool operator!=(const gfx3DMatrix& aMatrix) const;
|
2012-10-17 08:00:00 +00:00
|
|
|
|
|
|
|
bool FuzzyEqual(const gfx3DMatrix& aMatrix) const;
|
2011-07-22 22:28:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Divide all values in the matrix by a scalar value
|
|
|
|
*/
|
|
|
|
gfx3DMatrix& operator/=(gfxFloat scalar);
|
2010-06-21 20:35:49 +00:00
|
|
|
|
2010-03-01 23:08:26 +00:00
|
|
|
/**
|
|
|
|
* Create a 3D matrix from a gfxMatrix 2D affine transformation.
|
|
|
|
*
|
|
|
|
* \param aMatrix gfxMatrix 2D affine transformation.
|
|
|
|
*/
|
2011-07-22 22:28:33 +00:00
|
|
|
static gfx3DMatrix From2D(const gfxMatrix &aMatrix);
|
2010-03-01 23:08:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the matrix is isomorphic to a 2D affine transformation
|
|
|
|
* (i.e. as obtained by From2D). If it is, optionally returns the 2D
|
|
|
|
* matrix in aMatrix.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool Is2D(gfxMatrix* aMatrix) const;
|
|
|
|
bool Is2D() const;
|
2010-03-01 23:08:26 +00:00
|
|
|
|
2011-08-27 00:04:06 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the matrix can be reduced to a 2D affine transformation
|
|
|
|
* (i.e. as obtained by From2D). If it is, optionally returns the 2D
|
|
|
|
* matrix in aMatrix. This should only be used on matrices required for
|
2012-02-17 05:06:46 +00:00
|
|
|
* rendering, not for intermediate calculations. It is assumed that the 2D
|
|
|
|
* matrix will only be used for transforming objects on to the z=0 plane,
|
|
|
|
* therefore any z-component perspective is ignored. This means that if
|
|
|
|
* aMatrix is applied to objects with z != 0, the results may be incorrect.
|
2011-08-27 00:04:06 +00:00
|
|
|
*
|
|
|
|
* Since drawing is to a 2d plane, any 3d transform without perspective
|
|
|
|
* can be reduced by dropping the z row and column.
|
|
|
|
*/
|
2012-07-30 14:20:58 +00:00
|
|
|
bool CanDraw2D(gfxMatrix* aMatrix = nullptr) const;
|
2011-08-27 00:04:06 +00:00
|
|
|
|
2011-10-06 21:29:07 +00:00
|
|
|
/**
|
|
|
|
* Converts the matrix to one that doesn't modify the z coordinate of points,
|
|
|
|
* but leaves the rest of the transformation unchanged.
|
|
|
|
*/
|
|
|
|
gfx3DMatrix& ProjectTo2D();
|
|
|
|
|
2010-03-01 23:08:26 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the matrix is the identity matrix. The most important
|
|
|
|
* property we require is that gfx3DMatrix().IsIdentity() returns true.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsIdentity() const;
|
2011-07-22 22:28:33 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
/**
|
|
|
|
* Pre-multiplication transformation functions:
|
|
|
|
*
|
|
|
|
* These functions construct a temporary matrix containing
|
|
|
|
* a single transformation and pre-multiply it onto the current
|
|
|
|
* matrix.
|
|
|
|
*/
|
|
|
|
|
2011-08-27 00:06:03 +00:00
|
|
|
/**
|
|
|
|
* Add a translation by aPoint to the matrix.
|
2011-09-26 21:54:45 +00:00
|
|
|
*
|
|
|
|
* This creates this temporary matrix:
|
|
|
|
* | 1 0 0 0 |
|
|
|
|
* | 0 1 0 0 |
|
|
|
|
* | 0 0 1 0 |
|
|
|
|
* | aPoint.x aPoint.y aPoint.z 1 |
|
2011-08-27 00:06:03 +00:00
|
|
|
*/
|
2014-08-29 18:47:30 +00:00
|
|
|
void Translate(const Point3D& aPoint);
|
2011-08-27 00:06:03 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
/**
|
|
|
|
* Skew the matrix.
|
|
|
|
*
|
|
|
|
* This creates this temporary matrix:
|
|
|
|
* | 1 tan(aYSkew) 0 0 |
|
|
|
|
* | tan(aXSkew) 1 0 0 |
|
|
|
|
* | 0 0 1 0 |
|
|
|
|
* | 0 0 0 1 |
|
|
|
|
*/
|
|
|
|
void SkewXY(double aXSkew, double aYSkew);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scale the matrix
|
|
|
|
*
|
|
|
|
* This creates this temporary matrix:
|
|
|
|
* | aX 0 0 0 |
|
|
|
|
* | 0 aY 0 0 |
|
|
|
|
* | 0 0 aZ 0 |
|
|
|
|
* | 0 0 0 1 |
|
|
|
|
*/
|
|
|
|
void Scale(float aX, float aY, float aZ);
|
|
|
|
|
2012-03-12 20:32:02 +00:00
|
|
|
/**
|
|
|
|
* Return the currently set scaling factors.
|
|
|
|
*/
|
|
|
|
float GetXScale() const { return _11; }
|
|
|
|
float GetYScale() const { return _22; }
|
|
|
|
float GetZScale() const { return _33; }
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
/**
|
|
|
|
* Rotate around the X axis..
|
|
|
|
*
|
|
|
|
* This creates this temporary matrix:
|
|
|
|
* | 1 0 0 0 |
|
|
|
|
* | 0 cos(aTheta) sin(aTheta) 0 |
|
|
|
|
* | 0 -sin(aTheta) cos(aTheta) 0 |
|
|
|
|
* | 0 0 0 1 |
|
|
|
|
*/
|
|
|
|
void RotateX(double aTheta);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotate around the Y axis..
|
|
|
|
*
|
|
|
|
* This creates this temporary matrix:
|
|
|
|
* | cos(aTheta) 0 -sin(aTheta) 0 |
|
|
|
|
* | 0 1 0 0 |
|
|
|
|
* | sin(aTheta) 0 cos(aTheta) 0 |
|
|
|
|
* | 0 0 0 1 |
|
|
|
|
*/
|
|
|
|
void RotateY(double aTheta);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotate around the Z axis..
|
|
|
|
*
|
|
|
|
* This creates this temporary matrix:
|
|
|
|
* | cos(aTheta) sin(aTheta) 0 0 |
|
|
|
|
* | -sin(aTheta) cos(aTheta) 0 0 |
|
|
|
|
* | 0 0 1 0 |
|
|
|
|
* | 0 0 0 1 |
|
|
|
|
*/
|
|
|
|
void RotateZ(double aTheta);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply perspective to the matrix.
|
|
|
|
*
|
|
|
|
* This creates this temporary matrix:
|
|
|
|
* | 1 0 0 0 |
|
|
|
|
* | 0 1 0 0 |
|
|
|
|
* | 0 0 1 -1/aDepth |
|
|
|
|
* | 0 0 0 1 |
|
|
|
|
*/
|
|
|
|
void Perspective(float aDepth);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pre multiply an existing matrix onto the current
|
|
|
|
* matrix
|
|
|
|
*/
|
|
|
|
void PreMultiply(const gfx3DMatrix& aOther);
|
|
|
|
void PreMultiply(const gfxMatrix& aOther);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post-multiplication transformation functions:
|
|
|
|
*
|
|
|
|
* These functions construct a temporary matrix containing
|
|
|
|
* a single transformation and post-multiply it onto the current
|
|
|
|
* matrix.
|
|
|
|
*/
|
2012-08-03 21:29:22 +00:00
|
|
|
|
2011-08-27 00:07:05 +00:00
|
|
|
/**
|
|
|
|
* Add a translation by aPoint after the matrix.
|
|
|
|
* This is functionally equivalent to:
|
2011-09-26 21:54:45 +00:00
|
|
|
* matrix * gfx3DMatrix::Translation(aPoint)
|
2011-08-27 00:07:05 +00:00
|
|
|
*/
|
2014-08-29 18:47:30 +00:00
|
|
|
void TranslatePost(const Point3D& aPoint);
|
2011-08-27 00:07:05 +00:00
|
|
|
|
2012-08-03 21:29:22 +00:00
|
|
|
void ScalePost(float aX, float aY, float aZ);
|
|
|
|
|
2014-07-22 22:05:13 +00:00
|
|
|
/**
|
|
|
|
* Let T be the transformation matrix translating points in the coordinate
|
|
|
|
* space with origin aOrigin to the coordinate space used by this matrix.
|
|
|
|
* If this matrix is M, this function changes it to be (T-1)MT, the matrix
|
|
|
|
* that's equivalent to M but in the coordinate space that treats aOrigin
|
|
|
|
* as the origin.
|
|
|
|
*
|
|
|
|
* @param aOrigin The origin to translate to
|
|
|
|
* @return The modified matrix
|
|
|
|
*/
|
2014-08-29 18:47:30 +00:00
|
|
|
void ChangeBasis(const Point3D& aOrigin);
|
2014-07-22 22:05:13 +00:00
|
|
|
|
2011-07-22 22:28:33 +00:00
|
|
|
/**
|
|
|
|
* Transforms a point according to this matrix.
|
|
|
|
*/
|
|
|
|
gfxPoint Transform(const gfxPoint& point) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms a rectangle according to this matrix
|
|
|
|
*/
|
|
|
|
gfxRect TransformBounds(const gfxRect& rect) const;
|
|
|
|
|
2011-10-06 21:22:18 +00:00
|
|
|
|
|
|
|
gfxQuad TransformRect(const gfxRect& aRect) const;
|
|
|
|
|
2011-08-03 03:04:20 +00:00
|
|
|
/**
|
|
|
|
* Transforms a 3D vector according to this matrix.
|
|
|
|
*/
|
2014-08-29 18:47:30 +00:00
|
|
|
Point3D Transform3D(const Point3D& point) const;
|
2014-08-29 18:47:30 +00:00
|
|
|
Point4D Transform4D(const Point4D& aPoint) const;
|
2011-08-03 03:04:20 +00:00
|
|
|
|
2014-02-03 23:25:22 +00:00
|
|
|
/**
|
2014-07-08 04:55:42 +00:00
|
|
|
* Given a point (x,y) find a value for z such that (x,y,z,1) transforms
|
|
|
|
* into (x',y',0,w') and returns the latter.
|
2014-02-03 23:25:22 +00:00
|
|
|
*/
|
2014-08-29 18:47:30 +00:00
|
|
|
Point4D ProjectPoint(const gfxPoint& aPoint) const;
|
2011-08-03 03:04:20 +00:00
|
|
|
|
2011-07-22 22:28:33 +00:00
|
|
|
/**
|
|
|
|
* Inverts this matrix, if possible. Otherwise, the matrix is left
|
|
|
|
* unchanged.
|
|
|
|
*/
|
2011-08-27 00:06:03 +00:00
|
|
|
gfx3DMatrix Inverse() const;
|
2011-07-22 22:28:33 +00:00
|
|
|
|
2011-08-27 00:06:03 +00:00
|
|
|
gfx3DMatrix& Invert()
|
2011-08-03 03:04:20 +00:00
|
|
|
{
|
2011-08-27 00:06:03 +00:00
|
|
|
*this = Inverse();
|
|
|
|
return *this;
|
2011-08-03 03:04:20 +00:00
|
|
|
}
|
|
|
|
|
2011-08-03 03:04:22 +00:00
|
|
|
/**
|
|
|
|
* Returns a unit vector that is perpendicular to the plane formed
|
|
|
|
* by transform the screen plane (z=0) by this matrix.
|
|
|
|
*/
|
2014-08-29 18:47:30 +00:00
|
|
|
Point3D GetNormalVector() const;
|
2011-08-03 03:04:22 +00:00
|
|
|
|
2011-10-12 03:16:06 +00:00
|
|
|
/**
|
|
|
|
* Returns true if a plane transformed by this matrix will
|
|
|
|
* have it's back face visible.
|
|
|
|
*/
|
|
|
|
bool IsBackfaceVisible() const;
|
|
|
|
|
2011-07-22 22:28:33 +00:00
|
|
|
/**
|
|
|
|
* Check if matrix is singular (no inverse exists).
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsSingular() const;
|
2010-03-01 23:08:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a translation matrix.
|
|
|
|
*
|
|
|
|
* \param aX Translation on X-axis.
|
|
|
|
* \param aY Translation on Y-axis.
|
|
|
|
* \param aZ Translation on Z-axis.
|
|
|
|
*/
|
2011-07-22 22:28:33 +00:00
|
|
|
static gfx3DMatrix Translation(float aX, float aY, float aZ);
|
2014-08-29 18:47:30 +00:00
|
|
|
static gfx3DMatrix Translation(const Point3D& aPoint);
|
2010-03-01 23:08:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a scale matrix. Scales uniformly along all axes.
|
|
|
|
*
|
|
|
|
* \param aScale Scale factor
|
|
|
|
*/
|
2011-08-27 00:06:03 +00:00
|
|
|
static gfx3DMatrix ScalingMatrix(float aFactor);
|
2010-03-01 23:08:26 +00:00
|
|
|
|
2010-05-25 06:35:35 +00:00
|
|
|
/**
|
|
|
|
* Create a scale matrix.
|
|
|
|
*/
|
2011-08-27 00:06:03 +00:00
|
|
|
static gfx3DMatrix ScalingMatrix(float aX, float aY, float aZ);
|
|
|
|
|
|
|
|
gfxFloat Determinant() const;
|
2011-07-22 22:28:33 +00:00
|
|
|
|
2012-08-25 04:41:46 +00:00
|
|
|
void NudgeToIntegers(void);
|
2014-05-12 19:37:21 +00:00
|
|
|
void NudgeToIntegersFixedEpsilon();
|
2012-08-25 04:41:46 +00:00
|
|
|
|
2011-07-22 22:28:33 +00:00
|
|
|
private:
|
|
|
|
|
2011-08-27 00:06:03 +00:00
|
|
|
gfxFloat Determinant3x3() const;
|
|
|
|
gfx3DMatrix Inverse3x3() const;
|
2011-07-22 22:28:33 +00:00
|
|
|
|
2011-08-27 00:07:05 +00:00
|
|
|
gfx3DMatrix Multiply2D(const gfx3DMatrix &aMatrix) const;
|
|
|
|
|
2011-07-22 22:28:33 +00:00
|
|
|
public:
|
2010-05-25 06:35:35 +00:00
|
|
|
|
2010-03-01 23:08:26 +00:00
|
|
|
/** Matrix elements */
|
|
|
|
float _11, _12, _13, _14;
|
|
|
|
float _21, _22, _23, _24;
|
|
|
|
float _31, _32, _33, _34;
|
|
|
|
float _41, _42, _43, _44;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* GFX_3DMATRIX_H */
|