Re: [Algorithms] How to get 3dvector largest coordinate index?
Brought to you by:
vexxed72
From: Matt J <mjo...@gm...> - 2009-03-01 21:43:14
|
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 / > |