// __autor__ = Juan Carlos De Abreu R.
// __date__ = 03/19/2008
// __version__ = 1.0
//
// This file define an exercise for LAPAROS project.
// The exercise consist in cut the threads in the focus area.
//

include("cutThreadsInstrument.snf");

nCuts = 16;                     // Numer of cuts for the exersice.
cutTime = 5000;                 // Time for each cut.
exersiceTime = nCuts * cutTime; // Total time for do the exersice.


// Load a material file with the basic colors and get 
// the colors to use in the exersice.
Material("../Models/Basic.mtl");
redMtl = getMaterial("Red");
grayMtl = getMaterial("Gray");
blackMtl = getMaterial("Black");
greenMtl = getMaterial("Green");
yellowMtl = getMaterial("Yellow");

// Path for each geometry in the scene.
threadPath = "../Models/Line.obj";
simuladorPath = "../Models/Simulador3.obj";

// Load each geometry.
threadGeometry = Model(threadPath);
threadUpPartGeometry = Model(threadPath);
threadDownPartGeometry = Model(threadPath);
simuladorGeometry = Model(simuladorPath);

// Set default properties for each geometry.
threadGeometry.setMaterial(yellowMtl);
threadUpPartGeometry.setMaterial(blackMtl);
threadDownPartGeometry.setMaterial(blackMtl);
simuladorGeometry.castShadow(false);

// Set the camera position.
camera = Camera();
camera.setActive();
camera.setPos(0, 8, 10);

// 
threadNode = 
	Group(
		rotate(22, 0, 1, 0),
		rotate(22, 1, 0, 0),
		translate(0, 4, 0),
		Group(
			scale(1, 1, 1),
			threadGeometry
		),
		Group(
			translate(0, 4.5, 0),
			scale(1, 8, 1),
			threadUpPartGeometry
		),
		Group(
			translate(0, -4.5, 0),
			scale(1, 8, 1),
			threadDownPartGeometry
		)
	);

// 
scene = 
	Group(
		Group(
			translate(0, 4.2, 0),
			rotate(-90, 1, 0, 0),
			scale(25, 25, 25),
			simuladorGeometry
		),
		instrumentNode,
		threadNode,
		Group(translate(1, 7, 12), Light())
	);
	

// Function to change the position and orientation of the thread.
def changeThread() {
  newAngleX = rand(0, 45) - 22.5;
  newAngleY = rand(0, 45) - 22.5;
  newPosX = rand(0, 4) - 2;
  newPosY = rand(1, 3);
  newPosZ = rand(0, 2) - 1;
  threadNode.get(0).setAngle(newAngleX);
  threadNode.get(1).setAngle(newAngleY);
  threadNode.get(2).setX(newPosX);
  threadNode.get(2).setY(newPosY);
  threadNode.get(2).setZ(newPosZ);
  threadGeometry.setMaterial(yellowMtl);
}
Timer(cutTime, -1, changeThread(), changeThread());

// Set the keys for open/close the instrument.
setKeyFunc("A", close());
setKeyFunc("Z", open());

def open() {
  displayInstrument(openInstrument);
}

def close() {
  displayInstrument(closeInstrument);  
  if (collisionDetection(leftHeadInstrumentCloseGeometry, threadGeometry) || 
      collisionDetection(rightHeadInstrumentCloseGeometry, threadGeometry))
    threadGeometry.setMaterial(greenMtl);
  else if (collisionDetection(leftHeadInstrumentCloseGeometry, threadUpPartGeometry) || 
      collisionDetection(rightHeadInstrumentCloseGeometry, threadUpPartGeometry))
    threadGeometry.setMaterial(redMtl);
  else if (collisionDetection(leftHeadInstrumentCloseGeometry, threadDownPartGeometry) || 
      collisionDetection(rightHeadInstrumentCloseGeometry, threadDownPartGeometry))
    threadGeometry.setMaterial(redMtl);
}
