Revision: 70052 http://sourceforge.net/p/brlcad/code/70052 Author: brlcad Date: 2017-08-07 05:29:54 +0000 (Mon, 07 Aug 2017) Log Message: ----------- include an implementation of linear interpolation (lerp) between two vectors/points. take care to manage floating point error if we used the mathematically simpler form. lerp is useful for interpolating between two points, alpha blending, animation, and more.
Modified Paths: -------------- brlcad/trunk/include/vmath.h Modified: brlcad/trunk/include/vmath.h =================================================================== --- brlcad/trunk/include/vmath.h 2017-08-05 12:59:46 UTC (rev 70051) +++ brlcad/trunk/include/vmath.h 2017-08-07 05:29:54 UTC (rev 70052) @@ -1367,6 +1367,52 @@ /** + * @brief Linearly interpolate between two 3D vectors `a' and `b' by + * interpolant `t', expected in the range [0,1], with result in `o'. + * + * NOTE: We intentionally use the form "o = a*(1-t) + b*t" which is + * mathematically equivalent to "o = a + (b-a)*t". The latter might + * result in fewer math operations but cannot guarantee o==v1 when + * t==1 due to floating-point arithmetic error. + */ +#define VLERP(o, a, b, t) do { \ + (o)[X] = (a)[X] * (1 - (t)) + (b)[X] * (t); \ + (o)[Y] = (a)[Y] * (1 - (t)) + (b)[Y] * (t); \ + (o)[Z] = (a)[Z] * (1 - (t)) + (b)[Z] * (t); \ + } while (0) + +/** + * @brief Linearly interpolate between two 2D vectors `a' and `b' by + * interpolant `t', expected in the range [0,1], with result in `o'. + * + * NOTE: We intentionally use the form "o = a*(1-t) + b*t" which is + * mathematically equivalent to "o = a + (b-a)*t". The latter might + * result in fewer math operations but cannot guarantee o==v1 when + * t==1 due to floating-point arithmetic error. + */ +#define V2LERP(o, a, b, t) do { \ + (o)[X] = (a)[X] * (1 - (t)) + (b)[X] * (t); \ + (o)[Y] = (a)[Y] * (1 - (t)) + (b)[Y] * (t); \ + } while (0) + +/** + * @brief Linearly interpolate between two 4D vectors `a' and `b' by + * interpolant `t', expected in the range [0,1], with result in `o'. + * + * NOTE: We intentionally use the form "o = a*(1-t) + b*t" which is + * mathematically equivalent to "o = a + (b-a)*t". The latter might + * result in fewer math operations but cannot guarantee o==v1 when + * t==1 due to floating-point arithmetic error. + */ +#define HLERP(o, a, b, t) do { \ + (o)[X] = (a)[X] * (1 - (t)) + (b)[X] * (t); \ + (o)[Y] = (a)[Y] * (1 - (t)) + (b)[Y] * (t); \ + (o)[Z] = (a)[Z] * (1 - (t)) + (b)[Z] * (t); \ + (o)[W] = (a)[W] * (1 - (t)) + (b)[W] * (t); \ + } while (0) + + +/** * @brief Subtract two points to make a vector, dot with another * vector. */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ BRL-CAD Source Commits mailing list brlcad-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/brlcad-commits