I'll try to make this as concise as possible.
I have a 3D model defined by a list of triangles. Each triangle is defined with it's own set of three vertices. I need to optimize this down to just a single list of vertices with no duplication for future calculations with the vertices of each triangle now holding an index into the vertex list.
I've pretty much have something, but it seems rather kludgy. This is pseudo code, but it conveys the message clearly.
The function attempts to insert the vertex into the array sorted by x, y, z because I'll need to search quickly later to verify the existance of a pre-existing vertex and for later on. There's three possible return states, one being major failure (memory allocation), two not.
If it can add it, it does and needs to return an index in the array where the vertex was added and an indicator of that state.
The state indicator signals to the calling function that the list was modified and all index values in the index based triangle list of equal or greater value need to be incremented and the new surface index value assigned after.
If the vertex exists, it needs to signal this state to the calling function, because it means that the new surface index value can be assigned straight away.
This function returns:
1 for vertex added, thus array modification.
0 for memory allocation failure.
-1 for vertex exists and no array modification was made.
v_idx = the place where the vertex was added or found, depending on the returned state.
uStatus = insert_vertex (v_list, sl->surface[ss].v[sv], &v_idx));
Can anyone else see a better way to get the values I'm needing?
as an aside, if the array runs out of space, the function reallocs with space for 50 more vertices (though I may decide to make it a doubling) and at the end of all processing will realloc to trim it down to essential and no more.
I have a 3D model defined by a list of triangles. Each triangle is defined with it's own set of three vertices. I need to optimize this down to just a single list of vertices with no duplication for future calculations with the vertices of each triangle now holding an index into the vertex list.
I've pretty much have something, but it seems rather kludgy. This is pseudo code, but it conveys the message clearly.
The function attempts to insert the vertex into the array sorted by x, y, z because I'll need to search quickly later to verify the existance of a pre-existing vertex and for later on. There's three possible return states, one being major failure (memory allocation), two not.
If it can add it, it does and needs to return an index in the array where the vertex was added and an indicator of that state.
The state indicator signals to the calling function that the list was modified and all index values in the index based triangle list of equal or greater value need to be incremented and the new surface index value assigned after.
If the vertex exists, it needs to signal this state to the calling function, because it means that the new surface index value can be assigned straight away.
This function returns:
1 for vertex added, thus array modification.
0 for memory allocation failure.
-1 for vertex exists and no array modification was made.
v_idx = the place where the vertex was added or found, depending on the returned state.
uStatus = insert_vertex (v_list, sl->surface[ss].v[sv], &v_idx));
Can anyone else see a better way to get the values I'm needing?
as an aside, if the array runs out of space, the function reallocs with space for 50 more vertices (though I may decide to make it a doubling) and at the end of all processing will realloc to trim it down to essential and no more.