docs: static Geometry clip(Geometry geometry, Envelope2D extent,
  			double tolerance, double densify_dist) {
  		if (geometry.isEmpty())
  			return geometry;
  
  		if (extent.isEmpty())
  			return geometry.createInstance(); // return an empty geometry
  
  		int geomtype = geometry.getType().value();
  
  		// Test firstly the simplest geometry types point and envelope.
  		// After that we'll check the envelope intersection for the optimization
  		if (geomtype == Geometry.Type.Point.value()) {
  			Point2D pt = ((Point) geometry).getXY();
  			if (extent.contains(pt))
  				return geometry;
  			else
  				return geometry.createInstance(); // return an empty geometry
  		} else if (geomtype == Geometry.Type.Envelope.value()) {
  			Envelope2D env = new Envelope2D();
  			geometry.queryEnvelope2D(env);
  			if (env.intersect(extent)) {
  				Envelope result_env = new Envelope();
  				geometry.copyTo(result_env);
  				result_env.setEnvelope2D(env);
  				return result_env;
  			} else
  				return geometry.createInstance(); // return an empty geometry
  		}
  
  		// Test the geometry envelope
  		Envelope2D env_2D = new Envelope2D();
  		geometry.queryLooseEnvelope2D(env_2D);
  		if (extent.contains(env_2D))
  			return geometry;// completely inside of bounds
  		if (!extent.isIntersecting(env_2D))
  			return geometry.createInstance();// outside of bounds. return empty
  												// geometry.
  
  		MultiVertexGeometryImpl impl = (MultiVertexGeometryImpl) geometry
  				._getImpl();
  		GeometryAccelerators accel = impl._getAccelerators();
  		if (accel != null) {
  			RasterizedGeometry2D rgeom = accel.getRasterizedGeometry();
  			if (rgeom != null) {
  				RasterizedGeometry2D.HitType hit = rgeom
  						.queryEnvelopeInGeometry(extent);
  				if (hit == RasterizedGeometry2D.HitType.Inside) {
  					if (geomtype != Geometry.Type.Polygon.value())
  						throw GeometryException.GeometryInternalError();
  
  					Polygon poly = new Polygon(geometry.getDescription());
  					poly.addEnvelope(extent, false);
  					return poly;
  				} else if (hit == RasterizedGeometry2D.HitType.Outside) {
  					return geometry.createInstance();// outside of bounds.
  														// return empty
  														// geometry.
  				}
  			}
  		}
  
  		switch (geomtype) {
  		case Geometry.GeometryType.MultiPoint: {
  			MultiPoint multi_point = (MultiPoint) geometry;
  			MultiPoint multi_point_out = null;
  			int npoints = multi_point.getPointCount();
  			AttributeStreamOfDbl xy = (AttributeStreamOfDbl) ((MultiPointImpl) multi_point
  					._getImpl())
  					.getAttributeStreamRef(VertexDescription.Semantics.POSITION);
  			// create the new geometry only if there are points that has been
  			// clipped out.
  			// If all vertices are inside of the envelope, it returns the input
  			// multipoint.
  			int ipoints0 = 0;
  			for (int ipoints = 0; ipoints < npoints; ipoints++) {
  				Point2D pt = new Point2D();
  				xy.read(2 * ipoints, pt);
  
  				if (!extent.contains(pt)) {// vertex is outside of the envelope
  					if (ipoints0 == 0)
  						multi_point_out = (MultiPoint) multi_point
  								.createInstance();
  
  					if (ipoints0 < ipoints)
  						multi_point_out.add(multi_point, ipoints0, ipoints);
  
  					ipoints0 = ipoints + 1;// ipoints0 contains index of vertex
  											// right after the last clipped out
  											// vertex.
  				}
  			}
  
  			// add the rest of the batch to the result multipoint (only if
  			// something has been already clipped out)
  			if (ipoints0 > 0)
  				multi_point_out.add(multi_point, ipoints0, npoints);
  
  			if (ipoints0 == 0)
  				return multi_point;// everything is inside, so return the input
  									// geometry
  			else
  				return multi_point_out;// clipping has happend, return the
  										// clipped geometry
  		}
  		case Geometry.GeometryType.Polygon:
  		case Geometry.GeometryType.Polyline:
  			return clipMultiPath_((MultiPath) geometry, extent, tolerance,
  					densify_dist);
  		default:
  			assert (false);
  			throw GeometryException.GeometryInternalError();
  		}
  	}