Jump to content

Polygon Reduction library


Josh
 Share

Recommended Posts

Here is some code that uses it, but I just can't seem to get it to work right. I don't think there is an error in my code.

I had to fix this file for C++17. Maybe that is causing it? I do not understand these functions:
misc.h

	int VertMap(const std::vector<int>& collapse_map, int a, int mx) {
		if (mx <= 0) return 0;
		while (a >= mx)
		{
			a = collapse_map[a];
		}
		return a;
	}
					std::vector<linalg::aliases::float3> vert;
					std::vector<tridata> tri;
					std::vector<int> collapse_map;
					std::vector<int> permutation;
					tridata t;

					for (const auto& v : mesh->vertices)
					{
						vert.push_back(linalg::aliases::float3(v.position.x, v.position.y, v.position.z));
					}

					for (int n = 0; n < mesh->CountPrimitives(); ++n)
					{
						t.v[0] = mesh->GetPrimitiveVertex(n, 0);
						t.v[1] = mesh->GetPrimitiveVertex(n, 1);
						t.v[2] = mesh->GetPrimitiveVertex(n, 2);
						tri.push_back(t);
					}

					//-----------------------------
					// Reduce the mesh
					//-----------------------------

					ProgressiveMesh(vert, tri, collapse_map, permutation);

					//-----------------------------
					// PermuteVertices
					//-----------------------------

					// rearrange the vertex Array 
					std::vector<float3> temp_Array;
					unsigned int i;
					assert(permutation.size() == vert.size());
					for (i = 0; i < vert.size(); i++) {
						temp_Array.push_back(vert[i]);
					}
					for (i = 0; i < vert.size(); i++) {
						vert[permutation[i]] = temp_Array[i];
					}
					// update the changes in the entries in the triangle Array
					for (i = 0; i < tri.size(); i++) {
						for (int j = 0; j < 3; j++) {
							tri[i].v[j] = permutation[tri[i].v[j]];
						}
					}

					//-----------------------------
					// Build new mesh
					//-----------------------------

					model->Clear();
					mesh->extra = nullptr;
					mesh = model->AddMesh();

					std::vector<int> newvertex(vert.size());
					std::fill(newvertex.begin(), newvertex.end(), -1);

					//Add triangles
					int a, b, c, p0, p1, p2;
					int render_num = vert.size() * 0.5;
					for (unsigned int i = 0; i < tri.size(); i++)
					{
						p0 = VertMap(collapse_map, tri[i].v[0], render_num);
						p1 = VertMap(collapse_map, tri[i].v[1], render_num);
						p2 = VertMap(collapse_map, tri[i].v[2], render_num);
						if (p0 == p1 or p1 == p2 or p2 == p0) continue;
						a = newvertex[p0];
						if (a == -1)
						{
							a = mesh->AddVertex(vert[p0].x, vert[p0].y, vert[p0].z);
							newvertex[p0] = a;
						}
						b = newvertex[p1];
						if (b == -1)
						{
							b = mesh->AddVertex(vert[p1].x, vert[p1].y, vert[p1].z);
							newvertex[p1] = b;
						}
						c = newvertex[p2];
						if (c == -1)
						{
							c = mesh->AddVertex(vert[p2].x, vert[p2].y, vert[p2].z);
							newvertex[p2] = c;
						}
						//int a = mesh->AddVertex(vert[p0].x, vert[p0].y, vert[p0].z);
						//int b = mesh->AddVertex(vert[p1].x, vert[p1].y, vert[p1].z);
						//int c = mesh->AddVertex(vert[p2].x, vert[p2].y, vert[p2].z);
						mesh->AddPrimitive(a, b, c);
					}

					mesh->UpdateBounds();
					model->UpdateBounds();

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...