1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
// Copyright NVIDIA Corporation 2006 -- Ignacio Castano <icastano@nvidia.com>
#include <nvmesh/TriMesh.h>
#include <nvmesh/QuadTriMesh.h>
#include <nvmesh/weld/VertexWeld.h>
#include <nvmesh/weld/Weld.h>
using namespace nv;
// Weld trimesh vertices
void nv::WeldVertices(TriMesh * mesh)
{
nvDebug("--- Welding vertices.\n");
nvCheck(mesh != NULL);
uint count = mesh->vertexCount();
Array<uint> xrefs;
Weld<TriMesh::Vertex> weld;
uint newCount = weld(mesh->vertices(), xrefs);
nvDebug("--- %d vertices welded\n", count - newCount);
// Remap faces.
const uint faceCount = mesh->faceCount();
for(uint f = 0; f < faceCount; f++)
{
TriMesh::Face & face = mesh->faceAt(f);
face.v[0] = xrefs[face.v[0]];
face.v[1] = xrefs[face.v[1]];
face.v[2] = xrefs[face.v[2]];
}
}
// Weld trimesh vertices
void nv::WeldVertices(QuadTriMesh * mesh)
{
nvDebug("--- Welding vertices.\n");
nvCheck(mesh != NULL);
uint count = mesh->vertexCount();
Array<uint> xrefs;
Weld<TriMesh::Vertex> weld;
uint newCount = weld(mesh->vertices(), xrefs);
nvDebug("--- %d vertices welded\n", count - newCount);
// Remap faces.
const uint faceCount = mesh->faceCount();
for(uint f = 0; f < faceCount; f++)
{
QuadTriMesh::Face & face = mesh->faceAt(f);
face.v[0] = xrefs[face.v[0]];
face.v[1] = xrefs[face.v[1]];
face.v[2] = xrefs[face.v[2]];
if (face.isQuadFace())
{
face.v[3] = xrefs[face.v[3]];
}
}
}
// OLD code
#if 0
namespace {
struct VertexInfo {
uint id; ///< Original vertex id.
uint normal_face_group;
uint tangent_face_group;
uint material;
uint chart;
};
/// VertexInfo hash functor.
struct VertexHash : public IHashFunctor<VertexInfo> {
VertexHash(PiMeshPtr m) : mesh(m) {
uint c = mesh->FindChannel(VS_POS);
piCheck(c != PI_NULL_INDEX);
channel = mesh->GetChannel(c);
piCheck(channel != NULL);
}
uint32 operator () (const VertexInfo & v) const {
return channel->data[v.id].GetHash();
}
private:
PiMeshPtr mesh;
PiMesh::Channel * channel;
};
/// VertexInfo comparator.
struct VertexEqual : public IBinaryPredicate<VertexInfo> {
VertexEqual(PiMeshPtr m) : mesh(m) {}
bool operator () (const VertexInfo & a, const VertexInfo & b) const {
bool equal = a.normal_face_group == b.normal_face_group &&
a.tangent_face_group == b.tangent_face_group &&
a.material == b.material &&
a.chart == b.chart;
// Split vertex shared by different face types.
if( !equal ) {
return false;
}
// They were the same vertex.
if( a.id == b.id ) {
return true;
}
// Vertex equal if all the channels are equal.
return mesh->IsVertexEqual(a.id, b.id);
}
private:
PiMeshPtr mesh;
};
} // namespace
/// Weld the vertices.
void PiMeshVertexWeld::WeldVertices(const PiMeshSmoothGroup * mesh_smooth_group,
const PiMeshMaterial * mesh_material, const PiMeshAtlas * mesh_atlas )
{
piDebug( "--- Welding vertices:\n" );
piDebug( "--- Expand mesh vertices.\n" );
PiArray<VertexInfo> vertex_array;
const uint face_num = mesh->GetFaceNum();
const uint vertex_max = face_num * 3;
vertex_array.Resize( vertex_max );
for(uint i = 0; i < vertex_max; i++) {
uint f = i/3;
const PiMesh::Face & face = mesh->GetFace(f);
vertex_array[i].id = face.v[i%3];
// Reset face attributes.
vertex_array[i].normal_face_group = PI_NULL_INDEX;
vertex_array[i].tangent_face_group = PI_NULL_INDEX;
vertex_array[i].material = PI_NULL_INDEX;
vertex_array[i].chart = PI_NULL_INDEX;
// Set available attributes.
if( mesh_smooth_group != NULL ) {
if( mesh_smooth_group->HasNormalFaceGroups() ) {
vertex_array[i].normal_face_group = mesh_smooth_group->GetNormalFaceGroup( f );
}
if( mesh_smooth_group->HasTangentFaceGroups() ) {
vertex_array[i].tangent_face_group = mesh_smooth_group->GetTangentFaceGroup( f );
}
}
if( mesh_material != NULL ) {
vertex_array[i].material = mesh_material->GetFaceMaterial( f );
}
if( mesh_atlas != NULL && mesh_atlas->HasCharts() ) {
vertex_array[i].chart = mesh_atlas->GetFaceChart( f );
}
}
piDebug( "--- %d vertices.\n", vertex_max );
piDebug( "--- Collapse vertices.\n" );
uint * xrefs = new uint[vertex_max];
VertexHash hash(mesh);
VertexEqual equal(mesh);
const uint vertex_num = Weld( vertex_array, xrefs, hash, equal );
piCheck(vertex_num <= vertex_max);
piDebug( "--- %d vertices.\n", vertex_num );
// Remap face indices.
piDebug( "--- Remapping face indices.\n" );
mesh->RemapFaceIndices(vertex_max, xrefs);
// Overwrite xrefs to map new vertices to old vertices.
for(uint v = 0; v < vertex_num; v++) {
xrefs[v] = vertex_array[v].id;
}
// Update vertex order.
mesh->ReorderVertices(vertex_num, xrefs);
delete [] xrefs;
}
#endif // 0
|