java – 将3D世界(arcore锚/姿势)转换为相应的2D屏幕坐标
发布时间:2020-12-15 08:25:26 所属栏目:Java 来源:网络整理
导读:我正在努力实现这种转变.给定arcore中的锚Pose如何在屏幕中获得相应的2D坐标? 解决方法 最后,经过几天的调查并从不同的资源获取信息后,我才能使其正常运行.以下是一个代码片段(基于arcore示例 java应用程序),可以从世界坐标(在arcore中的姿势)转换为2D屏幕
我正在努力实现这种转变.给定arcore中的锚Pose如何在屏幕中获得相应的2D坐标?
解决方法
最后,经过几天的调查并从不同的资源获取信息后,我才能使其正常运行.以下是一个代码片段(基于arcore示例
java应用程序),可以从世界坐标(在arcore中的姿势)转换为2D屏幕坐标:
首先,我们需要计算从世界转换的矩阵 – >屏幕: public float[] calculateWorld2CameraMatrix(float[] modelmtx,float[] viewmtx,float[] prjmtx) { float scaleFactor = 1.0f; float[] scaleMatrix = new float[16]; float[] modelXscale = new float[16]; float[] viewXmodelXscale = new float[16]; float[] world2screenMatrix = new float[16]; Matrix.setIdentityM(scaleMatrix,0); scaleMatrix[0] = scaleFactor; scaleMatrix[5] = scaleFactor; scaleMatrix[10] = scaleFactor; Matrix.multiplyMM(modelXscale,modelmtx,scaleMatrix,0); Matrix.multiplyMM(viewXmodelXscale,viewmtx,modelXscale,0); Matrix.multiplyMM(world2screenMatrix,prjmtx,viewXmodelXscale,0); return world2screenMatrix; } 一旦我们有了这个矩阵,那么我们可以将点从3D世界投影到2D,但在这个投影中我们必须从NDC坐标转换为屏幕.以下是执行此转换的方法: double[] world2Screen(int screenWidth,int screenHeight,float[] world2cameraMatrix) { float[] origin = {0f,0f,1f}; float[] ndcCoord = new float[4]; Matrix.multiplyMV(ndcCoord,world2cameraMatrix,origin,0); ndcCoord[0] = ndcCoord[0]/ndcCoord[3]; ndcCoord[1] = ndcCoord[1]/ndcCoord[3]; double[] pos_2d = new double[]{0,0}; pos_2d[0] = screenWidth * ((ndcCoord[0] + 1.0)/2.0); pos_2d[1] = screenHeight * (( 1.0 - ndcCoord[1])/2.0); return pos_2d; } 最后,一个简单的用法示例: DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int height = displayMetrics.heightPixels; int width = displayMetrics.widthPixels; // Get projection matrix. float[] projmtx = new float[16]; camera.getProjectionMatrix(projmtx,0.1f,100.0f); // Get camera matrix and draw. float[] viewmtx = new float[16]; camera.getViewMatrix(viewmtx,0); float[] anchorMatrix = new float[16]; anchor.getPose().toMatrix(anchorMatrix,0); float[] world2screenMatrix = virtualObject.calculateWorld2CameraMatrix(anchorMatrix,projmtx); double[] anchor_2d = world2Screen(width,height,world2screenMatrix); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |