1. public interface Plot<T>
  2. {
  3.     public boolean next();
  4.     public void reset();
  5.     public void end();
  6.     public T get();
  7. }
  8.  
  9. public class PlotCell3f implements Plot<Vec3i>
  10. {
  11.  
  12.     private final Vec3f size = new Vec3f();
  13.     private final Vec3f off = new Vec3f();
  14.     private final Vec3f pos = new Vec3f();
  15.     private final Vec3f dir = new Vec3f();
  16.  
  17.     private final Vec3i index = new Vec3i();
  18.    
  19.     private final Vec3f delta = new Vec3f();
  20.     private final Vec3i sign = new Vec3i();
  21.     private final Vec3f max = new Vec3f();
  22.    
  23.     private int limit;
  24.     private int plotted;
  25.    
  26.     public PlotCell3f(float offx, float offy, float offz, float width, float height, float depth)
  27.     {
  28.         off.set( offx, offy, offz );
  29.         size.set( width, height, depth );
  30.     }
  31.    
  32.     public void plot(Vec3f position, Vec3f direction, int cells)
  33.     {
  34.         limit = cells;
  35.        
  36.         pos.set( position );
  37.         dir.norm( direction );
  38.        
  39.         delta.set( size );
  40.         delta.div( dir );
  41.        
  42.         sign.x = (dir.x > 0) ? 1 : (dir.x < 0 ? -1 : 0);
  43.         sign.y = (dir.y > 0) ? 1 : (dir.y < 0 ? -1 : 0);
  44.         sign.z = (dir.z > 0) ? 1 : (dir.z < 0 ? -1 : 0);
  45.  
  46.         reset();
  47.     }
  48.    
  49.     @Override
  50.     public boolean next()
  51.     {
  52.         if (plotted++ > 0)
  53.         {
  54.             float mx = sign.x * max.x;
  55.             float my = sign.y * max.y;
  56.             float mz = sign.z * max.z;
  57.            
  58.             if (mx < my && mx < mz)
  59.             {
  60.                 max.x += delta.x;
  61.                 index.x += sign.x;
  62.             }
  63.             else if (mz < my && mz < mx)
  64.             {
  65.                 max.z += delta.z;
  66.                 index.z += sign.z;
  67.             }
  68.             else
  69.             {
  70.                 max.y += delta.y;
  71.                 index.y += sign.y;
  72.             }
  73.         }
  74.         return (plotted <= limit);
  75.     }
  76.    
  77.     @Override
  78.     public void reset()
  79.     {
  80.         plotted = 0;
  81.        
  82.         index.x = (int)Math.floor((pos.x - off.x) / size.x);
  83.         index.y = (int)Math.floor((pos.y - off.y) / size.y);
  84.         index.z = (int)Math.floor((pos.z - off.z) / size.z);
  85.        
  86.         float ax = index.x * size.x + off.x;
  87.         float ay = index.y * size.y + off.y;
  88.         float az = index.z * size.z + off.z;
  89.  
  90.         max.x = (sign.x > 0) ? ax + size.x - pos.x : pos.x - ax;
  91.         max.y = (sign.y > 0) ? ay + size.y - pos.y : pos.y - ay;
  92.         max.z = (sign.z > 0) ? az + size.z - pos.z : pos.z - az;
  93.         max.div( dir );
  94.     }
  95.  
  96.     @Override
  97.     public void end()
  98.     {
  99.         plotted = limit + 1;
  100.     }
  101.    
  102.     @Override
  103.     public Vec3i get()
  104.     {
  105.         return index;
  106.     }
  107.    
  108.     public Vec3f actual() {
  109.         return new Vec3f(index.x * size.x + off.x,
  110.                 index.y * size.y + off.y,
  111.                 index.z * size.z + off.z);
  112.     }
  113.  
  114.     public Vec3f size() {
  115.         return size;
  116.     }
  117.    
  118.     public void size(float w, float h, float d) {
  119.         size.set(w, h, d);
  120.     }
  121.    
  122.     public Vec3f offset() {
  123.         return off;
  124.     }
  125.    
  126.     public void offset(float x, float y, float z) {
  127.         off.set(x, y, z);
  128.     }
  129.    
  130.     public Vec3f position() {
  131.         return pos;
  132.     }
  133.    
  134.     public Vec3f direction() {
  135.         return dir;
  136.     }
  137.    
  138.     public Vec3i sign() {
  139.         return sign;
  140.     }
  141.    
  142.     public Vec3f delta() {
  143.         return delta;
  144.     }
  145.    
  146.     public Vec3f max() {
  147.         return max;
  148.     }
  149.    
  150.     public int limit() {
  151.         return limit;
  152.     }
  153.    
  154.     public int plotted() {
  155.         return plotted;
  156.     }
  157.    
  158.    
  159.    
  160. }