def test_line(self): p1, p2, p3 = Point2(0, 0), Point2(1, 0), Point2(0, 1) l1, l2 = Line2(p1, p2), Line2(p1, p3) self.assertEqual(l1, l1) self.assertTrue(l1 is l1) self.assertNotEqual(l1, l2) ip = intersection(l1, l2) self.assertEqual(ip, Point2(0, 0)) self.assertTrue(do_intersect(l1, l2)) il = intersection(l1, l1) self.assertEqual(l1, il)
def draw_rv(): segments = [] for i in range(10): segments.append(sg.Segment2(sg.Point2(r(), r()), sg.Point2(r(), r()))) intersections = [] for s1, s2 in itertools.permutations(segments, 2): isect = sg.intersection(s1, s2) if isect: intersections.append(isect) for s in segments: draw(s) for i in intersections: draw(i) HEIGHT = 10 THETA_H = 45 THETA_V = 60 pt_h_r = sg.Point2(HEIGHT * math.tan(THETA_H * math.pi / 180), 0) pt_v_r = sg.Point2(HEIGHT * math.tan(THETA_V * math.pi / 180), 0) print(dir(sg.Transformation2)) pt_v_r = pt_v_r.transform(sg.Transformation2.Rotation(3, math.pi/2)) pt_r = sg.Segment2(pt_h_r, pt_v_r) draw(pt_h_r) draw(pt_v_r) draw(pt_r) plt.show()
def split_by_plane(self, node): plane = node.get_plane_line() inter = skgeom.intersection(plane, skgeom.Segment2(skgeom.Point2(self.x1, self.y1), skgeom.Point2(self.x2, self.y2))) #print(inter) px1 = node.partition_x_coord px2 = px1 + node.dx py1 = node.partition_y_coord py2 = py1 + node.dy (ix, iy) = intersect_portal_by_plane(self.x1, self.y1, self.x2, self.y2, px1, py1, px2, py2) #plane.x1, plane.y1, plane.x2, plane.y2) p1 = Point(self.x1, self.y1) p2 = Point(self.x2, self.y2) p1_class = p1.classify_against_plane(node) p2_class = p2.classify_against_plane(node) p1_to_intersection = Line(self.x1, self.y1, ix, iy) intersection_to_p2 = Line(ix, iy, self.x2, self.y2) if p1_class == IN_FRONT and p2_class == BEHIND: return (p1_to_intersection, intersection_to_p2) elif p1_class == BEHIND and p2_class == IN_FRONT: return (intersection_to_p2, p1_to_intersection) else: raise Exception("This shouldnt happen")
def generate_frustum_between_portals(last_portal_frustum, new_portal, level_bounds): (last_portal, _, _, _, _) = last_portal_frustum (last_pt1, last_pt2, _) = last_portal (min_x, max_x, min_y, max_y) = level_bounds bounds_width = max_x - min_x bounds_height = max_y - min_y max_bounds_dim = max(bounds_width, bounds_height) (new_pt1, new_pt2, _) = new_portal l1 = Segment2(last_pt1, new_pt2) l2 = Segment2(last_pt2, new_pt1) l1_vec = l1.to_vector() l2_vec = l2.to_vector() intersection = skgeom.intersection(l1, l2) #print("Got intersection: ", intersection) if not isinstance(intersection, Point2): return None assert isinstance(intersection, Point2) scale = skgeom.Transformation2(skgeom.SCALING, max_bounds_dim*2) scaled_l1_vec = l1_vec.transform(scale) scaled_l2_vec = l2_vec.transform(scale) scaled_l1_seg = Segment2(intersection, intersection + scaled_l1_vec) scaled_l2_seg = Segment2(intersection, intersection + scaled_l2_vec) poly = Polygon([point2ToPoint(intersection), point2ToPoint(scaled_l1_seg.point(1)), point2ToPoint(scaled_l2_seg.point(1))]) bounds_poly = Polygon([Point([min_x, min_y]), Point([max_x, min_y]), Point([max_x, max_y]), Point([min_x, max_y])]) #clipped_poly = poly.intersection(bounds_poly) #assert isinstance(clipped_poly, Polygon) return (new_portal, FrustumOrigin(intersection), FrustumLeftLine(scaled_l1_seg), FrustumRightLine(scaled_l2_seg), FrustumPolygon(poly)) #clipped_poly))
def calc_rv(): a = sg.Segment3(sg.Point3(3, 5, 2), sg.Point3(0, -2, 2)) b = sg.Segment3(sg.Point3(5, 3, 2), sg.Point3(-2, -2, 2)) v = sg.Ray3(sg.Point3(0, 0, 10), sg.Vector3(0, 0, -2)) gnd = sg.Plane3(sg.Point3(0, 0, 0), sg.Point3(4, 4, 0), sg.Point3(0, 4, 0)) # print(dir(v)) print(v.transform(sg.Transformation3())) # print(b.perpendicular(sg.Sign.COUNTERCLOCKWISE)) i = sg.intersection(gnd, v) print(i)
def intersect_portal_by_plane(portal_x1, portal_y1, portal_x2, portal_y2, plane_x1, plane_y1, plane_x2, plane_y2): """ returns a (x, y) tuple or None if there is no intersection """ portal_seg = skgeom.Segment2(skgeom.Point2(portal_x1, portal_y1), skgeom.Point2(portal_x2, portal_y2)) plane_line = skgeom.Segment2(skgeom.Point2(plane_x1, plane_y1), skgeom.Point2(plane_x2, plane_y2)).supporting_line() inter = skgeom.intersection(plane_line, portal_seg) #print(inter) if inter: return (inter.x(), inter.y()) """
def clip_frustum_against_level_bounds(left_side, right_side, bound_lines): INSIDE = 0 OUTSIDE = 1 cur_lines = [left_side, right_side] state = INSIDE for line in level_bound_lines: intersection = skgeom.intersection(left_ray, line) print(intersection)
def classify_against_plane(self, node): plane_line = node.get_plane_line() self_seg = skgeom.Segment2(skgeom.Point2(self.x1, self.y1), skgeom.Point2(self.x2, self.y2)) inter = skgeom.intersection(plane_line, self_seg) if inter: if isinstance(inter, skgeom.Segment2): return COINCIDENT else: return SPANNING else: num_positive = 0 num_negative = 0 #print("-- classifying point --") for point in [Point(self.x1,self.y1), Point(self.x2, self.y2)]: #proj = plane_line.projection(skgeom.Point2(point.x, point.y)) #print("projection: {}".format(proj)) val = point.classify_against_plane(node) if val == IN_FRONT: #print("one point in front") num_positive += 1 elif val == BEHIND: #print("one point behind") num_negative += 1 #print("-- done --\n") if num_positive == 0 and num_negative > 0: return BEHIND if num_positive > 0 and num_negative > 0: return SPANNING #print(self) #print(node) #raise Exception("This should not happen") # return SPANNING if num_positive == 2: return IN_FRONT elif num_negative == 2: return BEHIND return COINCIDENT
def get_segs_on_other_side_of_seg(seg, linedef_idx_to_seg_idxs_map, seg_idx_to_ssect_idx_map, level_data): linedef_idx = seg.linedef linedef = level_data['LINEDEFS'][linedef_idx] vertexes = level_data['VERTEXES'] src_direction = seg.direction seg_idxs_on_linedef = linedef_idx_to_seg_idxs_map[linedef_idx] seg_v1 = vertexes[seg.begin_vert] seg_v2 = vertexes[seg.end_vert] res = [] all_segs = level_data['SEGS'] other_side_seg_idxs = [si for si in seg_idxs_on_linedef if all_segs[si].direction != src_direction] other_side_segs = [(si,all_segs[si]) for si in other_side_seg_idxs] sorted_other_side_segs = sorted(other_side_segs, key=lambda s: s[1].begin_vert, reverse=True) idx_to_point2 = {} cnt_to_idx = {} cnt = 0 idx_to_point2[seg.begin_vert] = Point2(cnt, 0) cnt_to_idx[cnt] = seg.begin_vert cnt += 1 for (si,other_seg) in sorted_other_side_segs: ov1_idx = other_seg.begin_vert ov2_idx = other_seg.end_vert if ov2_idx not in idx_to_point2: idx_to_point2[ov2_idx] = Point2(cnt, 0) cnt_to_idx[cnt] = ov2_idx cnt += 1 if ov1_idx not in idx_to_point2: idx_to_point2[ov1_idx] = Point2(cnt, 0) cnt_to_idx[cnt] = ov1_idx cnt += 1 if seg.end_vert not in idx_to_point2: idx_to_point2[seg.end_vert] = Point2(cnt, 0) cnt_to_idx[cnt] = seg.end_vert cnt += 1 #print(idx_to_point2) #src_seg2 = Segment2(vertexToPoint2(seg_v1), vertexToPoint2(seg_v2)) src_seg2 = Segment2(idx_to_point2[seg.begin_vert], idx_to_point2[seg.end_vert]) #src_seg = LineString([idx_to_point2[seg.begin_vert], idx_to_point2[seg.end_vert]]) for (other_seg_idx, other_seg) in other_side_segs: #other_seg = level_data['SEGS'][other_seg_idx] #if other_seg.direction == src_direction: # continue #other_v1 = vertexes[other_seg.begin_vert] #other_v2 = vertexes[other_seg.end_vert] other_seg2 = Segment2(idx_to_point2[other_seg.begin_vert], idx_to_point2[other_seg.end_vert]) #other_seg = LineString([idx_to_point2[other_seg.begin_vert], idx_to_point2[other_seg.end_vert]]) intersection = skgeom.intersection(src_seg2, other_seg2) #intersection = src_seg.intersection(other_seg) #print("Got intersection {}".format(intersection)) if isinstance(intersection, Segment2): #if isinstance(intersection, LineString) and len(intersection.coords) > 0: tgt_ssect_idx = seg_idx_to_ssect_idx_map[other_seg_idx] cnts = [intersection.point(0).x(), intersection.point(1).x()] sorted_cnts = sorted(cnts) cnt1 = sorted_cnts[0] cnt2 = sorted_cnts[1] idx1 = cnt_to_idx[float(cnt1)] idx2 = cnt_to_idx[float(cnt2)] p1 = vertexToPoint2(vertexes[idx1]) p2 = vertexToPoint2(vertexes[idx2]) res.append((p1, p2, tgt_ssect_idx)) return res
s = Segment2(a, b) p = Segment2(c, d) print(type(2)) print(barycenter(a, 2, b, 12)) points = [ Point2(rnd.uniform(-10, 10), rnd.uniform(-10, 10)) for i in range(0, 100) ] draw(points) cvh = skgeom.convex_hull.graham_andrew(points) draw(Polygon(cvh)) plt.axis('equal') plt.show() print(skgeom.intersection(s, p)) poly = skgeom.Polygon([a, c, b, d]) print(poly) print("Poly a simple: ", poly.is_simple()) print(poly.vertices) for v in poly.vertices: print(v) h = [Point2(1, 1), Point2(1, 2), Point2(2, 1)] for v in poly.vertices: print(v) print(list(poly.vertices))