/usr/include/Wt/WMatrix4x4 is in libwt-dev 3.3.0-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | // This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2010 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef WMATRIX4X4_H_
#define WMATRIX4X4_H_
#include <Wt/WGenericMatrix>
namespace Wt {
/*! \class WMatrix4x4 Wt/WMatrix4x4 Wt/WMatrix4x4
* \brief A value class that describes a 3D affine transformation matrix.
*
* The matrix is a 4x4 matrix encoded using 16 parameters. The matrix
* stores its data internally in row order.
*
* Normally, a transformation matrix (composed translation/rotation/scale,
* but without perspective) is of this form:
* \code
* m00 m01 m02 dx
* m10 m11 m12 dy
* m20 m21 m22 dz
* 0 0 0 1
* \endcode
*
* In this representation, dx, dy and dz (= m(0, 3), m(1, 3) and m(2, 3))
* represent the translation components, and m(<i>x, y</i>) represent a
* 3D matrix that contains the scale, rotation (and skew) components. The
* matrix is also capable of representing perspective projections. In that
* case, the matrix will not match the form depicted above.
*
* In order to calculate the transformed vector w of a 3D vector v by the
* transformation contained in matrix T, v will be left-multiplied by T:
* \code
* w = T * v;
* \endcode
* In the formula above, v and w are homogenous 3D column vectors
* (x, y, z, w), equal to (x/w, y/w, z/w, 1). In normal use cases w is 1,
* except for vectors that were transformed by a perspective projection
* matrix.
*
* The transformation is used to represent a tansformed coordinate
* system, and provides methods to rotate(), scale() or
* translate() this coordinate system.
*
* This matrix class is matched to OpenGL's coordinate system and
* matrix notation. The rotate, translate, scale, lookAt, perspective,
* frustum and ortho methods of this class behave exactly like
* their OpenGL equivalents. The only difference is that the storage of this
* matrix is row-major, while OpenGL uses column-major. This should
* only be a concern if you need to access the raw data of the matrix,
* in which case you should use transposed().data() instead.
* When WWebGL uses this class, it sends the data in the correct order
* to the client.
*
*/
class WT_API WMatrix4x4: public WGenericMatrix<double, 4, 4>
{
public:
/*! \brief Default constructor.
*
* Creates the identity transformation matrix.
*/
WMatrix4x4() {}
/*! \brief Copy constructor.
*/
WMatrix4x4(const WMatrix4x4 &other): WGenericMatrix<double, 4, 4>(other) {}
/*! \brief Construct for a WGenericMatrix
*
* Creates the identity transformation matrix. As we inherit from
* WGenericMatrix, most overloaded operators create a WGenericMatrix.
* This implicit constructor ensures that you will not notice this.
*/
WMatrix4x4(const WGenericMatrix<double, 4, 4> &other): WGenericMatrix<double, 4, 4>(other) {}
/*! \brief Constructs a matrix from an array of elements.
*
* The input array is assumed to be in row-major order. If elements is 0,
* the matrix is not initialized.
*/
// Assumes d is ROW order
explicit WMatrix4x4(double *d): WGenericMatrix<double, 4, 4>(d) {}
/*! \brief Construct a custom matrix by specifying the parameters.
*
* Creates a matrix from the specified parameters.
*/
WMatrix4x4(double m11, double m12, double m13, double m14,
double m21, double m22, double m23, double m24,
double m31, double m32, double m33, double m34,
double m41, double m42, double m43, double m44): WGenericMatrix<double, 4, 4>(0)
{
(*this)(0, 0) = m11;
(*this)(0, 1) = m12;
(*this)(0, 2) = m13;
(*this)(0, 3) = m14;
(*this)(1, 0) = m21;
(*this)(1, 1) = m22;
(*this)(1, 2) = m23;
(*this)(1, 3) = m24;
(*this)(2, 0) = m31;
(*this)(2, 1) = m32;
(*this)(2, 2) = m33;
(*this)(2, 3) = m34;
(*this)(3, 0) = m41;
(*this)(3, 1) = m42;
(*this)(3, 2) = m43;
(*this)(3, 3) = m44;
}
/*! \brief Returns the determinant.
*/
double determinant() const;
/*! \brief Switch between left-hand and right-hand side coordinate systems
*
* Equivalent to scale(1, -1, -1)
*/
void flipCoordinates()
{
scale(1, -1, -1);
}
/*! \brief Construct a perspective projection matrix
*
* This function constructs a perspective projection where the
* camera is located in the origin. The visible volume is determined
* by whatever that is visible when looking from the origin through the
* rectangular 'window' defined by the coordinates (l, b, n) and
* (r, t, n) (parallel to the XY plane). The zone is further delimited
* by the near and the far clipping planes.
*
* The perspective matrix (P) is right-multiplied with the current
* transformation matrix (M): M * P. Usually, you will want M to be
* the identity matrix when using this method.
*/
void frustum(double left, double right, double bottom, double top,
double nearPlane, double farPlane)
{
using namespace boost::numeric::ublas;
WMatrix4x4 f(0);
f(0, 0) = 2 * nearPlane / (right - left);
f(0, 1) = 0;
f(0, 2) = (right + left) / (right - left);
f(0, 3) = 0;
f(1, 0) = 0;
f(1, 1) = 2 * nearPlane / (top - bottom);
f(1, 2) = (top + bottom) / (top - bottom);
f(1, 3) = 0;
f(2, 0) = 0;
f(2, 1) = 0;
f(2, 2) = - (farPlane + nearPlane) / (farPlane - nearPlane);
f(2, 3) = - 2 * farPlane * nearPlane / (farPlane - nearPlane);
f(3, 0) = 0;
f(3, 1) = 0;
f(3, 2) = -1;
f(3, 3) = 0;
impl() = prod(impl(), f.impl());
}
/*! \brief Returns the inversion of this matrix, if invertible
*
* If invertible is not 0, it will contain a bool that indicates if
* the operation succeeded and the inverse matrix is returned. Else,
* this method returns the unit matrix.
*/
WMatrix4x4 inverted(bool *invertible = 0) const;
/*! \brief Apply a transformation to position a camera
*
* (eyeX, eyeY, eyeZ) is the position of the camera.
*
* The camera looks at (centerX, centerY, centerZ).
*
* (upX, upY, upZ) is a vector that is the direction of the up vector.
*
* This method applies a rotation and translation transformation to
* the current matrix so that the given eye becomes (0, 0, 0), the
* center point is on the negative Z axis, and the up vector lies in the
* X=0 plane, with its Y component in the positive Y axis direction.
*
* The up vector must not be parallel to the line between eye and center.
* The vectors will be normalized and are not required to be perpendicular.
*
* If the lookat transformation matrix is M, and the current value of
* the Matrix4x4 matrix is T, the resulting matrix after lookAt returns
* will be M * T.
*
* This matrix is often used in conjunction with the
* perspective() method:
* \code
* // First, apply the lookAt transformation
* projectionMatrix.lookAt(1, 1, 1, 0, 0, 0, 0, 1, 0);
* // Then apply some perspective
* projectionMatrix.perspective(90, aspect, 0.1, 10);
* \endcode
*
*/
void lookAt(double eyeX, double eyeY, double eyeZ,
double centerX, double centerY, double centerZ,
double upX, double upY, double upZ)
{
using namespace boost::numeric::ublas;
// A 3D vector class would be handy here
// Compute and normalize lookDir
double lookDirX = centerX - eyeX;
double lookDirY = centerY - eyeY;
double lookDirZ = centerZ - eyeZ;
double lookDirNorm = std::sqrt(lookDirX*lookDirX + lookDirY*lookDirY + lookDirZ*lookDirZ);
lookDirX /= lookDirNorm;
lookDirY /= lookDirNorm;
lookDirZ /= lookDirNorm;
// Compute and normalize the 'side' vector: cross product of lookDir and upDir
double sideX = lookDirY*upZ - upY*lookDirZ;
double sideY = -(lookDirX*upZ - upX*lookDirZ);
double sideZ = lookDirX*upY - upX*lookDirY;
double sideNormal = std::sqrt(sideX*sideX + sideY*sideY + sideZ*sideZ);
sideX /= sideNormal;
sideY /= sideNormal;
sideZ /= sideNormal;
// Compute the normalized 'up' vector: cross-prod of normalized look
// and side dirs:
double upDirX = sideY*lookDirZ - lookDirY*sideZ;
double upDirY = -(sideX*lookDirZ - lookDirX*sideZ);
double upDirZ = sideX*lookDirY - lookDirX*sideY;
WMatrix4x4 l(
sideX, sideY, sideZ, -(eyeX*sideX + eyeY*sideY + eyeZ*sideZ),
upDirX, upDirY, upDirZ, -(eyeX*upDirX + eyeY*upDirY + eyeZ*upDirZ),
-lookDirX, -lookDirY, -lookDirZ, +(+eyeX*lookDirX + eyeY*lookDirY + eyeZ*lookDirZ),
0, 0, 0, 1
);
impl() = prod(impl(), l.impl());;
}
/* \brief Create an orhtographic projection matrix for use in OpenGL
*
* Create an orthographic projection matrix. The given left, right,
* bottom, top, near and far points will be linearly mapped to the OpenGL
* unit cube ((1,1,1) to (-1,-1,-1)).
*
* The orthographic matrix (O) is right-multiplied with the current
* transformation matrix (M): M * O. Usually, you will want M to be
* the identity matrix when using this method.
*/
void orhto(double left, double right, double bottom, double top,
double nearPlane, double farPlane)
{
using namespace boost::numeric::ublas;
WMatrix4x4 o(
2 / (right - left), 0, 0, - (right + left) / (right - left),
0, 2 / (top - bottom), 0, - (top + bottom) / (top - bottom),
0, 0, -2 / (farPlane - nearPlane), - (farPlane + nearPlane) / (farPlane - nearPlane),
0, 0, 0, 1
);
impl() = prod(impl(), o.impl());;
}
/*! \brief Construct a perspective projection matrix for use in OpenGL
*
* The camera is located in the origin and look in the direction of the
* negative Z axis.
*
* Angle is the vertical view angle, in degrees. Aspect is the aspect ratio
* of the viewport, and near and far are the distances of the front and
* rear clipping plane from the camera.
*
* The perspective matrix (P) is right-multiplied with the current
* transformation matrix (M): M * P. Usually, you will want M to be
* the identity matrix when using this method.
*/
void perspective(double angle, double aspect,
double nearPlane, double farPlane)
{
double halfHeight =
nearPlane * std::tan(angle / 2 / 180 * 3.14159265358979323846);
double halfWidth = halfHeight * aspect;
frustum(-halfWidth, halfWidth, -halfHeight, halfHeight,
nearPlane, farPlane);
}
/*! \brief Rotates the transformation around a random axis.
*
* Applies a rotation to the current transformation
* matrix, over \p angle degrees. The current matrix (M) is
* right-multiplied by the rotation matrix: M = M * R
*
*/
void rotate(double angle, double x, double y, double z)
{
using namespace boost::numeric::ublas;
double t = angle / 180.0 * 3.14159265358979323846;
double norm2 = std::sqrt(x*x + y*y + z*z);
x /= norm2;
y /= norm2;
z /= norm2;
double cost = std::cos(t);
double sint = std::sin(t);
WMatrix4x4 rot(0);
rot(0,0) = cost + x*x*(1-cost);
rot(0,1) = x*y*(1-cost) - z*sint;
rot(0,2) = x*z*(1-cost) + y*sint;
rot(0,3) = 0;
rot(1,0) = y*x*(1-cost) + z*sint;
rot(1,1) = cost + y*y*(1 - cost);
rot(1,2) = y*z*(1-cost) - x*sint;
rot(1,3) = 0;
rot(2,0) = z*x*(1-cost) - y*sint;
rot(2,1) = z*y*(1-cost) + x*sint;
rot(2,2) = cost + z*z*(1-cost);
rot(2,3) = 0;
rot(3,0) = 0;
rot(3,1) = 0;
rot(3,2) = 0;
rot(3,3) = 1;
impl() = prod(impl(), rot.impl());;
}
/*! \brief Scales the transformation.
*
* Equivalent to scale(xFactor, yFactor, 1);
*
* \sa scale(double, double, double)
*/
void scale(double xFactor, double yFactor) { scale (xFactor, yFactor, 1); }
/*! \brief Scales the transformation.
*
* Equivalent to M * S where M is the current transformation and S is
* \code
* x 0 0 0
* 0 y 0 0
* 0 0 z 0
* 0 0 0 1
* \endcode
*/
void scale(double x, double y, double z)
{
for (unsigned i = 0; i < 4; ++i) {
(*this)(i, 0) *= x;
(*this)(i, 1) *= y;
(*this)(i, 2) *= z;
}
}
/*! \brief Scales the transformation.
*
* Equivalent to scale(factor, factor, factor);
*
* \sa scale(double, double, double)
*/
void scale(double factor) { scale(factor, factor, factor); }
/*! \brief Translates the transformation.
*
* Equivalent to translate(x, y, 0)
*/
void translate(double x, double y)
{
translate(x, y, 0);
}
/*! \brief Translates the transformation.
*
* Translates the current transformation.
*
* Equivalent to M * T where M is the current transformation matrix
* and T is:
* \code
* 1 0 0 x
* 0 1 0 y
* 0 0 1 z
* 0 0 0 1
* \endcode
*/
void translate(double x, double y, double z)
{
using namespace boost::numeric::ublas;
WMatrix4x4 T;
T(0, 3) = x;
T(1, 3) = y;
T(2, 3) = z;
impl() = prod(impl(), T.impl());;
}
};
}
#endif // WMATRIX4X4_H
|