-
Notifications
You must be signed in to change notification settings - Fork 3
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
lineQuadratic() not finding one specific intersection #3
Comments
The order of the vertices seems to affect the result, when it probably shouldn't. fun doTest() {
val l0 = Vec2(512.0, 96.0)
val l1 = Vec2(128.0, 384.0)
val c0 = Vec2(128.0, 384.0)
val c1 = Vec2(320.0, 168.0)
val c2 = Vec2(512.0, 384.0)
// fails to find intersection
println(Intersections.lineQuadratic(
Line2.line(l0, l1),
Bezier2.curve(c0, c1, c2) as Bezier2.QuadraticBezier2?
).size)
// reversed line works
println(Intersections.lineQuadratic(
Line2.line(l1, l0),
Bezier2.curve(c0, c1, c2) as Bezier2.QuadraticBezier2?
).size)
// reversed curve works
println(Intersections.lineQuadratic(
Line2.line(l0, l1),
Bezier2.curve(c2, c1, c0) as Bezier2.QuadraticBezier2?
).size)
// both reversed works
println(Intersections.lineQuadratic(
Line2.line(l1, l0),
Bezier2.curve(c2, c1, c0) as Bezier2.QuadraticBezier2?
).size)
} Output:
|
Adding a small offset to one of the vertices also makes it returns the expected intersection. |
Here a java program demonstrating the issue which can be dropped into package io.lacuna.artifex;
import io.lacuna.artifex.utils.Intersections;
public class issue3 {
public static void main(String[] args) {
Vec2 l0 = new Vec2(512.0, 96.0);
Vec2 l1 = new Vec2(128.0, 384.0);
Vec2 c0 = new Vec2(128.0, 384.0);
Vec2 c1 = new Vec2(320.0, 168.0);
Vec2 c2 = new Vec2(512.0, 384.0);
calculate(l0, l1, c0, c1, c2);
calculate(l1, l0, c0, c1, c2); // reverse line
calculate(l0, l1, c2, c1, c0); // reverse curve
calculate(l1, l0, c2, c1, c0); // reverse both
}
private static void calculate(Vec2 l0, Vec2 l1, Vec2 c0, Vec2 c1, Vec2 c2) {
Line2 l = Line2.line(l0, l1);
Bezier2.QuadraticBezier2 c =
(Bezier2.QuadraticBezier2) Bezier2.curve(c0, c1, c2);
System.out.println("\n# Intersections between");
System.out.println("line: " + l);
System.out.println("curve: " + c);
Vec2[] intersections = Intersections.lineQuadratic(l, c);
System.out.println(intersections.length + " intersections");
for (Vec2 intersection : intersections) {
System.out.println(intersection);
}
}
} and the output of the program
|
I keep digging :-) Something goes wrong in And the reason seems to be that among
Next I'll try comparing the failing case with the one that works in which one vertex is slightly displaced. Update: the reason it works replacing (128.0, 384.0) with (128.01, 384.03) is because |
I don't know if this is a good fix, but it solves the issue for me: diff --git a/src/io/lacuna/artifex/utils/Scalars.java b/src/io/lacuna/artifex/utils/Scalars.java
index 6341851..5aa7221 100644
--- a/src/io/lacuna/artifex/utils/Scalars.java
+++ b/src/io/lacuna/artifex/utils/Scalars.java
@@ -59,7 +59,11 @@ public class Scalars {
}
public static double normalizationFactor(double a, double b, double c) {
- double exponent = getExponent(max(max(a, b), c));
+ double maxValue = max(a, b, c);
+ if(maxValue == 0.0) {
+ maxValue = min(a, b, c);
+ }
+ double exponent = getExponent(maxValue);
return (exponent < -8 || exponent > 8) ? Math.pow(2, -exponent) : 1;
}
@@ -75,4 +79,12 @@ public class Scalars {
public static double max(double a, double b, double c) {
return max(a, max(b, c));
}
+
+ public static double min(double a, double b) {
+ return a > b ? b : a;
+ }
+
+ public static double min(double a, double b, double c) {
+ return min(a, min(b, c));
+ }
} |
Is ... Maybe not, as that might change the behavior by ruling out negative exponents... |
Hi,
I'm working with intersections in OPENRNDR and I have the following design made out of 4 segments (two lines and two curves):
There should be 5 intersections detected but only 4 are found. The original program simplified to only use the two involved segments looks like this:
This is the output of the program:
Zero intersections are found.
This is a visualization of those vertices using Inkscape. Even if the blue line should be a curve, I think it shows that there should be an intersection:
Any ideas why this happens?
The text was updated successfully, but these errors were encountered: