在横向模式下使用Core Motion

时间:2012-11-17 16:08:44

标签: ios augmented-reality core-motion

我目前正致力于增强现实,为此我想使用陀螺仪和Core Motion。我研究过Apple pARk示例代码,我理解我花在阅读文档上的大部分数学,因为乍一看它还不清楚!一切都很好,直到我试图让它在横向模式下工作。

我不会在这里解释所有的理论太久了。但是对于那些经历过它的人来说,我的问题是,我们采用姿态的旋转矩阵将这个旋转应用到我们的坐标。好的,直到这里它都没问题,但似乎Core Motion不适应它的风景模式。我在这个问题上看到了类似的问题,但看起来似乎没有人有解决方案。

所以我试着自己做,这就是我的想法:

每次我们将设备旋转到横向时,都会旋转+ -90°(取决于横向左侧或右侧)。我决定创建一个4X4旋转矩阵来应用这个旋转。然后将它乘以cameraTransform矩阵(将姿态的3X3 CMRotationMatrix调整为4X4),然后我们获得矩阵cameraTransformRotated:

- (void)createMatLandscape{
switch(cameraOrientation){
    case UIDeviceOrientationLandscapeLeft:
        landscapeRightTransform[0] = cos(degreesToRadians(90));
        landscapeRightTransform[1] = -sin(degreesToRadians(90));
        landscapeRightTransform[2] = 0;
        landscapeRightTransform[3] = 0;

        landscapeRightTransform[4] = sin(degreesToRadians(90));
        landscapeRightTransform[5] = cos(degreesToRadians(90));
        landscapeRightTransform[6] = 0;
        landscapeRightTransform[7] = 0;

        landscapeRightTransform[8] = 0;
        landscapeRightTransform[9] = 0;
        landscapeRightTransform[10] = 1;
        landscapeRightTransform[11] = 0;

        landscapeRightTransform[12] = 0;
        landscapeRightTransform[13] = 0;
        landscapeRightTransform[14] = 0;
        landscapeRightTransform[15] = 1;

        multiplyMatrixAndMatrix(cameraTransformRotated, cameraTransform, landscapeRightTransform);
        break;
    case UIDeviceOrientationLandscapeRight:
        landscapeLeftTransform[0] = cos(degreesToRadians(-90));
        landscapeLeftTransform[1] = -sin(degreesToRadians(-90));
        landscapeLeftTransform[2] = 0;
        landscapeLeftTransform[3] = 0;

        landscapeLeftTransform[4] = sin(degreesToRadians(-90));
        landscapeLeftTransform[5] = cos(degreesToRadians(-90));
        landscapeLeftTransform[6] = 0;
        landscapeLeftTransform[7] = 0;

        landscapeLeftTransform[8] = 0;
        landscapeLeftTransform[9] = 0;
        landscapeLeftTransform[10] = 1;
        landscapeLeftTransform[11] = 0;

        landscapeLeftTransform[12] = 0;
        landscapeLeftTransform[13] = 0;
        landscapeLeftTransform[14] = 0;
        landscapeLeftTransform[15] = 1;

        multiplyMatrixAndMatrix(cameraTransformRotated, cameraTransform, landscapeLeftTransform);
        break;
    default:
        cameraTransformRotated[0] = cameraTransform[0];
        cameraTransformRotated[1] = cameraTransform[1];
        cameraTransformRotated[2] = cameraTransform[2];
        cameraTransformRotated[3] = cameraTransform[3];

        cameraTransformRotated[4] = cameraTransform[4];
        cameraTransformRotated[5] = cameraTransform[5];
        cameraTransformRotated[6] = cameraTransform[6];
        cameraTransformRotated[7] = cameraTransform[7];

        cameraTransformRotated[8] = cameraTransform[8];
        cameraTransformRotated[9] = cameraTransform[9];
        cameraTransformRotated[10] = cameraTransform[10];
        cameraTransformRotated[11] = cameraTransform[11];

        cameraTransformRotated[12] = cameraTransform[12];
        cameraTransformRotated[13] = cameraTransform[13];
        cameraTransformRotated[14] = cameraTransform[14];
        cameraTransformRotated[15] = cameraTransform[15];

        break;
}
}

然后在我们更新所有要点之前,我这样做:

multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransformRotated);

之后其余的代码保持不变,我只想让横向以横向方向正确显示。现在这是我唯一的想法,横向渲染不好,我将设备移到右侧或左侧,注释向下或向上(就像我没有添加此代码时那样) )。

有人提出解决方案吗?我将继续搜索,特别是在CMRotationMatrix上,它似乎不是一个典型的旋转矩阵,我找不到任何文档准确说明这个矩阵的不同元素是什么。

1 个答案:

答案 0 :(得分:2)

我昨天设法将这个(Apple的pARk样本)改编为横向(右),并希望分享所做的更改。它似乎工作正常,但请说出任何错误。这只支持横向右侧,但可以很容易地适应左侧。

在ARView.m中,

In - (void)初始化,切换边界高度和宽度

createProjectionMatrix(projectionTransform, 60.0f*DEGREES_TO_RADIANS, self.bounds.size.height*1.0f / self.bounds.size.width, 0.25f, 1000.0f);

In - (void)startCameraPreview

[captureLayer setOrientation:AVCaptureVideoOrientationLandscapeRight];

In - (void)drawRect:

//switch x and y
float y = (v[0] / v[3] + 1.0f) * 0.5f; 
float x = (v[1] / v[3] + 1.0f) * 0.5f;

poi.view.center = CGPointMake(self.bounds.size.width-x*self.bounds.size.width, self.bounds.size.height-y*self.bounds.size.height); //invert x