------------------------------------------------------------------------------ Sorts the vtkImageSliceCollection by layer number. Smaller layer numbers are first. Layer numbers can be any integer value.
| 87 | // Sorts the vtkImageSliceCollection by layer number. Smaller layer |
| 88 | // numbers are first. Layer numbers can be any integer value. |
| 89 | void vtkImageSliceCollection::Sort() |
| 90 | { |
| 91 | // Create a temporary array of pointers to images |
| 92 | int numElems = this->GetNumberOfItems(); |
| 93 | vtkImageSliceLayerPair defaultLayerArray[8]; |
| 94 | vtkImageSliceLayerPair* layerArray = defaultLayerArray; |
| 95 | if (numElems > 8) |
| 96 | { |
| 97 | layerArray = new vtkImageSliceLayerPair[numElems]; |
| 98 | } |
| 99 | |
| 100 | // Start at the beginning of the collection |
| 101 | vtkCollectionSimpleIterator ait; |
| 102 | this->InitTraversal(ait); |
| 103 | |
| 104 | // Fill the image array with the items in the collection |
| 105 | for (int ii = 0; ii < numElems; ii++) |
| 106 | { |
| 107 | vtkImageSlice* image = this->GetNextImage(ait); |
| 108 | layerArray[ii].image = image; |
| 109 | layerArray[ii].layer = image->GetProperty()->GetLayerNumber(); |
| 110 | } |
| 111 | |
| 112 | // The collection will be small (often with n=2) so do a brute-force |
| 113 | // selection sort, which also keeps items with the same layer number |
| 114 | // in the same order as before the sort. |
| 115 | for (int i = 0; i < numElems - 1; i++) |
| 116 | { |
| 117 | int imin = i; |
| 118 | int lmin = layerArray[imin].layer; |
| 119 | int j = i + 1; |
| 120 | |
| 121 | do |
| 122 | { |
| 123 | int l = layerArray[j].layer; |
| 124 | if (l < lmin) |
| 125 | { |
| 126 | imin = j; |
| 127 | lmin = l; |
| 128 | } |
| 129 | } while (++j < numElems); |
| 130 | |
| 131 | vtkImageSliceLayerPair t = layerArray[imin]; |
| 132 | layerArray[imin] = layerArray[i]; |
| 133 | layerArray[i] = t; |
| 134 | } |
| 135 | |
| 136 | // Now move the items around in the linked list - |
| 137 | // keep the links the same, but swap around the items |
| 138 | vtkCollectionElement* elem = this->Top; |
| 139 | for (int jj = 0; jj < numElems; jj++) |
| 140 | { |
| 141 | elem->Item = layerArray[jj].image; |
| 142 | elem = elem->Next; |
| 143 | } |
| 144 | |
| 145 | if (layerArray != defaultLayerArray) |
| 146 | { |
no test coverage detected