Archive for May 4th, 2010

Uniform Nonrational B-Splines in B-Processor

Tuesday, May 4th, 2010

Implementation in B-Processor / Java:
public static class UniformNonrationalBSpline extends Command {
  private Space net;
  /**
   * Constructs a frame command
   * @param net Space
   */
  public UniformNonrationalBSpline(Space net) {
    this.net = net;   
    parameters.put(“subdivisions”, 5.0);
    parameters.put(“UNRBSname”, “Unifrom Nonrational BSpline”);
  }
  /**
   *
{@inheritDoc}
   */
  public String title() {
    return “Unifrom Nonrational BSpline”;
  }
  //bring edges in order
  public static List<Edge> order(List<Edge> edges) {
      LinkedList<Edge> ordered = new LinkedList();
      if (edges.size() > 0) {
       Edge first = edges.get(0);
          ordered.add(first);
          edges.remove(0);
          while (edges.size()>0) {
           for (int i = 0; i < edges.size(); i++) {
            search:
            if (edges.get(i).contains(ordered.get(ordered.size()-1).to)) {
             //check direction
             if (!edges.get(i).from.equals(ordered.get(ordered.size()-1).to)) {
              Vertex temp = edges.get(i).to;
              edges.get(i).to = edges.get(i).from;
              edges.get(i).from = temp;
             }
             //add at end of Linked list
             ordered.add(edges.get(i));
             edges.remove(i);
             break search;
            } else if (edges.get(i).contains(ordered.get(0).from)) {
           //check direction
             if (!edges.get(i).to.equals(ordered.get(ordered.size()-1).from)) {
              Vertex temp = edges.get(i).to;
              edges.get(i).to = edges.get(i).from;
              edges.get(i).from = temp;
             }
             // add at beginning of Linked List
             ordered.add(0, edges.get(i));
             edges.remove(i);
             break search;
            }
           }
          }
      }
      return ordered;
    }
 
  public Vertex unrbsCalc(List<Vertex> gConst, double t) {
   Vertex blend = new Vertex(0,0,0);
   if (gConst.size()==4) {
    double[] tempCalc = new double[4];
    tempCalc[0] = ((1-t)*(1-t)*(1-t))/6;
    tempCalc[1] = (3*t*t*t-6*t*t+4)/6;
    tempCalc[2] = (-3*t*t*t+3*t*t+3*t+1)/6;
    tempCalc[3] = (t*t*t)/6;
    for (int i=0; i<=3; i++) {
     blend.x = blend.x + tempCalc[i]*gConst.get(i).x;
     blend.y = blend.y + tempCalc[i]*gConst.get(i).y;
     blend.z = blend.z + tempCalc[i]*gConst.get(i).z;
    }
    System.out.println(blend.x);
   }
 return blend;
  }
  /** {@inheritDoc} */
  @Override
  public void evaluate() {
    String UNRBSname = (String) parameters.get(“UNRBSname”);
    double subdiv = parameters.getDouble(“subdivisions”);
   
    List<Vertex> vertices = new ArrayList(net.getVertices());
    List<Edge> edges = new ArrayList(net.getEdges());
    List<Vertex> orderPoints = new ArrayList();
    List<Vertex> curvePoints = new LinkedList();
    if (vertices.size()>=3) {
     LinkedList<Edge> nEdges = new LinkedList();
     nEdges = (LinkedList) order(edges);
     // write into List
     for (int i=0; i<nEdges.size(); i++) {
      orderPoints.add(nEdges.get(i).from);
     }
     orderPoints.add(nEdges.get(nEdges.size()-1).to);
     // make Spline go through endpoints
     for (int i=0; i<=2; i++) {
      orderPoints.add(orderPoints.get(orderPoints.size()-1));
      orderPoints.add(0,orderPoints.get(0));
     }
     
     // create space object to draw new elements
  Space result = Space.createUnion(UNRBSname);
 // calculate Uniform Nonrational Spline Curve
  for (int i=3; i<= orderPoints.size()-2; i++) {
   for (int t=0; t<subdiv; t++) {
    curvePoints.add(unrbsCalc(orderPoints.subList(i-3, i+1),t/subdiv));
   }
  }
  // draw Spline Curve
  for (int i=1;i<curvePoints.size()-1;i++) {
   result.add(new Edge(curvePoints.get(i-1),curvePoints.get(i)));
  }
  
  
  net.getOwner().add(result);
    }
  }
}
Share