Skip to content

Commit

Permalink
replace Point2D with Point2d
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Jun 26, 2024
1 parent cd6c0fa commit 7bad48a
Show file tree
Hide file tree
Showing 36 changed files with 229 additions and 326 deletions.
47 changes: 24 additions & 23 deletions src/main/java/com/marginallyclever/convenience/Bezier.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.marginallyclever.convenience;

import javax.vecmath.Point2d;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -33,15 +34,15 @@ public Bezier(double x0,double y0,double x1,double y1,double x2,double y2,double

// Based on https://github.com/pelson/antigrain/blob/master/agg-2.4/src/agg_curves.cpp
// and https://github.com/mattdesl/adaptive-bezier-curve
public List<Point2D> generateCurvePoints(double distanceTolerance) {
ArrayList<Point2D> points = new ArrayList<Point2D>();
points.add(new Point2D(x0,y0));
public List<Point2d> generateCurvePoints(double distanceTolerance) {
List<Point2d> points = new ArrayList<>();
points.add(new Point2d(x0,y0));
recursive(x0,y0,x1,y1,x2,y2,x3,y3,points,distanceTolerance*distanceTolerance,0);
points.add(new Point2D(x3,y3));
points.add(new Point2d(x3,y3));
return points;
}

private void recursive(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,ArrayList<Point2D> points, double distanceTolerance,int level) {
private void recursive(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,List<Point2d> points, double distanceTolerance,int level) {
if(level > recursionLimit)
return;

Expand Down Expand Up @@ -74,7 +75,7 @@ private void recursive(double x1,double y1,double x2,double y2,double x3,double
if((d2 + d3)*(d2 + d3) <= distanceTolerance * (dx*dx + dy*dy)) {
// If the curvature doesn't exceed the distanceTolerance value we tend to finish subdivisions.
if(angleTolerance < curveAngleToleranceEpsilon) {
points.add(new Point2D(x1234, y1234));
points.add(new Point2d(x1234, y1234));
return;
}

Expand All @@ -87,17 +88,17 @@ private void recursive(double x1,double y1,double x2,double y2,double x3,double

if(da1 + da2 < angleTolerance) {
// Finally we can stop the recursion
points.add(new Point2D(x1234, y1234));
points.add(new Point2d(x1234, y1234));
return;
}

if(cuspLimit != 0.0) {
if(da1 > cuspLimit) {
points.add(new Point2D(x2, y2));
points.add(new Point2d(x2, y2));
return;
}
if(da2 > cuspLimit) {
points.add(new Point2D(x3, y3));
points.add(new Point2d(x3, y3));
return;
}
}
Expand All @@ -107,7 +108,7 @@ private void recursive(double x1,double y1,double x2,double y2,double x3,double
// p1,p3,p4 are co-linear, p2 is considerable
if(d2 * d2 <= distanceTolerance * (dx*dx + dy*dy)) {
if(angleTolerance < curveAngleToleranceEpsilon) {
points.add(new Point2D(x1234, y1234));
points.add(new Point2d(x1234, y1234));
return;
}

Expand All @@ -116,14 +117,14 @@ private void recursive(double x1,double y1,double x2,double y2,double x3,double
if(da1 >= Math.PI) da1 = 2.0*Math.PI - da1;

if(da1 < angleTolerance) {
points.add(new Point2D(x2, y2));
points.add(new Point2D(x3, y3));
points.add(new Point2d(x2, y2));
points.add(new Point2d(x3, y3));
return;
}

if(cuspLimit != 0.0) {
if(da1 > cuspLimit) {
points.add(new Point2D(x2, y2));
points.add(new Point2d(x2, y2));
return;
}
}
Expand All @@ -132,7 +133,7 @@ private void recursive(double x1,double y1,double x2,double y2,double x3,double
// p1,p2,p4 are co-linear, p3 is considerable
if(d3 * d3 <= distanceTolerance * (dx*dx + dy*dy)) {
if(angleTolerance < curveAngleToleranceEpsilon) {
points.add(new Point2D(x1234, y1234));
points.add(new Point2d(x1234, y1234));
return;
}

Expand All @@ -141,14 +142,14 @@ private void recursive(double x1,double y1,double x2,double y2,double x3,double
if(da1 >= Math.PI) da1 = 2.0*Math.PI - da1;

if(da1 < angleTolerance) {
points.add(new Point2D(x2, y2));
points.add(new Point2D(x3, y3));
points.add(new Point2d(x2, y2));
points.add(new Point2d(x3, y3));
return;
}

if(cuspLimit != 0.0) {
if(da1 > cuspLimit) {
points.add(new Point2D(x3, y3));
points.add(new Point2d(x3, y3));
return;
}
}
Expand All @@ -158,7 +159,7 @@ private void recursive(double x1,double y1,double x2,double y2,double x3,double
dx = x1234 - (x1 + x4) / 2.0;
dy = y1234 - (y1 + y4) / 2.0;
if(dx*dx + dy*dy <= distanceTolerance) {
points.add(new Point2D(x1234, y1234));
points.add(new Point2d(x1234, y1234));
return;
}
}
Expand All @@ -170,9 +171,9 @@ private void recursive(double x1,double y1,double x2,double y2,double x3,double
recursive(x1234, y1234, x234, y234, x34, y34, x4, y4, points, distanceTolerance, level + 1);
}

protected ArrayList<Point2D> generateCurvePointsOld() {
ArrayList<Point2D> list = new ArrayList<Point2D>();
list.add(new Point2D(x0,y0));
protected ArrayList<Point2d> generateCurvePointsOld() {
ArrayList<Point2d> list = new ArrayList<Point2d>();
list.add(new Point2d(x0,y0));

double steps=25;
for(double k=1;k<steps;k++) {
Expand All @@ -199,9 +200,9 @@ protected ArrayList<Point2D> generateCurvePointsOld() {
double yabc = getYAt(j);
//*/

list.add(new Point2D(xabc,yabc));
list.add(new Point2d(xabc,yabc));
}
list.add(new Point2D(x3,y3));
list.add(new Point2d(x3,y3));

return list;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.marginallyclever.convenience;

import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;

public class LineInterpolator {
static public final double SMALL_VALUE = 1e-5;

protected Point2D start = new Point2D();
protected Point2D end = new Point2D();
protected Point2d start = new Point2d();
protected Point2d end = new Point2d();

public LineInterpolator() {}

public LineInterpolator(Point2D start,Point2D end) {
public LineInterpolator(Point2d start,Point2d end) {
this.start.set(start);
this.end.set(end);
}
Expand All @@ -18,7 +21,7 @@ public LineInterpolator(Point2D start,Point2D end) {
* @param t [0...1]
* @param p will be to set to (b-a)*t+a
*/
public void getPoint(double t,Point2D p) {
public void getPoint(double t,Point2d p) {
p.x = (end.x - start.x) * t + start.x;
p.y = (end.y - start.y) * t + start.y;
}
Expand All @@ -27,15 +30,15 @@ public void getPoint(double t,Point2D p) {
* @param t [0...1]
* @param v set to the approximate tangent to the line at at t
*/
public void getTangent(double t,Point2D v) {
public void getTangent(double t, Vector2d v) {
if(t<0) t=0;
if(t>1-SMALL_VALUE) t=1-SMALL_VALUE;

double t0 = t;
double t1 = t + SMALL_VALUE;

Point2D c0 = new Point2D();
Point2D c1 = new Point2D();
Point2d c0 = new Point2d();
Point2d c1 = new Point2d();
getPoint(t0,c0);
getPoint(t1,c1);
v.x = c1.x-c0.x;
Expand All @@ -52,26 +55,26 @@ public void getTangent(double t,Point2D v) {
* @param t [0...1]
* @param n set to the normal to the approximate tangent to the line at at t
*/
public void getNormal(double t,Point2D n) {
public void getNormal(double t,Vector2d n) {
getTangent(t,n);
double z = n.y;
n.y = -n.x;
n.x = z;
}

public Point2D getStart() {
public Point2d getStart() {
return start;
}

public void setStart(Point2D start) {
public void setStart(Point2d start) {
this.start = start;
}

public Point2D getEnd() {
public Point2d getEnd() {
return end;
}

public void setEnd(Point2D end) {
public void setEnd(Point2d end) {
this.end = end;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.marginallyclever.convenience;


import javax.vecmath.Point2d;

/**
* Given a line segment as the X axis and an amplitude, this class will generate a sin curve along the line.
*/
Expand All @@ -12,15 +14,15 @@ public LineInterpolatorSinCurve() {
super();
}

public LineInterpolatorSinCurve(Point2D a,Point2D b) {
public LineInterpolatorSinCurve(Point2d a, Point2d b) {
super(a,b);
}

@Override
public void getPoint(double t, Point2D c) {
public void getPoint(double t, Point2d c) {
// line b-a (bitTan) is the tangent of the overall curve, and bigNorm is orthogonal to bigTan.
Point2D bigTan = new Point2D();
Point2D bigNorm = new Point2D();
Point2d bigTan = new Point2d();
Point2d bigNorm = new Point2d();

bigTan.x = end.x- start.x;
bigTan.y = end.y- start.y;
Expand Down
72 changes: 0 additions & 72 deletions src/main/java/com/marginallyclever/convenience/Point2D.java

This file was deleted.

17 changes: 9 additions & 8 deletions src/main/java/com/marginallyclever/convenience/QuadGraph.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.marginallyclever.convenience;

import javax.vecmath.Point2d;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -10,7 +11,7 @@
public class QuadGraph {
private static final int MAX_POINTS = 5;
public final Rectangle2D bounds = new Rectangle2D.Double();
public final List<Point2D> sites = new ArrayList<>();
public final List<Point2d> sites = new ArrayList<>();
public QuadGraph[] children = null;

/**
Expand Down Expand Up @@ -42,13 +43,13 @@ public void split() {

private void moveSitesIntoChildren() {
// put all sites into the new children
for(Point2D c : sites) {
for(Point2d c : sites) {
addCellToOneQuadrant(c);
}
sites.clear();
}

public boolean insert(Point2D e) {
public boolean insert(Point2d e) {
if(bounds.contains(e.x,e.y)) {
if(sites.size()<MAX_POINTS && children == null) {
sites.add(e);
Expand All @@ -61,23 +62,23 @@ public boolean insert(Point2D e) {
return false;
}

private boolean addCellToOneQuadrant(Point2D e) {
private boolean addCellToOneQuadrant(Point2d e) {
for(int i=0;i<4;++i) {
if(children[i].insert(e)) return true;
}
return false;
}

// locate the cell under point x,y
public Point2D search(Point2D p) {
public Point2d search(Point2d p) {
if(!bounds.contains(p.x,p.y)) return null;

Point2D bestFound = null;
Point2d bestFound = null;
double bestD = Double.MAX_VALUE;

if (!sites.isEmpty()) {
// search me
for(Point2D c : sites) {
for(Point2d c : sites) {
double d = p.distanceSquared(c);
if(bestD > d) {
bestD = d;
Expand All @@ -89,7 +90,7 @@ public Point2D search(Point2D p) {
if(children != null) {
for (int i = 0; i < 4; ++i) {
// look into the children
Point2D bestChildFound = children[i].search(p);
Point2d bestChildFound = children[i].search(p);
if (bestChildFound != null && bestFound == null) {
double d = p.distanceSquared(bestChildFound);
if(bestD > d) {
Expand Down
Loading

0 comments on commit 7bad48a

Please sign in to comment.