Posts Tagged ‘clock’

Background computation threads with igl::viewer::Viewer

Sunday, December 4th, 2016

Here’s a minimal example showing how to launch background computation threads for each mesh in a list of meshes. Meanwhile the main thread runs a mesh viewer with all meshes concatenated into one huge multi-component mesh. Whenever a computation thread signals that an update to the mesh needs to be made, the main thread will re-concatenate the meshes and update the viewer. In this example, the “computation” is determining how much to move a clock “hand” (random mesh).

Here’s the program running on the cow, cheburashka and knight models:

// Tiny example to demonstrate spawning a background computation thread for
// each mesh in a list and update the viewer when computation results in a
// change to (one of) the meshes.
// In this example, three meshes are read in and interpreted as "hands" of a
// clock. The "computation" is just a busy-wait until the hand should move
// (after one second, one minute, one hour). This is, of course, a terrible way
// to implement a clock.
// ./test libigl/tutorial/shared/{,,}
#include <igl/read_triangle_mesh.h>
#include <igl/point_mesh_squared_distance.h>
#include <igl/combine.h>
#include <igl/viewer/Viewer.h>
#include <Eigen/Geometry>
#include <thread>
int main(int argc, char * argv[])
  using namespace std;
  using namespace Eigen;
  // Read in k meshes (<=3 will be used)
  std::vector<Eigen::MatrixXd> VV(argc-1);
  std::vector<Eigen::MatrixXi> FF(argc-1);
  for(int i = 1;i<argc;i++)
    VV[i-1].col(0).array() -= VV[i-1].col(0).mean();
    VV[i-1].col(1).array() -= VV[i-1].col(1).minCoeff();
  bool continue_computing = true;
  // Flag to redraw and mutex to guard it
  bool redraw = false;
  std::mutex m;
  // After waiting `tic` seconds, rotate `V` by `deg` degrees
  const auto rot = [&continue_computing,&redraw,&m](
    const double tic, const double deg, Eigen::MatrixXd& V)
      // Let's watch this at 500x: Real clocks are slow.
      V *= Eigen::AngleAxisd(deg/180.*igl::PI,
        std::lock_guard<std::mutex> lock(m);
        redraw = true;
  // Launch background "computation" threads for each "hand" of the clock
  // std::ref is important, otherwise std::bind passes by copy
  std::vector<std::thread> threads;
    case 3:
    case 2:
    case 1:
    default: break;
  igl::viewer::Viewer v;;
  // Helper function to view k meshes
  const auto & set_meshes = [](
    const std::vector<Eigen::MatrixXd> & VV,
    const std::vector<Eigen::MatrixXi> & FF,
    igl::viewer::Viewer & v)
    Eigen::MatrixXd V;
    Eigen::MatrixXi F;
  // Continuous draw loop. TODO: trigger draws using glfwPostEmptyEvent
  v.core.is_animating = true;
  // Before every draw check if the meshes have changed. 
  v.callback_pre_draw = 
    [&redraw,&m,&VV,&FF,&set_meshes](igl::viewer::Viewer & v)->bool
          std::lock_guard<std::mutex> lock(m);
          redraw = false;
      return false;
  // Tell computation threads to stop
  continue_computing = false;
  // Join with computation threads: return to serial main thread.
  for(auto & t : threads) if(t.joinable()) t.join();

mesh clock igl viewer threads

This is pretty hacky, but it will allow me to use the standard libigl viewer for making comparisons of mesh-editing methods whose performances are very different.

Music clock

Monday, January 11th, 2010

The music clock is an invisible clock. It tells the time through musical
tones. For now, it reads the hour and the minutes mod 12. Hopefully I will be
able to explore this idea and come up with a universally understandable and
recognizable system.

I use ajax requests and ruby and sox on the backend to make this work.

Clock design take 2

Saturday, January 9th, 2010

After a friend showed me the Raphaël javascript library, I immediately wanted to rewrite my
old java geometric clock prototype applet into a new javascript version.

Here it is:

New Clock Design

Wednesday, December 2nd, 2009

I’ve made a new digital design prototype for a physical clock I plan to build.
Here’s a preview of my geometric clock prototype in action:

Here’s a preview of my geometric clock prototype frozen:
geometric clock design prototype