Advertisement
dvulakh

ColorRectangleVis.java

Mar 4th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.44 KB | None | 0 0
  1.  
  2. import java.awt.image.*;
  3. import javax.imageio.*;
  4. import java.util.List;
  5. import java.util.*;
  6. import java.awt.*;
  7. import java.io.*;
  8.  
  9. public class ColorRectangleVis1 {
  10.    
  11.     /*** DRAWING CONSTANTS ***/
  12.     public static final Color DEAD = Color.DARK_GRAY;
  13.     public static final Color BG_COL = Color.BLACK;
  14.     public static final double QUOT_MULT = 100;
  15.     public static final double MIN_LOG = 1;
  16.     public static final int POINT_SZ = 10;
  17.     public static final int MARGIN = 10;
  18.     public static int MIN_HUE = 240;
  19.     public static int MAX_HUE = 0;
  20.    
  21.     /*** DRAWING CODES ***/
  22.     public static final int DIFF = 0;
  23.     public static final int SOLS = 1;
  24.     public static final int QUOT = 2;
  25.    
  26.     /*** RECTANGLE UNIT ***/
  27.     public class Rect {
  28.        
  29.         // Rectangle info
  30.         private LinkedList<Integer> per;
  31.         private ColorRectangleVis1 vis;
  32.         private double diff;
  33.         private Rect[] ch;
  34.         private int[] pos;
  35.         private int[] dim;
  36.         private int sol;
  37.        
  38.         // Constructor
  39.         public Rect(ColorRectangleVis1 vis, LinkedList<Integer> per, double diff, int sol) {
  40.             this.per = new LinkedList<Integer>(per);
  41.             this.pos = new int[] { 0, 0 };
  42.             this.dim = new int[] { 0, 0 };
  43.             this.diff = diff;
  44.             this.vis = vis;
  45.             this.sol = sol;
  46.         }
  47.        
  48.         // Value
  49.         public double v() { return vis.getCode() != DIFF ? vis.getCode() != SOLS ? sol > 0 ? QUOT_MULT * diff / sol : vis.mx[per.size()] : sol : diff; }
  50.        
  51.         // Build tree
  52.         public void init(int[] pos, int[] dim) {
  53.             this.pos = pos;
  54.             this.dim = dim;
  55.             ch = new Rect[vis.getM()];
  56.             if (diff < 0) {
  57.                 diff = 0;
  58.                 for (int i = 0; i < vis.getM(); i++) {
  59.                     per.add(i + 1);
  60.                     if (vis.getVisinfo().containsKey(per)) {
  61.                         ch[i] = vis.getVisinfo().get(per);
  62.                         ch[i].init( new int[] {
  63.                                 this.pos[0] + this.dim[0] / vis.getDim()[0] * (i % vis.getDim()[0]),
  64.                                 this.pos[1] + this.dim[1] / vis.getDim()[1] * (i / vis.getDim()[0]), },
  65.                                 new int[] { this.dim[0] / vis.getDim()[0], this.dim[1] / vis.getDim()[1] } );
  66.                         diff += ch[i].diff;
  67.                         sol += ch[i].sol;
  68.                     }
  69.                     per.removeLast();
  70.                 }
  71.             }
  72.             vis.mn[per.size()] = vis.mn[per.size()] < 0 ? v() : Math.min(vis.mn[per.size()], v());
  73.             vis.mx[per.size()] = Math.max(vis.mx[per.size()], v());
  74.         }
  75.        
  76.         // Determine point color
  77.         public Color colLinear(double v, double mx) { return Color.getHSBColor((float)(MIN_HUE + v * (MAX_HUE - MIN_HUE) / mx) / 360, 1, 1); }
  78.         public Color col(double v, double mx) {
  79.             //return Color.getHSBColor((float)(MIN_HUE + Math.log(Math.max(MIN_LOG, v)) * (MAX_HUE - MIN_HUE) / Math.log(Math.max(MIN_LOG, mx))) / 360, 1, 1);
  80.             return Color.getHSBColor(0, 0, (float)(0 + Math.log(Math.max(MIN_LOG, v)) * (100) / Math.log(Math.max(MIN_LOG, mx))) / 100);
  81.         }
  82.         public Color col() { return col(v() - vis.mn[per.size()], vis.mx[per.size()] - vis.mn[per.size()]); }
  83.        
  84.         // Draw point
  85.         public void draw() {
  86.             if (per.size() == vis.getDrawDepth()) {
  87.                 g.setColor(col());
  88.                 if (dim[0] == 1) {
  89.                     vis.getImg().setRGB(pos[0], pos[1], g.getColor().getRGB());
  90.                     System.out.println("yes");
  91.                 }
  92.                 else
  93.                     g.fillRect(pos[0], pos[1], dim[0], dim[1]);
  94.             }
  95.             else
  96.                 for (Rect r : ch)
  97.                     if (r != null)
  98.                         r.draw();
  99.         }
  100.        
  101.     }
  102.    
  103.     /*** PRIVATE MEMBER VARIABLES ***/
  104.     // Info file (use restart.txt)
  105.     private BufferedReader fin;
  106.     // Visualization info
  107.     private HashMap<List<Integer>, Rect> visinfo;
  108.     private List<Integer> prefix;
  109.     private int drawDepth;
  110.     private int drawCode;
  111.     public double[] mx;
  112.     public double[] mn;
  113.     private int[] dim;
  114.     private Rect root;
  115.     private int depth;
  116.     private int m;
  117.     // Image file
  118.     private BufferedImage img;
  119.     private Graphics2D g;
  120.     private String iname;
  121.    
  122.     /*** MUTATORS ***/
  123.     public void setVisinfo(HashMap<List<Integer>, Rect> treeinfo) { this.visinfo = treeinfo; }
  124.     public void setDrawDepth(int drawDepth) { this.drawDepth = drawDepth; }
  125.     public void setCode(int drawCode) { this.drawCode = drawCode; }
  126.     public void setIname(String iname) { this.iname = iname; }
  127.     public void setFin(BufferedReader fin) { this.fin = fin; }
  128.     public void setImg(BufferedImage img) { this.img = img; }
  129.     public void setDepth(int depth) { this.depth = depth; }
  130.     public void setRoot(Rect root) {  this.root = root; }
  131.     public void setDim(int[] dim) { this.dim = dim; }
  132.     public void setG(Graphics2D g) { this.g = g; }
  133.     public void setM(int m) { this.m = m; }
  134.    
  135.     /*** ACCESSORS ***/
  136.     public HashMap<List<Integer>, Rect> getVisinfo() { return visinfo; }
  137.     public int getDrawDepth() { return drawDepth; }
  138.     public BufferedReader getFin() { return fin; }
  139.     public BufferedImage getImg() { return img; }
  140.     public String getIname() { return iname; }
  141.     public int getCode() { return drawCode; }
  142.     public int getDepth() { return depth; }
  143.     public Rect getRoot() { return root; }
  144.     public Graphics2D getG() { return g; }
  145.     public int[] getDim() { return dim; }
  146.     public int getM() { return m; }
  147.  
  148.     /*** CONSTRUCTOR ***/
  149.     public ColorRectangleVis1(int m, String fname, String iname, int drawCode, int[] dim, String prefix_str) {
  150.        
  151.         // Set colors
  152.         if (drawCode == SOLS) {
  153.             int tmp = MAX_HUE;
  154.             MAX_HUE = MIN_HUE;
  155.             MIN_HUE = tmp;
  156.         }
  157.        
  158.         // Read prefix
  159.         prefix = new LinkedList<Integer>();
  160.         String[] prefix_elem = prefix_str.split("-|,|:|;| ");
  161.         for (String s : prefix_elem)
  162.             if (s.length() > 0)
  163.                 prefix.add(Integer.parseInt(s));
  164.        
  165.         // Set m and open files
  166.         System.out.println("Opening File...");
  167.         try {
  168.             this.fin = new BufferedReader(new FileReader(new File(fname)));
  169.             System.out.println("File Opened");
  170.         } catch (FileNotFoundException e) { System.out.println("Error Opening File"); }
  171.         this.mn = new double[m + 1];
  172.         this.mx = new double[m + 1];
  173.         this.drawCode = drawCode;
  174.         this.iname = iname;
  175.         this.dim = dim;
  176.         this.m = m;
  177.        
  178.         // Initialize min and max arrays
  179.         for (int i = 0; i <= m; i++) {
  180.             mx[i] = -1;
  181.             mn[i] = -1;
  182.         }
  183.        
  184.         // Generate visualization structure
  185.         readFile();
  186.         depth -= prefix.size();
  187.         System.out.println("Constructing Visualization...");
  188.         root = new Rect(this, new LinkedList<Integer>(), -1, 0);
  189.         root.init( new int[] { MARGIN, MARGIN }, new int[] { (int)(Math.pow(this.dim[0], depth) * POINT_SZ), (int)(Math.pow(this.dim[1], depth) * POINT_SZ) } );
  190.         System.out.println("Finished Constructing Visualization");
  191.        
  192.         for (int i = 0; i <= depth; i++) {
  193.            
  194.             // Generate image
  195.             drawDepth = i;
  196.             System.out.println("Drawing Image...");
  197.             img = new BufferedImage(2 * MARGIN + root.dim[0], 2 * MARGIN + root.dim[1], BufferedImage.TYPE_INT_RGB);
  198.             g = img.createGraphics();
  199.             g.setColor(BG_COL);
  200.             g.fillRect(0, 0, img.getWidth(), img.getHeight());
  201.             g.setColor(DEAD);
  202.             g.fillRect(MARGIN, MARGIN, root.dim[0], root.dim[1]);
  203.             root.draw();
  204.            
  205.             // Save image
  206.             System.out.println("Saving Image...");
  207.             try { ImageIO.write(img, "png", new File(iname + "_" + Integer.toString(i) + ".png")); } catch (IOException e) { System.out.println("Error Saving Image"); }
  208.             System.out.println("Done");
  209.            
  210.         }
  211.        
  212.     }
  213.    
  214.     /*** STRIP PERMUTATION PREFIX ***/
  215.     private LinkedList<Integer> strip(LinkedList<Integer> per) {
  216.         ListIterator<Integer> pit = per.listIterator(), rit = prefix.listIterator();
  217.         while (rit.hasNext())
  218.             if (!pit.hasNext() || rit.next() != pit.next())
  219.                 return null;
  220.             else
  221.                 pit.remove();
  222.         return per;
  223.     }
  224.    
  225.     /*** GENERATE IMAGE ***/
  226.     // Read file
  227.     private void readFile() {
  228.         String l;
  229.         System.out.println("Reading File...");
  230.         visinfo = new HashMap<List<Integer>, Rect>();
  231.         try { l = fin.readLine(); }
  232.         catch (IOException e) { System.out.println("Error Reading File"); return; }
  233.         double tot = 0;
  234.         while(l != null) {
  235.             String[] sep = l.split(" ");
  236.             int n = Integer.parseInt(sep[0]);
  237.             depth = Math.max(depth, n);
  238.             LinkedList<Integer> p = new LinkedList<Integer>();
  239.             for (int i = 1; i <= n; i++)
  240.                 p.add(Integer.parseInt(sep[i]));
  241.             int sol = Integer.parseInt(sep[n + 1]);
  242.             double t = Double.parseDouble(sep[n + 2]);
  243.             p = strip(p);
  244.             if (p != null)
  245.                 visinfo.put(p, new Rect(this, p, t, sol));
  246.             tot = t > 0 ? tot + t : tot;
  247.             try { l = fin.readLine(); }
  248.             catch (IOException e) { System.out.println("Error Reading File"); return; }
  249.         }
  250.         System.out.println("Finished Reading File");
  251.         System.out.println(tot);
  252.     }
  253.    
  254.     /*** MAIN ***/
  255.     public static void main(String[] args) {
  256.         new ColorRectangleVis1(Integer.parseInt(args[0]), args[1], args[2], Integer.parseInt(args[3]), new int[] {Integer.parseInt(args[4]), Integer.parseInt(args[5])}, args.length > 6 ? args[6] : "" );
  257.     }
  258.  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement