1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ARメジャーの機能を手軽に実装します

3次元上の2点間の距離をはかりたい

複雑な計算が必要なのでしょうか?

ARKitでかんたん計算

iOSには平面へのヒットテストや、三次元の2点間の距離を手軽に測れる機能があります。
ARはセンシングが肝なので、距離計算はなかなかに正確です。

手順

ヒットテストで、画面タップと平面との交点座標を検出します。

@objc func tap(recognizer: UITapGestureRecognizer) {
    let hitTestResults = sceneView.hitTest(recognizer.location(in: sceneView),types:[.estimatedHorizontalPlane,.estimatedVerticalPlane])
    guard let result = hitTestResults.first else { return }
    let worldCoordinates = simd_float3(x: result.worldTransform.columns.3.x, y: result.worldTransform.columns.3.y, z: result.worldTransform.columns.3.z)
}

あとは、1回目のタップの座標と2回目のタップの座標の距離をプリセットのdistance関数で計算します。

if coordinates1 == nil {
    coordinate1 = worldCoordinates
} else {
    coordinate2 = worldCoordinates
    let distance = distance(coordinate1, coordinate2)
    print(distance)
}

0.40427696

関数はメートル単位の結果を返します。

40cmの物差しを測ってみると、ちゃんと40cmと計算されました。

ARKitのセンサーは優秀

意外と正確に距離が取れるので、いろいろなアプリケーションに使えそうですね。

🐣


フリーランスエンジニアです。
お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLやARKitを使ったアプリを作っています。
機械学習/AR関連の情報を発信しています。

Twitter
Medium

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?