Ejemplo n.º 1
0
    def calcNormal(self):
        """
        This method calculates the physical surface normal of the face using the :py:func:`aljabr.planeNorm` function from
        the aljabr module. This results in a direction vector at right angles to the
        two edges vt2_vt1 and vt2_vt3.
        """

        if len(self.verts) > 2:
            vt1 = self.verts[0].co
            vt2 = self.verts[1].co
            vt3 = self.verts[2].co
            self.no = aljabr.planeNorm(vt1, vt2, vt3)
        else:
            self.no = [0.0, 0.0, 1.0]
Ejemplo n.º 2
0
def findOptimalTransformation(vertsM, normM, facesM, vertsB, normB):
    """
    Find and apply the transformation to align meshes
    """
    #find the average of normal vector
    noB = [0,0,1]
    
    nox = 0
    noy = 0
    noz = 0
    
    #if the normals are storaged in the obj file
    if len(normM) > 0:
        for i in normM:
            nox += i[0]
            noy += i[1]
            noz += i[2]
    #if the normals aren't storaged in the obj file
    elif len(facesM) > 0:
        for f in facesM:
            if len(f) >= 3:
                n = aljabr.planeNorm(vertsM[f[0]], vertsM[f[1]], vertsM[f[2]])
            else:
                n = [0,0,1] #edges
                
            nox += n[0]
            noy += n[1]
            noz += n[2]      
            normM.append(n)
    else:
        nox = 0
        noy = 0
        noz = 1
        normM.append(1)
        
    noM = [nox/len(normM), noy/len(normM), noz/len(normM)]   
    
    
    #find the rotation matrix
    matrixR = aljabr.vectorsToRotMatrix(noB,noM) 
    #apply the rotation matrix
    applyTransformation(vertsM, matrixR)
    
    #special case: if the normal vector is -1 on z-axis then rotate the mesh 180 degrees on z
    if noM[2] < -0.30:
        matrixR1 = sp.mat([[math.cos(math.pi),-math.sin(math.pi),0], [math.sin(math.pi),math.cos(math.pi),0], [0,0,1]])
        #applica la matrice di rotazione
        applyTransformation(vertsM, matrixR1)
    
    #linear regression
    angle = fitline(vertsM)
    angleDeg = angle*(180/math.pi)
    #stop condition: angle < 1 or condStop = 0
    condStop = -1
    while math.fabs(angle*(180/math.pi)) > 1 and condStop == -1:
        matrixR2 = sp.mat([[math.cos(-angle),-math.sin(-angle),0], [math.sin(-angle),math.cos(-angle),0], [0,0,1]])
         #apply the rotation matrix
        applyTransformation(vertsM, matrixR2)
        angleOld = angle
        angle = fitline(vertsM)
        if math.fabs(angle) > math.fabs(angleOld):
            condStop = 0
    
    #find the scale matrix
    bbM = calcbb(vertsM)
    factor1 = math.sqrt(math.pow(1.74, 2) / math.pow(bbM[0] - bbM[1], 2));
    factor2 = math.sqrt(math.pow(2.72, 2) / math.pow(bbM[2] - bbM[3], 2));
    factor3 = math.sqrt(math.pow(1.2, 2) / math.pow(bbM[4] - bbM[5], 2));
    factor = max(factor1, factor2, factor3);
    matrixS = sp.mat([[factor,0,0], [0,factor,0], [0,0,factor]])
    #apply the scale matrix
    applyTransformation(vertsM, matrixS)
    
    #adjust the pivot
    bb = calcbb(vertsM)
    dx = (math.sqrt(math.pow(bb[0] - bb[1], 2)) / 2) - bb[1]
    dy = (math.sqrt(math.pow(bb[2] - bb[3], 2)) / 2) - bb[3]
    for v in vertsM:
        v[0] = v[0] + dx
        v[1] = v[1] + dy
    return (vertsM, facesM)