Re: [Algorithms] How to get 3dvector largest coordinate index?
Brought to you by:
vexxed72
From: Matt J <mjo...@gm...> - 2009-02-28 02:32:35
|
Yeah, okay, i get it, i got it backwards. I just said that =) Anyway, here another way that only has two conditionals, no shifts, and no max's and easier to follow. Not sure if it is faster though. The FP uses a stack either way, right? I think this will work but never tested const float f[3] = { fabsf(n.x), fabsf(n.y), fabsf(n.z) }; unsigned int i = ( f[1] > f[0] ) ? 1 : 0; return (f[2] > f[i]) ? 2 : i; ..........Finally, an all integer way: const unsigned int u[3] = { 0x7FFFFFFF & *(unsigned int *)&n.x, 0x7FFFFFFF & *(unsigned int *)&n.y, 0x7FFFFFFF & *(unsigned int *)&n.z }; unsigned int i = ( u[1] > u[0] ) ? 1 : 0; return (u[2] > u[i]) ? 2 : i; If (y > x) == 0 then that means x <= y. > nope: > If (y > x) == 0 then that means x >= y. > > 2009/2/27 Matt J <mjo...@gm...> > >> >> Hmm, I fail to see how this works: >> >> If (y > x) == 0 then that means x <= y. >> >> So, >> >> If x <= y and z < y then 0 << 0 = 0 >> If x <= y and z >= y then 0 << 1 = 0 >> >> In the first case, y is the greatest component In the second case, z is >> the greatest component. >> >> Both are encoded as 0. Something I'm missing? >> >> >> Hi, >>> >>> I need to compute the index (not the actual value) of the largest >>> coordinate of a normal, for some space hashing. >>> >>> I'm not sure how fast you guys usually find this index, but I've just >>> created the following trick which I think is quite fast: >>> >>> > inline uint LargestCoordinate(const Vector3d &v) >>> > { >>> > const float x = fabs(v.x); >>> > const float z = fabs(v.z); >>> > const float y = Maths::max( fabs(v.y), z ); >>> > return uint(fabs(y)>fabs(x)) << uint(fabs(z)>=fabs(y)); >>> > } >>> >> |