Structures as inputs within Quartz Composition patches

8 03 2009

I figured I should write this down as it doesn’t seem to be documented anywhere and it took me a while to figure it out:

So, one of my side projects has been to write a fluid solver quartz patch. It contains Jos Stam’s 2D fluid solver from 2003 (GDC Paper) which is an awesome display of efficiency that I admit I only understand partially. The patch takes inputs describing external density and velocity sources and inputs them into the sample code provided with the original GDC presentation. The output of the patch is a black and white image displaying either the density or velocity view. At some point I might make it output the velocity field as a structure as well so it can be used like the optical flow example code, but that is for later.

To be honest most of the required steps were fairly easy and are documented in other example patches and on the web. The thing I couldn’t find was how to take a multi-dimensional structure as an input and parse it to create the necessary inputs for the fluid solver. The patch that was creating the input was also custom so I knew the data types being fed in but they were obviously being changed internally so I couldn’t rely on that.

In the end it was relatively easy and they were converting the provided CFArrays into NSDictionarys. Thus to access the u and v coordinates

The input is defined as:

@property(assign) NSDictionary* inputDensityArray;

And the relevant section of the execute method:

NSDictionary* uArray = (NSDictionary*)[self.inputDensityArray objectForKey: @"u"];
NSDictionary* vArray = (NSDictionary*)[self.inputDensityArray objectForKey: @"v"];
double x,y;
NSNumber* u, *v;

if(uArray && vArray)
  int count = [uArray count] < [vArray  count] ? [uArray count] : [vArray count] ;
  for(int i=0;i<count;i++)
    u = (NSNumber*)[uArray objectForKey: [NSNumber numberWithInt:i]];
    v = (NSNumber*)[vArray objectForKey: [NSNumber numberWithInt:i]];
    if(u && v)
      x = [u doubleValue];
      y = [v doubleValue];
      handleDensityInput( x,y , self.inputDensityAmount);

Please forgive my poor structure, I’m still learning the basics of Objective-C largely through trial and error.

At some point I might post the full patch but while it works, it isn’t complete…




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: