瀏覽代碼

added matrix concatenation [,] and [;]

Former-commit-id: c1a22ca91fc14deaa6c4d1a803edb9f4e9ad02db
dpanozzo 13 年之前
父節點
當前提交
57165cd1b9
共有 1 個文件被更改,包括 70 次插入0 次删除
  1. 70 0
      concat.h

+ 70 - 0
concat.h

@@ -0,0 +1,70 @@
+#ifndef IGL_COLON_H
+#define IGL_COLON_H
+#include <Eigen/Dense>
+namespace igl
+{
+  // Concatenates dense matrices
+  // Templates:
+  //   T  should be a eigen matrix primitive type like int or double
+  // Inputs:
+  //   A first matrix
+  //   B second matrix
+  //   horiz if true, matrices are concatenated horizontally
+  // Output:
+  //   O if horiz = false return [A;B] else [A,B]
+  template <typename T>
+  inline void concat(
+                     const T A, 
+                     const T B,
+                     const bool horiz,                 
+                     T& O);
+  
+  template <typename T>
+  inline T concat(
+                  const T A, 
+                  const T B,
+                  bool horiz = false
+                  );
+
+}
+
+// Implementation
+#include <cstdio>
+
+template <typename T>
+inline void concat(
+                   const T A, 
+                   const T B,
+                   const bool horiz,                 
+                   T& O)
+{
+  if (horiz)
+  {
+    // O = [A,B]
+    assert(A.rows() == B.rows());
+    O = MatrixXd(A.rows(),A.cols()+B.cols());
+    O << A,B;
+  }
+  else
+  {
+    // O = [A;B]
+    assert(A.cols() == B.cols());
+    O = MatrixXd(A.rows()+B.rows(),A.cols());
+    O << A,B;
+  }
+}
+
+template <typename T>
+inline T concat(
+                const T A, 
+                const T B,
+                bool horiz = false
+                )
+{
+  O = T(1,1);
+  concat(A,B,horiz,O);
+  return O;
+}
+
+#endif
+