Convert ON_Mesh* to float3 triangles

Supposing you have an ON_Mesh* sceneMesh and you want to get all the triangles of the geometry in float3 triplets of triangles, how can you convert any type of Mesh (triangular, quad, ngons) to such a representation?
For trianglular and quad meshes I have applied the following:


for (int faceIndex = 0; faceIndex < sceneMesh ->FaceCount(); ++faceIndex) {
	const ON_MeshFace& face = sceneMesh ->m_F[faceIndex];

	if (face.IsTriangle()) {
		// Get the vertex indices for the current face
		int v0 = face.vi[0];
		int v1 = face.vi[1];
		int v2 = face.vi[2];
		// Convert the vertex indices to float3 format
		float3 triangle = {
			static_cast<float>(sceneMesh->Vertex(v0).x),
			static_cast<float>(sceneMesh->Vertex(v0).y),
			static_cast<float>(sceneMesh->Vertex(v0).z)
		};
		vertices.push_back(triangle);
		triangle = {
			static_cast<float>(sceneMesh->Vertex(v1).x),
			static_cast<float>(sceneMesh->Vertex(v1).y),
			static_cast<float>(sceneMesh->Vertex(v1).z)
		};
		vertices.push_back(triangle);
		triangle = {
			static_cast<float>(sceneMesh->Vertex(v2).x),
			static_cast<float>(sceneMesh->Vertex(v2).y),
			static_cast<float>(sceneMesh->Vertex(v2).z)
		};
		vertices.push_back(triangle);
	}
	if (face.IsQuad()) {
		int v0 = face.vi[0];
		int v1 = face.vi[1];
		int v2 = face.vi[2];
		int v3 = face.vi[3];

		vertices.push_back({
			static_cast<float>(sceneMesh->Vertex(v0).x),
			static_cast<float>(sceneMesh->Vertex(v0).y),
			static_cast<float>(sceneMesh->Vertex(v0).z)
			});
		vertices.push_back({
			static_cast<float>(sceneMesh->Vertex(v1).x),
			static_cast<float>(sceneMesh->Vertex(v1).y),
			static_cast<float>(sceneMesh->Vertex(v1).z)
			});
		vertices.push_back({
			static_cast<float>(sceneMesh->Vertex(v2).x),
			static_cast<float>(sceneMesh->Vertex(v2).y),
			static_cast<float>(sceneMesh->Vertex(v2).z)
			});

		vertices.push_back({
			static_cast<float>(sceneMesh->Vertex(v0).x),
			static_cast<float>(sceneMesh->Vertex(v0).y),
			static_cast<float>(sceneMesh->Vertex(v0).z)
			});
		vertices.push_back({
			static_cast<float>(sceneMesh->Vertex(v2).x),
			static_cast<float>(sceneMesh->Vertex(v2).y),
			static_cast<float>(sceneMesh->Vertex(v2).z)
			});
		vertices.push_back({
			static_cast<float>(sceneMesh->Vertex(v3).x),
			static_cast<float>(sceneMesh->Vertex(v3).y),
			static_cast<float>(sceneMesh->Vertex(v3).z)
			});
	}
}

It looks it works, but how can modify it to convert ngons too?
Thanks!

Ngons are collections of triangles that already exist in the m_F array. So if you got all faces from that array, you will also have triangulated ngons. Try it :smiling_face:

1 Like