Re: [Algorithms] How to get 3dvector largest coordinate index?
Brought to you by:
vexxed72
From: Marc H. <ma...@gm...> - 2009-03-01 22:53:36
|
Oops, I did forget the fabs, I wrote the sketch in email, then wrote a version for testing on my PC. As for the 3, I explained that in the message. There's 8 possible values for 3 bits, but only 6 are possible configurations of the comparisons. On Sun, Mar 1, 2009 at 1:42 PM, Matt J <mjo...@gm...> wrote: > > Your lookup table varies from 0..3, e.g. 0, 1, 2, 3, but there are only 3 > components total, not 4, so I'm not sure how you calculated it > > Also, you need to look at the absolute value, which you need to determinate > the largest component in magnitude > >> >> This problem felt a lot like you could somehow just do math on the >> conditionals and get the answer. The second best I could come up with >> is: >> >> (Course, conditional moves makes much of this moot, but on with the show) >> >> //I used 3 for values that can never happen for fun, always cool to go >> 1 past the end of an array >> //Replace with 0, 1, or 2 in shipping code, assert in debug >> static uint s_lookup[] = { >> /*000*/ 2, >> /*001*/ 2, >> /*010*/ 3, >> /*011*/ 0, >> /*100*/ 1, >> /*101*/ 3, >> /*110*/ 1, >> /*111*/ 0 >> } >> >> inline uint LargestCoordinate(const Vector3d &v) >> { >> uint first_bit = v.x > v.y; >> uint second_bit = (v.x > v.z) << 1; >> uint third_bit = (v.y > v.z) << 2; >> >> uint index = first_bit | second_bit | third_bit; >> >> return lookup[index]; >> } >> >> Then, I thought, I could just load a single uint, shift right by the >> index, then and off the low 2 bits: >> //Replace binary syntax with something to get around C's lack of >> syntax. (like http://c-faq.com/misc/sd28.html ) >> inline uint LargestCoordinate(const Vector3d &v) >> { >> uint val = 0b0001110100111010; >> >> uint rotate = (v.y > v.z) << 3 | (v.x > v.z) << 2 | (v.x > v.y) << 1; >> >> return ( val >> rotate ) & 0b11; >> } >> >> I found I needed to use the int trick also, otherwise the (float > >> float) statement generates a jump, blah. >> >> >> (Hire me!) >> >> >> -- >> m h >> arc ernandez >> / these are the days of lethal perfection / > > ------------------------------------------------------------------------------ > Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA > -OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise > -Strategies to boost innovation and cut costs with open source participation > -Receive a $600 discount off the registration fee with the source code: SFAD > http://p.sf.net/sfu/XcvMzF8H > _______________________________________________ > GDAlgorithms-list mailing list > GDA...@li... > https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list > -- m h arc ernandez / these are the days of lethal perfection / |