Browse Source

get_seconds example, still not sure why ctime is not working with GLUT...
transpose_blocks of matrix, with example,
fixed warning in ReAntTweakbar


Former-commit-id: a4ca5001bbe1382b8ad8f2af0ebdb663023685e3

jalec 13 years ago
parent
commit
47136a5d14

+ 1 - 1
ReAntTweakBar.h

@@ -627,7 +627,7 @@ bool igl::ReTwBar::set_value_from_string(
   TwType type, 
   const char * value_str)
 {
-  void * value;
+  void * value = NULL;
   // possible value slots
   int i;
   float v;

+ 22 - 0
examples/get_seconds/Makefile

@@ -0,0 +1,22 @@
+
+.PHONY: all
+
+all: example
+
+.PHONY: example
+
+igl_lib=../../
+
+CFLAGS=-g -Wall
+inc=-I$(igl_lib)
+lib=
+
+example: example.o
+	g++ $(CFLAGS) -o example example.o $(lib)
+	rm example.o
+
+example.o: example.cpp
+	g++ $(CFLAGS) -c example.cpp -o example.o $(inc)
+clean:
+	rm -f example.o
+	rm -f example

+ 20 - 0
examples/get_seconds/README

@@ -0,0 +1,20 @@
+Simple program showing how to use get_seconds from the igl library
+
+Compile with:
+  make
+
+Run with:
+  ./example
+which should produces something similar to:
+  start: 0.001414s
+  1.00141s - 0.001414s = 1s
+  2.00141s - 0.001414s = 2s
+  3.00142s - 0.001414s = 3s
+  4.00141s - 0.001414s = 4s
+  5.00141s - 0.001414s = 5s
+  6.00141s - 0.001414s = 6s
+  7.00141s - 0.001414s = 7s
+  8.00141s - 0.001414s = 8s
+  9.00141s - 0.001414s = 9s
+  10.0014s - 0.001414s = 10s
+  end: 10.0014s

+ 25 - 0
examples/get_seconds/example.cpp

@@ -0,0 +1,25 @@
+#include <cstdio>
+
+#include "get_seconds.h"
+using namespace igl;
+#include <cmath>
+using namespace std;
+
+int main(int argc, char * argv[])
+{
+  double start = get_seconds();
+  printf("start: %lgs\n",start);
+  double lap = start;
+  double now;
+  do
+  {
+    now = get_seconds();
+    if((now-lap)>=1.0)
+    {
+      printf("%lgs - %lgs = %lgs\n",now,start,now-start);
+      lap = now-((now-start)-floor(now-start));
+    }
+  }while((now - start)<10.0);
+  printf("end: %lgs\n",now);
+  return 0;
+}

+ 23 - 0
examples/transpose_blocks/Makefile

@@ -0,0 +1,23 @@
+
+.PHONY: all
+
+all: example
+
+.PHONY: example
+
+igl_lib=../../
+eigen=/usr/local/include/eigen3
+
+CFLAGS=-g
+inc=-I$(igl_lib) -I$(eigen)
+lib=
+
+example: example.o
+	g++ $(CFLAGS) -o example example.o $(lib)
+	rm example.o
+
+example.o: example.cpp
+	g++ $(CFLAGS) -c example.cpp -o example.o $(inc)
+clean:
+	rm -f example.o
+	rm -f example

+ 34 - 0
examples/transpose_blocks/README

@@ -0,0 +1,34 @@
+This is a simple example program that shows how to use the transpose_blocks
+function of the igl library
+
+
+To Build:
+  make
+
+To Run:
+  ./example
+which should produce
+  A=[
+    0   1   2   3
+    4   5   6   7
+  100 101 102 103
+  104 105 106 107
+  200 201 202 203
+  204 205 206 207
+  ];
+  B=[
+    0   4
+    1   5
+    2   6
+    3   7
+  100 104
+  101 105
+  102 106
+  103 107
+  200 204
+  201 205
+  202 206
+  203 207
+  ];
+
+

+ 53 - 0
examples/transpose_blocks/example.cpp

@@ -0,0 +1,53 @@
+#include <cstdio>
+#include <iostream>
+using namespace std;
+#include "transpose_blocks.h"
+using namespace igl;
+
+int main(int argc,char * argv[])
+{
+  // hieght of block
+  int m = 2;
+  // width of block
+  int n = 4;
+  // number of blocks
+  int k = 3;
+  // dimension blocks run along
+  int dim = 1;
+
+  // Input
+  Eigen::MatrixXd A;
+
+  if(dim == 1)
+  {
+    A.resize(m*k,n);
+  }else{
+    A.resize(m,n*k);
+  }
+
+  // loop over blocks
+  for(int b = 0;b<k;b++)
+  {
+    for(int i = 0;i<m;i++)
+    {
+      for(int j = 0;j<n;j++)
+      {
+        if(dim == 1)
+        {
+          A(b*m+i,j) = 100*b + i*n + j;
+        }else// dim == 2
+        {
+          A(i,b*n+j) = 100*b + i*n + j;
+        }
+      }
+    }
+  }
+  cout<<"A=["<<endl<<A<<endl<<"];"<<endl;
+
+  Eigen::MatrixXd B;
+  transpose_blocks(A,k,dim,B);
+  
+
+  cout<<"B=["<<endl<<B<<endl<<"];"<<endl;
+  return 0;
+}

+ 4 - 1
get_seconds.h

@@ -3,18 +3,21 @@
 
 namespace igl
 {
-  // Return the current time since Epoch in seconds
+  // Return the current time in seconds since program start
   inline double get_seconds();
 
 }
 
 //Implementation
 #include <sys/time.h>
+//#include <ctime>
 
 inline double igl::get_seconds()
 {
   timeval time;
   gettimeofday(&time, NULL);
   return time.tv_sec + time.tv_usec / 1e6;
+  // This does not work on mac os x with glut in the main loop
+  //return double(clock())/CLOCKS_PER_SEC;
 }
 #endif

+ 12 - 0
matlab-to-eigen.html

@@ -66,6 +66,18 @@ tr.d1 td
         <td>Find the minimum coefficient over all entries of the matrix.</td>
       </tr>
 
+      <tr class=d0>
+        <td><pre><code>eye(w,h)</code></pre></td>
+        <td><pre><code>MatrixXd::Identity(w,h), MatrixXf::Identity(w,h), etc.</code></pre></td>
+        <td></td>
+      </tr>
+
+      <tr class=d1>
+        <td><pre><code>A(i:(i+w),j:(j+h)) = eye(w,h)</code></pre></td>
+        <td><pre><code>A.setIdentity()</code></pre></td>
+        <td></td>
+      </tr>
+
       <!-- Insert rows for each command pair -->
 
       <!-- Leave this here for copy and pasting

+ 98 - 0
transpose_blocks.h

@@ -0,0 +1,98 @@
+#ifndef IGL_TRANSPOSE_BLOCKS
+#define IGL_TRANSPOSE_BLOCKS
+
+#include <Eigen/Core>
+
+namespace igl
+{
+  // Templates:
+  //   T  should be a eigen matrix primitive type like int or double
+  // Inputs:
+  //   A  m*k by n (dim: 1) or m by n*k (dim: 2) eigen Matrix of type T values
+  //   k  number of blocks
+  //   dim  dimension in which to transpose
+  // Output
+  //   B  n*k by m (dim: 1) or n by m*k (dim: 2) eigen Matrix of type T values,
+  //   NOT allowed to be the same as A
+  //
+  // Example:
+  // A = [
+  //   1   2   3   4
+  //   5   6   7   8
+  // 101 102 103 104
+  // 105 106 107 108
+  // 201 202 203 204
+  // 205 206 207 208];
+  // transpose_blocks(A,1,3,B);
+  // B -> [
+  //   1   5
+  //   2   6
+  //   3   7
+  //   4   8
+  // 101 105
+  // 102 106
+  // 103 107
+  // 104 108
+  // 201 205
+  // 202 206
+  // 203 207
+  // 204 208];
+  //   
+  template <typename T>
+  void transpose_blocks(
+    const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & A,
+    const size_t k,
+    const size_t dim,
+    Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B);
+}
+
+// Implementation
+#include <cassert>
+
+template <typename T>
+void igl::transpose_blocks(
+  const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & A,
+  const size_t k,
+  const size_t dim,
+  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B)
+{
+  // Eigen matrices must be 2d so dim must be only 1 or 2
+  assert(dim == 1 || dim == 2);
+  // Output is not allowed to be input
+  assert(&A != &B);
+
+
+  // block height, width, and number of blocks
+  int m,n;
+  if(dim == 1)
+  {
+    m = A.rows()/k;
+    n = A.cols();
+  }else// dim == 2
+  {
+    m = A.rows();
+    n = A.cols()/k;
+  }
+
+  // resize output
+  if(dim == 1)
+  {
+    B.resize(n*k,m);
+  }else//dim ==2
+  {
+    B.resize(n,m*k);
+  }
+
+  // loop over blocks
+  for(int b = 0;b<(int)k;b++)
+  {
+    if(dim == 1)
+    {
+      B.block(b*n,0,n,m) = A.block(b*m,0,m,n).transpose();
+    }else//dim ==2
+    {
+      B.block(0,b*m,n,m) = A.block(0,b*n,m,n).transpose();
+    }
+  }
+}
+#endif