package p5;
import anar.*;




import processing.core.PApplet;

public class Test05aCsplineDarboux extends PApplet {

  /*
   * Example for Anar library by Guillaume LaBelle + Julien Nembrini
   * http://anar.ch
   */


  Obj myObj = new Obj();


  public void setup(){
    size(800,400,OPENGL);
    Anar.init(this);
    Anar.drawAxis();
    myObj = createModule();
  }

  public Obj createModule(){
    // Initialize a local object
    Obj module = new Obj();

    // First construction: a basic line
    Pts construction = new Pts();

    Param c = new Param(100,0,200);
    Param z = new Param(0);
    
    construction.add(0,0,0);
    construction.add(30,20,0);
    construction.add(60, -20,0);
    construction.add(Anar.Pt(c,z,z));
    construction.add(120,0,40);
    construction.add(150,0,0);
    construction.add(180,0,0);

    // From this first set of points, create a curve of degree 4
    CSpline curve = new CSpline(construction,4);

    Param size = new Param(10,0,50);
    
    // Track parameters of point C
    Anar.sliders(c);
    Anar.sliders(size);

    // Create set of Points from an uniform distribution of points
    Pts curveWithPointsUniform = curve.getPts(200);
    module.add(curveWithPointsUniform);


    // now create a sweep from the darboux vector extracted 
    // from the sequence of points on the curve
    Pts ptsA;
    Pts ptsB;
    Pts ptsC;
    
    // /////////////////////////////
    ptsA = curveWithPointsUniform;
    ptsB = new Pts();
    ptsC = new Pts();
    
    for (int i1 = 1; i1<ptsA.numOfPts()-2; i1++){
        Pt p0 = ptsA.pt(i1-1);
        Pt p1 = ptsA.pt(i1);
        Pt p2 = ptsA.pt(i1+1);
        Pt p3 = ptsA.pt(i1+2);

        if (i1%33==0) module.add(new Circle(p0,p1,p2));
        PtDarboux omega = new PtDarboux(p0,p1,p2,p3,size);
        
        ptsB.add(omega);
//        ptsC.add(Anar.Pt(p1).minus(omega).multiply(-1).plus(p1));
    }
    ptsA.remove(0);
    
    module.add( new SweepTwoPaths(ptsA,ptsB));    
    
    // note: glitches appear in the sweep because the cardinal spline is not C_3 continuous
    
    Anar.camTarget(construction);

    return module;
  }


  public void draw(){
    background(155);
    myObj.draw();
  }


  public void keyPressed(){

    if(key==' ')
      RhinoScript.export(myObj,"curveTest");
  }


  public static void main(String[] args){
    PApplet.main(new String[]{Test05aCsplineDarboux.class.getName()});
  }
}

