def get_angle(p, q, r): """ Compute angle between 3 points Function coming from https://github.com/pgRouting/pgrouting/blob/master/src/alpha_shape/alpha.c :param p: Point :param q: Point :param r: Point :return: angle """ logger = logging.getLogger("my_hull") try: from CGAL import CGAL_Alpha_shape_2 from CGAL.CGAL_Kernel import Point_2, Polygon_2, Vector_2 except: logger.error("CGAL library must be install to use this option, use an other HULL_METHOD") raise v1 = Vector_2(q, p) v2 = Vector_2(q, r) crossproduct = v1.x() * v2.y() - v1.y() * v2.x(); dot = v1.x() * v2.x() + v1.y() * v2.y(); angle = math.atan2(crossproduct, dot) if angle < 0.0: angle += 2 * np.pi return angle
def test_2d(): print("2D Tests") p1 = Point_2(0, 0) p2 = Point_2(1, 2) v1 = Vector_2(1, 1) v2 = Vector_2(1, 2) # operations on points assertion(p1 + v2 == p2, 1) assertion(p1 - v2 < p2, 2) assertion(p2 - p1 == v2, 3) assertion(p2 - ORIGIN == v2, 3.1) assertion(p2 > p1, 4) assertion(p2 >= p2, 5) assertion(p2 <= p2, 6) assertion(p2 != p1, 7) # operations on vector assertion(v1 + v2 == Vector_2(2, 3), 8) assertion(v1 - v2 == Vector_2(0, -1), 9) assertion(v2 * v1 == 3, 10) assertion(v2 * 2 == Vector_2(2, 4), 11) assertion(2 * v2 == Vector_2(2, 4), "11 bis") assertion(v1 / 2 == Vector_2(0.5, 0.5), 12) assertion(v1 / 2.0 == Vector_2(0.5, 0.5), 12) assertion(-v2 == Vector_2(-1, -2), 13) assertion(v2 != v1, 14) # operations on ORIGIN/NULL_VECTOR assertion(ORIGIN - p2 == -v2, 14) assertion(ORIGIN + v2 == p2, 15) assertion(p1 == ORIGIN, 15.1) assertion(p1 - p1 == NULL_VECTOR, 15.2) # test inplace operations pt_tmp = p1.deepcopy() pt_tmp += v2 assertion(pt_tmp == p2, 16) vect_tmp = Vector_2(2, 3) vect_tmp -= v1 assertion(vect_tmp == v2, 17)
def same_direction_as_constraint(v0, v1): s = Vector_2(v0.point(), v1.point()) if s * vector_direction > 0: return (v0, v1) elif s * vector_direction < 0: return (v1, v0) else: raise ValueError( "These two vectors are orthogonal ({va}, {vb}) and ({v0}, {v1}])" .format(va=va.point(), vb=vb.point(), v0=v0.point(), v1=v1.point()))
def iter_edges_for_input_constraint(self, va, vb): # CAUTION the CGAL method vertices_in_constraint does NOT # always return the vertices in the order from va to vb vector_direction = Vector_2(va.point(), vb.point()) def same_direction_as_constraint(v0, v1): s = Vector_2(v0.point(), v1.point()) if s * vector_direction > 0: return (v0, v1) elif s * vector_direction < 0: return (v1, v0) else: raise ValueError( "These two vectors are orthogonal ({va}, {vb}) and ({v0}, {v1}])" .format(va=va.point(), vb=vb.point(), v0=v0.point(), v1=v1.point())) for v0, v1 in ilinks(self.cdt.vertices_in_constraint(va, vb)): yield same_direction_as_constraint(v0, v1)
def crossProduct(vec1: Vector, vec2: Vector) -> Vector3D: vec1_3d = Vector3D(vec1.x(), vec1.y(), 0) vec2_3d = Vector3D(vec2.x(), vec2.y(), 0) return _cross_product(vec1_3d, vec2_3d)