diff --git a/src/line-geometry.cpp b/src/line-geometry.cpp
index d36b1b63d3e507081c293d7b8d39ec65676ab29d..d050ec458c266605d4142a65e09e4593080508f5 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 {
NR::Maybe<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());
+ NR::Maybe<NR::Point> no_point = NR::Nothing();
+ if (fabs(denom) < 1e-6)
+ return no_point;
NR::Coord lambda = (line.d0 - NR::dot(pt, line.normal)) / denom;
return pt + lambda * v_dir;
return *result;
}
+double Line::lambda (NR::Point const pt)
+{
+ 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} */
+std::pair<double, double> coordinates (NR::Point const &v1, NR::Point const &v2, NR::Point const &w)
+{
+ double det = determinant (v1, v2);;
+ if (fabs (det) < epsilon) {
+ // 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;
+ double lambda2 = determinant (v1, w) / det;
+ return std::make_pair (lambda1, lambda2);
+}
+
+/* whether w lies inside the sector spanned by v1 and 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);
+}
+
+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 (v) - NR::atan2 (w));
+}
+
+/*
+ * Returns the two corners of the quadrangle A, B, C, D spanning the edge that is hit by a semiline
+ * starting at pt and going into direction dir.
+ * If none of the sides is hit, it returns a pair containing two identical points.
+ */
+std::pair<NR::Point, NR::Point>
+side_of_intersection (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D,
+ NR::Point const &pt, NR::Point const &dir)
+{
+ NR::Point dir_A (A - pt);
+ NR::Point dir_B (B - pt);
+ NR::Point dir_C (C - pt);
+ NR::Point dir_D (D - pt);
+
+ std::pair<NR::Point, NR::Point> result;
+ double angle = -1;
+ double tmp_angle;
+
+ if (lies_in_sector (dir_A, dir_B, dir)) {
+ result = std::make_pair (A, B);
+ angle = pos_angle (dir_A, dir_B);
+ }
+ if (lies_in_sector (dir_B, dir_C, dir)) {
+ tmp_angle = pos_angle (dir_B, dir_C);
+ if (tmp_angle > angle) {
+ angle = tmp_angle;
+ result = std::make_pair (B, C);
+ }
+ }
+ if (lies_in_sector (dir_C, dir_D, dir)) {
+ tmp_angle = pos_angle (dir_C, dir_D);
+ if (tmp_angle > angle) {
+ angle = tmp_angle;
+ result = std::make_pair (C, D);
+ }
+ }
+ if (lies_in_sector (dir_D, dir_A, dir)) {
+ tmp_angle = pos_angle (dir_D, dir_A);
+ if (tmp_angle > angle) {
+ angle = tmp_angle;
+ result = std::make_pair (D, A);
+ }
+ }
+ if (angle == -1) {
+ // no intersection found; return a pair containing two identical points
+ return std::make_pair (A, A);
+ } else {
+ return result;
+ }
+}
+
+NR::Maybe<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 NR::Nothing();
+ }
+
+ 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();