VolumeCollection* RawVoxVolumeReader::read(const std::string &url)
throw (tgt::CorruptedFileException, tgt::IOException, std::bad_alloc)
{
    VolumeURL origin(url);
    std::string fileName = origin.getPath();

    LINFO("Reading file " << fileName);

    std::fstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
    if (!fin.good())
        throw tgt::IOException();

    RawVoxHeader header;
    fin.read(reinterpret_cast<char*>(&header), sizeof(header));
    svec3 dimensions = svec3(header.sizeX_, header.sizeY_, header.sizeZ_);

    if(header.magic_ != 1381388120) {
        throw tgt::CorruptedFileException("Wrong magic number.");
    }

    VolumeRAM* dataset;
    switch(header.bitsPerVoxel_) {
    case 8:
        LINFO("Reading 8 bit dataset");
        dataset = new VolumeRAM_UInt8(dimensions);
        break;
    case 16:
        LINFO("Reading 16 bit dataset");
        dataset = new VolumeRAM_UInt16(dimensions);
        break;
    case 32:
        LINFO("Reading 32 bit (float) dataset");
        dataset = new VolumeRAM_Float(dimensions);
        break;
    default:
        LERROR("Unknown bpp!");
        throw tgt::UnsupportedFormatException("Unexpected bpp.");
    }

    fin.read(reinterpret_cast<char*>(dataset->getData()), dataset->getNumBytes());

    if ( fin.eof() ) {
        delete dataset;
        throw tgt::CorruptedFileException();
    }

    fin.close();

    VolumeCollection* volumeCollection = new VolumeCollection();
    Volume* volumeHandle = new Volume(dataset, vec3(1.0f), vec3(0.0f));
    oldVolumePosition(volumeHandle);
    volumeHandle->setOrigin(fileName);
    volumeCollection->add(volumeHandle);

    return volumeCollection;
}
void mexFunction(int nlhs, mxArray  *plhs[], 
                 int nrhs, const mxArray  *prhs[] )

{    mxArray  *blk_cell_pr;
     double   *A,  *B,  *AI, *BI, *blksize; 
     mwIndex  *irA, *jcA, *irB, *jcB;
     int      *cumblksize, *blknnz;
     int       mblk, isspA, isspB, iscmpA;

     mwIndex  subs[2];
     mwSize   nsubs=2; 
     int      m, n, n2, nsub, k, index, numblk, NZmax, type; 
     double   r2; 

/* CHECK FOR PROPER NUMBER OF ARGUMENTS */

   if (nrhs < 2){
      mexErrMsgTxt("mexsvec: requires at least 2 input arguments."); }
   if (nlhs > 1){ 
      mexErrMsgTxt("mexsvec: requires 1 output argument."); }

/* CHECK THE DIMENSIONS */

    mblk = mxGetM(prhs[0]); 
    if (mblk > 1) { 
       mexErrMsgTxt("mexsvec: blk can have only 1 row."); }
    m = mxGetM(prhs[1]); 
    n = mxGetN(prhs[1]); 
    if (m != n) { 
       mexErrMsgTxt("mexsvec: matrix must be square."); }

    subs[0] = 0; subs[1] = 1;
    index = mxCalcSingleSubscript(prhs[0],nsubs,subs); 
    blk_cell_pr = mxGetCell(prhs[0],index);
    numblk  = mxGetN(blk_cell_pr);
    blksize = mxGetPr(blk_cell_pr); 
    if (numblk == 1) { 
       n2 = n*(n+1)/2; 
    } else { 
       cumblksize = mxCalloc(numblk+1,sizeof(int)); 
       blknnz = mxCalloc(numblk+1,sizeof(int)); 
       cumblksize[0] = 0; blknnz[0] = 0; 
       n = 0; n2 = 0; 
       for (k=0; k<numblk; ++k) {
           nsub = (int) blksize[k];
           n  += nsub; 
           n2 += nsub*(nsub+1)/2;  
           cumblksize[k+1] = n; 
           blknnz[k+1] = n2;  }
    }
    /***** assign pointers *****/
    A = mxGetPr(prhs[1]); 
    isspA = mxIsSparse(prhs[1]);
    iscmpA = mxIsComplex(prhs[1]);   
    if (isspA) {  irA = mxGetIr(prhs[1]); 
                  jcA = mxGetJc(prhs[1]); 
                  NZmax = mxGetNzmax(prhs[1]);  
    } else { 
       NZmax = n2; 
    }
    if (iscmpA) { AI = mxGetPi(prhs[1]); }
    if ((numblk > 1) & (!isspA)) {
       mexErrMsgTxt("mexsvec: matrix must be sparse for numblk > 1"); }
    if (nrhs > 2) { 
       if (mxGetM(prhs[2])>1) { isspB = (int)*mxGetPr(prhs[2]); }
       else if (NZmax < n2/2) { isspB = 1; }
       else                   { isspB = 0; } 
    } else {        
       if (NZmax < n2/2) { isspB = 1; }
       else              { isspB = 0; }
    } 
    if (nrhs > 3) { type = (int)*mxGetPr(prhs[3]); } 
    else          { type = 0; } 
    /***** create return argument *****/
    if (isspB) {
       if (iscmpA) { 
          plhs[0] = mxCreateSparse(n2,1,NZmax,mxCOMPLEX); 
       } else { 
          plhs[0] = mxCreateSparse(n2,1,NZmax,mxREAL); 
       }
       B = mxGetPr(plhs[0]);
       irB = mxGetIr(plhs[0]); 
       jcB = mxGetJc(plhs[0]); 
       jcB[0] = 0; 
    } else {
       if (iscmpA) { 
          plhs[0] = mxCreateDoubleMatrix(n2,1,mxCOMPLEX); 
       } else { 
          plhs[0] = mxCreateDoubleMatrix(n2,1,mxREAL); 
       }
       B = mxGetPr(plhs[0]);  
    } 
    if (iscmpA) { BI = mxGetPi(plhs[0]); }   
    /***** Do the computations in a subroutine *****/
    r2 = sqrt(2); 
    if (type == 0) { 
       if (iscmpA) {
         if (numblk == 1) { 
            svec1cmp(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI);  }
         else {
            svec2cmp(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI); 
         }
       } else { 
         if (numblk == 1) { 
            svec1(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB);  }
         else {
            svec2(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB); 
         }
       }
    } else {
       if (iscmpA) { 
         if (numblk == 1) { 
            svec3cmp(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI);  }
         else {
            svec4cmp(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI); 
	 }
       } else {
         if (numblk == 1) { 
            svec3(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB);  }
         else {
            svec4(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB); 
	 }
       }
    }
    return;
 }
Exemple #3
0
void VolumeGL::generateTexture(const Volume* volume) 
        throw (VoreenException, std::bad_alloc) 
{
    if (!tgt::hand(tgt::greaterThan(volume->getDimensions(), svec3(1)))) {
        std::stringstream message;
        message << "OpenGL volumes must have a size greater than one in all dimensions. Actual size: " << volume->getDimensions();
        LERROR(message.str());
        throw VoreenException(message.str());
    }
    
    if (!GpuCaps.is3DTexturingSupported()) {
        std::string message = "3D textures apparently not supported by the OpenGL driver";
        LERROR(message);
        throw VoreenException(message);
    }

    if (!GpuCaps.isNpotSupported() && !isPowerOfTwo(getDimensions())) {
        std::string message = "Non-power-of-two textures apparently not supported by the OpenGL driver";
        LERROR(message);
        throw VoreenException(message);
    }

    //
    // Determine GL format
    //
    GLint format;         ///< The format of textures which will are created.
    GLint internalFormat; ///< The internal format of the textures which are created.
    GLenum dataType;      ///< The data type of the textures which are created.
    // scalar
    if (dynamic_cast<const VolumeAtomic<uint8_t>*>(volume)) {
        format = GL_ALPHA;
        internalFormat = GL_ALPHA8;
        dataType = GL_UNSIGNED_BYTE;
    }
    else if (dynamic_cast<const VolumeAtomic<int8_t>*>(volume)) {
        format = GL_ALPHA;
        internalFormat = GL_ALPHA8;
        dataType = GL_BYTE;
    }
    else if (dynamic_cast<const VolumeAtomic<uint16_t>*>(volume)) {
        format = GL_ALPHA;
        internalFormat = GL_ALPHA16;
        dataType = GL_UNSIGNED_SHORT;
    }
    else if (dynamic_cast<const VolumeAtomic<int16_t>*>(volume)) {
        format = GL_ALPHA;
        internalFormat = GL_ALPHA16;
        dataType = GL_SHORT;
    }
    else if (dynamic_cast<const VolumeAtomic<uint32_t>*>(volume)) {
        format = GL_ALPHA;
        internalFormat = GL_ALPHA;
        dataType = GL_UNSIGNED_INT;
    }
    else if (dynamic_cast<const VolumeAtomic<int32_t>*>(volume)) {
        format = GL_ALPHA;
        internalFormat = GL_ALPHA;
        dataType = GL_INT;
    }
    else if (dynamic_cast<const VolumeAtomic<uint64_t>*>(volume)) {
        std::string message = "VolumeUInt64 not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    else if (dynamic_cast<const VolumeAtomic<int64_t>*>(volume)) {
        std::string message = "VolumeInt64 not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    else if (dynamic_cast<const VolumeAtomic<float>*>(volume)) {
        format = GL_ALPHA;
        internalFormat = GL_ALPHA;
        dataType = GL_FLOAT;
    }
    else if (dynamic_cast<const VolumeAtomic<double>*>(volume)) {
        std::string message = "VolumeDouble not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    // vec2
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector2<uint8_t> >*>(volume)) {
        format = GL_LUMINANCE_ALPHA;
        internalFormat = GL_LUMINANCE_ALPHA;
        dataType = GL_UNSIGNED_BYTE;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector2<int8_t> >*>(volume)) {
        format = GL_LUMINANCE_ALPHA;
        internalFormat = GL_LUMINANCE_ALPHA;
        dataType = GL_BYTE;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector2<uint16_t> >*>(volume)) {
        format = GL_LUMINANCE_ALPHA;
        internalFormat = GL_LUMINANCE_ALPHA;
        dataType = GL_UNSIGNED_SHORT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector2<int16_t> >*>(volume)) {
        format = GL_LUMINANCE_ALPHA;
        internalFormat = GL_LUMINANCE_ALPHA;
        dataType = GL_SHORT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector2<uint32_t> >*>(volume)) {
        format = GL_LUMINANCE_ALPHA;
        internalFormat = GL_LUMINANCE_ALPHA;
        dataType = GL_UNSIGNED_INT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector2<int32_t> >*>(volume)) {
        format = GL_LUMINANCE_ALPHA;
        internalFormat = GL_LUMINANCE_ALPHA;
        dataType = GL_INT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector2<uint64_t> >*>(volume)) {
        std::string message = "Volume2xUInt64 not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector2<int64_t> >*>(volume)) {
        std::string message = "Volume2xInt64 not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector2<float> >*>(volume)) {
        format = GL_LUMINANCE_ALPHA;
        internalFormat = GL_LUMINANCE_ALPHA;
        dataType = GL_FLOAT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector2<double> >*>(volume)) {
        std::string message = "Volume2xDouble not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    // vec3
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector3<uint8_t> >*>(volume)) {
        format = GL_RGB;
        internalFormat = GL_RGB8;
        dataType = GL_UNSIGNED_BYTE;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector3<int8_t> >*>(volume)) {
        format = GL_RGB;
        internalFormat = GL_RGB8;
        dataType = GL_BYTE;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector3<uint16_t> >*>(volume)) {
        format = GL_RGB;
        internalFormat = GL_RGB16;
        dataType = GL_UNSIGNED_SHORT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector3<int16_t> >*>(volume)) {
        format = GL_RGB;
        internalFormat = GL_RGB16;
        dataType = GL_SHORT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector3<uint32_t> >*>(volume)) {
        format = GL_RGB;
        internalFormat = GL_RGB;
        dataType = GL_UNSIGNED_INT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector3<int32_t> >*>(volume)) {
        format = GL_RGB;
        internalFormat = GL_RGB;
        dataType = GL_INT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector3<uint64_t> >*>(volume)) {
        std::string message = "Volume3xUInt64 not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector3<int64_t> >*>(volume)) {
        std::string message = "Volume3xInt64 not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector3<float> >*>(volume)) {
        format = GL_RGB;
        internalFormat = GL_RGB;
        dataType = GL_FLOAT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector3<double> >*>(volume)) {
        std::string message = "Volume3xDouble not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    // vec4
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector4<uint8_t> >*>(volume)) {
        format = GL_RGBA;
        internalFormat = GL_RGBA8;
        dataType = GL_UNSIGNED_BYTE;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector4<int8_t> >*>(volume)) {
        format = GL_RGBA;
        internalFormat = GL_RGBA8;
        dataType = GL_BYTE;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector4<uint16_t> >*>(volume)) {
        format = GL_RGBA;
        internalFormat = GL_RGBA16;
        dataType = GL_UNSIGNED_SHORT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector4<int16_t> >*>(volume)) {
        format = GL_RGBA;
        internalFormat = GL_RGBA16;
        dataType = GL_SHORT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector4<uint32_t> >*>(volume)) {
        format = GL_RGBA;
        internalFormat = GL_RGBA;
        dataType = GL_UNSIGNED_INT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector4<int32_t> >*>(volume)) {
        format = GL_RGBA;
        internalFormat = GL_RGBA;
        dataType = GL_INT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector4<uint64_t> >*>(volume)) {
        std::string message = "Volume4xUInt64 not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector4<int64_t> >*>(volume)) {
        std::string message = "Volume4xInt64 not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector4<float> >*>(volume)) {
        format = GL_RGBA;
        internalFormat = GL_RGBA;
        dataType = GL_FLOAT;
    }
    else if (dynamic_cast<const VolumeAtomic<tgt::Vector4<double> >*>(volume)) {
        std::string message = "Volume4xDouble not supported as OpenGL volume.";
        LERROR(message);
        throw VoreenException(message);
    }
    // special types (TODO: extract from here)
#ifdef VRN_MODULE_FLOWREEN
    else if (dynamic_cast<const VolumeFlow3D*>(volume)) {
        format = GL_RGB;
        internalFormat = GL_RGB16;
        dataType = GL_FLOAT;
    }
#endif
    else {
        LERROR("unknown or unsupported volume type");
    }


    //
    // Create texture
    //
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    VolumeTexture* vTex = 0;

    tgt::vec3* temp2 = 0;
#ifdef VRN_MODULE_FLOWREEN
    // Textures containing flow data need to contain data
    // within range [0.0, 1.0], so the values have to be mapped
    const VolumeFlow3D* flowTex = dynamic_cast<const VolumeFlow3D*>(volume);
    if (flowTex) {
        const float minValue = flowTex->getMinValue();
        const float maxValue = flowTex->getMaxValue();
        const float range = (maxValue - minValue);

        const tgt::vec3* const voxels = flowTex->voxel();
        temp2 = new tgt::vec3[volume->getNumVoxels()];
        for (size_t i = 0; i < volume->getNumVoxels(); ++i) {
            if (voxels[i] != tgt::vec3::zero) {
                temp2[i].x = (voxels[i].x - minValue) / range;
                temp2[i].y = (voxels[i].y - minValue) / range;
                temp2[i].z = (voxels[i].z - minValue) / range;
            }
        }
    }
#endif  


    // use temp data if this was created
    if (temp2) {
        vTex = new VolumeTexture(reinterpret_cast<GLubyte*>(temp2), volume->getDimensionsWithBorder(),
        format, internalFormat, dataType, tgt::Texture::LINEAR);
    }
    else {
        vTex = new VolumeTexture(static_cast<const GLubyte*>(volume->getData()),
            volume->getDimensionsWithBorder(),
            format, internalFormat, dataType, tgt::Texture::LINEAR);
    }

    LGL_ERROR;

    vTex->bind();
    if (volume->getData())
        vTex->uploadTexture();

    // set texture wrap to clamp
    vTex->setWrapping(tgt::Texture::CLAMP);

    LGL_ERROR;

    // delete temporary data that has eventually be created
    delete[] temp2;

    // prevent deleting twice
    vTex->setPixelData(0);

    // append to internal data structure
    texture_ = vTex;

    LGL_ERROR;
}