Example #1
0
void COORD::UnitTest()
{
    assert(COORD(3, 3) + COORD(2, 2) == COORD(5, 5));
    COORD coord(5, 2);
    coord += COORD(2, 5);
    assert(coord == COORD(7, 7));
    assert(COORD(2, 2) + North == COORD(2, 3));
    assert(COORD(2, 2) + East == COORD(3, 2));
    assert(COORD(2, 2) + South == COORD(2, 1));
    assert(COORD(2, 2) + West == COORD(1, 2));
    assert(Compass[E_NORTH] == North);
    assert(Compass[E_EAST] == East);
    assert(Compass[E_WEST] == West);
    assert(Compass[E_SOUTH] == South);
    assert(Clockwise(E_NORTH) == E_EAST);
    assert(Clockwise(E_EAST) == E_SOUTH);
    assert(Clockwise(E_SOUTH) == E_WEST);
    assert(Clockwise(E_WEST) == E_NORTH);
    assert(Opposite(E_NORTH) == E_SOUTH);
    assert(Opposite(E_EAST) == E_WEST);
    assert(Opposite(E_SOUTH) == E_NORTH);
    assert(Opposite(E_WEST) == E_EAST);
    assert(Anticlockwise(E_NORTH) == E_WEST);
    assert(Anticlockwise(E_EAST) == E_NORTH);
    assert(Anticlockwise(E_SOUTH) == E_EAST);
    assert(Anticlockwise(E_WEST) == E_SOUTH);
    assert(ManhattanDistance(COORD(3, 2), COORD(-4, -7)) == 16);
    assert(DirectionalDistance(COORD(3, 2), COORD(-4, -7), E_NORTH) == -9);
    assert(DirectionalDistance(COORD(3, 2), COORD(-4, -7), E_EAST) == -7);
    assert(DirectionalDistance(COORD(3, 2), COORD(-4, -7), E_SOUTH) == 9);
    assert(DirectionalDistance(COORD(3, 2), COORD(-4, -7), E_WEST) == 7);
}
	// Input:
	//		poly - general polygon
	//		threshold - smaller threshold, smaller grid
	// Output:
	//		mesh - result mesh
	void Triangulation(CP_GeneralPolygon2D &poly, bool isClockwise, CP_Mesh3D &mesh) {
		CP_MeshTriangle3D *pTriangle = NULL;
		vector<CP_MeshVertex3D*> candidateVertexArray;

		int numOfEdge = poly.GetEdgeSize();
		if (numOfEdge == 0)
			return;
		for (int i = 0; i < poly.GetVertSize(); ++i) 
			mesh.AddVert(poly.GetVert(i));

		while (numOfEdge > 3) {
			GenEdge *edge0 = poly.GetEdge(0);
			CP_MeshVertex3D *L11 = edge0->m_pStart;
			CP_MeshVertex3D *L12 = edge0->m_pEnd;
			for (int k = 1; k < numOfEdge; ++k) {
				GenEdge *edgeK = poly.GetEdge(k);
				CP_MeshVertex3D *Lk2 = edgeK->m_pEnd;

				// Test if Lk2 in the left side of v_L11_L12
				if (!isClockwise && !IsLk2LeftOfL11L12(L11, L12, Lk2) ||
					isClockwise && !IsLk2RightOfL11L12(L11, L12, Lk2)) 
					continue;

				// Test if any edge in polygon intersect with line_L11_Lk2 or line_L12_Lk2
				CP_LineSegment3D line_L11_Lk2(L11->m_point, Lk2->m_point);
				CP_LineSegment3D line_L12_Lk2(L12->m_point, Lk2->m_point);
				CP_LineSegment3D lineJ;
				int j = 1;
				for (; j < numOfEdge; ++j) {
					GenEdge *edgeJ = poly.GetEdge(j);
					lineJ.m_startPt = edgeJ->m_pStart->m_point;
					lineJ.m_endPt = edgeJ->m_pEnd->m_point;
					if (!curve_basic::IsLinesegOverlap(&lineJ, &line_L11_Lk2) &&
						!curve_basic::IsLinesegOverlap(&lineJ, &line_L12_Lk2)) 
					{
					if (curve_basic::IsLinesegIntersect(&lineJ, &line_L11_Lk2) || 
						curve_basic::IsLinesegIntersect(&lineJ, &line_L12_Lk2))
						break;
					}
					
				}
				if (j != numOfEdge)
					continue;

				candidateVertexArray.push_back(Lk2); // Add Lk2 into candidate points when condition satisfied
			}		

			// Find closest vertex
			if (candidateVertexArray.size() == 0) {
				Anticlockwise(poly);
				continue;
			}
			int index = FindClosestVertex(L11, L12, candidateVertexArray);
			CP_MeshVertex3D *L02 = candidateVertexArray[index];
			candidateVertexArray.clear();

			// Create triangle and add to array
			pTriangle = new CP_MeshTriangle3D(L11, L12, L02);
			mesh.AddTriangle(pTriangle);

			// Reshape
			ReshapePoly(poly, L11, L12, L02);
			numOfEdge = poly.GetEdgeSize();
			// return;
		}

		// Add the last triangle into array
		pTriangle = new CP_MeshTriangle3D(poly.GetEdge(0), poly.GetEdge(1));
		mesh.AddTriangle(pTriangle);
		// Optimize
		// m_optimizedStructure.mf_optimize(&m_triangleArray, &m_vertexArray, threshold);
	}