void PerspectiveMapViewer::processEvent(const sf::Event & event) { if(event.type == sf::Event::MouseWheelMoved) { m_camera.mouseWheelMoved(event.mouseWheel.delta); } else if(event.type == sf::Event::MouseButtonPressed) { if(m_raycaster->isHit) { if(event.mouseButton.button == sf::Mouse::Left) { // Delete pointed voxel m_map.setVoxel(m_raycaster->hit.pos, Voxel(voxel::AIR)); } else if(event.mouseButton.button == sf::Mouse::Right) { // Place a voxel m_map.setVoxel(m_raycaster->hit.prevPos, Voxel(voxel::STONE)); } } } else if(event.type == sf::Event::KeyPressed) { if(event.key.code == sf::Keyboard::Key::Escape) m_camera.setEnabled(!m_camera.isEnabled()); } }
Voxel* Quadtree::Subdivide(Voxel* v){ Voxel* subDivideVoxel = new Voxel[4]; float l = v->getLength() / 2.f; QVector3D pos = v->getPosition(); subDivideVoxel[0] = Voxel(pos + QVector3D(-l/2.0, 0, -l/2.0), l); subDivideVoxel[1] = Voxel(pos + QVector3D(l/2.0, 0, -l/2.0), l); subDivideVoxel[2] = Voxel(pos + QVector3D(l/2.0, 0, l/2.0), l); subDivideVoxel[3] = Voxel(pos + QVector3D(-l/2.0, 0, l/2.0), l); return subDivideVoxel; }
// TODO Integer SpatiocyteWorld::add_neighbors(const Species& sp, const SpatiocyteWorld::private_coordinate_type center) { Integer count(0); const SpatiocyteWorld::molecule_info_type info(get_molecule_info(sp)); for (Integer i(0); i < 12; ++i) { const private_coordinate_type n((*space_).get_neighbor_private(center, i)); if (new_voxel_private(Voxel(sp, n, info.radius, info.D, info.loc)).second) { ++count; } else { throw "Error in add_neighbors()"; } } return count; // Integer count(0); // const SpatiocyteWorld::molecule_info_type info(get_molecule_info(sp)); // std::vector<SpatiocyteWorld::private_coordinate_type> neighbors( // (*space_).get_neighbors(center)); // for (std::vector<SpatiocyteWorld::private_coordinate_type>::iterator itr( // neighbors.begin()); itr != neighbors.end(); itr++) // { // if (new_voxel_private(Voxel(sp, *itr, info.radius, info.D, info.loc)).second) // ++count; // else // throw "Error in add_neighbors()"; // } // return count; }
std::pair<std::pair<ParticleID, Voxel>, bool> SpatiocyteWorld::new_voxel(const Voxel& v) { const private_coordinate_type private_coord(coord2private(v.coordinate())); return new_voxel_private( Voxel(v.species(), private_coord, v.radius(), v.D(), v.loc())); }
__global__ void kernelClearVoxelMap(Voxel* voxelmap, const uint32_t voxelmap_size) { for (uint32_t i = blockIdx.x * blockDim.x + threadIdx.x; i < voxelmap_size; i += gridDim.x * blockDim.x) { voxelmap[i] = Voxel(); } }
std::pair<std::pair<ParticleID, Voxel>, bool> SpatiocyteWorld::new_voxel_private( const Species& sp, const private_coordinate_type& coord) { const molecule_info_type minfo(get_molecule_info(sp)); return new_voxel_private( Voxel(sp, coord, minfo.radius, minfo.D, minfo.loc)); }
std::pair<std::pair<ParticleID, Voxel>, bool> SpatiocyteWorld::new_voxel_structure(const Voxel& v) { const bool is_succeeded((*space_).update_voxel_private(ParticleID(), v)); const coordinate_type coord(private2coord(v.coordinate())); return std::make_pair(std::make_pair(ParticleID(), Voxel(v.species(), coord, v.radius(), v.D(), v.loc())), is_succeeded); }
Voxel BlockMap::getVoxel(const Vector3i & pos) { Block * b = getBlock(Block::toBlockCoords(pos)); if(b != nullptr) { return b->get( Block::toInnerCoord(pos.x), Block::toInnerCoord(pos.y), Block::toInnerCoord(pos.z)); } else return Voxel(voxel::AIR, voxel::AIR_UNLOADED, 0x02); }
Voxel Volume::getInterpolatedVoxel(float x, float y, int z) { int x0 = floor(x); int x1 = ceil(x); int y0 = floor(y); int y1 = ceil(y); if (x1 == m_Width) x1 = m_Width - 1; if (y1 == m_Height) y1 = m_Height - 1; float v1 = this->voxel(x0, y0, z).getValue(); float v2 = this->voxel(x1, y0, z).getValue(); float v3 = this->voxel(x1, y1, z).getValue(); float v4 = this->voxel(x0, y1, z).getValue(); return Voxel((v1 + v2 + v3 + v4) / 4); }
void Chunk::Generate(std::mt19937 generator, std::uniform_int_distribution<> shadeRandom) { float horizontalFrequency = 1.0f / 24.0f; for (int z = m_chunkZ; z < m_chunkZ + m_chunkDepth; z++) { for (int x = m_chunkX; x < m_chunkX + m_chunkWidth; x++) { // the last type generated VoxelType lastType; // the height used for this part of the chunk int height = static_cast<int>(noise_fractal_brownian_motion(m_noise, 4, x * horizontalFrequency, 0.0f, z * horizontalFrequency) * (m_chunkHeight * 2.0f)); int maxY = m_chunkY + height; if (maxY <= 0) { // create three water blocks for water depth // shade doesn't matter for water, so just set it to zero m_transparentRenderer.AddVoxel(VoxelPosition(x, -1, z), Voxel(VoxelType::WATER, 0)); m_transparentRenderer.AddVoxel(VoxelPosition(x, -2, z), Voxel(VoxelType::WATER, 0)); m_transparentRenderer.AddVoxel(VoxelPosition(x, -3, z), Voxel(VoxelType::WATER, 0)); continue; } for (int y = m_chunkY; y < maxY; y++) { float verticalFrequency = 1.0f / (static_cast<float>(height) / 2.0f); float density = noise_fractal_brownian_motion(m_noise, 4, x * horizontalFrequency, y * verticalFrequency, z * horizontalFrequency) + height * m_chunkHeight; if (density >= 0.0f) { VoxelType type = ((y == maxY - 1 && lastType == VoxelType::DIRT) ? VoxelType::GRASS : _GetTypeFromY(y)); // check if we should replace a dirt type with grass if (y == maxY - 1 && type == VoxelType::DIRT) type = VoxelType::GRASS; m_opaqueRenderer.AddVoxel(VoxelPosition(x, y, z), Voxel(type, shadeRandom(generator))); // if the y is less than or equal to zero, add three blocks of the same type if (y <= 0) { m_opaqueRenderer.AddVoxel(VoxelPosition(x, y - 1, z), Voxel(type, shadeRandom(generator))); m_opaqueRenderer.AddVoxel(VoxelPosition(x, y - 2, z), Voxel(type, shadeRandom(generator))); m_opaqueRenderer.AddVoxel(VoxelPosition(x, y - 3, z), Voxel(type, shadeRandom(generator))); } lastType = _GetTypeFromY(y); } } lastType = VoxelType::SAND; } } }
bool Volume::loadFromFile(QString filename, QProgressBar* progressBar) { // load file FILE *fp = NULL; fopen_s(&fp, filename.toStdString().c_str(), "rb"); if (!fp) { std::cerr << "+ Error loading file: " << filename.toStdString() << std::endl; return false; } // progress bar progressBar->setRange(0, m_Size + 10); progressBar->setValue(0); // read header and set volume dimensions unsigned short uWidth, uHeight, uDepth; fread(&uWidth, sizeof(unsigned short), 1, fp); fread(&uHeight, sizeof(unsigned short), 1, fp); fread(&uDepth, sizeof(unsigned short), 1, fp); m_Width = int(uWidth); m_Height = int(uHeight); m_Depth = int(uDepth); // check dataset dimensions if ( m_Width <= 0 || m_Width > 1000 || m_Height <= 0 || m_Height > 1000 || m_Depth <= 0 || m_Depth > 1000) { std::cerr << "+ Error loading file: " << filename.toStdString() << std::endl; std::cerr << "Unvalid dimensions - probably loaded .dat flow file instead of .gri file?" << std::endl; return false; } // compute dimensions int slice = m_Width * m_Height; m_Size = slice * m_Depth; m_Voxels.resize(m_Size); // read volume data // read into vector before writing data into volume to speed up process std::vector<unsigned short> vecData; vecData.resize(m_Size); fread((void*)&(vecData.front()), sizeof(unsigned short), m_Size, fp); fclose(fp); progressBar->setValue(10); // store volume data for (int i = 0; i < m_Size; i++) { // data is converted to FLOAT values in an interval of [0.0 .. 1.0]; // uses 4095.0f to normalize the data, because only 12bit are used for the // data values, and then 4095.0f is the maximum possible value const float value = std::fmax(0.0f, float(vecData[i]) / 4095.0f); m_Voxels[i] = Voxel(value); progressBar->setValue(10 + i); } progressBar->setValue(0); std::cout << "Loaded VOLUME with dimensions " << m_Width << " x " << m_Height << " x " << m_Depth << std::endl; return true; }
long long CGraphFitting::fastFitting() { int num_pixels=pImg3D->foreNum+1; int num_labels=getValidNum(); //////////////////////////////////////////////////////////// int *data = new int[num_pixels*num_labels]; int curIndex=0; int slice=pImg3D->getS(); int width=pImg3D->getW(); int height=pImg3D->getH(); for(int z = 0;z<slice;++z) for(int y=0;y<height;++y) for(int x=0;x<width;++x) { bool vessel=pImg3D->isVessel(x,y,z); if(vessel) { for (int l=0; l < num_labels; l++ ) { if(l==num_labels-1) data[curIndex*num_labels+l]=5000;//INFINIT; else data[curIndex*num_labels+l]=models[l].compEnergy(x,y,z,*pImg3D); } curIndex++; } } for (int l=0; l < num_labels; l++ ) { if(l==num_labels-1) data[curIndex*num_labels+l]=0; else data[curIndex*num_labels+l]=INFINIT; } ////////////////////////////////////////////////////////////////// int *label = new int[num_labels]; for(int k=0;k<num_labels;++k) label[k]=LABELCOST; ///////////////////////////////////////////////////////////////// int *smooth = new int[num_labels*num_labels]; memset(smooth,0,sizeof(int)*num_labels*num_labels); for( int i=0; i<num_labels; i++ ) for ( int j=0; j<num_labels; j++ ) smooth[ i*num_labels+j ] = smooth[ i+j*num_labels ] =SMOOTHCOST* int(i!=j); long long energy=0; try{ GCoptimizationGeneralGraph *gc = new GCoptimizationGeneralGraph(num_pixels,num_labels); gc->setDataCost(data); gc->setLabelCost(label); #ifdef USE_SMOOTHCOST gc->setSmoothCost(smooth); for(int z = 0;z<slice;++z) for(int y=0;y<height;++y) for(int x=0;x<width;++x) { if(!pImg3D->isVessel(x,y,z)) continue; int numHoles=0; ////////////////////////////////////////////////////////////////////////////////// if(inRange(Voxel(x+1,y,z)))//right neighbour { if(pImg3D->isVessel(x+1,y,z)) gc->setNeighbors(pImg3D->getRealPos(x,y,z),pImg3D->getRealPos(x+1,y,z)); else ++numHoles; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// if(inRange(Voxel(x,y+1,z)))//top neighbour { if(pImg3D->isVessel(x,y+1,z)) gc->setNeighbors(pImg3D->getRealPos(x,y,z),pImg3D->getRealPos(x,y+1,z)); else ++numHoles; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// if(inRange(Voxel(x,y,z+1)))//front neighbour { if(pImg3D->isVessel(x,y,z+1)) gc->setNeighbors(pImg3D->getRealPos(x,y,z),pImg3D->getRealPos(x,y,z+1)); else ++numHoles; } ////////////////////////////////////////////////////////////////////////////////// if(inRange(Voxel(x-1,y,z))&& !pImg3D->isVessel(x-1,y,z) ) ++numHoles;//left hole if(inRange(Voxel(x,y-1,z))&& !pImg3D->isVessel(x,y-1,z) ) ++numHoles;//down hole if(inRange(Voxel(x,y,z-1))&& !pImg3D->isVessel(x,y,z-1) ) ++numHoles;//back hole if(numHoles>0) gc->setNeighbors(pImg3D->getRealPos(x,y,z),num_pixels-1,numHoles); } #endif //printf("\nBefore optimization energy is %d",gc->compute_energy()); //std::cout<<"\nBefore optimization energy is "<<gc->compute_energy(); energy=gc->compute_energy(); gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations); //printf("\nAfter optimization energy is %d",gc->compute_energy()); //std::cout<<"\nAfter optimization energy is "<<gc->compute_energy(); gc->compute_energy(); for ( int i = 0; i < num_pixels; i++ ) { int tag = gc->whatLabel(i); models[tag].addSupport(); pLabels[i]=tag; //if(result[i]!=num_labels-1) printf("%d ",result[i]); } //////////////////////////////////////////////////////////////////////////////////// for(int i=0;i<num_labels;++i) { int sp=models[i].getSupport(); models[i].setValid(sp>0); if(i==num_labels-1)//last model ,must be valid models[i].setValid(true); } /////////////////////////////////////////////// for(int i=0;i<num_labels-1;++i) { if(models[i].isValid()) { fitLine(i,gc); } } delete gc; } catch (GCException e){ e.Report(); } delete [] smooth; delete []label; delete [] data; return energy; }
// GridAccel Method Definitions GridAccel::GridAccel(const vector<Reference<Primitive> > &p, bool forRefined, bool refineImmediately) : gridForRefined(forRefined) { PBRT_GRID_STARTED_CONSTRUCTION(this, p.size()); // Create reader-writeer mutex for grid rwMutex = RWMutex::Create(); // Initialize _primitives_ with primitives for grid if (refineImmediately) for (u_int i = 0; i < p.size(); ++i) p[i]->FullyRefine(primitives); else primitives = p; // Compute bounds and choose grid resolution for (u_int i = 0; i < primitives.size(); ++i) bounds = Union(bounds, primitives[i]->WorldBound()); Vector delta = bounds.pMax - bounds.pMin; // Find _voxelsPerUnitDist_ for grid int maxAxis = bounds.MaximumExtent(); float invMaxWidth = 1.f / delta[maxAxis]; Assert(invMaxWidth > 0.f); float cubeRoot = 3.f * powf(float(primitives.size()), 1.f/3.f); float voxelsPerUnitDist = cubeRoot * invMaxWidth; for (int axis = 0; axis < 3; ++axis) { NVoxels[axis] = Round2Int(delta[axis] * voxelsPerUnitDist); NVoxels[axis] = Clamp(NVoxels[axis], 1, 64); } PBRT_GRID_BOUNDS_AND_RESOLUTION(&bounds, NVoxels); // Compute voxel widths and allocate voxels for (int axis = 0; axis < 3; ++axis) { Width[axis] = delta[axis] / NVoxels[axis]; InvWidth[axis] = (Width[axis] == 0.f) ? 0.f : 1.f / Width[axis]; } int nVoxels = NVoxels[0] * NVoxels[1] * NVoxels[2]; voxels = AllocAligned<Voxel *>(nVoxels); memset(voxels, 0, nVoxels * sizeof(Voxel *)); // Add primitives to grid voxels for (u_int i = 0; i < primitives.size(); ++i) { // Find voxel extent of primitive BBox pb = primitives[i]->WorldBound(); int vmin[3], vmax[3]; for (int axis = 0; axis < 3; ++axis) { vmin[axis] = posToVoxel(pb.pMin, axis); vmax[axis] = posToVoxel(pb.pMax, axis); } // Add primitive to overlapping voxels PBRT_GRID_VOXELIZED_PRIMITIVE(vmin, vmax); for (int z = vmin[2]; z <= vmax[2]; ++z) for (int y = vmin[1]; y <= vmax[1]; ++y) for (int x = vmin[0]; x <= vmax[0]; ++x) { int o = offset(x, y, z); if (!voxels[o]) { // Allocate new voxel and store primitive in it voxels[o] = voxelArena.Alloc<Voxel>(); *voxels[o] = Voxel(primitives[i]); } else { // Add primitive to already-allocated voxel voxels[o]->AddPrimitive(primitives[i]); } } } PBRT_GRID_FINISHED_CONSTRUCTION(this); }
std::vector<float> Volume::rayCasting2() { float pixel_width = m_Width * m_factor; float pixel_height = m_Height * m_factor; std::vector<float> out; out.resize(pixel_width * pixel_height); for (int x = 0; x < pixel_width; x++) { for (int y = 0; y < pixel_height; y++) { // intensity Voxel& value = Voxel(); // Compositions Faktor float alpha = 0.0; // current voxel Voxel& voxel = Voxel(); // position in volume float p_x = (float)x / (float)m_factor; float p_y = (float)y / (float)m_factor; for (int z = 0; z < m_Depth; z += m_samples) { // interpolated voxel if (((float)x / (float)m_factor) != (int)((float)x / (float)m_factor)) { voxel = getInterpolatedVoxel(p_x, p_y, z); } else { voxel = this->voxel((int)p_x, (int)p_y, z); } // Maximum-Intensity-Projektion if (mip) { if (voxel.operator>(value)) { value = voxel; } } // First-Hit Renderingtechnik if (firstHit) { if (voxel.getValue() > 0.f) { value = voxel; break; } } // Average rendering if (average) { value = value.operator+(voxel); } // Alpha-Compositing else { alpha += voxel.getValue() * ((1.0 - z / m_Depth) * m_transparency); if (alpha > 1.0) { alpha = 1.0; break; } } } if (average) out[y * pixel_width + x] = (value.operator/=(m_Depth / m_samples)).getValue(); if (alphaCompositing) out[y * pixel_width + x] = alpha; else out[y * pixel_width + x] = value.getValue(); } } return out; }
std::vector<float> Volume::rayCasting() { m_factor = 1; std::vector<float> out; out.resize(m_Width * m_Height); for (int x = 0; x < m_Width; x++) { for (int y = 0; y < m_Height; y++) { // intensity Voxel& value = Voxel(); // Compositions Faktor float alpha = 0.0; for (int z = 0; z < m_Depth; z += m_samples) { // Maximum-Intensity-Projektion if (mip) { if (this->voxel(x, y, z).operator>(value)) { value = this->voxel(x, y, z); } } // First-Hit Renderingtechnik if (firstHit) { if (this->voxel(x, y, z).getValue() > 0.f) { value = this->voxel(x, y, z); break; } } // Average rendering if (average) { value = value.operator+(this->voxel(x, y, z)); } // Alpha-Compositing else { alpha += this->voxel(x, y, z).getValue() * ((1.0 - z / m_Depth) * m_transparency); if (alpha > 1.0) { alpha = 1.0; break; } } } if (average) out[y*m_Width + x] = (value.operator/=(m_Depth / m_samples)).getValue(); if (alphaCompositing) out[y*m_Width + x] = alpha; else out[y*m_Width + x] = value.getValue(); } } return out; }