TriangleMeshShape* MeshSweeper::makeCone(const Polyline& circle, const Vec3& path, bool flat) //[]----------------------------------------------------[] //| Make cone | //[]----------------------------------------------------[] { int np = circle.getNumberOfVertices(); int nv = np *2; // number of vertices int nb = np - 2; // number of triangles of the base int nt = np + nb; // number of triangles TriangleMesh::Arrays data; data.vertices = new Vec3[data.numberOfVertices = nv]; data.normals = new Vec3[data.numberOfNormals = np + 2]; data.triangles = new TriangleMesh::Triangle[data.numberOfTriangles = nt]; Vec3 c(0, 0, 0); if (true) { Polyline::VertexIterator vit(circle.getVertexIterator()); for (int i = 0; i < np; i++) { const Vec3 p = vit++.position; data.vertices[i] = p; c+=p; } c *= Math::inverse<REAL>((REAL)np); // Adiciona vértice superior do cone data.vertices[np+1] = path + data.vertices[np].normalize(); } if (true) { Vec3 N = triangleNormal(data.vertices); data.normals[np] = N; data.normals[np+1] = -N; } TriangleMesh::Triangle* triangle = data.triangles; int npm = np + 1; for (int i = 0; i < np; i++) { int k = (i + 1) % np; triangle->setVertices(i, npm, k); if (flat) { data.normals[i] = triangleNormal(data.vertices, i, npm, k); triangle->setNormal(i); } else { data.normals[i] = (data.vertices[i + np] - c).versor(); triangle->setNormals(i, i, k); } triangle++; } int v0 = 0; int v1 = 1; int v2 = 2; int aux = np - 2; for (int i = 0; i < aux; i++) { triangle->setVertices(v0, v1, v2); triangle->setNormal(np); triangle++; v1 = (v1+1) % np; v2 = (v2+1) % np; } return new TriangleMeshShape(data); }
TriangleMeshShape* MeshSweeper::makeCylinder(const Polyline& circle, const Vec3& path, bool flat) //[]----------------------------------------------------[] //| Make cylinder | //[]----------------------------------------------------[] { int np = circle.getNumberOfVertices(); int nv = np * 2; // number of vertices int nb = np - 2; // number of triangles of the base int nt = nv + 2 * nb; // number of triangles TriangleMesh::Arrays data; data.vertices = new Vec3[data.numberOfVertices = nv]; data.normals = new Vec3[data.numberOfNormals = np + 2]; data.triangles = new TriangleMesh::Triangle[data.numberOfTriangles = nt]; Vec3 c(0, 0, 0); if (true) { Polyline::VertexIterator vit(circle.getVertexIterator()); for (int i = 0; i < np; i++) { const Vec3 p = vit++.position; c += p; data.vertices[i + np] = p; data.vertices[i] = p + path; } c *= Math::inverse<REAL>((REAL)np); } if (true) { Vec3 N = triangleNormal(data.vertices); // base normal data.normals[np] = N; data.normals[np + 1] = -N; } TriangleMesh::Triangle* triangle = data.triangles; for (int i = 0; i < np; i++) { int j = (i + np); int k = (i + 1) % np; triangle->setVertices(i, j, k); triangle[1].setVertices(j, k + np, k); if (flat) { data.normals[i] = triangleNormal(data.vertices, i, j, k); triangle->setNormal(i); triangle[1].setNormal(i); } else { data.normals[i] = (data.vertices[i + np] - c).versor(); triangle->setNormals(i, i, k); triangle[1].setNormals(i, k, k); } triangle += 2; } int v0 = 0; int v1 = 1; int v2 = 2; for (int i = 0; i < nb; i++) { triangle->setVertices(v0, v1, v2); triangle[nb].setVertices(v0 + np, v2 + np, v1 + np); triangle->setNormal(np); triangle[nb].setNormal(np + 1); triangle++; //v2 = ((v1 = (v0 = v2) + 1) + 1) % np; v1 = (v1+1) % np; v2 = (v2+1) % np; } return new TriangleMeshShape(data); }