Exemplo n.º 1
0
    def reproject(self,
                  vector: Vector,
                  boundary=500,
                  filter=True) -> Union[Shape, None]:
        original_vertices = vector.vertices().copy()
        # Convert to camera coord.
        projected_vertices = \
            original_vertices.dot(self._T_world_to_camera[:3, :3].T) + \
            self._T_world_to_camera[:3, 3].T

        # Filter backward vertices.
        if filter:
            forward_mask = projected_vertices[:, 2] > 0.0
            projected_vertices = projected_vertices[forward_mask]
            original_vertices = original_vertices[forward_mask]

        # Project to image coord.
        projected_vertices = projected_vertices.dot(self._K.T)
        projected_vertices = \
            projected_vertices[:, :2] / projected_vertices[:, [2]]

        # Clipping.
        if filter and boundary > 0:
            clipping_mask = \
                (projected_vertices[:, 0] >= -boundary) & \
                (projected_vertices[:, 1] >= -boundary) & \
                (projected_vertices[:, 0] < self._image_width + boundary) & \
                (projected_vertices[:, 1] < self._image_height + boundary)
            projected_vertices = projected_vertices[clipping_mask]
            original_vertices = original_vertices[clipping_mask]

        if isinstance(vector, (Polyline3D, Polygon3D)) and \
                len(projected_vertices) < 2:
            # Make sure at least show a segment of polyline and polygon
            # vector.
            return
        elif isinstance(vector, Point3D) and len(projected_vertices) == 0:
            # Make sure the point correspondence in the screen.
            return

        shape_class = self.get_shape_class(vector.__class__)
        shape = shape_class(projected_vertices)
        shape.set_origin_vertices(original_vertices)
        return shape