<commit_before>//
// Implementation of methods for the basic data classes
//
// Copyright (c) 2001-2008 Virtual Terrain Project
// Free for all uses, see license.txt for details.
//

#include "MathTypes.h"
#include "vtLog.h"

int DPolyArray::s_previous_poly = -1;

/**
* Return the index of the polygon at a specified point, or -1 if
* there is no polygon there.
*
* For speed, it first test the polygon which was found last time.
* For spatially linear testing, this can be a 10x speedup.
*/
int DPolyArray::FindPoly(const DPoint2 &p) const
{
	if (s_previous_poly != -1)
	{
		if (at(s_previous_poly).ContainsPoint(p))
			return s_previous_poly;
	}
	int num = size();
	for (int i = 0; i < num; i++)
	{
		const DPolygon2 &poly = at(i);
#if 0
		// possible further speed test: first test against extents
		if (world_x < poly->xmin || world_x > poly->xmax ||
			world_z < poly->zmin || world_z > poly->zmax)
			continue;
#endif
		if (poly.ContainsPoint(p))
		{
			// found
			s_previous_poly = i;
			return i;
		}
	}
	// not found
	s_previous_poly = -1;
	return -1;
}


//
// DLine2 methods
//

void DLine2::Add(const DPoint2 &p)
{
	unsigned int i, size = GetSize();
	for (i=0; i < size; i++)
		GetAt(i) += p;
}

void DLine2::Mult(double factor)
{
	unsigned int i, size = GetSize();
	for (i=0; i < size; i++)
		GetAt(i) *= factor;
}

void  DLine2::InsertPointAfter(int iInsertAfter, const DPoint2 &Point)
{
	int iNumPoints = GetSize();
	int iIndex;
	if (iInsertAfter == iNumPoints - 1)
		Append(Point);
	else
	{
		for (iIndex = iNumPoints - 1; iIndex > iInsertAfter ; iIndex--)
			SetAt(iIndex + 1, GetAt(iIndex));
		SetAt(iInsertAfter + 1, Point);
	}
}

void DLine2::RemovePoint(int i)
{
	RemoveAt(i);
}

void DLine2::ReverseOrder()
{
	DPoint2 p;
	int i, size = GetSize();
	for (i = 0; i < size/2; i++)
	{
		p = GetAt(i);
		SetAt(i, GetAt(size-1-i));
		SetAt(size-1-i, p);
	}
}

/**
 * Given four points A B C D, where B and C are identical, then this
 *  line is a triangle not a rectangle.  Remove the identical points to
 *  produce A B D.  Points are identical if they are within dEpsilon
 *  of each other.
 */
int DLine2::RemoveDegeneratePoints(double dEpsilon)
{
	int removed = 0;
	for (int i = 0; i < (int) GetSize()-1; i++)
	{
		DPoint2 diff = GetAt(i+1) - GetAt(i);
		if (fabs(diff.x) < dEpsilon && fabs(diff.y) < dEpsilon)
		{
			RemoveAt(i);
			removed++;
		}
	}
	return removed;
}

bool DLine2::IsConvex() const
{
	int positive = 0;
	int negative = 0;
	unsigned int length = GetSize();<commit_msg>improved RemoveDegeneratePoints to catch more cases<commit_after>//
// Implementation of methods for the basic data classes
//
// Copyright (c) 2001-2008 Virtual Terrain Project
// Free for all uses, see license.txt for details.
//

#include "MathTypes.h"
#include "vtLog.h"

int DPolyArray::s_previous_poly = -1;

/**
* Return the index of the polygon at a specified point, or -1 if
* there is no polygon there.
*
* For speed, it first test the polygon which was found last time.
* For spatially linear testing, this can be a 10x speedup.
*/
int DPolyArray::FindPoly(const DPoint2 &p) const
{
	if (s_previous_poly != -1)
	{
		if (at(s_previous_poly).ContainsPoint(p))
			return s_previous_poly;
	}
	int num = size();
	for (int i = 0; i < num; i++)
	{
		const DPolygon2 &poly = at(i);
#if 0
		// possible further speed test: first test against extents
		if (world_x < poly->xmin || world_x > poly->xmax ||
			world_z < poly->zmin || world_z > poly->zmax)
			continue;
#endif
		if (poly.ContainsPoint(p))
		{
			// found
			s_previous_poly = i;
			return i;
		}
	}
	// not found
	s_previous_poly = -1;
	return -1;
}


//
// DLine2 methods
//

void DLine2::Add(const DPoint2 &p)
{
	unsigned int i, size = GetSize();
	for (i=0; i < size; i++)
		GetAt(i) += p;
}

void DLine2::Mult(double factor)
{
	unsigned int i, size = GetSize();
	for (i=0; i < size; i++)
		GetAt(i) *= factor;
}

void  DLine2::InsertPointAfter(int iInsertAfter, const DPoint2 &Point)
{
	int iNumPoints = GetSize();
	int iIndex;
	if (iInsertAfter == iNumPoints - 1)
		Append(Point);
	else
	{
		for (iIndex = iNumPoints - 1; iIndex > iInsertAfter ; iIndex--)
			SetAt(iIndex + 1, GetAt(iIndex));
		SetAt(iInsertAfter + 1, Point);
	}
}

void DLine2::RemovePoint(int i)
{
	RemoveAt(i);
}

void DLine2::ReverseOrder()
{
	DPoint2 p;
	int i, size = GetSize();
	for (i = 0; i < size/2; i++)
	{
		p = GetAt(i);
		SetAt(i, GetAt(size-1-i));
		SetAt(size-1-i, p);
	}
}

/**
 * Given four points A B C D, where B and C are identical, then this
 *  line is a triangle not a rectangle.  Remove the identical points to
 *  produce A B D.  Points are identical if they are within dEpsilon
 *  of each other.
 */
int DLine2::RemoveDegeneratePoints(double dEpsilon)
{
	int removed = 0;
	for (int i = 0; i < (int) GetSize(); i++)
	{
		DPoint2 diff = GetSafePoint(i+1) - GetAt(i);
		if (fabs(diff.x) < dEpsilon && fabs(diff.y) < dEpsilon)
		{
			RemoveAt(i);
			removed++;
			i--;
		}
	}
	return removed;
}

bool DLine2::IsConvex() const
{
	int positive = 0;
	int negative = 0;
	unsigned int length = GetSize();
{'fraction_non_alphanumeric': 0.14446417998815866, 'fraction_numerical': 0.018354055654233273, 'mean_word_length': 3.320545609548167, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, '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': '5807592', 'n_tokens_mistral': 2087, 'n_tokens_neox': 1956, 'n_words': 603}