Skip to content

Commit 573ff36

Browse files
committed
Remove logger dependency, and mutable transform methods
also use int[] instead of ArrayList, faster Note: getPoint3D still takes the biggest chunk of time while rotating the plot in 3d
1 parent 0b626f0 commit 573ff36

4 files changed

Lines changed: 31 additions & 49 deletions

File tree

src/main/java/com/babai/ssplot/cli/SSPlotCLI.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ public class SSPlotCLI {
4040
Plotter plt;
4141

4242
public SSPlotCLI() {
43-
CLILogger clilogger = new CLILogger();
44-
clilogger.log("Welcome to SSPlot CLI!");
43+
// CLILogger clilogger = new CLILogger();
44+
// clilogger.log("Welcome to SSPlot CLI!");
4545

46-
plt = new Plotter(clilogger);
46+
System.out.println("Welcome to SSPlot CLI!");
47+
plt = new Plotter();
4748
}
4849

49-
public static void main (String[] args) {
50+
public static void main(String[] args) {
5051
var cli = new SSPlotCLI();
5152
ArgParse.nextArg("i", args).ifPresentOrElse(
5253
inputFile -> {

src/main/java/com/babai/ssplot/math/plot/Canvas.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@
3232
import java.awt.geom.*;
3333
import java.awt.image.BufferedImage;
3434

35-
import com.babai.ssplot.util.InfoLogger;
36-
3735
public class Canvas {
38-
private final static Font titleFont = new Font("Serif", Font.BOLD, 22);
36+
private final static Font titleFont = new Font("Inter", Font.BOLD, 22);
3937

4038
private int W, H; /* Size of image */
4139
private boolean axesVisible = true;
@@ -53,13 +51,10 @@ public class Canvas {
5351
private String xlbl, ylbl;
5452
private Project2D projector;
5553

56-
private InfoLogger logger;
57-
5854
/*
5955
* This only sets properties of the plot
6056
* but does not start drawing. */
61-
public Canvas(InfoLogger logger) {
62-
this.logger = logger;
57+
public Canvas() {
6358
this.projector = new Project2D();
6459
scaleFactor = 1.0;
6560
dx = 0; dy = 0;
@@ -342,9 +337,6 @@ public void drawTitle(String title) {
342337
g.setColor(titleColor);
343338
drawText(title, titlePos);
344339

345-
Point2D.Double p2 = javaToCartesian(titlePos);
346-
log(String.format("Added title \"%s\" at (%6.2f, %6.2f)", title, p2.x, p2.y));
347-
348340
g.setColor(prevColor);
349341
g.setFont(prevFont);
350342
}
@@ -379,7 +371,6 @@ public double getScaleFactor() {
379371

380372
public void setScaleFactor(double scaleFactor) {
381373
this.scaleFactor = scaleFactor;
382-
log("Zoom : " + getScaleFactor() + " x");
383374
}
384375

385376
public boolean isAxesVisible() {
@@ -406,11 +397,6 @@ public void setAxes3d(boolean axes3d) {
406397

407398
public void toggleAxes() {
408399
setAxesVisible(!isAxesVisible());
409-
if (isAxesVisible()) {
410-
log("Axes visible.");
411-
} else {
412-
log("Axes hidden.");
413-
}
414400
}
415401

416402
public int getPlotWidth() {
@@ -449,12 +435,20 @@ public void shift(int i, int j) {
449435

450436
/*********************************** Helper Methods **************************************************/
451437

438+
// NOTE: these mutate argument (passed point) to avoid allocating new Point2D.Double
439+
452440
/* Transforms from Cartesian space to Java Graphics space. */
453441
/* Takes care of scaling and translation */
454442
public Point2D.Double cartesianToJava(Point2D.Double p) {
455-
double x = scaleFactor*(p.x-zc.x) + zc.x;
456-
double y = scaleFactor*(p.y-zc.y) + zc.y;
457-
return cartesianToJavaUnscaled(x, y);
443+
p.x = scaleFactor*(p.x-zc.x) + zc.x;
444+
p.y = scaleFactor*(p.y-zc.y) + zc.y;
445+
return cartesianToJavaUnscaled(p);
446+
}
447+
448+
private Point2D.Double cartesianToJavaUnscaled(Point2D.Double p) {
449+
p.x = p.x + dx + moveX;
450+
p.y = H - (p.y + dy + moveY);
451+
return p;
458452
}
459453

460454
private Point2D.Double cartesianToJavaUnscaled(double x, double y) {
@@ -467,9 +461,9 @@ private Point2D.Double cartesianToJavaUnscaled(double x, double y) {
467461
/* Takes care of scaling and translation */
468462
// FIXME does not work with non Zero Zoom Center
469463
public Point2D.Double javaToCartesian(Point2D.Double p1) {
470-
double x = (p1.x - dx - moveX)/scaleFactor;
471-
double y = ((H - p1.y) - dy - moveY)/scaleFactor;
472-
return new Point2D.Double(x, y);
464+
p1.x = (p1.x - dx - moveX)/scaleFactor;
465+
p1.y = ((H - p1.y) - dy - moveY)/scaleFactor;
466+
return p1;
473467
}
474468

475469

@@ -481,14 +475,6 @@ public Point2D.Double polarToCartesian(double r, double theta) {
481475
return p;
482476
}
483477

484-
485-
// Utility logging method
486-
private void log(String string) {
487-
if (logger != null) {
488-
logger.log(string + "\n");
489-
}
490-
}
491-
492478
/************************************ 3D *********************************************/
493479
/**Set the projector from 3d to 2d. */
494480
public void setProjection(Project2D p) {

src/main/java/com/babai/ssplot/math/plot/Plotter.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,22 @@
2626
import java.awt.Color;
2727
import java.awt.geom.Point2D;
2828
import java.awt.image.BufferedImage;
29-
import java.util.ArrayList;
30-
31-
import com.babai.ssplot.util.InfoLogger;
3229

3330
public final class Plotter {
3431
private final Project2D p;
35-
private final InfoLogger logger;
3632
private Canvas canv;
3733

3834
public static final int DEFAULT_W = 450, DEFAULT_H = 450;
3935

40-
public Plotter(InfoLogger logger) {
41-
this.logger = logger;
36+
public Plotter() {
4237
p = new Project2D();
4338
initPlot(DEFAULT_W, DEFAULT_H);
4439
}
4540

4641
public void initPlot(int W, int H) {
4742
p.setView(0, 0, 0);
4843
if (canv == null) {
49-
canv = new Canvas(logger);
44+
canv = new Canvas();
5045
}
5146
canv.initPlot(W, H);
5247
}
@@ -71,9 +66,9 @@ private void plotData(PlotData pdata, int lastIndex) {
7166
initPlot(DEFAULT_W, DEFAULT_H);
7267
}
7368

74-
var dataCols = new ArrayList<Integer>();
75-
for (int i = 0; i < pdata.getColumnCount(); i++) {
76-
dataCols.add(pdata.getDataCol(i));
69+
int[] dataCols = new int[pdata.getColumnCount()];
70+
for (int i = 0; i < dataCols.length; i++) {
71+
dataCols[i] = pdata.getDataCol(i);
7772
}
7873

7974
Point2D.Double p1 = null, p2 = null;
@@ -176,14 +171,14 @@ private void plotOthers(PlotData pdata) {
176171
}
177172
}
178173

179-
private Point2D.Double getPoint3D(double[] row, ArrayList<Integer> dataCols, Project2D projector,
174+
private Point2D.Double getPoint3D(double[] row, int[] dataCols, Project2D projector,
180175
int i, int j, int k)
181176
{
182-
return canv.cartesianToJava(projector.project(row[dataCols.get(i)], row[dataCols.get(j)], row[dataCols.get(k)]));
177+
return canv.cartesianToJava(projector.project(row[dataCols[i]], row[dataCols[j]], row[dataCols[k]]));
183178
}
184179

185-
private Point2D.Double getPoint2D(double[] row, ArrayList<Integer> dataCols, int c1, int c2) {
186-
return canv.cartesianToJava(new Point2D.Double(row[dataCols.get(c1)], row[dataCols.get(c2)]));
180+
private Point2D.Double getPoint2D(double[] row, int[] dataCols, int c1, int c2) {
181+
return canv.cartesianToJava(new Point2D.Double(row[dataCols[c1]], row[dataCols[c2]]));
187182
}
188183

189184
public BufferedImage getImage() {

src/main/java/com/babai/ssplot/ui/MainFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public MainFrame() {
9494
logger = new StatLogger();
9595
logger.log(Text.tag("h1", "Welcome to SSPlot!"));
9696

97-
var plt = new Plotter(logger);
97+
var plt = new Plotter();
9898
if (isDark) {
9999
plt.setFgColor(Color.WHITE);
100100
plt.setBgColor(Color.decode("#474c5b"));

0 commit comments

Comments
 (0)