sgorsten / linalg

linalg.h is a single header, public domain, short vector math library for C++
The Unlicense
849 stars 68 forks source link

User Code Extensions #12

Closed john-chapman closed 5 years ago

john-chapman commented 6 years ago

Hi, I'm currently looking into linalg as a replacement for glm, and so far I like linalg a lot.

However I'd like a little more flexibility; specifically I think it would be useful if linalg directly supported user code extensions to the vec and mat data structures via macros, e.g. in each of the template instantiations there would be a declaration like this:

template<class T> struct vec<T,2>
{
#ifdef LINALG_VEC2_USER
   LINALG_VEC2_USER
#endif
 // ...

This trivially allows user code to extend these types, for example by adding custom ctors or operator overloads. In my case this would significantly simplify the transition from glm -> linalg by injecting glm behavior:

melax commented 6 years ago

interesting suggestion. this could also be a good idea to let people write convenient interop with another library such as opencv or eigen. For example, in your macro, you could add constructor and cast operators with opencv's cvMat.

melax commented 6 years ago

note that the operator can be implemented outside the class.
However, there already is a
operator defined which does componentwise multiplication. While this operator is consistent with the HLSL conventions, it can sometimes be a pitfall for those expecting M=AB to be doing traditional matrix multiplication. thats why you see mul() for doing typical matrix matrix or matrix vector multiplication and qmul doing quaternion multiplication. One option is to create subclasses for data that will have a specific semantic meaning, such as Transforms, Quaternions, and Planes.
for example:

   class Plane : public vec<float,4> { ... };
   class Pose : {  float3 position; float4 orientation; ... } ;  // assuming quat&vec formulation
sgorsten commented 5 years ago

It's not quite what you asked for, but I've introduced a new customization point in the v3 branch, as of dd95a1613b1cd0b98c13a9674180dd8f5b2f6b61.

Conversions between linalg types and user types can be enabled by specializing linalg::converter<T,U>. This can be used to allow linalg types to construct from external types, as well as for linalg types to declare a conversion operator to external types. This allows for user-defined constructors to be injected into the library in self-contained way, without modifying the definition of linalg types.

Note that some of the other operations you mentioned, such as mat * mat and mat * vec operators, were adopted as breaking changes for v3 already.