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 即为 屏幕上对应的坐标。








  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值