void Mesh::createFaceNormals(void) { // // Copy your previous (PA04) solution here. // // Calculate the number of qu(i)ds int nFaceI = (nI - 1) + wrapI, nFaceJ = (nJ - 1) + wrapJ; nFaces = (nFaceI * nFaceJ) * 2; // Allocate the arrays faceNormals = new Vector3[nFaces]; centroids = new Point3[nFaces]; for (int i = 0; i < nFaceI; i++) { for (int j = 0; j < nFaceJ; j++) { // Calculate both face indexes at (i, j) int uLeftIndex = faceIndex(i, j, true), lRrightIndex = faceIndex(i, j, false); // Get the points sorrounding the vertex Point3 *points = new Point3[4]; // P3 P2 . . . p0, p1, p2, p3 (index positions) // P0 P1 quadBoundary(i, j, points); // double check third parameter // Calculate the two centroids Point3 uLeftCentroid = triangleCentroid(points[0], points[2], points[3]); Point3 lRightCentroid = triangleCentroid(points[0], points[1], points[2]); // Calculate the two face normals Vector3 uLeftFaceNormal = faceNormal(points[0], points[2], points[3]); Vector3 lRightFaceNormal = faceNormal(points[0], points[1], points[2]); // Add to the arrays faceNormals[uLeftIndex] = uLeftFaceNormal; faceNormals[lRrightIndex] = lRightFaceNormal; centroids[uLeftIndex] = uLeftCentroid; centroids[lRrightIndex] = lRightCentroid; delete [] points; } } }
int main() { /*TMemoryPool<int, 20> pool; for (int i = 0; i < 20; ++i) { int* num = pool.create(); *num = i; std::cout << num << " -> " << *num << std::endl; }*/ Boundary2D quadBoundary(Point2D(100.0f, 100.0f), Point2D(100.0f, 100.0f)); size_t quadCapacity = 5; Point2D minQuadSize(10.0f, 10.0f); QuadTree<size_t> qtree(quadBoundary, quadCapacity, minQuadSize); srand(static_cast <unsigned> (time(0))); for (int i = 0; i < 50; ++i) { size_t id = i; float x = static_cast <float> (rand()) / static_cast <float> (RAND_MAX) * 200.0f; float y = static_cast <float> (rand()) / static_cast <float> (RAND_MAX) * 200.0f; Point2D pos(x, y); qtree.insert(id, pos); } for (int i = 0; i < 20; ++i) { size_t id = 1337 + i; Point2D pos(0.0f, 0.0f); qtree.insert(id, pos); } qtree.print(); for (int i = 0; i < 20; ++i) { size_t id = 1337 + i; Point2D pos(0.0f, 0.0f); qtree.remove(id, pos); } std::cout << "DIVIDER" << std::endl; qtree.print(); Boundary2D targetRange(Point2D(100.0f, 100.0f), Point2D(20.0f, 20.0f)); std::vector<QuadTree<size_t>::Entry> entries = qtree.queryRange(targetRange); std::cout << std::endl << "RangeQuery " << targetRange << ": "; for (const QuadTree<size_t>::Entry& entry : entries) { std::cout << "[" << entry.obj << ":" << entry.pos << "]"; } std::cout << std::endl; }
void Mesh::createFaceNormals(void) { // // ASSIGNMENT (PA04) // // Add code to instance the face normals. If `wrapI` is false, // there are `nI-1` quads in the `i` direction. If it's true, // there are `nI`. Similarly, `wrapJ` and `nJ` determine the number // of quads in the `j` direction. Each quad has two faces. Based // on these considerations, you can calculate `nFaces`, the number // of faces. Use it to allocate arrays `faceNormals[]` and // `centroids[]` on the heap, then visit each face put its face // normal and centroid in those arrays, respectively. // // You may find the quadBoundary() function helpful here: Pass it // `i` and `j` for each quad and then use the points it returns to // build the normals and centroids of each of the two (triangular) // faces. // // 21 lines in instructor solution (YMMV) // /* THIS SOUNDS EASY */ // Calculate the number of qu(i)ds int nFaceI = (nI - 1) + wrapI, nFaceJ = (nJ - 1) + wrapJ; nFaces = (nFaceI * nFaceJ) * 2; // Allocate the arrays faceNormals = new Vector3[nFaces]; centroids = new Point3[nFaces]; for (int i = 0; i < nFaceI; i++) { for (int j = 0; j < nFaceJ; j++) { // Calculate both face indexes at (i, j) int uLeftIndex = faceIndex(i, j, true), lRrightIndex = faceIndex(i, j, false); // Get the points sorrounding the vertex Point3 *points = new Point3[4]; // P3 P2 . . . p0, p1, p2, p3 (index positions) // P0 P1 quadBoundary(i, j, points); // double check third parameter // Calculate the two centroids Point3 uLeftCentroid = triangleCentroid(points[0], points[2], points[3]); Point3 lRightCentroid = triangleCentroid(points[0], points[1], points[2]); // Calculate the two face normals Vector3 uLeftFaceNormal = faceNormal(points[0], points[2], points[3]); Vector3 lRightFaceNormal = faceNormal(points[0], points[1], points[2]); // Add to the arrays faceNormals[uLeftIndex] = uLeftFaceNormal; faceNormals[lRrightIndex] = lRightFaceNormal; centroids[uLeftIndex] = uLeftCentroid; centroids[lRrightIndex] = lRightCentroid; delete [] points; } } }