예제 #1
0
파일: CarveFunc.py 프로젝트: johnzjq/mySVN
def heuristic_tet_carving():
    print("----Heuristic shrinking-wrapping----")

    #keep all the tetrahedra that have fix facets on the shell
    #in update_stack() this step is not needed because it has already been handled in carving
    for fId in ModelData.listShellFaceIDs:
        if ModelData.dictFaces[fId].get_tag() == ClassFace.FIX:
            tetId = TetraFunc.find_tetids_by_faceid(fId, 1)
            keep_tetrahedron(tetId[0], TetraFunc.get_faceids_from_tetid(tetId[0]))
    #count
    numCount = 0
    #initiate Stack and lists
    sortedTetIDStack = update_stack()
    #heruistic carving
    while len(sortedTetIDStack) > 0:
        #pop the first not fix element of stack if possible
        curTet = sortedTetIDStack.pop(0)
        curTetId = curTet[0]
        print curTetId
        
        #fetch all the shellFaces (indices) from a given Tet
        shellFaceIDsInCurTet = curTet[1]
        #fetch all the dictFaces (indices) from a given Tet
        faceIDsInCurTet = curTet[2]

        #classify the tetrahedron
        numTMP = len(shellFaceIDsInCurTet)#equal to the no. of faces on the shell
        numFIX = 0
        for fid in faceIDsInCurTet:
            if ModelData.dictFaces[fid].get_tag() == ClassFace.FIX:
                numFIX += 1
        numKEEP = len(faceIDsInCurTet) - numTMP - numFIX#keep face is not constrainted as fixed ones
        numPRESERV = numFIX + numKEEP
        #test by a set of constraints
        bStop = False
        bPostponed = False
        carveTetList = [] #for infinite tetehedra
        keepFaceList = [] #for coplanar bounded facets
        unboundFaceList = [] #for coplanar unbouned facets
        
        # new code
        #if numFIX == 0 and numTMP == 1:
        #    #constraint3
        #    if constraintsCtrl['Constraint3'] and not bStop:
        #        bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[0])
        #elif numFIX == 1 and numTMP == 3:
        #    #constraint5
        #    if constraintsCtrl['Constraint5'] and not bStop:
        #            bStop = constraint_5(curTetId, faceIDsInCurTet)
        #    #check all faces with constraint 3 not needed in theory (flat tetrahedra)
        #    if constraintsCtrl['Constraint3'] and not bStop:
        #        bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[0])
        #    if constraintsCtrl['Constraint3'] and not bStop:
        #        bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[1])
        #    if constraintsCtrl['Constraint3'] and not bStop:
        #        bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[2])
        #elif numFIX in (1, 2) and numTMP == 2:
        #    #c1 5
        #    if constraintsCtrl['Constraint5'] and not bStop:
        #            bStop = constraint_5(curTetId, faceIDsInCurTet)

        #    if constraintsCtrl['Constraint1'] and not bStop:
        #        #extract the opposite edge
        #        eList = []
        #        for i in shellFaceIDsInCurTet:
        #            for j in range(0, 3):
        #                if ModelData.dictFaces[i].get_vids()[j] not in eList:
        #                    eList.append(ModelData.dictFaces[i].get_vids()[j])
        #                else:
        #                    eList.remove(ModelData.dictFaces[i].get_vids()[j])
        #    
        #        if not bStop:
        #            bStop, bPostponed, carveTetList = constraint_1(curTetId, eList)
        #    
        #    #check all faces with constraint 3 not needed in theory (flat tetrahedra)
        #    if constraintsCtrl['Constraint3'] and not bStop:
        #        bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[0])
        #    if constraintsCtrl['Constraint3'] and not bStop:
        #        bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[1])
        #elif numFIX in (1, 2, 3) and numTMP == 1:
        #    #c1 2 3 5
        #    #constraint5
        #    if constraintsCtrl['Constraint5'] and not bStop:
        #        bStop = constraint_5(curTetId, faceIDsInCurTet)
        #   
        #    #constraint3
        #    if constraintsCtrl['Constraint3'] and not bStop:
        #        bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[0])

        #    if (constraintsCtrl['Constraint2'] or constraintsCtrl['Constraint1']) and not bStop:
        #        #extract the candidate face, the opposite edge list (three) the and opposite vert
        #        tmpFace = shellFaceIDsInCurTet[0]
        #        #the opposite vertex
        #        oppVert = -1
        #        #the opposite edge list
        #        eList = []
        #        for v in ModelData.dictTetrahedrons[curTetId].get_vids():
        #            if v not in ModelData.dictFaces[tmpFace].get_vids():
        #                oppVert = v
        #                if numFIX == 1:
        #                    #only two edges
        #                    fixFace = list(set(faceIDsInCurTet) - set(shellFaceIDsInCurTet))
        #                    for v_1 in ModelData.dictFaces[fixFace[0]].get_vids():
        #                        if v_1 != oppVert:
        #                            eList.append((v, v_1))
        #                else:
        #                    #three edges
        #                    for n in ModelData.dictFaces[tmpFace].get_vids():
        #                        eList.append((v, n))
        #                break
        #        #check with constraints 1 2
        #        #2
        #        if constraintsCtrl['Constraint2'] and not bStop:
        #            bStop, bPostponed, carveTetList = constraint_2(curTetId, oppVert)
        #        #1
        #        if numFIX == 1:
        #            #two edges
        #            if constraintsCtrl['Constraint1'] and not bStop:
        #                bStop, bPostponed, carveTetList = constraint_1(curTetId, eList[0])
        #            if constraintsCtrl['Constraint1'] and not bStop:
        #                bStop, bPostponed, carveTetList = constraint_1(curTetId, eList[1])
        #        else:
        #            #three edges
        #            if constraintsCtrl['Constraint1'] and not bStop:
        #                bStop, bPostponed, carveTetList = constraint_1(curTetId, eList[0])
        #            if constraintsCtrl['Constraint1'] and not bStop:
        #                bStop, bPostponed, carveTetList = constraint_1(curTetId, eList[1])
        #            if constraintsCtrl['Constraint1'] and not bStop:
        #                bStop, bPostponed, carveTetList = constraint_1(curTetId, eList[2])
        #elif numFIX == 0:
        #    #check all faces with constraint 3 not needed in theory (flat tetrahedra)
        #    if constraintsCtrl['Constraint3'] and not bStop:
        #        for fID in shellFaceIDsInCurTet:
        #            bStop, keepFaceList, unboundFaceList = constraint_3(fID)


        #old code
        #every carving step is constrainted, esp. those without fixed faces
        if numTMP == 4:
            print('Isolated tetrahedron')
            pass
            #check all faces with constraint 3 not needed in theory
            if constraintsCtrl['Constraint3'] and not bStop:
                bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[0])
            if constraintsCtrl['Constraint3'] and not bStop:
                bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[1])
            if constraintsCtrl['Constraint3'] and not bStop:
                bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[2])
            if constraintsCtrl['Constraint3'] and not bStop:
                bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[3])
        elif numTMP == 3:
            #check with constraints 5
            if numFIX == 1:
                if constraintsCtrl['Constraint5'] and not bStop:
                    bStop = constraint_5(curTetId, faceIDsInCurTet)
            #check all faces with constraint 3 not needed in theory
            if constraintsCtrl['Constraint3'] and not bStop:
                bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[0])
            if constraintsCtrl['Constraint3'] and not bStop:
                bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[1])
            if constraintsCtrl['Constraint3'] and not bStop:
                bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[2])
        elif numTMP == 2:
            if constraintsCtrl['Constraint1']:
                #extract the opposite edge
                eList = []
                for i in shellFaceIDsInCurTet:
                    for j in range(0, 3):
                        if ModelData.dictFaces[i].get_vids()[j] not in eList:
                            eList.append(ModelData.dictFaces[i].get_vids()[j])
                        else:
                            eList.remove(ModelData.dictFaces[i].get_vids()[j])
            
                #1 different with new code
                if constraintsCtrl['Constraint1'] and not bStop:
                    bStop, bPostponed, carveTetList = constraint_1(curTetId, eList)
            #5
            if numFIX != 0:
                if constraintsCtrl['Constraint5'] and not bStop:
                    bStop = constraint_5(curTetId, faceIDsInCurTet)
                
            #check both faces with constraint 3 not needed in theory
            if constraintsCtrl['Constraint3'] and not bStop:
                bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[0])
            if constraintsCtrl['Constraint3'] and not bStop:
                bStop, keepFaceList, unboundFaceList = constraint_3(shellFaceIDsInCurTet[1])

        elif numTMP == 1:
            if constraintsCtrl['Constraint2'] or constraintsCtrl['Constraint1']:
                #extract the candidate face, the opposite edge list (three) the and opposite vert
                tmpFace = shellFaceIDsInCurTet[0]
                #the opposite vertex
                oppVert = -1
                #the opposite edge list
                eList = []
                for v in ModelData.dictTetrahedrons[curTetId].get_vids():
                    if v not in ModelData.dictFaces[tmpFace].get_vids():
                        oppVert = v
                        for n in ModelData.dictFaces[tmpFace].get_vids():
                            eList.append((v, n))
                        break
                #check with constraints 1 2 3 5
                #2 different with new code
                if constraintsCtrl['Constraint2'] and not bStop:
                    bStop, bPostponed, carveTetList = constraint_2(curTetId, oppVert)
                #1 different with new code
                if constraintsCtrl['Constraint1'] and not bStop:
                    bStop, bPostponed, carveTetList = constraint_1(curTetId, eList[0])
                if constraintsCtrl['Constraint1'] and not bStop:
                    bStop, bPostponed, carveTetList = constraint_1(curTetId, eList[1])
                if constraintsCtrl['Constraint1'] and not bStop:
                    bStop, bPostponed, carveTetList = constraint_1(curTetId, eList[2])
           
            if numFIX != 0 and constraintsCtrl['Constraint5'] and not bStop:
               #5
                bStop = constraint_5(curTetId, faceIDsInCurTet)   
               
            #3
            if numFIX != 3 and constraintsCtrl['Constraint3'] and not bStop:
                bStop, keepFaceList, unboundFaceList = constraint_3(tmpFace)
             
        else:
            print("Isolated Tetrahedron!", numTMP, numKNOWN)
            #raise Exception
        
        #manipulate the tetrahedron
        numTet = len(ModelData.dictTetrahedrons)
        if bStop:
            #keep
            if len(keepFaceList) != 0:
                #keep a bunch of "coplanar" tetrehedra
                for faceId in keepFaceList:
                    tetId = TetraFunc.find_tetids_by_faceid(faceId, 1)
                    if tetId != []:
                        keep_tetrahedron(tetId[0], TetraFunc.get_faceids_from_tetid(tetId[0]))
                #carve a bunch of "coplanar" unbouned tetrehedra
                if len(unboundFaceList) != 0:
                    for faceId in unboundFaceList:
                        tetId = TetraFunc.find_tetids_by_faceid(faceId, 1)
                        if tetId != []:
                            carve_tetrahedron(tetId[0], TetraFunc.get_faceids_from_tetid(tetId[0]))
            else:
                #keep
                keep_tetrahedron(curTetId, faceIDsInCurTet)
        else:
            #carve or postpone
            if not bPostponed:
                #Carve
                bIsCarved = True
                carve_tetrahedron(curTetId, faceIDsInCurTet)
            else:
                #postpone
                if ModelData.dictTetrahedrons[curTetId].get_tag() == ClassFace.INF:
                    #all the tetrehedra are infinite (INF)
                    for tetId in carveTetList:
                        #carve a bunch of tetrahedra
                        carve_tetrahedron(tetId, TetraFunc.get_faceids_from_tetid(tetId))
                else:
                    #postphone the tetrahedron
                    ModelData.dictTetrahedrons[curTetId].set_tag(ClassFace.INF)

        #Update the dictFaces
        ModelDataFuncs.remove_faces()
        #Update the stack and lists
        sortedTetIDStack = update_stack()

        #Output intermediate results
        if globalOutputCarve  and numTet > len(ModelData.dictTetrahedrons) and len(ModelData.dictTetrahedrons) % globalOutputFreq == 0:
            ModelDataFuncs.writer_obj(ModelData.strInputFileName + '_' + str(len(ModelData.dictTetrahedrons)) + "_CARVE_.obj")
예제 #2
0
def do_the_work(inputFilePath, isSemantic, debugControl):

    #Init mid files
    if os.path.exists(MIDFILEPATH):
        os.remove(MIDFILEPATH)

    #Init datastructure
    ModelDataFuncs.Init()
    ModelData.strInputFileName, ext = os.path.splitext(inputFilePath)

    #read file
    print ("----Processing file----")
    print (inputFilePath)
    if isSemantic == True:
        if os.path.splitext(inputFilePath)[1] == '.obj':
            print('WARNING: obj format does not contain semantics')
            print('Semantics will be deduced')
            print ("Check and tessellate the file...")
            MyCov = ConvProvider()
            MyCov.convert(inputFilePath, MIDFILEPATH, True)
            #read a tesselated objfile
            if ModelDataFuncs.reader_obj(MIDFILEPATH) == False:
                raise Exception
        else:
            #poly with semantics
            try:
                if not ModelDataFuncs.reader_poly_with_semantics(inputFilePath):
                    return
            except:
                raise ImportError
    else:
        #preprocess (convert and tessellation)
        print ("Check and tessellate the file...")
        MyCov = ConvProvider()
        MyCov.convert(inputFilePath, MIDFILEPATH, True)
        #read a tesselated objfile
        if ModelDataFuncs.reader_obj(MIDFILEPATH) == False:
            raise Exception

    #invert the normal of the input model
    if ModelData.global_INVERT_NORMAL:
        ModelDataFuncs.invert_poly_normal()
    #only for debug
    #tmp = ModelData.centerVertex
    #ModelData.centerVertex = (0.0, 0.0, 0.0)
    #ModelDataFuncs.writer_obj(ModelData.strInputFileName+"_CLS.obj")
    #ModelData.centerVertex = tmp
    #
    if int(debugControl) == 1:
        #Decomposit all the triangles
        DecomposeFunc.model_decompositionEx()

        #Merge all the coplaner dictFaces
        #coplaner_face_merge()
        #ModelDataFuncs.writer_obj(ModelData.strInputFileName+"_DEC.obj")

    elif int(debugControl) == 2:
        #Constrained Tetrahedralization
        if False == TetraFunc.CDT():
            print("Constrained Delauney Tetrahedralization FAILED!")
            return

        #ModelDataFuncs.writer_obj(ModelData.strInputFileName+"_CDT.obj")

        #Heuristic carving
        CarveFunc.heuristic_tet_carving()
        #ModelDataFuncs.writer_obj(ModelData.strInputFileName+"_CARVE.obj")

        #Reconstruct the mesh from tetrahedron
        TetraFunc.extract_mesh_from_tet(isSemantic)
        #Output
        ModelDataFuncs.writer_obj(ModelData.strInputFileName+"_OUTPUT.obj")
        if isSemantic:
            ModelDataFuncs.writer_poly_with_semantics(ModelData.strInputFileName + "_OUTPUT.poly")
    elif int(debugControl) == 3:
        #only deduce the semantics
        TetraFunc.deduce_semantics_of_poly(isSemantic)
        ModelDataFuncs.writer_poly_with_semantics(ModelData.strInputFileName + "_OUTPUT.poly")

    else:
        #Decomposit all the triangles
        DecomposeFunc.model_decompositionEx()

        #Merge all the coplaner dictFaces
        #coplaner_face_merge()
        ModelDataFuncs.writer_obj(ModelData.strInputFileName +"_DEC.obj")

        #Constrained Tetrahedralization
        if False == TetraFunc.CDT():
            print("Constrained Delauney Tetrahedralization FAILED!")
            return
        ModelDataFuncs.writer_obj(ModelData.strInputFileName+"_CDT.obj")

        #Heuristic carving
        CarveFunc.heuristic_tet_carving()
        ModelDataFuncs.writer_obj(ModelData.strInputFileName+"_CARVE.obj")

        #Reconstruct the mesh from tetrahedron
        TetraFunc.extract_mesh_from_tet(isSemantic)
        #Output
        ModelDataFuncs.writer_obj(ModelData.strInputFileName + "_OUTPUT.obj")
        if isSemantic:
            ModelDataFuncs.writer_poly_with_semantics(ModelData.strInputFileName + "_OUTPUT.poly")