Is there a standard C++ equivalent of C#'s Vector3?
Jus开发者_StackOverflow社区t wondering if C++ has a standard equivalent of the Vector2/3/4 (structures I think?) in C#?
Edit: For clarification a XNA C# Vector2/3/4 "structures" (I'm not entirely sure what they are) basically hold 2, 3, or 4 float values like a struct in C++ defined as:
struct Vector3
{
float x, y, z;
};
I've been basically using just that code in C++, but I was hoping for a standard alternative and was unable to find one.
The Vector3 struct in C# is from XNA, not the base class libraries.
The equivelent in C++ would be to use XMFLOAT3.
Nothing standard that I know of, but here's some code to get you started
http://www.flipcode.com/archives/Faster_Vector_Math_Using_Templates.shtml
If you are using C++/CLI and targeting Windows and .NET, you can use Vector2, etc.
Though there's no equivalent to Vector3 in standard C++, there are several linear algebra libraries that have one. Some that are very small and fast:
- vmmlib - BSD-licensed, written for OpenGL compatibility
- Eigen - LGPL-licensed, but it won't make you change your license
- GLM - MIT licensed, written to mimic the OpenGL Shading Language
A valarray<complex<T>>
could be used like a Vector2
in many cases. For Vector3/4
I know no standard equivalent.
I don't believe that there are any "canned" objects in C++ which are equivalent. The STL vector
is not the same thing. That is essentially a dynamic array for storage of any value types whereas the vector that you're referring to is class with added member functionality, typically seen used in XNA graphic applications (in my experience at least). You are on to something with your recognition of the fact that Vector2/3/4 are data structures. You could very easily store the values of the W, X, Y, Z components in your own structure, but the added member functions provided with the Vector2/3/4 object would need to be added by you.
There are also several C++ 3D graphics libraries similar to XNA, such as Ogre3D, Irrlicht, and others. Might be a bit of an overkill if you only need the Vector3 structure though.
精彩评论