Example #1
0
void wrl::constraint::calc_pos(double& x, double& y, const vec3& p,
                               const vec3& v) const
{
    double t = (wvector(T) * zvector(T) - p * zvector(T)) / (v * zvector(T));
    vec3 q = p + v * t - wvector(T);

    double xx = q * xvector(T);
    double yy = q * yvector(T);

    x = snap(xx, grid_d);
    y = snap(yy, grid_d);
}
Example #2
0
void wrl::constraint::calc_rot(double& a, double& d, const vec3& p,
                               const vec3& v) const
{
    double t = (wvector(T) * zvector(T) - p * zvector(T)) / (v * zvector(T));
    vec3 q = p + v * t - wvector(T);

    double aa = to_degrees(atan2(q * yvector(T), q * xvector(T)));
    double dd = length(q);

    a = snap(aa, double(grid_a));
    d = snap(dd,       (grid_d));
}
Example #3
0
void wrl::atom::save(app::node node)
{
    const vec3 x = xvector(default_M);
    const vec3 y = yvector(default_M);
    const vec3 z = zvector(default_M);
    const vec3 p = wvector(default_M);

    const quat q(mat3(x, y, z));

    // Add the entity transform to this element.

    app::node n;

    n = app::node("rot_x"); n.set_f(q[0]); n.insert(node);
    n = app::node("rot_y"); n.set_f(q[1]); n.insert(node);
    n = app::node("rot_z"); n.set_f(q[2]); n.insert(node);
    n = app::node("rot_w"); n.set_f(q[3]); n.insert(node);

    n = app::node("pos_x"); n.set_f(p[0]); n.insert(node);
    n = app::node("pos_y"); n.set_f(p[1]); n.insert(node);
    n = app::node("pos_z"); n.set_f(p[2]); n.insert(node);

    if (body_id)
    {
        n = app::node("body");
        n.set_i(body_id);
        n.insert(node);
    }

    save_params(node);
}
Example #4
0
bool wrl::constraint::point(const vec3& p, const vec3& v, mat4& A)
{
    if (to_degrees(acos(mouse_v * v)) > 1.0)
    {
        if (mode)
        {
            double a;
            double d;

            calc_rot(a, d, p, v);

            if (fabs(a - mouse_a) > 0.0 || fabs(d - mouse_d) > 0.0)
            {
                A = translation( wvector(T))
                    *    rotation( zvector(T), to_radians(a - mouse_a))
                    * translation(-wvector(T));

                return true;
            }
        }
        else
        {
            double x;
            double y;

            calc_pos(x, y, p, v);

            if (fabs(x - mouse_x) > 0.0 || fabs(y - mouse_y) > 0.0)
            {
                A = translation(xvector(T) * (x - mouse_x)
                                + yvector(T) * (y - mouse_y));
                return true;
            }
        }
    }
    return false;
}
Example #5
0
// "readImage()": reads a 3D image
ValueType niftiManager::readImage( const std::string& imageFilenameRef, std::vector<std::vector<std::vector<float> > >* imagePointer ) const
{
    std::vector<std::vector<std::vector<float> > >& image( *imagePointer );
    std::string imageFilename( imageFilenameRef );
    std::string imageFilenameZipped( imageFilenameRef );

    std::string extension( boost::filesystem::path(imageFilename).extension().string() );


    if ( extension == ".gz" )
    {
        imageFilename.resize(imageFilename.size()-3);
        // Variables for calling system commands
        std::string sysCommand( "gzip -dcf " + imageFilenameZipped + " > " + imageFilename );
        int success(system(sysCommand.c_str()));
    }
    else if ( extension == getFileExtension( ETFull ) )
    {
    }
    else
    {
        std::cerr << "File \"" << imageFilenameZipped << "\" has no recognized extension (\"" << extension << "\") stopping." << std::endl;
        return VTError;
    }

    nifti_image*  niftiImage = NULL;

    boost::mutex& ioMutex(boost::detail::thread::singleton<boost::mutex>::instance()) ;
    ioMutex.lock();
    {
        niftiImage = nifti_image_read(imageFilename.c_str(), 1);
    }
    ioMutex.unlock();

    if( !niftiImage )
    {
        std::cerr<< "ERROR @ niftiManager::readImage(): there was an error calling nifti_image_read() on image file "<< imageFilename <<std::endl;
        return VTError;
    }

    if( niftiImage->ndim > 3 )
    {
        std::cerr << "ERROR @ niftiManager::readImage(): nifti file has more than 3 dimensions (" << niftiImage->ndim <<"): "<< niftiImage->nx <<" "<< niftiImage->ny <<" "<< niftiImage->nz <<" "<< niftiImage->nt <<" "<< niftiImage->nu <<" "<< niftiImage->nv <<std::endl;
        return VTError;
    }
    const size_t repType( niftiImage->datatype );
    const size_t repByteSize( niftiImage->nbyper );
    const size_t dataSize( niftiImage->nvox );

    ValueType imageValueType;

    if(repType == DT_UINT8 )
    {
        imageValueType = VTUINT8;
    }
    else if(repType == DT_FLOAT32 )
    {
        imageValueType = VTFloat32;
    }
    else
    {
        std::cerr<< "ERROR @ niftiManager::readImage(): image representation type not recognized (neither UINT8 nor FLOAT32)" << std::endl;
        return VTError;
    }

    const size_t dimx( niftiImage->nx );
    const size_t dimy( niftiImage->ny );
    const size_t dimz( niftiImage->nz);

    image.clear();
    {
        std::vector<float> zvector(dimz,0);
        std::vector<std::vector<float> >yzmatrix(dimy,zvector);
        image.resize(dimx,yzmatrix);
    }
    void* niftiData(niftiImage->data);

    for (int i=0 ; i<dimz ; ++i)
    {
        for (int j=0 ; j<dimy ; ++j)
        {
            for (int k=0 ; k<dimx ; ++k)
            {
                size_t voxOffset( (i*dimy*dimx) + (j*dimx) + k );
                if( voxOffset >= dataSize )
                {
                    std::cerr<< "ERROR @ niftiManager::readImage():: pointer offset was too high when loading nifti data" << std::endl;
                    return VTError;
                }
                size_t byteOffset( voxOffset * repByteSize );
                void* dataPos = static_cast<unsigned char*>(niftiData) + byteOffset;

                if (repType == DT_UINT8)
                {
                    unsigned char datapoint = *(static_cast<unsigned char*>(dataPos));
                    image[k][j][i] = (float)datapoint;
                }
                else if (repType == DT_FLOAT32)
                {
                    float datapoint = *(static_cast<float*>(dataPos));
                    image[k][j][i] = datapoint;
                }
                else
                {
                    std::cerr<< "ERROR @ niftiManager::readImage(): image representation type not recognized (neither UINT8 nor FLOAT32)" << std::endl;
                    return VTError;
                }
            }
        }
    }
    // clean up
    nifti_image_free( niftiImage );

    if ( extension == ".gz" )
    {
        // Variables for calling system commands
        std::string sysCommand( "rm -f " + imageFilename );
        int success(system(sysCommand.c_str()));
    }
    return imageValueType;
}// end niftiManager::readImage() -----------------------------------------------------------------