// Copyright Contributors to the Open Shading Language project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage

shader test (output color Cspline = 0, output color DxCspline = 0,
             output float Fspline = 0, output float DxFspline = 0,
             output float NumKnots = 0)
{
   string bases[5] = { "catmull-rom", "bezier", "bspline", "hermite", "linear" };
   color  colors[5] = { color(1,0,0),
                        color(0,1,0),
                        color(0,0,1),
                        color(1,1,0),
                        color(1,0,1) };
 
   float x;
   color colknots[13];
   float fltknots[13];

   int basis = (int)(v*5.0);
   if (basis >= 5) basis = 4;

   color knot_col = colors[basis];

   colknots[ 0] =  0.0*knot_col;
   colknots[ 1] =  0.0*knot_col;
   colknots[ 2] =  1.0*knot_col;
   colknots[ 3] =  1.0*knot_col;
   colknots[ 4] =  1.0*knot_col;
   colknots[ 5] =  0.5*knot_col;
   colknots[ 6] =  0.5*knot_col;
   colknots[ 7] =  0.5*knot_col;
   colknots[ 8] =  1.0*knot_col;
   colknots[ 9] =  1.0*knot_col;
   colknots[10] =  1.0*knot_col;
   colknots[11] =  0.0*knot_col;
   colknots[12] =  0.0*knot_col;

   Cspline   = spline(bases[basis], u, colknots);
   DxCspline = 100.0*Dx(Cspline);

   fltknots[ 0] =  0.0*u;
   fltknots[ 1] =  0.0*u;
   fltknots[ 2] =  1.0*u;
   fltknots[ 3] =  1.0*u;
   fltknots[ 4] =  1.0*u;
   fltknots[ 5] =  0.5*u;
   fltknots[ 6] =  0.5*u;
   fltknots[ 7] =  0.5*u;
   fltknots[ 8] =  1.0*u;
   fltknots[ 9] =  1.0*u;
   fltknots[10] =  1.0*u;
   fltknots[11] =  0.0*u;
   fltknots[12] =  0.0*u;

   Fspline   = spline(bases[basis], u, fltknots);
   DxFspline = 100.0*Dx(Fspline);

   NumKnots   = spline("linear", u, 4, fltknots);
   
    {
        // Regression test for a bug involving splines on arrays, where
        // the number of knots is less than the length of the whole array,
        // and the knots have derivatives -- prior to fixing the bug, the
        // derivatives were accessed with the wrong offset (assuming that
        // array length == num_knots).  Bug fixed 19-Dec-2013.
        color knots[16];
        for (int i = 0; i < 4; ++i)
            knots[i] = color (i/3, i/3, i/3);
        for (int i = 4; i < arraylength(knots); ++i)
            knots[i] = color(u*1.0e9, u*1.0e9, u*1.0e9);
        color c = spline ("linear", u, 4, knots);
        if (! (c[0] < 2.0 && c[1] < 2.0 && c[2] < 2.0 &&
               Dx(c[0]) < 2.0 && Dx(c[1]) < 2.0 && Dx(c[2]) < 2.0 &&
               Dy(c[0]) < 2.0 && Dy(c[1]) < 2.0 && Dy(c[2]) < 2.0))
            printf ("Hey, found a bad value!\n");
    }
}
