def smooth(self):
     smoothed_triangles = [Polygon.copy(tr.points) for tr in self.triangles]
     for key, value in self.points_to_points.items():
         if key in self.bound_points: continue
         new_point = Polygon.get_center(value)
         for i in self.points_to_triangles[key]:
             smoothed_triangles[i].replace_point(key, new_point)
     self.triangles = smoothed_triangles
     self.calculate_hashes()
     return self.triangles
Example #2
0
 def precalculate_polygon(self):
     diameter = self.diameter
     self.bounds_hash = {}
     my_polygon = Polygon()
     for i in range(len(self.polygon)):
         point1 = self.polygon.points[i - 1]
         point2 = self.polygon.points[i]
         my_polygon.extend(Edge.split(point1, point2, diameter))
     self.precalculated_polygon = Polygon.copy(my_polygon.points)
     return my_polygon
    def triangulate(self, input_polygon, diameter):
        self.initial_polygon = Polygon.copy(input_polygon.points)
        decomposer = PolygonDecomposer()
        polygon = Polygon.copy(input_polygon.points)
        polygons = decomposer.decompose(polygon)
        self.bound_points = {}
        temp_triangulator = MonotonePolygonTriangulator(diameter, Polygon.copy(input_polygon.points))
        temp_triangulator.precalculate_polygon()

        self.bounds_polygon = temp_triangulator.precalculated_polygon
        
        self.bound_points = dict(self.bound_points, **temp_triangulator.get_bound_points())
        self.triangles = []
        for pol in polygons:
            triangulator = MonotonePolygonTriangulator(diameter, pol)
            self.triangles.extend(triangulator.triangulate())
        self.calculate_hashes()
        return self.triangles
Example #4
0
    def get_bound_points(self, subdivide_n=0):
        self.bound_points = {}

        precalculated_copy = Polygon.copy(self.precalculated_polygon.points)
        precalculated_copy.subdivide(subdivide_n)

        for p in precalculated_copy:
            self.bound_points[p] = True
        return self.bound_points
Example #5
0
    def inner_decompose(self, polygon):
        distances = get_distances(polygon)
        if not distances:
            self.polygons.append(Polygon.copy(polygon.points))
            return

        element = distances[0]
        point1 = element[1]
        point2 = element[2]

        new_polygon = polygon.split_points(point1, point2)

        self.inner_decompose(new_polygon)
        self.inner_decompose(polygon)
Example #6
0
import matplotlib.pyplot as plt

from pyFEM.geometry import Point, Polygon, Triangulator
from pyFEM.conditions import ConditionsItem, BoundaryConditions, CompleteTaskConditions
from pyFEM.matrices import generate_matrix

from pyFEM.geometry import PolygonDecomposer

if __name__ == '__main__':
    subdivide_n = 1

    p1 = Point(0, 0)
    p2 = Point(0, 3)
    p3 = Point(6, 3)
    p4 = Point(6, 0)
    polygon = Polygon.copy([p1, p2, p3, p4])
    bounds_list = [([p1, p2], ConditionsItem(0, 1, 0)),
                   ([p2, p3], ConditionsItem(10000, 1, 0)),
                   ([p3, p4], ConditionsItem(0, 1, 0)),
                   ([p4, p1], ConditionsItem(10000, 1, 100))]

    p1 = Point(0, 0)
    p2 = Point(0, 5)
    p3 = Point(5, 0)
    polygon = Polygon.copy([p1, p2, p3])
    bounds_list = [([p1, p2], ConditionsItem(1000, 1, 100)),
                   ([p2, p3], ConditionsItem(1000, 1, 100)),
                   ([p3, p1], ConditionsItem(1, 1, 0))]
    

    x_limits = (min([p.x for p in polygon]), max(p.x for p in polygon))
Example #7
0
    def inner_triangulate(self):
        count = 0

        if len(self.curr_polygon) < 3:
            return self.curr_polygon

        if len(self.curr_polygon) == 3:
            self.triangles.append(self.curr_polygon)
            return self.curr_polygon

        if len(self.curr_polygon) == 4:
            return self.process_polygon4(self.curr_polygon)

        # main loop
        while self.curr_angles:
            # first stupid/simple situations
            if len(self.curr_polygon) == 4:
                self.process_polygon4(self.curr_polygon)
                break

            if len(self.curr_polygon) == 3:
                self.triangles.append(self.curr_polygon)
                self.save_triangle_points(self.curr_polygon, len(self.triangles) - 1)
                break

            if len(self.curr_polygon) < 3:
                break

            # and now sophisticated stuff

            # at first delete element with smalest angle
            element = self.curr_angles[0]
            point = element[0]
            angle = element[1]

            del self.curr_angles[0]
            self.curr_polygon.curr_point = point

            # if it is just a cutoff - do it
            # and go to next loop iteration
            if angle < math.pi / 2.0:
                # p "upper cutoff"
                self.cut_off_vertex(self.curr_polygon.curr_index, angle)
                continue

            a1 = math.pi / 2.0
            a2 = 5.0 * math.pi / 6.0
            a3 = 2.0 * math.pi

            bad_point = False
            used_elements = []

            while True:  # while bad_point
                if 0 <= angle <= a1:
                    # p "inner cutoff"
                    bad_point = False
                    self.cut_off_vertex(self.curr_polygon.curr_index, angle)
                elif a1 <= angle <= a2:
                    # p 'case 2'
                    p = self.process_case2(angle)
                    if not p:
                        used_elements.append([Point(point.x, point.y), angle])
                        bad_point = True
                    bad_point = False
                    # if all is ok - than we will insert
                    # a new point - need to add first elements
                    if used_elements:
                        self.curr_angles = used_elements + self.curr_angles
                        used_elements = []

                    p1 = self.curr_polygon.curr_point
                    p2 = self.curr_polygon.prev()
                    p3 = self.curr_polygon.next()

                    tr1 = Polygon.copy([p1, p3, p])
                    tr2 = Polygon.copy([p1, p, p2])

                    self.triangles.append(tr1)
                    self.save_triangle_points(tr1, len(self.triangles) - 1)

                    self.triangles.append(tr2)
                    self.save_triangle_points(tr2, len(self.triangles) - 1)

                    angle1 = self.curr_polygon.get_angle(self.curr_polygon.curr_index - 1)
                    angle2 = self.curr_polygon.get_angle(self.curr_polygon.curr_index + 1)

                    self.curr_polygon.replace_current(p)

                    # change angle of previous point
                    new_angle = self.curr_polygon.get_angle(self.curr_polygon.curr_index - 1)
                    self.change_alpha(self.curr_polygon.prev(), angle1, new_angle, self.curr_angles)

                    # add angle of next point
                    new_angle = self.curr_polygon.get_angle(self.curr_polygon.curr_index + 1)
                    self.change_alpha(self.curr_polygon.next(), angle2, new_angle, self.curr_angles)

                    # add angle of current point
                    new_angle = self.curr_polygon.get_angle(self.curr_polygon.curr_index)
                    self.insert_alpha_point(new_angle, self.curr_polygon.curr_point, self.curr_angles)
                elif a2 <= angle <= a3:
                    return
                    # print('case 3')
                    # arr = self.process_case3(angle)
                    # if not arr:
                    #     used_elements.append([Point(point.x, point.y), angle])
                    #     bad_point = True
                    # else:
                    #     bad_point = False

                    # if used_elements:
                    #     self.curr_angles = used_elements + self.curr_angles
                    #     used_elements = []

                    # p = arr[0]
                    # index = arr[1]

                    # self.curr_polygon.curr_index = index
                    # p2 = self.curr_polygon.curr_point
                    # p1 = self.curr_polygon.prev

                    # tr = Polygon.copy([p1, p2, p])
                    # self.triangles.append(tr)
                    # self.save_triangle_points(tr, len(self.triangles) - 1)

                    # self.curr_polygon.insert_point(index, p)

                    # # insert angle of current vertex (a new vertex)
                    # new_angle = self.curr_polygon.get_angle(index)
                    # self.insert_alpha_point(new_angle, p, self.curr_angles)

                    # new_angle = self.curr_polygon.get_angle(index - 1)
                    # self.change_alpha(self.curr_polygon.prev, angle, new_angle, self.curr_angles)

                    # new_angle = self.curr_polygon.get_angle(index - 2)
                    # self.change_alpha(self.curr_polygon.prevprev, angle, new_angle, self.curr_angles)

                if len(self.curr_angles) <= 4:
                    break

                # break if self.curr_angles.size <= 4

                # p self.curr_angles
                # p self.curr_polygon

                if bad_point:
                    element = self.curr_angles[0]
                    point = element[0]
                    angle = element[1]

                    del self.curr_angles[0]

                    self.curr_polygon.curr_point = point

                    print("move next")
                else:
                    if used_elements:
                        self.curr_angles = used_elements + self.curr_angles
                        used_elements.clear

                if not bad_point:
                    break
Example #8
0
#!/usr/bin/env python2

import matplotlib.pyplot as plt
from matplotlib import collections as mcoll

from pyFEM.geometry import Point, Polygon, Triangulator


if __name__ == '__main__':
    poly = Polygon.copy([Point(0, 0), Point(0, 4), Point(4, 4), Point(4, 0)])
    diam = 1.0

    triangulator = Triangulator()
    triangulator.triangulate(poly, diam)
    triangulator.smooth()
    triangulator.subdivide_triangles(1)
    triangulator.calculate_hashes()
    nums = triangulator.enumerate_triangles()
    
    triangles = []
    all_points_x = []
    all_points_y = []
    text = []
    for i, tri in enumerate(triangulator.triangles):
        triangles.append(tuple([(p.x, p.y) for p in tri.points]))
        all_points_x.extend([p.x for p in tri.all_points])
        all_points_y.extend([p.y for p in tri.all_points])
        text.extend([str(nums[p]) for p in tri.all_points])
        print("triangle {0}: {1}".format(i, [nums[p] for p in tri.all_points]))
    # print("bound points: {0}".format([nums[pt]
    #                                   for pt, bound