rustgd / cgmath

A linear algebra and mathematics library for computer graphics.
https://docs.rs/cgmath
Apache License 2.0
1.12k stars 155 forks source link

Why does `Point` exist? #502

Open InnocentusLime opened 4 years ago

InnocentusLime commented 4 years ago

I've been re-reading the docs for cgmath and I honestly don't understand why there are both Vector and Point in the library.

In pure maths these two concepts are barely distinguished, while in cgmath they are strictly split by introducing two structs. Not only that, these structs have different functionality implemented for them which consequently makes the crate more complicated. What was the motivation behind that partition?

P.S. I personally feel like all the functionality for Vector and Point should be implemented for one struct --- Vector (and Point should be depreacted). That will drastically simplify the crate and can fix issue #501. I can send a PR

elrnv commented 4 years ago

I think traditionally having Point be distinct from Vector is an effort to encode the distinction between affine spaces and euclidean spaces into the type system. This is usually done to prevent certain types of bugs (for example adding two points together is invalid, but subtracting them is perfectly fine).

I think the graphics community is somewhat split on this topic because each choice has its tradeoffs.

FWIW I think that nalgebra already fills the niche of dense linear algebra with strict types, so probably cgmath can play the role of a simpler API as you propose.

InnocentusLime commented 4 years ago

I see. Well, I guess I'll create a PR for that.

nstoddard commented 4 years ago

When I switched to cgmath I expected the distinction between Point and Vector to prevent some bugs in my code, but in practice I don't think I've encountered many, if any, situations where it actually prevented a bug. On the other hand, I've found many cases where I've had to convert between points and vectors, so I'm somewhat in favor of merging the two (only somewhat because the distinction does make sense mathematically).

kvark commented 4 years ago

Just wanted to explicitly chime in here to say that your feedback is much welcome, and I agree that the niche for rich typing is already taken, and we can have cgmath with simpler types.

LukasKalbertodt commented 4 years ago

Just to throw my opinion out there, too: I very much appreciate the distinction between Point and Vector in cgmath. In fact, it's one of the biggest positive things about the library IMO. The following is not meant in a threatening way, but if cgmath removes Point, I will probably switch to another library which has two types. And for me, the distinction of the two types actually prevented some real world bugs.

Instead of thinking about removing Point, maybe just try to make it easier to convert between them. One example where the distinction "annoyed" me when I had to calculate the centroid of some points (I am aware cgmath has a function for that, but it only works for a slice of points, not iterators). But yeah, even the current API is fine in my book.

kvark commented 4 years ago

@LukasKalbertodt it really comes down to what niche cgmath tries to settle in. It used to be one of the first libraries with a clear-ish goal. But after a long period of being unmaintained, and with a lot of interesting alternatives appearing, cgmath needs to play a catch-up if it wants to stay afloat. It needs to re-find itself again in the ecosystem.

If you truly care, help with reviews and code cleanup, API shaping for 0.18. Would be interesting to see some basic analysis on what other libraries expose in terms of vector-point distinction.

aloucks commented 4 years ago

I agree overall that cgmath should fill the "simple to use" niche, but I don't think that precludes keeping the Point struct. I've also found it useful. Maybe the solution is to make explicit point/vector conversion more ergonomic?

InnocentusLime commented 4 years ago

I agree overall that cgmath should fill the "simple to use" niche, but I don't think that precludes keeping the Point struct. I've also found it useful. Maybe the solution is to make explicit point/vector conversion more ergonomic?

The conversion is kind of here. Thanks to points implementing EuclideanSpace. The main problem in my opinion would be confusing times a person may have when trying to use the Decomposed struct.

Not only that, I see no benefit in having Point, because OpenGL (and probably DirectX) don't have that. They have only VecN. So, removing Point will not only simplify the api (because it may invoke removal or merging of some traits), but will also make cgmath more familiar to the existing Graphics APIs. And, to my knowledge, cgmath exists to be used together with things like OpenGL, doesn't it? If it is so, the user should have little trouble translating data to the OpenGL implementation and there should be less difference between code in GLSL/HLSL and in Rust