Vector, matrix, normalize, rotate. What package?

Discussion in 'Python' started by =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=, Feb 27, 2007.

  1. Hello!

    I'm trying to find what package I should use if I want to:

    1. Create 3d vectors.
    2. Normalize those vectors.
    3. Create a 3x3 rotation matrix from a unit 3-d vector and an angle in
    radians.
    4. Perform matrix multiplication.

    It seems to me that perhaps numpy should be able to help me with this.
    However, I can only figure out how to do 1 and 4 using numpy. Meybe
    someone knows a way to use numpy for 2 and 3? If not, what Python
    package helps me with geometry related tasks such as 2 and 3?

    Any help here would be greatly appreciated!

    Regards,
    Mattias
     
    =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=, Feb 27, 2007
    #1
    1. Advertising

  2. =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=

    shredwheat Guest

    On Feb 27, 2:49 pm, "Mattias Brändström" <> wrote:
    > I'm trying to find what package I should use if I want to:
    >
    > 1. Create 3d vectors.
    > 2. Normalize those vectors.
    > 3. Create a 3x3 rotation matrix from a unit 3-d vector and an angle in
    > radians.
    > 4. Perform matrix multiplication.


    You should have good luck with cgkit. If you are having trouble
    getting a compile of v2, there is an older v1 that is pure python.

    There are various implementations all around the net, but I'm not sure
    of anything standalone and actually released.
     
    shredwheat, Feb 27, 2007
    #2
    1. Advertising

  3. =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=

    Paul Rubin Guest

    "Mattias Brändström" <> writes:
    > I'm trying to find what package I should use if I want to:
    > 1. Create 3d vectors.
    > 2. Normalize those vectors.
    > 3. Create a 3x3 rotation matrix from a unit 3-d vector and an angle in
    > radians.
    > 4. Perform matrix multiplication.


    If this is a math exercise, just use plain python and code it all by
    hand, there's not much to it. You might also like to read about
    quaternion multiplication--if you read German, the German Wikipedia
    article looks more helpful than the English one about that.

    http://de.wikipedia.org/wiki/Quaternion
     
    Paul Rubin, Feb 27, 2007
    #3
  4. =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=

    James Stroud Guest

    Mattias Brändström wrote:
    > Hello!
    >
    > I'm trying to find what package I should use if I want to:
    >
    > 1. Create 3d vectors.
    > 2. Normalize those vectors.
    > 3. Create a 3x3 rotation matrix from a unit 3-d vector and an angle in
    > radians.
    > 4. Perform matrix multiplication.
    >
    > It seems to me that perhaps numpy should be able to help me with this.
    > However, I can only figure out how to do 1 and 4 using numpy. Meybe
    > someone knows a way to use numpy for 2 and 3? If not, what Python
    > package helps me with geometry related tasks such as 2 and 3?
    >
    > Any help here would be greatly appreciated!
    >
    > Regards,
    > Mattias
    >


    As Paul is hinting, your best bet is to make use of quaternions, you
    will save yourself a lot of frustration as soon as you need to do
    anything with them outside of matrix-multiplying a bunch of 3D
    coordinates. See the Scientific Python module:
    Scientific.Geometry.Quaternion. To make a matrix from Quaternion, q, use
    "q.asRotations().tensor".

    To make a quaternion from an axis and an angle, here is what I use:

    #######################################################################
    # axis_angle_to_quaternion()
    #######################################################################
    def axis_angle_to_quaternion(axis, angle):
    """
    Takes an I{axis} (3x1 array) and an I{angle} (in degrees) and
    returns the rotation as a
    I{Scientific.Geometry.Quaternion.Quaternion}.

    @param axis: 3x1 array specifiying an axis
    @type axis: numarray.array
    @param angle: C{float} specifying the rotation around I{axis}
    @type angle: float
    @return: a I{Quaternion} from an I{axis} and an I{angle}
    @rtype: Quaternion
    """
    axis = normalize(axis)

    angle = math.radians(float(angle))
    qx = float(axis[0])
    qy = float(axis[1])
    qz = float(axis[2])
    sin_a = math.sin(angle / 2.0)
    cos_a = math.cos(angle / 2.0)
    qx = qx * sin_a
    qy = qy * sin_a
    qz = qz * sin_a
    qw = cos_a

    return Quaternion(qw, qx, qy, qz).normalized()


    See your linear algebra text on how to normalize a 1x3 vector.

    James
     
    James Stroud, Feb 28, 2007
    #4
  5. =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=

    greg Guest

    Mattias Brändström wrote:

    > 1. Create 3d vectors.
    > 2. Normalize those vectors.
    > 3. Create a 3x3 rotation matrix from a unit 3-d vector and an angle in
    > radians.
    > 4. Perform matrix multiplication.
    >
    > Meybe someone knows a way to use numpy for 2 and 3?


    Here's some code I wrote recently to do normalisation
    of vectors using Numeric:

    from Numeric import add, sqrt

    def dots(u, v):
    """Return array of dot products of arrays of vectors."""
    return add.reduce(u * v, -1)

    def units(v):
    """Array of unit vectors from array of vectors."""
    ds = 1.0 / sqrt(dots(v, v))
    return ds * v

    These work best if you give them multiple vectors to
    work on at once, otherwise you don't get much advantage
    from using Numeric.

    I don't have anything to hand for rotation about a
    vector, but if you can find a formula, you should be
    able to use similar techniques to "vectorize" it
    using Numeric.

    --
    Greg
     
    greg, Mar 1, 2007
    #5
  6. =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=

    Guest

    On Feb 27, 4:49 pm, "Mattias Brändström" <> wrote:
    > Hello!
    >
    > I'm trying to find what package I should use if I want to:
    >
    > 1. Create 3d vectors.
    > 2. Normalize those vectors.
    > 3. Create a 3x3 rotation matrix from a unit 3-d vector and an angle in
    > radians.
    > 4. Perform matrix multiplication.
    >
    > It seems to me that perhaps numpy should be able to help me with this.
    > However, I can only figure out how to do 1 and 4 using numpy. Meybe
    > someone knows a way to use numpy for 2 and 3? If not, what Python
    > package helps me with geometry related tasks such as 2 and 3?


    Try Alex Holkner's euclid.py module:

    http://cheeseshop.python.org/pypi/euclid/0.01


    Richard
     
    , Mar 1, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy
    Replies:
    2
    Views:
    465
    Howard
    Jun 3, 2004
  2. RC
    Replies:
    1
    Views:
    908
    Bjoern Hoehrmann
    Aug 3, 2006
  3. Replies:
    8
    Views:
    1,960
    Csaba
    Feb 18, 2006
  4. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    420
    Holgerson
    Oct 26, 2007
  5. IanJSparks
    Replies:
    0
    Views:
    1,181
    IanJSparks
    Jan 10, 2008
Loading...

Share This Page