Small problem with Python/Rhinocommon Vector3f


#1

One is supposed to be able to scale vector objects by simply multiplying them by a number:

import Rhino
vec=Rhino.Geometry.Vector3d(0,0,1)
mult=2.0
scaled=vec*mult
print scaled
>>> (0,0,2)

However, with a vector3f object instead of vector3d object, there is a problem:

import Rhino
vec=Rhino.Geometry.Vector3f(0,0,1)
mult=2
scaled=vec*mult
print scaled
#this works
>>> (0,0,2)

mult=2.0
scaled=vec*mult
#this errors out
>>> Message: unsupported operand type(s) for *: 'Vector3f' and 'float'

So it is accepting integer values, but not floats…

Only reason I found this is that mesh vertex normals seem to be output as vector3f and not vector3d.

–Mitch


#2

Great catch! Though I guess it was just a matter if time until someone found it. Wonder if the root cause exists in any other functions/operands.


(Menno Deij - van Rijswijk) #3

The multiplication operator is available in RhinoCommon.

See https://github.com/mcneel/rhinocommon/blob/3faf1c3f1ad741eab7c1e26836709407082611cf/dotnet/opennurbs/opennurbs_fpoint.cs

Line 1105.

This is (probably) related to the fact that ‘2.0’ in Python is type ‘double’ [8 bytes] and not ‘float’ [4 bytes] (even though Python calls it ‘float’ it is 8 bytes and interpreted as ‘double’ in .NET).

I’m not sure why multiplication as ‘int’ does work though.

If you construct a Vector3d from the Vector3f using copy constructor and scale that, it should work.


(Giulio Piacentino) #4

Hi Mitch, Al and Menno - we could add a multiplication operator – but with a double, it should return a Vector3d to retain precision. Would that work for you? We could also just make it work in Python, and then return a Vector3f, but it would still lose the precision of the multiplicand.

Thanks,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com


#5

Hi Giulio,

I don’t really know how to answer that question… I don’t even really know the difference between a vector3d and a vector3f… All I wanted to do was scale the vectors that resulted from mesh vertex normals (which turned out to be 3f), I looked for a scale method in RhinoCommon and came across Multiply…

Since it looked like it should support the * operator, I tried… and was surprised when it failed is all.

–Mitch


(Menno Deij - van Rijswijk) #6

I have sent you a pull request on GitHub to add said operator a couple of days ago.


#7

Another good example of how much work it is to make things work intuitively and automagically in all cases!