Saturday, December 15, 2007

std::transform

I've heard people talking about the std::transform functions for ages, but I didn't really get around to exploring them until recently. What they bring to the table is a compact yet powerful way of performing a batch operations on stl containers, much like matlab's matrix math routines but with way more potential. You can perform common vector arithmetic such as addition, subtraction etc. but also select certain components of a container, such as the routine below that in one single line prints all the hash values of a std::map. Furthermore, by writing your code in std::transform notation you're very likely unleashing a higher degree of optimization potential in your compilers, both current and future ones. For example, the Intel compiler (I'm running 9.1 atm) immediately starts spewing out "LOOP WAS AUTO-PARALLELIZED" all over the place, which generally hasn't been too common a sight for me in the past.

Now for a couple examples, copied straight from http://www.sgi.com/tech/stl/ :

Print all of a map's keys.
int main()
{
map M;
M[1] = 0.3;
M[47] = 0.8;
M[33] = 0.1;

transform(M.begin(), M.end(), ostream_iterator(cout, " "),
select1st<map::value_type>());
// The output is 1 33 47.
}
Each element in V3 will be the difference of the corresponding elements in V1 and V2
const int N = 1000;
vector V1(N);
vector V2(N);
vector V3(N);

iota(V1.begin(), V1.end(), 1);
fill(V2.begin(), V2.end(), 75);

assert(V2.size() >= V1.size() && V3.size() >= V1.size());
transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),
minus());

Friday, December 14, 2007

The stuff of legend

So I haven't posted anything in the longest of times but today I just have to break this unintentional silence; my co-worker Nafees (bin Zafar) just walked into our cubicle with the biggest smile on his face, eyes gleaming. In his hand was a letter from the Academy of Motion Picture Arts and Sciences. It turns out he, Doug Roble and Ryo Sakaguchi have scored a Scientific & Technical Achievement Award for our internal fluid simulator FSIM! For those of you who don't know, it was one of the first fluid simulators ever to be used in a feature film - check out the flooding of Bruinen in "The Lord of the Rings: The Fellowship of the Ring" (it's the one where the ringwraiths get their asses wooped out in the river). While FSIM has seen substantial improvements over the years it remains based on their original work, and this award serves to emphasize how groundbreaking that was. So although many of these guys seldom or never turn to alcohol for comfort or joy, I just might have to get hammered tonight in their honor. Celebration!

Tuesday, September 04, 2007

readelf

Yet again abusing my blog to store random software development notes,
readelf is a linux command that dumps information about ELF-format object files (e.g. .so libraries). Just pipe it into a temp file using e.g.

readelf ~/maya/2008.cg/plugins/HairDynamics.so > out

and search for the unfiltered symbol you want to examine using your favorite text editor/grepper. This will produce a line similar to:

r19: 00000000 0 NOTYPE GLOBAL DEFAULT UND _ZN2My5Library10FileWriter4

which in this case means the particular My::Library::FileWriter is UNDefined. Highly useful when messing around with link/runtime shared object errors.

Sunday, August 19, 2007

Quote of the year

"Too often, we lose sight of life's simple pleasures. Remember, when someone annoys you it takes 42 muscles in your face to frown BUT, it only takes 4 muscles to extend your arm and bitch-slap the motherfucker upside the head." - picked up from a Walter Mosley novel

Thursday, August 09, 2007

The SIGGRAPH buzzword of the year...

... has to be "rotation invariant" or some other term pertaining to minimal-distortion mesh editing. Over the last couple of days I have seen more bunnies and armadillos being bent, stretched and twisted than during my entire life. Makes perfect sense, what with all the high-resolution shape acquisition gear that's getting increasingly available.

Some highly cool stuff includes the real-time shape and texture capturing system (just insert whatever into the green cube and voila: it shows up on the display and interacts with the environment), as well as some new promising hybrid display technology (imagine readily calibratable LCDs...).

The most frustrating bit has to be the "300" team that refused to reveal the secret behind their huge fluid sim grids. They just showed some impressive results and proceeded to refuse answering questions about how they got around the serious memory issues involved in cramming that size system through a CG solver. Yeah, parallel this and parallel that; the important part is still heavily obfuscated, totally against the SIGGRAPH purpose (and spirit). Boo.

Monday, August 06, 2007

Touchdown

So finally I'm back on US soil again, after 3 months of bumming around in my parent's house back home in Sweden. Since even the Americans seem aware of the crappy summer Sweden's seen so far I couldn't be more pleased about my current situation: staying in my future apartment (which I'll be sharing with another Swedish employee at DD, Mårten) just a block from the Venice Beach boardwalk, and looking forward to a week in sunny San Diego. What follows thereafter is less enticing, since I'll have to return to Sweden to get my O-2 visa, but that couldn't be further from my mind at the moment as I'm about to embark on some nightly activities after a day of soaking in the sun.

Monday, June 11, 2007

SIGGRAPH 2007 poster

Success! I just got word that my SIGGRAPH 2007 sketch submission was approved, albeit as a poster. Come say hi at sap_0519: "Robust Fitting of Super-Helices to Parametric Curves".

Friday, May 18, 2007

Livin on the edge

I just wrote my master's thesis in two weeks. If it gets accepted I'm off to LA for the most awesome job ever and the beach within walking distance. If it gets rejected I'm stranded in Sweden with a mediocre salary and 1.5 months of summer a year. Ahh, life on the edge, who can resist its tainted charm!

Sunday, May 13, 2007

Sthlm turf war @ the Sartorialist

Turns out the Sartorialist really digs Stockholm. Quite typically, its inhabitants got so enthused by all the attention that a regular word war broke out between the natural enemies Östermalm and Södermalm. Hilarious.

Tuesday, May 01, 2007

Duff's Device

This is a neat little trick for copying data containers that I don't see mentioned nearly as often in modern-day forums as in old Usenet posts. Whether this is because most STL implementations now include some similar contraption or because it's been superseded by fancier things is unclear, but I thought I'd publish it here in any case.

Quoting Wikipedia, "Duff's Device is an optimized implementation of a serial copy that uses a technique widely applied in assembly language for loop unwinding". Basically, a standard for-loop copy, e.g.
do
*to++ = *from++;
while(--count>0);
can be unrolled to minimize the amount of branches by using a while loop nested into a switch-statement:
switch(count%8){
case 0: do{ *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
}while(--n>0);
}
Many sources, including Wikipedia, claim that Duff's Device is a waste of optimization time and that sticking to library routines is always better. Others claim it has its uses though, albeit not as a general replacement to memcpy or std::copy. To quote Tom Duff himself, "If your code is too slow, you must make it faster. If no better algorithm is available, you must trim cycles."

Tuesday, April 24, 2007

Even more hair!

I decided to post yet another hair clip, this one showing off some more long hair action. Obviously someone will have to fiddle around with the dynamics parameters for a couple of weeks before this goes into production, but the basics are there...






Monday, April 23, 2007

Thursday, April 19, 2007

New hair dynamics clip

For all those interested in my work (I mean you mom!), I put another short clip of hair simulation on my website. Since it's powered by YouTube I might as well post it here too. Enjoy!








(Model courtesy of Digital Domain)

Monday, March 26, 2007

Windows XP screen yumminess


Only about half a decade too late, Windows XP finally has some tools to make it look more... like a Mac. With new, near-functional, Yahoo! Widgets and brilliant RocketDock from PunkSoftware, my laptop PC never looked better (except for when running Compiz/XGL on Ubuntu which for other reasons just won't fly on my HP quite yet). Both of the apps mentioned deliver lots in terms of both slick looks and long-sought-for functionality. Widgets are similar to the Apple Dashboard widgets, the Vista Sidebar gadgets et al. and are very lightweight and fast. Only problem is they still suffer from some annoying technical issues, which from all I can tell rather stem from poor testing than complicated algorithms, and as such hopefully will be addressed shortly. Awaiting bugfixes to the Yahoo! Widgets engine and various plugins (the latter provided by a seemingly sprawling user community), I can still enjoy the tremendous RocketDock which far outdoes all competition on the vast 3rd party dock market. It has almost everything a secret OSX fan could ask for in a dock; the super-smooth zoom, the alpha-blended PNG icons, the animated minimize-to-thumbnail, the jumping, even the tiny running app indicators. Time to hide that task bar once and for all?

Friday, March 23, 2007

Hair preview!

Today I finally got time to put a vid on my website showing what I'm doing with my life right now: hair. It's a really short sequence demonstrating a handful of guide hairs being subject some high-intensity motion. I guess I'll post it here as well, but make sure to keep an eye on the website for future sneak peeks! Notice the inertia, stiffness and tolerance to large derivatives. Those wondering about the sparsity of hair strands should know that the Renderman/MentalRay shaders will interpolate these "guide hairs" and create 1000s of hairs between them once the scene passes through the rendering pipeline. By increasing the density of guide hairs in areas that somehow will interact with the environment (e.g. having fingers drawn through them) one can better capture the detail in those areas, while saving processing time by populating less dynamic areas much more sparsely. Typically a full head of lead character hair has between a couple of hundred and a couple of thousand guide hairs.






Wednesday, March 21, 2007

R.I.P. John W. Backus

John W. Backus, the man behind Fortran, has passed away at an age of 82. It's hard to assess what an impact Backus, mainly through the conception of Fortran, has had on the development of computer science in general and scientific computing in particular, but he definitely ranks among the big ones. Read the NY Times article.

Thursday, March 15, 2007

Computer art that actually looks good

Working among computer artists every day, watching them craft amazing worlds using only their imagination and a computer, I might not be as easily amused by computer art as some. In fact most artsy stuff that even comes close to a computer seems to loose a lot of its organic, unpredictable feel. Jared Tarbell however really astonishes and inspires me with his procedurally generated pieces - and they're all code. Deliciously open source code at that. So check out http://www.complexification.net today.

Wednesday, March 14, 2007

Typecasting without type conversion

All the C-buffs out there probably already know about this but in order to just cast a type to another one without the implicit type conversion, some pointer games need to be played. E.g. if you want to cast a float into a type that's more suited for bitwise operations, say an unsigned integer, you don't want the whole type conversion thing to round off the float and set the integer to, well, the integer representation of that float. Instead, you want to perform a bitwise exact copy, something that

float f;
unsigned i = (unsigned) f;
i = unsigned(f);
i = const_cast<unsigned>(f);

all fail to do. Instead, use

unsigned i = *(unsigned *) &f;

Easy as that, with no need for memcpy:s or other unsafe operations. To get the correct result though, make sure that whatever datatypes are involved are of the same size, e.g. float and ints on any 32-bit system.

Thursday, February 22, 2007

Virtual and private inheritance

Wow, cool. So I just browsed through the excellent "Scientific and Engineering C++" by Barton & Nackman (ISBN 0-201-53393-6), and found this little nugget on inheritance:

If you want your class to inherit both from an interface and from an implementation of that interface (such as IClass and ClassImpl), do virtual inheritance from the interface and private inheritance from the implementation, like:

class MyClass :
public virtual IClass,
private ClassImpl {
};


As expressed by the authors of this masterful piece of litterature, this establishes an "is-usable-as" relation to the interface and a secret "is-implemented-in-terms-of" relation to the implementation. C++ will thus allow you to treat pointers to your class as IClass pointers and at the same time automatically route method calls to the ClassImpl. In other words OOP at its best!

Too bad virtual function calls impose such bad overhead that they're generally abandoned to the favor of template classes nowadays...