move explicit bezier to vg
[vg.git] / vg_m.h
diff --git a/vg_m.h b/vg_m.h
index 3d4f53ffd53eca654a003663be5a014ea43238d3..5ab6a22d8620d459498e15f99194a3d0ebdc33fc 100644 (file)
--- a/vg_m.h
+++ b/vg_m.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 2021-2023 Harry Godden (hgn) - All Rights Reserved 
+/* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved 
  *
  *  0. Misc
  *  1. Scalar operations
@@ -24,8 +24,7 @@
  *    6.a Random numbers
  */
 
-#ifndef VG_M_H
-#define VG_M_H
+#pragma once
 
 #include "vg_platform.h"
 #include <math.h>
@@ -1568,6 +1567,16 @@ static inline int box_overlap( boxf a, boxf b )
    ;
 }
 
+static int box_within_pt( boxf box, v3f pt )
+{
+   if( (pt[0] >= box[0][0]) && (pt[1] >= box[0][1]) && (pt[2] >= box[0][2]) &&
+       (pt[0] <= box[1][0]) && (pt[1] <= box[1][1]) && (pt[2] <= box[1][2]) )
+   {
+      return 1;
+   }
+   else return 0;
+}
+
 static int box_within( boxf greater, boxf lesser )
 {
    v3f a, b;
@@ -1579,8 +1588,7 @@ static int box_within( boxf greater, boxf lesser )
    {
       return 1;
    }
-
-   return 0;
+   else return 0;
 }
 
 static inline void box_init_inf( boxf box ){
@@ -1644,7 +1652,7 @@ static int plane_intersect3( v4f a, v4f b, v4f c, v3f p )
    return 1;
 }
 
-int plane_intersect2( v4f a, v4f b, v3f p, v3f n )
+static int plane_intersect2( v4f a, v4f b, v3f p, v3f n )
 {
    f32 const epsilon = 1e-6f;
 
@@ -2050,7 +2058,7 @@ static void closest_point_elipse( v2f p, v2f e, v2f o )
  * -----------------------------------------------------------------------------
  */
 
-int ray_aabb1( boxf box, v3f co, v3f dir_inv, f32 dist )
+static int ray_aabb1( boxf box, v3f co, v3f dir_inv, f32 dist )
 {
    v3f v0, v1;
    f32 tmin, tmax;
@@ -2295,6 +2303,56 @@ static void eval_bezier3( v3f p0, v3f p1, v3f p2, f32 t, v3f p )
    v3_muladds( p, p2, t*t, p );
 }
 
+static f32 explicit_bezier( f32 A[2], f32 B[2], f32 C[2], f32 D[2], f32 x )
+{
+   f32 dAxDx = D[0]-A[0],
+       unitBx = (B[0] - A[0]) / dAxDx,
+       unitCx = (C[0] - A[0]) / dAxDx,
+
+       /* cubic coefficients */
+       a =  3.0f*unitBx - 3.0f*unitCx + 1.0f,
+       b = -6.0f*unitBx + 3.0f*unitCx,
+       c =  3.0f*unitBx,
+       d = -(x - A[0]) / dAxDx,
+
+        t0 = 0.0f,
+       Ft0 = d,
+        t1 = 1.0f,
+       Ft1 = a+b+c+d,
+        tc, Ftcx;
+
+   /* Illinois method to find root */
+   for( u32 j=0; j<8; j ++ )
+   {
+      tc = t1 - Ft1*(t1-t0)/(Ft1-Ft0);
+      Ftcx = tc*tc*tc*a + tc*tc*b + tc*c + d;
+
+      if( fabsf(Ftcx) < 0.00001f )
+         break;
+      
+      if( Ft1*Ftcx < 0.0f )
+      {
+         t0 = t1;
+         Ft0 = Ft1;
+      }
+      else
+         Ft0 *= 0.5f;
+
+      t1 = tc;
+      Ft1 = Ftcx;
+   }
+
+   /* Evaluate parametric bezier */
+   f32 t2 = tc*tc,
+       t3 = tc*tc*tc;
+   
+   return  D[1] * t3
+         + C[1] * (-3.0f*t3 + 3.0f*t2)
+         + B[1] * ( 3.0f*t3 - 6.0f*t2 + 3.0f*tc)
+         + A[1] * (-1.0f*t3 + 3.0f*t2 - 3.0f*tc + 1.0f);
+}
+
+
 /*
  * -----------------------------------------------------------------------------
  * Section 5.f                      Volumes
@@ -2610,5 +2668,3 @@ static void vg_rgb_hsv( v3f rgb, v3f hsv ){
 
    hsv[0] = vg_fractf( hsv[0] * (60.0f/360.0f) );
 }
-
-#endif /* VG_M_H */