public void addObstacles(Geometry ... geom) { if (this.mapFilename == null) throw new Error("Please set a map file first!"); BufferedImage img = null; try { img = ImageIO.read(new File(mapFilename)); System.out.println("IMGTYPE: " + img.getType()); Graphics2D g2 = img.createGraphics(); ShapeWriter writer = new ShapeWriter(); g2.setPaint(Color.black); for (Geometry g : geom) { AffineTransformation at = new AffineTransformation(); at.scale(1.0/mapResolution, -1.0/mapResolution); at.translate(0, img.getHeight()); Geometry scaledGeom = at.transform(g); Shape shape = writer.toShape(scaledGeom); System.out.println("Shape: " + shape.getBounds2D()); g2.fill(shape); } File outputfile = new File(TEMP_MAP_DIR + File.separator + "tempMap" + (numObstacles++) + ".png"); ImageIO.write(img, "png", outputfile); this.mapFilename = outputfile.getAbsolutePath(); } catch (IOException e) { e.printStackTrace(); } }
public void addObstacles(Geometry geom, Pose ... poses) { if (this.mapFilename == null) throw new Error("Please set a map file first!"); BufferedImage img = null; try { img = ImageIO.read(new File(mapFilename)); Graphics2D g2 = img.createGraphics(); ShapeWriter writer = new ShapeWriter(); g2.setPaint(Color.black); for (Pose pose : poses) { AffineTransformation at = new AffineTransformation(); at.rotate(pose.getTheta()); at.translate(pose.getX(), pose.getY()); at.scale(1.0/mapResolution, -1.0/mapResolution); at.translate(0, img.getHeight()); Geometry scaledGeom = at.transform(geom); Shape shape = writer.toShape(scaledGeom); System.out.println("Shape: " + shape.getBounds2D()); g2.fill(shape); } File outputfile = new File(TEMP_MAP_DIR + File.separator + "tempMap" + (numObstacles++) + ".png"); ImageIO.write(img, "png", outputfile); this.mapFilename = outputfile.getAbsolutePath(); } catch (IOException e) { e.printStackTrace(); } }
private Geometry makeObstacle(Pose p) { GeometryFactory gf = new GeometryFactory(); Geometry geom = null; if (obstacleFootprint == null) { geom = gf.createPolygon(new Coordinate[] { new Coordinate(0.0,0.0), new Coordinate(0.0,OBSTACLE_SIZE), new Coordinate(OBSTACLE_SIZE,OBSTACLE_SIZE), new Coordinate(OBSTACLE_SIZE,0.0), new Coordinate(0.0,0.0) }); } else { geom = gf.createPolygon(obstacleFootprint); } AffineTransformation at = new AffineTransformation(); at.rotate(p.getTheta()); at.translate(p.getX(), p.getY()); Geometry transGeom = at.transform(geom); Pose center = new Pose(p.getX(), p.getY(), p.getTheta()); obstacles.add(transGeom); obstacleCenters.add(center); return transGeom; }
/** * If the user mutate the geometry of the object, the object cache must not * be updated. */ private void testInPlaceUpdate() throws SQLException { Connection conn = getConnection(url); try { ResultSet rs = conn.createStatement().executeQuery( "SELECT 'POINT(1 1)'::geometry"); assertTrue(rs.next()); // Mutate the geometry ((Geometry) rs.getObject(1)).apply(new AffineTransformation(1, 0, 1, 1, 0, 1)); rs.close(); rs = conn.createStatement().executeQuery( "SELECT 'POINT(1 1)'::geometry"); assertTrue(rs.next()); // Check if the geometry is the one requested assertEquals(1, ((Point) rs.getObject(1)).getX()); assertEquals(1, ((Point) rs.getObject(1)).getY()); rs.close(); } finally { conn.close(); } }
private Geometry createArrow(Pose pose1, Pose pose2) { GeometryFactory gf = new GeometryFactory(); double aux = 1.8; double distance = (1.6/targetArrowHeadWidth)*Math.sqrt(Math.pow((pose2.getX()-pose1.getX()),2)+Math.pow((pose2.getY()-pose1.getY()),2)); double theta = Math.atan2(pose2.getY() - pose1.getY(), pose2.getX() - pose1.getX()); Coordinate[] coords = new Coordinate[8]; coords[0] = new Coordinate(0.0,-0.3); coords[1] = new Coordinate(distance-aux,-0.3); coords[2] = new Coordinate(distance-aux,-0.8); coords[3] = new Coordinate(distance,0.0); coords[4] = new Coordinate(distance-aux,0.8); coords[5] = new Coordinate(distance-aux,0.3); coords[6] = new Coordinate(0.0,0.3); coords[7] = new Coordinate(0.0,-0.3); Polygon arrow = gf.createPolygon(coords); AffineTransformation at = new AffineTransformation(); at.scale(targetArrowHeadWidth/1.6, targetArrowHeadWidth/1.6); at.rotate(theta); at.translate(pose1.getX(), pose1.getY()); Geometry ret = at.transform(arrow); return ret; }
private Geometry createArrow(Pose pose) { GeometryFactory gf = new GeometryFactory(); Coordinate[] coords = new Coordinate[8]; coords[0] = new Coordinate(0.0,-0.3); coords[1] = new Coordinate(2.0,-0.3); coords[2] = new Coordinate(2.0,-0.8); coords[3] = new Coordinate(3.0,0.0); coords[4] = new Coordinate(2.0,0.8); coords[5] = new Coordinate(2.0,0.3); coords[6] = new Coordinate(0.0,0.3); coords[7] = new Coordinate(0.0,-0.3); Polygon arrow = gf.createPolygon(coords); AffineTransformation at = new AffineTransformation(); at.scale(1.6/targetArrowHeadWidth, 1.6/targetArrowHeadWidth); at.rotate(pose.getTheta()); at.translate(pose.getX(), pose.getY()); Geometry ret = at.transform(arrow); return ret; }
public AsteroidsSprite getNearestIntersected(Point loc, double rot, GameState world) { // Create worldTransform to transform to the local-space shape to world-space: AffineTransformation trans = new AffineTransformation(); trans.rotate(-rot); trans.translate(loc.getX(), loc.getY()); worldShape = new PolyWrapper((Polygon)trans.transform(localShape)); // List of AsteroidsSprites that intersect with a circle described by // <loc, distanceToNearest> List<AsteroidsSprite> intersected = world.intersects(worldShape); // Nearest AsteroidsSprite: AsteroidsSprite nearest = AsteroidsSprite.nearest(loc, intersected); return nearest; }
public static AffineTransformation getWorldToRectangle( Envelope worldEnvelope, Rectangle pixelRectangle ) { int cols = (int) pixelRectangle.getWidth(); int rows = (int) pixelRectangle.getHeight(); double worldWidth = worldEnvelope.getWidth(); double worldHeight = worldEnvelope.getHeight(); double x = -worldEnvelope.getMinX(); double y = -worldEnvelope.getMinY(); AffineTransformation translate = AffineTransformation.translationInstance(x, y); double xScale = cols / worldWidth; double yScale = rows / worldHeight; AffineTransformation scale = AffineTransformation.scaleInstance(xScale, yScale); int m00 = 1; int m10 = 0; int m01 = 0; int m11 = -1; int m02 = 0; int m12 = rows; AffineTransformation mirror_y = new AffineTransformation(m00, m01, m02, m10, m11, m12); AffineTransformation world2pixel = new AffineTransformation(translate); world2pixel.compose(scale); world2pixel.compose(mirror_y); return world2pixel; }
public void testTransformationUtils2() throws Exception { Envelope env = new Envelope(100, 200, 1000, 5000); Rectangle rect = new Rectangle(0, 0, 100, 4000); AffineTransformation worldToPixel = TransformationUtils.getWorldToRectangle(env, rect); Coordinate srcPt = new Coordinate(150.0, 3000.0); Coordinate transformed = worldToPixel.transform(srcPt, new Coordinate()); assertEquals(50, (int) transformed.x); assertEquals(2000, (int) transformed.y); srcPt = new Coordinate(100.0, 1000.0); transformed = worldToPixel.transform(srcPt, new Coordinate()); assertEquals(0, (int) transformed.x); assertEquals(4000, (int) transformed.y); }
protected Geometry rotate(Geometry geom) { if (this.rotationAngle != 0.0) { AffineTransformation trans = AffineTransformation.rotationInstance(this.rotationAngle, this.dim.getCentre().x, this.dim.getCentre().y); geom.apply(trans); } return geom; }
public static void transformTest() { /* * 37.5116785 127.1021436 * * 37.5119898 127.1025482 * * 37.5134122 127.1025103 */ /* * */ Coordinate coord1 = new Coordinate(0.14195979899497488, 0.056451612903225756); Coordinate coord2 = new Coordinate(0.44597989949748745, 0.1088709677419355); Coordinate coord3 = new Coordinate(0.9849246231155779, 0.9905913978494624); Coordinate coord4 = new Coordinate(37.5116785, 127.1021436); Coordinate coord5 = new Coordinate(37.5119898, 127.1025482); Coordinate coord6 = new Coordinate(37.5134122, 127.1025103); Coordinate test = new Coordinate(0.9510050251256281, 0.728494623655914); Point p = JTSFactoryFinder.getGeometryFactory().createPoint(test); AffineTransformation affine = new AffineTransformationBuilder(coord1, coord2, coord3, coord4, coord5, coord6).getTransformation(); Geometry geom = affine.transform(p); System.out.println(geom.toText()); }
/** * Returns a {@link Geometry} representing the footprint of the robot in a given pose. * @param x The x coordinate of the pose used to create the footprint. * @param y The y coordinate of the pose used to create the footprint. * @param theta The orientation of the pose used to create the footprint. * @return A {@link Geometry} representing the footprint of the robot in a given pose. */ public Geometry makeFootprint(double x, double y, double theta) { AffineTransformation at = new AffineTransformation(); at.rotate(theta); at.translate(x,y); Geometry rect = at.transform(footprint); return rect; }
/** * Returns a {@link Geometry} representing the inner footprint of the robot in a given pose. * @param x The x coordinate of the pose used to create the inner footprint. * @param y The y coordinate of the pose used to create the inner footprint. * @param theta The orientation of the pose used to create the inner footprint. * @return A {@link Geometry} representing the footprint of the inner robot in a given pose. */ public Geometry makeInnerFootprint(double x, double y, double theta) { AffineTransformation at = new AffineTransformation(); at.rotate(theta); at.translate(x,y); Geometry rect = at.transform(innerFootprint); return rect; }
@Override public List<Geometry> listToroidalGeometries(final Geometry geom) { final Geometry copy = (Geometry) geom.clone(); final List<Geometry> geoms = new ArrayList<Geometry>(); final AffineTransformation at = new AffineTransformation(); geoms.add(copy); for (int cnt = 0; cnt < 8; cnt++) { at.setToTranslation(adjustedXVector[cnt], adjustedYVector[cnt]); geoms.add(at.transform(copy)); } return geoms; }
public Geometry returnToroidalGeom(final GamaPoint loc) { final List<Geometry> geoms = new ArrayList<Geometry>(); final Point pt = GeometryUtils.GEOMETRY_FACTORY.createPoint(loc); final AffineTransformation at = new AffineTransformation(); geoms.add(pt); for (int cnt = 0; cnt < 8; cnt++) { at.setToTranslation(adjustedXVector[cnt], adjustedYVector[cnt]); geoms.add(at.transform(pt)); } return GeometryUtils.GEOMETRY_FACTORY.buildGeometry(geoms); }
protected Geometry rotate(Geometry geom) { if (rotationAngle != 0.0) { AffineTransformation trans = AffineTransformation.rotationInstance(rotationAngle, dim.getCentre().x, dim.getCentre().y); geom.apply(trans); } return geom; }
/** * Helper function to run the Vectorize operation with given parameters and * retrieve the vectors. * * @param src the source {@link GridCoverage2D}. * @param args a {@code Map} of parameter names and values or <code>null</code>. * * @return the generated vectors as JTS Polygons */ @SuppressWarnings("unchecked") public static Collection<Polygon> doVectorize( GridCoverage2D src, Map<String, Object> args ) { if (args == null) { args = new HashMap<String, Object>(); } ParameterBlockJAI pb = new ParameterBlockJAI("Vectorize"); pb.setSource("source0", src.getRenderedImage()); // Set any parameters that were passed in for( Entry<String, Object> e : args.entrySet() ) { pb.setParameter(e.getKey(), e.getValue()); } // Get the desintation image: this is the unmodified source image data // plus a property for the generated vectors RenderedOp dest = JAI.create("Vectorize", pb); // Get the vectors Collection<Polygon> polygons = (Collection<Polygon>) dest.getProperty(VectorizeDescriptor.VECTOR_PROPERTY_NAME); RegionMap regionParams = CoverageUtilities.getRegionParamsFromGridCoverage(src); double xRes = regionParams.getXres(); double yRes = regionParams.getYres(); final AffineTransform mt2D = (AffineTransform) src.getGridGeometry().getGridToCRS2D(PixelOrientation.CENTER); final AffineTransformation jtsTransformation = new AffineTransformation(mt2D.getScaleX(), mt2D.getShearX(), mt2D.getTranslateX() - xRes / 2.0, mt2D.getShearY(), mt2D.getScaleY(), mt2D.getTranslateY() + yRes / 2.0); for( Polygon polygon : polygons ) { polygon.apply(jtsTransformation); } return polygons; }
public static Geometry translate(Geometry g, double x, double y) { AffineTransformation trans = AffineTransformation.translationInstance(x, y); Geometry newG = (Geometry) g.clone(); newG.apply(trans); return newG; }
/** * Rotates the geometry around the point (x,y) * @param g * @param angle * @param x * @param y * @return */ public static Geometry rotate(Geometry g, double angle, double x, double y) { AffineTransformation trans = AffineTransformation.translationInstance(-x, -y); trans.rotate(angle); trans.translate(x, y); Geometry newG = (Geometry) g.clone(); newG.apply(trans); return newG; }
/** * Reflects the geometry around the line x=y. * * @param g * @param angle * @return */ public static Geometry reflect(Geometry g) { AffineTransformation trans = AffineTransformation.reflectionInstance(1, 1); Geometry newG = (Geometry) g.clone(); newG.apply(trans); return newG; }
/** * <p>Create geometry clipped and then converted to MVT 'extent' coordinates. Result * contains both clipped geometry (intersection) and transformed geometry for encoding to * MVT.</p> * <p>Allows specifying separate tile and clipping coordinates. {@code clipEnvelope} can be bigger * than {@code tileEnvelope} to have geometry exist outside the MVT tile extent.</p> * * @param geometryList original 'source' geometry, passed through * {@link #flatFeatureList(Geometry)} * @param tileEnvelope world coordinate bounds for tile, used for transforms * @param clipEnvelope world coordinates to clip tile by * @param geomFactory creates a geometry for the tile envelope * @param mvtLayerParams specifies vector tile properties * @param filter geometry values that fail filter after transforms are removed * @return tile geometry result * @see TileGeomResult */ public static TileGeomResult createTileGeom(List<Geometry> geometryList, Envelope tileEnvelope, Envelope clipEnvelope, GeometryFactory geomFactory, MvtLayerParams mvtLayerParams, IGeometryFilter filter) { final Geometry tileClipGeom = geomFactory.toGeometry(clipEnvelope); final AffineTransformation t = new AffineTransformation(); final double xDiff = tileEnvelope.getWidth(); final double yDiff = tileEnvelope.getHeight(); final double xOffset = -tileEnvelope.getMinX(); final double yOffset = -tileEnvelope.getMinY(); // Transform Setup: Shift to 0 as minimum value t.translate(xOffset, yOffset); // Transform Setup: Scale X and Y to tile extent values, flip Y values t.scale(1d / (xDiff / (double) mvtLayerParams.extent), -1d / (yDiff / (double) mvtLayerParams.extent)); // Transform Setup: Bump Y values to positive quadrant t.translate(0d, (double) mvtLayerParams.extent); // The area contained in BOTH the 'original geometry', g, AND the 'clip envelope geometry' is // the 'tile geometry' final List<Geometry> intersectedGeoms = flatIntersection(tileClipGeom, geometryList); final List<Geometry> transformedGeoms = new ArrayList<>(intersectedGeoms.size()); // Transform intersected geometry Geometry nextTransformGeom; Object nextUserData; for (Geometry nextInterGeom : intersectedGeoms) { nextUserData = nextInterGeom.getUserData(); nextTransformGeom = t.transform(nextInterGeom); // Floating --> Integer, still contained within doubles nextTransformGeom.apply(RoundingFilter.INSTANCE); // TODO: Refactor line simplification // Can't use 0d, specify value < .5d nextTransformGeom = TopologyPreservingSimplifier.simplify(nextTransformGeom, .1d); nextTransformGeom.setUserData(nextUserData); // Apply filter on transformed geometry if (filter.accept(nextTransformGeom)) { transformedGeoms.add(nextTransformGeom); } } return new TileGeomResult(intersectedGeoms, transformedGeoms); }
@Override public ILocation normalizeLocation(final ILocation point, final boolean nullIfOutside) { // TODO Subclass (or rewrite) this naive implementation to take care of // irregular // geometries. // TODO Take into account the fact that some topologies may consider // invalid locations. if (environment.getGeometry().covers(point)) { return point; } if (isTorus()) { final Point pt = GeometryUtils.GEOMETRY_FACTORY.createPoint(GeometryUtils.toCoordinate(point)); for (int cnt = 0; cnt < 8; cnt++) { final AffineTransformation at = new AffineTransformation(); at.translate(adjustedXVector[cnt], adjustedYVector[cnt]); final GamaPoint newPt = new GamaPoint(at.transform(pt).getCoordinate()); if (environment.getGeometry().covers(newPt)) { return newPt; } } } // See if rounding errors of double do not interfere with the // computation. // In which case, the use of Maths.approxEquals(value1, value2, // tolerance) could help. // if ( envWidth == 0.0 ) { // xx = xx != envMinX ? nullIfOutside ? nil : envMinX : xx; // } else if ( xx < envMinX /* && xx > hostMinX - precision */) { // xx = /* !isTorus ? */nullIfOutside ? nil : envMinX /* : xx % envWidth // + envWidth */; // } else if ( xx >= envMaxX /*- precision*/) { // xx = /* !isTorus ? */nullIfOutside ? nil : envMaxX /* : xx % envWidth // */; // } // if ( xx == nil ) { return null; } // if ( envHeight == 0.0 ) { // yy = yy != envMinY ? nullIfOutside ? nil : envMinY : yy; // } else if ( yy < envMinY/* && yy > hostMinY - precision */) { // yy = /* !isTorus ? */nullIfOutside ? nil : envMinY /* : yy % // envHeight + envHeight */; // } else if ( yy >= envMaxY /*- precision*/) { // yy = /* !isTorus ? */nullIfOutside ? nil : envMaxY /* : yy % // envHeight */; // } // if ( yy == nil ) { return null; } // point.setLocation(xx, yy, point.getZ()); return null; }
public static AffineTransformation getRectangleToWorld( Rectangle pixelRectangle, Envelope worldEnvelope ) throws NoninvertibleTransformationException { return getWorldToRectangle(worldEnvelope, pixelRectangle).getInverse(); }
/** * Extracts traversal sections of a given with from the supplied {@link Coordinate}s. * * @param coordinates the list of coordinates. * @param width the total with of the sections. * @return the list of {@link LineString sections}. */ public static List<LineString> getSectionsFromCoordinates( List<Coordinate> coordinates, double width ) { if (coordinates.size() < 3) { throw new IllegalArgumentException("This method works only on lines with at least 3 coordinates."); } double halfWidth = width / 2.0; List<LineString> linesList = new ArrayList<LineString>(); // first section Coordinate centerCoordinate = coordinates.get(0); LineSegment l1 = new LineSegment(centerCoordinate, coordinates.get(1)); Coordinate leftCoordinate = l1.pointAlongOffset(0.0, halfWidth); Coordinate rightCoordinate = l1.pointAlongOffset(0.0, -halfWidth); LineString lineString = geomFactory.createLineString(new Coordinate[]{leftCoordinate, centerCoordinate, rightCoordinate}); linesList.add(lineString); for( int i = 1; i < coordinates.size() - 1; i++ ) { Coordinate previous = coordinates.get(i - 1); Coordinate current = coordinates.get(i); Coordinate after = coordinates.get(i + 1); double firstAngle = azimuth(current, previous); double secondAngle = azimuth(current, after); double a1 = min(firstAngle, secondAngle); double a2 = max(firstAngle, secondAngle); double centerAngle = a1 + (a2 - a1) / 2.0; AffineTransformation rotationInstance = AffineTransformation.rotationInstance(-toRadians(centerAngle), current.x, current.y); LineString vertical = geomFactory.createLineString(new Coordinate[]{new Coordinate(current.x, current.y + halfWidth), current, new Coordinate(current.x, current.y - halfWidth)}); Geometry transformed = rotationInstance.transform(vertical); linesList.add((LineString) transformed); } // last section centerCoordinate = coordinates.get(coordinates.size() - 1); LineSegment l2 = new LineSegment(centerCoordinate, coordinates.get(coordinates.size() - 2)); leftCoordinate = l2.pointAlongOffset(0.0, halfWidth); rightCoordinate = l2.pointAlongOffset(0.0, -halfWidth); lineString = geomFactory.createLineString(new Coordinate[]{leftCoordinate, centerCoordinate, rightCoordinate}); linesList.add(lineString); return linesList; }
/** * Returns a {@link Geometry} representing the footprint of a robot in a given pose. * @param fp A polygon representing the footprint of the robot centered in (0,0) * and appropriately oriented (can be obtained from a {@link TrajectoryEnvelope} instance * via method {@link TrajectoryEnvelope#getFootprint()}). * @param x The x coordinate of the pose used to create the footprint. * @param y The y coordinate of the pose used to create the footprint. * @param theta The orientation of the pose used to create the footprint. * @return A {@link Geometry} representing the footprint of the robot in a given pose. */ public static Geometry getFootprint(Polygon fp, double x, double y, double theta) { AffineTransformation at = new AffineTransformation(); at.rotate(theta); at.translate(x,y); Geometry rect = at.transform(fp); return rect; }