------------------------------------------------------------------------------ The heart of the algorithm. The cells are sorted in i-j space into a contiguous array. Then the offsets into the array are built.
| 237 | // The heart of the algorithm. The cells are sorted in i-j space into |
| 238 | // a contiguous array. Then the offsets into the array are built. |
| 239 | void vtkInternalSpanSpace::Build() |
| 240 | { |
| 241 | // The first thing to do is to sort the elements across span |
| 242 | // space. The shape of the span space is upper diagonal (because |
| 243 | // smax >= smin) but for simplicity sake (for now) we just use a |
| 244 | // rectangular discretization (of dimensions Dim*Dim). |
| 245 | vtkSMPTools::Sort(this->Space, this->Space + this->NumCells); |
| 246 | |
| 247 | // Now that this is done, we create a matrix of offsets into the |
| 248 | // sorted array. This enables rapid access into the sorted cellIds, |
| 249 | // including access to span space rows of cells. Also for |
| 250 | // convenience we replicate the cell ids. This further supports |
| 251 | // parallel traversal which is a common use case. If I was smarter I |
| 252 | // could use the CellIds already contained in the tuple and not have |
| 253 | // to duplicate this, but then sorting requires a custom class with |
| 254 | // iterators, etc. |
| 255 | |
| 256 | // First count the number of contributions in each bucket. |
| 257 | vtkIdType cellId, numElems; |
| 258 | for (cellId = 0; cellId < this->NumCells; ++cellId) |
| 259 | { |
| 260 | this->Offsets[this->Space[cellId].Index]++; |
| 261 | this->CellIds[cellId] = this->Space[cellId].CellId; |
| 262 | } |
| 263 | |
| 264 | // Now accumulate offset array |
| 265 | vtkIdType i, j, jOffset, idx, currentOffset = 0; |
| 266 | for (j = 0; j < this->Dim; ++j) |
| 267 | { |
| 268 | jOffset = j * this->Dim; |
| 269 | for (i = 0; i < this->Dim; ++i) |
| 270 | { |
| 271 | idx = i + jOffset; |
| 272 | numElems = this->Offsets[idx]; |
| 273 | this->Offsets[idx] = currentOffset; |
| 274 | currentOffset += numElems; |
| 275 | } |
| 276 | } |
| 277 | this->Offsets[this->Dim * this->Dim] = this->NumCells; |
| 278 | |
| 279 | // We don't need the span space tuple array any more, we have |
| 280 | // offsets and cell ids computed. |
| 281 | delete[] this->Space; |
| 282 | this->Space = nullptr; |
| 283 | } |
| 284 | |
| 285 | namespace |
| 286 | { // begin anonymous namespace |