Sunday 21 December 2014

High Resolution performance timer

I recently stumbled across this issue because I was trying to compile some code on an ARM based computer. There was code in the program I wanted to compile that uses assembly! I am not going to get into the details over which method might be faster or has higher resolution. From what I have learned this is the most compact and portable code to use if you want a high resolution counter that can be used for something like performance profiling.


This was the original code that was causing the issue. This code depends on the x86 instruction set.

#ifdef _WIN32
   unsigned long long tick;
   QueryPerformanceCounter((LARGE_INTEGER *)&tick); // works great on Windows ONLY
   return tick;
#else
 uint32_t hi, lo;
   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); // Works well on x86 only
   return ( (uint64_t)lo)|( (uint64_t)hi)<< 32 );
#endif



Thanks to improvements on the POSIX based
int clock_gettime(clockid_t clk_id, struct timespec *tp);
 
We can replace our not portable assembly code for our easy to use clock_gettime code like so



#ifdef _WIN32
   unsigned long long tick;
   QueryPerformanceCounter((LARGE_INTEGER *)&tick); // works great on Windows ONLY
   return tick;
#else
   timespec timeInfo;
   clock_gettime(CLOCK_MONOTONIC_RAW, &timeInfo); // nanosecond resolution
   unsigned long long int nanosecs = ((unsigned long long)timeInfo.tv_sec)*1000000000  + 
                       ((unsigned long long)timeInfo.tv_nsec);
   return nanosecs;
#endif

Best of luck.

References:
  1. http://man7.org/linux/man-pages/man2/clock_gettime.2.html
  2. http://tdistler.com/2010/06/27/high-performance-timing-on-linux-windows
  3. http://en.wikipedia.org/wiki/High_Precision_Event_Timer

Sunday 12 October 2014

Mesh Subdivision with the Loop Algorithm

Recently I have been learning more about Polygonal meshes and operations you can perform on them. One of the first operations that can be done on a mesh, that is a kind of cool, is subdivision. The basic idea is that you have a mesh that is very course (few polygons, rough) and we want a method that can construct a smoother mesh from this course mesh. This has been a popular method for constructing extremely smooth meshes for movies for many years.






This is an example very course mesh of a Camel. This mesh was not constructed this way by chance. An artist spent many hours adjusting verticies (points) and faces (polygons or triangles) on this mesh in order for subdivision to work well.

The Loop algorithm is a popular algorithm to perform mesh subdivision. One reason for its popularity is that it ensures that after subdivision the mesh is C2 continuous (smooth second derivative on surface) everywhere on the mesh except for at extreme points.

After lots of careful thought I decided to implement the algorithm in 3 steps.
  1. Add all of the new verticies for the mesh (easy)
    1. Take every edge and add a new point between the two points that define the edge
  2. Construct/fix all of the polygons in the mesh (not so easy)
    1. Add edges between all of the new points and update the face/polygon labels for these edges
    2. Most bugs came from this step
  3. Compute the new positions of the verticies on a copy of the mesh from 2 (almost easy)
    1. Simply use the old mesh to compute all of the neighbours for old points. 
    2. Computed the weighted sum of positions from neighbours and selected point (See loop Algorithm) and assign this position on the mesh copy
    3. For new points I used the new mesh to get the neighbours of each new point that was in the old mesh
    4. Last I had to run a query to get the edge between the points from (3). This got me the edge I should use to find the last two neighbour points for the Loop Algorithm
The coding also assumes some knowledge of how to work with a half-edge data structure for the mesh.

original mesh



After one subdivision



After two

After three



final mesh

Camel mesh after simplifaction




References:
  1. http://www.cs.ubc.ca/labs/imager/tr/2014/CartelModeling/Cartel_website/
  2. https://www.graphics.rwth-aachen.de/media/papers/sqrt31.pdf
  3. http://research.microsoft.com/en-us/um/people/cloop/thesis.pdf

Saturday 6 September 2014

Install SciPy on Mac OS


Way to many versions of python on Mac OS, but this seems to work.

Intstall homebrew
install gfortran
     brew install gfortran
     or
     brew install gcc
sudo easy_install scipy

References
  1. http://urinieto.com/2012/02/install-python-2-7-x-under-snow-leopard-lion-64-bits/
  2. http://stackoverflow.com/questions/11517164/scipy-numpy-matplotlib-troubles-on-osx
  3. http://brew.sh/
  4. http://docs.scipy.org/doc/numpy/user/install.html


Thursday 14 August 2014

Spelling and Grammar Checking for Latex

One of the hurdles of starting to use Latex for document creation after getting used to Word is "is there spelling AND grammar checking?" I am happy to say there is a solution to this problem.

There are a few Latex editors already that have support for spell checking but grammar checking is another issue all together. In this post I am going to give a quick guide on a great Latex editor and how you can add grammar checking as well.

TexStudio is becoming a very popular latex editor. It works across many platforms including Windows, Linux and Mac. TexStudio also has built in support for being able to compile and display the document being worked on.

LangaugeTool is a very interesting tool that can be used as stand alone grammar checker. It works as a server on your computer and any system can send HTTP requests to it to ask if there are any grammar issues.

Here are the steps to get everything setup:

  1. Download the version of TexStudio that will work for your operating system.
  2. Install TexStudio
  3. Download LangaugeTool. It uses java so you will need Java available on you computer already.
  4. Unpack Language tool into some directory
  5. In TexStudio Configure TXS first, by using Options > Configure TexStudio… > Grammar like this:
    http://languagetool.wdfiles.com/local--files/checking-la-tex-with-languagetool/texstudio262.png
  6. If TexStudio is configured to use inline grammar checking (which is the default, see Options > Configure TexStudio… > Editor > Inline Checking > Grammar), you should see grammar checks immediately:
  7. After setting this up you should automatically start seeing grammer highlighting in your documents.



As a side note you can easily grab more spelling dictionaries for TexStudio.

Best of Luck.

References
  1. http://wiki.languagetool.org/checking-la-tex-with-languagetool#toc4