package p5;
import anar.*;




import processing.core.PApplet;

public class Test04qSnapToExistingGeometry extends PApplet {

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


  boolean record = false;

  
  Obj     form;

  Sliders mySliders;


  public void setup(){
    size(800,400,OPENGL);
    Anar.init(this);
    Anar.drawAxis(true);
    createForm();
  }


  void createForm(){
    Obj core = new Obj();
    form = new Obj();

    // CREATE PARAMETERS and Add to SliderList
    Param height = new Param(5,0,20,1.4f).tag("HH");
    Param angle = new Param(0.05f, -PI/16f,PI/16f).tag("RR");
    Param width = new Param(50,0,100).tag("WW");
    Anar.sliders(height);
    Anar.sliders(angle);
    Anar.sliders(width);

    // Create a Basic 3D form (a core structure)
    Face f = new Circle(width,5);

    for (int i = 0; i<40; i++)
      core.add(f = new Face(f,new RotateZ(angle).translateZ(height)));

    form.add(core);
    Anar.camTarget(core);

    // Create a second shape gate = (parametric sequence of lines)
    Param lineWidth = new Param(2,0,100).tag("LW");
    Param lineSpace = new Param(3,0,20).tag("LS");
    Anar.sliders(lineWidth);
    Anar.sliders(lineSpace);

    Obj gate = new Obj();
    Pts line = new Pts();
    Pt p;

    line.add(p = Anar.Pt(0,0,0));
    line.add(Anar.Pt(p,new TranslateY(lineWidth)));

    gate.add(line);

    for (int i = 0; i<10; i++)
      gate.add(line = new Pts(gate.lineEnd(),new TranslateX(lineSpace)));

    gate.add(new Pts(Anar.Pt(0,0,0),Anar.Pt(0,0,30)));

    form.add(gate); // added, gate will be displayed

    // Now we have two basic Shapes (core and gate)
    // let's track three points used as referential plan
    // for the orientation of the gate (snapped on the core)

    // Three points on the surface with three colors
    Pt ia = core.face(5).pt(0);
    ia.fill(255,0,0);
    Pt ib = core.face(6).pt(0);
    ib.fill(255,255,0);
    Pt ic = core.face(5).ptEnd();
    ic.fill(255);

    form.add(ia);
    form.add(ib);
    form.add(ic);

    // Create a transformation with a plan of reference
    // where the three given points define the referential plan)
    Transform t = new Transform(ia,ib);
    t.postTransform = false; // remove the post Translation, so the part is
    // moved
    // and placed according to the referential coordinates.

    // Apply the transformation to a copy of the object
    form.add(new Obj(gate,t));


    // Create similar as previous process but for many faces on the top of the
    // form.
    for (int i = 0; i<16; i++)
      for (int j = 0; j<4; j++){
        Pt iia = form.faceEnd(i+1).pt(j);
        iia.fill(255,0,0);
        Pt iib = form.faceEnd(i).pt(j);
        iib.fill(255,255,0);
        Pt iic = form.faceEnd(i+1).pt(j+1);
        iic.fill(255);

        form.add(iia);
        form.add(iib);
        form.add(iic);

        Transform tt = new Transform(iia,iib,iic);
        tt.postTransform = false;

        form.add(new Obj(gate,tt));
      }


//    println(form.numOfFaces());
  }


  public void draw(){
    background(155);
    Anar.draw();
    form.draw();
  }
  
  
  public void keyPressed(){
    Anar.add(form.orphaned());
    form.translateX(100);
    
  }


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

