/* Copyright (C) 2014 A.Vontobel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The license is in LICENCE-GPL.txt.
* If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
*/
package net.sf.drawj2d.bgd;
import net.sf.drawj2d.cmd.API;
import java.awt.Graphics2D;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import net.sf.drawj2d.cmd.Fkt;
/**
*
* @author av
*/
public class BgdInterpreter {
API api;
clBGD bgd;
double mmwidth;
double mmheight;
double[] bounds = {-4, 11, -4, 4}; // even if something fails we want dimensions > 0
private final double mstliste[] = {1, 2, 5}; // vorzuschlagende Massstäbe: 1:10, 1: 20, 1: 50, 1: 100, etc
private boolean verbose = false;
private final boolean debug = false;
/**
* Creates a new instance of BgdInterpreter
* @param g The graphic context. java.awt.Graphics2D
*/
public BgdInterpreter (Graphics2D g, double mmwidth, double mmheight) {
api = new API (g);
assert mmwidth > 0;
assert mmheight > 0;
this.mmwidth = mmwidth;
this.mmheight = mmheight;
}
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
/** Let the interpreter source the input file
* @param sourcefile source file or "_opt_stdin"
* @return true if no exception has been caught. */
public boolean sourceFile(String sourcefile) {
boolean success = false;
try {
if(!sourcefile.equals("_opt_stdin")) {
bgd = new clBGD(sourcefile, verbose);
success = true;
} else {
// LinkedList sourcelist = new LinkedList();
InputStreamReader eingabestrom;
eingabestrom = new InputStreamReader(System.in); // Read Stdin
BufferedReader eingabe = new BufferedReader(eingabestrom);
bgd = new clBGD(eingabe, verbose);
eingabe.close();
eingabestrom.close();
// // Evaluate sourcelist
// if (verbose) System.out.println();
// int zeilennr = 0;
// for (String s : sourcelist) {
// zeilennr++;
// if (verbose) System.out.println(Fkt.nf(zeilennr, 3) + ": " + s);
// Thing input = new Thing(s);
// Thing ans = interp.eval(input);
// if (verbose) System.out.println(" @ " + ans.toString());
// }
// if (verbose) System.out.println();
success = true;
}
} catch (IOException ex) {
System.err.println(ex.getLocalizedMessage());
if (debug) ex.printStackTrace();
success = false;
}
bounds = calculateBounds();
return success;
}
private double[] calculateBounds() {
double minX = Double.MAX_VALUE;
double maxX = -Double.MAX_VALUE;
double minZ = Double.MAX_VALUE;
double maxZ = -Double.MAX_VALUE;
// Hintergrundpunkte
for (clHintergrundPunkt aktpkt : bgd.getHgPunkte()) {
if (aktpkt.getPkt()[0] > maxX) maxX = aktpkt.getPkt()[0];
if (aktpkt.getPkt()[0] < minX) minX = aktpkt.getPkt()[0];
if (aktpkt.getPkt()[1] > maxZ) maxZ = aktpkt.getPkt()[1];
if (aktpkt.getPkt()[1] < minZ) minZ = aktpkt.getPkt()[1];
}
// Hintergrundlinien
for (clHintergrundLinie aktuell : bgd.getHgLinien()) {
double x0 = aktuell.getVon()[0]; double z0 = aktuell.getVon()[1];
double x1 = aktuell.getBis()[0]; double z1 = aktuell.getBis()[1];
if (x0 > maxX) maxX = x0; if (x0 < minX) minX = x0;
if (z0 > maxZ) maxZ = z0; if (z0 < minZ) minZ = z0;
if (x1 > maxX) maxX = x1; if (x1 < minX) minX = x1;
if (z1 > maxZ) maxZ = z1; if (z1 < minZ) minZ = z1;
}
// Hintergrundkreise
for (clHintergrundKreis aktuell : bgd.getHgKreise()) {
double x0 = aktuell.getZentrum()[0] - aktuell.getRadius();
double x1 = aktuell.getZentrum()[0] + aktuell.getRadius();
double z0 = aktuell.getZentrum()[1] - aktuell.getRadius();
double z1 = aktuell.getZentrum()[1] + aktuell.getRadius();
if (x0 > maxX) maxX = x0; if (x0 < minX) minX = x0;
if (z0 > maxZ) maxZ = z0; if (z0 < minZ) minZ = z0;
if (x1 > maxX) maxX = x1; if (x1 < minX) minX = x1;
if (z1 > maxZ) maxZ = z1; if (z1 < minZ) minZ = z1;
}
// Hintergrundbögen
for (clHintergrundBogen aktuell : bgd.getHgBogen()) {
// TODO prüfen
double x0 = aktuell.getZentrum()[0] + aktuell.getRadius()*Math.cos(aktuell.getSektor()[0]);
double x1 = aktuell.getZentrum()[0] + aktuell.getRadius()*Math.cos(aktuell.getSektor()[1]);
double z0 = aktuell.getZentrum()[1] - aktuell.getRadius()*Math.sin(aktuell.getSektor()[0]);
double z1 = aktuell.getZentrum()[1] - aktuell.getRadius()*Math.sin(aktuell.getSektor()[1]);
// TODO ergänzen: wenn Bogen bei 0°,90°,180°,270° durchläuft, sind diese Punkte massgebend
if (x0 > maxX) maxX = x0; if (x0 < minX) minX = x0;
if (z0 > maxZ) maxZ = z0; if (z0 < minZ) minZ = z0;
if (x1 > maxX) maxX = x1; if (x1 < minX) minX = x1;
if (z1 > maxZ) maxZ = z1; if (z1 < minZ) minZ = z1;
}
if (minX == maxX && minZ == maxZ) {
minX -= 6;
maxX += 6;
minZ -= 4;
maxZ += 4;
}
// wenn Keine Daten vorhanden sind:
if (minX == Double.MAX_VALUE) minX = -1;
if (maxX == -Double.MAX_VALUE) maxX = 11;
if (minZ == Double.MAX_VALUE) minZ = -4;
if (maxZ == -Double.MAX_VALUE) maxZ = 4;
if (debug) System.out.println("Bounds x: " + minX + " ... " + maxX);
if (debug) System.out.println("Bounds z: " + minZ + " ... " + maxZ);
double[] b = {minX, maxX, minZ, maxZ};
return b;
}
/**
* Do actually draw.
* @param autoscale
*/
public void interpret(boolean autoscale) {
if (autoscale) {
double unitwidth = bounds[1] - bounds[0];
assert unitwidth >= 0;
double unitheight = bounds[3] - bounds[2];
assert unitheight >= 0;
assert Math.max(unitwidth, unitheight) > 0;
double maxmmx = mmwidth / unitwidth;
double maxmmz = mmheight / unitheight;
double maxmm = Math.min(maxmmx, maxmmz);
if (debug) System.out.println("Autoscale maxmm: " + maxmm);
double mst = 1000d / maxmm;
double[] nächstgrösser = new double[mstliste.length];
double temp;
for (int i = 0; i < mstliste.length; i++) {
temp = Math.ceil(Math.log(mst / mstliste[i]) / Math.log(10));
nächstgrösser[i] = mstliste[i] * Math.pow(10, temp);
}
mst = Fkt.min(nächstgrösser);
double mm = 1000d / mst;
if (verbose) System.out.println("Autoscale 1:" + Fkt.nf(mst,1) + ", mm: " + mm);
api.moveto(25, mmheight - 15); // in mm, unitlenght not yet set
api.label("1:" + Fkt.nf(mst,1));
api.moveto(0, 0);
api.unitlength(mm);
// Do autocenter. Command line parameters X, Y, center have no effect.
double dX = api.mm(mmwidth/2d) - (bounds[0] + unitwidth/2d);
double dY = api.mm(mmheight/2d) - (bounds[2] + unitheight/2d);
if (verbose) System.out.println("Autooffset dX = " + Fkt.nf(dX, 3) + "m, dY = " + Fkt.nf(dY, 3) + "m");
if (verbose) System.out.println();
api.offset(dX, dY);
}
// Hintergrundpunkte
for (clHintergrundPunkt aktpkt : bgd.getHgPunkte()) {
double x1 = aktpkt.getPkt()[0];
double z1 = aktpkt.getPkt()[1];
api.point(x1, z1);
}
// Hintergrundlinien
for (clHintergrundLinie aktlinie : bgd.getHgLinien()) {
double x0 = aktlinie.getVon()[0]; double z0 = aktlinie.getVon()[1];
double x1 = aktlinie.getBis()[0]; double z1 = aktlinie.getBis()[1];
api.line(x0, z0, x1, z1);
}
// Hintergrundkreise
for (clHintergrundKreis aktkreis : bgd.getHgKreise()) {
double x1 = aktkreis.getZentrum()[0];
double z1 = aktkreis.getZentrum()[1];
double r = aktkreis.getRadius();
api.circle(x1, z1, r);
}
// Hintergrundbögen
for (clHintergrundBogen aktbogen : bgd.getHgBogen()) {
double x1 = aktbogen.getZentrum()[0];
double z1 = aktbogen.getZentrum()[1];
double r = aktbogen.getRadius();
// Sektoren: in bgd im Gegenuhrzeigersinn
// in Drawj2d im Uhrzeigersinn
double startsektor = -aktbogen.getSektor()[1];
double endsektor = -aktbogen.getSektor()[0];
api.arc(x1, z1, r, startsektor, endsektor);
}
}
}
Ads help maintain this website.