MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
matrix4.h
Go to the documentation of this file.
1 /*
2  This file is part of the MinGfx Project.
3 
4  Copyright (c) 2017,2018 Regents of the University of Minnesota.
5  All Rights Reserved.
6 
7  Original Author(s) of this File:
8  Dan Keefe, 2017, University of Minnesota
9 
10  Author(s) of Significant Updates/Modifications to the File:
11  ...
12  */
13 
14 #ifndef SRC_MATRIX4_H_
15 #define SRC_MATRIX4_H_
16 
17 #include <iostream>
18 
19 #include "point3.h"
20 #include "vector3.h"
21 #include "ray.h"
22 
23 
24 namespace mingfx {
25 
26 
50 class Matrix4 {
51 public:
52 
55 
58  Matrix4(const float* a);
59 
62  Matrix4(const std::vector<float> &a);
63 
65  Matrix4(const Matrix4& m2);
66 
68  virtual ~Matrix4();
69 
71  bool operator==(const Matrix4& m2) const;
72 
74  bool operator!=(const Matrix4& m2) const;
75 
77  Matrix4& operator=(const Matrix4& m2);
78 
79 
82  const float * value_ptr() const;
83 
86  float operator[](const int i) const;
87 
90  float& operator[](const int i);
91 
94  float operator()(const int row, const int col) const;
95 
98  float& operator()(const int row, const int col);
99 
100 
105  Vector3 ColumnToVector3(int c) const;
106 
109  Point3 ColumnToPoint3(int c) const;
110 
111  std::vector<float> ToVector() const;
112 
113 
114  // --- Static Constructors for Special Matrices ---
115 
123  const float r1c1, const float r1c2, const float r1c3, const float r1c4,
124  const float r2c1, const float r2c2, const float r2c3, const float r2c4,
125  const float r3c1, const float r3c2, const float r3c3, const float r3c4,
126  const float r4c1, const float r4c2, const float r4c3, const float r4c4
127  );
128 
129  // --- Model Transformations ---
130 
132  static Matrix4 Scale(const Vector3 &v);
133 
135  static Matrix4 Translation(const Vector3 &v);
136 
138  static Matrix4 RotationX(const float radians);
139 
141  static Matrix4 RotationY(const float radians);
142 
144  static Matrix4 RotationZ(const float radians);
145 
147  static Matrix4 Rotation(const Point3 &p, const Vector3 &v, const float a);
148 
173  static Matrix4 Align(const Point3 &a_p, const Vector3 &a_v1, const Vector3 &a_v2,
174  const Point3 &b_p, const Vector3 &b_v1, const Vector3 &b_v2);
175 
176 
177  // --- View Matrices ---
178 
183  static Matrix4 LookAt(Point3 eye, Point3 target, Vector3 up);
184 
185  // --- Projection Matrices ---
186 
189  static Matrix4 Perspective(float fov_y_in_degrees, float aspect_ratio, float near_plane_dist, float far_plane_dist);
190 
192  static Matrix4 Frustum(float left, float right, float bottom, float top, float near_plane_dist, float far_plane_dist);
193 
194  // --- Inverse, Transposeand Other General Matrix Functions ---
195 
198  Matrix4 Inverse() const;
199 
205 
208 
211  float SubDeterminant(int exclude_row, int exclude_col) const;
212 
214  Matrix4 Cofactor() const;
215 
217  float Determinant() const;
218 
219 
220 
221 private:
222  float m[16];
223 };
224 
225 
226 
227 // ---------- Operator Overloads for Working with Points, Vectors, & Matrices ----------
228 
229 
230 // --- Matrix multiplication for Points, Vectors, & Matrices ---
231 
233 Matrix4 operator*(const Matrix4& m, const float& s);
234 
236 Matrix4 operator*(const float& s, const Matrix4& m);
237 
239 Point3 operator*(const Matrix4& m, const Point3& p);
240 
242 Vector3 operator*(const Matrix4& m, const Vector3& v);
243 
245 Matrix4 operator*(const Matrix4& m1, const Matrix4& m2);
246 
247 
248 
250 Ray operator*(const Matrix4& m, const Ray& r);
251 
252 // --- Stream operators ---
253 
254 std::ostream & operator<< ( std::ostream &os, const Matrix4 &m);
255 std::istream & operator>> ( std::istream &is, Matrix4 &m);
256 
257 
258 } // end namespace
259 
260 #endif
A 4x4 transformation matrix stored internally as an array of floats in column-major order so as to be...
Definition: matrix4.h:50
float Determinant() const
Returns the determinant of the 4x4 matrix.
static Matrix4 Scale(const Vector3 &v)
Returns the scale matrix described by the vector.
Matrix4(const float *a)
Constructs a matrix given from an array of 16 floats in OpenGL matrix format (i.e....
Matrix4(const std::vector< float > &a)
Constructs a matrix given from a vector of 16 floats in OpenGL matrix format (i.e....
Matrix4 Cofactor() const
Returns the cofactor matrix.
static Matrix4 Perspective(float fov_y_in_degrees, float aspect_ratio, float near_plane_dist, float far_plane_dist)
Returns a perspective projection matrix equivalent to the one gluPerspective creates.
const float * value_ptr() const
Returns a pointer to the raw data array used to store the matrix. This is a 1D array of 16-elements s...
float SubDeterminant(int exclude_row, int exclude_col) const
Returns the determinant of the 3x3 matrix formed by excluding the specified row and column from the 4...
static Matrix4 LookAt(Point3 eye, Point3 target, Vector3 up)
Returns a view matrix that centers the camera at the 'eye' position and orients it to look at the des...
float & operator()(const int row, const int col)
Access an individual element of the array using the syntax: Matrix4 mat; mat(1,2) = 1....
static Matrix4 RotationX(const float radians)
Returns the rotation matrix about the x axis by the specified angle.
Matrix4(const Matrix4 &m2)
Copy constructor.
static Matrix4 RotationZ(const float radians)
Returns the rotation matrix about the z axis by the specified angle.
std::vector< float > ToVector() const
Matrix4()
The default constructor creates an identity matrix:
Vector3 ColumnToVector3(int c) const
Returns the c-th column of the matrix as a Vector type, e.g.,: Vector3 xAxis = mat....
virtual ~Matrix4()
Destructor.
static Matrix4 Frustum(float left, float right, float bottom, float top, float near_plane_dist, float far_plane_dist)
Returns a projection matrix equivalent the one glFrustum creates.
Matrix4 Orthonormal() const
Returns an orthonormal version of the matrix, i.e., guarantees that the rotational component of the m...
Matrix4 & operator=(const Matrix4 &m2)
Matrix assignment operator.
bool operator==(const Matrix4 &m2) const
Check for "equality", taking floating point imprecision into account.
float operator()(const int row, const int col) const
Access an individual element of the array using the syntax: Matrix4 mat; float row1col2 = mat(1,...
bool operator!=(const Matrix4 &m2) const
Check for "inequality", taking floating point imprecision into account.
Matrix4 Inverse() const
Returns the inverse of the 4x4 matrix if it is nonsingular. If it is singular, then returns the ident...
float operator[](const int i) const
Accesses the ith element of the raw data array used to store the matrix. This is a 1D array of 16-ele...
static Matrix4 Rotation(const Point3 &p, const Vector3 &v, const float a)
Returns the rotation matrix around the vector v placed at point p, rotate by angle a.
static Matrix4 FromRowMajorElements(const float r1c1, const float r1c2, const float r1c3, const float r1c4, const float r2c1, const float r2c2, const float r2c3, const float r2c4, const float r3c1, const float r3c2, const float r3c3, const float r3c4, const float r4c1, const float r4c2, const float r4c3, const float r4c4)
Returns a matrix constructed from individual elements passed in row major order so that the matrix lo...
static Matrix4 Align(const Point3 &a_p, const Vector3 &a_v1, const Vector3 &a_v2, const Point3 &b_p, const Vector3 &b_v1, const Vector3 &b_v2)
Creates a transformation matrix that maps a coordinate space, a, defined one point,...
static Matrix4 Translation(const Vector3 &v)
Returns the translation matrix described by the vector.
Point3 ColumnToPoint3(int c) const
Returns the c-th column of the matrix as a Vector type, e.g.,: Point3 pos = mat.getColumnAsPoint3(3);...
static Matrix4 RotationY(const float radians)
Returns the rotation matrix about the y axis by the specified angle.
float & operator[](const int i)
Accesses the ith element of the raw data array used to store the matrix. This is a 1D array of 16-ele...
Matrix4 Transpose() const
Returns the transpose of the matrix.
A 3D Point with floating point coordinates, used for storing vertices and all sorts of other 3D graph...
Definition: point3.h:52
Stores the mathematical object of a ray that begins at an origin (a 3D point) and points in a directi...
Definition: ray.h:54
A 3D Vector with floating point coordinates, used for storing normals and all sorts of other 3D graph...
Definition: vector3.h:62
Namespace for the MinGfx Toolkit.
Definition: aabb.h:21
std::ostream & operator<<(std::ostream &os, const Color &c)
std::istream & operator>>(std::istream &is, Color &c)
Matrix4 operator*(const Matrix4 &m, const float &s)
Multiply matrix and scalar, returns the new matrix.