Wednesday, September 30, 2009

du -sh *

Linux disk space usage, in human readable form. Nuf said.

Thursday, September 03, 2009

std::nth_element

Ever wanted to know what the N:th element in a sorted version of the sequence [first,last) is, without sorting the whole thing? I sure did. Unfortunately I didn't know about std::nth_element!
std::nth_element( first, nth, last );
Paraphrasing sgi.com, after this function is applied, nth is guaranteed to be the same as if the entire sequence was sorted. Meanwhile, non of the (first,nth] elements are larger than the nth element, and so logically the opposite goes for the [nth+1,last) elements. This method does less work than both std::sort and std::partial_sort, and thus is likely to be faster.

Tuesday, January 06, 2009

float2 and float3 in Maya Python API

Staying true to form, Autodesk sure has made a mess of the Maya Python API. Something I ran into recently was dealing with float2:s (for MFnMesh::getUVAtPoint()). Turns out you need to go through MScriptUtil like so:
import maya.OpenMaya as om
# this creates the float2
pArray = [0,0]
x1 = om.MScriptUtil()
x1.createFromList( pArray, 2 )
uvPoint = x1.asFloat2Ptr()
# (call to OpenMaya.MFnMesh.getUVAtPoint( ..., uvPoint, ... ) 
# goes here)
# retrieve results
uv0 = om.MScriptUtil.getFloat2ArrayItem( uvPoint, 0, 0 )
uv1 = om.MScriptUtil.getFloat2ArrayItem( uvPoint, 0, 1 )

Curiously enough the '2' in float2ptr and float2array refers to 2-dimensional arrays, hence the extra 0 in the call to the latter. We're only dealing with float2& (which typedefs to float[2]) in our getUVAtPoint call, but frankly: did you expect anything else than a hackfest when you popped that Maya 2009 box open?

Python stack trace

Outputting the stack trace can be immensely useful in certain situations.


import traceback, sys

def bar():
raise Exception('Booboo')

def foo():
bar()

try:
foo()
except:
traceback.print_exc()
print 'Got here.'


Result:

Traceback (most recent call last):
File "./foo.py", line 14, in
foo()
File "./foo.py", line 11, in foo
bar()
File "./foo.py", line 8, in bar
raise Exception('Booboo')
Exception: Booboo
Got here.

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());