## Archive for August, 2010

### Vim syntax highlighting for cmake’s CMakeLists.txt files

Tuesday, August 31st, 2010

I use the following at the end of my .vimrc file to force vim to highlight cmake’s CMakeLists.txt files like any other cmake file:




Looks much better than whatever it was trying to do before, which for me was nothing.

### Universal File Dialog, a first step

Tuesday, August 31st, 2010

I’m switching from Qt to straight Glut with AntTweakBar for my next project. I’m happy about getting rid of Qt. For my small prototyping projects it was too heavy (hard to get a target working on computers that don’t have Qt installed) and too much work to add simple ui for tweaking parameters and flipping flags. AntTweakBar doesn’t yet solve the deployable problem (I’m trying to figure that out too) but is miles better for prototyping. The only thing I miss from Qt is the native File Save/File Open Dialogs. Since Glut is fairly universal I have taken a stab at writing a small header that could be the start of a universal file dialog “library”. I’d only want this library to depend on standard includes and it only needs to implement get_file_save_path and get_file_open_path.

Here’s what I put in FileDialog.h:


#include <stdio.h>
#define FILE_DIALOG_MAX_BUFFER 1024

// Sets buffer to a path to an existing file
// buffer[0]=0 on cancel
//
// Usage:
//   char buffer[FILE_DIALOG_MAX_BUFFER];
//   get_open_file_path(buffer);
void get_open_file_path(char buffer[]){
#ifdef __APPLE__
// For apple use applescript hack
FILE * output = popen(
"osascript -e \""
"   tell application \\\"System Events\\\"\n"
"           activate\n"
"           set existing_file to choose file\n"
"   end tell\n"
"   set existing_file_path to (POSIX path of (existing_file))\n"
"\" 2>/dev/null | tr -d '\n' ","r");
while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL ){
}
#else
// For every other machine type
printf("Please enter a file path: ");
gets(buffer);
#endif
}

// Sets buffer to a path to a new/existing file
// buffer[0]=0 on cancel
//
// Usage:
//   char buffer[FILE_DIALOG_MAX_BUFFER];
//   get_save_file_path(buffer);
void get_save_file_path(char buffer[]){
#ifdef __APPLE__
// For apple use applescript hack
// There is currently a bug in Applescript that strips extensions off
// of chosen existing files in the "choose file name" dialog
// I'm assuming that will be fixed soon
FILE * output = popen(
"osascript -e \""
"   tell application \\\"System Events\\\"\n"
"           activate\n"
"           set existing_file to choose file name\n"
"   end tell\n"
"   set existing_file_path to (POSIX path of (existing_file))\n"
"\" 2>/dev/null | tr -d '\n' ","r");
while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL ){
}
#else
// For every other machine type
printf("Please enter a file path: ");
gets(buffer);
#endif
}


And here’s a sample program that calls both functions. Save it in test.c:


#include "FileDialog.h"
#include <stdio.h>

int main(void){
char buffer[FILE_DIALOG_MAX_BUFFER];

get_open_file_path(buffer);
if(buffer[0] == 0)
printf("Cancelled\n");
else
printf("Open file path: %s\n",buffer);

get_save_file_path(buffer);
if(buffer[0] == 0)
printf("Cancelled\n");
else
printf("Save file path: %s\n",buffer);

return 0;
};

 

Compile and run on a Mac with:


gcc -o test test.c;
./test


Update: I've been looking into a way to do the above for Mac using Carbon or Cocoa but it seems impossible to do correctly without starting up an entire new app...

Update: I've noticed many annoying quirks about the above applescript. There's no way to specify only showing certain file extensions (just UTI types but that's not always what you want). It's quite slow to pop up. But most annoying was that focus was not returned to my GLUT app. Here's the hack I place in my app to use applescript to return the focus to my app. Seems to work great:


// you call to FileDialog.h
get_open_file_path(...)
#ifdef __APPLE__
// Hack to put focus back on app
FILE * output = popen(
"osascript -e \""
"   tell application \\\"NameOfYourApp\\\"\n"
"           activate\n"
"   end tell\"\n"
,"r");
#endif


Of course, replace NameOfYourApp with the name of your app. If you're unsure what this name is, try using the name that appears above your app's icon on the dock when you hover over it.

### Compiling, installing and using AntTweakBar on Mac OS X

Monday, August 30th, 2010

Seeing Bruno Levy‘s demos at SIGGRAPH 2010 convinced me that I want to start using the painlessly easy prototyping GUI library, AntTweakBar. The premise of AntTweakBar is great. Prototyping should be easy and a researcher shouldn’t spend any time on UI that just flips flags and changes input parameter values. With AntTweakBar UI is added with a single line of code for any variable you want to expose. Moreover AntTweakBar figures out which type of UI element should show up: switches for bools, RGB/HSV value manipulators for colors, spinners for numbers. And it’s all Glut and OpenGL (and DirectX I guess), which for most is exactly what they’re already using.

Compiling and installing AntTweakBar as a dynamically linked library on a mac is a bit tricky though. Here’s what I did:

Dowload the source from the AntTweakBar site.

Go to the source directory:


cd AntTweakBar/src


I had to make some changes to the Makefile.osx. Edit the lines to look like this


CXXCFG          = -O3 -arch i386 -arch x86_64
LFLAGS          = -arch i386 -arch x86_64
OUT_DIR         = /usr/local/lib

BASE            = /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks

 

I want to compile AntTweakBar universally (32-bit and 64-bit) for a 10.6 machine so I need those -arch options. You can leave those out if you just want the default setting.

Copy Makefile.osx over the exisiting Makefile:


cp Makefile.osx Makefile


Then build with


sudo make


This will put the dylib in the right place so you don't have to mess with DYLD_LIBRARY_PATH each time you link to AntTweakBar

Since I've installed libAntTweakBar.dylib all the way at the /usr/local/ level, I'll also put the header file there:


sudo cp ../include/AntTweakBar.h /usr/local/include


Now, hop over to the examples directory:


cd ../examples


Now you should be able to universally compile a simple example with the following:


gcc -O3 -arch i386 -arch x86_64 -Wall -fno-strict-aliasing -D_MACOSX TwSimpleGLUT.c -lAntTweakBar -lpthread -lm -framework OpenGL -framework GLUT -o TwSimpleGLUT


And run it with:


./TwSimpleGLUT


And you should get something like this:

Update:
Mac ports does have a port for AntTweakBar, but it's always such a nightmare dealing with the 32-bit/64-bit problem with mac ports so I have done the above. With macports you can just issue:


sudo port install anttweakbar


And you should be able to compile and run the example in the same manner.

Thursday, August 26th, 2010

### Compiling L-BFGS-B on Mac OS X and mixing with c++

Tuesday, August 17th, 2010

A new optimization problem required a black box solver that could impose constant bounds on the variables. We opted to use an implementation of L-BFGS-B which does exactly that.
I’m working on a Mac OS X, 10.6, machine and our code base is in c++. So I needed to compile the L-BFGS-B code, which is in fortran and be able to call it from cpp files.

### Compiling L-BFGS-B

Note: the following solution requires that you have a proper f2c installed. I used these steps to compile a universal (32-bit and 64-bit) f2c.

You’ll notice there are four *.f files:
routines.f contains the solver
driver*.f contain examples showing you how to set up the parameters to the solver

The Makefile included in the source will try to compile all four files into *.o object files then make executables out of each example. When I try:


make


it seems that while the object files for each *.f file get created, my f77 doesn’t understand how to make the executables. I get errors like this:


f77 -O driver3.o routines.o -o x.lbfgsb3
invalid parameter driver3.o
Undefined symbols:
"_MAIN__", referenced from:
_main in libf2c.a(main.o)
collect2: ld returned 1 exit status


This would be OK because I don’t really care about the example executables. But in order to figure out how to call subroutines from routines.o from C/C++, I’ll need to know that they work in the first place. Moreover, f77 was creating x86_64 object files and my code base is i386 (though I’m trying my hardest to build the dependencies as universals).

After digging around in the F77 script I first found where I could tell the compiler to make universal binaries. I do that by setting the CFLAGS environment variable. So as a replacement for the faulty Makefile, I’ve composed this script which should do the same thing but make everything universal for the mac:

Save this in the same directory as the *.f files as make_all.sh:


#!/bin/bash

# save old CFLAGS value
OLD_CFLAGS=$CFLAGS; # set CFLAGS used by F77 to contain gcc parameters necessary to produce # universal object files export CFLAGS="$CFLAGS -arch i386 -arch x86_64";

# compile the algorithm as a universal object file
f77 -O -c -o routines.o routines.f

# compile each of the examples as universal executables
for i in 1 2 3
do
# translate driver*.f to driver*.c
f2c driver$i.f # compile driver*.c to universal binary driver*.o cc$CFLAGS -c -o driver$i.o driver$i.c
# link driver*.o and routines.o into unversal binary executable x.lbfgsb*
cc $CFLAGS driver$i.o routines.o -lf2c -lm -o x.lbfgsb$i # remove driver*.c rm driver$i.c
done

# restore old CFLAGS value
\\
\\

\begin{tabular}{llll}
\fblockmatrix{0.5in}{0.5in}{A}&
\fblockmatrix{0.1in}{0.5in}{x}&
\blockmatrix{0.1in}{0.5in}{=}&
\fblockmatrix{0.1in}{0.5in}{b}
\\
\fblockmatrix{0.5in}{0.25in}{G} & & &
\fblockmatrix{0.1in}{0.25in}{0}
\\
\end{tabular}
\\\\

\end{document}


Which produces this:

Admittedly there’s a lot left to do. The parenthesis behavior is annoying and it’s not terribly easy to stack them.

### hair

Sunday, August 8th, 2010

### Content-aware content

Saturday, August 7th, 2010