package commons.graph.worker;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import commons.graph.Calculator;
import commons.graph.model.Node;
import commons.graph.model.Nodes;
import commons.graph.model.Point;
public class SmartVirtualizer extends EdgeWorker
{

Set<String> done = new HashSet<String>();
double aberration = 0.;

public SmartVirtualizer (double aberration)
{
   this.aberration = aberration * 2;
}

/**
 * Slightely redundant. Consider using specific traversion logic for this.
 */
public void work (String a, String b)
{
   if (done.contains(a)) return;
   Node parent = nodes.getNode(a);
   int nc = edges.countChildren(a);
   double step = Calculator.PI2 / (double)nc;
   int n = 0;
   if (nc > 0)
   {
      Nodes nodes = edges.getChildNodes(a);
      for (Iterator<Node> iter = nodes.nodeIterator(); iter.hasNext();)
      {
         Node child = iter.next();
         double radius = edges.distance(parent.label, child.label);
         if (depth > 1)
         {
            double d = (depth - 1) * 1.5;
            radius /= d;
         }
         double alpha = n++ * step;
         double rand = Math.random() * aberration;
         double dev = aberration * alpha / 2 - rand * alpha;
//         OsUtl.trace("rand:" + rand + " dev:" + dev + " aberration*alpha:"+(aberration * alpha)+" rand*alpha:"+(rand * alpha));
         alpha = alpha + dev;
         Point point = Calculator.circularPoint(radius, alpha);
         point.add(parent.x, parent.y);
         
         child.x = point.x;
         child.y = point.y;
      }
   }
   done.add(a);
}
}
