Archive for October, 2009

scripting essentials: fullpath

One of the things you end up doing in cli world a lot is jacking around with files.

Usually you are doing some junk with files in some (collection of) terminal(s) with a given working directory and referring to files with a local path eg: src/main/webapp/WEB-INF/web.xml

Then… for some reason, you want to manipulate them from another context, maybe a browser or some shells doing stuff in another working directory. In some cases you could change working directories, but is that really what you want?

Kind of breaks the flow, doesn’t it? “Now let me hop over here…”

Why are you hopping? You want to grep src/main/webapp/WEB-INF/web.xml? Just use the fullpath name, eg: /home/jameson/src/devilish/src/main/webapp/WEB-INF/web.xml !

But how? EZ: echo ${PWD}/src/main/webapp/WEB-INF/web.xml

By jamming around the fullname instead of hopping around the filesystem like a bunny rabbit, you will be better able to maintain the separation between the different contexts of your activities.

Ya dig it? If not, try this approach out for a while and see if you can feel the difference.

To make this easier, I prefer to use a bash function like this:

fullpath() {
    local f
    for f in ${*}; do
        (
		 	(
			 	cd ${f} && echo ${PWD}
			) || (
			 	cd $(dirname ${f} ) && echo ${PWD}/$( basename ${f} )
			)
		) 2> /dev/null
    done
}

This takes care of some annoying corner cases that I can’t remember at the moment… but feel free to shorten it

This tip says: move the world around your mind, not your mind around the world.

Leave a Comment

lame java collection erasure problem

Just ran into an example of where Java’s dumb erasure strategy just didn’t cut it.

I wrote a small class to collect up vararg junk into typed collections, something like:

public class Collector {
    public Collection collect( typename... vs ) {...}
    public Collection collect( Collection collection, typename... vs ) {...}
};

This worked fine up until the point where the typename was a typed collection, at which point the second method was invoked instead.

My work around was to rename the latter to “collectInto”

Lame…

Leave a Comment

pretty column (field) printing with awk

You know what you have to do… you have to collect all the strings for every field, keep the max length for each column, then at the end spit them back out again.

But… you need to store each field for each row and awk only supports 1 dimensional arrays… suck it up and say the script only supports up to a max of 255 fields:

BEGIN {
	row = 0
	max_fields = 0;
	super_max = 255;
} {
	for ( i = 1 ; i <= NF ; i++ ) {
		idx = row + super_max * i;
		len = length( $i );
		data[ idx ] = $i;
		if ( len > max[ i ] ) {
			max[ i ] = len;
		}
	}
	if ( NF > max_fields ) {
		max_fields= NF;
	}
	row++;
} END {
	for ( r = 0 ; r < row ; r++ ) {
		for ( i = 1 ; i <= max_fields ; i++ ) {
			idx = r + super_max * i;
			fmt = "%-" max[ i ]  "s ";
			printf( fmt , data[ idx ], max[ i ]  );
		}
		printf( "\n" );
	}
}

columnbo.awk

Here is some prettified output:

this.logger            = logger;
this.dataSourceFactory = dataSourceFactory;
this.wsdlUrl           = wsdlUrl;
this.wsdlKey           = wsdlKey;
this.documentDAO       = documentDAO;

Leave a Comment

dear maven, yes! I want to use fckng java 1.5 generics, you ass!

OMG! I am so…. tired of doing this for every new archetype’d project:

<build>
	<plugins>
		<plugin>
			<!-- seriously? -->
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.5</source>
				<target>1.5</target>
			</configuration>
		</plugin>
	</plugins>
</build>

The gnashing of teeth!

Leave a Comment

Triangle Islands : opengl + haxe

Introduction

I was looking around for a neat demo app to show off Haxe Archetype ’s new opengl template when I came across this really nice description of the olde triangle mountain algorithm .

The basic idea is to subdivide triangles and randomly move vertices up or down like this really nice diagram shows:

Splitting up Triangles and Making them Mountains

OK…
so maybe that looks a little confusing… but the text makes it really clear: split each triangle into 4 subtriangles. Sometimes, likeGroo , I’m a little slow of mind, so I drew a little picture to help me out:
From this, my brain part could pretty directly come up with:
 t[ 0 ] =  pt1, mid1, mid3
 t[ 1 ] = mid1,  pt2, mid2
 t[ 2 ] = mid3, mid2,  pt3
 t[ 3 ] = mid1, mid2, mid3
It looks likethis inhaxe .

Avoid the Gaps

So far so good! Split up the triangles! Raise/Lower those vertices! But…

BEWARE!

If you just start jacking around willy-nilly (or even willy+nilly [untested with willy±nilly]), you might not notice that when it’s time to split ”t[1]” and and ”t[3]” they are ”’both”’ gunna split the segment that connects ”mid1” and ”mid2”.

Lawd help you if you raise that pt in one and lower it in the other… or even raise it by different amounts: you will gap yourself up good and you
won’t like it.

It will look something like this:
and noone will think you are cool… Not even mom and dad.

My solution is pretty uncool actually:MidPointBuddy has a getMidPoint which returns the midpoint for any segment and keeps track of them in a hash table.

Simple, but effective… but lame.

Some minor openGL points

If you know me or if you have read any of this you probably know that I completely and shamelessly rip off everyone all the time and never do my own work.

Occassionaly I might throw together some crappy third rate ascii art:
/*  p1              p2
      .------------.
       \` .         \
        \   ` .      \
         \     ` .    \
          \        ` . \
        p4 .------------. p3  */
var p1 = new Pt3( -f, 0, -f );
var p2 = new Pt3(  f, 0, -f );
var p3 = new Pt3(  f, 0,  f );
var p4 = new Pt3( -f, 0,  f );
but mostly I just rip everything off from the internet and never make any sort of contributions to anything.
This is the case with the app that drives this mess. “Borrowed” it from thexinf gang .
Though I may have made some teensy changes.

Here is another example… I used to have some code like this a billion years ago, but I have no idea where it is now… probably on some thumb drive…

Luckily, there is no point in knowing of remembering anything anymore and with the help of Steve Baker (who I’ve never met, but I hear is very nice), I cobbed something likethis together:
GL.shadeModel( GL.FRONT_AND_BACK );
GL.enable( GL.LIGHTING );
GL.enable( GL.COLOR_MATERIAL );
GL.colorMaterial( GL.FRONT_AND_BACK, GL.AMBIENT_AND_DIFFUSE );
GL.enable( GL.LIGHT0 );
Put that together with alittle :
e1 = p1 - p2;
e2 = p3 - p2;
normal.cross( e1, e2 ).normalize();
...
cross(e1,e2) {
    this.x = ( e1.y * e2.z - e1.z * e2.y );
    this.y = ( e1.z * e2.x - e1.x * e2.z );
    this.z = ( e1.x * e2.y - e1.y * e2.x );
    return this;
}
sprinkle in a little:

to taste and off it goes.

Go get it and run it! I’m not charging you for it!

I know you have already download and installed haxe and must have subversion installed…
 % svn checkout http://brianin3d-triangle-mountain.googlecode.com/svn/trunk/ brianin3d-triangle-mountain-read-only
 % cd brianin3d-triangle-mountain-read-only
 % haxe compile.hxml
 % ./app
I apologize for the controls: x,y,z,q,r

Check it out and see how easy it is to have a lot of fun withopengl andhaxe .

Got a better idea for a project? Fire it up with Haxe Archetype ’s new flash tip:

Holler!

Links

  • the code
  • dhood’s blog
  • Haxe Archetype
  • opengl for Haxe
  • Basic OpenGL Lighting
  • Saddy Faces?

    Sometimes things don’t work out like you’d like… I know… it’s a bummer…

    XF86VidModeQueryExtension

    Back when I first wrote this, this happened on my workstation, but not on my desktop:
    Called from opengl/GLU.hx line 271
    Called from GLFW__impl.hx line 16
    Uncaught exception - load.c(232) : Failed to load library : /usr/lib/haxe/lib/opengl/0,2,0/ndll/Linux/opengl.ndll (/usr/lib/haxe/lib/opengl/0,2,0/ndll/Linux/opengl.ndll: undefined symbol: XF86VidModeQueryExtension)
    

    they were both running Ubuntu 8.10, both with NVidia drivers… The problem ”’only”’ occurred on the workstation after some number of updates (and in 9.x) the problem magically disappeared…

    osx fix

    When I first ran this thing I got some junk like:

    DLLLoader.hx:50: added to Env: DYLD_LIBRARY_PATH Value /usr/lib/haxe/lib/opengl/0,2,0/ndll/Mac
    Called from opengl/GLU.hx line 271
    Called from GLFW__impl.hx line 16
    Uncaught exception - load.c(232) : Failed to load library : ./opengl.ndll (dlopen(./opengl.ndll, 1): Library not loaded: libglfw.dylib
      Referenced from: /Users/brian/tmp/misc/brianin3d-triangle-mountain-read-only/opengl.ndll
      Reason: image not found)
    

    I fixed it like this: DYLD_LIBRARY_PATH=/usr/lib/haxe/lib/opengl/0,2,0/ndll/Mac:/Developer/SDKs/MacOSX10.5.sdk/usr/lib ./app

    ~

    Leave a Comment

    Older Posts »