Exemple #1
0
    def find_intersection_with_polygon(self, subject):
        # self is window
        subject_segments = [
            Segment(p1, p2)
            for p1, p2 in zip(subject.points[:-1], subject.points[1:])
        ]
        subject_segments.append(Segment(subject.points[-1], subject.points[0]))

        self_segments = [
            Segment(p1, p2)
            for p1, p2 in zip(self.points[:-1], self.points[1:])
        ]
        self_segments.append(Segment(self.points[-1], self.points[0]))

        intersection_points = []
        for self_segment in self_segments:
            for subject_segment in subject_segments:
                if (
                    Segment.is_intersection(self_segment, subject_segment)
                    is True and
                    Segment.intersection(self_segment, subject_segment)
                    is not None
                ):
                    intersection_points.append(
                        Segment.intersection(subject_segment, self_segment)
                    )
        # for point in intersection_points:
        #     Point(point.x, point.y, self)
        print len(intersection_points)

        subject_points = create_list(intersection_points, subject_segments)
        self_points = create_list(intersection_points, self_segments)
        into_self_windows = []  # list of input into window

        i = 0
        for point in self_points:
            if point in intersection_points and i == 0:
                into_self_windows.append(point)
                i = (i + 1) % 2

        visited = {
            point: False
            for point in chain(subject_points, self_points)
        }

        start_point = into_self_windows[0]
        actual_index = subject_points.index(start_point)
        actual_index = (actual_index + 1) % len(subject_points)
        actual_point = subject_points[actual_index]
        output_point_list = [start_point]

        actual_poly = self_points

        def not_actual_poly(actual_poly):
            if actual_poly == subject_points:
                return self_points
            else:
                return subject_points

        while actual_point != start_point:
            if visited[actual_point]:
                break
            visited[actual_point] = True
            output_point_list.append(actual_point)
            if actual_point in not_actual_poly(actual_poly):
                actual_poly = not_actual_poly(actual_poly)
                actual_index = actual_poly.index(actual_point)
                actual_index = (actual_index + 1) % len(actual_poly)
                actual_point = actual_poly[actual_index]
            else:
                actual_index = (actual_index + 1) % len(actual_poly)
                actual_point = actual_poly[actual_index]

        output_poly = Polygon(
            None, None, self.widget, polygon_point_list=output_point_list
        )
        self.widget.selected_obj = output_poly
        self.widget.object_set.add(output_poly)
Exemple #2
0
def re_draw_polygon_inside(
        polygon, texture=None, lights=None, viewer=None, min_x=None, min_y=None,
        bump_map=None
):
    clean_polygon_inside(polygon)
    polygon.token_inside = object()

    token_obj = polygon.token_inside
    color = Color(0, 0, 0)
    canvas = polygon.widget.canvas

    segments = [
        Segment(p1, p2)
        for p1, p2 in zip(polygon.points[:-1], polygon.points[1:])
    ]
    segments.append(Segment(polygon.points[-1], polygon.points[0]))

    mini = min(point.y for point in polygon.points)
    maxi = max(point.y for point in polygon.points)
    for i in range(mini, maxi):
        actual_line = Segment(SimplePoint(x=0, y=i), SimplePoint(x=1000, y=i))

        intersections = [
            Segment.intersection(actual_line, segment) for segment in segments
            if Segment.is_intersection(actual_line, segment)
        ]
        intersections.sort(key=lambda point: point.x, reverse=True)
        if intersections:
            for i1, i2 in zip(intersections[::2], intersections[1::2]):
                if texture and lights and viewer:
                    for j in range(int(i2.x), int(i1.x)):
                        r, g, b = texture.getpixel((j - min_x, i - min_y))
                        if bump_map:
                            r1, g1, b1 = bump_map.getpixel((j - min_x, i - min_y))
                            norm = Vec3d(r1/128.0 - 1, g1/128.0 - 1, b1/128.0 - 1)
                        else:
                            norm = None
                        color = Color(
                            float(r)/255 *
                            get_light_phong(j, i, viewer, lights, norm=norm),
                            float(g)/255 *
                            get_light_phong(j, i, viewer, lights, norm=norm),
                            float(b)/255 *
                            get_light_phong(j, i, viewer, lights, norm=norm)
                        )
                        put_pixel(
                            j, i, color, canvas, str(hash(token_obj))
                        )
                elif texture:
                    for j in range(int(i2.x), int(i1.x)):
                        r, g, b = texture.getpixel((j - min_x, i - min_y))
                        color = Color(float(r)/255, float(g)/255, float(b)/255)
                        put_pixel(
                            j, i, color, canvas, str(hash(token_obj))
                        )
                elif lights and viewer:
                    for j in range(int(i2.x), int(i1.x)):
                        color = Color(
                            get_light_phong(j, i, viewer, lights),
                            get_light_phong(j, i, viewer, lights),
                            get_light_phong(j, i, viewer, lights)
                        )
                        put_pixel(
                            j, i, color, canvas, str(hash(token_obj))
                        )
                else:
                    draw_line(
                        int(i1.x), i, int(i2.x), i, color, obj=token_obj, canvas=canvas
                    )