vuforia 之 Target space 和 screen space 之间的转换方法

最近在研究 【高通 AR 】技术,通过其Examples来学习相关SDK。

在研究 ImageTagets 的时候,想获取:

1、目标空间对应的3D point 对应的 屏幕上2D point ; 

2、屏幕上触碰一点 对应的 空间坐标。

虽然其开发文档:https://developer.vuforia.com/resources/dev-guide/screen-coordinates 对这2个需求进行了实现方式阐述,但是都不是很具体。所以花了不少时间去实现(尤其是需求1)。现将 空间坐标 转换成 屏幕坐标的方法总结如下:


Target space to screen space

//xq
- (CGPoint)getScreenPointByPose:(QCAR::Matrix34F)pose
{
    // need to account for the orientation on view size
    CGFloat viewWidth = self.frame.size.height; // Portrait
    CGFloat viewHeight = self.frame.size.width; // Portrait
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsLandscape(orientation))
    {
        viewWidth = self.frame.size.width;
        viewHeight = self.frame.size.height;
    }
    
    // calculate any mismatch of screen to video size
    QCAR::CameraDevice& cameraDevice = QCAR::CameraDevice::getInstance();
    const QCAR::CameraCalibration& cameraCalibration = cameraDevice.getCameraCalibration();
    QCAR::VideoMode videoMode = cameraDevice.getVideoMode(QCAR::CameraDevice::MODE_DEFAULT);
    
    CGFloat scale = viewWidth/videoMode.mWidth;
    if (videoMode.mHeight * scale < viewHeight)
        scale = viewHeight/videoMode.mHeight;
    CGFloat scaledWidth = videoMode.mWidth * scale;
    CGFloat scaledHeight = videoMode.mHeight * scale;
    
    CGPoint margin = {(scaledWidth - viewWidth)/2, (scaledHeight - viewHeight)/2};
    CGPoint center =[self projectCoord:CGPointMake(0,0) inView:cameraCalibration andPose:pose withOffset:margin andScale:scale];
    NSLog(@"center = %@",[NSValue valueWithCGPoint:center]);
    
    CGPoint centerNew = center;
    centerNew.x = viewHeight - center.y;
    centerNew.y = center.x;
    NSLog(@"centerNew = %@",[NSValue valueWithCGPoint:centerNew]);
   
    return  centerNew;
    
}

- (CGPoint) projectCoord:(CGPoint)coord inView:(const QCAR::CameraCalibration&)cameraCalibration andPose:(QCAR::Matrix34F)pose withOffset:(CGPoint)offset andScale:(CGFloat)scale
{
    CGPoint converted;
    
    QCAR::Vec3F vec(coord.x,coord.y,0);
    QCAR::Vec2F sc = QCAR::Tool::projectPoint(cameraCalibration, pose, vec);
    converted.x = sc.data[0]*scale - offset.x;
    converted.y = sc.data[1]*scale - offset.y;
    
    return converted;
}

其中:方法一中传入参数  pose   - ( void )renderFrameQCAR 方法获取,即被跟踪到目标的 3*4姿态矩阵。(详见 :https://developer.vuforia.com/resources/dev-guide/pose-matrix-explained) ,返回值 centerNew 即为 屏幕上对应的坐标。








你可能感兴趣的:(screen,高通,space,vuforia)