<commit_before>// $Id$
// 
// Ported from JTS junit/algorithm/RobustLineIntersectionTest.java rev. 1.3

#include <tut.hpp>
// geos
#include <geos/io/WKTReader.h>
#include <geos/algorithm/LineIntersector.h>
#include <geos/geom/PrecisionModel.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Geometry.h> // required for use in auto_ptr
#include <geos/geom/LineString.h> 
#include <geos/geom/Coordinate.h>
// std
#include <sstream>
#include <string>
#include <memory>

namespace geos {
	namespace geom {
		class Geometry;
	}
}

using namespace geos::geom; // 

namespace tut
{
	//
	// Test Group
	//

	struct test_lineintersector_data {};

	typedef test_group<test_lineintersector_data> group;
	typedef group::object object;

	group test_lineintersector_group(
		"geos::algorithm::RobustLineIntersection");

	// These are static to avoid namespace pollution
	// The struct test_*_data above is probably there
	// for the same reason...
	//
	static PrecisionModel pm;
	static GeometryFactory gf(&pm);
        static geos::io::WKTReader reader(&gf);

	typedef std::auto_ptr<Geometry> GeomPtr;
	typedef std::auto_ptr<Geometry> GeomPtr;

	bool equals(const Coordinate& p0, const Coordinate& p1,
	            double distanceTolerance)
        {
                return p0.distance(p1) <= distanceTolerance;
        }

	void testIntPoints(const Coordinate& p, const Coordinate& q,
	                   double distanceTolerance)
        {
                bool isEqual = equals(p, q, distanceTolerance);
		ensure("testIntPoints", isEqual);
        }

	/**
	 *
	 * @param pt array of 4 Coordinates
	 * @param expectedIntersectionNum
	 * @param intPt the expected intersection points
	 *              (maybe null if not tested)
	 *              must contain at least expectedIntersectionNum
	 *              elements
	 */
	void computeIntersection(const std::vector<Coordinate>& pt,
	                         int expectedIntersectionNum,
	                         const std::vector<Coordinate>& intPt,
	                         double distanceTolerance)
	{
		geos::algorithm::LineIntersector li;
		li.computeIntersection(pt[0], pt[1], pt[2], pt[3]);

		int intNum = li.getIntersectionNum();
		ensure_equals(intNum, expectedIntersectionNum);

		if ( intPt.empty() ) return;

		ensure_equals(intPt.size(), intNum);

		// test that both points are represented here
		//bool isIntPointsCorrect = true;
		if (intNum == 1)
		{
			testIntPoints(intPt[0], li.getIntersection(0),
			              distanceTolerance);
		}
		else if (intNum == 2)
		{
			testIntPoints(intPt[0], li.getIntersection(0),
			                        distanceTolerance);
			testIntPoints(intPt[1], li.getIntersection(0),
			                        distanceTolerance);

			if ( !(
		equals(intPt[0],li.getIntersection(0), distanceTolerance)
			        ||
		equals(intPt[0],li.getIntersection(1), distanceTolerance) ) )
			{
				testIntPoints(intPt[0], li.getIntersection(0),
				              distanceTolerance);
				testIntPoints(intPt[0], li.getIntersection(1),
				              distanceTolerance);
			}

			else if ( !(
		equals(intPt[1],li.getIntersection(0), distanceTolerance)
				||
		equals(intPt[1],li.getIntersection(1), distanceTolerance) ) )
			{
				testIntPoints(intPt[1], li.getIntersection(0),
				              distanceTolerance);
				testIntPoints(intPt[1], li.getIntersection(1),
				              distanceTolerance);
			}
		}
		//assertTrue("Int Pts not equal", isIntPointsCorrect);
	}

	void computeIntersection(const std::string& wkt1,
	                         const std::string& wkt2,
	                         int expectedIntersectionNum,
	                         const std::string& expectedWKT,
	                         double distanceTolerance)
                //throws ParseException
        {
		GeomPtr g1(reader.read(wkt1));
		GeomPtr g2(reader.read(wkt2));

		LineString* l1ptr = dynamic_cast<LineString*>(g1.get());
		LineString* l2ptr = dynamic_cast<LineString*>(g2.get());

		ensure(l1ptr);
		ensure(l2ptr);

		LineString& l1 = *l1ptr;
		LineString& l2 = *l2ptr;

	        std::vector<Coordinate> pt;
		pt.push_back(l1.getCoordinateN(0));
		pt.push_back(l1.getCoordinateN(1));
		pt.push_back(l2.getCoordinateN(0));
		pt.push_back(l2.getCoordinateN(1));

                GeomPtr g(reader.read(expectedWKT));

		std::auto_ptr<CoordinateSequence> cs ( g->getCoordinates() );

	        std::vector<Coordinate> intPt;
		for (size_t i=0; i<cs->size(); ++i)
			intPt.push_back(cs->getAt(i));

                computeIntersection(pt, expectedIntersectionNum,
		                    intPt, distanceTolerance);
        }

	void computeIntersection(const std::string& wkt1,
	                         const std::string& wkt2,
	                         int expectedIntersectionNum,
	                         const std::vector<Coordinate>& intPt,
	                         double distanceTolerance)
                // throws ParseException
        {
		GeomPtr g1(reader.read(wkt1));
		GeomPtr g2(reader.read(wkt2));

		LineString* l1ptr = dynamic_cast<LineString*>(g1.get());
		LineString* l2ptr = dynamic_cast<LineString*>(g2.get());

		ensure(l1ptr);
		ensure(l2ptr);

		LineString& l1 = *l1ptr;
		LineString& l2 = *l2ptr;

	        std::vector<Coordinate> pt;
		pt.push_back(l1.getCoordinateN(0));
		pt.push_back(l1.getCoordinateN(1));
		pt.push_back(l2.getCoordinateN(0));
		pt.push_back(l2.getCoordinateN(1));

		computeIntersection(pt, expectedIntersectionNum,
		                    intPt, distanceTolerance);
        }




	//
	// Test Cases
	//

	// 1 - Test from strk which is bad in GEOS (2009-04-14).<commit_msg>better isolation of test-specific functions<commit_after>// $Id$
// 
// Ported from JTS junit/algorithm/RobustLineIntersectionTest.java rev. 1.3

#include <tut.hpp>
// geos
#include <geos/io/WKTReader.h>
#include <geos/algorithm/LineIntersector.h>
#include <geos/geom/PrecisionModel.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Geometry.h> // required for use in auto_ptr
#include <geos/geom/LineString.h> 
#include <geos/geom/Coordinate.h>
// std
#include <sstream>
#include <string>
#include <memory>

namespace geos {
	namespace geom {
		class Geometry;
	}
}

using namespace geos::geom; // 

namespace tut
{
	//
	// Test Group
	//

	struct test_robustlineintersection_data
	{
	typedef std::auto_ptr<Geometry> GeomPtr;

	bool equals(const Coordinate& p0, const Coordinate& p1,
	            double distanceTolerance)
        {
                return p0.distance(p1) <= distanceTolerance;
        }

	void testIntPoints(const Coordinate& p, const Coordinate& q,
	                   double distanceTolerance)
        {
                bool isEqual = equals(p, q, distanceTolerance);
		ensure("testIntPoints", isEqual);
        }

	/**
	 *
	 * @param pt array of 4 Coordinates
	 * @param expectedIntersectionNum
	 * @param intPt the expected intersection points
	 *              (maybe null if not tested)
	 *              must contain at least expectedIntersectionNum
	 *              elements
	 */
	void computeIntersection(const std::vector<Coordinate>& pt,
	                         int expectedIntersectionNum,
	                         const std::vector<Coordinate>& intPt,
	                         double distanceTolerance)
	{
		geos::algorithm::LineIntersector li;
		li.computeIntersection(pt[0], pt[1], pt[2], pt[3]);

		int intNum = li.getIntersectionNum();
		ensure_equals(intNum, expectedIntersectionNum);

		if ( intPt.empty() ) return;

		ensure_equals(intPt.size(), intNum);

		// test that both points are represented here
		//bool isIntPointsCorrect = true;
		if (intNum == 1)
		{
			testIntPoints(intPt[0], li.getIntersection(0),
			              distanceTolerance);
		}
		else if (intNum == 2)
		{
			testIntPoints(intPt[0], li.getIntersection(0),
			                        distanceTolerance);
			testIntPoints(intPt[1], li.getIntersection(0),
			                        distanceTolerance);

			if ( !(
		equals(intPt[0],li.getIntersection(0), distanceTolerance)
			        ||
		equals(intPt[0],li.getIntersection(1), distanceTolerance) ) )
			{
				testIntPoints(intPt[0], li.getIntersection(0),
				              distanceTolerance);
				testIntPoints(intPt[0], li.getIntersection(1),
				              distanceTolerance);
			}

			else if ( !(
		equals(intPt[1],li.getIntersection(0), distanceTolerance)
				||
		equals(intPt[1],li.getIntersection(1), distanceTolerance) ) )
			{
				testIntPoints(intPt[1], li.getIntersection(0),
				              distanceTolerance);
				testIntPoints(intPt[1], li.getIntersection(1),
				              distanceTolerance);
			}
		}
		//assertTrue("Int Pts not equal", isIntPointsCorrect);
	}

	void computeIntersection(const std::string& wkt1,
	                         const std::string& wkt2,
	                         int expectedIntersectionNum,
	                         const std::string& expectedWKT,
	                         double distanceTolerance)
                //throws ParseException
        {
		GeomPtr g1(reader.read(wkt1));
		GeomPtr g2(reader.read(wkt2));

		LineString* l1ptr = dynamic_cast<LineString*>(g1.get());
		LineString* l2ptr = dynamic_cast<LineString*>(g2.get());

		ensure(l1ptr);
		ensure(l2ptr);

		LineString& l1 = *l1ptr;
		LineString& l2 = *l2ptr;

	        std::vector<Coordinate> pt;
		pt.push_back(l1.getCoordinateN(0));
		pt.push_back(l1.getCoordinateN(1));
		pt.push_back(l2.getCoordinateN(0));
		pt.push_back(l2.getCoordinateN(1));

                GeomPtr g(reader.read(expectedWKT));

		std::auto_ptr<CoordinateSequence> cs ( g->getCoordinates() );

	        std::vector<Coordinate> intPt;
		for (size_t i=0; i<cs->size(); ++i)
			intPt.push_back(cs->getAt(i));

                computeIntersection(pt, expectedIntersectionNum,
		                    intPt, distanceTolerance);
        }

	void computeIntersection(const std::string& wkt1,
	                         const std::string& wkt2,
	                         int expectedIntersectionNum,
	                         const std::vector<Coordinate>& intPt,
	                         double distanceTolerance)
                // throws ParseException
        {
		GeomPtr g1(reader.read(wkt1));
		GeomPtr g2(reader.read(wkt2));

		LineString* l1ptr = dynamic_cast<LineString*>(g1.get());
		LineString* l2ptr = dynamic_cast<LineString*>(g2.get());

		ensure(l1ptr);
		ensure(l2ptr);

		LineString& l1 = *l1ptr;
		LineString& l2 = *l2ptr;

	        std::vector<Coordinate> pt;
		pt.push_back(l1.getCoordinateN(0));
		pt.push_back(l1.getCoordinateN(1));
		pt.push_back(l2.getCoordinateN(0));
		pt.push_back(l2.getCoordinateN(1));

		computeIntersection(pt, expectedIntersectionNum,
		                    intPt, distanceTolerance);
        }

	test_robustlineintersection_data()
		:
		pm(),
		gf(&pm),
		reader(&gf)
	{
	}

	PrecisionModel pm;
	GeometryFactory gf;
        geos::io::WKTReader reader;

	};

	typedef test_group<test_robustlineintersection_data> group;
	typedef group::object object;

	group test_robustlineintersection_group(
		"geos::algorithm::RobustLineIntersection");


	//
	// Test Cases
	//

	// 1 - Test from strk which is bad in GEOS (2009-04-14).
{'fraction_non_alphanumeric': 0.12088306560172306, 'fraction_numerical': 0.016692093691106524, 'mean_word_length': 2.6561679790026247, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5320034', 'n_tokens_mistral': 4019, 'n_tokens_neox': 3518, 'n_words': 665}