Properly set shape data from robotics library to HACD structure
Posted: Mon Dec 07, 2020 5:31 pm
Hi, I need to implement convex decomposition for dynamic objects. I also using robotics library for getting shape of the object https://github.com/roboticslibrary/rl/b ... pe.cpp#L74.
At first I looked at an example https://github.com/kripken/bullet/blob/ ... o.cpp#L169, but it does not suit me as there they use WavefrontObj for getting data, and I receive other structures. For example, for simple cube the array of indexes will have 36 elements, vertices 108 (x, y, z), but HACD structure will require 8 unique points and 12 triangles consisting of points' indexes. For setting the data, I added the addVertex function which saves unique positions of vertices I debugged example with cube decomposition to see how points were set and by analogy I placed them(which, interestingly, in the example were set in clockwise order). In addition to the fact that this method is slow, it also displays complex objects with inaccuracies (with cube everything is ok). Maybe there is a function that converts the data for HACD structure properly, or there are some other ideas how to set data properly?
At first I looked at an example https://github.com/kripken/bullet/blob/ ... o.cpp#L169, but it does not suit me as there they use WavefrontObj for getting data, and I receive other structures. For example, for simple cube the array of indexes will have 36 elements, vertices 108 (x, y, z), but HACD structure will require 8 unique points and 12 triangles consisting of points' indexes. For setting the data, I added the addVertex function which saves unique positions of vertices
Code: Select all
int addVertex(btVector3 v)
{
int index = unique_vertices.size();
auto it = std::find_if(unique_vertices.begin(), unique_vertices.end(), [v](btVector3 vertex) {return vertex == v; });
if (it != unique_vertices.end())
index = it - unique_vertices.begin();
else
unique_vertices.push_back(v);
return index;
}
Code: Select all
for (int i = 0; i < this->indices.size() / 3; i++)
{
int index0 = this->indices[i * 3];
int index1 = this->indices[i * 3 + 1];
int index2 = this->indices[i * 3 + 2];
btVector3 vertex0(this->vertices[index0 * 3], this->vertices[index0 * 3 + 1], this->vertices[index0 * 3 + 2]);
btVector3 vertex1(this->vertices[index1 * 3], this->vertices[index1 * 3 + 1], this->vertices[index1 * 3 + 2]);
btVector3 vertex2(this->vertices[index2 * 3], this->vertices[index2 * 3 + 1], this->vertices[index2 * 3 + 2]);
int v0 = addVertex(vertex0);
int v1 = addVertex(vertex1);
int v2 = addVertex(vertex2);
//clockwise order ?
triangle_indices.push_back(v2);
triangle_indices.push_back(v1);
triangle_indices.push_back(v0);
}
for (const btVector3& v : this->unique_vertices)
{
HACD::Vec3<HACD::Real> vertex(v.x(), v.y(), v.z());
points.push_back(vertex);
}
for (int i = 0; i < this->triangle_indices.size() / 3; i++)
{
HACD::Vec3<long> triangle(this->triangle_indices[i * 3], this->triangle_indices[i * 3 + 1], this->triangle_indices[i * 3 + 2]);
triangles.push_back(triangle);
}