[Bf-python] Hashing Vectors
Campbell Barton
cbarton at metavr.com
Wed Jul 19 03:20:02 CEST 2006
Martin Poirier wrote:
> --- Campbell Barton <cbarton at metavr.com> wrote:
>
>
>> If hashing a vector at its current state is evil,
>> could there be a way to...
>> a) Speed up conversion to a tuple? - not sure how
>> this might be done,
>> have a vec.tuple() function to avoid python
>> iterating over 2,3,4 axis
>> for every conversion.
>>
>
> Did you try doing it manually? You'll always know in
> advance the number of dimensions you're dealing with,
> so just do:
>
> (v[0], v[1], v[2]) instead of tupple(v)
>
>
>
>> b) Have a method that returns a non tuple hashable
>> hashable type -
>> somthing like vec.key() which could return a long
>> int or whatever ends
>> up being best to hash. - document that this is
>> location dependant and
>> will change of the location changes.
>>
>
> It would have to be tupple based, that is pretty much
> the cleanest way to make an immutable vector, short of
> packing everything non-destructively in an int.
>
>
>> c) Have a new iterator datatype for vector arrays,
>> with functions like
>> adding a unique vector, and other useful operations-
>> such an array would
>> be useful for Blender.Geometry
>>
>
>
> A Vector Set.
>
> Thinking about this rang a bell though. Vector's are
> floating point data, so any test done on equality is
> potentially wrong due to float errors.
>
> Martin
>
>
For vector arrays (sets would be unordered) and append function could
have an optional error margin.
vec_array.append(vec, 0.0001)
The way Im using them at the moment dosent create float errors but your
right.
Benchmarked conversion without hashing, here are the results
0.623914957047 # tuple()
0.396674871445 # (x,y,z)
0.44420599937 # ([0], [1], [2])
0.59113907814 # tuple()
0.375689983368 # (x,y,z)
0.433271884918 # ([0], [1], [2])
0.509349107742 # (x,y,z) - from a function
--
from Blender import Mesh, sys
me= Mesh.Get('Cube')
verts= me.verts
t= sys.time()
for v in me.verts:
co= v.co
i= tuple(co)
print sys.time()-t
t= sys.time()
for v in me.verts:
co= v.co
i= (co.x, co.y, co.z)
print sys.time()-t
t= sys.time()
for v in me.verts:
co= v.co
i= (co[0], co[1], co[2])
print sys.time()-t
def vec_tuple3d(v):
return v.x, v.y, v.z
t= sys.time()
for v in me.verts:
co= v.co
i= vec_tuple3d(co)
print sys.time()-t
--
--
Campbell J Barton
133 Hope Street
Geelong West, Victoria 3218 Australia
URL: http://www.metavr.com
e-mail: cbarton at metavr.com
phone: AU (03) 5229 0241
More information about the Bf-python
mailing list