如何计算方位角相对于 3D 中的相机视角方向的仰角......? [英] How to calculate azimut & elevation relative to a camera direction of view in 3D ...?

查看:34
本文介绍了如何计算方位角相对于 3D 中的相机视角方向的仰角......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有点生疏了.

我有一个向量(camDirectionX、camDirectionY、camDirectionZ)代表我的相机视角.我有一个 (camX, camY, camZ) 是我的相机位置.

I have a vector (camDirectionX, camDirectionY, camDirectionZ) that represents my camera direction of view. I have a (camX, camY, camZ) that is my camera position.

然后,我在 (objectX, objectY, objectZ) 处放置了一个对象

Then, I have an object placed at (objectX, objectY, objectZ)

如何从相机的角度计算方位角和我的物体的高度 ??

How can I calculate, from the camera point of view, the azimut & elevation of my object ??

推荐答案

为了简化问题,我要做的第一件事是变换坐标空间,使相机位于 (0, 0, 0) 并指向正下方轴之一(所以方向是 (0, 0, 1)).将相机转换为 (0, 0, 0) 非常简单,所以我不会深入研究.旋转使相机方向为 (0, 0, 1) 有点棘手...

The first thing I would do, to simplify the problem, is transform the coordinate space so the camera is at (0, 0, 0) and pointing straight down one of the axes (so the direction is say (0, 0, 1)). Translating so the camera is at (0, 0, 0) is pretty trivial, so I won't go into that. Rotating so that the camera direction is (0, 0, 1) is a little trickier...

一种方法是构建相机的完整正交基,然后将其粘贴在旋转矩阵中并应用它.相机的正交基"是表示从相机指向前方、上方和右侧的三个向量的奇特方式.它们都应该相互成 90 度(这是正交位的意思),并且它们的长度都应该是 1(这是正常位的意思).

One way of doing it is to construct the full orthonormal basis of the camera, then stick that in a rotation matrix and apply it. The "orthonormal basis" of the camera is a fancy way of saying the three vectors that point forward, up, and right from the camera. They should all be at 90 degrees to each other (which is what the ortho bit means), and they should all be of length 1 (which is what the normal bit means).

您可以通过一些叉积技巧获得这些向量:两个向量的叉积与两者都垂直(成 90 度).

You can get these vectors with a bit of cross-product trickery: the cross product of two vectors is perpendicular (at 90 degrees) to both.

为了得到右向向量,我们可以将相机方向向量与 (0, 1, 0)(一个指向正上方的向量)进行叉积.您需要对从叉积中得到的向量进行归一化.

To get the right-facing vector, we can just cross-product the camera direction vector with (0, 1, 0) (a vector pointing straight up). You'll need to normalise the vector you get out of the cross-product.

为了得到相机的向上向量,我们可以将相机方向向量与我们刚刚计算的向右向量交叉乘积.假设两个输入向量都被归一化,这应该不需要归一化.

To get the up vector of the camera, we can cross product the camera direction vector with the right-facing vector we just calculated. Assuming both input vectors are normalised, this shouldn't need normalising.

我们现在有了相机的正交基.如果我们将这些向量粘贴到 3x3 矩阵的行中,我们会得到一个旋转矩阵,它将转换我们的坐标空间,因此相机直接指向其中一个轴(哪个取决于您粘贴向量的顺序).

We now have the orthonormal basis of the camera. If we stick these vectors into the rows of a 3x3 matrix, we get a rotation matrix that will transform our coordinate space so the camera is pointing straight down one of the axes (which one depends on the order you stick the vectors in).

现在计算物体的方位角和仰角相当容易.

It's now fairly easy to calculate the azimuth and elevation of the object.

要获得方位角,只需在对象的 x/z 坐标上做一个 atan2.

To get the azimuth, just do an atan2 on the x/z coordinates of the object.

要获得高程,将对象坐标投影到 x/z 平面上(只需将 y 坐标设置为 0),然后执行:

To get the elevation, project the object coordinates onto the x/z plane (just set the y coordinate to 0), then do:

acos(dot(normalise(object coordinates), normalise(projected coordinates)))

这将始终给出一个正角度——如果对象的 y 坐标小于 0,您可能想要否定它.

This will always give a positive angle -- you probably want to negate it if the object's y coordinate is less than 0.

所有这些的代码看起来像:

The code for all of this will look something like:

fwd = vec3(camDirectionX, camDirectionY, camDirectionZ)
cam = vec3(camX, camY, camZ)
obj = vec3(objectX, objectY, objectZ)

# if fwd is already normalised you can skip this
fwd = normalise(fwd)

# translate so the camera is at (0, 0, 0)
obj -= cam

# calculate the orthonormal basis of the camera
right = normalise(cross(fwd, (0, 1, 0)))
up = cross(right, fwd)

# rotate so the camera is pointing straight down the z axis
# (this is essentially a matrix multiplication)
obj = vec3(dot(obj, right), dot(obj, up), dot(obj, fwd))

azimuth = atan2(obj.x, obj.z)

proj = vec3(obj.x, 0, obj.z)
elevation = acos(dot(normalise(obj), normalise(proj)))
if obj.y < 0:
    elevation = -elevation

需要注意的一点是,当您的相机正面朝上或朝下时,原始摄像机向量与 (0, 1, 0) 的叉积将返回零长度向量.为了完全定义相机的方向,我假设它总是直的",但是当它朝上或朝下时这并不意味着什么——你需要另一个规则.

One thing to watch out for is that the cross-product of your original camera vector with (0, 1, 0) will return a zero-length vector when your camera is facing straight up or straight down. To fully define the orientation of the camera, I've assumed that it's always "straight", but that doesn't mean anything when it's facing straight up or down -- you need another rule.

这篇关于如何计算方位角相对于 3D 中的相机视角方向的仰角......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆