Browse Source

linmath: allow constructing matrix from rows

This also enables using mat[n] wherever an LVecBase4 is accepted, as well as Mat4(*mat).
rdb 7 years ago
parent
commit
371c34d13b

+ 42 - 0
panda/src/linmath/lmatrix3_src.I

@@ -44,6 +44,14 @@ size() {
   return 3;
 }
 
+/**
+ *
+ */
+INLINE_LINMATH FLOATNAME(LMatrix3)::Row::
+operator const FLOATNAME(LVecBase3) &() const {
+  return *(const FLOATNAME(LVecBase3) *)_row;
+}
+
 /**
  * Defines a row-level constant accessor to the matrix.
  */
@@ -68,6 +76,14 @@ size() {
   return 3;
 }
 
+/**
+ *
+ */
+INLINE_LINMATH FLOATNAME(LMatrix3)::CRow::
+operator const FLOATNAME(LVecBase3) &() const {
+  return *(const FLOATNAME(LVecBase3) *)_row;
+}
+
 /**
  * Returns an identity matrix.
  *
@@ -132,6 +148,32 @@ FLOATNAME(LMatrix3)(FLOATTYPE e00, FLOATTYPE e01, FLOATTYPE e02,
   _m(2, 2) = e22;
 }
 
+/**
+ * Constructs the matrix from three individual rows.
+ */
+INLINE_LINMATH FLOATNAME(LMatrix3)::
+FLOATNAME(LMatrix3)(const FLOATNAME(LVecBase3) &row0,
+                    const FLOATNAME(LVecBase3) &row1,
+                    const FLOATNAME(LVecBase3) &row2) {
+  TAU_PROFILE("LMatrix3::LMatrix3(const LVecBase3 &, ...)", " ", TAU_USER);
+
+#ifdef HAVE_EIGEN
+  _m.row(0) = row0._v;
+  _m.row(1) = row1._v;
+  _m.row(2) = row2._v;
+#else
+  _m(0, 0) = row0._v(0);
+  _m(0, 1) = row0._v(1);
+  _m(0, 2) = row0._v(2);
+  _m(1, 0) = row1._v(0);
+  _m(1, 1) = row1._v(1);
+  _m(1, 2) = row1._v(2);
+  _m(2, 0) = row2._v(0);
+  _m(2, 1) = row2._v(1);
+  _m(2, 2) = row2._v(2);
+#endif  // HAVE_EIGEN
+}
+
 /**
  *
  */

+ 8 - 4
panda/src/linmath/lmatrix3_src.h

@@ -38,6 +38,7 @@ PUBLISHED:
     INLINE_LINMATH FLOATTYPE operator [](int i) const;
     INLINE_LINMATH FLOATTYPE &operator [](int i);
     INLINE_LINMATH static int size();
+    INLINE_LINMATH operator const FLOATNAME(LVecBase3) &() const;
   public:
     FLOATTYPE *_row;
     friend class FLOATNAME(LMatrix3);
@@ -48,6 +49,7 @@ PUBLISHED:
   PUBLISHED:
     INLINE_LINMATH FLOATTYPE operator [](int i) const;
     INLINE_LINMATH static int size();
+    INLINE_LINMATH operator const FLOATNAME(LVecBase3) &() const;
   public:
     const FLOATTYPE *_row;
     friend class FLOATNAME(LMatrix3);
@@ -58,10 +60,12 @@ PUBLISHED:
   INLINE_LINMATH FLOATNAME(LMatrix3) &operator = (
       const FLOATNAME(LMatrix3) &other);
   INLINE_LINMATH FLOATNAME(LMatrix3) &operator = (FLOATTYPE fill_value);
-  INLINE_LINMATH FLOATNAME(LMatrix3)(
-    FLOATTYPE e00, FLOATTYPE e01, FLOATTYPE e02,
-    FLOATTYPE e10, FLOATTYPE e11, FLOATTYPE e12,
-    FLOATTYPE e20, FLOATTYPE e21, FLOATTYPE e22);
+  INLINE_LINMATH FLOATNAME(LMatrix3)(FLOATTYPE, FLOATTYPE, FLOATTYPE,
+                                     FLOATTYPE, FLOATTYPE, FLOATTYPE,
+                                     FLOATTYPE, FLOATTYPE, FLOATTYPE);
+  INLINE_LINMATH FLOATNAME(LMatrix3)(const FLOATNAME(LVecBase3) &,
+                                     const FLOATNAME(LVecBase3) &,
+                                     const FLOATNAME(LVecBase3) &);
   ALLOC_DELETED_CHAIN(FLOATNAME(LMatrix3));
 
   EXTENSION(INLINE_LINMATH PyObject *__reduce__(PyObject *self) const);

+ 51 - 0
panda/src/linmath/lmatrix4_src.I

@@ -44,6 +44,14 @@ size() {
   return 4;
 }
 
+/**
+ *
+ */
+INLINE_LINMATH FLOATNAME(LMatrix4)::Row::
+operator const FLOATNAME(LVecBase4) &() const {
+  return *(const FLOATNAME(LVecBase4) *)_row;
+}
+
 /**
  * Defines a row-level constant accessor to the matrix.
  */
@@ -68,6 +76,14 @@ size() {
   return 4;
 }
 
+/**
+ *
+ */
+INLINE_LINMATH FLOATNAME(LMatrix4)::CRow::
+operator const FLOATNAME(LVecBase4) &() const {
+  return *(const FLOATNAME(LVecBase4) *)_row;
+}
+
 /**
  * Returns an identity matrix.
  *
@@ -178,6 +194,41 @@ FLOATNAME(LMatrix4)(FLOATTYPE e00, FLOATTYPE e01, FLOATTYPE e02, FLOATTYPE e03,
   _m(3, 3) = e33;
 }
 
+/**
+ * Constructs the matrix from four individual rows.
+ */
+INLINE_LINMATH FLOATNAME(LMatrix4)::
+FLOATNAME(LMatrix4)(const FLOATNAME(LVecBase4) &row0,
+                    const FLOATNAME(LVecBase4) &row1,
+                    const FLOATNAME(LVecBase4) &row2,
+                    const FLOATNAME(LVecBase4) &row3) {
+  TAU_PROFILE("LMatrix4::LMatrix4(const LVecBase4 &, ...)", " ", TAU_USER);
+
+#ifdef HAVE_EIGEN
+  _m.row(0) = row0._v;
+  _m.row(1) = row1._v;
+  _m.row(2) = row2._v;
+  _m.row(3) = row3._v;
+#else
+  _m(0, 0) = row0._v(0);
+  _m(0, 1) = row0._v(1);
+  _m(0, 2) = row0._v(2);
+  _m(0, 3) = row0._v(3);
+  _m(1, 0) = row1._v(0);
+  _m(1, 1) = row1._v(1);
+  _m(1, 2) = row1._v(2);
+  _m(1, 3) = row1._v(3);
+  _m(2, 0) = row2._v(0);
+  _m(2, 1) = row2._v(1);
+  _m(2, 2) = row2._v(2);
+  _m(2, 3) = row2._v(3);
+  _m(3, 0) = row3._v(0);
+  _m(3, 1) = row3._v(1);
+  _m(3, 2) = row3._v(2);
+  _m(3, 3) = row3._v(3);
+#endif  // HAVE_EIGEN
+}
+
 /**
  *
  */

+ 10 - 4
panda/src/linmath/lmatrix4_src.h

@@ -36,6 +36,7 @@ PUBLISHED:
     INLINE_LINMATH FLOATTYPE operator [](int i) const;
     INLINE_LINMATH FLOATTYPE &operator [](int i);
     INLINE_LINMATH static int size();
+    INLINE_LINMATH operator const FLOATNAME(LVecBase4) &() const;
   public:
     FLOATTYPE *_row;
     friend class FLOATNAME(LMatrix4);
@@ -46,6 +47,7 @@ PUBLISHED:
   PUBLISHED:
     INLINE_LINMATH FLOATTYPE operator [](int i) const;
     INLINE_LINMATH static int size();
+    INLINE_LINMATH operator const FLOATNAME(LVecBase4) &() const;
   public:
     const FLOATTYPE *_row;
     friend class FLOATNAME(LMatrix4);
@@ -60,10 +62,14 @@ PUBLISHED:
       const FLOATNAME(UnalignedLMatrix4) &other);
   INLINE_LINMATH FLOATNAME(LMatrix4) &operator = (FLOATTYPE fill_value);
 
-  INLINE_LINMATH FLOATNAME(LMatrix4)(FLOATTYPE e00, FLOATTYPE e01, FLOATTYPE e02, FLOATTYPE e03,
-                                     FLOATTYPE e10, FLOATTYPE e11, FLOATTYPE e12, FLOATTYPE e13,
-                                     FLOATTYPE e20, FLOATTYPE e21, FLOATTYPE e22, FLOATTYPE e23,
-                                     FLOATTYPE e30, FLOATTYPE e31, FLOATTYPE e32, FLOATTYPE e33);
+  INLINE_LINMATH FLOATNAME(LMatrix4)(FLOATTYPE, FLOATTYPE, FLOATTYPE, FLOATTYPE,
+                                     FLOATTYPE, FLOATTYPE, FLOATTYPE, FLOATTYPE,
+                                     FLOATTYPE, FLOATTYPE, FLOATTYPE, FLOATTYPE,
+                                     FLOATTYPE, FLOATTYPE, FLOATTYPE, FLOATTYPE);
+  INLINE_LINMATH FLOATNAME(LMatrix4)(const FLOATNAME(LVecBase4) &,
+                                     const FLOATNAME(LVecBase4) &,
+                                     const FLOATNAME(LVecBase4) &,
+                                     const FLOATNAME(LVecBase4) &);
   ALLOC_DELETED_CHAIN(FLOATNAME(LMatrix4));
 
   EXTENSION(INLINE_LINMATH PyObject *__reduce__(PyObject *self) const);