0

I'm building a Sphere from scratch in Maya, rather than creating faces using the spheres vertex list I need to make a plane and rotate it so it would match a regular spheres face.

enter image description here

My idea was to get the center angle between the Sphere's face vertices's horizontally and vertically. This works for the Y axis, but as soon as I apply an X rotation the orientation of the face is lost.

In the image I've deliberatively rotated one of the Sphere faces on the X axis to illustrate what kind of rotation I need to calculate. The implementation is written in Python so I've got access to all the vector methods if needed. Please note this sphere implementation is for another purpose so the setup may seem a little odd!

import pymel.core as pm
import pymel.core.datatypes as dt
import pymel.util as util

degrees = util.arrays.degrees
cos     = util.arrays.cos
sin     = util.arrays.sin
atan2   = util.math.atan2
acos    = util.math.acos
sqrt    = util.math.sqrt
PI      = util.arrays.pi
TWO_PI  = PI * 2

def distance(x1, y1, z1, x2, y2, z2):
    return sqrt( (x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2 )


# Sphere class
class Sphere():

# Initialise radius (float), subdivisionsAxis (int), subdivisionsHeight (int) 
def __init__(self, radius = 10, subdivisionsAxis = 8, subdivisionsHeight = 8):

    # Loop through each subdivision on y axis
    for i in range(subdivisionsHeight):

        if i == 0 or i == subdivisionsHeight - 1:

            # Store the triangle vertices's in this list
            data = self.generateSphereData(radius, subdivisionsAxis, subdivisionsHeight, i, 'triangle')

            length = len(data) / 11
            for j in range(length):
                index = j * 11
                x1 = data[index]
                y1 = data[index + 1]
                z1 = data[index + 2]
                x2 = data[index + 3]
                y2 = data[index + 4]
                z2 = data[index + 5]
                x3 = data[index + 6]
                y3 = data[index + 7]
                z3 = data[index + 8]
                # Angle y
                ay = data[index + 9]
                # Angle z
                az = data[index + 10]

                v1 = dt.FloatVector(x1, y1, z1)
                v2 = dt.FloatVector(x2, y2, z2)
                v3 = dt.FloatVector(x3, y3, z3)

                # Ignore the top and bottom triangles for now...
                # pm.polyCreateFacet( p = [ v1, v2, v3 ] )

            else:
                # Store the quads vertices's in this list
                data = self.generateSphereData(radius, subdivisionsAxis, subdivisionsHeight, i, 'quad')

                length = len(data) / 14

                for j in range(length):
                    index = j * 14
                    x1 = data[index]
                    y1 = data[index + 1]
                    z1 = data[index + 2]
                    x2 = data[index + 3]
                    y2 = data[index + 4]
                    z2 = data[index + 5]
                    x3 = data[index + 6]
                    y3 = data[index + 7]
                    z3 = data[index + 8]
                    x4 = data[index + 9]
                    y4 = data[index + 10]
                    z4 = data[index + 11]
                    # Angle y
                    ay = data[index + 12]
                    # Angle z
                    az = data[index + 13]

                    v1 = dt.FloatVector(x1, y1, z1)
                    v2 = dt.FloatVector(x2, y2, z2)
                    v3 = dt.FloatVector(x3, y3, z3)
                    v4 = dt.FloatVector(x4, y4, z4)

                    # Calculate centroid
                    cx = (x1 + x2 + x3 + x4) / 4
                    cy = (y1 + y2 + y3 + y4) / 4
                    cz = (z1 + z2 + z3 + z4) / 4

                    # Calculate the width and height

                    # Calculate dimensions for facet
                    tw = distance(x1, y1, z1, x4, y4, z4)
                    bw = distance(x2, y2, z2, x3, y3, z3)
                    w  = tw if bw < tw else bw
                    h  = distance(x2, y2, z2, x1, y1, z1)

                    # Calculate rotation of face
                    centroid = dt.FloatVector(cx, cy, cz)

                    mesh = pm.polyPlane(width=1, height=1, subdivisionsX=1, subdivisionsY=1, axis=(1, 0, 0))
                    mesh[0].setTranslation(centroid)
                    mesh[0].setRotation([0, degrees(-ay), 0])

                    pm.spaceLocator(p=v1)
                    pm.spaceLocator(p=v2)
                    pm.spaceLocator(p=v3)
                    pm.spaceLocator(p=v4)

                    # pm.polyCreateFacet( p = [ v1, v2, v3, v4 ] )


# Generate a vertex list of the spheres current subdivision height level
# Arguments: radius (float), subdivisionsAxis (int), subdivisionsHeight (int), index (int), polygonType (string) 

def generateSphereData(self, radius, subdivisionsAxis, subdivisionsHeight, index, polygonType):
    positions = []

    if polygonType == 'triangle':
        for i in range(subdivisionsAxis):

            # If were generating the top triangles we need the triangle base to 
            # Be at the previous subdivision level, so change the index to index - 1
            if index < subdivisionsHeight: 
                nextIndex = index + 1
            else:                
                nextIndex = index - 1

            if i < subdivisionsAxis - 1:
                j = i + 1
            else:
                j = 0

            # Top vertex
            r1 = radius  * sin(index * (PI / subdivisionsAxis))
            x1 = r1      * cos(i * (TWO_PI / subdivisionsAxis))
            y1 = radius  * cos(index * (PI / subdivisionsHeight))
            z1 = r1      * sin(i * (TWO_PI / subdivisionsAxis))

            # Left vertex
            r2 = radius  * sin(nextIndex * (PI / subdivisionsAxis))
            x2 = r2      * cos(i * (TWO_PI / subdivisionsAxis))
            y2 = radius  * cos(nextIndex * (PI / subdivisionsHeight))
            z2 = r2      * sin(i * (TWO_PI / subdivisionsAxis))

            # Right vertex
            x3 = r2      * cos(j * (TWO_PI / subdivisionsAxis))
            y3 = radius  * cos(nextIndex * (PI / subdivisionsHeight))
            z3 = r2      * sin(j * (TWO_PI / subdivisionsAxis))

            # Calculate angles
            ay = 0
            az = 0

            positions += [x1, y1, z1, x2, y2, z2, x3, y3, z3, ay, az]

    elif polygonType == 'quad':

        nextIndex = index + 1

        for i in range(subdivisionsAxis):

            if i < subdivisionsAxis - 1:
                j = i + 1
            else:
                j = 0

            # Bottom y
            r1 = radius * sin(index * (PI / subdivisionsAxis))
            y1 = radius * cos(index * (PI / subdivisionsHeight))

            # Top y
            r2 = radius * sin(nextIndex * (PI / subdivisionsAxis))
            y2 = radius * cos(nextIndex * (PI / subdivisionsHeight))

            # Top left vertex
            x1 = r2     * cos(i * (TWO_PI / subdivisionsAxis))
            z1 = r2     * sin(i * (TWO_PI / subdivisionsAxis))

            # Bottom left vertex
            x2 = r1     * cos(i * (TWO_PI / subdivisionsAxis))
            z2 = r1     * sin(i * (TWO_PI / subdivisionsAxis))

            # Bottom right vertex
            x3 = r1     * cos(j * (TWO_PI / subdivisionsAxis))
            z3 = r1     * sin(j * (TWO_PI / subdivisionsAxis))

            # Top right vertex
            x4 = r2     * cos(j * (TWO_PI / subdivisionsAxis))
            z4 = r2     * sin(j * (TWO_PI / subdivisionsAxis))

            # Calculate angles
            ay1 = i * (TWO_PI / subdivisionsAxis)
            ay2 = j * (TWO_PI / subdivisionsAxis)
            ay  = ay1 + ((ay2 - ay1) / 2)

            az1 = index     * (PI / subdivisionsHeight)
            az2 = nextIndex * (PI / subdivisionsHeight)
            az  = az1 + ((az2 - az1) / 2)

            positions += [x1, y2, z1, x2, y1, z2, x3, y1, z3, x4, y2, z4, ay, az]

    return positions

Sphere(20, 8, 8)
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
David
  • 33
  • 4
  • I suspect the answer is mathematically simple, but I can't make head nor tail of the question. – Beta Jan 17 '13 at 00:11
  • I need to rotate a plane to the orientation of a corresponding spheres face. – David Jan 17 '13 at 00:13
  • All right, how do you represent the plane and the face? And is there a particular point around which you'd like to rotate the plane? – Beta Jan 17 '13 at 00:26
  • I have a vertex list for all the quads and triangles. I've calculated the center of the face to place the plane, I just need to rotate it so it mimics the corresponding face. – David Jan 17 '13 at 00:33
  • Find the normals of the plane and face (by taking the cross-product of two adjacent edges and normalizing). Check the normals for being nearly antiparallel with the dot product; if they aren't then take their cross-product to get rotation axis and angle, if they are then do a 180 degree rotation first, then take the cross-product. Is that clear enough? Should I expand it into an Answer? – Beta Jan 17 '13 at 00:56
  • Please could you possibly write your reply above in pseudo code so I could try to implement. – David Jan 17 '13 at 01:00

2 Answers2

1

All right, pseudocode. How about this:

planeNormal = cross(plane.firstEdge, plane.secondEdge)
faceNormal = cross(face.firstEdge, face.secondEdge)

normalize(planeNormal)
normalize(faceNormal)

if dot(planeNormal, faceNormal)<0    # if they're more than 90 degrees apart
  rotate(plane, plane.firstEdge, pi) # rotate the plane 180 degrees
  planeNormal = -planeNormal

axis = cross(planeNormal, faceNormal)
angle = arccos(magnitude(axis))
normalize(axis)
rotate(plane, axis, angle)
Beta
  • 96,650
  • 16
  • 149
  • 150
  • I don't have a face to calculate the normal from. The plane I'm creating is the face for the sphere, so any calculations will need to be made from the position vector I have for each face in the sphere. – David Jan 17 '13 at 16:44
  • @David: You have a position vector for the face, and (I presume) for the center of the sphere. Calculating the normal of the face from those shouldn't be too challenging. – Beta Jan 17 '13 at 17:06
0

If I read your question correctly you want to apply a rotation to a bunch of quads and triangles so that they make a face of a sphere. I believe what you really mean to ask is how do you rotate a collection of quads and tris so that a vector normal to the center of the shape intersects a desired point. That desired point is the center of your sphere.

Lets break this up! Here is the pseudo code for what you want to do:

for i,shape in enumerate(listOfShapes):
    [nc1,nc2] = normal_of_center(shape)
    R = rotationFrom2Vecs([nc1,nc2],[s,nc2])
    listOfShapes[i] = shape*R

For each shape (a shape is just a list of 3 or 4 points) you calculate a vector that is normal to the shape and centered at the center of that shape. This vector is just two points: [nc1,nc2] (hint nc2: is just the average coordinates of the shape in question, nc1 is any point that lays on the normal of the plane defined by the shape).

The next thing we want to do is calculate the rotation matrix, R, between this vector and the vector that connects the center of your shape (nc2) to the center of the sphere (s). But how the heck do I calculate a 3x3 rotation matrix from 2 vectors, you're probably asking yourself?

Finally you apply this rotation matrix to the shape by right multiplication, which know is the only valid way of doing it due to the rules of matrix multiplication. I'm not familiar with pymel, I don't know if it has matrix multiplication routines or not. If it doesn't you will want to use numpy arrays and matrices which are very straight forward and convenient for projects like this.

If you are confused about the overall idea, I can draw up some quick and dirty diagrams. Is this the fastest way? No. However it definitely is the clearest method.

Community
  • 1
  • 1
Adam Cadien
  • 1,137
  • 9
  • 19
  • This is exactly what im looking for. Diagrams would help out greatly if you have the time. – David Jan 17 '13 at 10:13