ViewportPointToCameraImagePoint() public method

Converts a normalized Unity viewport position into its corresponding normalized position on the color camera image.
public ViewportPointToCameraImagePoint ( Vector2 pos ) : Vector2
pos Vector2 Normalized position for the 3D viewport.
return Vector2
Ejemplo n.º 1
0
    /// <summary>
    /// Estimates the depth of a point on a screen, based on nearest neighbors.
    /// </summary>
    /// <returns>
    /// <c>true</c> if a successful depth estimate was obtained.
    /// </returns>
    /// <param name="cam">The Unity camera.</param>
    /// <param name="pos">The point in pixel coordinates to perform depth estimation.</param>
    /// <param name="colorCameraPoint">
    /// The point (x, y, z), where (x, y) is the back-projection of the UV
    /// coordinates to the color camera space and z is the z coordinate of
    /// the point in the point cloud nearest to the user selection after
    /// projection onto the image plane. If there is not a point cloud point
    /// close to the user selection after projection onto the image plane,
    /// then the point will be set to (0.0, 0.0, 0.0) and isValidPoint will
    /// be set to false.
    /// </param>
    public bool EstimateDepthOnScreen(Camera cam, Vector2 pos, out Vector3 colorCameraPoint)
    {
        // Set up parameters
        Matrix4x4 colorCameraTUnityWorld = m_colorCameraTUnityCamera * cam.transform.worldToLocalMatrix;
        Vector2   normalizedPos          = cam.ScreenToViewportPoint(pos);

        // If the camera has a TangoARScreen attached, it is not displaying the entire color camera image.  Correct
        // the normalized coordinates by taking the clipping into account.
        TangoARScreen arScreen = cam.gameObject.GetComponent <TangoARScreen>();

        if (arScreen != null)
        {
            normalizedPos = arScreen.ViewportPointToCameraImagePoint(normalizedPos);
        }

        bool isValidPoint;
        int  returnType = TangoSupport.ScreenCoordinateToWorldNearestNeighbor(
            m_points,
            m_pointsCount,
            m_depthTimestamp,
            m_colorCameraIntrinsics,
            ref colorCameraTUnityWorld,
            normalizedPos,
            out colorCameraPoint,
            out isValidPoint);

        if (returnType != Common.ErrorType.TANGO_SUCCESS)
        {
            Debug.LogErrorFormat("TangoSupport.ScreenCoordinateToWorldNearestNeighbor failed with error code {0}.",
                                 returnType);
        }

        return((returnType == Common.ErrorType.TANGO_SUCCESS) && isValidPoint);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Given a screen coordinate, find a plane that most closely fits depth values in that area.
    ///
    /// This assumes you are using this in an AR context.
    /// </summary>
    /// <returns><c>true</c>, if plane was found, <c>false</c> otherwise.</returns>
    /// <param name="cam">The Unity camera.</param>
    /// <param name="pos">The point in screen space to perform detection on.</param>
    /// <param name="planeCenter">Filled in with the center of the plane in Unity world space.</param>
    /// <param name="plane">Filled in with a model of the plane in Unity world space.</param>
    public bool FindPlane(Camera cam, Vector2 pos, out Vector3 planeCenter, out Plane plane)
    {
        Matrix4x4 colorCameraTUnityWorld = m_colorCameraTUnityCamera * cam.transform.worldToLocalMatrix;
        Vector2   normalizedPos          = cam.ScreenToViewportPoint(pos);

        // If the camera has a TangoARScreen attached, it is not displaying the entire color camera image.  Correct
        // the normalized coordinates by taking the clipping into account.
        TangoARScreen arScreen = cam.gameObject.GetComponent <TangoARScreen>();

        if (arScreen != null)
        {
            normalizedPos = arScreen.ViewportPointToCameraImagePoint(normalizedPos);
        }

        int returnValue = TangoSupport.FitPlaneModelNearClick(
            m_points, m_pointsCount, m_depthTimestamp, m_colorCameraIntrinsics, ref colorCameraTUnityWorld, normalizedPos,
            out planeCenter, out plane);

        if (returnValue == Common.ErrorType.TANGO_SUCCESS)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Given a screen coordinate, finds a plane that most closely fits the
    /// depth values in that area.
    ///
    /// This function is slow, as it looks at every single point in the point
    /// cloud. Avoid calling this more than once a frame. This also assumes the
    /// Unity camera intrinsics match the device's color camera.
    /// </summary>
    /// <returns><c>true</c>, if a plane was found; <c>false</c> otherwise.</returns>
    /// <param name="cam">The Unity camera.</param>
    /// <param name="pos">The point in screen space to perform detection on.</param>
    /// <param name="planeCenter">Filled in with the center of the plane in Unity world space.</param>
    /// <param name="plane">Filled in with a model of the plane in Unity world space.</param>
    public bool FindPlane(Camera cam, Vector2 pos, out Vector3 planeCenter, out Plane plane)
    {
        if (m_pointsCount == 0)
        {
            // No points to check, maybe not connected to the service yet
            planeCenter = Vector3.zero;
            plane       = new Plane();
            return(false);
        }

        Vector2 normalizedPos = cam.ScreenToViewportPoint(pos);

        // If the camera has a TangoARScreen attached, it is not displaying the entire color camera image.  Correct
        // the normalized coordinates by taking the clipping into account.
        TangoARScreen arScreen = cam.gameObject.GetComponent <TangoARScreen>();

        if (arScreen != null)
        {
            normalizedPos = arScreen.ViewportPointToCameraImagePoint(normalizedPos);
        }

        DVector4 planeModel = new DVector4();

        bool returnValue = TangoSupport.FitPlaneModelNearClick(
            m_mostRecentPointCloud,
            arScreen.m_screenUpdateTime,
            normalizedPos,
            out planeCenter,
            out planeModel);

        planeCenter = m_mostRecentUnityWorldTDepthCamera.MultiplyPoint3x4(planeCenter);
        Vector3 normal = new Vector3((float)planeModel.x,
                                     (float)planeModel.y,
                                     (float)planeModel.z);

        normal = m_mostRecentUnityWorldTDepthCamera.MultiplyVector(normal);
        Vector3.Normalize(normal);
        float distance = (float)planeModel.w / normal.magnitude;

        plane = new Plane(normal, distance);

        return(returnValue);
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Given a screen coordinate, finds a plane that most closely fits the
    /// depth values in that area.
    ///
    /// This function is slow, as it looks at every single point in the point
    /// cloud. Avoid calling this more than once a frame. This also assumes the
    /// Unity camera intrinsics match the device's color camera.
    /// </summary>
    /// <returns><c>true</c>, if a plane was found; <c>false</c> otherwise.</returns>
    /// <param name="cam">The Unity camera.</param>
    /// <param name="pos">The point in screen space to perform detection on.</param>
    /// <param name="planeCenter">Filled in with the center of the plane in Unity world space.</param>
    /// <param name="plane">Filled in with a model of the plane in Unity world space.</param>
    public bool FindPlane(Camera cam, Vector2 pos, out Vector3 planeCenter, out Plane plane)
    {
        if (m_pointsCount == 0)
        {
            // No points to check, maybe not connected to the service yet
            planeCenter = Vector3.zero;
            plane       = new Plane();
            return(false);
        }

        Matrix4x4 colorCameraTUnityWorld = m_colorCameraTUnityCamera * cam.transform.worldToLocalMatrix;
        Vector2   normalizedPos          = cam.ScreenToViewportPoint(pos);

        // If the camera has a TangoARScreen attached, it is not displaying the entire color camera image.  Correct
        // the normalized coordinates by taking the clipping into account.
        TangoARScreen arScreen = cam.gameObject.GetComponent <TangoARScreen>();

        if (arScreen != null)
        {
            normalizedPos = arScreen.ViewportPointToCameraImagePoint(normalizedPos);
        }

        TangoCameraIntrinsics alignedIntrinsics = new TangoCameraIntrinsics();

        VideoOverlayProvider.GetDeviceOrientationAlignedIntrinsics(TangoEnums.TangoCameraId.TANGO_CAMERA_COLOR,
                                                                   alignedIntrinsics);
        int returnValue = TangoSupport.FitPlaneModelNearClick(
            m_points, m_pointsCount, m_depthTimestamp, alignedIntrinsics, ref colorCameraTUnityWorld,
            normalizedPos, out planeCenter, out plane);

        if (returnValue == Common.ErrorType.TANGO_SUCCESS)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 5
0
    /// <summary>
    /// Estimates the depth of a point on a screen, based on nearest neighbors.
    /// </summary>
    /// <returns>
    /// <c>true</c> if a successful depth estimate was obtained.
    /// </returns>
    /// <param name="cam">The Unity camera.</param>
    /// <param name="pos">The point in pixel coordinates to perform depth estimation.</param>
    /// <param name="colorCameraPoint">
    /// The point (x, y, z), where (x, y) is the back-projection of the UV
    /// coordinates to the color camera space and z is the z coordinate of
    /// the point in the point cloud nearest to the user selection after
    /// projection onto the image plane. If there is not a point cloud point
    /// close to the user selection after projection onto the image plane,
    /// then the point will be set to (0.0, 0.0, 0.0) and isValidPoint will
    /// be set to false.
    /// </param>
    public bool EstimateDepthOnScreen(Camera cam, Vector2 pos, out Vector3 colorCameraPoint)
    {
        // Set up parameters
        Matrix4x4 colorCameraTUnityWorld = TangoSupport.COLOR_CAMERA_T_UNITY_CAMERA * cam.transform.worldToLocalMatrix;
        Vector2   normalizedPos          = cam.ScreenToViewportPoint(pos);

        // If the camera has a TangoARScreen attached, it is not displaying the entire color camera image.  Correct
        // the normalized coordinates by taking the clipping into account.
        TangoARScreen arScreen = cam.gameObject.GetComponent <TangoARScreen>();

        if (arScreen != null)
        {
            normalizedPos = arScreen.ViewportPointToCameraImagePoint(normalizedPos);
        }

        bool returnValue = TangoSupport.ScreenCoordinateToWorldNearestNeighbor(
            m_mostRecentPointCloud,
            arScreen.m_screenUpdateTime,
            normalizedPos,
            out colorCameraPoint);

        return(returnValue);
    }
Ejemplo n.º 6
0
    /// <summary>
    /// Minji Kim 2017.09.25
    /// Find Edge Wrapper
    /// call FindEdgesNearPoint
    /// TangoSupport_findEdgesNearPoint
    /// </summary>
    /// <param name="cam"></param>
    /// <param name="pos"></param>
    /// <returns></returns>
    public bool FindEdges(TangoUnityImageData imageBuffer,
                          Camera cam, Vector2 pos, out TangoSupport.TangoSupportEdge[] edges, out int num_edges)
    //out Vector3[] end_points, out Vector3 nearest_on_edge)
    {
        if (m_pointsCount == 0)
        {
            // No points to check, maybe not connected to the service yet
            edges     = new TangoSupport.TangoSupportEdge[1];
            num_edges = 0;
            return(false);
        }

        Vector2 normalizedPos = cam.ScreenToViewportPoint(pos);

        // If the camera has a TangoARScreen attached, it is not displaying the entire color camera image.  Correct
        // the normalized coordinates by taking the clipping into account.
        TangoARScreen arScreen = cam.gameObject.GetComponent <TangoARScreen>();

        if (arScreen != null)
        {
            normalizedPos = arScreen.ViewportPointToCameraImagePoint(normalizedPos);
        }

        // if the image data has not been updated, update it manually
        if (imageBuffer.data == null)
        {
            GetTangoUnityImageData(ref imageBuffer);
        }

        bool returnValue = TangoSupport.FindEdgesNearPoint(
            imageBuffer,
            m_mostRecentPointCloud,
            arScreen.m_screenUpdateTime,
            normalizedPos,
            out edges,
            out num_edges
            );

        Vector3[] start = new Vector3[num_edges];
        Vector3[] end   = new Vector3[num_edges];
        Vector3[] near  = new Vector3[num_edges];
        TangoSupport.TangoSupportEdge[] n_edges = new TangoSupport.TangoSupportEdge[num_edges];
        for (int j = 0; j < num_edges; j++)
        {
            start[j] = new Vector3(edges[j].end_points_x1, edges[j].end_points_y1, edges[j].end_points_z1);
            end[j]   = new Vector3(edges[j].end_points_x2, edges[j].end_points_y2, edges[j].end_points_z2);
            near[j]  = new Vector3(edges[j].closest_point_on_edge_x, edges[j].closest_point_on_edge_y,
                                   edges[j].closest_point_on_edge_z);
            start[j] = m_mostRecentUnityWorldTDepthCamera.MultiplyVector(start[j]);
            end[j]   = m_mostRecentUnityWorldTDepthCamera.MultiplyVector(end[j]);
            near[j]  = m_mostRecentUnityWorldTDepthCamera.MultiplyVector(near[j]);
            Vector3.Normalize(start[j]);
            Vector3.Normalize(end[j]);
            Vector3.Normalize(near[j]);
            n_edges[j].end_points_x1           = start[j][0];
            n_edges[j].end_points_y1           = start[j][1];
            n_edges[j].end_points_z1           = start[j][2];
            n_edges[j].end_points_x2           = end[j][0];
            n_edges[j].end_points_y2           = end[j][1];
            n_edges[j].end_points_z2           = end[j][2];
            n_edges[j].closest_point_on_edge_x = near[j][0];
            n_edges[j].closest_point_on_edge_y = near[j][1];
            n_edges[j].closest_point_on_edge_z = near[j][2];
        }
        edges = n_edges;

        return(returnValue);
    }