Standardize 2D / 3D vector / coordinate class

Question
This is what has been listening to me for some time, but I could not find a definitive answer to it:

  • Does anyone know of a proposal for embedding a standard 2D and / or 3D vector (structures with elements x, y, and z) in the STL?
  • If not, is there a realistic way to get such a class in the next version of the standard — unless you write a complete and perfectly written sentence?
  • And are there good reasons (except for those who do not have time) why this has not yet been done?

I am definitely ready to contribute, but I believe that I do not have enough experience to produce something of high enough quality for adoption (I am not a professional programmer).

Reasoning / Background
To date, I have seen dozens of libraries and frameworks (be it graphics, physics, mathematics, navigation, sensor fusion ...), which basically implement their own version

struct Vector2d { double x,y; //... }; /* ... * operator overloads */ 

and / or its three-dimensional equivalent - not to mention all the cases when I myself implemented it before I took the time to make the correct, reusable version.
Obviously, this is not something difficult, and I'm not worried about suboptimal implementations, but every time I want to combine two libraries or reuse code from another project, I have to take care of converting one version to another (either casting or - if perhaps a text replacement).

Now that the committee seeks to significantly expand the standard library for C ++ 17 (especially with 2D graphics), I would like from the very beginning I would like to have a common 2D vector baked in all interfaces, so I can just write, eg:

 drawLine(transformCoordinates(trackedObject1.estimatePos(),params), transformCoordinates(trackedObject2.estimatePos(),params)); 

but not

 MyOwnVec2D t1{trackedObject1.estimatePosX(), trackedObject1.estPosY()}; MyOwnVec2D t2{trackedObject2.estimatePosX(), trackedObject2.estPosY()}; t1 = transformCoordinates(t1,params); t2 = transformCoordinates(t2,params); drawLine(t1.x,t1.y,t2.x,t2.y); 

The example may be a little exaggerated, but I think it shows my point.

I know std::valarray , which is already going in the right direction, since it allows you to perform standard operations such as addition and multiplication, but it carries too much weight if you need only two or three coordinates. I think a valarray with a fixed size and no dynamic memory allocation (e.g. based on std::array ) would be an acceptable solution, especially since it would have a trivial iterator implementation, but I personally would prefer a class with x, y (and z).

Note: I'm sorry if this topic has already been discussed (and I would be surprised if it weren’t for it), but every time I look for 2d vectors, I get results that say something like std::vector<std::vector<T>> or how to implement a certain transformation, but not on the topic of standardization.

+5
source share
2 answers

Are there any good reasons (except for those who do not have time) why this has not yet been done?

There is essentially no reason.

Forming a type containing two or three elements is completely trivial, and all operations can also be defined trivially. In addition, the C ++ standard library is not intended for universal mathematical tools: it makes sense to use specialized third-party libraries for this if you are serious about mathematical types and constructions that go beyond functions and operators that you can combine in half an hour.

And we do not standardize things that do not need to be standardized.

If C ++ was to get some standardized 3D graphics API, then I can see this change, but not until then. And hopefully, C ++ will never get any standardized 3D graphics API, because that’s not what it is for.

However, if you feel this strongly, you can start a conversation on std-discussion , where all the experts (and some, experts); sometimes such conversations lead to the formation of sentences, and it is not necessary that you write it.

+4
source

In case anyone is interested in this, I would like to indicate that the July 2014 version of “Proposal for adding 2D rendering and display in C ++” includes a 2D class / structure (my question was based on initial draft since January 2014). Therefore, perhaps there will be at least a simple standard 2D vector in C ++ 1z.

+1
source

Source: https://habr.com/ru/post/1210667/


All Articles