Archive for September, 2012

MATLAB demo for “A Cotangent Laplacian for Images as Surfaces”

Tuesday, September 25th, 2012

By popular request I’ve put up a small MATALB demo corresponding to the colorization example from our technical report “A Cotangent Laplacian for Images as Surfaces”.

Hope Gangloff, do you resent letting the science influence the art? Do you let the science influence the art? Does the science influence the art? Or does the art influence the science? Do you let the art influence the science? Hope Gangloff, do you resent letting the art influence the science?

Sunday, September 23rd, 2012

Append third column of zeros to MATLAB matrix, but only if it doesn’t already exist

Saturday, September 22nd, 2012

Say I have input to a function which might be a list of (x,y) positions or a list of 3d (x,y,z) positions. To keep my function simple I may want to just treat the 2d input as 3d by appending z=0 to each point. This may be easily and safely achieved in a number of ways, but I think this is the shortest. Say that my list of potentially (x,y) or (x,y,z) positions is in an n by 2 or 3 matrix V. Then I can append a column 0s or do nothing to the matrix (depending on the size) with the following call:


V(:,end+1:3) = 0;


PHP cURL command line app not waiting for response

Monday, September 17th, 2012

I had written a PHP command line app that took a URL as a command line argument:


php foo.php http://www.nytimes.com/2012/09/16/arts/shock-me-if-you-can.html?ref=arts&pagewanted=all


The foo.php was simply calling curl to grab the page in question, do a little parsing and then spit it back out. When I issued the command above though, the program seemed to finish immediately and then later spit out the results. I was left thinking that the curl commands were asynchronous.
It turned out to be much simpler. I had an ampersand (&) in the url argument. This was forking the process in my bash terminal. All I needed to do was put quotes around the argument:


php foo.php "http://www.nytimes.com/2012/09/16/arts/shock-me-if-you-can.html?ref=arts&pagewanted=all"


Kill Apple Remote Desktop via ssh

Monday, September 10th, 2012

I tried to remote control my work machine from home using Apple Remote Desktop and got the following error:


Apple Remote Desktop or another administration application is currently running.


The problem was that I had left the same Apple Remote Desktop client running on my work machine. To solve this I sshed to that machine and issued:


killall "Remote Desktop"


Update: You can then use remote desktop to reopen the Apple Remote Desktop client and go to preferences > Security and check “Allow control of this computer when this application is running”

Sans-serif not supported in MATLAB text using latex interpreter

Thursday, September 6th, 2012

If I try to issue in MATLAB:


title('\textsf{sans-serif}','interpreter','latex');


I get the following error:


Warning: Font cmss10 is not supported.
Warning: Font cmss10 is not supported.
Font creation failed twice.
java.io.IOException when creating Font object
Using File /Applications/MATLAB_R2011b.app/sys/fonts/ttf/cm/mwa_cmss10.ttf to create font mwa_cmss10
File.exists(): false
File.length(): 0
Exception:
at java.awt.Font.createFont(Font.java:978)
at com.mathworks.hg.util.FontConverter.convertToJavaFont(FontConverter.java:169)
at com.mathworks.hg.uij.GraphicsContext.SetWindowFont(GraphicsContext.java:1039)
Caused by:
null
Warning: Could not set font mwa_cmss10


I fixed this by finding cmss10.ttf on the web. And copying on top of /Applications/MATLAB_R2011b.app/sys/fonts/ttf/cm/mwa_cmss10.ttf.

Then when I issue the above command I get the correct output and only a few warnings:


Warning: Font cmss10 is not supported.
Warning: Font cmss10 is not supported.


Nabla (∇) in matlab plot title

Thursday, September 6th, 2012

I tried:


title('\nabla');


Which produces no errors or warnings on the terminal, but the title in the plot on my mac is just a little question mark in a black box: �
You can do a little better by switching to the latex interpreter, which unfortunately uses that smelly latex font


title('$\nabla$','interpreter','latex');


Cheap tricks for OpenGL transparency

Wednesday, September 5th, 2012

I’ve been experimenting with some hacks using basic OpenGL commands to achieve high/good-quality transparency (alpha-blending).

I’ll compare 4 methods.

1. Traditional: For this one (and all the others) we turn on alpha blending:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


and then just use a normal depth test without back face culling in a single pass:


glDisable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
// render teapot with alpha = alpha


Notice two things. The handle is not visible in the second picture and we have zebra stripe artifacts in the first picture due to alpha blending without sorting. Sorting is slow and view-dependent and won’t even guarantee correct results. So instead we’ll see how far we can get with cheap tricks.

2. GL_GREATER: This is a 3-pass attempt.

double f = 0.75; // Or some other factor
glDisable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
// render teapot with alpha = f*alpha

glDisable(GL_CULL_FACE);
glDepthFunc(GL_GREATER);
// render teapot with alpha = alpha

glDisable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
// render teapot with alpha = (alpha-f*alpha)/(1.0-f*alpha);


This often produces reasonable results but the idea is a bit strange. It’s sort like depth peeling from opposite ends. Hoping that if we only have two layers, we’ll catch both of them.

Notice now we have the handle showing up in the second image, but we still have ordering artifacts. Admittedly a lot of these would go away with back face culling, but then we don’t get two faced rending (blue inside, gold outside).

3. ALWAYS: This method will seem like a step back because it introduces more ordering artifacts due to the two-face rendering, but the concept is a bit simpler.

double f = 0.75; // Or some other factor
glDisable(GL_CULL_FACE);
glDepthFunc(GL_ALWAYS);
// render teapot with alpha = f*alpha

glDisable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
// render teapot with alpha = (alpha-f*alpha)/(1.0-f*alpha);

4. The idea is again the same that by taking two passes we first pick up a little bit of everything albeit not sorted in the correct order. Then we finish with one pass that ensures that the top layer is correct.

We’re getting artifacts from the ordering in the first image, but I’ll show in a second that these are just coming from not handling the two-face rendering correctly. What we see in both cases is that the bottom of the teapot shows clearly through the bottom.

5. QUINTUPLE PASS: This is the final solution that I arrived at. It involves five passes, which using display lists should be OK for many real-time applications.

glDisable(GL_CULL_FACE);
glDepthFunc(GL_LESS);
// render teapot with alpha = 0, to prime the depth buffer

glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glDepthFunc(GL_ALWAYS);
// render teapot with alpha = f*alpha

glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glDepthFunc(GL_LEQUAL);
// render teapot with alpha = (alpha-f*alpha)/(1.0-f*alpha)

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK;
glDepthFunc(GL_ALWAYS);
// render teapot with alpha = f*alpha

// There's a trade off here. With culling enabled then a perfectly
// opaque object (alpha=1) may be wrong. With it disabled, ordering
// artifacts may appear
// glEnable(GL_CULL_FACE);
// glCullFace(GL_BACK);
glDisable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
// render teapot with alpha = (alpha-f*alpha)/(1.0-f*alpha)


The idea is that first we prepare the depth buffer with the final depth values, then we use the previous method but just to show the inside “back-facing” surface, then we do the same to show the front.

6. Now we get two very nice renderings that even participate with the background well. We can continuously blend our alpha value to 1 and expect to see a perfectly correct opaque rendering. We can notice some small ordering artifacts in the top image near the handle. If we know we want a transparent object then we can flip back face culling on for the last pass to get rid of this. But as noticed this will give an incorrect result when alpha ≈ 1.

Incompatible libpng warning (errors) at runtime

Wednesday, September 5th, 2012

Using Yotam’s YImage library I ran into a problem at runtime:


libpng warning: Application was compiled with png.h from libpng-1.5.4
libpng warning: Application  is  running with png.c from libpng-1.4.12
libpng warning: Incompatible libpng version in application and library
libpng encountered a problem writing ...png


Turns out the culprit was which png library g++ was finding at the time I compiled the YImage library and the one I was linking my program too. To make it work I replaced:


-lpng


with


-L/usr/X11/lib -lpng


which ensures that I’m linking to the libpng that belongs to X11, which is for some reason what YImage uses.