在Rust中声明2D,3D向量类型的惯用方式? [英] Idiomatic way to declare 2D, 3D vector types in Rust?

查看:114
本文介绍了在Rust中声明2D,3D向量类型的惯用方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找写一些小型的库,该库在空间中使用2D和3D点或方向(矢量在矢量/矩阵意义上,而不是Rust的Vec).

I'm looking to write some small library that use 2D and 3D points or directions in space
(vector in vector/matrix sense, not Rust's Vec).

Rust在这里没有施加规则,因此您可以创建一个元组或一个带有x, y, z成员的新struct.或单个data: [f64; 3]成员.

Rust doesn't impose rules here, so you can make a tuple of floats, or a new struct with x, y, z members. or a single data: [f64; 3] member.

我想在这里定义类型而不是使用[f64; 3]的原因是,这样我可以声明诸如lengthnormalized以及AddSub运算符之类的方法.

The reason I'd like to define a type here instead of using [f64; 3], is so I can declare methods such as length, normalized, also Add, Sub operators.

在上面声明小的2D-3D固定大小的数据类型有什么好的基础?

Whats a good basis to declare small 2D - 3D fixed sized data types on?

请注意,尽管现有的库很好,但是我想编写自己的库,因为它只需要一些基本的操作,而且我想了解内部的情况.

Note, while there are good existing libraries, I'd like to write my own since it only needs some basic operations, and I like to understand whats going on internally.

推荐答案

这个问题很普遍 ,但没有明确的最佳方式来表达您想要的内容.这很大程度上取决于您打算如何使用它.

This question is pretty broad and there isn't a clear best way to express what you want. It depends a lot on what you are planning to do with it.

与其他答案相比,我会提出一个略有不同的解决方案:使用具有x, y, z组件的结构,并完全使用 strong类型.

I would propose a slightly different solution compared to the other answers: use a struct with x, y, z components and use strong typing to the full extent.

向量可以用来表示许多东西(点,颜色等);您正在谈论空间中的2D和3D点.首先要注意的是点向量与方向向量之间的差异. 此处是math.stackexchange的一个很好的答案,可以很好地解释该主题.

Vectors can be used to represent many things (points, colors, ...); you are talking about 2D and 3D points in space. The first thing to be aware of is the difference between point and direction vectors. Here is a great answer on math.stackexchange that explains the topic really well.

此差异可以反映在类型系统中以捕获逻辑错误.这正是 cgmath 所做的事情.所以我想说您实际上要两种类型定义如下:

This difference can be reflected in the type system to catch logic errors. That's exactly what cgmath is doing. So I'd say that you actually want two types defined something like this:

struct Point3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

  • 为什么没有元组结构或数组?编写v.x的语义比v.0v[0]更清晰.如果您有向量类型来表示任意数据,则后两种情况是合适的.但是正如我提到的,我认为强类型输入是一个不错的选择,我们应该相应地选择好名字.
  • 为什么pub?为什么不呢?如果您在谈论空间中的点或方向,则没有无效矢量(此处忽略NaN float值).因此,没有真正的理由限制对字段的访问.
  • 为什么要使用f32?好问题……实际上您应该使用类型参数,但随后您需要绑定某种特征...
    • Why no tuple struct or array? The semantics of writing v.x are way clearer than v.0 or v[0]. The latter two cases are fitting if you have a vector type to represent arbitrary data. But as I mentioned, I think strong typing is a good choice here and we should choose good names accordingly.
    • Why pub? Why not? If you are talking about points or directions in space, there are no invalid vectors (ignoring the NaN float value here). So there isn't really a reason to restrict access to the fields.
    • Why f32? Good question... actually you should probably use a type parameter, but then you need to have some kind of trait bound ...
    • ...好吧,这使我得出了您不希望看到的结论:我认为在这种情况下,正确且惯用的"方法需要做一些工作.这项工作可以由cgmath之类的库正确完成.这个cgmath库尤其具有非常好的API设计IMO.大多数功能是通过特征(如VectorSpace)实现的,这些特征反映了所有背后的数学原理.我也想为我的项目自己编写向量类型,但最终我被说服使用了一个经过良好测试,设计良好的库.

      ... well this brings me to my conclusion which you won't like: I think doing it correct and "idiomatic" requires some work in this case. And this work is properly done by libraries like cgmath. This cgmath library in particular has a very nice API design, IMO. Most functionality is implemented through traits (like VectorSpace) that reflect some of the maths behind it all. I, too, wanted to write vector types on my own for a project of mine, but I was eventually convinced to use a well tested, well designed library instead.

      那么正确"的做法是什么? cgmath的实现方式差不多:

      So how to do it "right"? Pretty much how cgmath does it:

      • 强力打字
      • 正确命名
      • 具有大多数抽象特征功能

      这篇关于在Rust中声明2D,3D向量类型的惯用方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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