Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add faster MathUtil.hypot function #1112

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.awt.Stroke;
import java.awt.geom.Point2D;

import org.locationtech.jts.math.MathUtil;

public class AWTUtil
{

Expand All @@ -35,7 +37,7 @@ public static Point2D multiply(Point2D v, double x) {
public static Point2D vector(Point2D a, Point2D b, double size) {
double dx = b.getX() - a.getX();
double dy = b.getY() - a.getY();
double len = Math.hypot(dx, dy);
double len = MathUtil.hypot(dx, dy);
return new Point2D.Double(size * dx/len, size * dy/len);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;

import org.locationtech.jts.math.MathUtil;
import org.locationtech.jtstest.testbuilder.ui.Viewport;


Expand Down Expand Up @@ -101,7 +102,7 @@ private static GeneralPath arrowHalfOffset(Point2D p0, Point2D p1) {
double dx = p1.getX() - p0.getX();
double dy = p1.getY() - p0.getY();

double len = Math.hypot(dx, dy);
double len = MathUtil.hypot(dx, dy);

double vy = dy / len;
double vx = dx / len;
Expand Down Expand Up @@ -162,7 +163,7 @@ private static GeneralPath arrowHeadHalf(Point2D origin, Point2D p1,
double dx = p1.getX() - origin.getX();
double dy = p1.getY() - origin.getY();

double vlen = Math.hypot(dx, dy);
double vlen = MathUtil.hypot(dx, dy);

if (vlen <= 0) return null;

Expand Down Expand Up @@ -206,7 +207,7 @@ private static boolean isTooSmallToRender(Point2D p0, Point2D p1, double minLen)
double dx = p1.getX() - p0.getX();
double dy = p1.getY() - p0.getY();

double len = Math.hypot(dx, dy);
double len = MathUtil.hypot(dx, dy);

return len < minLen;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.locationtech.jts.geom.LineSegment;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.math.MathUtil;
import org.locationtech.jtstest.testbuilder.geom.ConstrainedInteriorPoint;
import org.locationtech.jtstest.testbuilder.ui.GraphicsUtil;
import org.locationtech.jtstest.testbuilder.ui.Viewport;
Expand Down Expand Up @@ -103,7 +104,7 @@ private void paintLabelLine(String label, Geometry line, Viewport viewport, Grap
double offsetLen = 15;
double nudgeX = 5;

double dirVecLen = Math.hypot(dx, dy);
double dirVecLen = MathUtil.hypot(dx, dy);

double offsetX = offsetLen * dx / dirVecLen;
double offsetY = offsetLen * dy / dirVecLen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public static double length(CoordinateSequence pts)
double dx = x1 - x0;
double dy = y1 - y0;

len += Math.hypot(dx, dy);
len += MathUtil.hypot(dx, dy);

x0 = x1;
y0 = y1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateSequence;
import org.locationtech.jts.math.MathUtil;

/**
* Functions for computing length.
Expand Down Expand Up @@ -49,7 +50,7 @@ public static double ofLine(CoordinateSequence pts)
double dx = x1 - x0;
double dy = y1 - y0;

len += Math.hypot(dx, dy);
len += MathUtil.hypot(dx, dy);

x0 = x1;
y0 = y1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.io.WKTWriter;
import org.locationtech.jts.math.MathUtil;
import org.locationtech.jts.util.Assert;

/**
Expand Down Expand Up @@ -131,7 +132,7 @@ public static double nonRobustComputeEdgeDistance(
{
double dx = p.x - p1.x;
double dy = p.y - p1.y;
double dist = Math.hypot(dx, dy); // dummy value
double dist = MathUtil.hypot(dx, dy); // dummy value
Assert.isTrue(! (dist == 0.0 && ! p.equals(p1)), "Invalid distance calculation");
return dist;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Triangle;
import org.locationtech.jts.math.MathUtil;
import org.locationtech.jts.util.Assert;

/**
Expand Down Expand Up @@ -367,7 +368,7 @@ private static Coordinate pointWitMinAngleWithX(Coordinate[] pts, Coordinate P)
double dx = p.x - P.x;
double dy = p.y - P.y;
if (dy < 0) dy = -dy;
double len = Math.hypot(dx, dy);
double len = MathUtil.hypot(dx, dy);
double sin = dy / len;

if (sin < minSin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.Serializable;
import java.util.Comparator;

import org.locationtech.jts.math.MathUtil;
import org.locationtech.jts.util.Assert;
import org.locationtech.jts.util.NumberUtil;

Expand Down Expand Up @@ -435,7 +436,7 @@ public Coordinate create() {
public double distance(Coordinate c) {
double dx = x - c.x;
double dy = y - c.y;
return Math.hypot(dx, dy);
return MathUtil.hypot(dx, dy);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import java.io.Serializable;

import org.locationtech.jts.math.MathUtil;

/**
* Defines a rectangular region of the 2D coordinate plane.
* It is often used to represent the bounding box of a {@link Geometry},
Expand Down Expand Up @@ -303,7 +305,7 @@ public double getDiameter() {
}
double w = getWidth();
double h = getHeight();
return Math.hypot(w, h);
return MathUtil.hypot(w, h);
}
/**
* Returns the <code>Envelope</code>s minimum x-value. min x &gt; max x
Expand Down Expand Up @@ -780,7 +782,7 @@ else if (minx > env.maxx)
// if either is zero, the envelopes overlap either vertically or horizontally
if (dx == 0.0) return dy;
if (dy == 0.0) return dx;
return Math.hypot(dx, dy);
return MathUtil.hypot(dx, dy);
}

public boolean equals(Object other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.locationtech.jts.algorithm.Orientation;
import org.locationtech.jts.algorithm.RobustLineIntersector;
import org.locationtech.jts.io.WKTConstants;
import org.locationtech.jts.math.MathUtil;


/**
Expand Down Expand Up @@ -337,7 +338,7 @@ public Coordinate pointAlongOffset(double segmentLengthFraction, double offsetDi

double dx = p1.x - p0.x;
double dy = p1.y - p0.y;
double len = Math.hypot(dx, dy);
double len = MathUtil.hypot(dx, dy);
double ux = 0.0;
double uy = 0.0;
if (offsetDistance != 0.0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.locationtech.jts.geom.CoordinateSequence;
import org.locationtech.jts.geom.CoordinateSequenceFilter;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.math.MathUtil;
import org.locationtech.jts.util.Assert;
/**
* Represents an affine transformation on the 2D Cartesian plane.
Expand Down Expand Up @@ -494,7 +495,7 @@ public AffineTransformation setToReflectionBasic(double x0, double y0, double x1
}
double dx = x1 - x0;
double dy = y1 - y0;
double d = Math.hypot(dx, dy);
double d = MathUtil.hypot(dx, dy);
double sin = dy / d;
double cos = dx / d;
double cs2 = 2 * sin * cos;
Expand Down Expand Up @@ -525,7 +526,7 @@ public AffineTransformation setToReflection(double x0, double y0, double x1, dou
// rotate vector to positive x axis direction
double dx = x1 - x0;
double dy = y1 - y0;
double d = Math.hypot(dx, dy);
double d = MathUtil.hypot(dx, dy);
double sin = dy / d;
double cos = dx / d;
rotate(-sin, cos);
Expand Down Expand Up @@ -576,7 +577,7 @@ public AffineTransformation setToReflection(double x, double y)
}

// rotate vector to positive x axis direction
double d = Math.hypot(x, y);
double d = MathUtil.hypot(x, y);
double sin = y / d;
double cos = x / d;
rotate(-sin, cos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.locationtech.jts.index.strtree;

import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.math.MathUtil;

/**
* Functions for computing distances between {@link Envelope}s.
Expand Down Expand Up @@ -44,7 +45,7 @@ public static double maximumDistance(Envelope env1, Envelope env2)
private static double distance(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
return Math.hypot(dx, dy);
return MathUtil.hypot(dx, dy);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions modules/core/src/main/java/org/locationtech/jts/math/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ public static int ceil(int num, int denom) {
return div * denom >= num ? div : div + 1;
}

/**
* Computes the length of the vector (x,y).
* This is the length of the hypotenuse of
* a right triangle with sides of length x and y.
*
* This function is faster than the standard {@link Math.hypot} function.
*
* @param x the x ordinate
* @param y the y ordinate
* @return the length of vector (x,y)
*/
public static double hypot(double x, double y) {
return Math.sqrt(x * x + y * y);
}

private static final double LOG_10 = Math.log(10);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public Vector2D negate() {
}

public double length() {
return Math.hypot(x, y);
return MathUtil.hypot(x, y);
}

public double lengthSquared() {
Expand Down Expand Up @@ -196,7 +196,7 @@ public double distance(Vector2D v)
{
double delx = v.x - x;
double dely = v.y - y;
return Math.hypot(delx, dely);
return MathUtil.hypot(delx, dely);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.locationtech.jts.geom.LineSegment;
import org.locationtech.jts.geom.Position;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.math.MathUtil;

/**
* Generates segments which form an offset curve.
Expand Down Expand Up @@ -398,7 +399,7 @@ static void computeOffsetSegment(LineSegment seg, int side, double distance, Lin
int sideSign = side == Position.LEFT ? 1 : -1;
double dx = seg.p1.x - seg.p0.x;
double dy = seg.p1.y - seg.p0.y;
double len = Math.hypot(dx, dy);
double len = MathUtil.hypot(dx, dy);
// u is the vector that is the length of the offset, in the direction of the segment
double ux = sideSign * distance * dx / len;
double uy = sideSign * distance * dy / len;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.util.LinearComponentExtracter;
import org.locationtech.jts.math.MathUtil;

/**
* Generates points offset by a given distance
Expand Down Expand Up @@ -95,7 +96,7 @@ private void computeOffsetPoints(Coordinate p0, Coordinate p1, double offsetDist
{
double dx = p1.x - p0.x;
double dy = p1.y - p0.y;
double len = Math.hypot(dx, dy);
double len = MathUtil.hypot(dx, dy);
// u is the vector that is the length of the offset, in the direction of the segment
double ux = offsetDistance * dx / len;
double uy = offsetDistance * dy / len;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.locationtech.jts.algorithm.HCoordinate;
import org.locationtech.jts.algorithm.NotRepresentableException;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.math.MathUtil;

/**
* Models a site (node) in a {@link QuadEdgeSubdivision}.
Expand Down Expand Up @@ -161,7 +162,7 @@ Vertex sub(Vertex v) {

/* magnitude of vector */
double magn() {
return (Math.hypot(p.x, p.y));
return (MathUtil.hypot(p.x, p.y));
}

/* returns k X v (cross product). this is a vector perpendicular to v */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.locationtech.jtslab.edgeray;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.math.MathUtil;

class EdgeRay {

Expand All @@ -28,7 +29,7 @@ public static double areaTermBoth(double x0, double y0, double x1, double y1) {

double dx = x1 - x0;
double dy = y1 - y0;
double len = Math.hypot(dx, dy);
double len = MathUtil.hypot(dx, dy);

double u0x = dx / len;
double u0y = dy / len;
Expand Down Expand Up @@ -59,7 +60,7 @@ public static double areaTerm(

double dx = x1 - x0;
double dy = y1 - y0;
double len = Math.hypot(dx, dy);
double len = MathUtil.hypot(dx, dy);

if (len <= 0) return 0;

Expand Down
Loading