diff --git a/src/line-geometry.cpp b/src/line-geometry.cpp
index 68741d8a707c5ab5694017c34606f266081c92b8..056bfb71a964db081c7bfac0b2c409f9dbea4da5 100644 (file)
--- a/src/line-geometry.cpp
+++ b/src/line-geometry.cpp
#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 {
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);
- g_return_val_if_fail(fabs(denom) > 1e-6, NR::Nothing());
+ boost::optional<NR::Point> no_point;
+ if (fabs(denom) < 1e-6)
+ return no_point;
NR::Coord lambda = (line.d0 - NR::dot(pt, line.normal)) / denom;
return pt + lambda * v_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;
}
-inline static double determinant (NR::Point const &a, NR::Point const &b)
+double Line::lambda (NR::Point const pt)
{
- return (a[NR::X] * b[NR::Y] - a[NR::Y] * b[NR::X]);
+ double sign = (NR::dot (pt - this->pt, this->v_dir) > 0) ? 1.0 : -1.0;
+ double lambda = sign * NR::L2 (pt - this->pt);
+ // FIXME: It may speed things up (but how much?) if we assume that
+ // pt lies on the line and thus skip the following test
+ NR::Point test = point_from_lambda (lambda);
+ if (!pts_coincide (pt, test)) {
+ g_warning ("Point does not lie on line.\n");
+ return 0;
+ }
+ return lambda;
}
/* The coordinates of w with respect to the basis {v1, v2} */
{
double det = determinant (v1, v2);;
if (fabs (det) < epsilon) {
- g_warning ("Vectors do not form a basis.\n");
- return std::make_pair (0.0, 0.0);
+ // vectors are not linearly independent; we indicate this in the return value(s)
+ return std::make_pair (HUGE_VAL, HUGE_VAL);
}
double lambda1 = determinant (w, v2) / det;
@@ -98,12 +109,22 @@ std::pair<double, double> coordinates (NR::Point const &v1, NR::Point const &v2,
bool lies_in_sector (NR::Point const &v1, NR::Point const &v2, NR::Point const &w)
{
std::pair<double, double> coords = coordinates (v1, v2, w);
+ if (coords.first == HUGE_VAL) {
+ // catch the case that the vectors are not linearly independent
+ // FIXME: Can we assume that it's safe to return true if the vectors point in different directions?
+ return (NR::dot (v1, v2) < 0);
+ }
return (coords.first >= 0 and coords.second >= 0);
}
-static double pos_angle (NR::Point A, NR::Point B)
+bool lies_in_quadrangle (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D, NR::Point const &pt)
+{
+ return (lies_in_sector (D - A, B - A, pt - A) && lies_in_sector (D - C, B - C, pt - C));
+}
+
+static double pos_angle (NR::Point v, NR::Point w)
{
- return fabs (NR::atan2 (A) - NR::atan2 (B));
+ return fabs (NR::atan2 (v) - NR::atan2 (w));
}
/*
@@ -157,6 +178,23 @@ side_of_intersection (NR::Point const &A, NR::Point const &B, NR::Point const &C
}
}
+boost::optional<NR::Point> Line::intersection_with_viewbox (SPDesktop *desktop)
+{
+ 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>();
+ }
+
+ Line line (e.first, e.second);
+ return this->intersect (line);
+}
+
void create_canvas_point(NR::Point const &pos, double size, guint32 rgba)
{
SPDesktop *desktop = inkscape_active_desktop();