def test_get_insert_args(self): assert Polyline2D._get_insert_args(_POLYLINE_INFO_1, _POLYLINE_INFO_2) == ( [(2, Vector2D(3.0, 3.0))], [(1, Vector2D(3.0, 2.0)), (2, Vector2D(5.0, 4.0))], )
def test_new_class(self): with pytest.raises(TypeError): Vector([1, 2]) with pytest.raises(TypeError): Vector(1) with pytest.raises(TypeError): Vector(1, 2, 3, 4) assert Vector(1, 2) == Vector2D(1, 2) assert Vector(1, 2, 3) == Vector3D(1, 2, 3) assert Vector(*[1, 2]) == Vector2D(1, 2)
def test_project(self): camera_intrinsics = CameraIntrinsics( camera_matrix=[[1, 0, 0], [0, 1, 0], [0, 0, 1]], p1=1, p2=1, k1=1 ) point_1_fisheye_true = camera_intrinsics.project((1, 2), is_fisheye=True) point_2_fisheye_true = camera_intrinsics.project((1, 2, 3), is_fisheye=True) assert point_1_fisheye_true == Vector2D(1.195033740719647, 2.390067481439294) assert point_2_fisheye_true == Vector2D(0.4039719111977248, 0.8079438223954496) point_1_fisheye_false = camera_intrinsics.project((1, 2), is_fisheye=False) point_2_fisheye_false = camera_intrinsics.project((1, 2, 3), is_fisheye=False) assert point_1_fisheye_false == Vector2D(17.0, 29.0) assert point_2_fisheye_false == Vector2D(1.740740740740741, 2.9259259259259256)
def test_project(self): camera_matrix = CameraMatrix(matrix=_3x3_MATRIX) with pytest.raises(TypeError): camera_matrix.project([]) with pytest.raises(TypeError): camera_matrix.project([1]) with pytest.raises(TypeError): camera_matrix.project([1, 1, 1, 1]) with pytest.raises(ZeroDivisionError): camera_matrix.project([0, 0, 0]) assert camera_matrix.project([0, 0]) == Vector2D(3, 5) assert camera_matrix.project([1, 2]) == Vector2D(8, 13) assert camera_matrix.project([1, 2, 4]) == Vector2D(4.25, 7.0)
def test_loads(self): labeledpolygonline2d = LabeledPolyline2D.loads( _LABELED_POLYLINE2D_CONTENTS) assert labeledpolygonline2d[0] == Vector2D(1, 2) assert labeledpolygonline2d.category == "cat" assert labeledpolygonline2d.attributes == {"gender": "male"} assert labeledpolygonline2d.instance == "12345"
def test_init(self): labeledpolyline2d = LabeledPolyline2D([(1, 2)], category="cat", attributes={"gender": "male"}, instance="12345") assert labeledpolyline2d[0] == Vector2D(1, 2) assert labeledpolyline2d.category == "cat" assert labeledpolyline2d.attributes == {"gender": "male"} assert labeledpolyline2d.instance == "12345"
def test_distort(self): distortion_coefficients = DistortionCoefficients(p1=1.0, p2=2.0, k1=3.0, k2=4.0) with pytest.raises(TypeError): distortion_coefficients.distort((1.0, 2.0, 3.0, 4.0), is_fisheye=True) distored_2d_fisheye_true_1 = distortion_coefficients.distort((1.0, 2.0), is_fisheye=True) distored_2d_fisheye_true_2 = distortion_coefficients.distort( (1.0, 2.0, 3.0), is_fisheye=True ) assert distored_2d_fisheye_true_1 == Vector2D(6.158401093771876, 12.316802187543752) assert distored_2d_fisheye_true_2 == Vector2D(0.83187700005734, 1.66375400011468) distored_2d_fisheye_false_1 = distortion_coefficients.distort((1.0, 2.0), is_fisheye=False) distored_2d_fisheye_false_2 = distortion_coefficients.distort( (1.0, 2.0, 3.0), is_fisheye=False ) assert distored_2d_fisheye_false_1 == Vector2D(134.0, 253.0) assert distored_2d_fisheye_false_2 == Vector2D(3.3004115226337447, 4.934156378600823)
def test_and(self): box2d_1 = Box2D(1, 2, 3, 4) box2d_2 = Box2D(2, 3, 4, 5) vector2d = Vector2D(1, 2) user_sequence = UserSequence() user_sequence._data = ["a", "b", "c", "d"] assert (box2d_1 & box2d_2) == Box2D(2, 3, 3, 4) assert box2d_1.__and__([1, 2, 3]) == NotImplemented assert box2d_1.__and__(vector2d) == NotImplemented assert box2d_1.__and__(user_sequence) == NotImplemented
def distort(self, point: Sequence[float], is_fisheye: bool = False) -> Vector2D: """Add distortion to a point. Arguments: point: A Sequence containing the coordinates of the point to be distorted. is_fisheye: Whether the sensor is fisheye camera, default is False. Raises: TypeError: When the dimension of the input point is neither two nor three. Returns: Distorted 2d point. Examples: Distort a point with 2 dimensions >>> distortion_coefficients.distort((1.0, 2.0)) Vector2D(134.0, 253.0) Distort a point with 3 dimensions >>> distortion_coefficients.distort((1.0, 2.0, 3.0)) Vector2D(3.3004115226337447, 4.934156378600823) Distort a point with 2 dimensions, fisheye is True >>> distortion_coefficients.distort((1.0, 2.0), is_fisheye=True) Vector2D(6.158401093771876, 12.316802187543752) """ # pylint: disable=invalid-name if len(point) == 3: x = point[0] / point[2] y = point[1] / point[2] elif len(point) == 2: x = point[0] y = point[1] else: raise TypeError( "The point to be projected must have 2 or 3 dimensions") x2 = x**2 y2 = y**2 xy2 = 2 * x * y r2 = x2 + y2 radial_distortion = self._calculate_radial_distortion(r2, is_fisheye) tangential_distortion = self._calculate_tangential_distortion( r2, x2, y2, xy2, is_fisheye) x = x * radial_distortion + tangential_distortion[0] y = y * radial_distortion + tangential_distortion[1] return Vector2D(x, y)
def test_init(self): assert MultiPolyline2D() == MultiPolyline2D([]) assert MultiPolyline2D(_MULTI_POLYLINE_SEQUENCE) == MultiPolyline2D([ [Vector2D(1, 1), Vector2D(2, 2), Vector2D(4, 4), Vector2D(5, 5)], [Vector2D(2, 1), Vector2D(4, 3), Vector2D(6, 5)], ])
def test_init(self): with pytest.raises(TypeError): Vector2D(1, 2, 3) with pytest.raises(TypeError): Vector2D([1, 2]) with pytest.raises(TypeError): Vector2D() assert Vector2D(*[1, 2]) == Vector2D(1, 2) assert Vector2D(x=1, y=1) == Vector2D(1, 1) vector_2d = Vector2D(1, 2) assert vector_2d.x == 1 assert vector_2d.y == 2
def test_init(self): with pytest.raises(TypeError): Box2D() with pytest.raises(TypeError): Box2D(1) with pytest.raises(TypeError): Box2D(x=0) with pytest.raises(TypeError): Box2D() with pytest.raises(TypeError): Box2D([1, 2, 3, 4]) assert Box2D(*[1, 2, 3, 4]) == Box2D(1, 2, 3, 4) box2d = Box2D(1, 2, 3, 4) assert box2d.xmin == 1 assert box2d.ymin == 2 assert box2d.xmax == 3 assert box2d.ymax == 4 assert box2d.tl == Vector2D(1, 2) assert box2d.br == Vector2D(3, 4) assert box2d.width == 2 assert box2d.height == 2
def test_loads(self): content = { "polygon": [ { "x": 1, "y": 2 }, ], "category": "cat", "attributes": { "gender": "male" }, "instance": 12345, } labeledpolygon = LabeledPolygon.loads(content) assert labeledpolygon[0] == Vector2D(1, 2) assert labeledpolygon.category == "cat" assert labeledpolygon.attributes == {"gender": "male"} assert labeledpolygon.instance == 12345
def project(self, point: Sequence[float]) -> Vector2D: """Project a point to the pixel coordinates. Arguments: point: A Sequence containing the coordinates of the point to be projected. Returns: The pixel coordinates. Raises: TypeError: When the dimension of the input point is neither two nor three. Examples: Project a point in 2 dimensions >>> camera_matrix.project([1, 2]) Vector2D(12, 19) Project a point in 3 dimensions >>> camera_matrix.project([1, 2, 4]) Vector2D(6.0, 10.0) """ if len(point) == 3: x = point[0] / point[2] y = point[1] / point[2] elif len(point) == 2: x = point[0] y = point[1] else: raise TypeError( "The point to be projected must have 2 or 3 dimensions") x = self.fx * x + self.skew * y + self.cx y = self.fy * y + self.cy return Vector2D(x, y)
def test_init(self): sequence = [[1, 1], [1, 2], [2, 2]] assert Polyline2D() == Polyline2D([]) assert Polyline2D(sequence) == Polyline2D( [Vector2D(1, 1), Vector2D(1, 2), Vector2D(2, 2)])
def test_neg(self): vector = Vector(1, 1) assert -vector == Vector2D(-1, -1)
def test_neg(self): keypoint = Keypoint2D(1, 1, 1) assert -keypoint == Vector2D(-1, -1)
def test_dumps(self): vector = Vector2D(1, 2) assert vector.dumps() == _DATA[0]
def test_loads(self): vector = Vector2D.loads(_DATA[0]) assert vector._data == (1, 2)
def test_init(self): sequence = [[1, 1], [1, 2], [2, 2]] assert Polyline2D() == Polyline2D([]) result = Polyline2D([Vector2D(1, 1), Vector2D(1, 2), Vector2D(2, 2)]) assert Polyline2D(sequence) == result assert Polyline2D(np.array(sequence)) == result
from tensorbay.geometry import Box2D, MultiPolyline2D, Polyline2D, Vector2D _POLYLINE_SEQUENCE_1 = [[1, 1], [2, 2], [4, 4], [5, 5]] _POLYLINE_SEQUENCE_2 = [[2, 1], [4, 3], [6, 5]] _POLYLINE_1 = Polyline2D(_POLYLINE_SEQUENCE_1) _POLYLINE_2 = Polyline2D(_POLYLINE_SEQUENCE_2) _MULTI_POLYLINE_SEQUENCE = [_POLYLINE_SEQUENCE_1, _POLYLINE_SEQUENCE_2] _MULTI_POLYLINE = MultiPolyline2D([_POLYLINE_SEQUENCE_1, _POLYLINE_SEQUENCE_2]) _POLYLINE_INFO_1 = ( { "index": 0, "point": Vector2D(1, 1), "vector": Vector2D(1, 1), "time": 0.25, "last_time": 0, }, { "index": 1, "point": Vector2D(2, 2), "vector": Vector2D(2, 2), "time": 0.75, "last_time": 0.25, }, { "index": 2, "point": Vector2D(4, 4), "vector": Vector2D(1, 1),
def test_get_insert_arg(self): assert Polyline2D._get_insert_arg( 0.2, _POLYLINE_INFO_1[0]) == (1, Vector2D(1.8, 1.8)) assert Polyline2D._get_insert_arg( 0.8, _POLYLINE_INFO_1[1]) == (2, Vector2D(4.2, 4.2))