package brtestgratings;

import javax.media.j3d.*;
import javax.vecmath.*;

public class RotateGrating
{
    int N;// = 600;
    Color3f colour;
    private Alpha alpha = new Alpha(-1, 20000);
    private Transform3D cmt=new Transform3D();
    private Transform3D temp=new Transform3D();

    Canvas3D canvas;
    GraphicsContext3D gc;
    double a=0, width, step;
    int v;
    double height, radius; //height=8.0, radius=4;
    double shift=1.0;
    QuadArray twistStrip;// = new QuadArray(N,
                         //QuadArray.COORDINATES  | QuadArray.COLOR_3);

  public void RotateGrating()
  {
    for(v = 0; v < N; v+=4, a=v*step*Math.PI/(N/2))
    {
      twistStrip.setCoordinate(v, new Point3d(radius*Math.cos(a),
                                  height,radius*Math.sin(a)));
      twistStrip.setCoordinate(v+1, new Point3d(radius*Math.cos(a),
                                  -height,radius*Math.sin(a)));
      twistStrip.setCoordinate(v+2, new Point3d(radius*Math.cos(a+width),
                                  -height, radius*Math.sin(a+width)));
      twistStrip.setCoordinate(v+3, new Point3d(radius*Math.cos(a+width),
                                   height, radius*Math.sin(a+width)));

      twistStrip.setColor(v, colour);
      twistStrip.setColor(v+1, colour);
      twistStrip.setColor(v+2, colour);
      twistStrip.setColor(v+3, colour);
   }
  }

      public  Geometry getRotate(Canvas3D canvasIn, int orient)
      {canvas=canvasIn;
        gc=canvas.getGraphicsContext3D();
        double angle = alpha.value() * 2.0*Math.PI;
        if (orient==0)  // horizontal orientation
        {
         temp.rotY(-angle);
         cmt.rotZ(Math.PI/2);
         cmt.mul(temp);
         gc.setModelTransform(cmt);
        }
        else if (orient==1)  // Vertical orientation
        {
         cmt.rotY(-angle);
         gc.setModelTransform(cmt);
        }
         return twistStrip;
      }

      public void setParameter(int numberOfArrayIn,
                               double heightIn,
                               double radiusIn,
                               double widthIn,
                               Color3f clr)
      {
        height=heightIn;
        radius=radiusIn;
        N=numberOfArrayIn;
        step=widthIn;
        width=step*Math.PI/(N/4);
        twistStrip = new QuadArray(N,
                     QuadArray.COORDINATES  | QuadArray.COLOR_3);
        colour=clr;
        RotateGrating();
      }
}