/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.net.*;
import java.lang.reflect.*;

public class Test 
{
  double foo;
  public static void assert(boolean b)
  {
    if (!b) {
      throw new Error();
    }
  }

  public static void main(String s[]) 
  {
    // testExpressions();
    //timeExpressions();
    time();
    System.exit(0);
  }

  private static void testExpressions() 
  {
    //// Test ELExpressions
    // Add(1, Add(2))
    //  a  b   c  d
    ELOperator add = new Add();

    ELScalar b = new ELScalar(1);
    ELScalar d = new ELScalar(0.5);
    
    ELExpression cArgs[] = {d};
    ELCall c = new ELCall(add, cArgs);

    ELExpression aArgs[] = {b, c};
    ELCall a = new ELCall(add, aArgs);

    assert(b.numNestedSubexpressions() == 1);
    assert(c.numNestedSubexpressions() == 2);
    assert(a.numNestedSubexpressions() == 4);
    assert(a.unparse().equals("Add(1.0, Add(0.5))"));
    assert(a.getNestedSubexpression(0) == a);
    assert(a.getNestedSubexpression(1) == b);
    assert(a.getNestedSubexpression(2) == c);

    GeneticParameters gp = new GeneticParameters(".");
    ELCall e = (ELCall)gp.parseELExpression("Add(Sub(Mul(LowColorNoise(),1), LenaImage()), HGradient())");
    assert(e.depth() == 3);

    ELExpressionGraph graph = new ELExpressionGraph();
    JFrame frame = new JFrame();
    frame.getContentPane().add(graph);
    graph.setExpression(e);
    frame.setVisible(true);
  }

  // Handy for optimizing a single expression.
  static void time() {
    GeneticParameters gp = new GeneticParameters(".");
    ELCall e1 = (ELCall)gp.parseELExpression("ColorGradient()");
    ELMatrix out = ELMatrix.alloc(128, 128);
    ELEnvironment evt = new ELEnvironment();
    for(int i = 0; i < 20; i++) {
      long t1 = System.currentTimeMillis();
      for(int j= 0; j < 5; j++) {
        e1.evaluate(out, evt, new ParameterList());
      }
      System.out.println((System.currentTimeMillis() - t1));
    }
  }

  static void timeExpressions() {
    GeneticParameters gp = new GeneticParameters(".");
    ELCall e1 = (ELCall)gp.parseELExpression("Add(Sub(EnvironmentMap(LowColorNoise(),1), Blur(LenaImage())), HGradient())");
    ELCall e2 = (ELCall)gp.parseELExpression("Sub(Sub(Distort(LowColorNoise(),1), ToHSV(LenaImage())), HGradient())");
    ELMorphInterpolator morph = new ELMorphInterpolator(e1, e2);
    
    ELMatrix out = ELMatrix.alloc(256, 256);
    ELEnvironment evt = new ELEnvironment();

    for(int i = 0; i < 10; i++) {
      long t1 = System.currentTimeMillis();
      morph.evaluate(out, evt, new ParameterList());
      System.out.println((System.currentTimeMillis() - t1));
    }
  }
}
