float(pc.split()[1]))
                    ymax = max(float(pa.split()[1]), float(pb.split()[1]),
                               float(pc.split()[1]))

                    if ((xmin < float(nodes.split()[0]) < xmax)
                            and (ymin < float(nodes.split()[1]) < ymax)):
                        #coordList = Point([float(nodes.split()[0]), float(nodes.split()[1])])  # check if the lidar point is inside the rectangal
                        x1 = cut_element[0][0]
                        y1 = cut_element[0][1]
                        x2 = cut_element[1][0]
                        y2 = cut_element[1][1]
                        x3 = cut_element[2][0]
                        y3 = cut_element[2][1]

                        xsi, eta = inside(float(nodes.split()[0]),
                                          float(nodes.split()[1]), x1, y1, x2,
                                          y2, x3, y3)
                        # here we try to find if the the point is inside the element
                        #if element.contains(coordList) is True:
                        if (0.0 < xsi < 1.0 and 0.0 < eta < 1.0):
                            d1 = math.sqrt(eta**2 + xsi**2)
                            d2 = math.sqrt((eta - 1.0)**2 + xsi**2)
                            d3 = math.sqrt((xsi - 1.0)**2 + (eta**2))
                            if min(d1, d2, d3) > math.sqrt(
                                    2
                            ) / 10.0:  # WE WANT TO AVOID OF COLLECTING POINTS WHICH ARE NEAR THE VERTICES
                                xnew = float(nodes.split()[0])
                                ynew = float(nodes.split()[1])
                                # "A" is a list which contains all the lidar points inside th element

                                # We want to interpolate H for the Lidar point located in the element
def ORIGINAL_FUNCTION(lidar,new_points):

    with open ('parameters.txt','r') as f:
        for x, line in enumerate(f):
            if x == 0:
                water_depth = line
                water_depth = water_depth.replace("\n","")

            #if x == 1:
             #   topology = line
              #  topology = topology.replace("\n","")

            if x == 2:
                epsilon = float(line)

            if x == 3:
                alpha = float(line)

            #if x == 4:
             #   new_points = line

      #          print("epsilon",)

    #READ VECTOR SOLUTION (either eta or H)

    with open(water_depth, 'r') as f1:
        for x, line in enumerate(f1):        #with enumerate function we make a relationship between 2 parameeters of (x and line). x is the number of the line, and line is the content of each line
            if "EndHeader" in line:
                num1 = x     # x is the number of the line which  coordinate with start

    with open(water_depth, 'r') as f1:
        for x, line in enumerate(f1):
            if "NodeCount" in line:
                num2 = x
    with open(water_depth, 'r') as f:
        for x, line in enumerate(f):
            if x == num2:                          # we want to find when the connectivities start and when they are finished
                total_nodes = float(line.split()[1])   # THIS IS THE TOTAL NUMBER OF NODES


                # READ ALL COORDINATES X, Y and the solution variable (eta or H)
    with open('coordinate','w') as f3:
        with open(water_depth, "r") as f2:
            for x, line in enumerate(f2):
                if num1-2 < x <= (total_nodes +num1):
                    f3.write(line)

    with open('coordinate', "r") as f:
        for x, line in enumerate(f):
            if x>=0:
                points = f.readlines()
    print(points[1])







    #we want to extract the connectivities of the elements


    with open("log_file2.txt","r") as f:
        csvfile = csv.reader(f, delimiter =' ')  #  Try to read old connectivities

        with open('log_file3.txt','a') as flog:
            flog.write("epsilon=")
            flog.write(str(epsilon))
            flog.write("\n")

            flog.write("alpha=")
            flog.write(str(alpha))
            flog.write("\n")


        total_number_newpoints = 0
        for x, line in enumerate(csvfile):
            if x>=0:
                with open('log_file3.txt', 'a') as flog:
                    flog.write("processing element=")
                    flog.write(str(int(x-total_nodes-num1)))
                    flog.write("\n")
            #a ,b,c are node numbers
                a = line[0]
                b = line[1]
                c = line[2]

    # pa,pb,pc are the nodes represented by x,y, eta or H
                pa = points[int(a)]
                pb = points[int(b)]
                pc = points[int(c)]
                cut_element = []
            # first find if the element is wet, dry, or cut elemets
                cut_element = check_element(pa, pb, pc)

    # cut_element contains [(x1,y1),(x2,y2),(x3,y3),(x1,y1)] for defining the polygon
                with open('log_file3.txt', 'a') as flog:
                    flog.write("cut_element=")
                    flog.write(str(cut_element))
                    flog.write("\n")

                if cut_element != []:
                    #element = Polygon(cut_element)



            #we should do the loop for cut-elements only
            #we want to check all the lidar points to see if they are in the cut element
                    A = []
                    for nodes in lidar:
    # now we reduce the lidar nodes which we want to check for each element to the min(x1,x2,x3) and min(y1,y2,y3) till max(x1,x2,x3) and max(y1,y2,y3)
                        xmin = min(float(pa.split()[0]),float(pb.split()[0]),float(pc.split()[0]))
                        xmax = max(float(pa.split()[0]),float(pb.split()[0]),float(pc.split()[0]))
                        ymin = min(float(pa.split()[1]),float(pb.split()[1]),float(pc.split()[1]))
                        ymax = max(float(pa.split()[1]),float(pb.split()[1]),float(pc.split()[1]))



                        if ((xmin<float(nodes.split()[0])<xmax) and
                                (ymin<float(nodes.split()[1]) < ymax)):
                            #coordList = Point([float(nodes.split()[0]), float(nodes.split()[1])])  # check if the lidar point is inside the rectangal
                            x1 = cut_element[0][0]
                            y1 = cut_element[0][1]
                            x2 = cut_element[1][0]
                            y2 = cut_element[1][1]
                            x3 = cut_element[2][0]
                            y3 = cut_element[2][1]

                            xsi, eta = inside(float(nodes.split()[0]),float(nodes.split()[1]),x1,y1,x2,y2,x3,y3)
                        # here we try to find if the the point is inside the element
                            #if element.contains(coordList) is True:
                            if (0.0 < xsi < 1.0 and 0.0 < eta < 1.0):
                                d1 = math.sqrt(eta**2 + xsi**2)
                                d2 = math.sqrt((eta-1.0)**2 + xsi**2)
                                d3 = math.sqrt((xsi-1.0)**2+(eta**2))
                                if min(d1, d2, d3) > math.sqrt(2)/10.0:                # WE WANT TO AVOID OF COLLECTING POINTS WHICH ARE NEAR THE VERTICES
                                    xnew = float(nodes.split()[0])
                                    ynew = float(nodes.split()[1])
                            # "A" is a list which contains all the lidar points inside th element



                        # We want to interpolate H for the Lidar point located in the element
                                    Hnew = interpol(float(pa.split()[0]),float(pa.split()[1]),float(pa.split()[2]),float(pb.split()[0]),float(pb.split()[1]),float(pb.split()[2]),float(pc.split()[0]),float(pc.split()[1]),float(pc.split()[2]),xnew,ynew)

                                    if Hnew < alpha*epsilon:                           # We changed abs(Hnew) with Hnew
                                        A = A+ [(xnew,ynew,float(nodes.split()[2]))]

                                # done for the current element
                    if A!=[]:
                        with open(new_points, "a") as feta:

                        # we are trying to find the points which are  making bump(max of z) or valley(min of z)
                            feta.write(str(max(A,key=itemgetter(2))))
                            feta.write("\n")
                            total_number_newpoints= total_number_newpoints + 1
                            if max(A,key=itemgetter(2)) != min(A,key=itemgetter(2)):
                                feta.write(str(min(A,key=itemgetter(2))))
                                feta.write("\n")
                                total_number_newpoints = total_number_newpoints + 1
                                with open('log_file3.txt', 'a') as flog:
                                    flog.write("done with this cut_element=")
                                    flog.write(str(cut_element))
                                    flog.write("\n")
                                    cut_element = []

    with open('log_file3.txt', 'a') as flog:
        flog.write("processing element done")
        flog.write("\n")
        flog.write("total number points = ")
        flog.write(str(total_number_newpoints))
        flog.write("\n")