/* * placeNodeInQueue method will look at a node to see if its polygon is free and if so it will add the polygon being held * to a AIPolygonQueue * Parameter: (AIBinaryTreeNode)nodeToPlace is the node that needs its polygon check to see if its free * Return: none */ void placeNodeInQueue(AIBinaryTreeNode nodeToPlace, AIPolygonQueue tempQueue) { if (nodeToPlace.isFree() == true) { tempQueue.enqueue(nodeToPlace.getPolygon()); } }
public AIBinaryTreeNode rightNode; //reference to the rightNode child /* * AIBinaryTreeNode method is the constructor for this class. It will set up all the variables to their initial * state * Parameter: (AIPolygon)polygonToAdd is the polygon that this node will hold * Return: none */ public AIBinaryTreeNode(AIPolygon polygonToAdd) { polygonBeingHeld = polygonToAdd; free = false; leftNode = null; rightNode = null; }
/* * makeNewPolygons method will take an array of vertices and a position that the second polygon starts and make two new binarySpaceNodes from those verices * Parameters: (AIBinaryTreeNode)polygonNode is the node that holds the polygon that was logically split. will be used to place the leftNode child and RightNode child * (int)secondPolygonStartingIndex is the position inside the polygonToSplit array that the second polygon starts * (Vector3[])polygonToSplit is the array that olds the vertices for both the new polygons that are about to be created * (ref int)idCounter is the counter of the polygons created so far and is also used to give a polygon an ID at creation * Return: none */ bool makeNewPolygons(AIBinaryTreeNode polygonNode, int secondPolygonstartingIndex, Vector3[] polygonToSplit, ref int idCounter) { Vector3[] firstPolygonVertices = new Vector3[secondPolygonstartingIndex + 1]; Vector3[] secondPolygonVertices = new Vector3[verticesTomakePolygonFromCount - secondPolygonstartingIndex]; int count = 0; int firstCount = 0; for (; firstCount < secondPolygonstartingIndex; firstCount++) //adds the first polygon's vertices until the start of the second polygon { firstPolygonVertices [firstCount] = new Vector3(verticeToMakePolygonFrom [count].x, verticeToMakePolygonFrom [count].y, verticeToMakePolygonFrom [count].z); count++; } for (int secondCount = 0; secondCount < secondPolygonVertices.Length; secondCount++) //adds the second polygon's vertices until the end of the list { secondPolygonVertices [secondCount] = new Vector3(verticeToMakePolygonFrom [count].x, verticeToMakePolygonFrom [count].y, verticeToMakePolygonFrom [count].z); count++; } firstPolygonVertices [firstCount] = new Vector3(verticeToMakePolygonFrom [count - 1].x, verticeToMakePolygonFrom [count - 1].y, verticeToMakePolygonFrom [count - 1].z); //add the final intersecting point in the array if (isAllVerticesDifferent(firstPolygonVertices) == false || isAllVerticesDifferent(secondPolygonVertices) == false) { return(false); } polygonNode.leftNode = new AIBinaryTreeNode(new AIPolygon(firstPolygonVertices, idCounter)); //make a new BinarySpaceNode with the new vertices and sets it to polygonNode's leftNode reference idCounter++; polygonNode.rightNode = new AIBinaryTreeNode(new AIPolygon(secondPolygonVertices, idCounter)); //make a new BinarySpaceNode with the new vertices and sets it to polygonNode's rightNode reference idCounter++; return(true); }
//public void trySubdivide( public AIBinaryTreeNode subDivide2(AIBinaryTreeNode polygonNode, ref int idCounter) { if (polygonNode == null) { return(null); } if (polygonNode.checkIfTerminated(arrayOfEdges) == true) { return(null); } if (polygonNode.checkIfFree(edgeVertices) == true) { return(polygonNode); } bool polygonSplitSuccessful = tryGetTwoPolygons(polygonNode, ref idCounter); if (polygonSplitSuccessful == true) { indexForEdges++; if (subDivide2(polygonNode.leftNode, ref idCounter) != null) //recusively calls on the leftNode child { polygonNode.leftNode.setToFree(); } if (subDivide2(polygonNode.rightNode, ref idCounter) != null) //recusively calls on the RightNode child { polygonNode.rightNode.setToFree(); } } return(null); }
/* * postOrderTraversal method will recursivly traverse the Binary Space Partition tree and enqueue any Free polygons * it finds into the AIPolygonQueue in a postOrder order * Parameter: (AIBinaryTreeNode)nodeToCheck is the current node that needs to be checked and then added into the Queue * Reurn: none */ void postOrderTraversal(AIBinaryTreeNode nodeToCheck, AIPolygonQueue tempQueue) { if (nodeToCheck != null) { postOrderTraversal(nodeToCheck.leftNode, tempQueue); //recursive call on the LeftNode child postOrderTraversal(nodeToCheck.rightNode, tempQueue); //recursivecall on the RightNode child placeNodeInQueue(nodeToCheck, tempQueue); //check to see if the polygon is free and if so add it to the Queue } }
/* * AIBinarySpaceTree method is the constructor for this class. It will set up all the variables to their initial state. * Parameters: (GameObject)objectToAdd will be the object that the polygons are going to be split with * (Vector3[,])arrayOfEdgesToAdd will be the array of edges with no Y coordinate change for objectToAdd * (AIPolygon)polygonToAdd is the starting polygon that will be the in the rootNode of the tree * (AIPolygonQueue)polygonQueueToAdd is the reference to the Queue that the free polygons will be added to * Return: none */ public AIBinarySpaceTree(GameObject objectToAdd, Vector3[,] arrayOfEdgesToAdd, Vector3[] arrayOfEdgeVerticesAdd, AIPolygon polygonToAdd, AIPolygonQueue polygonQueueToAdd) { rootNode = new AIBinaryTreeNode(polygonToAdd); arrayOfEdges = arrayOfEdgesToAdd; edgeVertices = getArrayOfEdgeVertices(); indexForEdges = 0; indexCount = 0; usedEdgesForObject = new bool[arrayOfEdges.Length / 2]; for (int count = 0; count < usedEdgesForObject.Length; count++) { usedEdgesForObject [count] = false; } }
/* The method of tryGetTwoPolygons attempts to merge two polygons * Parameter: AIBinaryTreeNode polygonNode, ref int idCounter * Return: bool * true if merge was successful, false otherwise */ bool tryGetTwoPolygons(AIBinaryTreeNode polygonNode, ref int idCounter) { int[] indexArrayToUse = getAllFalseIndices(); bool[] indexArrayOfBools = getABoolArrayOfFalses(indexArrayToUse.Length); int indexToUse = -1; indexArrayToUse = getAllFalseIndices(); indexArrayOfBools = getABoolArrayOfFalses(indexArrayToUse.Length); indexToUse = -1; while (allIndicesAreTrue(indexArrayOfBools) == false) { indexToUse = -1; indexToUse = getIndexToUseForEdges(indexArrayOfBools); if (indexToUse == -1) { return(false); } for (int count = 0; count < polygonNode.getPolygon().getVerticesCount(); count++) { int secondPolygonStartingIndex = getTwoPolygons(polygonNode.getPolygon(), indexArrayToUse [indexToUse], count); // seperates the one polygon into two and returns the stating index of the second polygon if (secondPolygonStartingIndex == -1) { indexArrayOfBools [indexToUse] = true; } else { if (makeNewPolygons(polygonNode, secondPolygonStartingIndex, polygonNode.getPolygon().getVertices(), ref idCounter) == true) // makes two new polygons from the seperated one { usedEdgesForObject [indexArrayToUse [indexToUse]] = true; indexCount++; return(true); } } } indexArrayOfBools [indexToUse] = true; } return(false); }