[Bf-python] Hashing Vectors

Joe Eagar joeedh at gmail.com
Tue Jul 18 13:03:57 CEST 2006


Campbell Barton wrote:
> Hi, There are a few scripts I have that I use a dict for vertex 
> locations, so as not to double up on locations as an example
>
> vert_dict[tuple(vertex.co)]= vertex.index
>
> It would be nicer and faster if the 2,3, or 4  floats could be hashed, 
> any hints as to hashing a vector and possibly a matrix type?
>
Well, python ints do have unlimited length. So, you could simply times 
the coordinates by a fixed amount (say, 1000) to retain a reasonable 
amount of precision, convert them to ints, then pack the coordinates 
into a giant integer (96 bits or so :D ).

Um.  that'd be:


int1 = int(x*1000.0)
int2 = int(x*1000.0)
int3 = int(x*1000.0)
hash = int1 + (int2 << 32) + (int3 << 64)

Joe



More information about the Bf-python mailing list