Cleaning a Java Area object

To determine the projection of a given 3D part, I am slicing the parts in a sequence of cuts at different heights. Each cut creates one or more perimeters or rings. The approximated desired projection is obtained by performing the union of each cut. 
 The image above represents the output perimeter in red color, and the number tells me there is just one. Each perimeter start is marked by a circle and its end by a square. The image below, however, does have two different output perimeters, that is, the vertical projection can be decomposed in two different disconnected areas.
 I am coding in Java and using the Area class from java.awt. The main reason is that the class does include a method to do the union of areas.  However, after some weird results and some debugging, I realized the output of such a union method is not always clean. Sometimes the output is polluted with some additional paths that are not really useful or valid. In my case, I was getting some paths consisting in just one point. So what I did was to sanitize the output by rebuilding the object with only the valid paths (those who do not create an empty area).

Not a big deal but I wasted some time till I figure this was the problem. This code worked nicely for me.


Area clean(Area a) {
    GeneralPath gp=new GeneralPath();
    Area out=new Area();
    float[] coords = new float[6]; 
    int type;
    for(PathIterator pi = a.getPathIterator(null); !pi.isDone(); pi.next() ) {
      type=pi.currentSegment(coords);
      if(type==PathIterator.SEG_MOVETO) { gp=new GeneralPath(); gp.moveTo(coords[0],coords[1]); }
      if(type==PathIterator.SEG_LINETO) gp.lineTo(coords[0],coords[1]); 
      if(type==PathIterator.SEG_CLOSE)  {
        gp.closePath(); 
        Area n=new Area(gp);
        if(! n.isEmpty() ) out.add(n); // only the non-null areas are added
      }
    }
    return out; 
  }

Comments

Popular posts from this blog

VFD control with Arduino using RS485 link

How to get sinusoidal s-curve for a stepper motor

Stepper motor step signal timing calculation