def detVec(prim, dirVec, exception):
        global epsilon
        reload(GeoMath)
        vec1 = GeoMath.vecNormalize(GeoMath.vecSub(list(prim.vertices()[0].point().position()), list(prim.vertices()[1].point().position())))
        vec2 = GeoMath.vecNormalize(GeoMath.vecSub(list(prim.vertices()[2].point().position()), list(prim.vertices()[1].point().position())))
        prim_normal = list(prim.normal())
        if(list(prim_normal) != [0, 1, 0]):
            # We consider that y is vertical and x horizontal
            if(math.fabs(vec1[1]) > math.fabs(vec2[1])):
                # If the vectors are dependent
                if(math.fabs(GeoMath.vecDotProduct(vec1, vec2)) > epsilon):
                    vecV = GeoMath.rotateVecByVec(vec2, prim_normal, 90)
                    # Quads!!
                    if(GeoMath.vecDotProduct(vecV, vec1) < -epsilon):
                        vecV = GeoMath.rotateVecByVec(vec2, prim_normal, -90)
                else:
                    vecV = vec1
                vecH = vec2
            else:
                # If the vectors are dependent
                if(math.fabs(GeoMath.vecDotProduct(vec1, vec2)) > epsilon):
                    vecV = GeoMath.rotateVecByVec(vec1, prim_normal, 90)
                    # Quads!!
                    if(GeoMath.vecDotProduct(vecV, vec2) < -epsilon):
                        vecV = GeoMath.rotateVecByVec(vec1, prim_normal, -90)
                else:
                    vecV = vec2
                vecH = vec1
        else:
            # We consider that x is vertical and z horizontal
            if(math.fabs(vec1[0]) > math.fabs(vec2[0])):
                # If the vectors are dependent
                if(math.fabs(GeoMath.vecDotProduct(vec1, vec2)) > epsilon):
                    vecV = GeoMath.rotateVecByVec(vec2, prim_normal, 90)
                    # Quads!!
                    if(GeoMath.vecDotProduct(vecV, vec1) < -epsilon):
                        vecV = GeoMath.rotateVecByVec(vec2, prim_normal, -90)
                else:
                    vecV = vec1
                vecH = vec2
            else:
                # If the vectors are dependent
                if(math.fabs(GeoMath.vecDotProduct(vec1, vec2)) > epsilon):
                    vecV = GeoMath.rotateVecByVec(vec1, prim_normal, 90)
                    #Quads!!!
                    if(GeoMath.vecDotProduct(vecV, vec2) < -epsilon):
                        vecV = GeoMath.rotateVecByVec(vec1, prim_normal, -90)
                else:
                    vecV = vec2
                vecH = vec1

        if(GeoMath.vecDotProduct(dirVec, vecH) < 0):
            vecH = GeoMath.vecSub([0, 0, 0], vecH)
        if(GeoMath.vecDotProduct(dirVec, vecV) < 0):
            vecV = GeoMath.vecSub([0, 0, 0], vecV)
        vecH = GeoMath.vecNormalize(vecH)
        vecV = GeoMath.vecNormalize(vecV)
        return vecH, vecV
Пример #2
0
    def detVec(prim, dirVec, exception):
        global epsilon
        reload(GeoMath)
        vec1 = GeoMath.vecNormalize(
            GeoMath.vecSub(list(prim.vertices()[0].point().position()),
                           list(prim.vertices()[1].point().position())))
        vec2 = GeoMath.vecNormalize(
            GeoMath.vecSub(list(prim.vertices()[2].point().position()),
                           list(prim.vertices()[1].point().position())))
        prim_normal = list(prim.normal())
        if (list(prim_normal) != [0, 1, 0]):
            # We consider that y is vertical and x horizontal
            if (math.fabs(vec1[1]) > math.fabs(vec2[1])):
                # If the vectors are dependent
                if (math.fabs(GeoMath.vecDotProduct(vec1, vec2)) > epsilon):
                    vecV = GeoMath.rotateVecByVec(vec2, prim_normal, 90)
                    # Quads!!
                    if (GeoMath.vecDotProduct(vecV, vec1) < -epsilon):
                        vecV = GeoMath.rotateVecByVec(vec2, prim_normal, -90)
                else:
                    vecV = vec1
                vecH = vec2
            else:
                # If the vectors are dependent
                if (math.fabs(GeoMath.vecDotProduct(vec1, vec2)) > epsilon):
                    vecV = GeoMath.rotateVecByVec(vec1, prim_normal, 90)
                    # Quads!!
                    if (GeoMath.vecDotProduct(vecV, vec2) < -epsilon):
                        vecV = GeoMath.rotateVecByVec(vec1, prim_normal, -90)
                else:
                    vecV = vec2
                vecH = vec1
        else:
            # We consider that x is vertical and z horizontal
            if (math.fabs(vec1[0]) > math.fabs(vec2[0])):
                # If the vectors are dependent
                if (math.fabs(GeoMath.vecDotProduct(vec1, vec2)) > epsilon):
                    vecV = GeoMath.rotateVecByVec(vec2, prim_normal, 90)
                    # Quads!!
                    if (GeoMath.vecDotProduct(vecV, vec1) < -epsilon):
                        vecV = GeoMath.rotateVecByVec(vec2, prim_normal, -90)
                else:
                    vecV = vec1
                vecH = vec2
            else:
                # If the vectors are dependent
                if (math.fabs(GeoMath.vecDotProduct(vec1, vec2)) > epsilon):
                    vecV = GeoMath.rotateVecByVec(vec1, prim_normal, 90)
                    #Quads!!!
                    if (GeoMath.vecDotProduct(vecV, vec2) < -epsilon):
                        vecV = GeoMath.rotateVecByVec(vec1, prim_normal, -90)
                else:
                    vecV = vec2
                vecH = vec1

        if (GeoMath.vecDotProduct(dirVec, vecH) < 0):
            vecH = GeoMath.vecSub([0, 0, 0], vecH)
        if (GeoMath.vecDotProduct(dirVec, vecV) < 0):
            vecV = GeoMath.vecSub([0, 0, 0], vecV)
        vecH = GeoMath.vecNormalize(vecH)
        vecV = GeoMath.vecNormalize(vecV)
        return vecH, vecV
    def do(self, scale=False):
        # Calcule points to tbn matrix
        self.calculatePoints()
        # Get some arbitrary vectors conected from vertices of prim

        vec1 = GeoMath.vecSub(self.get_previous_point(), self.get_point_which_is_relative())
        vec2 = GeoMath.vecSub(self.get_next_point(), self.get_point_which_is_relative())
        # logging.debug('Two arbitrary vec1 and vec2:' + str(vec1) + ' ' + str(vec2))

        # We have to know which angle reside between the two coencted vectors, to know if suposed vectors
        # in tangent space will be correct

        angle = GeoMath.vecDotProduct(vec1, vec2) / (GeoMath.vecModul(vec1) * GeoMath.vecModul(vec2))
        angle = math.acos(angle)
        angle = math.degrees(angle)
        # logging.debug('Angle between vecs:' + str(angle))

        # We put relative one arbitrary point to tangent space


        # logging.debug('Point relative:' + str(self.get_point_which_is_relative()))
        # Determine x and y vectors, now we'll have suposed horizontal and vertical vectors acording to
        # prim and direction of the crack
        hasTheNormalToY = GeoMath.vecDotProduct(list(self.get_prim().normal()), [0, 1, 0])
        # logging.debug('Has the normal to y?:' + str(hasTheNormalToY))
        if(hasTheNormalToY < (1 - epsilon) and hasTheNormalToY > (-1 + epsilon)):
            vecH, vecV = DetermineVectors.DetermineVectors.detVec(self.get_prim(), [0, 1, 0], [0, 0, 1])
            # logging.debug('Yes, it has the normal to y and vecs are:' + str(vecH) + ' ' + str(vecV))
        else:
            vecH, vecV = DetermineVectors.DetermineVectors.detVec(self.get_prim(), [0, 0, 1], [0, 0, 1])
            # logging.debug('No, it isnt has the normal to y and vecs are:' + str(vecH) + ' ' + str(vecV))
        # CHAPUZA CON NUMEROS COMPLEJOS!!! Precision de python pésima, 1.000000001>1?? no! y math.acos error
        cosAngle = GeoMath.vecDotProduct(vecH, vec1) / (GeoMath.vecModul(vec1) * GeoMath.vecModul(vecH))
        complexAngle = cmath.acos(cosAngle)
        if(complexAngle.imag == 0):
            angleBetweenDetVecAndVecH = math.acos(cosAngle)
        else:
            if(cosAngle < 0):
                angleBetweenDetVecAndVecH = math.acos(-1)
            else:
                angleBetweenDetVecAndVecH = math.acos(1)

        # Now we have to ensure that the vec1 has the same direction that the horizontal vector, if not, we
        # change and the horizontal vector will be vec2. Also we have to check if the prim is not a quad,
        # in this case we have to get the vertical vector from horizontal vector, rotating the known angle
        # between the two vectors conected in prim (in quad we know that the angle is 90 and we already have the
        # good vectors)
        if((math.fabs(angleBetweenDetVecAndVecH) < epsilon) or (math.fabs(angleBetweenDetVecAndVecH) > (math.pi - epsilon))):
            if(scale):
                x = GeoMath.vecScalarProduct([1, 0, 0], GeoMath.vecModul(vec1))
            x = [1, 0, 0]
            y = GeoMath.rotateVecByVec(x, [0, 0, 1], angle)
            if(scale):
                y = GeoMath.vecScalarProduct(GeoMath.vecNormalize(y), GeoMath.vecModul(vec2))
            tbn = GeoMath.createTBNmatrix(self.get_previous_point(), self.get_point_which_is_relative(), self.get_next_point(), x, [0, 0], y)
        else:
            if(scale):
                x = [1, 0, 0]
            y = GeoMath.rotateVecByVec(x, [0, 0, 1], angle)
            if(scale):
                y = GeoMath.vecScalarProduct(GeoMath.vecNormalize(y), GeoMath.vecModul(vec1))
            tbn = GeoMath.createTBNmatrix(self.get_previous_point(), self.get_point_which_is_relative(), self.get_next_point(), y, [0, 0], x)
        # logging.debug('tbn: ' + str(tbn.printAttributes()))
        tbnInverse = GeoMath.Matrix(3, 3)
        tbnInverse.copy(tbn)
        tbnInverse.matrix3Inverse()

        self.set_tbn(tbn)
        self.set_tbn_inverse(tbnInverse)
Пример #4
0
    def defCrack(self, prim, Ipoint, Fpoint, texturePrim):
        reload(AutoPattern)
        reload(Bresenham)
        reload(Data)
        reload(GeoMath)
        reload(HouInterface)
        global epsilon
        global primnumber
        # TEMP: only for debug the patterns
        # Size x and size y is the valor of some material with the minor wavelength(bigger pattern)
        curPoint = Ipoint
        self.patternCrack[prim] = []
        vertices = [list(p.point().position()) for p in prim.vertices()]
        print "vertices"
        print vertices
        # Convert prim to tangent space of patterns
        # Get some arbitrary vectors conected from vertices of prim
        vec1 = GeoMath.vecSub(vertices[0], vertices[1])
        vec2 = GeoMath.vecSub(vertices[2], vertices[1])
        # We have to know which angle reside between the two coencted vectors, to know if suposed vectors
        # in tangent space will be correct
        angle = GeoMath.vecDotProduct(vec1, vec2) / (GeoMath.vecModul(vec1) * GeoMath.vecModul(vec2))
        angle = math.acos(angle)
        angle = math.degrees(angle)

        # We put relative one arbitrary point to tangent space
        pointWhichIsRelative = vertices[1]
        # Determine x and y vectors, now we'll have suposed horizontal and vertical vectors acording to
        # prim and direction of the crack

        vecH, vecV = DetermineVectors.DetermineVectors.detVec(prim, GeoMath.vecSub(Ipoint, Fpoint), [0, 0, 1])
        # CHAPUZA CON NUMEROS COMPLEJOS!!! Precision de python pésima, 1.000000001>1?? no! y math.acos error

        cosAngle = GeoMath.vecDotProduct(vecH, vec1) / (GeoMath.vecModul(vec1) * GeoMath.vecModul(vecH))
        complexAngle = cmath.acos(cosAngle)
        if(complexAngle.imag == 0):
            angleBetweenDetVecAndVecH = math.acos(cosAngle)
        else:
            if(cosAngle < 0):
                angleBetweenDetVecAndVecH = math.acos(-1)
            else:
                angleBetweenDetVecAndVecH = math.acos(1)

        #=======================================================================
        # Now we have to ensure that the vec1 has the same direction that the horizontal vector, if not, we
        # change and the horizontal vector will be vec2. Also we have to check if the prim is not a quad,
        # in this case we have to get the vertical vector from horizontal vector, rotating the known angle
        # between the two vectors conected in prim (in quad we know that the angle is 90 and we already have the
        # good vectors)
        #=======================================================================
        print "Create TBN"
        if((math.fabs(angleBetweenDetVecAndVecH) < epsilon) or (math.fabs(angleBetweenDetVecAndVecH) > (math.pi - epsilon))):
            x = GeoMath.vecScalarProduct([1, 0, 0], GeoMath.vecModul(vec1))
            y = GeoMath.rotateVecByVec(x, [0, 0, 1], angle)
            y = GeoMath.vecScalarProduct(GeoMath.vecNormalize(y), GeoMath.vecModul(vec2))
            tbn = GeoMath.createTBNmatrix(vertices[0], vertices[1], vertices[2], x, [0, 0], y)
        else:
            x = GeoMath.vecScalarProduct([1, 0, 0], GeoMath.vecModul(vec2))
            y = GeoMath.rotateVecByVec(x, [0, 0, 1], angle)
            y = GeoMath.vecScalarProduct(GeoMath.vecNormalize(y), GeoMath.vecModul(vec1))
            tbn = GeoMath.createTBNmatrix(vertices[0], vertices[1], vertices[2], y, [0, 0], x)

        print "Edn create tbn"
        tbnInverse = GeoMath.Matrix(3, 3)
        tbnInverse.copy(tbn)
        tbnInverse.matrix3Inverse()

        # Get the first material:
        print "texture get first layer"
        texture = texturePrim.getFirstLayer(Ipoint)
        nextMaterial = texture.get_material()
        print "end get material"
        # Create status of the process to show to the user
        distance_to_complete = GeoMath.vecModul(GeoMath.vecSub(curPoint, Fpoint))
        ui_process_status = UIProcessStatus.UIProcessStatus('crack for prim',
                                                                distance_to_complete)
        while(GeoMath.vecModul(GeoMath.vecSub(curPoint, Fpoint)) > epsilon):
            # Print status of the process
            dist = GeoMath.vecModul(GeoMath.vecSub(curPoint, Fpoint))
            ui_process_status.calculate_status(dist, inverse=True)
            ui_process_status.print_status()

            genPattern = Data.GeneralPattern()
            for wavelength in nextMaterial.mat.keys():
                singleMat = nextMaterial.mat[wavelength]
                setOfTypeOfPattern = CDF.cdf([[singleMat.classesAndPercentage[k], k] for k in singleMat.classesAndPercentage.keys()])
                if(wavelength == 0):
                    nextPoint = Bresenham.Bresenham.bresenham(Ipoint, curPoint, Fpoint, setOfTypeOfPattern.getSizex(), setOfTypeOfPattern.getSizey(), prim, [1, 0, 0])
                    pat = AutoPattern.AutoPattern(curPoint, nextPoint, setOfTypeOfPattern, prim, wavelength, self.patternCrack, tbn, tbnInverse, pointWhichIsRelative, texture, texturePrim).pattern
                genPattern.applyPattern(pat, wavelength)

            # Check texture
            previousTexture = texture
            pii, texture = self.checkTexture(texturePrim, previousTexture, genPattern, Fpoint, nextPoint)
            logging.debug('Pii defcrack: ' + str(pii))
            logging.debug('CurPoint defcrack: ' + str(curPoint))
            logging.debug('genPattern ' + str(genPattern.getPoints()))
            '''
            if(not curPoint):
                curPoint=genPattern.getLastPoint()
            '''
            curPoint = genPattern.getLastPoint()
            if(texture):
                nextMaterial = texture.get_material()
            else:
                if(not (GeoMath.vecModul(GeoMath.vecSub(curPoint, Fpoint)) > epsilon)):
                    logging.error('Texture no matched, previous texture applied')
            # version 4
            self.patternCrack[prim].append(genPattern)