void runTest()
  {
    BufferedLineSegmentIntersector uut;

    {
      LineSegment ls1(0, 0, 1, 1);
      LineSegment ls2(0, 0, 1, 1);
      LineSegment result;
      HOOT_STR_EQUALS(true, uut.intersect(ls1, 1, ls2, result));
      HOOT_STR_EQUALS("LINESEGMENT(0 0,1 1)",
                      result);
    }

    {
      LineSegment ls1(0, 1, 1, 0);
      LineSegment ls2(0, 0, 1, 1);
      LineSegment result;
      HOOT_STR_EQUALS(true, uut.intersect(ls1, .14142135623730950488016887242097, ls2, result));
      HOOT_STR_EQUALS("LINESEGMENT(0.4 0.4,0.6 0.6)",
                      result);
    }

    {
      LineSegment ls1(2, 0, 3, 2);
      LineSegment ls2(2, 0, 4, 4);
      LineSegment result;
      HOOT_STR_EQUALS(true, uut.intersect(ls1, .1, ls2, result));
      HOOT_STR_EQUALS("LINESEGMENT(2 0,3.04472 2.08944)", result);
    }
  }
示例#2
0
void MaximalSubline::ThresholdMatchCriteria::matchingSubline(LineSegment &a, LineSegment &b) const
{
    BufferedLineSegmentIntersector bi;

    LineSegment sublineA, sublineB;
    bi.intersect(a, _maxDistance, b, sublineB);
    bi.intersect(b, _maxDistance, a, sublineA);

    a = sublineA;
    b = sublineB;
}
  void runRandomTest()
  {
    BufferedLineSegmentIntersector uut;

    double scale = 5;

    double uutTime = 0.0;
    double geosTime = 0.0;
    // the geos intersection is an approximation so we need a fairly large epsilon
    double epsilon = 1e-2;
    size_t count = 1000;
    for (size_t i = 0; i < count; i++)
    {
      double r = Tgs::Random::instance()->generateUniform() * 3.0;
      LineSegment ls1(Tgs::Random::instance()->generateUniform() * scale,
                      Tgs::Random::instance()->generateUniform() * scale,
                      Tgs::Random::instance()->generateUniform() * scale,
                      Tgs::Random::instance()->generateUniform() * scale);
      LineSegment ls2(Tgs::Random::instance()->generateUniform() * scale,
                      Tgs::Random::instance()->generateUniform() * scale,
                      Tgs::Random::instance()->generateUniform() * scale,
                      Tgs::Random::instance()->generateUniform() * scale);

      LineSegment lsGeos;
      double start = Tgs::Time::getTime();
      bool resultGeos = geosIntersect(ls1, r, ls2, lsGeos);
      double end = Tgs::Time::getTime();
      geosTime += end - start;

      LineSegment lsUut;
      start = Tgs::Time::getTime();
      bool resultUut = uut.intersect(ls1, r, ls2, lsUut);
      end = Tgs::Time::getTime();
      uutTime += end - start;

      // if the line is very close to the buffer then we can't expect consistent results.
      bool touchy = fabs(ls1.distance(ls2) - r) <= epsilon;
      CPPUNIT_ASSERT_EQUAL(resultGeos || touchy, resultUut || touchy);
      if (resultGeos && !touchy)
      {
        CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, lsGeos.p0.distance(lsUut.p0), epsilon);
        CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, lsGeos.p1.distance(lsUut.p1), epsilon);
      }
    }

    LOG_INFO("GEOS Mean Time: " << (1000.0 * geosTime) / (double)count << "ms");
    LOG_INFO("UUT Mean Time: " << (1000.0 * uutTime) / (double)count << "ms");
    LOG_INFO("Ratio: " << geosTime / uutTime);
  }
  void runCircleTest()
  {
    {
      LineSegment ls(-2, 0.5, 2, 0.5);
      Coordinate origin(0, 0);

      Coordinate p1, p2;
      BufferedLineSegmentIntersector uut;
      uut.circleIntersection(origin, 1, ls, p1, p2);

      HOOT_STR_EQUALS("0.866025 0.5, -0.866025 0.5", p1 << ", " << p2);
    }

    {
      LineSegment ls(-1, 2.5, 3, 2.5);
      Coordinate origin(1, 2);

      Coordinate p1, p2;
      BufferedLineSegmentIntersector uut;
      uut.circleIntersection(origin, 1, ls, p1, p2);

      HOOT_STR_EQUALS("1.86603 2.5, 0.133975 2.5", p1 << ", " << p2);
    }

    // tangent at the top of the circle
    {
      LineSegment ls(-1, 3, 3, 3);
      Coordinate origin(1, 2);

      Coordinate p1, p2;
      BufferedLineSegmentIntersector uut;
      uut.circleIntersection(origin, 1, ls, p1, p2);

      HOOT_STR_EQUALS("1 3, nan nan", p1 << ", " << p2);
    }

    // arbitrary line that was causing problems. Intersections are outside the line segment.
    {
      LineSegment ls(0.648952, 2.00472, 0.783395, 4.02088);
      Coordinate origin(0.686158, 1.21443);

      Coordinate p1, p2;
      BufferedLineSegmentIntersector uut;
      uut.circleIntersection(origin, 0.424808, ls, p1, p2);

      HOOT_STR_EQUALS("nan nan, nan nan", p1 << ", " << p2);
    }
  }