improve continuity highlighting

massively simplified outline tracing
This commit is contained in:
Branislav Stojkovic
2018-12-16 01:47:30 +01:00
parent 58a4ff987f
commit 2cc916c8c1
10 changed files with 230 additions and 301 deletions
@@ -32,7 +32,6 @@ import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.Observable;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.Display;
@@ -392,6 +392,10 @@ public class DrawingManager {
lastDrawnStateMap.clear();
}
public void clearContinuityArea() {
this.continuityArea = null;
}
public List<IDIYComponent<?>> findComponentsAt(Point point, Project project) {
List<IDIYComponent<?>> components = new ArrayList<IDIYComponent<?>>();
for (int i = 0; i < project.getComponents().size(); i++) {
@@ -30,21 +30,18 @@ import java.awt.Paint;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.RenderingHints.Key;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
@@ -88,8 +85,8 @@ class G2DWrapper extends Graphics2D implements IDrawingObserver {
private AffineTransform currentTx;
private AffineTransform initialTx;
private Area currentArea;
private List<Area> currentContinuityPositiveAreas;
private List<Area> currentContinuityNegativeAreas;
private List<Area> continuityPositiveAreas;
private List<Area> continuityNegativeAreas;
private Shape lastShape;
private double zoom;
@@ -104,8 +101,8 @@ class G2DWrapper extends Graphics2D implements IDrawingObserver {
this.canvasGraphics = canvasGraphics;
this.zoom = zoom;
currentArea = new Area();
currentContinuityPositiveAreas = new ArrayList<Area>();
currentContinuityNegativeAreas = new ArrayList<Area>();
continuityPositiveAreas = new ArrayList<Area>();
continuityNegativeAreas = new ArrayList<Area>();
currentTx = new AffineTransform();
}
@@ -115,8 +112,8 @@ class G2DWrapper extends Graphics2D implements IDrawingObserver {
public void startedDrawingComponent() {
drawingComponent = true;
currentArea = new Area();
currentContinuityPositiveAreas = new ArrayList<Area>();
currentContinuityNegativeAreas = new ArrayList<Area>();
continuityPositiveAreas = new ArrayList<Area>();
continuityNegativeAreas = new ArrayList<Area>();
originalStroke = canvasGraphics.getStroke();
originalColor = canvasGraphics.getColor();
originalTx = canvasGraphics.getTransform();
@@ -140,7 +137,7 @@ class G2DWrapper extends Graphics2D implements IDrawingObserver {
canvasGraphics.setTransform(originalTx);
canvasGraphics.setComposite(originalComposite);
canvasGraphics.setFont(originalFont);
return new ComponentArea(currentArea, currentContinuityPositiveAreas, currentContinuityNegativeAreas);
return new ComponentArea(currentArea, continuityPositiveAreas, continuityNegativeAreas);
}
@Override
@@ -185,9 +182,9 @@ class G2DWrapper extends Graphics2D implements IDrawingObserver {
if (trackingContinuityAllowed) {
if (trackingContinuityPositive)
currentContinuityPositiveAreas.add(area);
continuityPositiveAreas.add(area);
else
currentContinuityNegativeAreas.add(area);
continuityNegativeAreas.add(area);
}
lastShape = s;
}
@@ -203,95 +200,8 @@ class G2DWrapper extends Graphics2D implements IDrawingObserver {
if (!drawingComponent || !trackingAllowed || s.equals(lastShape)) {
return;
}
lastShape = s;
PathIterator pathIterator = s.getPathIterator(null);
double thickness;
if (getStroke() instanceof BasicStroke) {
thickness = ((BasicStroke) getStroke()).getLineWidth() + 2 * LINE_SENSITIVITY_MARGIN;
} else {
thickness = 2 * LINE_SENSITIVITY_MARGIN;
}
Point2D prevPoint = null;
while (!pathIterator.isDone()) {
double[] coord = new double[6];
int type = pathIterator.currentSegment(coord);
switch (type) {
case PathIterator.SEG_MOVETO:
prevPoint = new Point2D.Double(coord[0], coord[1]);
break;
case PathIterator.SEG_LINETO:
// Represent straight line with a rectangle.
Point2D nextPoint = new Point2D.Double(coord[0], coord[1]);
Double theta = Math.atan2(nextPoint.getY() - prevPoint.getY(), nextPoint.getX() - prevPoint.getX());
double width =
Math.sqrt(Math.pow(nextPoint.getX() - prevPoint.getX(), 2)
+ Math.pow(nextPoint.getY() - prevPoint.getY(), 2));
double midX = (prevPoint.getX() + nextPoint.getX()) / 2;
double midY = (prevPoint.getY() + nextPoint.getY()) / 2;
Rectangle2D rect = new Rectangle2D.Double(midX - width / 2, midY - thickness / 2, width, thickness);
Area area = new Area(rect);
area.transform(AffineTransform.getRotateInstance(theta, midX, midY));
// area.transform(currentTx);
appendShape(area);
// Set the prev point to line end
prevPoint = nextPoint;
break;
case PathIterator.SEG_CUBICTO:
CubicCurve2D cubicCurve =
new CubicCurve2D.Double(prevPoint.getX(), prevPoint.getY(), coord[0], coord[1], coord[2], coord[3],
coord[4], coord[5]);
addCubicCurveArea(cubicCurve);
prevPoint = new Point2D.Double(coord[4], coord[5]);
break;
case PathIterator.SEG_QUADTO:
QuadCurve2D quadCurve =
new QuadCurve2D.Double(prevPoint.getX(), prevPoint.getY(), coord[0], coord[1], coord[2], coord[3]);
addQuadCurveArea(quadCurve);
prevPoint = new Point2D.Double(coord[3], coord[3]);
break;
}
pathIterator.next();
}
}
/**
* Adds a cubic curve to the component area. It uses subdivision to approximate the curve with a
* series of straight lines.
*
* @param curve
*/
private void addCubicCurveArea(CubicCurve2D curve) {
// When end points are close enough, approximate the curve with a
// straight line.
if (curve.getFlatness() < CURVE_SENSITIVITY) {
appendShapeOutline(new Line2D.Double(curve.getP1(), curve.getP2()));
} else {
CubicCurve2D leftSubcurve = new CubicCurve2D.Double();
CubicCurve2D rightSubcurve = new CubicCurve2D.Double();
curve.subdivide(leftSubcurve, rightSubcurve);
addCubicCurveArea(leftSubcurve);
addCubicCurveArea(rightSubcurve);
}
}
/**
* Adds a quad curve to the component area. It uses subdivision to approximate the curve with a
* series of straight lines.
*
* @param curve
*/
private void addQuadCurveArea(QuadCurve2D curve) {
// When end points are close enough, approximate the curve with a
// straight line.
if (curve.getFlatness() < CURVE_SENSITIVITY) {
appendShapeOutline(new Line2D.Double(curve.getP1(), curve.getP2()));
} else {
QuadCurve2D leftSubcurve = new QuadCurve2D.Double();
QuadCurve2D rightSubcurve = new QuadCurve2D.Double();
curve.subdivide(leftSubcurve, rightSubcurve);
addQuadCurveArea(leftSubcurve);
addQuadCurveArea(rightSubcurve);
}
appendShape(getStroke().createStrokedShape(s));
}
@Override
@@ -237,6 +237,7 @@ public class Presenter implements IPlugInPort {
LOG.info(String.format("loadProject(%s, %s)", project.getTitle(), freshStart));
this.currentProject = project;
drawingManager.clearComponentAreaMap();
drawingManager.clearContinuityArea();
updateSelection(EMPTY_SELECTION);
messageDispatcher.dispatchMessage(EventType.PROJECT_LOADED, project, freshStart);
messageDispatcher.dispatchMessage(EventType.REPAINT);
@@ -598,6 +599,7 @@ public class Presenter implements IPlugInPort {
if (!oldProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(), "Add "
+ componentTypeSlot.getName());
drawingManager.clearContinuityArea();
projectFileManager.notifyFileChange();
}
} else if (ConfigurationManager.getInstance().readBoolean(HIGHLIGHT_CONTINUITY_AREA, false)) {
@@ -689,6 +691,7 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(),
"Rotate Selection");
messageDispatcher.dispatchMessage(EventType.REPAINT);
drawingManager.clearContinuityArea();
return true;
}
@@ -917,6 +920,7 @@ public class Presenter implements IPlugInPort {
moveComponents(controlPointMap, dx, dy, false);
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(), "Move Selection");
messageDispatcher.dispatchMessage(EventType.REPAINT);
drawingManager.clearContinuityArea();
}
@Override
@@ -1198,6 +1202,7 @@ public class Presenter implements IPlugInPort {
rotateComponents(this.selectedComponents, direction, isSnapToGrid());
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(),
"Rotate Selection");
drawingManager.clearContinuityArea();
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
}
@@ -1308,6 +1313,7 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(),
"Mirror Selection");
messageDispatcher.dispatchMessage(EventType.REPAINT);
drawingManager.clearContinuityArea();
}
}
@@ -1420,6 +1426,7 @@ public class Presenter implements IPlugInPort {
if (!preDragProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, preDragProject, currentProject.clone(), "Drag");
drawingManager.clearContinuityArea();
projectFileManager.notifyFileChange();
}
messageDispatcher.dispatchMessage(EventType.REPAINT);
@@ -1469,6 +1476,7 @@ public class Presenter implements IPlugInPort {
updateSelection(newSelection);
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(), "Duplicate");
drawingManager.clearContinuityArea();
projectFileManager.notifyFileChange();
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
@@ -1489,6 +1497,7 @@ public class Presenter implements IPlugInPort {
}
currentProject.getComponents().removeAll(selectedComponents);
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(), "Delete");
drawingManager.clearContinuityArea();
projectFileManager.notifyFileChange();
updateSelection(EMPTY_SELECTION);
messageDispatcher.dispatchMessage(EventType.REPAINT);
@@ -1985,6 +1994,7 @@ public class Presenter implements IPlugInPort {
if (!oldProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(),
"Edit Selection");
drawingManager.clearContinuityArea();
projectFileManager.notifyFileChange();
}
messageDispatcher.dispatchMessage(EventType.REPAINT);
@@ -2022,6 +2032,7 @@ public class Presenter implements IPlugInPort {
if (!oldProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(),
"Edit Project");
drawingManager.clearContinuityArea();
projectFileManager.notifyFileChange();
}
drawingManager.fireZoomChanged();
@@ -2200,6 +2211,7 @@ public class Presenter implements IPlugInPort {
if (!oldProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(),
"Edit Selection");
drawingManager.clearContinuityArea();
projectFileManager.notifyFileChange();
}
messageDispatcher.dispatchMessage(EventType.REPAINT);
Binary file not shown.
@@ -1,24 +1,20 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC 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 3 of the License, or
(at your option) any later version.
DIYLC 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 DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
*
* DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
*
* This file is part of DIYLC.
*
* DIYLC 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 3 of the
* License, or (at your option) any later version.
*
* DIYLC 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 DIYLC. If not, see
* <http://www.gnu.org/licenses/>.
*/
package org.diylc.components.connectivity;
import java.awt.Color;
@@ -43,7 +39,8 @@ import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Copper Trace", author = "Branislav Stojkovic", category = "Connectivity",
creationMethod = CreationMethod.POINT_BY_POINT, instanceNamePrefix = "Trace",
description = "Straight copper trace", zOrder = IDIYComponent.TRACE, bomPolicy = BomPolicy.NEVER_SHOW,
autoEdit = false, keywordPolicy = KeywordPolicy.SHOW_TAG, keywordTag = "PCB", transformer = SimpleComponentTransformer.class)
autoEdit = false, keywordPolicy = KeywordPolicy.SHOW_TAG, keywordTag = "PCB",
transformer = SimpleComponentTransformer.class)
public class CopperTrace extends AbstractLeadedComponent<Void> {
private static final long serialVersionUID = 1L;
@@ -160,4 +157,9 @@ public class CopperTrace extends AbstractLeadedComponent<Void> {
protected Size getDefaultLength() {
return null;
}
@Override
protected boolean IsCopperArea() {
return true;
}
}
@@ -67,7 +67,9 @@ public class Eyelet extends AbstractComponent<String> {
int diameter = getClosestOdd((int) size.convertToPixels());
int holeDiameter = getClosestOdd((int) holeSize.convertToPixels());
g2d.setColor(color);
drawingObserver.startTrackingContinuityArea(true);
g2d.fillOval(point.x - diameter / 2, point.y - diameter / 2, diameter, diameter);
drawingObserver.stopTrackingContinuityArea();
g2d.setColor(componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING ? SELECTION_COLOR
: color.darker());
g2d.drawOval(point.x - diameter / 2, point.y - diameter / 2, diameter, diameter);
Binary file not shown.
Binary file not shown.
@@ -102,7 +102,7 @@ public class ConfigPlugin implements IPlugIn {
.createConfigAction(plugInPort, "Sticky Points", IPlugInPort.STICKY_POINTS_KEY, true), CONFIG_MENU);
swingUI.injectMenuAction(
ActionFactory.getInstance()
.createConfigAction(plugInPort, "Highlighted Connected Areas", IPlugInPort.HIGHLIGHT_CONTINUITY_AREA, false), CONFIG_MENU);
.createConfigAction(plugInPort, "Highlight Connected Areas", IPlugInPort.HIGHLIGHT_CONTINUITY_AREA, false), CONFIG_MENU);
File themeDir = new File("themes");
if (themeDir.exists()) {