Archive for June, 2010

Check your stereo speaker setup

Wednesday, June 30th, 2010

When ever I set up the laptop with those little computer speakers I have to check the stereo setup: so I’m sure the right speaker is on the right and the left on the left. On the Mac I do this by going to System Preferences > Sound then switching the sound all the way to the right and flipping the volume up and down to make little bleeps. Then the same for the left.

Finally here’s a definitive webapp solution:


Vi(m) tip #9: Copy, Cut and Paste into Mac OS X clipboard

Wednesday, June 30th, 2010

I can get a lot done in vim without ever having to use the mouse or really exit “vim world”. But the one thing that I keep falling back on is copy and pasting from and to other applications. Here’s a way to do these
operations without resorting to the mouse or foreign keystrokes. Issue these in command mode:

Cut line under cursor


:.!pbcopy

Copy line under cursor


:.!pbcopy|pbpaste

Paste as line beneath cursor


:r !pbpaste

You can also use this if you’ve made a selection in Visual Mode:

Cut current selection

(warning: this grabs the whole lines of any lines within selection)


:'<,'>!pbcopy

Copy current selection

(warning: this grabs the whole lines of any lines within selection)


:'<,'>!pbcopy|pbpaste

Note: It seems for that one you should just select the first letter of each line. Not the entire block, then it just cuts…

Update: It’s much simpler:


"*yy

Yanks the current line to the clipboard. Similarly,


"*dd

cuts the current line and


"*p

pastes the clipboard below the current line.

OpenGL texture map all white

Monday, June 28th, 2010

After a long battle today, I found a bug that was making all my textures render white. Here’s pseudo code of what I had:

Bugged code

GLwidget.h


class GLwidget : QGLWidget {
  GLWidget();
  ...
  void initializeGL();
  void paintGL();
  ...
}

GLwidget.cpp


GLWidget::GLWidget(){
  // code that generates texture
  makeMyTexture(...);
  // opengl calls that setup texture in GL land
  glGenTextures(1, &texName);
  glBindTexture(GL_TEXTURE_2D, texName);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  ...
  glTexImage2D(GL_TEXTURE_2D, ...) ;
}
  ...
void GLWidget ::initializeGL(){
  // opengl initialization code
}
void GLWidget :: paintGL(){
  // code that actually uses the texture
  glEnable(GL_TEXTURE_2D)
  ...
  glDisable(GL_TEXTURE_2D)
}

My GLWidget class inherited from a class that called initializeGL and paintGL at the appropriate times. The only problem was that I was building and setting my texture in the class constructor before initializeGL() was ever called. Then when it was, it wiped out my texture.

The correct order should be:

Correct

GLwidget.cpp


GLWidget::GLWidget(){
  ...
}
void GLWidget ::initializeGL(){
  // opengl initialization code
  ...
  // code that generates texture
  makeMyTexture(...);
  // opengl calls that setup texture in GL land
  glGenTextures(1, &texName);
  glBindTexture(GL_TEXTURE_2D, texName);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  ...
  glTexImage2D(GL_TEXTURE_2D, ...) ;
}
void GLWidget :: paintGL(){
  // code that actually uses the texture
  glEnable(GL_TEXTURE_2D)
  ...
  glDisable(GL_TEXTURE_2D)
}

Initialize your texture after you have initialized opengl.

X11 libraries not found on Mac OS X 10.6

Monday, June 28th, 2010

I never had this trouble on my mac os x 10.5 machine, but now on 10.6 I’m getting errors when including X11 libraries. When I try to compile using -lX11 or -lXext I get complaints from the linker like


ld: library not found for -lX11

or


ld: library not found for -lXext

To fix this you could modify your environment paths, or add the following to your compile flags:


-L/usr/X11/lib

I do that and everything compiles and links fine.

One time password for single use guest key

Saturday, June 26th, 2010

What’s the online equivalent to leaving your house key under the doormat?

A number of times I have been caught in a situation where I’m not at a computer and need to get something off of my email. I call a friend, entrust them with my password, retrieve the information and proceed to worry that I have (1) allowed my friend to (un)intentionally view something private of mine or (2) at least put my friend in the unfair situation where he needs to convince himself not to snoop. Whether he ever would or should is not the point, the situation of my giving him such a powerful position is bad enough.

Here’s a solution:
When I sign up for a new email account, I should give a normal password and a “one time password”. I don’t tell anyone my OTP. But then if I ever need to let a friend into my account I give them this OTP that works just once.

The email provider could come up with all sorts of different schemes for how the “just once” is defined. By time: logs off automatically after 60 seconds. By activity: logs off after a n searches. Or just limit access for a single session: only able to view certain emails or of a certain age…blah blah blah.

It could be as secure as you wanted to be. The point is that the current situation isn’t secure at all: you have to give out your entire password. I guess the reason giving out your password is not the same as leaving a key under your doormat is that your key is a single object and it doesn’t unlock every door in your house much less provide you with a simple way to search for valuable items.

Barycentric versus voronoi regional area in mass matrices

Friday, June 25th, 2010

In a recent project, we were minimizing an energy over a surface approximated by a triangle mesh. Our finite element method approach reduced our energy to solving a system of linear equations with coefficients from a stiffness matrix and mass matrix. In the lumped version of a mesh’s mass matrix, diagonal entries correspond to areas on the surface around each vertex. You want all the areas to sum to the total area of the surface and you would like the area for each vertex to some how reflect the influence of its incident triangles.

Barycentric

One natural choice is a barycentric area. Take the barycenter (centroid, center of mass), which lies at the intersection of medians. Using those medians divide the triangle into three quadrilaterals each corresponding its incident point:

generic barycentric quads

These quadrilaterals are by definition of equal area. The barycentric mass matrix entry for vertex i is the sum of its corresponding quadrilaterals from all incident triangles, or more simply: one-third of the total area of all neighboring triangles. Notice that the contribution of a triangle to its vertices is the same for all three, regardless of there placement around the triangle.

generic barycentric region on mesh

Voronoi

Another, also natural, choice is voronoi area. Take the circumcenter, which lies at the center of the circumscribing circle of the triangle. It follows quickly that the circumcenter is equidistant from all vertices on the triangle. Connect this point to the midpoints along the edges of the triangle, again forming three quadrilaterals.

acute voronoi quads

Note that this time the quadrilaterals are not equal. They are not even guaranteed to be positive. For obtuse triangles, the acute angled vertices will receive negative areas (assuming you demand their sums equal the area of the triangle). Here is a plot of voronoi areas as the angle of an isosceles triangle grows from 0 (acute) to π (obtuse). The x axis is the angle of the isosceles triangle and the y axis is area. The green line shows the area of corresponding to the vertex at the angle that’s growing, the blue corresponds to the other two areas.

voronoi isoceles plot

Notice that at π/2 we might expect the other two areas to cross into negative but actually this happens later. So voronoi areas won’t produce negative coefficients until fairly obtuse.

Again, the voronoi mass matrix entry for vertex i is the sum of its corresponding quadrilaterals from all incident triangles. Since areas can be negative, this means negative entries could end up in the voronoi mass matrix.

generic voronoi on mesh

Connectivity dependence

The Voronoi area of a vertex is called so because the area represents its corresponding voronoi region: all the points on the surface for which this vertex is the closest vertex (assuming the mesh has “nice” triangles: non-obtuse). In this way the voronoi area depends only on the vertices positioning on the surface not the connectivity of the mesh.

The barycentric area, while having the advantage of always being positive, is heavily connectivity dependent as a vertex gets one-third of the area of any connected triangle.

For a simple example, here’re two meshes. One with regular positions and regular connectivity:
regular positions, regular connectivity

And one with regular positions and the same regular connectivity but a single edge at each highlighted vertex has been flipped:
regular positions, one edge flipped

And here are the barycentric (left) and voronoi (right) areas for the highlighted vertices on the regular connectivity mesh.
regular with areas

Now, notice how when the edge is flipped the barycentric area changes drastically, and actually changes for all vertices incident on the flipped edge. The voronoi however stays the same for this vertex and all vertices.
one-edge flipped with areas

Hybrid approach to vonoroi

To handle the problem of negative voronoi areas for some obtuse triangles, there exists a hybrid approach. For acute triangles, use voronoi as is. For obtuse triangles snap the circumcenter to the midpoint along the edge opposite the large angle and use the corresponding quads (I described this in an earlier post). Notice for a right angle triangle the circumcenter corresponds with this midpoint, so as a triangle becomes obtuse there isn’t a jump in area distribution. A keen eye will notice that I am doing this in the voronoi area picture above for the obtuse triangle.

Euclidean (floored) modulo (mod, %) operator for C, C++

Thursday, June 24th, 2010

Once again, I was gotcha-ed by the sign of the mod operator in C++. I did -1%n, hoping to get n-1 but instead got -1.
Here’s a little C/C++ function (you could even macro/inline it) to give the “true” (Euclidean) modulo:


static int mod(int a, int base){    
  return ((a % base) + base) % base;
}

Update: So the above runs about twice as slow as just issuing % once, which is to be expected since % is called twice (guess + is pretty cheap in comparison). If half the time a will be positive and half the time negative then a average speed up of 1.5x over the above code can be achieved with the following:


static int mod(int a, int base){    
  return (a < 0 ? ((a % base) + base) % base : a % base);
 }
 

But it is still slower if you already know for sure that a is not negative.

Update: It's worth noting, as it was noted in the comments, that what I describe above is only correct if the base is positive. If the base is negative than euclidean modulo allows a negative result and must be defined accordingly.

Vi(m) tip #8: scroll cursor to top, center or bottom of screen

Wednesday, June 23rd, 2010

In command mode you can do some handy quick scrolling.

Scroll so that line with cursor is in the center of the screen:


z.

Scroll so that line with cursor is at the top of the screen:


zt

Scroll so that line with cursor is at the bottom of the screen:


zb

source

Brightness Menulet (revised)

Monday, June 21st, 2010

I’ve taken another swing at making a brightness menulet like the volume menulet. My old version could only toggle brightness (because of UI problems). But the new version is a full on slider!
brightness menulet slider
Here’s the xcode project and intel/ppc builds for at least mac os x 10.5:
Brightness Menulet Xcode Project
Brightness Menulet (intel)
Brightness Menulet (ppc)

Note: This menulet (and the original brightness code) does not work with 10.6 on my Mac Cinema HD display, but it does work with 10.6 on my girlfriend’s MacBook.

Split (large) files in half using bash script

Monday, June 21st, 2010

Here’s a simple bash script that splits a given file into two equal (up to a byte) halves. It uses split which has different syntax between the GNU and BSD implementations, so see the comments for switching between the two.
I tested this on a 6.4 GB dmg file and I was able to reverse the split with cat just fine. I diffed the re-concatenated file and the original and they were byte-wise equal.

Save this in a file called half.sh:


#!/bin/bash
#
# usage: half foo 
#
# This will create foo.aa and foo.ab, where foo.aa is the first half(+1) of foo
# and foo.ab is the second half. Split by bytes not lines.
#
#
# To reverse just issue:
# cat foo.aa foo.ab > foo
#

# get file size
# GNU:
#size=`stat -c "%s" "$1"`
# BSD:
size=`stat -f "%z" "$1"`

# divide by 2 and round up
half_size=`echo "($size+1)/2" | bc`
split -b $half_size "$1" "$1."