c++仅使用矩阵按深度排序。可能的?
如果我有一个包含旋转/平移矩阵的对象的 std::vector,有没有办法可以使用该矩阵来计算 Z 位置,以便我可以按深度对对象进行排序?
If I have a std::vector of objects containing a rotated / translates matrix, is there a way I can use that matrix to calculate a Z position so that I can sort the objects by depth?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简短回答:
沿矩阵[3][2]排序,矩阵[3][2]是对象中心的z位置(在世界空间中)。
长答案:
在齐次坐标中,位置是 (x,y,z,1) (与方向 (x,y,z,0) 相反,我们稍后会看到)
从一个空间变换你的点对于另一个,将其乘以变换矩阵。因此,如果您想通过矩阵变换对象的中心(0,0,0,1),则唯一剩下的项是矩阵的右列。 z 分量是第三个。
对于方向,w=0,因此右列(保存平移)将乘以 0。这意味着:平移方向不会改变方向,这是有道理的。
显然,如果对对象的中心进行排序,两个靠近的对象可能会重叠。
Short answer :
sort along matrix[3][2], which is the z position (in world space) of your object's center.
Long answer :
In homogeneous coordinates, a position is (x,y,z,1) ( as opposed to a direction which is (x,y,z,0), as we will see later )
To transform your point from one space to another, you multiply it by the transformation matrix. So if you want to transform the object's center, which is thus (0,0,0,1) by a matrix, the only remaining term is the right column of the matrix. The z component is the third.
With a direction, w=0, so the right column (which holds translation) will be multiplied by 0. This means : translating a direction does not change the direction, which makes sense.
Obviously, if you sort object's centers, two close objects may overlay.
猜测一下您要问的问题:
如果您有一个变换向量(即 4x4)矩阵,则 Z 分量位于矩阵的平移分量中。它应该是 (row,col) = 2,3 或 3,2,具体取决于您的约定。
例如:
Taking a guess at what you're asking:
If you have a vector of transformation (i.e. 4x4) matrices, then the Z component is in the translation component of the matrix. It should be (row,col) = 2,3 or 3,2 depending on your convention.
E.g:
定义所需的顺序,然后使用 std::sort,如 此示例。
基于评论的更新:
您似乎要问的中心问题是如何定义用作排序基础的顺序。如果您的对象是单点,则应用相应的变换(如果您没有预先计算/momoized)并对结果向量的 Z 分量进行排序。
如果您的对象由多个点组成,这会变得更加困难。在这种情况下,您需要定义与每个对象关联的合适的代理单点位置,然后将其用作排序的基础。由于对象本质上是多点的,因此在某些情况下任何替代单点表示都可能提供较差的结果。但是,如果对象都是简单的平面多边形,除了沿着边或顶点之外,它们不会彼此相交,那么您可以将每个多边形的顶点位置平均为一个点,作为合适的代理。该方法也可以很好地扩展到不相交的凸多面体。如果对象变得太复杂,那么您要么必须将它们分解为不相交的正多边形或凸多面体并对它们进行排序,要么接受并不总是正确的。
Define the desired ordering and then use std::sort as in this example.
UPDATE BASED ON COMMENT:
The central issue you seem to be asking is how to define the ordering that you use as the basis for the sort. If your objects are single points, then you apply the corresponding transformation (if you haven't precomputed / momoized it) and sort on the Z component of resulting vector.
It becomes more difficult if your objects are comprised of multiple points. In that case you will need to define a suitable surrogate single point location associated with each object that you can then use as the basis for your sort. Since the objects are multi-point in nature, any surrogate single point representation will probably provide poor results under some circumstances. However, if the objects are all simple, planar polygons, that do not intersect each other except along edges or vertices, then you could average each polygon's vertex location to a single point that serves as a suitable surrogate. This method extends well also to non-intersecting convex polyhedra as well. If the objects become too complicated, then you'll either have to decompose them into non-intersecting regular polygons or convex polyhedra and sort those, or accept not always getting it right.
如果我理解的话,您可能可以通过排序谓词来做到这一点。
If I understand, you can probably do this with a predicate on the sort.