Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.*;
- import javax.imageio.*;
- import java.util.List;
- import java.util.*;
- import java.awt.*;
- import java.io.*;
- public class ColorRectangleVis1 {
- /*** DRAWING CONSTANTS ***/
- public static final Color DEAD = Color.DARK_GRAY;
- public static final Color BG_COL = Color.BLACK;
- public static final double QUOT_MULT = 100;
- public static final double MIN_LOG = 1;
- public static final int POINT_SZ = 10;
- public static final int MARGIN = 10;
- public static int MIN_HUE = 240;
- public static int MAX_HUE = 0;
- /*** DRAWING CODES ***/
- public static final int DIFF = 0;
- public static final int SOLS = 1;
- public static final int QUOT = 2;
- /*** RECTANGLE UNIT ***/
- public class Rect {
- // Rectangle info
- private LinkedList<Integer> per;
- private ColorRectangleVis1 vis;
- private double diff;
- private Rect[] ch;
- private int[] pos;
- private int[] dim;
- private int sol;
- // Constructor
- public Rect(ColorRectangleVis1 vis, LinkedList<Integer> per, double diff, int sol) {
- this.per = new LinkedList<Integer>(per);
- this.pos = new int[] { 0, 0 };
- this.dim = new int[] { 0, 0 };
- this.diff = diff;
- this.vis = vis;
- this.sol = sol;
- }
- // Value
- public double v() { return vis.getCode() != DIFF ? vis.getCode() != SOLS ? sol > 0 ? QUOT_MULT * diff / sol : vis.mx[per.size()] : sol : diff; }
- // Build tree
- public void init(int[] pos, int[] dim) {
- this.pos = pos;
- this.dim = dim;
- ch = new Rect[vis.getM()];
- if (diff < 0) {
- diff = 0;
- for (int i = 0; i < vis.getM(); i++) {
- per.add(i + 1);
- if (vis.getVisinfo().containsKey(per)) {
- ch[i] = vis.getVisinfo().get(per);
- ch[i].init( new int[] {
- this.pos[0] + this.dim[0] / vis.getDim()[0] * (i % vis.getDim()[0]),
- this.pos[1] + this.dim[1] / vis.getDim()[1] * (i / vis.getDim()[0]), },
- new int[] { this.dim[0] / vis.getDim()[0], this.dim[1] / vis.getDim()[1] } );
- diff += ch[i].diff;
- sol += ch[i].sol;
- }
- per.removeLast();
- }
- }
- vis.mn[per.size()] = vis.mn[per.size()] < 0 ? v() : Math.min(vis.mn[per.size()], v());
- vis.mx[per.size()] = Math.max(vis.mx[per.size()], v());
- }
- // Determine point color
- public Color colLinear(double v, double mx) { return Color.getHSBColor((float)(MIN_HUE + v * (MAX_HUE - MIN_HUE) / mx) / 360, 1, 1); }
- public Color col(double v, double mx) {
- //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);
- return Color.getHSBColor(0, 0, (float)(0 + Math.log(Math.max(MIN_LOG, v)) * (100) / Math.log(Math.max(MIN_LOG, mx))) / 100);
- }
- public Color col() { return col(v() - vis.mn[per.size()], vis.mx[per.size()] - vis.mn[per.size()]); }
- // Draw point
- public void draw() {
- if (per.size() == vis.getDrawDepth()) {
- g.setColor(col());
- if (dim[0] == 1) {
- vis.getImg().setRGB(pos[0], pos[1], g.getColor().getRGB());
- System.out.println("yes");
- }
- else
- g.fillRect(pos[0], pos[1], dim[0], dim[1]);
- }
- else
- for (Rect r : ch)
- if (r != null)
- r.draw();
- }
- }
- /*** PRIVATE MEMBER VARIABLES ***/
- // Info file (use restart.txt)
- private BufferedReader fin;
- // Visualization info
- private HashMap<List<Integer>, Rect> visinfo;
- private List<Integer> prefix;
- private int drawDepth;
- private int drawCode;
- public double[] mx;
- public double[] mn;
- private int[] dim;
- private Rect root;
- private int depth;
- private int m;
- // Image file
- private BufferedImage img;
- private Graphics2D g;
- private String iname;
- /*** MUTATORS ***/
- public void setVisinfo(HashMap<List<Integer>, Rect> treeinfo) { this.visinfo = treeinfo; }
- public void setDrawDepth(int drawDepth) { this.drawDepth = drawDepth; }
- public void setCode(int drawCode) { this.drawCode = drawCode; }
- public void setIname(String iname) { this.iname = iname; }
- public void setFin(BufferedReader fin) { this.fin = fin; }
- public void setImg(BufferedImage img) { this.img = img; }
- public void setDepth(int depth) { this.depth = depth; }
- public void setRoot(Rect root) { this.root = root; }
- public void setDim(int[] dim) { this.dim = dim; }
- public void setG(Graphics2D g) { this.g = g; }
- public void setM(int m) { this.m = m; }
- /*** ACCESSORS ***/
- public HashMap<List<Integer>, Rect> getVisinfo() { return visinfo; }
- public int getDrawDepth() { return drawDepth; }
- public BufferedReader getFin() { return fin; }
- public BufferedImage getImg() { return img; }
- public String getIname() { return iname; }
- public int getCode() { return drawCode; }
- public int getDepth() { return depth; }
- public Rect getRoot() { return root; }
- public Graphics2D getG() { return g; }
- public int[] getDim() { return dim; }
- public int getM() { return m; }
- /*** CONSTRUCTOR ***/
- public ColorRectangleVis1(int m, String fname, String iname, int drawCode, int[] dim, String prefix_str) {
- // Set colors
- if (drawCode == SOLS) {
- int tmp = MAX_HUE;
- MAX_HUE = MIN_HUE;
- MIN_HUE = tmp;
- }
- // Read prefix
- prefix = new LinkedList<Integer>();
- String[] prefix_elem = prefix_str.split("-|,|:|;| ");
- for (String s : prefix_elem)
- if (s.length() > 0)
- prefix.add(Integer.parseInt(s));
- // Set m and open files
- System.out.println("Opening File...");
- try {
- this.fin = new BufferedReader(new FileReader(new File(fname)));
- System.out.println("File Opened");
- } catch (FileNotFoundException e) { System.out.println("Error Opening File"); }
- this.mn = new double[m + 1];
- this.mx = new double[m + 1];
- this.drawCode = drawCode;
- this.iname = iname;
- this.dim = dim;
- this.m = m;
- // Initialize min and max arrays
- for (int i = 0; i <= m; i++) {
- mx[i] = -1;
- mn[i] = -1;
- }
- // Generate visualization structure
- readFile();
- depth -= prefix.size();
- System.out.println("Constructing Visualization...");
- root = new Rect(this, new LinkedList<Integer>(), -1, 0);
- 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) } );
- System.out.println("Finished Constructing Visualization");
- for (int i = 0; i <= depth; i++) {
- // Generate image
- drawDepth = i;
- System.out.println("Drawing Image...");
- img = new BufferedImage(2 * MARGIN + root.dim[0], 2 * MARGIN + root.dim[1], BufferedImage.TYPE_INT_RGB);
- g = img.createGraphics();
- g.setColor(BG_COL);
- g.fillRect(0, 0, img.getWidth(), img.getHeight());
- g.setColor(DEAD);
- g.fillRect(MARGIN, MARGIN, root.dim[0], root.dim[1]);
- root.draw();
- // Save image
- System.out.println("Saving Image...");
- try { ImageIO.write(img, "png", new File(iname + "_" + Integer.toString(i) + ".png")); } catch (IOException e) { System.out.println("Error Saving Image"); }
- System.out.println("Done");
- }
- }
- /*** STRIP PERMUTATION PREFIX ***/
- private LinkedList<Integer> strip(LinkedList<Integer> per) {
- ListIterator<Integer> pit = per.listIterator(), rit = prefix.listIterator();
- while (rit.hasNext())
- if (!pit.hasNext() || rit.next() != pit.next())
- return null;
- else
- pit.remove();
- return per;
- }
- /*** GENERATE IMAGE ***/
- // Read file
- private void readFile() {
- String l;
- System.out.println("Reading File...");
- visinfo = new HashMap<List<Integer>, Rect>();
- try { l = fin.readLine(); }
- catch (IOException e) { System.out.println("Error Reading File"); return; }
- double tot = 0;
- while(l != null) {
- String[] sep = l.split(" ");
- int n = Integer.parseInt(sep[0]);
- depth = Math.max(depth, n);
- LinkedList<Integer> p = new LinkedList<Integer>();
- for (int i = 1; i <= n; i++)
- p.add(Integer.parseInt(sep[i]));
- int sol = Integer.parseInt(sep[n + 1]);
- double t = Double.parseDouble(sep[n + 2]);
- p = strip(p);
- if (p != null)
- visinfo.put(p, new Rect(this, p, t, sol));
- tot = t > 0 ? tot + t : tot;
- try { l = fin.readLine(); }
- catch (IOException e) { System.out.println("Error Reading File"); return; }
- }
- System.out.println("Finished Reading File");
- System.out.println(tot);
- }
- /*** MAIN ***/
- public static void main(String[] args) {
- 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] : "" );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement