Func* Func::clone() const { Func* f = new (allocFuncMem( m_name, m_numParams, isClosureBody() || isGeneratorFromClosure() )) Func(*this); f->initPrologues(m_numParams, isGenerator()); f->m_funcId = InvalidFuncId; return f; }
void Config::cayley( unsigned int size_log2 ) { m_function_tsize_log2 = size_log2; m_function_tsize = 1<<m_function_tsize_log2; m_function_slices = m_function_tsize; allocFuncMem(); std::cerr << __func__ << ": sampling function "; for(int k=0; k<m_function_tsize; k++) { for(int j=0; j<m_function_tsize; j++) { for(int i=0; i<m_function_tsize; i++) { double x = 2.0*i/m_function_tsize-1.0; double y = 2.0*j/m_function_tsize-1.0; double z = 2.0*k/m_function_tsize-1.0; #if 0 // octdong x *= 1.1; y *= 1.1; z *= 1.1; float f = x*x*x + x*x*z*z - y*y; #else // cayley float f = 1.0 - 16*x*y*z - 4*x*x - 4*y*y - 4*z*z; #endif switch( m_function_format ) { case FUNC_UNSIGNED_BYTE: ((unsigned char*)m_function_data_)[k*m_function_tsize*m_function_tsize + j*m_function_tsize + i ] = (unsigned char)std::min(255.0f,std::max(0.0f,100.0f*f+127.0f) ); break; case FUNC_UNSIGNED_SHORT: ((unsigned short*)m_function_data_)[k*m_function_tsize*m_function_tsize + j*m_function_tsize + i ] = (unsigned short)std::min(65535.0f,std::max(0.0f,20000.0f*f+32767.0f) ); break; case FUNC_FLOAT: ((float*)m_function_data_)[k*m_function_tsize*m_function_tsize + j*m_function_tsize + i ] = f; break; default: exit( EXIT_FAILURE ); } } } if( (40*k)/m_function_tsize != (40*(k+1))/m_function_tsize ) std::cerr << '.'; } std::cerr << "\n"; }
void Config::parseParFile( const std::string& filename, unsigned int downsample_log2 ) { unsigned int downsample = 1u<<downsample_log2; string path = getPath( filename ); ifstream load_par( filename.c_str() ); if(!load_par.good()) { cerr << __func__ << ": Failed to open \"" << filename << "\" for reading.\n"; exit( EXIT_FAILURE ); } int function_real_width = 0; int function_real_height = 0; int function_real_slices = 0; m_function_xscale = 1.0; m_function_yscale = 1.0; m_function_zscale = 1.0; string pattern; while( !load_par.eof() ) { string line; getline( load_par, line ); string::size_type sp = line.find(' '); if( sp != string::npos ) { string key = line.substr(0, sp); string val = line.substr(sp+1, line.size() ); if(key == "ImageWidth") function_real_width = lexical_cast<unsigned int>( val ); else if( key == "ImageHeight" ) function_real_height = lexical_cast<unsigned int>( val ); else if( key == "TotalSlices" ) function_real_slices = lexical_cast<unsigned int>( val ); else if( key == "FilenamePattern" ) pattern = val; else if( key == "XScale" ) m_function_xscale = lexical_cast<float>( val ); else if( key == "YScale" ) m_function_yscale = lexical_cast<float>( val ); else if( key == "ZScale" ) m_function_zscale = lexical_cast<float>( val ); } } // --- sanity checks cerr << __func__ << ": Data size of file: ["<<function_real_width<< " x "<<function_real_height<< " x "<<function_real_slices<<"]\n"; if( function_real_width == 0 || function_real_height == 0 || function_real_slices == 0 ) { exit( EXIT_FAILURE ); } m_function_tsize_log2 = (unsigned int)ceilf(log2(max(function_real_width,function_real_height))); if(m_function_tsize_log2 < downsample_log2+3) { cerr << __func__ << ": Downsampled image too small.\n"; exit( EXIT_FAILURE ); } m_function_tsize_log2 -= downsample_log2; m_function_tsize = 1<<m_function_tsize_log2; if(m_force_pow2) { m_function_slices = 1<<((unsigned int)ceilf(log2(ceilf( function_real_slices / downsample )))); } else { m_function_slices = (unsigned int)ceilf( function_real_slices / downsample ); } cerr << __func__ << ": Data size in mem: [" << m_function_tsize << " x " << m_function_tsize << " x " << m_function_slices << "]\n"; /* if( m_function_tsize < 16 || m_function_slices < 16 ) { std::cerr << __func__ << ": Dataset is way too small.\n"; exit( EXIT_FAILURE ); } */ if( m_function_slices == 0 ) exit( EXIT_FAILURE ); string::size_type prefix_sp = pattern.find('#'); if( prefix_sp == string::npos ) { cerr << __func__ << ": pattern doesn't contain a '#'.\n"; exit( EXIT_FAILURE ); } string prefix = getPath( filename ) + pattern.substr(0, prefix_sp); string postfix = pattern.substr(prefix_sp+1, pattern.size() ); cerr << __func__ << ": pattern prefix = '" << prefix << "'\n"; cerr << __func__ << ": pattern postfix = '" << postfix << "'\n"; cerr << __func__ << ": reading slices "; // --- read model allocFuncMem(); { std::vector<unsigned short> raw_slices( function_real_width*function_real_height*downsample ); for(int s=0; s<m_function_slices; s++) { // --- read a set of slices for(int ss = 0; ss<downsample; ss++) { int slice = s*downsample + ss; if( slice < function_real_slices ) { string slicename = prefix + lexical_cast<string>(slice+1) + postfix; ifstream slicefile( slicename.c_str() ); if(!slicefile.good() ) { cerr << __func__ << "Error opening " << slicename << " for reading.\n"; exit( EXIT_FAILURE ); } slicefile.read( (char*)(&raw_slices[function_real_width*function_real_height*ss]), sizeof(unsigned short)*function_real_width*function_real_height ); } else { fill_n( (char*)(&raw_slices[function_real_width*function_real_height*ss]), sizeof(unsigned short)*function_real_width*function_real_height, 0 ); } } // --- fix endianess for(int i=0; i<raw_slices.size(); i++) { unsigned int hi = *((unsigned char*)&raw_slices[i]+0); unsigned int lo = *((unsigned char*)&raw_slices[i]+1); unsigned int val = (hi<<8) + lo; if( val == 0xf830 ) val = 0; raw_slices[i] = val; } // --- downsample for(int j=0; j<m_function_tsize; j++) { for(int i=0; i<m_function_tsize; i++) { unsigned long long int value = 0ull; for(int ss=0; ss<downsample; ss++) { for(int jj=0; jj<downsample; jj++) { for(int ii=0; ii<downsample; ii++) { int iii = i*downsample+ii; int jjj = j*downsample+jj; if( iii < function_real_width && jjj < function_real_height ) { value += raw_slices[ ss*function_real_width*function_real_width + jjj*function_real_width + iii ]; } } } } value /= downsample*downsample*downsample; switch( m_function_format ) { case FUNC_UNSIGNED_BYTE: ((unsigned char*)m_function_data_)[s*m_function_tsize*m_function_tsize + j*m_function_tsize + i ] = value>>4; break; case FUNC_UNSIGNED_SHORT: ((unsigned short*)m_function_data_)[s*m_function_tsize*m_function_tsize + j*m_function_tsize + i ] = value; break; case FUNC_FLOAT: ((float*)m_function_data_)[s*m_function_tsize*m_function_tsize + j*m_function_tsize + i ] = value; break; default: exit( EXIT_FAILURE ); } } } if( (40*(s))/m_function_slices != (40*(s+1))/m_function_slices ) cerr << '.'; } } cerr << '\n'; }
void Config::readSetOfSlices( const std::vector<std::string>& files, unsigned int downx, unsigned int downy, unsigned int downz ) { ILuint image; ilGenImages( 1, &image ); ilBindImage( image); ILenum type, bpp; switch( m_function_format ) { case FUNC_FLOAT: m_function_iso_min = 0; m_function_iso_max = 0xfff; m_function_isovalue = 0x200; type = IL_FLOAT; bpp = 4; break; case FUNC_UNSIGNED_BYTE: m_function_iso_min = 0; m_function_iso_max = 0xff; m_function_isovalue = 0x20; type = IL_UNSIGNED_BYTE; bpp = 1; break; case FUNC_UNSIGNED_SHORT: m_function_iso_min = 0; m_function_iso_max = 0xfff; m_function_isovalue = 0x200; type = IL_UNSIGNED_SHORT; bpp = 2; break; } int w, h; for(int i=0; i<files.size(); i++) { std::vector<char> filename(files[i].begin(), files[i].end()); filename.push_back('\0'); if( ilLoadImage( &filename[0] ) == IL_FALSE ) { std::cerr << __func__ << ": failed to read image " << files[i] << "\n"; exit( EXIT_FAILURE ); } std::cerr << __func__ << ": read " << files[i] << "\n"; if( i==0 ) { w = ilGetInteger( IL_IMAGE_WIDTH ); h = ilGetInteger( IL_IMAGE_HEIGHT ); std::cerr << __func__ << ": input = [ " << w << " x " << h << "]\n"; std::cerr << __func__ << ": type = " << type << "\n"; std::cerr << __func__ << ": bpp = " << bpp << "\n"; m_function_tsize_log2 = (unsigned int)ceilf(log2(max(w,h))); m_function_tsize = 1<<m_function_tsize_log2; m_function_slices = files.size(); allocFuncMem(); } ilCopyPixels(0, 0, 0, m_function_tsize, m_function_tsize, 1, IL_LUMINANCE, type, (unsigned char*)m_function_data_ + bpp*m_function_tsize*m_function_tsize*i ); } ilDeleteImages( 1, &image ); }
void Config::snarfRAW( std::string& filename, unsigned int dst_log2 ) { int spec_r = filename.rfind('.'); int spec_l = filename.rfind('.', spec_r-1); string spec = filename.substr(spec_l+1, spec_r-spec_l-1); if(spec.size() != 2) { std::cerr << "illegal spec string.\n"; abort(); } int src_log2 = -1; if('0' <= spec[1] && spec[1] <= '9') { src_log2 = spec[1]-'0'; } if( src_log2 < 0 || 9 < src_log2 ) { std::cerr << "hmmm.\n"; abort(); } if( src_log2 < dst_log2 ) { std::cerr << "we don't support upsampling.\n"; } std::cerr << __func__ << ": source file=\""<<filename<<"\", src_log2=" << src_log2 << ", dst_log2="<<dst_log2<<".\n"; int src_size = 1<<src_log2; int dst_size = 1<<dst_log2; int scale = 1<<(src_log2-dst_log2); m_function_tsize_log2 = dst_log2; m_function_tsize = dst_size; m_function_slices = dst_size; allocFuncMem(); // --- read raw data int chunksize = src_size*src_size*src_size; unsigned char* data = new unsigned char[chunksize]; std::ifstream file( filename.c_str(), std::ios::binary ); if(!file.good()) { std::cerr << "File is not good.\n"; abort(); } file.read( (char*)data, chunksize ); file.close(); // --- downsample for(int k=0; k<dst_size; k++) { for(int j=0; j<dst_size; j++) { for(int i=0; i<dst_size; i++) { // --- sum over volume unsigned long int value = 0; for(int kk=0; kk<scale; kk++) { for(int jj=0; jj<scale; jj++) { for(int ii=0; ii<scale; ii++) { value += data[ (k*scale+kk)*src_size*src_size + (j*scale+jj)*src_size + (i*scale+ii) ]; } } } // --- and store switch( m_function_format ) { case FUNC_UNSIGNED_BYTE: ((unsigned char*)m_function_data_)[k*m_function_tsize*m_function_tsize + j*m_function_tsize + i ] = value/(scale*scale*scale); break; case FUNC_UNSIGNED_SHORT: ((unsigned short*)m_function_data_)[k*m_function_tsize*m_function_tsize + j*m_function_tsize + i ] = (256*value)/(scale*scale*scale); break; case FUNC_FLOAT: ((float*)m_function_data_)[k*m_function_tsize*m_function_tsize + j*m_function_tsize + i ] = (value)/(128.0*scale*scale*scale) - 1.0; break; default: exit( EXIT_FAILURE ); } } } } delete [] data; }
Func* Func::clone() const { Func* f = new (allocFuncMem(m_name, m_numParams)) Func(*this); f->initPrologues(m_numParams); f->m_funcId = InvalidId; return f; }