/*
* Copyright (C) 2014 Steve Ratcliffe
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 or
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package test.svg.subdiv;
import uk.me.parabola.imgfmt.app.Area;
import uk.me.parabola.imgfmt.app.trergn.Subdivision;
import test.display.CommonDisplay;
import test.svg.XY;
/**
* Read in a .img and write a svg file that shows the subdivisions it contains.
*
*/
public class DivDisp
extends CommonDisplay
{
private SvgDivDoc doc
;
private int level
;
public DivDisp
(SvgDivDoc doc
) {
this.
doc = doc
;
}
protected void print
() {
openTre
();
openRgn
();
DivStat stats =
new DivStat
();
Area bounds = tre.
getBounds();
int maxDimension = bounds.
getMaxDimension();
XY.
setScale(maxDimension /
6000 +
1);
Subdivision
[] subdivisions = tre.
subdivForLevel(level
);
for (Subdivision div : subdivisions
) {
// Get width and heights
int width = div.
getWidth();
int height = div.
getHeight();
assert width
>=
0 && height
>=
0;
long area =
(long) width
* height
;
XY blc = doc.
addDiv(div, width, height
);
if (blc ==
null)
continue;
int npoints = rgn.
pointsForSubdiv(div
).
size();
int nlines = rgn.
linesForSubdiv(div
).
size();
int nshapes = rgn.
shapesForSubdiv(div
).
size();
stats.
add(area, npoints, nlines, nshapes
);
doc.
addLabel(div, blc, npoints, nlines, nshapes
);
}
System.
out.
printf("Total elements: %d\n", stats.
totalElements());
System.
out.
printf("Number of divs: %d\n", stats.
nDivs());
System.
out.
printf("Average elements per div %d\n", stats.
avgElementsPerDiv());
stats.
printHistogram();
}
/**
* @param name The file name being displayed.
* @param level The zoom level to display. Default is 0 (the most detailed level).
*/
public void run
(String name,
int level
) {
this.
level = level
;
this.
setOutStream(System.
err);
this.
display(name,
"TRE");
}
class DivStat
{
private int ndivs
;
private long totArea
;
private int npoints
;
private int nlines
;
private int nshapes
;
private int[] histogram =
new int[12];
public void add
(long area,
int npoints,
int nlines,
int nshapes
) {
this.
ndivs++
;
this.
totArea += area
;
this.
npoints += npoints
;
this.
nlines += nlines
;
this.
nshapes += nshapes
;
if (area
> 0) {
double v =
Math.
log10(area
);
histogram
[(int) v
]++
;
}
//System.out.printf("%d %d %d\n", this.ndivs, area, npoints+nlines+nshapes);
}
public int totalElements
() {
return npoints + nlines + nshapes
;
}
public int avgElementsPerDiv
() {
return totalElements
() / ndivs
;
}
public void printHistogram
() {
for (int i =
0; i
< histogram.
length; i++
) {
System.
out.
printf("%2d: %d\n", i, histogram
[i
]);
}
}
public int nDivs
() {
return ndivs
;
}
}
}