From patchwork Thu Sep 22 20:39:23 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesse Barker X-Patchwork-Id: 4280 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 0DCCE23EFD for ; Thu, 22 Sep 2011 20:39:25 +0000 (UTC) Received: from mail-fx0-f52.google.com (mail-fx0-f52.google.com [209.85.161.52]) by fiordland.canonical.com (Postfix) with ESMTP id E356BA180B3 for ; Thu, 22 Sep 2011 20:39:24 +0000 (UTC) Received: by fxe23 with SMTP id 23so4410652fxe.11 for ; Thu, 22 Sep 2011 13:39:24 -0700 (PDT) Received: by 10.223.55.136 with SMTP id u8mr777834fag.46.1316723964712; Thu, 22 Sep 2011 13:39:24 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.152.18.198 with SMTP id y6cs183405lad; Thu, 22 Sep 2011 13:39:24 -0700 (PDT) Received: by 10.227.202.135 with SMTP id fe7mr1129191wbb.14.1316723964023; Thu, 22 Sep 2011 13:39:24 -0700 (PDT) Received: from indium.canonical.com (indium.canonical.com. [91.189.90.7]) by mx.google.com with ESMTPS id ft20si8204705wbb.2.2011.09.22.13.39.23 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 22 Sep 2011 13:39:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) client-ip=91.189.90.7; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) smtp.mail=bounces@canonical.com Received: from ackee.canonical.com ([91.189.89.26]) by indium.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1R6q3H-0004KW-Ch for ; Thu, 22 Sep 2011 20:39:23 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id 50CD8E1574 for ; Thu, 22 Sep 2011 20:39:23 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: libmatrix X-Launchpad-Branch: ~jesse-barker/libmatrix/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 28 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~jesse-barker/libmatrix/trunk] Rev 28: Add documenting comments to the matrix classes. Update the vector ones to Message-Id: <20110922203923.10385.81757.launchpad@ackee.canonical.com> Date: Thu, 22 Sep 2011 20:39:23 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="13996"; Instance="initZopeless config overlay" X-Launchpad-Hash: ad253da1bcbaf49011be80cc4f129883eb6fbb59 ------------------------------------------------------------ revno: 28 committer: Jesse Barker branch nick: trunk timestamp: Thu 2011-09-22 10:46:39 -0700 message: Add documenting comments to the matrix classes. Update the vector ones to reflect the matrix updates. modified: mat.h vec.h --- lp:libmatrix https://code.launchpad.net/~jesse-barker/libmatrix/trunk You are subscribed to branch lp:libmatrix. To unsubscribe from this branch go to https://code.launchpad.net/~jesse-barker/libmatrix/trunk/+edit-subscription === modified file 'mat.h' --- mat.h 2011-06-23 15:21:20 +0000 +++ mat.h 2011-09-22 17:46:39 +0000 @@ -46,6 +46,11 @@ // However, the internal data representation is column-major, so when using // the raw data access member to treat the data as a singly-dimensioned array, // it does not have to be transposed. +// +// A template class for creating, managing and operating on a 2x2 matrix +// of any type you like (intended for built-in types, but as long as it +// supports the basic arithmetic and assignment operators, any type should +// work). template class tmat2 { @@ -70,6 +75,7 @@ } ~tmat2() {} + // Reset this to the identity matrix. void setIdentity() { m_[0] = 1; @@ -78,6 +84,7 @@ m_[3] = 1; } + // Transpose this. Return a reference to this. tmat2& transpose() { T tmp_val = m_[1]; @@ -86,11 +93,16 @@ return *this; } + // Compute the determinant of this and return it. T determinant() { return (m_[0] * m_[3]) - (m_[2] * m_[1]); } + // Invert this. Return a reference to this. + // + // NOTE: If this is non-invertible, we will + // throw to avoid undefined behavior. tmat2& inverse() throw(std::runtime_error) { T d(determinant()); @@ -109,6 +121,8 @@ return *this; } + // Print the elements of the matrix to standard out. + // Really only useful for debug and test. void print() const { static const int precision(6); @@ -126,8 +140,12 @@ std::cout << " |" << std::endl; } + // Allow raw data access for API calls and the like. + // For example, it is valid to pass a tmat2 into a call to + // the OpenGL command "glUniformMatrix2fv()". operator const T*() const { return &m_[0];} + // Test if 'rhs' is equal to this. bool operator==(const tmat2& rhs) const { return m_[0] == rhs.m_[0] && @@ -136,11 +154,13 @@ m_[3] == rhs.m_[3]; } + // Test if 'rhs' is not equal to this. bool operator!=(const tmat2& rhs) const { return !(*this == rhs); } + // A direct assignment of 'rhs' to this. Return a reference to this. tmat2& operator=(const tmat2& rhs) { if (this != &rhs) @@ -153,6 +173,7 @@ return *this; } + // Add another matrix to this. Return a reference to this. tmat2& operator+=(const tmat2& rhs) { m_[0] += rhs.m_[0]; @@ -162,11 +183,13 @@ return *this; } + // Add another matrix to a copy of this. Return the copy. const tmat2 operator+(const tmat2& rhs) { return tmat2(*this) += rhs; } + // Subtract another matrix from this. Return a reference to this. tmat2& operator-=(const tmat2& rhs) { m_[0] -= rhs.m_[0]; @@ -176,11 +199,13 @@ return *this; } + // Subtract another matrix from a copy of this. Return the copy. const tmat2 operator-(const tmat2& rhs) { return tmat2(*this) += rhs; } + // Multiply this by another matrix. Return a reference to this. tmat2& operator*=(const tmat2& rhs) { T c0r0((m_[0] * rhs.m_[0]) + (m_[2] * rhs.m_[1])); @@ -194,11 +219,13 @@ return *this; } + // Multiply a copy of this by another matrix. Return the copy. const tmat2 operator*(const tmat2& rhs) { return tmat2(*this) *= rhs; } + // Multiply this by a scalar. Return a reference to this. tmat2& operator*=(const T& rhs) { m_[0] *= rhs; @@ -208,11 +235,13 @@ return *this; } + // Multiply a copy of this by a scalar. Return the copy. const tmat2 operator*(const T& rhs) { return tmat2(*this) *= rhs; } + // Divide this by a scalar. Return a reference to this. tmat2& operator/=(const T& rhs) { m_[0] /= rhs; @@ -222,11 +251,15 @@ return *this; } + // Divide a copy of this by a scalar. Return the copy. const tmat2 operator/(const T& rhs) { return tmat2(*this) /= rhs; } + // Use an instance of the ArrayProxy class to support double-indexed + // references to a matrix (i.e., m[1][1]). See comments above the + // ArrayProxy definition for more details. ArrayProxy operator[](int index) { return ArrayProxy(&m_[index]); @@ -240,12 +273,16 @@ T m_[4]; }; +// Multiply a scalar and a matrix just like the member operator, but allow +// the scalar to be the left-hand operand. template const tmat2 operator*(const T& lhs, const tmat2& rhs) { return tmat2(rhs) * lhs; } +// Multiply a copy of a vector and a matrix (matrix is right-hand operand). +// Return the copy. template const tvec2 operator*(const tvec2& lhs, const tmat2& rhs) { @@ -254,6 +291,8 @@ return tvec2(x,y); } +// Multiply a copy of a vector and a matrix (matrix is left-hand operand). +// Return the copy. template const tvec2 operator*(const tmat2& lhs, const tvec2& rhs) { @@ -262,6 +301,7 @@ return tvec2(x, y); } +// Compute the outer product of two vectors. Return the resultant matrix. template const tmat2 outer(const tvec2& a, const tvec2& b) { @@ -273,6 +313,10 @@ return product; } +// A template class for creating, managing and operating on a 3x3 matrix +// of any type you like (intended for built-in types, but as long as it +// supports the basic arithmetic and assignment operators, any type should +// work). template class tmat3 { @@ -309,6 +353,7 @@ } ~tmat3() {} + // Reset this to the identity matrix. void setIdentity() { m_[0] = 1; @@ -322,6 +367,7 @@ m_[8] = 1; } + // Transpose this. Return a reference to this. tmat3& transpose() { T tmp_val = m_[1]; @@ -336,6 +382,7 @@ return *this; } + // Compute the determinant of this and return it. T determinant() { tmat2 minor0(m_[4], m_[5], m_[7], m_[8]); @@ -346,6 +393,10 @@ (m_[6] * minor6.determinant()); } + // Invert this. Return a reference to this. + // + // NOTE: If this is non-invertible, we will + // throw to avoid undefined behavior. tmat3& inverse() throw(std::runtime_error) { T d(determinant()); @@ -374,6 +425,8 @@ return *this; } + // Print the elements of the matrix to standard out. + // Really only useful for debug and test. void print() const { static const int precision(6); @@ -403,8 +456,12 @@ std::cout << " |" << std::endl; } + // Allow raw data access for API calls and the like. + // For example, it is valid to pass a tmat3 into a call to + // the OpenGL command "glUniformMatrix3fv()". operator const T*() const { return &m_[0];} + // Test if 'rhs' is equal to this. bool operator==(const tmat3& rhs) const { return m_[0] == rhs.m_[0] && @@ -418,11 +475,13 @@ m_[8] == rhs.m_[8]; } + // Test if 'rhs' is not equal to this. bool operator!=(const tmat3& rhs) const { return !(*this == rhs); } + // A direct assignment of 'rhs' to this. Return a reference to this. tmat3& operator=(const tmat3& rhs) { if (this != &rhs) @@ -440,6 +499,7 @@ return *this; } + // Add another matrix to this. Return a reference to this. tmat3& operator+=(const tmat3& rhs) { m_[0] += rhs.m_[0]; @@ -454,11 +514,13 @@ return *this; } + // Add another matrix to a copy of this. Return the copy. const tmat3 operator+(const tmat3& rhs) { return tmat3(*this) += rhs; } + // Subtract another matrix from this. Return a reference to this. tmat3& operator-=(const tmat3& rhs) { m_[0] -= rhs.m_[0]; @@ -473,11 +535,13 @@ return *this; } + // Subtract another matrix from a copy of this. Return the copy. const tmat3 operator-(const tmat3& rhs) { return tmat3(*this) -= rhs; } + // Multiply this by another matrix. Return a reference to this. tmat3& operator*=(const tmat3& rhs) { T c0r0((m_[0] * rhs.m_[0]) + (m_[3] * rhs.m_[1]) + (m_[6] * rhs.m_[2])); @@ -501,11 +565,13 @@ return *this; } + // Multiply a copy of this by another matrix. Return the copy. const tmat3 operator*(const tmat3& rhs) { return tmat3(*this) *= rhs; } + // Multiply this by a scalar. Return a reference to this. tmat3& operator*=(const T& rhs) { m_[0] *= rhs; @@ -520,11 +586,13 @@ return *this; } + // Multiply a copy of this by a scalar. Return the copy. const tmat3 operator*(const T& rhs) { return tmat3(*this) *= rhs; } + // Divide this by a scalar. Return a reference to this. tmat3& operator/=(const T& rhs) { m_[0] /= rhs; @@ -539,11 +607,15 @@ return *this; } + // Divide a copy of this by a scalar. Return the copy. const tmat3 operator/(const T& rhs) { return tmat3(*this) /= rhs; } + // Use an instance of the ArrayProxy class to support double-indexed + // references to a matrix (i.e., m[1][1]). See comments above the + // ArrayProxy definition for more details. ArrayProxy operator[](int index) { return ArrayProxy(&m_[index]); @@ -557,12 +629,16 @@ T m_[9]; }; +// Multiply a scalar and a matrix just like the member operator, but allow +// the scalar to be the left-hand operand. template const tmat3 operator*(const T& lhs, const tmat3& rhs) { return tmat3(rhs) * lhs; } +// Multiply a copy of a vector and a matrix (matrix is right-hand operand). +// Return the copy. template const tvec3 operator*(const tvec3& lhs, const tmat3& rhs) { @@ -572,6 +648,8 @@ return tvec3(x, y, z); } +// Multiply a copy of a vector and a matrix (matrix is left-hand operand). +// Return the copy. template const tvec3 operator*(const tmat3& lhs, const tvec3& rhs) { @@ -581,6 +659,7 @@ return tvec3(x, y, z); } +// Compute the outer product of two vectors. Return the resultant matrix. template const tmat3 outer(const tvec3& a, const tvec3& b) { @@ -597,6 +676,10 @@ return product; } +// A template class for creating, managing and operating on a 4x4 matrix +// of any type you like (intended for built-in types, but as long as it +// supports the basic arithmetic and assignment operators, any type should +// work). template class tmat4 { @@ -626,6 +709,7 @@ } ~tmat4() {} + // Reset this to the identity matrix. void setIdentity() { m_[0] = 1; @@ -646,6 +730,7 @@ m_[15] = 1; } + // Transpose this. Return a reference to this. tmat4& transpose() { T tmp_val = m_[1]; @@ -669,6 +754,7 @@ return *this; } + // Compute the determinant of this and return it. T determinant() { tmat3 minor0(m_[5], m_[6], m_[7], m_[9], m_[10], m_[11], m_[13], m_[14], m_[15]); @@ -681,6 +767,10 @@ (m_[12] * minor12.determinant()); } + // Invert this. Return a reference to this. + // + // NOTE: If this is non-invertible, we will + // throw to avoid undefined behavior. tmat4& inverse() throw(std::runtime_error) { T d(determinant()); @@ -726,6 +816,8 @@ return *this; } + // Print the elements of the matrix to standard out. + // Really only useful for debug and test. void print() const { static const int precision(6); @@ -771,8 +863,12 @@ std::cout << " |" << std::endl; } + // Allow raw data access for API calls and the like. + // For example, it is valid to pass a tmat4 into a call to + // the OpenGL command "glUniformMatrix4fv()". operator const T*() const { return &m_[0];} + // Test if 'rhs' is equal to this. bool operator==(const tmat4& rhs) const { return m_[0] == rhs.m_[0] && @@ -793,11 +889,13 @@ m_[15] == rhs.m_[15]; } + // Test if 'rhs' is not equal to this. bool operator!=(const tmat4& rhs) const { return !(*this == rhs); } + // A direct assignment of 'rhs' to this. Return a reference to this. tmat4& operator=(const tmat4& rhs) { if (this != &rhs) @@ -822,6 +920,7 @@ return *this; } + // Add another matrix to this. Return a reference to this. tmat4& operator+=(const tmat4& rhs) { m_[0] += rhs.m_[0]; @@ -843,11 +942,13 @@ return *this; } + // Add another matrix to a copy of this. Return the copy. const tmat4 operator+(const tmat4& rhs) { return tmat4(*this) += rhs; } + // Subtract another matrix from this. Return a reference to this. tmat4& operator-=(const tmat4& rhs) { m_[0] -= rhs.m_[0]; @@ -869,11 +970,13 @@ return *this; } + // Subtract another matrix from a copy of this. Return the copy. const tmat4 operator-(const tmat4& rhs) { return tmat4(*this) -= rhs; } + // Multiply this by another matrix. Return a reference to this. tmat4& operator*=(const tmat4& rhs) { T c0r0((m_[0] * rhs.m_[0]) + (m_[4] * rhs.m_[1]) + (m_[8] * rhs.m_[2]) + (m_[12] * rhs.m_[3])); @@ -911,11 +1014,13 @@ return *this; } + // Multiply a copy of this by another matrix. Return the copy. const tmat4 operator*(const tmat4& rhs) { return tmat4(*this) *= rhs; } + // Multiply this by a scalar. Return a reference to this. tmat4& operator*=(const T& rhs) { m_[0] *= rhs; @@ -937,11 +1042,13 @@ return *this; } + // Multiply a copy of this by a scalar. Return the copy. const tmat4 operator*(const T& rhs) { return tmat4(*this) *= rhs; } + // Divide this by a scalar. Return a reference to this. tmat4& operator/=(const T& rhs) { m_[0] /= rhs; @@ -963,11 +1070,15 @@ return *this; } + // Divide a copy of this by a scalar. Return the copy. const tmat4 operator/(const T& rhs) { return tmat4(*this) /= rhs; } + // Use an instance of the ArrayProxy class to support double-indexed + // references to a matrix (i.e., m[1][1]). See comments above the + // ArrayProxy definition for more details. ArrayProxy operator[](int index) { return ArrayProxy(&m_[index]); @@ -981,12 +1092,16 @@ T m_[16]; }; +// Multiply a scalar and a matrix just like the member operator, but allow +// the scalar to be the left-hand operand. template const tmat4 operator*(const T& lhs, const tmat4& rhs) { return tmat4(rhs) * lhs; } +// Multiply a copy of a vector and a matrix (matrix is right-hand operand). +// Return the copy. template const tvec4 operator*(const tvec4& lhs, const tmat4& rhs) { @@ -997,6 +1112,8 @@ return tvec4(x, y, z, w); } +// Multiply a copy of a vector and a matrix (matrix is left-hand operand). +// Return the copy. template const tvec4 operator*(const tmat4& lhs, const tvec4& rhs) { @@ -1007,6 +1124,7 @@ return tvec4(x, y, z, w); } +// Compute the outer product of two vectors. Return the resultant matrix. template const tmat4 outer(const tvec4& a, const tvec4& b) { === modified file 'vec.h' --- vec.h 2011-09-22 15:34:06 +0000 +++ vec.h 2011-09-22 17:46:39 +0000 @@ -58,7 +58,7 @@ void x(const T& val) { x_ = val; } void y(const T& val) { y_ = val; } - // A direct assignment of 'rhs' to this. + // A direct assignment of 'rhs' to this. Return a reference to this. tvec2& operator=(const tvec2& rhs) { if (this != &rhs) @@ -261,7 +261,7 @@ void y(const T& val) { y_ = val; } void z(const T& val) { z_ = val; } - // A direct assignment of 'rhs' to this. + // A direct assignment of 'rhs' to this. Return a reference to this. tvec3& operator=(const tvec3& rhs) { if (this != &rhs) @@ -489,7 +489,7 @@ void z(const T& val) { z_ = val; } void w(const T& val) { w_ = val; } - // A direct assignment of 'rhs' to this. + // A direct assignment of 'rhs' to this. Return a reference to this. tvec4& operator=(const tvec4& rhs) { if (this != &rhs)