Graphics Reference
In-Depth Information
skipcell: ;
}
}
// Couldn't locate vertex, so add it to grid, then return vertex itself
int x = int(v-
>
x / CELL_SIZE);
int y = int(v-
>
y / CELL_SIZE);
AddVertexToBucket(*v, GetGridCellBucket(x, y));
return v;
}
The
WeldVertex()
function can now be called repeatedly for all vertices subject to
welding.
void WeldVertices(Point v[], int n)
{
// Initialize the hash table of linked vertex lists
for(intk=0;k<NUM_BUCKETS; k++)
first[k] = -1;
numVertices = 0;
// Loop over all vertices, doing something with the welded vertex
for(inti=0;i<n;i++) {
Point *pVert = WeldVertex(&v[i]);
if (pVert != &v[i])
...report v[i] was welded to pVert...
}
}
After all vertices have been processed, the first
numVertices
entries of the
vertex
array contain all unique vertices. This makes it easy to, for example, output them to
a data file.
fwrite(&numVertices, sizeof(numVertices), 1, stream);
// Output number of verts
fwrite(&vertex[0], sizeof(Point), numVertices, stream);
// Output verts themselves
Looping over all faces and welding the vertices of these faces automatically
removes any unused vertices, in that only the vertices referenced by a face are being
welded.
A less sophisticated welding method than the one just described is to sort all
vertices (
x
i
,
y
i
,
z
i
) on the absolute component value sum,
y
i
+ |
|
x
i
| +
z
i
|
. Given the