## Vector Math in C Externals

Oct 06 2016 | 12:19 pm
Hi
I am about to transfer some of my javascript externals that use three.js into c-externals and I am looking for a vector - math - library that would be suitable. Has anybody suggestions?
cheers
martin

• Oct 11 2016 | 2:58 pm
I got the following answer to my question through another channel:
can't you just make use of Jitter's vector math functions and structs? they are all defined in jit-includes/jit.vecmath.h
eg, to add two vec3s:
t_jit_vec3 v1 = { x1,y1,z1 }; t_jit_vec3 v2 = { x2,y2,z2 }; jit_vec3_accum_add( &v1, &v2 );
and another example, here's how we get the mat4 transform attribute in jit.anim.node:
t_jit_mat4 xform, tmpmat; long i; jit_mat4_identity(&xform); jit_mat4_from_quat(&xform, &x->quat); jit_mat4_set_translation(&xform, &x->position); jit_mat4_identity(&tmpmat); jit_mat4_set_scale(&tmpmat, &x->scale); jit_mat4_mult(&xform, &xform, &tmpmat);
for(i=0; i jit_atom_setfloat(*av+i,xform.vals[i]);
hope this helps -rob
• Oct 12 2016 | 1:34 pm
I like Eigen http://eigen.tuxfamily.org/ and use it for several purposes in some of my Max externals. Works well in C++11 style coding, cross-platform, and can use SIMD instructions. I recommend it instead of the Max jitter functions.
• Oct 12 2016 | 1:44 pm
Interesting. Are there examples on how to get started with writing C++11 style max externals? maybe even together with the eigen library?
I am reluctant to dive into C++ domain. I am happy that I was able to get my first C-style external to compile on Windows and OSX, because to get to this point is usually a big pain for me and I don't really know what I am doing until all linker and compile errors have been googled away....
Once I have an example running I can hack my way to a solution.
the max-sdk is focused on C and to my knowledge there are no examples for C++.
• Oct 12 2016 | 2:54 pm
The Max SDK is C only. Eventually, you have to get to that API contract. What is possible, is to use/define C++ objects and/or template metaprogramming which eventually resolves down to that C API.
At https://github.com/grrrwaaa/maxcpp is a combination of macros and C++ templates which I found but have not personally used myself. That repro also has some examples using itself.
I do not know of any public examples of using Eigen with the Max SDK. Eigen is very flexible and provides mechanisms to access the raw data/memory that it is manipulating. Using that raw access, you can then pass pointers or copy arrays in ways that are C-compatible and therefore can be used with Max's SDK. For example, Eigen can easily manipulate math matrices. You can ask Eigen for a C pointer to the values contained in that matrix. Then, you can copy the values or pass the pointer to Max's SDK to create atoms, attributes, etc.