Exemplo n.º 1
0
void getInfo ( void *obj, info_t *info_ ) {
	VirtualVolume *volume = (VirtualVolume *) obj;
	std::string fmt = volume->getPrintableFormat();

	info_->root_dir   = volume->getROOT_DIR();
	info_->VXL_V      = volume->getVXL_V();
	info_->VXL_H      = volume->getVXL_H();
	info_->VXL_D      = volume->getVXL_D();
	info_->ORG_V      = volume->getORG_V();
	info_->ORG_H      = volume->getORG_H();
	info_->ORG_D      = volume->getORG_D();
	info_->DIM_V      = volume->getDIM_V();
	info_->DIM_H      = volume->getDIM_H();
	info_->DIM_D      = volume->getDIM_D();
	info_->DIM_C      = volume->getDIM_C();
	info_->BYTESxCHAN = volume->getBYTESxCHAN();
	info_->n_active   = volume->getNACtiveChannels();
	info_->active     = volume->getActiveChannels();
	info_->DIM_T      = volume->getDIM_T();
	info_->t0         = volume->getT0();
	info_->t1         = volume->getT1();
}
Exemplo n.º 2
0
//automatically called when current thread is started
void CImport::run()
{
    /**/itm::debug(itm::LEV1, 0, __itm__current__function__);

    try
    {
        timerIO.start();

        /********************* 1) IMPORTING CURRENT VOLUME ***********************
        PRECONDITIONS:
        reimport = true  ==> the volume cannot be directly imported (i.e., w/o the
        additional info provided by the user) or the user explicitly asked for re-
        importing the volume.
        reimport = false ==> the volume is directly importable
        *************************************************************************/
        /**/itm::debug(itm::LEV_MAX, strprintf("importing current volume at \"%s\"", path.c_str()).c_str(), __itm__current__function__);

        // skip nonmatching entries
        QDir dir(path.c_str());
        if( dir.dirName().toStdString().substr(0,3).compare(itm::RESOLUTION_PREFIX) != 0)
            throw RuntimeException(strprintf("\"%s\" is not a valid resolution: the name of the folder does not start with \"%s\"",
                                             path.c_str(), itm::RESOLUTION_PREFIX.c_str() ).c_str());

        if(reimport)
            volumes.push_back(VirtualVolume::instance(path.c_str(), format, AXS_1, AXS_2, AXS_3, VXL_1, VXL_2, VXL_3));
        else
            volumes.push_back(VirtualVolume::instance(path.c_str()));




        /********************* 2) IMPORTING OTHER VOLUMES ***********************
        Importing all the available resolutions within the current volume's
        parent directory.
        *************************************************************************/
        /**/itm::debug(itm::LEV_MAX, "Importing other volumes of the multiresolution octree", __itm__current__function__);
        /* -------------------- detect candidate volumes -----------------------*/
        /**/itm::debug(itm::LEV_MAX, "Detecting volumes that CAN be loaded (let us call them CANDIDATE volumes: the correspondent structures will be destroyed after this step)", __itm__current__function__);
        vector<VirtualVolume*> candidateVols;
        QDir curParentDir(path.c_str());
        curParentDir.cdUp();
        QStringList otherDirs = curParentDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
        for(int k=0; k<otherDirs.size(); k++)
        {
            string path_i = curParentDir.absolutePath().append("/").append(otherDirs.at(k).toLocal8Bit().constData()).toStdString();            
            QDir dir_i(path_i.c_str());

            // skip volumes[0]
            if(dir.dirName() == dir_i.dirName())
                continue;

            // skip nonmatching entries
            if(dir_i.dirName().toStdString().substr(0,3).compare(itm::RESOLUTION_PREFIX) != 0)
                continue;

            /**/itm::debug(itm::LEV_MAX, strprintf("Checking for loadable volume at \"%s\"", path_i.c_str()).c_str(), __itm__current__function__);
            if( !reimport && VirtualVolume::isDirectlyImportable( path_i.c_str()) )
                candidateVols.push_back(VirtualVolume::instance(path_i.c_str()));
            else
                volumes.push_back(VirtualVolume::instance(path_i.c_str(), volumes[0]->getPrintableFormat(),
                                  volumes[0]->getAXS_1(), volumes[0]->getAXS_2(), volumes[0]->getAXS_3(),
                                  volumes[0]->getVXL_1(), volumes[0]->getVXL_2(), volumes[0]->getVXL_3()));
        }
        /* -------------------- import candidate volumes ------------------------*/
        /**/itm::debug(itm::LEV_MAX, "Importing loadable volumes (previously checked)", __itm__current__function__);
        for(int k=0; k<candidateVols.size(); k++)
        {
            int ratio = iim::round(  pow((volumes[0]->getMVoxels() / candidateVols[k]->getMVoxels()),(1/3.0f))  );

             /**/itm::debug(itm::LEV_MAX, strprintf("Importing loadable volume at \"%s\"", candidateVols[k]->getROOT_DIR()).c_str(), __itm__current__function__);
            if( !reimport && VirtualVolume::isDirectlyImportable( candidateVols[k]->getROOT_DIR()) )
                volumes.push_back(VirtualVolume::instance(candidateVols[k]->getROOT_DIR()));
            else
                volumes.push_back(VirtualVolume::instance(candidateVols[k]->getROOT_DIR(),    candidateVols[k]->getPrintableFormat(),
                              volumes[0]->getAXS_1(),       volumes[0]->getAXS_2(),       volumes[0]->getAXS_3(),
                              volumes[0]->getVXL_1()*ratio, volumes[0]->getVXL_2()*ratio, volumes[0]->getVXL_3()*ratio));
        }
        /* -------------------- destroy candidate volumes -----------------------*/
        /**/itm::debug(itm::LEV_MAX, "Destroying candidate volumes", __itm__current__function__);
        for(int k=0; k<candidateVols.size(); k++)
            delete candidateVols[k];
        /* ------------- sort imported volumes by ascending size ---------------*/
        /**/itm::debug(itm::LEV_MAX, "Sorting volumes by ascending size", __itm__current__function__);
        std::sort(volumes.begin(), volumes.end(), sortVolumesAscendingSize);
        /* ---------------------- check imported volumes -----------------------*/
        if(volumes.size() < 2)
            throw RuntimeException(strprintf("%d resolution found at %s: at least two resolutions are needed for the multiresolution mode",
                                             volumes.size(), qPrintable(curParentDir.path()) ).c_str());
        for(int k=0; k<volumes.size()-1; k++)
        {
            if(volumes[k]->getPrintableFormat().compare( volumes[k+1]->getPrintableFormat() ) != 0)
                throw RuntimeException(strprintf("Volumes have different formats at \"%s\"", qPrintable(curParentDir.absolutePath())).c_str());
            if(volumes[k]->getDIM_T() != volumes[k+1]->getDIM_T())
                throw RuntimeException(strprintf("Volumes have different time frames at \"%s\"", qPrintable(curParentDir.absolutePath())).c_str());
        }



        /**************** 3) GENERATING / LOADING VOLUME 3D MAP *****************
        We generate once for all a volume map from lowest-resolution volume.
        *************************************************************************/
        string volMapPath = curParentDir.path().toStdString() + "/" + VMAP_BIN_FILE_NAME;
        if(hasVolumeMapToBeRegenerated(volMapPath.c_str(), "0.9.42", vmapTDimMax, volumes[0]->getDIM_T()) || reimport || regenerateVMap)
        {
            /**/itm::debug(itm::LEV_MAX, "Entering volume's map generation section", __itm__current__function__);

            // check that the lowest resolution does not exceed the maximum allowed size for the volume map
            VirtualVolume* lowestResVol = volumes[0];
            if(lowestResVol->getDIM_H() > vmapXDimMax ||
               lowestResVol->getDIM_V() > vmapYDimMax ||
               lowestResVol->getDIM_D() > vmapZDimMax)
                itm::warning("@TODO: resample along XYZ so as to match the volume map maximum size", __itm__current__function__);

            vmapXDim = lowestResVol->getDIM_H();
            vmapYDim = lowestResVol->getDIM_V();
            vmapZDim = lowestResVol->getDIM_D();
            vmapCDim = lowestResVol->getDIM_C();
            // if the number of time frames exceeds the maximum, we put only the first vmapTDimMax in the volume map
            vmapTDim = std::min(vmapTDimMax, lowestResVol->getDIM_T());

            // generate volume map
            lowestResVol->setActiveFrames(0, vmapTDimMax -1);
            vmapData = lowestResVol->loadSubvolume_to_UINT8();
            FILE *volMapBin = fopen(volMapPath.c_str(), "wb");
            if(!volMapBin)
                throw RuntimeException(strprintf("Cannot write volume map at \"%s\". Please check your write permissions.", volMapPath.c_str()).c_str());
            uint16 verstr_size = static_cast<uint16>(strlen(itm::version.c_str()) + 1);
            fwrite(&verstr_size, sizeof(uint16), 1, volMapBin);
            fwrite(itm::version.c_str(), verstr_size, 1, volMapBin);
            fwrite(&vmapTDim,  sizeof(uint32), 1, volMapBin);
            fwrite(&vmapCDim,  sizeof(uint32), 1, volMapBin);
            fwrite(&vmapYDim,  sizeof(uint32), 1, volMapBin);
            fwrite(&vmapXDim,  sizeof(uint32), 1, volMapBin);
            fwrite(&vmapZDim,  sizeof(uint32), 1, volMapBin);
            size_t vmapSize = ((size_t)vmapYDim)*vmapXDim*vmapZDim*vmapCDim*vmapTDim;
            fwrite(vmapData, vmapSize, 1, volMapBin);
            fclose(volMapBin);
        }
        else
        {
            /**/itm::debug(itm::LEV_MAX, "Entering volume's map loading section", __itm__current__function__);

            // load volume map
            FILE *volMapBin = fopen(volMapPath.c_str(), "rb");
            uint16 verstr_size;
            if(!volMapBin)
                throw RuntimeException(strprintf("Cannot read volume map at \"%s\". Please check your read permissions.", volMapPath.c_str()).c_str());
            if(!fread(&verstr_size, sizeof(uint16), 1, volMapBin))
                throw RuntimeException("Unable to read volume map file (<version_size> field). Please delete the volume map and re-open the volume.");
            char ver[1024];
            if(!fread(ver, verstr_size, 1, volMapBin))
                throw RuntimeException("Unable to read volume map file (<version> field). Please delete the volume map and re-open the volume.");
            if(fread(&vmapTDim, sizeof(uint32), 1, volMapBin) != 1)
                throw RuntimeException("Unable to read volume map file (<vmapTDim> field). Please delete the volume map and re-open the volume.");
            if(fread(&vmapCDim, sizeof(uint32), 1, volMapBin) != 1)
                throw RuntimeException("Unable to read volume map file (<vmapCDim> field). Please delete the volume map and re-open the volume.");
            if(fread(&vmapYDim, sizeof(uint32), 1, volMapBin) != 1)
                throw RuntimeException("Unable to read volume map file (<vmapYDim> field). Please delete the volume map and re-open the volume.");
            if(fread(&vmapXDim,  sizeof(uint32), 1, volMapBin)!= 1)
                throw RuntimeException("Unable to read volume map file (<vmapXDim> field). Please delete the volume map and re-open the volume.");
            if(fread(&vmapZDim,  sizeof(uint32), 1, volMapBin)!= 1)
                throw RuntimeException("Unable to read volume map file (<vmapZDim> field). Please delete the volume map and re-open the volume.");
            size_t vmapSize = ((size_t)vmapYDim)*vmapXDim*vmapZDim*vmapCDim*vmapTDim;
            vmapData = new uint8[vmapSize];
            if(fread(vmapData, vmapSize, 1, volMapBin) != 1)
                throw RuntimeException("Unable to read volume map file (<vmapData> field). Please delete the volume map and re-open the volume.");
            fclose(volMapBin);


            //--- Alessandro 29/09/2013: checking that the loaded vmap corresponds to one of the loaded volumes
//            /**/itm::debug(itm::LEV_MAX, "checking that the loaded vmap corresponds to one of the loaded volumes", __itm__current__function__);
//            bool check_passed = false;
//            for(int i=0; i<volumes.size() && !check_passed; i++)
//                if(volumes[i]->getDIM_V() == vmapYDim  &&
//                   volumes[i]->getDIM_H() == vmapXDim  &&
//                   volumes[i]->getDIM_D() == vmapZDim  &&
//                   volumes[i]->getDIM_C() == vmapCDim)
//                    check_passed = true;
//            if(!check_passed)
//                throw RuntimeException(QString("Volume map stored at \"").append(volMapPath.c_str()).append("\" does not correspond to any of the loaded resolutions. Please delete or regenerate the volume map.").toStdString().c_str());

        }

        //everything went OK
        emit sendOperationOutcome(0, timerIO.elapsed());

        /**/itm::debug(itm::LEV1, "EOF", __itm__current__function__);
    }
    catch( iim::IOException& exception)  {reset(); emit sendOperationOutcome(new RuntimeException(exception.what()));}
    catch( iom::exception& exception)    {reset(); emit sendOperationOutcome(new RuntimeException(exception.what()));}
    catch( RuntimeException& exception)  {reset(); emit sendOperationOutcome(new RuntimeException(exception.what()));}
    catch(const char* error)             {reset(); emit sendOperationOutcome(new RuntimeException(error));}
    catch(...)                           {reset(); emit sendOperationOutcome(new RuntimeException("Unknown error occurred"));}
}