TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::_internal::Welder Class Reference

Public Types

enum  { GRID_RES = 32 }
 
typedef Array< int > List
 

Public Member Functions

 Welder (const Array< Vector3 > &_oldVertexArray, Array< Vector3 > &_newVertexArray, Array< int > &_toNew, Array< int > &_toOld, float _radius)
 
void toGridCoords (Vector3 v, int &x, int &y, int &z) const
 
int getIndex (const Vector3 &vertex)
 
void weld ()
 

Public Attributes

List grid [GRID_RES][GRID_RES][GRID_RES]
 
const Array< Vector3 > & oldVertexArray
 
Array< Vector3 > & newVertexArray
 
Array< int > & toNew
 
Array< int > & toOld
 
const float radius
 
Vector3 offset
 
Vector3 scale
 

Private Member Functions

Welderoperator= (const Welder &w)
 

Member Typedef Documentation

Indices of newVertexArray elements in or near a grid cell.

Member Enumeration Documentation

anonymous enum
Enumerator
GRID_RES 
33 {GRID_RES = 32};
Definition: MeshAlgWeld.cpp:33

Constructor & Destructor Documentation

G3D::_internal::Welder::Welder ( const Array< Vector3 > &  _oldVertexArray,
Array< Vector3 > &  _newVertexArray,
Array< int > &  _toNew,
Array< int > &  _toOld,
float  _radius 
)
85  :
86  oldVertexArray(_oldVertexArray),
87  newVertexArray(_newVertexArray),
88  toNew(_toNew),
89  toOld(_toOld),
90  radius(_radius) {
91 
92  // Compute a scale factor that moves the range
93  // of all ordinates to [0, 1]
94  Vector3 minBound = Vector3::inf();
95  Vector3 maxBound = -minBound;
96 
97  for (int i = 0; i < oldVertexArray.size(); ++i) {
98  minBound = minBound.min(oldVertexArray[i]);
99  maxBound = maxBound.max(oldVertexArray[i]);
100  }
101 
102  offset = minBound;
103  scale = maxBound - minBound;
104  for (int i = 0; i < 3; ++i) {
105  // The model might have zero extent along some axis
106  if (fuzzyEq(scale[i], 0.0f)) {
107  scale[i] = 1.0;
108  } else {
109  scale[i] = 1.0f / scale[i];
110  }
111  }
112 }
const Array< Vector3 > & oldVertexArray
Definition: MeshAlgWeld.cpp:37
Array< int > & toOld
Definition: MeshAlgWeld.cpp:40
Vector3 offset
Definition: MeshAlgWeld.cpp:46
Array< int > & toNew
Definition: MeshAlgWeld.cpp:39
Vector3 scale
Definition: MeshAlgWeld.cpp:47
Array< Vector3 > & newVertexArray
Definition: MeshAlgWeld.cpp:38
const float radius
Definition: MeshAlgWeld.cpp:43
static const Vector3 & inf()
Definition: Vector3.cpp:124
bool fuzzyEq(double a, double b)
Definition: g3dmath.h:857

+ Here is the call graph for this function:

Member Function Documentation

int G3D::_internal::Welder::getIndex ( const Vector3 vertex)

Gets the index of a vertex, adding it to newVertexArray if necessary.

123  {
124 
125  int closestIndex = -1;
126  double distanceSquared = inf();
127 
128  int ix, iy, iz;
129  toGridCoords(vertex, ix, iy, iz);
130 
131  // Check against all vertices within radius of this grid cube
132  const List& list = grid[ix][iy][iz];
133 
134  for (int i = 0; i < list.size(); ++i) {
135  double d = (newVertexArray[list[i]] - vertex).squaredMagnitude();
136 
137  if (d < distanceSquared) {
138  distanceSquared = d;
139  closestIndex = list[i];
140  }
141  }
142 
143  if (distanceSquared <= radius * radius) {
144 
145  return closestIndex;
146 
147  } else {
148 
149  // This is a new vertex
150  int newIndex = newVertexArray.size();
151  newVertexArray.append(vertex);
152 
153  // Create a new vertex and store its index in the
154  // neighboring grid cells (usually, only 1 neighbor)
155 
156  Set<List*> neighbors;
157 
158  for (float dx = -1; dx <= +1; ++dx) {
159  for (float dy = -1; dy <= +1; ++dy) {
160  for (float dz = -1; dz <= +1; ++dz) {
161  int ix, iy, iz;
162  toGridCoords(vertex + Vector3(dx, dy, dz) * radius, ix, iy, iz);
163  neighbors.insert(&(grid[ix][iy][iz]));
164  }
165  }
166  }
167 
168  Set<List*>::Iterator neighbor(neighbors.begin());
169  Set<List*>::Iterator none(neighbors.end());
170 
171  while (neighbor != none) {
172  (*neighbor)->append(newIndex);
173  ++neighbor;
174  }
175 
176  return newIndex;
177  }
178 }
double inf()
Definition: g3dmath.cpp:40
Array< Vector3 > & newVertexArray
Definition: MeshAlgWeld.cpp:38
void toGridCoords(Vector3 v, int &x, int &y, int &z) const
Definition: MeshAlgWeld.cpp:115
const float radius
Definition: MeshAlgWeld.cpp:43
Array< int > List
Definition: MeshAlgWeld.cpp:31
List grid[GRID_RES][GRID_RES][GRID_RES]
Definition: MeshAlgWeld.cpp:35

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Welder& G3D::_internal::Welder::operator= ( const Welder w)
private
void G3D::_internal::Welder::toGridCoords ( Vector3  v,
int &  x,
int &  y,
int &  z 
) const

Computes the grid index from an ordinate.

115  {
116  v = (v - offset) * scale;
117  x = iClamp(iFloor(v.x * GRID_RES), 0, GRID_RES - 1);
118  y = iClamp(iFloor(v.y * GRID_RES), 0, GRID_RES - 1);
119  z = iClamp(iFloor(v.z * GRID_RES), 0, GRID_RES - 1);
120 }
int iFloor(double fValue)
Definition: g3dmath.h:603
Vector3 offset
Definition: MeshAlgWeld.cpp:46
Vector3 scale
Definition: MeshAlgWeld.cpp:47
G3D::int16 z
Definition: Vector3int16.h:46
G3D::int16 y
Definition: Vector2int16.h:38
Definition: MeshAlgWeld.cpp:33
G3D::int16 x
Definition: Vector2int16.h:37
int iClamp(int val, int low, int hi)
Definition: g3dmath.h:545

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::_internal::Welder::weld ( )
181  {
182  newVertexArray.resize(0);
183 
184  // Prime the vertex positions
185  for (int i = 0; i < oldVertexArray.size(); ++i) {
187  }
188 
189  // Now create the official remapping by snapping to
190  // nearby vertices.
191  toNew.resize(oldVertexArray.size());
192  toOld.resize(newVertexArray.size());
193 
194  for (int oi = 0; oi < oldVertexArray.size(); ++oi) {
195  toNew[oi] = getIndex(oldVertexArray[oi]);
196  toOld[toNew[oi]] = oi;
197  }
198 }
void resize(size_t n, bool shrinkIfNecessary=true)
Definition: Array.h:490
const Array< Vector3 > & oldVertexArray
Definition: MeshAlgWeld.cpp:37
Array< int > & toOld
Definition: MeshAlgWeld.cpp:40
Array< int > & toNew
Definition: MeshAlgWeld.cpp:39
Array< Vector3 > & newVertexArray
Definition: MeshAlgWeld.cpp:38
int getIndex(const Vector3 &vertex)
Definition: MeshAlgWeld.cpp:123

+ Here is the call graph for this function:

Member Data Documentation

List G3D::_internal::Welder::grid[GRID_RES][GRID_RES][GRID_RES]
Array<Vector3>& G3D::_internal::Welder::newVertexArray
Vector3 G3D::_internal::Welder::offset

(oldVertexArray[i] - offset) * scale is on the range [0, 1]

const Array<Vector3>& G3D::_internal::Welder::oldVertexArray
const float G3D::_internal::Welder::radius

Must be less than one grid cell, not checked

Vector3 G3D::_internal::Welder::scale
Array<int>& G3D::_internal::Welder::toNew
Array<int>& G3D::_internal::Welder::toOld

The documentation for this class was generated from the following file: