{"id":13563942,"url":"https://github.com/duzexu/ARuler","last_synced_at":"2025-04-03T20:32:30.172Z","repository":{"id":70340758,"uuid":"96535097","full_name":"duzexu/ARuler","owner":"duzexu","description":"Mesure distance using apple ARKit","archived":false,"fork":false,"pushed_at":"2018-10-15T09:41:03.000Z","size":64748,"stargazers_count":1271,"open_issues_count":7,"forks_count":141,"subscribers_count":48,"default_branch":"master","last_synced_at":"2025-03-27T14:14:02.674Z","etag":null,"topics":["apple-arkit","arkit","mesure-distance","ruler","scenekit","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/duzexu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-07-07T12:10:29.000Z","updated_at":"2025-03-18T15:12:07.000Z","dependencies_parsed_at":"2023-03-01T22:01:32.890Z","dependency_job_id":null,"html_url":"https://github.com/duzexu/ARuler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duzexu%2FARuler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duzexu%2FARuler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duzexu%2FARuler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duzexu%2FARuler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/duzexu","download_url":"https://codeload.github.com/duzexu/ARuler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247024150,"owners_count":20870940,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apple-arkit","arkit","mesure-distance","ruler","scenekit","swift"],"created_at":"2024-08-01T13:01:24.783Z","updated_at":"2025-04-03T20:32:30.166Z","avatar_url":"https://github.com/duzexu.png","language":"Swift","funding_links":[],"categories":["Swift","Projects"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./Design/logo.png\" width=\"100%\"/\u003e\n\u003c/p\u003e\n\n# ARuler\nMesure distance using apple ARKit\n\n[ENGLISH README](./README_EN.md)\n\n## 预览\n  \u003cimg src=\"./Design/preview_vertical.gif\" width=\"320\"/\u003e\n  \n## 运行\ncd到工程目录下，运行`pod install`\n\n## 安装\n因为ARKit使用限制，设备要求为6s以上，系统最低要求为iOS11，Xcode版本为9以上\n\n测量时需保证光线充足\n\n## 问题\n\u003cdel\u003eARKit目前只能识别水平的平面，所以只能测量水平面的长度，不能测量垂直平面上的长度，不知道视频里的是怎么实现的，有知道的小伙伴可以一起交流一下\u003c/del\u003e\n\n去除了识别平面后才进行测量的逻辑，采用最近的特征点，可以测量垂直的物体啦\n\n## 说明\n项目中确定起始和结束点的位置主要有以下几种\n\n##### 如果存在检测到的平面，返回平面上一点\n\n```\nlet planeHitTestResults = sceneView.hitTest(position, types: .existingPlaneUsingExtent)\n    if let result = planeHitTestResults.first {\n\n        let planeHitTestPosition = SCNVector3.positionFromTransform(result.worldTransform)\n        let planeAnchor = result.anchor\n\n        // Return immediately - this is the best possible outcome.\n        return (planeHitTestPosition, planeAnchor as? ARPlaneAnchor, true)\n    }\n```\n\n##### 根据当前ARFrame中的rawFeaturePoints计算点\n\n* 获取以摄像头和屏幕上一点对应的透视消失点为轴，角度为10度，最小距离为10cm，最大距离为三米组成的截锥体内的特征点\n\n```\nlet highQualityfeatureHitTestResults = sceneView.hitTestWithFeatures(position, coneOpeningAngleInDegrees: 5, minDistance: 0.1, maxDistance: 3.0)\n```\n\n* 假设抽样的特征点符合正态分布，过滤偏差值大于 3σ 的值\n\n```\nfunc fliterWithFeatures(_ features:[FeatureHitTestResult]) -\u003e [SCNVector3] {\n    guard features.count \u003e= 3 else {\n        return features.map { (featureHitTestResult) -\u003e SCNVector3 in\n            return featureHitTestResult.position\n        };\n    }\n        \n    var points = features.map { (featureHitTestResult) -\u003e SCNVector3 in\n        return featureHitTestResult.position\n    }\n    // 平均值\n    let average = points.average!\n    // 方差\n    let variance = sqrtf(points.reduce(0) { (sum, point) -\u003e Float in\n        var sum = sum\n        sum += (point-average).length()*100*(point-average).length()*100\n        return sum\n        }/Float(points.count-1))\n    // 标准差\n    let standard = sqrtf(variance)\n    let σ = variance/standard\n    points = points.filter { (point) -\u003e Bool in\n        if (point-average).length()*100 \u003e 3*σ {\n            print(point,average)\n        }\n        return (point-average).length()*100 \u003c 3*σ\n    }\n    return points\n}\n```\n\n* 根据过滤的点用[最小二乘法](https://wenku.baidu.com/view/c9d0713710661ed9ac51f305.html)进行平面推定\n\n```\nlet detectPlane = planeDetectWithFeatureCloud(featureCloud: warpFeatures)\n```\n\n* 根据截锥体轴上的点和向量及平面上的点和法向量计算交点\n\n```\nvar planePoint = SCNVector3Zero\nif detectPlane.x != 0 {\n\tplanePoint = SCNVector3(detectPlane.w/detectPlane.x,0,0)\n}else if detectPlane.y != 0 {\n    planePoint = SCNVector3(0,detectPlane.w/detectPlane.y,0)\n}else {\n    planePoint = SCNVector3(0,0,detectPlane.w/detectPlane.z)\n}\n    \nlet ray = sceneView.hitTestRayFromScreenPos(position)\nlet crossPoint = planeLineIntersectPoint(planeVector: SCNVector3(detectPlane.x,detectPlane.y,detectPlane.z), planePoint: planePoint, lineVector: ray!.direction, linePoint: ray!.origin)\n```\n\n* 如果直线和平面平行或不符合平面推定条件，返回点平均值\n* 如果截锥体内没有符合的点，查找出距离轴最近的点\n\n## 参考\n看到微博上[AR虚拟尺子视频](https://m.weibo.cn/1652421612/4122791333240092)，就想试着模仿着实现一下\n\n部分代码来自[苹果ARKitDemo](https://developer.apple.com/sample-code/wwdc/2017/PlacingObjects.zip)\n\n平面拟合代码参考这篇[博客](http://blog.csdn.net/zhouyelihua/article/details/46122977)\n\n## 建议\n有任何建议或问题请提issue\n\n## 开源协议\nARuler开源在 GPL 协议下，详细请查看 [LICENSE](./LICENSE) 文件\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fduzexu%2FARuler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fduzexu%2FARuler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fduzexu%2FARuler/lists"}