Jump to content

point normal to rotate


ben

Recommended Posts

I'm just starting with Python and as a learning case I'm wrting a small script to export points position in an xml.

I'm using this script in a game engine to place geometry and it works fine.

Now I would like to export orientation as well.

To do that I need to convert point normal to rotation, and that's where I'm stuck...

I'm not good at math so any hint (or a few lines) would be really apreciated.

ben

Link to comment
Share on other sites

I recently had the same scenario come up which I had to convert a direction vector to degrees. I found this link to be useful as far as the math part is concerned. The "Axis Angle Result" section sums it up pretty well.

http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm

Ive also attached a hip file which does this in a vop sop.

convertDirToDegrees_001sl.hip

Link to comment
Share on other sites

  • 3 weeks later...

great! enjoy your weekend!

something I recently found to be wrong with my posted example is that I forgot to normalize the result of the cross vop. Be sure to stick a normalize VOP before the quaternion VOP or you may get results that are slightly off.

Link to comment
Share on other sites

I use the quaternion VOP and the "rot" point attribute alot for stuff like this, instead of rotating normals. You can check out my attached sample. I don't know if it is helpful, but maybe it will become useful later on. :)

Link to comment
Share on other sites

  • 1 year later...

Hey, I'm working on a project where I'm trying to do something similar.

I've got some python code below that takes a line and computes a direction vector by comparing each point position by the next point position. Currently the code is placing a plane on each point and rotating it to face the direction vector, eventually I'm going to be instancing different geometry onto each point.

The results I'm getting are very similar to the results of a sweep sop but some planes are "twisted" slightly.

Below is an image, the wireframe planes are the result of the sweep sop and the shaded planes are the result of the code.

I'm not sure if I can expect to get the same results as the sweep sop with only two vectors or if there's a problem with my math.

post-6960-132381273418_thumb.jpg

import math

geo = hou.pwd().geometry()                                                      # Input Geo
normal_attrib = geo.addAttrib(hou.attribType.Point, "N", (0.0,0.0,0.0), True)   # Create point normal attribute
len_attrib = geo.addAttrib(hou.attribType.Point, "len", (0.0))                  # Create point length attribute

# Compute a direction vector for each point by subtracting the previous point's position from the current point position
pos = hou.Vector3(0,0,0)
for i in geo.points():
    ptNormal = i.position() - pos
    i.setAttribValue(len_attrib, i.position().distanceTo(pos))
    pos = i.position()
    i.setAttribValue(normal_attrib, ptNormal)

# Fix incorrect values for for 1st point by assigning the values of the 2nd point
geo.iterPoints()[0].setAttribValue(normal_attrib, geo.iterPoints()[1].attribValue("N"))
geo.iterPoints()[0].setAttribValue(len_attrib, geo.iterPoints()[1].attribValue("len"))

upVector = hou.Vector3(0,1,0)                                               # Up Vector (+Y)
for i in geo.points():

    dirVector = hou.Vector3(i.attribValue("N")).normalized()                # Normalized direction vector from point attribute "N"
    ang = hou.hmath.radToDeg(math.acos(upVector.dot(dirVector)))            # Value in degrees of the Arc Cosine of Dot product of up and direction vectors
    rot = hou.hmath.buildRotateAboutAxis(upVector.cross(dirVector), ang)    # Return a transformation matrix containing only a rotation, given an axis and a rotation amount
    if 1 == 1:
        plane = geo.createPolygon()
        point = geo.createPoint()
        point.setPosition(hou.Vector3(5.0,0.0,5.0))
        plane.addVertex(point)
        point = geo.createPoint()
        point.setPosition(hou.Vector3(5.0,0.0,-5.0))
        plane.addVertex(point)
        point = geo.createPoint()
        point.setPosition(hou.Vector3(-5.0,0.0,-5.0))
        plane.addVertex(point)
        point = geo.createPoint()
        point.setPosition(hou.Vector3(-5.0,0.0,5.0))
        plane.addVertex(point)
        geo.transformPrims( [plane], rot * hou.hmath.buildTranslate(i.position()) ) # Transform geo (Rotation Matrix * Translation Matrix)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...