+
+ e.strIPP = (*i).first;
+ e.file = *(((*i).second)->begin()); // all the gdcm::File of a given xcm item *have* the same IPP; first one is enough
+ e.dist=0.0;
+ vectElem.push_back(e);
+ }
+ sortVectElem(&vectElem);
+
+ // now, create the final std::map !
+ // final_xcm<to_str(e.dist , xcm[e.strIPP]>
+ // xcm = final_xcm;
+ // check what we need to free !
+
+ int dist;
+ char c_dist[100];
+ std::string str_dist;
+ int lgr=vectElem.size();
+ GDCM_NAME_SPACE::XCoherentFileSetmap final_xcm;
+ for (int i2=0; i2<lgr; i2++)
+ {
+ dist = (vectElem[i2].dist*1000);
+ sprintf(c_dist,"%010d",dist);
+ str_dist = c_dist;
+/*
+ std::cout << "dist " << vectElem[i2].dist
+ << " str_dist " << str_dist
+ << " IPP " << vectElem[i2].strIPP
+ << std::endl;
+
+*/
+ final_xcm[str_dist] = xcm[vectElem[i2].strIPP];
+ }
+
+ /// \TODO : check what needs to be cleared // JPR
+
+ xcm = final_xcm;
+
+ }
+
+
+bool vtkGdcm4DSplitter::sortVectElem(std::vector<ELEM> *fileList)
+{
+//based on Jolinda Smith's algorithm
+//
+// NOTE : if you need to use Jolinda Smith's algorithm, get the one inside gdcm::SerieHelper
+// this one is a light version.
+
+//Tags always use the same coordinate system, where "x" is left
+//to right, "y" is posterior to anterior, and "z" is foot to head (RAH).
+
+ //iop is calculated based on the file file
+ float cosines[6];
+ double normal[3];
+ double ipp[3];
+ double dist;
+ double min = 0, max = 0;
+ bool first = true;
+
+ //double ZSpacing; // useless here! // JPR
+ bool DirectOrder = true; // remove it!
+
+ // ZSpacing = -1.0; // will be updated if process doesn't fail
+
+ //std::multimap<double,File *> distmultimap; // JPR
+ std::multimap<double,ELEM> distmultimap; // JPR
+
+ // Use a multimap to sort the distances from 0,0,0
+ //for ( FileList::const_iterator // JPR
+ for ( std::vector<ELEM>::iterator // JPR
+ it = fileList->begin();
+ it != fileList->end(); ++it )
+ {
+ //gdcmDebugMacro("deal with " << (*it)->file->GetFileName() );
+ if ( first )
+ {
+ (*it).file->GetImageOrientationPatient( cosines );
+
+ // The "Image Orientation Patient" tag gives the direction cosines
+ // for the rows and columns for the three axes defined above.
+ // Typical axial slices will have a value 1/0/0/0/1/0:
+ // rows increase from left to right,
+ // columns increase from posterior to anterior. This is your everyday
+ // "looking up from the bottom of the head with the eyeballs up" image.
+
+ // The "Image Position Patient" tag gives the coordinates of the first
+ // voxel in the image in the "RAH" coordinate system, relative to some
+ // origin.
+
+ // First, calculate the slice normal from IOP :
+
+ // You only have to do this once for all slices in the volume. Next,
+ // for each slice, calculate the distance along the slice normal
+ // using the IPP ("Image Position Patient") tag.
+ // ("dist" is initialized to zero before reading the first slice) :
+
+ normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
+ normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
+ normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
+
+ // For each slice (here : the first), calculate the distance along
+ // the slice normal using the IPP tag
+
+ ipp[0] = (*it).file->GetXOrigin();
+ ipp[1] = (*it).file->GetYOrigin();
+ ipp[2] = (*it).file->GetZOrigin();
+
+ dist = 0;
+ for ( int i = 0; i < 3; ++i )
+ {
+ dist += normal[i]*ipp[i];
+ }
+
+ //gdcmDebugMacro("dist : " << dist);
+ distmultimap.insert(std::pair<const double,ELEM>(dist, *it));
+
+ max = min = dist;
+ first = false;
+ }
+ else
+ {
+ // Next, for each slice, calculate the distance along the slice normal
+ // using the IPP tag
+ ipp[0] = (*it).file->GetXOrigin();
+ ipp[1] = (*it).file->GetYOrigin();
+ ipp[2] = (*it).file->GetZOrigin();
+
+ dist = 0;
+ for ( int i = 0; i < 3; ++i )
+ {
+ dist += normal[i]*ipp[i];
+ }
+
+ (*it).dist = dist; // JPR
+
+ distmultimap.insert(std::pair<const double,ELEM>(dist, *it));
+ //gdcmDebugMacro("dist : " << dist);
+ min = (min < dist) ? min : dist;
+ max = (max > dist) ? max : dist;
+ }