Skip to content

Commit

Permalink
BEAST-351: Add dynamic interval adjustment to Plotter. Improve variable
Browse files Browse the repository at this point in the history
naming and remove some garbage in Plotter. Work on graphing Beast data.
Add unit test to formatting tools.
  • Loading branch information
calvertdw committed Aug 10, 2016
1 parent b873816 commit 0b09cf8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 54 deletions.
4 changes: 2 additions & 2 deletions IHMCJavaToolkit/src/us/ihmc/tools/FormattingTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public static double roundToSignificantFigures(double number, int significantFig
return 0;
}

final double d = Math.ceil(Math.log10(number < 0 ? -number : number));
final int power = significantFigures - (int) d;
final double log10 = Math.ceil(Math.log10(Math.abs(number)));
final int power = significantFigures - (int) log10;

final double magnitude = Math.pow(10, power);
final long shifted = Math.round(number * magnitude);
Expand Down
10 changes: 9 additions & 1 deletion IHMCJavaToolkit/test/us/ihmc/tools/FormattingToolsTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package us.ihmc.tools;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import us.ihmc.tools.FormattingTools;
import us.ihmc.tools.testing.TestPlanAnnotations.DeployableTestMethod;

public class FormattingToolsTest
Expand Down Expand Up @@ -74,5 +74,13 @@ public void testUnderScoredToCamelCase()

resultingFormattedString = FormattingTools.underscoredToCamelCase("1234_@$%_BCDF", true);
assertTrue(resultingFormattedString.equals("1234@$%Bcdf"));
}

@DeployableTestMethod(estimatedDuration = 0.1)
@Test(timeout = 30000)
public void testFormatToSignificantFigures()
{
assertEquals("not equal", 100, FormattingTools.roundToSignificantFigures(123.45, 1), 1e-12);
assertEquals("not equal", 120, FormattingTools.roundToSignificantFigures(123.45, 2), 1e-12);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import us.ihmc.quadrupedRobotics.QuadrupedTestGoals;
import us.ihmc.quadrupedRobotics.controller.QuadrupedControlMode;
import us.ihmc.quadrupedRobotics.simulation.QuadrupedGroundContactModelType;
import us.ihmc.simulationconstructionset.SimulationConstructionSet;
import us.ihmc.simulationconstructionset.util.simulationRunner.GoalOrientedTestConductor;
import us.ihmc.tools.thread.ThreadTools;

Expand All @@ -21,7 +22,7 @@ public abstract class QuadrupedSpeedTorqueLimitGraphing implements QuadrupedMult
private QuadrupedForceTestYoVariables variables;
private PushRobotTestConductor pusher;

public void createSimulation() throws IOException
public SimulationConstructionSet createSimulation() throws IOException
{
QuadrupedTestFactory testFactory = createQuadrupedTestFactory();
testFactory.setControlMode(QuadrupedControlMode.FORCE);
Expand All @@ -31,6 +32,8 @@ public void createSimulation() throws IOException
conductor = testFactory.createTestConductor();
variables = new QuadrupedForceTestYoVariables(conductor.getScs());
pusher = new PushRobotTestConductor(conductor.getScs(), "body");

return conductor.getScs();
}

public void trotAroundSuperAggressively()
Expand Down
86 changes: 36 additions & 50 deletions Plotting/src/us/ihmc/plotting/Plotter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -32,6 +31,8 @@

public class Plotter extends JPanel
{
private static final boolean SHOW_LABELS_BY_DEFAULT = false;

private static final long serialVersionUID = 3113130298799362369L;

private final ArrayList<ArtifactsChangedListener> artifactsChangedListeners = new ArrayList<ArtifactsChangedListener>();
Expand All @@ -41,7 +42,7 @@ public class Plotter extends JPanel
private static final Color TEXT_COLOR = Color.WHITE;

private boolean drawHistory = false;
private boolean showLabels = false;
private boolean showLabels = SHOW_LABELS_BY_DEFAULT;

private long updateDelayInMillis = 0;
private long lastUpdate = 0;
Expand All @@ -51,12 +52,14 @@ public class Plotter extends JPanel
private Dimension preferredSize = new Dimension(275, 275);
private final HashMap<String, Artifact> artifacts = new HashMap<String, Artifact>();

private Rectangle visibleRectangle = new Rectangle();

private int centerX;
private int centerY;
private double offsetX = 0.0;
private double offsetY = 0.0;
private double plotRange = 20.0;
private double scaleFactor;
private double metersToPixels;
private double upperLeftLongitude, upperLeftLatitude, lowerRightLongitude, lowerRightLatitude;
private double selectedX = 0.0;
private double selectedY = 0.0;
Expand Down Expand Up @@ -115,18 +118,18 @@ public void paintComponent(Graphics graphics)
Graphics2D graphics2d = (Graphics2D) graphics;

// get current size and determine scaling factor
Dimension panelSize = this.getSize();
int heightInPixels = (int) Math.round(panelSize.getHeight());
int widthInPixels = (int) Math.round(panelSize.getWidth());
computeVisibleRect(visibleRectangle);
int heightInPixels = (int) Math.round(visibleRectangle.getHeight());
int widthInPixels = (int) Math.round(visibleRectangle.getWidth());
centerX = widthInPixels / 2;
centerY = heightInPixels / 2;
scaleFactor = heightInPixels / plotRange;
metersToPixels = heightInPixels / plotRange;

double headingOffset = 0.0;

// set current offset
centerX = centerX - (int) Math.round(offsetX * scaleFactor);
centerY = centerY + (int) Math.round(offsetY * scaleFactor);
centerX = centerX - (int) Math.round(offsetX * metersToPixels);
centerY = centerY + (int) Math.round(offsetY * metersToPixels);

// paint background
super.paintComponent(graphics2d);
Expand All @@ -140,7 +143,7 @@ public void paintComponent(Graphics graphics)
{
if (artifact.getLevel() == 86)
{
artifact.draw(graphics2d, centerX, centerY, headingOffset, scaleFactor);
artifact.draw(graphics2d, centerX, centerY, headingOffset, metersToPixels);
}
}
else
Expand Down Expand Up @@ -170,7 +173,7 @@ public void paintComponent(Graphics graphics)
else
{
// change grid line scale from 1m to 10cm ehn below 10m
double interval = Math.pow(10, MathTools.orderOfMagnitude(plotRange) - 1);
double interval = Math.pow(10, MathTools.orderOfMagnitude(25.0 / metersToPixels) + 1);

if (overrideAutomaticInterval)
{
Expand Down Expand Up @@ -277,7 +280,7 @@ else if ((i + yCountOffset) % 5 == 0)
{
if (artifact.getDrawHistory() && (artifact.getLevel() == i))
{
artifact.drawHistory(graphics2d, centerX, centerY, scaleFactor);
artifact.drawHistory(graphics2d, centerX, centerY, metersToPixels);
}
}
else
Expand All @@ -302,7 +305,7 @@ else if ((i + yCountOffset) % 5 == 0)
{
if (artifact.getLevel() == i)
{
artifact.draw(graphics2d, centerX, centerY, headingOffset, scaleFactor); // , _orientation);
artifact.draw(graphics2d, centerX, centerY, headingOffset, metersToPixels); // , _orientation);
}
}
else
Expand All @@ -318,8 +321,8 @@ else if ((i + yCountOffset) % 5 == 0)
{
graphics2d.setColor(Color.red);
int xSize = 8;
int xX = centerX + ((int) Math.round(selectedX * scaleFactor));
int yX = centerY - ((int) Math.round(selectedY * scaleFactor));
int xX = centerX + ((int) Math.round(selectedX * metersToPixels));
int yX = centerY - ((int) Math.round(selectedY * metersToPixels));
graphics2d.drawLine(xX - xSize, yX - xSize, xX + xSize, yX + xSize);
graphics2d.drawLine(xX - xSize, yX + xSize, xX + xSize, yX - xSize);
}
Expand All @@ -328,10 +331,10 @@ else if ((i + yCountOffset) % 5 == 0)
if (SHOW_SELECTION)
{
graphics2d.setColor(Color.red);
int areaX1Int = centerX + ((int) Math.round(area1X * scaleFactor));
int areaY1Int = centerY - ((int) Math.round(area1Y * scaleFactor));
int areaX2Int = centerX + ((int) Math.round(area2X * scaleFactor));
int areaY2Int = centerY - ((int) Math.round(area2Y * scaleFactor));
int areaX1Int = centerX + ((int) Math.round(area1X * metersToPixels));
int areaY1Int = centerY - ((int) Math.round(area1Y * metersToPixels));
int areaX2Int = centerX + ((int) Math.round(area2X * metersToPixels));
int areaY2Int = centerY - ((int) Math.round(area2Y * metersToPixels));
int Xmin, Xmax, Ymin, Ymax;
if (areaX1Int > areaX2Int)
{
Expand Down Expand Up @@ -581,46 +584,24 @@ public void clearPolygon()
repaint();
}

@SuppressWarnings("unused")
private Point convertCoordinates(JPanel plot, Point2D.Double pt)
{
// get current size and determine scaling factor
Dimension d = this.getSize();
int h = (int) Math.round(d.getHeight());
Math.round(d.getWidth());

scaleFactor = h / plotRange;

// detemine plot size
Dimension plotD = plot.getSize();
int plotH = (int) Math.round(plotD.getHeight());
int plotW = (int) Math.round(plotD.getWidth());

// place plot so bottom is cenetered at location
int xnew = (int) Math.round((pt.x * scaleFactor) - (plotW / 2));
int ynew = (int) Math.round((h - (pt.y * scaleFactor)) - (plotH));

return new Point(xnew, ynew);
}

private double unConvertXCoordinate(int coordinate)
{
return new Integer(coordinate - centerX).doubleValue() / scaleFactor;
return new Integer(coordinate - centerX).doubleValue() / metersToPixels;
}

private double unConvertYCoordinate(int coordinate)
{
return new Integer((centerY - coordinate)).doubleValue() / scaleFactor;
return new Integer((centerY - coordinate)).doubleValue() / metersToPixels;
}

private Coordinate convertFromMetersToPixels(Coordinate coordinate)
{
double x = coordinate.getX();
double y = coordinate.getY();

x = new Double(centerX + ((int) Math.round(x * scaleFactor))).doubleValue();
x = new Double(centerX + ((int) Math.round(x * metersToPixels))).doubleValue();

y = new Double(centerY - ((int) Math.round(y * scaleFactor))).doubleValue();
y = new Double(centerY - ((int) Math.round(y * metersToPixels))).doubleValue();

return new Coordinate(x, y, Coordinate.PIXEL);
}
Expand All @@ -630,9 +611,9 @@ private Coordinate convertFromPixelsToMeters(Coordinate coordinate)
double x = coordinate.getX();
double y = coordinate.getY();

x = (x - new Integer(centerX).doubleValue()) / scaleFactor;
x = (x - new Integer(centerX).doubleValue()) / metersToPixels;

y = ((new Integer(centerY).doubleValue()) - y) / scaleFactor;
y = ((new Integer(centerY).doubleValue()) - y) / metersToPixels;

return new Coordinate(x, y, Coordinate.METER);
}
Expand Down Expand Up @@ -692,7 +673,7 @@ public Dimension getPreferredSize()

public void setPreferredSize(int width, int height)
{
preferredSize = new Dimension(width, height);
preferredSize.setSize(width, height);
}

public void setDoubleClickListener(DoubleClickListener doubleClickListener)
Expand Down Expand Up @@ -867,7 +848,12 @@ public void mouseReleased(MouseEvent e)

public void showInNewWindow()
{
JFrame frame = new JFrame("Plotter");
showInNewWindow("Plotter");
}

public void showInNewWindow(String title)
{
JFrame frame = new JFrame(title);
frame.getContentPane().add(this, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
Expand Down

0 comments on commit 0b09cf8

Please sign in to comment.