Code

NR::Maybe => boost::optional
[inkscape.git] / src / line-geometry.cpp
index 7b6ba04490498992ca02c7b2199031c093c27f9d..056bfb71a964db081c7bfac0b2c409f9dbea4da5 100644 (file)
 
 #include "line-geometry.h"
 #include "inkscape.h"
+#include "desktop.h"
 #include "desktop-style.h"
 #include "desktop-handles.h"
 #include "display/sp-canvas.h"
 #include "display/sodipodi-ctrl.h"
-//#include "display/curve.cpp"
 
 namespace Box3D {
 
@@ -52,9 +52,9 @@ Line &Line::operator=(Line const &line) {
     return *this;
 }
 
-NR::Maybe<NR::Point> Line::intersect(Line const &line) {
+boost::optional<NR::Point> Line::intersect(Line const &line) {
     NR::Coord denom = NR::dot(v_dir, line.normal);
-    NR::Maybe<NR::Point> no_point = NR::Nothing();
+    boost::optional<NR::Point> no_point;
     if (fabs(denom) < 1e-6)
         return no_point;
 
@@ -72,7 +72,7 @@ void Line::set_direction(NR::Point const &dir)
 NR::Point Line::closest_to(NR::Point const &pt)
 {
        /* return the intersection of this line with a perpendicular line passing through pt */ 
-    NR::Maybe<NR::Point> result = this->intersect(Line(pt, (this->v_dir).ccw(), false));
+    boost::optional<NR::Point> result = this->intersect(Line(pt, (this->v_dir).ccw(), false));
     g_return_val_if_fail (result, NR::Point (0.0, 0.0));
     return *result;
 }
@@ -91,11 +91,6 @@ double Line::lambda (NR::Point const pt)
     return lambda;
 }
 
-inline static double determinant (NR::Point const &a, NR::Point const &b)
-{
-    return (a[NR::X] * b[NR::Y] - a[NR::Y] * b[NR::X]);
-}
-
 /* The coordinates of w with respect to the basis {v1, v2} */
 std::pair<double, double> coordinates (NR::Point const &v1, NR::Point const &v2, NR::Point const &w)
 {
@@ -183,57 +178,21 @@ side_of_intersection (NR::Point const &A, NR::Point const &B, NR::Point const &C
     }
 }
 
-double cross_ratio (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D)
-{
-    Line line (A, D);
-    double lambda_A = line.lambda (A);
-    double lambda_B = line.lambda (B);
-    double lambda_C = line.lambda (C);
-    double lambda_D = line.lambda (D);
-
-    if (fabs (lambda_D - lambda_A) < epsilon || fabs (lambda_C - lambda_B) < epsilon) {
-        // We return NR_HUGE so that we can catch this case in the calling functions
-        return NR_HUGE;
-    }
-    return (((lambda_C - lambda_A) / (lambda_D - lambda_A)) * ((lambda_D - lambda_B) / (lambda_C - lambda_B)));
-}
-
-double cross_ratio (VanishingPoint const &V, NR::Point const &B, NR::Point const &C, NR::Point const &D)
+boost::optional<NR::Point> Line::intersection_with_viewbox (SPDesktop *desktop)
 {
-    if (V.is_finite()) {
-        return cross_ratio (V.get_pos(), B, C, D);
-    } else {
-        if (B == D) {
-            // catch this case so that the line BD below is non-degenerate
-            return 0;
-        }
-        Line line (B, D);
-        double lambda_B = line.lambda (B);
-        double lambda_C = line.lambda (C);
-        double lambda_D = line.lambda (D);
-
-        if (fabs (lambda_C - lambda_B) < epsilon) {
-            // We return NR_HUGE so that we can catch this case in the calling functions
-            return NR_HUGE;
-        }
-        return (lambda_D - lambda_B) / (lambda_C - lambda_B);
+    NR::Rect vb = desktop->get_display_area();
+    /* remaining viewbox corners */
+    NR::Point ul (vb.min()[NR::X], vb.max()[NR::Y]);
+    NR::Point lr (vb.max()[NR::X], vb.min()[NR::Y]);
+
+    std::pair <NR::Point, NR::Point> e = side_of_intersection (vb.min(), lr, vb.max(), ul, this->pt, this->v_dir);
+    if (e.first == e.second) {
+        // perspective line lies outside the canvas
+        return boost::optional<NR::Point>();
     }
-}
-
-NR::Point fourth_pt_with_given_cross_ratio (NR::Point const &A, NR::Point const &C, NR::Point const &D, double gamma)
-{
-    Line line (A, D);
-    double lambda_A = line.lambda (A);
-    double lambda_C = line.lambda (C);
-    double lambda_D = line.lambda (D);
 
-    double beta = (lambda_C - lambda_A) / (lambda_D - lambda_A);
-    if (fabs (beta - gamma) < epsilon) {
-        // FIXME: How to handle the case when the point can't be computed?
-        // g_warning ("Cannot compute point with given cross ratio.\n");
-        return NR::Point (0.0, 0.0);
-    }
-    return line.point_from_lambda ((beta * lambda_D - gamma * lambda_C) / (beta - gamma));
+    Line line (e.first, e.second);
+    return this->intersect (line);
 }
 
 void create_canvas_point(NR::Point const &pos, double size, guint32 rgba)