Jump to content
CinnamonMetal

Dot Vector in Python ?

Recommended Posts

How do I compare two floats against a float value of 1.0 using Vector3.dot() ? 

 if hou.Vector3.dot(primColor,rounded,1.0):
            print("match")
        else:
            print("no match")

 

Share this post


Link to post
Share on other sites
#Like this
vector_one = hou.Vector3((1,0,0))
vector_two = hou.Vector3((1,0,0))

if vector_one.dot(vector_two) == 1:
	print 'match'


#Or like this
if hou.Vector3((1,0,0)).dot(hou.Vector3((1,0,0))) == 1:
	print 'match'

 

-b

Share this post


Link to post
Share on other sites
if not hou.Vector3(primColor).dot and hou.Vector3(rounded).dot == 1:
            print("match")
        else:
            print("no match")

The result is always no match; when it should be match

Share this post


Link to post
Share on other sites

Correct me if I'm wrong, hou.Vector3 takes a tuple; I attempted using it without a tuple and it worked ?

I'm referencing your alternative code example. I don't want to use integer or float values for the vector I want to use a variable containing a tuple; unless you were already aware of that based on my original question ;)  

I'm looping over each primitive; with that in mind, my goal is to take a vector3 tuple compare that with 1.0 whether the truncated value is equal to that of the full float value based on color, if based on color is anything of importance to begin with ? ;)

Share this post


Link to post
Share on other sites

Im not entirely sure what you actually want to do but if you want to use the dot product method of the hou.Vector3 class, you are not doing it the right way.

In your code you are calling: 

hou.Vector3(primColor).dot

This makes python return the method it self, not the value the function is supposed to return. If you print this you will get something that starts with "bound method..."

 

To make this work you also need to supply the dot method with a vector:

hou.Vector3(primColor).dot(rounded)

The first statement will always return True whereas the second will return any vaule between -1 and 1. Depending on the directions of the two vectors (primColor and rounded) you pass in.

Cheers
Bonsak

Edited by bonsak

Share this post


Link to post
Share on other sites

I have three sets of floats or a tuple.  I'm going to take into consideration you're right; by right I mean I'm comparing a truncated tuple (Vector3) with the full value tuple (Vector3) otherwise made up of three float values (float,float,float) you get the idea ;) 

Anyhow if they match, then the result should print "match" that isn't the case looping over primitives; rather I'm getting "no match" therefore, my problem may not be the Vector3 it may be the truncated tuple :unsure:

 

Share this post


Link to post
Share on other sites

I think the problem is that you are not using the dot method the correct way.
You are using
"hou.Vector3(primColor).dot"

but you need to use
"hou.Vector3(primColor).dot(rounded)"

hou.Vector3(primColor).dot
will never return the dot product. It will return the method it self as i wrote earlier.

-b

Share this post


Link to post
Share on other sites
hou.Vector3(primColor).dot(hou.Vector3((rounded))) == 1

I'm using it correctly but comparing 1 / 1.0.  If my math is correct, then the full float values is multiplied by 1.0 and so is the truncated float value multiplied by 1.0; which would produce the same value which went in.

 

Share this post


Link to post
Share on other sites

What are the actual values of the "primColor" and "rounded" variables?

-b

Edited by bonsak

Share this post


Link to post
Share on other sites

As a rule of thumb you never compare two different float values using ==.  Floating point arithmetic is not numerically precise, every operation performed on a float results in a "Floating-Point Error" (tiny imprecisions due to the nature of decimal representations on modern hardware).  

If you want to know if your color and your rounded values are normalized and parallel(which seems to be the question your asking?) then the most straightforward way would be,

dot = hou.Vector3(primColor).dot(hou.Vector3(rounded))
tolerance = .000001
if dot >= 1.0-tolerance and dot <= 1.0+tolerance :
  print "match"

.  Houdini's Vectors types also have almostEquals functions which do similar tests for each element in the array.

  • Confused 1

Share this post


Link to post
Share on other sites

The condition using 'and' verifies whether the floating point value is down to an extremely small value to compare.

Although I'm getting an error regarding the tolerance variable ?

tolerance = 0.000001

 

Share this post


Link to post
Share on other sites

I'm not sure what exactly your asking.

tolerance = 0.000001

This works in my interpreter, I get 1e-06.

 

What

if dot >= 1.0-tolerance and dot <= 1.0+tolerance :

does is check if dot is within 2*tolerance of 1.0.  It's the same as saying abs(dot-1.0) <= 2*tolerance.

 

So if tolerance was .001 on a number line it would look like,(not to scale)

        .9990   .9999    1.0     1.0010     1.0011

--------|-------x--------|--------|---------x1----

so x, being .9999, would be true as its greater then .9990 and less then 1.0010, but x1 would be false as its 1.0011 which is greater then 1.0010.

Share this post


Link to post
Share on other sites

What are the actual values of primColor and rounded when you get the floating point error? Or are you getting an error just by declaring

tolerance = 0.000001

 

Share this post


Link to post
Share on other sites

The primColor and rounding happen per primitive and works successfully except when I declare the tolerance variable along with a condition then it breaks.  From what @MrScienceOfficer supplied as code should tell me per primitive whether the full float and truncated float values match.

 

Share this post


Link to post
Share on other sites

I'm getting an error within the function.  Does there appear anything wrong here ?

tolerance = 0.000001
        if dot >= 1.0-tolerance and dot <=1.0+tolerance:
            print("match")
        else:
            print("no match")

Otherwise the error is; AttributeError: 'module' object has no attribute 'theLooper'; being the name of the function ?

Edited by CinnamonMetal

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×