/** * make new shape polygon * * @param p * @param polygon * @param intersectPoint */ private Polygon reMakePolygonShape(Polygon p, Polygon polygon, List<Point2D> intersectPoint) { ConvexHull convexHull; List<Point> newPoints = new ArrayList<Point>(); for (int j = 0; j < p.npoints; j++) { if (polygon.contains(p.xpoints[j], p.ypoints[j])) { newPoints.add(new Point(p.xpoints[j], p.ypoints[j])); } } for (Point2D point2D : intersectPoint) { newPoints.add(new Point((int) point2D.getX(), (int) point2D.getY())); } for (int i = 0; i < polygon.npoints; i++) { if (p.contains(polygon.xpoints[i], polygon.ypoints[i])) { Point point = new Point(polygon.xpoints[i], polygon.ypoints[i]); newPoints.add(point); } } convexHull = new ConvexHull(); for (Point point : newPoints) { convexHull.addPoint((int) point.getX(), (int) point.getY()); } Polygon polygon1 = convexHull.convex(); // newPoints.clear(); return polygon1; }
/** make new shape polygon */ private void reMakePolygonShape() { ConvexHull convexHull; List<Point> newPoints = new ArrayList<Point>(); for (int j = 0; j < polygon.npoints; j++) { if (mainClusterShape.contains(polygon.xpoints[j], polygon.ypoints[j])) { newPoints.add(new Point(polygon.xpoints[j], polygon.ypoints[j])); } } for (Point2D point2D : getIntersectPoint()) { newPoints.add(new Point((int) point2D.getX(), (int) point2D.getY())); } for (int i = 0; i < mainClusterShape.npoints; i++) { if (polygon.contains(mainClusterShape.xpoints[i], mainClusterShape.ypoints[i])) { Point point = new Point(mainClusterShape.xpoints[i], mainClusterShape.ypoints[i]); newPoints.add(point); } } convexHull = new ConvexHull(); for (Point point : newPoints) { convexHull.addPoint((int) point.getX(), (int) point.getY()); } setPolygon(convexHull.convex()); newPoints.clear(); }
private static void graph(Polygon[] finalPolygons) { int weiners = 0; String[] Starter = sPoint.split(","); String[] Ender = ePoint.split(","); int endx = Integer.parseInt(Ender[0]); int endy = Integer.parseInt(Ender[1]); int G = 40; for (int i = 0; i < G; i++) { for (int j = 0; j < G; j++) { int loketest = 0; if ((i == Startx && j == Starty) || i == endx && j == endy) { loketest = 2; } for (Polygon helpme : finalPolygons) { if (helpme.contains(i, j)) { loketest = 1; } } if (loketest == 1) { System.out.print("1"); } else if (loketest == 2) { System.out.print("X"); } else { System.out.print("0"); } } System.out.println(" \t"); weiners++; } }
@Override public boolean isInBounds(Bounds bounds) { // TODO there is no float polygon, so I have to think about s.th. else (or leave it the way it // is now) int x[] = new int[nodes.length]; int y[] = new int[nodes.length]; int i = 0; for (Node node : nodes) { x[i] = (int) (node.getPos().getLongitude() * 1000); // 1000 is a random factor, can be changed i++; } i = 0; for (Node node : nodes) { y[i] = (int) (node.getPos().getLatitude() * 1000); i++; } Polygon area = new Polygon(x, y, nodes.length); Rectangle2D.Double box = new Rectangle2D.Double( bounds.getLeft() * 1000 - 1, bounds.getTop() * 1000 - 1, bounds.getWidth() * 1000 + 1, bounds.getHeight() * 1000 + 1); boolean inside = false; for (Node node : nodes) { if (node.isInBounds(bounds)) { inside = true; } } return inside || area.contains(box) || area.intersects(box); }
/** * this method calculates the border entities, using entities and convexHull of the entities * * @param convex is the convex of all Entities * @param entities are the self entities * @param scale is the scale for making smaller convex hull */ public Set<EntityID> getBorderEntities(ConvexHull convex, Set<EntityID> entities, double scale) { if (scale >= 1.0) { System.err.println( "scale should not be over 1.0! check it in border entities, border entities doesn't work now!"); return null; } Building building; Polygon convexObject = convex.convex(); Set<EntityID> borderEntities = new FastSet<EntityID>(); if (convexObject.npoints == 0) { // I don't know why this happens, report me if this error writes usually! TODO check // this if something comes wrong here System.out.println("Something gone wrong in setting border entities for Firebrigade!!!"); return null; } Polygon smallBorderPolygon = scalePolygon(convexObject, scale); // Polygon bigBorderPolygon = scalePolygon(convexObject, 1.1); if (MRLConstants.LAUNCH_VIEWER) { // MrlConvexHullLayer.BIG_Whole_BORDER_HULL = convex; // MrlConvexHullLayer.SMALL_Whole_BORDER_HULL = smallBorderPolygon; } for (EntityID entityID : entities) { StandardEntity entity = world.getEntity(entityID); if (entity instanceof Refuge) continue; if (!(entity instanceof Building)) continue; building = (Building) entity; int vertexes[] = building.getApexList(); for (int i = 0; i < vertexes.length; i += 2) { if ((convexObject.contains(vertexes[i], vertexes[i + 1])) && !(smallBorderPolygon.contains(vertexes[i], vertexes[i + 1]))) { borderEntities.add(building.getID()); break; } } } return borderEntities; }
/** find the cluster roads */ private void clusterRoad() { for (StandardEntity entity : world.getRoads()) { if (entity instanceof Road) { if (polygon.contains(((Road) entity).getX(), ((Road) entity).getY())) { this.roads.add((Road) entity); } } } }
/** * find the cluster buildings */ private void clusterBuildings() { for (StandardEntity entity : world.getBuildings()) { if (entity instanceof Building) { if (polygon.contains(((Building) entity).getX(), ((Building) entity).getY())) { this.buildings.add((Building) entity); } } } }
/** * Traces the boundary of the area with pixel values within 'tolerance' of the value of the pixel * at the starting location. 'tolerance' is in uncalibrated units. 'mode' can be FOUR_CONNECTED or * EIGHT_CONNECTED. Mode LEGACY_MODE is for compatibility with previous versions of ImageJ; * ignored if tolerance > 0. Mode bit THRESHOLDED_MODE for internal use only; it is set by * autoOutline with 'upper' and 'lower' arguments. When successful, npoints>0 and the boundary * points can be accessed in the public xpoints and ypoints fields. */ public void autoOutline(int startX, int startY, double tolerance, int mode) { if (startX < 0 || startX >= width || startY < 0 || startY >= height) return; if (fpixels != null && Float.isNaN(getPixel(startX, startY))) return; exactPixelValue = tolerance == 0; boolean thresholdMode = (mode & THRESHOLDED_MODE) != 0; boolean legacyMode = (mode & LEGACY_MODE) != 0 && tolerance == 0; if (!thresholdMode) { double startValue = getPixel(startX, startY); lowerThreshold = (float) (startValue - tolerance); upperThreshold = (float) (startValue + tolerance); } int x = startX; int y = startY; int seedX; // the first inside pixel if (inside(x, y)) { // find a border when coming from inside seedX = x; // (seedX, startY) is an inside pixel do { x++; } while (inside(x, y)); } else { // find a border when coming from outside (thresholded only) do { x++; if (x >= width) return; // no border found } while (!inside(x, y)); seedX = x; } boolean fourConnected; if (legacyMode) fourConnected = !thresholdMode && !(isLine(x, y)); else fourConnected = (mode & FOUR_CONNECTED) != 0; // now, we have a border between (x-1, y) and (x,y) boolean first = true; while (true) { // loop until we have not traced an inner hole boolean insideSelected = traceEdge(x, y, fourConnected); if (legacyMode) return; // in legacy mode, don't care what we have got if (insideSelected) { // not an inner hole if (first) return; // started at seed, so we got it (sucessful) if (xmin <= seedX) { // possibly the correct particle Polygon poly = new Polygon(xpoints, ypoints, npoints); if (poly.contains(seedX, startY)) return; // successful, particle contains seed } } first = false; // we have traced an inner hole or the wrong particle if (!inside(x, y)) do { x++; // traverse the hole if (x > width) throw new RuntimeException("Wand Malfunction"); // should never happen } while (!inside(x, y)); do { x++; } while (inside(x, y)); // retry here; maybe no inner hole any more } }
public boolean contains(int x, int y) { if (!super.contains(x, y)) return false; if (xSpline != null) { FloatPolygon poly = new FloatPolygon(xSpline, ySpline, splinePoints); return poly.contains(x - this.x, y - this.y); } else if (xpf != null) { FloatPolygon poly = new FloatPolygon(xpf, ypf, nPoints); return poly.contains(x - this.x, y - this.y); } else { Polygon poly = new Polygon(xp, yp, nPoints); return poly.contains(x - this.x, y - this.y); } }
/** find the cluster paths */ private void clusterPaths() { List<Road> roadList = new ArrayList<Road>(); for (StandardEntity entity : world.getRoads()) { if (entity instanceof Road) { if (polygon.contains(((Road) entity).getX(), ((Road) entity).getY())) { roadList.add((Road) entity); } } } for (Road r : roadList) { paths.add(world.getPath(r.getID()).getId()); } }
@Test public void rutPoly() { Polygon p = new Polygon(); p.addPoint(12.334562619445387, 55.67289341268228) .addPoint(12.33499177288854, 55.67093310816091) .addPoint(12.33816750836057, 55.67135663906081) .addPoint(12.338038762329136, 55.672966014667445); p.endPolygon(); Point point = new Point(12.3304366092813, 55.6721631491783); assertThat(p.contains(point), is(false)); }
@Test public void rutPoly3() { Polygon p = new Polygon(); p.addPoint(12.461793988551225, 55.67976726842135) .addPoint(12.461579411830385, 55.67156381784342) .addPoint(12.462952702845898, 55.671660623749055) .addPoint(12.463124364222589, 55.676113436533356) .addPoint(12.463596433009696, 55.67971887552748); p.endPolygon(); Point point1_1 = new Point(12.4487090944424, 55.6797463411036); Point point1_2 = new Point(12.4466566276518, 55.6797500964789); Point point2_1 = new Point(12.4622486075482, 55.6779172094357); /* Outside */ assertThat(p.contains(point1_1), is(false)); assertThat(p.contains(point1_2), is(false)); /* Inside */ assertThat(p.contains(point2_1), is(true)); }
@Test public void rutPoly2() { Polygon p = new Polygon(); p.addPoint(4.562619445387, 2.89341268228) .addPoint(4.99177288854, 0.93310816091) .addPoint(8.16750836057, 1.35663906081) .addPoint(8.038762329136, 2.966014667445); p.endPolygon(); Point point = new Point(0.4366092813, 2.1631491783); assertThat(p.contains(point), is(false)); }
void analyzeParticle(int x, int y, ImagePlus imp, ImageProcessor ip) { // Wand wand = new Wand(ip); ImageProcessor ip2 = redirectIP != null ? redirectIP : ip; wand.autoOutline(x, y, level1, level2, wandMode); if (wand.npoints == 0) { IJ.log("wand error: " + x + " " + y); return; } Roi roi = new PolygonRoi(wand.xpoints, wand.ypoints, wand.npoints, roiType); Rectangle r = roi.getBounds(); if (r.width > 1 && r.height > 1) { PolygonRoi proi = (PolygonRoi) roi; pf.setPolygon(proi.getXCoordinates(), proi.getYCoordinates(), proi.getNCoordinates()); ip2.setMask(pf.getMask(r.width, r.height)); if (floodFill) ff.particleAnalyzerFill(x, y, level1, level2, ip2.getMask(), r); } ip2.setRoi(r); ip.setValue(fillColor); ImageStatistics stats = getStatistics(ip2, measurements, calibration); boolean include = true; if (excludeEdgeParticles) { if (r.x == minX || r.y == minY || r.x + r.width == maxX || r.y + r.height == maxY) include = false; if (polygon != null) { Rectangle bounds = roi.getBounds(); int x1 = bounds.x + wand.xpoints[wand.npoints - 1]; int y1 = bounds.y + wand.ypoints[wand.npoints - 1]; int x2, y2; for (int i = 0; i < wand.npoints; i++) { x2 = bounds.x + wand.xpoints[i]; y2 = bounds.y + wand.ypoints[i]; if (!polygon.contains(x2, y2)) { include = false; break; } if ((x1 == x2 && ip.getPixel(x1, y1 - 1) == fillColor) || (y1 == y2 && ip.getPixel(x1 - 1, y1) == fillColor)) { include = false; break; } x1 = x2; y1 = y2; } } } ImageProcessor mask = ip2.getMask(); if (minCircularity > 0.0 || maxCircularity < 1.0) { double perimeter = roi.getLength(); double circularity = perimeter == 0.0 ? 0.0 : 4.0 * Math.PI * (stats.pixelCount / (perimeter * perimeter)); if (circularity > 1.0) circularity = 1.0; // IJ.log(circularity+" "+perimeter+" "+stats.area); if (circularity < minCircularity || circularity > maxCircularity) include = false; } if (stats.pixelCount >= minSize && stats.pixelCount <= maxSize && include) { particleCount++; if (roiNeedsImage) roi.setImage(imp); stats.xstart = x; stats.ystart = y; saveResults(stats, roi); if (showChoice != NOTHING) drawParticle(drawIP, roi, stats, mask); } if (redirectIP != null) ip.setRoi(r); ip.fill(mask); }
public void processPolygonSpline(boolean shiftDown) { mCurrPC.setBatchModeOn(); if (!shiftDown) mCurrPC.unselectAll(); // loop on spline pts to construct a Polygon Polygon srcArea = new Polygon(); for (int s = 0; s < mSplinePoints.size(); s++) { Point p = (Point) mSplinePoints.elementAt(s); int x = p.x - mLeft - 5; int y = p.y - mTop - 5; srcArea.addPoint(x, y); } mSplinePoints.removeAllElements(); // search for matches byte[] currFilterResults = fdm.getResults(); double[] yArray1 = getYArray1(); double[] xArray1 = getXArray1(); double[] yArray2 = getYArray2(); double[] xArray2 = getXArray2(); Point p1 = new Point(); Point p2 = new Point(); for (int i = 0; i < mCurrPC.getSize(); i++) { if (!mIgnoreFilter && (mCurrPC.isDeleted(i) || currFilterResults[i] != 4 || !mCurrPC.isSelectedLayer(i))) continue; double yi = yArray1[i]; double xi = xArray1[i]; double xi2 = Float.NaN; double yi2 = Float.NaN; double xx2 = Float.NaN; double yy2 = Float.NaN; // correct the X value if necessary xi = correctX(xi); double xx1 = (xi - winXOrigin) * winXScale; double yy1 = (yi - winYOrigin) * winYScale; // correct the Y coordinate if necessary yy1 = correctY(yy1); if (!isYAxisScaler()) { yi2 = yArray2[i]; yy2 = (yi2 - winYOrigin) * winYScale; ; yy2 = correctY(yy2); } else yy2 = yy1; if (!isXAxisScaler()) { xi2 = xArray2[i]; xi2 = correctX(xi2); xx2 = (xi2 - winXOrigin) * winXScale; } else xx2 = xx1; p1.setLocation((int) xx1, (int) yy1); p2.setLocation((int) xx2, (int) yy2); if (srcArea.contains(p1) || srcArea.contains(p2)) { mCurrPC.select(i); } } mViewManager.invalidateAllViews(); mCurrPC.setBatchModeOff(); }
public void processSectionSpline(boolean shiftDown) { mCurrPC.setBatchModeOn(); if (!shiftDown) mCurrPC.unselectAll(); // loop on spline pts for (int s = 0; s < mSplinePoints.size() - 2; s++) { Point p1 = (Point) mSplinePoints.elementAt(s); Point p2 = (Point) mSplinePoints.elementAt(s + 1); int x1 = p1.x - mLeft - 5; int y1 = p1.y - mTop - 5; int x2 = p2.x - mLeft - 5; int y2 = p2.y - mTop - 5; // construct the search polygon double dx = x1 - x2; double dy = y1 - y2; int width1 = ComputeSectionPixelWidth(p1); int width2 = ComputeSectionPixelWidth(p2); if (dx == 0 && dy == 0) return; double dist = (double) Math.pow(dx * dx + dy * dy, 0.5f); double p1x = x1 + (double) width1 * (-dy / dist); double p1y = y1 + (double) width1 * (dx / dist); double p2x = x1 - (double) width1 * (-dy / dist); double p2y = y1 - (double) width1 * (dx / dist); double p3x = x2 + (double) width2 * (-dy / dist); double p3y = y2 + (double) width2 * (dx / dist); double p4x = x2 - (double) width2 * (-dy / dist); double p4y = y2 - (double) width2 * (dx / dist); Polygon srcArea = new Polygon(); srcArea.addPoint((int) p2x, (int) p2y); srcArea.addPoint((int) p1x, (int) p1y); srcArea.addPoint((int) p3x, (int) p3y); srcArea.addPoint((int) p4x, (int) p4y); // search for matches byte[] currFilterResults = fdm.getResults(); double[] yArray1 = getYArray1(); double[] xArray1 = getXArray1(); double[] yArray2 = getYArray2(); double[] xArray2 = getXArray2(); mCurrPC.setBatchModeOn(); for (int i = 0; i < mCurrPC.getSize(); i++) { if (!mIgnoreFilter && (mCurrPC.isDeleted(i) || currFilterResults[i] != 4 || !mCurrPC.isSelectedLayer(i))) continue; double yi = yArray1[i]; double xi = xArray1[i]; double xi2 = Float.NaN; double yi2 = Float.NaN; double xx2 = Float.NaN; double yy2 = Float.NaN; // correct the X value if necessary xi = correctX(xi); double xx1 = (xi - winXOrigin) * winXScale; double yy1 = (yi - winYOrigin) * winYScale; // correct the Y coordinate if necessary yy1 = correctY(yy1); if (!isYAxisScaler()) { yi2 = yArray2[i]; yy2 = (yi2 - winYOrigin) * winYScale; ; yy2 = correctY(yy2); } else yy2 = yy1; if (!isXAxisScaler()) { xi2 = xArray2[i]; xi2 = correctX(xi2); xx2 = (xi2 - winXOrigin) * winXScale; } else xx2 = xx1; if (srcArea.contains(new Point((int) xx1, (int) yy1)) || srcArea.contains(new Point((int) xx2, (int) yy2))) { mCurrPC.select(i); } } } // for splines mSplinePoints.removeAllElements(); mViewManager.invalidateAllViews(); mCurrPC.setBatchModeOff(); }
public void rubberbandEnded(Rubberband rb, boolean shiftDown) { // get the limits and zoom the plot if (mCurrPC == null) return; Rectangle rbRect = rb.getBounds(); if (rbRect.width == 0 || rbRect.height == 0) { // click selection if (toolMode == Constants.SELECT_MODE) { // find any matches at the mouseclick location int x = rbRect.x - mLeft - 5; int y = rbRect.y - mTop - 5; // construct a search region; int[] xpoints = {x - 1, x + 1, x + 1, x - 1, x - 1}; int[] ypoints = {y - 1, y - 1, y + 1, y + 1, y - 1}; Polygon sr = new Polygon(xpoints, ypoints, 5); if (!shiftDown) mCurrPC.unselectAll(); mCurrPC.setBatchModeOn(); byte[] currFilterResults = fdm.getResults(); double[] yArray1 = getYArray1(); double[] xArray1 = getXArray1(); double[] yArray2 = getYArray2(); double[] xArray2 = getXArray2(); for (int i = 0; i < mCurrPC.getSize(); i++) { if (!mIgnoreFilter && (mCurrPC.isDeleted(i) || currFilterResults[i] != 4) || !mCurrPC.isSelectedLayer(i)) continue; // search for matches double yi = yArray1[i]; double xi = xArray1[i]; double xi2 = Float.NaN; double yi2 = Float.NaN; double x2 = Float.NaN; double y2 = Float.NaN; // correct the X value if necessary xi = correctX(xi); double x1 = (xi - winXOrigin) * winXScale; double y1 = (yi - winYOrigin) * winYScale; // correct the Y coordinate if necessary y1 = correctY(y1); if (!isYAxisScaler()) { yi2 = yArray2[i]; y2 = (yi2 - winYOrigin) * winYScale; y2 = correctY(y2); } else y2 = y1; if (!isXAxisScaler()) { xi2 = xArray2[i]; xi2 = correctX(xi2); x2 = (xi2 - winXOrigin) * winXScale; } else x2 = x1; if (sr.contains(new Point((int) x1, (int) y1)) || sr.contains(new Point((int) x2, (int) y2))) { mCurrPC.select(i); } } mViewManager.invalidateAllViews(); mCurrPC.setBatchModeOff(); } else if (toolMode == Constants.ZOOM_MODE) { if (!shiftDown) { doZoomIn(rbRect); } else { // zoom out double xInc = getXZoomIncrement(); double yInc = getYZoomIncrement(); double[] oldYs = {getMaxYVal(), getMinYVal()}; double[] newYs = { getMinYVal() < 0 ? getMinYVal() - yInc : getMinYVal() + yInc, getMaxYVal() < 0 ? getMaxYVal() - yInc : getMaxYVal() + yInc }; double[] oldXs = {getMinXVal(), getMaxXVal()}; double[] newXs = { getMinXVal() < 0 ? getMinXVal() - xInc : getMinXVal() + xInc, getMaxXVal() < 0 ? getMaxXVal() - xInc : getMaxXVal() + xInc }; this.zoomDomain(oldYs, newYs, oldXs, newXs); } mViewManager.invalidateAllViews(); } } else { // region selected int x = rbRect.x - mLeft - 5; int y = rbRect.y - mTop - 5; int x2 = x + rbRect.width; int y2 = y + rbRect.height; Point p1 = new Point(); Point p2 = new Point(); if (toolMode == Constants.SELECT_MODE) { if (!shiftDown) mCurrPC.unselectAll(); // construct a search region; int[] xpoints = {x, x2, x2, x, x}; int[] ypoints = {y, y, y2, y2, y}; Polygon sr = new Polygon(xpoints, ypoints, 5); // search for matches byte[] currFilterResults = fdm.getResults(); double[] yArray1 = getYArray1(); double[] xArray1 = getXArray1(); double[] yArray2 = getYArray2(); double[] xArray2 = getXArray2(); mCurrPC.setBatchModeOn(); for (int i = 0; i < mCurrPC.getSize(); i++) { if (!mIgnoreFilter && (mCurrPC.isDeleted(i) || currFilterResults[i] != 4 || !mCurrPC.isSelectedLayer(i))) continue; double yi = yArray1[i]; double xi = xArray1[i]; double xi2 = Float.NaN; double yi2 = Float.NaN; double xx2 = Float.NaN; double yy2 = Float.NaN; // correct the X value if necessary xi = correctX(xi); double xx1 = (xi - winXOrigin) * winXScale; double yy1 = (yi - winYOrigin) * winYScale; // correct the Y coordinate if necessary yy1 = correctY(yy1); if (!isYAxisScaler()) { yi2 = yArray2[i]; yy2 = (yi2 - winYOrigin) * winYScale; yy2 = correctY(yy2); } else yy2 = yy1; if (!isXAxisScaler()) { xi2 = xArray2[i]; xi2 = correctX(xi2); xx2 = (xi2 - winXOrigin) * winXScale; } else xx2 = xx1; p1.setLocation((int) xx1, (int) yy1); p2.setLocation((int) xx2, (int) yy2); if (sr.contains(p1) || sr.contains(p2)) { mCurrPC.select(i); } } mViewManager.invalidateAllViews(); mCurrPC.setBatchModeOff(); } else if (toolMode == Constants.ZOOM_MODE) { // correct the x value if necessary double xx = correctX((double) x); double xx2 = correctX((double) x2); // correct the Y coordinate if necessary double yy = correctY((double) y); double yy2 = correctY((double) y2); double newX = (xx / winXScale) + winXOrigin; double newX2 = (xx2 / winXScale) + winXOrigin; double newY = (yy / winYScale) + winYOrigin; double newY2 = (yy2 / winYScale) + winYOrigin; double[] oldYs = {getMaxYVal(), getMinYVal()}; if (this.toString().indexOf("Depth") >= 0) { double temp = newY2; newY2 = newY; newY = temp; } double[] newYs = {newY2, newY}; double[] oldXs = {getMinXVal(), getMaxXVal()}; double[] newXs = {newX, newX2}; this.zoomDomain(oldYs, newYs, oldXs, newXs); mViewManager.invalidateAllViews(); } } }