void CellPartitioning<T>::getNeighbouringEntities(const T& t, float range) { size_t numcells = 1 + range / mCellWidth; size_t ci = getCellIndex(t->getPosition()); size_t si = ci % mNumCells; size_t sj = ci / mNumCells; mNeighbours.clear(); #ifdef CELL_DEBUG size_t cell1006 = 0; bool checked = false; Platoon* pl = dynamic_cast<Platoon*>(EntityManager::instance().getEntity(1004)); std::vector<std::pair<size_t, size_t>> checkedcells; if(t->getEntityID() == 1006) { // std::cout << "Pos: " << t->getPosition() << " Index: " << ci << " si: " << si << " sj: " << sj << "\n"; cell1006 = getCellIndex(pl->getPosition()); } #endif for(size_t i = std::max<size_t>(0, si - numcells - 1); i <= std::min<size_t>(mNumCells - 1, si + numcells - 1); i++) { for(size_t j = std::max<size_t>(0, sj - numcells - 1); j <= std::min<size_t>(mNumCells - 1, sj + numcells - 1); j++) { if(j * mNumCells + i >= mCells.size()) { std::cout << "\nAbout to crash. j * mNumCells + i >= mCells.size().\n" << j << " * " << mNumCells << " + " << i << " = " << j * mNumCells + i << " >= " << mCells.size() << ".\n"; } #ifdef CELL_DEBUG if(j * mNumCells + i == cell1006) checked = true; checkedcells.push_back(std::make_pair(i, j)); #endif for(T t2 : mCells.at(j * mNumCells + i)) { #ifdef CELL_DEBUG if(t->getEntityID() == 1006 && t != t2) std::cout << "At " << j * mNumCells + i << ": " << t2->getEntityID() << "\n"; #endif if(t != t2 && (t->getPosition() - t2->getPosition()).length() <= range) { mNeighbours.push_back(t2); } } } } #ifdef CELL_DEBUG if(t->getEntityID() == 1006) { std::cout << "Diff to 1004: " << (t->getPosition() - pl->getPosition()).length(); std::cout << " My cell: (" << si << ", " << sj << ") = " << ci << " - his cell: (" << cell1006 % mNumCells << ", " << cell1006 / mNumCells << ") = " << cell1006 << "\n"; if(!checked) { std::cout << "Did not check his cell. Checked cells:"; for(auto c : checkedcells) std::cout << " (" << c.first << ", " << c.second << ") = " << c.second * mNumCells + c.first; std::cout << "\n"; } } #endif if(mNeighbours.size()) mCurrentNeighbour = 0; else mCurrentNeighbour = -1; }
//! Clears the complex on the specified region. Please observe that the actually cleared region //! consists of all lattice cells intersecting the passed region, therefore resulting in a cleared region //! typically larger than passed one, up to the lattice granularity. void TCacheResource::clear(QRegion region) { if (!m_region.intersects(region)) return; //Get the region bbox TRect bbox(toTRect(region.boundingRect())); //For all cells intersecting the bbox TPoint initialPos(getCellPos(bbox.getP00())); TPoint pos; for (pos.x = initialPos.x; pos.x <= bbox.x1; pos.x += latticeStep) for (pos.y = initialPos.y; pos.y <= bbox.y1; pos.y += latticeStep) { QRect cellQRect(toQRect(TRect(pos, TDimension(latticeStep, latticeStep)))); if (region.intersects(cellQRect) && m_region.intersects(cellQRect)) { //Release the associated cell from cache and clear the cell from the content region. TImageCache::instance()->remove(getCellCacheId(pos)); m_region -= cellQRect; --m_cellsCount; //DIAGNOSTICS_GLOADD("crCellsCnt", -1); //Release the cell from m_cellDatas m_cellDatas[getCellIndex(pos)].m_modified = true; } } if (m_region.isEmpty()) { m_tileType = NONE; m_locksCount = 0; } }
void TableView::onTouchEnded(Touch* touch, Event* unusedEvent) { ScrollView::onTouchEnded(touch, unusedEvent); do { CC_BREAK_IF(m_startFocusCellIndex == -1); auto _focusItem = getCellByIndex(m_startFocusCellIndex); CC_BREAK_IF(!_focusItem); Rect _rect = { 0, 0, getContentSize().width, getContentSize().height }; int32_t _cellIndex; if (_rect.containsPoint(this->convertTouchToNodeSpace(touch))) _cellIndex = getCellIndex(m_container->convertTouchToNodeSpace(touch)); else _cellIndex = -1; if (_cellIndex == m_startFocusCellIndex) { _focusItem->onBlur(); _focusItem->onClick(); if (m_cellTouchCallfunc) { m_cellTouchCallfunc(this, _focusItem, m_startFocusCellIndex, TouchType::CLICK); } } else _focusItem->onBlur(); } while (0); }
void Field::JewelClick(Event *event) { TouchEvent *te = safeCast<TouchEvent*>(event); Point ind = getCellIndex(te->localPosition); if (state==fsSwaping) { if (At(ind)) { state = fsWaiting; At(jewel_drag_ind)->UnSelect(); At(ind)->UnSelect(); back_swap = false; Swap(At(ind),At(jewel_drag_ind)); } } if (state==fsWaiting) { if (At(ind)) { //At(ind)->PlayAnimation(); jewel_drag_ind = ind; At(ind)->SetSelected(); state = fsSwaping; } } DropField(); }
bool Field::Swap(spJewel First, spJewel Second, bool skip_animation) { Point diff = getCellIndex(First->getPosition()) - getCellIndex(Second->getPosition()); if (abs(diff.x)>1 || (abs(diff.y)>1) || abs(diff.x)==abs(diff.y) || Second==First || !(Second->getState()==jsNormal && First->getState()==jsNormal)) return false; ForceSwap(First, Second); if (!skip_animation) { state = fsAnimation; jewel_new_ind = getCellIndex( First->getPosition()); jewel_drag_ind = getCellIndex( Second->getPosition()); First->addTween(Sprite::TweenPosition(Second->getPosition()),200); Second->addTween(Sprite::TweenPosition(First->getPosition()),200)->addEventListener(TweenEvent::DONE, CLOSURE(this, &Field::EndSwapCallback)); } return true; }
bool TableView::onTouchBegan(Touch* touch, Event* unusedEvent) { bool _re = ScrollView::onTouchBegan(touch, unusedEvent); if (_re) { m_startFocusCellIndex = getCellIndex(m_container->convertTouchToNodeSpace(touch)); GridCell* _item = getCellByIndex(m_startFocusCellIndex); if (_item) { _item->onFocus(); } } return _re; }
void CellPartitioning<T>::updateEntity(const T& t, const Vector2& oldpos) { size_t i = getCellIndex(oldpos); size_t j = getCellIndex(t->getPosition()); if(i != j) { if(mCells.at(i).erase(t) != 1) { std::cout << "Updating entity " << t->getEntityID() << " that does not exist?\n"; for(size_t i = 0; i < mNumCells - 1; i++) { for(size_t j = 0; j < mNumCells - 1; j++) { for(T t2 : mCells.at(j * mNumCells + i)) { if(t2->getEntityID() == t->getEntityID()) { std::cout << "Entity was supposed to be at " << i << ", but it is at " << j * mNumCells + i << ".\n"; std::cout << "(New position: " << j << "\n"; } } } } std::cout << "Done searching for the entity.\n"; return; } mCells.at(j).insert(t); } }
void TCacheResource::addRef2(const TRect &rect) { //DIAGNOSTICS_NUMBEREDSTRSET(prefix + QString::number((UINT) this) + " | Stack | ", //"crStack", "addRef", ::traduce(rect)); //Add a reference to all cells intersecting the passed one TPoint initialPos(getCellPos(rect.getP00())); TPoint pos; for (pos.x = initialPos.x; pos.x <= rect.x1; pos.x += latticeStep) for (pos.y = initialPos.y; pos.y <= rect.y1; pos.y += latticeStep) { PointLess cellIndex(getCellIndex(pos)); CellData &cellData = m_cellDatas[cellIndex]; cellData.m_referenced = true; cellData.m_refsCount++; } }
//! Copies the passed tile in the tile complex. The passed tile \b must //! possess integer geometry (ie tile.m_pos must have integer coordinates), //! otherwise this function is a no-op. bool TCacheResource::upload(const TPoint &pos, TRasterP ras) { int tileType; if (!checkRasterType(ras, tileType)) return false; if (m_tileType == NONE) m_tileType = tileType; //For all cells of the lattice which intersect the tile, upload the content in the //complex TRect tileRect(ras->getBounds() + pos); TPoint initialPos(getCellPos(tileRect.getP00())); //DIAGNOSTICS_NUMBEREDSTRSET(prefix + QString::number((UINT) this) + " | Stack | ", //"crStack", "upload", ::traduce(TRect(pos, ras->getSize()))); TPoint currPos; for (currPos.x = initialPos.x; currPos.x <= tileRect.x1; currPos.x += latticeStep) for (currPos.y = initialPos.y; currPos.y <= tileRect.y1; currPos.y += latticeStep) { //Copy tile's content into the cell's raster. TRect cellRect(currPos, TDimension(latticeStep, latticeStep)); TRect overlapRect(tileRect * cellRect); assert(!overlapRect.isEmpty()); PointLess cellIndex(getCellIndex(currPos)); std::pair<TRasterP, CellData *> cellInfos(touch(cellIndex)); TRasterP cellRas(cellInfos.first); TRect temp(overlapRect - currPos); TRasterP overlappingCellRas(cellRas->extract(temp)); temp = TRect(overlapRect - tileRect.getP00()); TRasterP overlappingTileRas(ras->extract(temp)); assert(overlappingCellRas->getBounds() == overlappingTileRas->getBounds()); TRop::copy(overlappingCellRas, overlappingTileRas); cellInfos.second->m_modified = true; } //Update the complex's content region m_region += toQRect(tileRect); return true; }
bool TCacheResource::downloadAll(const TPoint &pos, TRasterP ras) { int tileType; if (!checkRasterType(ras, tileType)) return false; //Build the tile's rect TRect tileRect(ras->getBounds() + pos); if (!contains(m_region, tileRect)) return false; //DIAGNOSTICS_NUMBEREDSTRSET(prefix + QString::number((UINT) this) + " | Stack | ", //"crStack", "downloadAll", ::traduce(TRect(pos, ras->getSize()))); //For all cells intersecting the tile's rect, copy all those intersecting the //complex's content region. TPoint initialPos(getCellPos(tileRect.getP00())); TPoint currPos; for (currPos.x = initialPos.x; currPos.x <= tileRect.x1; currPos.x += latticeStep) for (currPos.y = initialPos.y; currPos.y <= tileRect.y1; currPos.y += latticeStep) { TRect cellRect(currPos, TDimension(latticeStep, latticeStep)); TRect overlapRect(tileRect * cellRect); assert(!overlapRect.isEmpty()); QRect overlapQRect(toQRect(overlapRect)); if (m_region.intersects(overlapQRect)) { //Extract the associated rasters and perform the copy to the input tile. std::pair<TRasterP, CellData *> cellInfos(touch(getCellIndex(currPos))); TRasterP cellRas(cellInfos.first); TRect temp(overlapRect - currPos); TRasterP overlappingCellRas(cellRas->extract(temp)); temp = TRect(overlapRect - tileRect.getP00()); TRasterP overlappingTileRas(ras->extract(temp)); TRop::copy(overlappingTileRas, overlappingCellRas); } } return true; }
//! Fills the passed tile with the data contained in the complex, returning //! the copied region. //! The same restriction of the upload() method applies here. QRegion TCacheResource::download(const TPoint &pos, TRasterP ras) { int tileType; if (!checkRasterType(ras, tileType)) return QRegion(); //Build the tile's rect TRect tileRect(ras->getBounds() + pos); if (!m_region.intersects(toQRect(tileRect))) return QRegion(); //For all cells intersecting the tile's rect, copy all those intersecting the //complex's content region. TPoint initialPos(getCellPos(tileRect.getP00())); TPoint currPos; for (currPos.x = initialPos.x; currPos.x <= tileRect.x1; currPos.x += latticeStep) for (currPos.y = initialPos.y; currPos.y <= tileRect.y1; currPos.y += latticeStep) { TRect cellRect(currPos, TDimension(latticeStep, latticeStep)); TRect overlapRect(tileRect * cellRect); assert(!overlapRect.isEmpty()); QRect overlapQRect(toQRect(overlapRect)); if (m_region.intersects(overlapQRect)) { //Extract the associated rasters and perform the copy to the input tile. std::pair<TRasterP, CellData *> cellInfos(touch(getCellIndex(currPos))); TRasterP cellRas(cellInfos.first); TRect temp(overlapRect - currPos); TRasterP overlappingCellRas(cellRas->extract(temp)); temp = TRect(overlapRect - tileRect.getP00()); TRasterP overlappingTileRas(ras->extract(temp)); TRop::copy(overlappingTileRas, overlappingCellRas); } } return m_region.intersected(QRegion(toQRect(tileRect))); }
pcl::MovingLeastSquares<PointInT, PointOutT>::MLSVoxelGrid::MLSVoxelGrid (PointCloudInConstPtr& cloud, IndicesPtr &indices, float voxel_size) : voxel_grid_ (), bounding_min_ (), bounding_max_ (), data_size_ (), voxel_size_ (voxel_size) { pcl::getMinMax3D (*cloud, *indices, bounding_min_, bounding_max_); Eigen::Vector4f bounding_box_size = bounding_max_ - bounding_min_; double max_size = (std::max) ((std::max)(bounding_box_size.x (), bounding_box_size.y ()), bounding_box_size.z ()); // Put initial cloud in voxel grid data_size_ = static_cast<uint64_t> (1.5 * max_size / voxel_size_); for (unsigned int i = 0; i < indices->size (); ++i) if (pcl_isfinite (cloud->points[(*indices)[i]].x)) { Eigen::Vector3i pos; getCellIndex (cloud->points[(*indices)[i]].getVector3fMap (), pos); uint64_t index_1d; getIndexIn1D (pos, index_1d); Leaf leaf; voxel_grid_[index_1d] = leaf; } }
void TableView::onTouchMoved(Touch* touch, Event* unusedEvent) { ScrollView::onTouchMoved(touch, unusedEvent); do { CC_BREAK_IF(m_startFocusCellIndex == -1); int _cellIndex = getCellIndex(m_container->convertTouchToNodeSpace(touch)); //如果手指移动距离大于可移动值则判断无法触发点击 if (touch->getDelta().getLength() > CLICK_MAX_MOVE_DISTANCE || m_movedPos.getDistance(m_beganPos) > CLICK_MAX_MOVE_DISTANCE || _cellIndex < 0 || _cellIndex != m_startFocusCellIndex ) { GridCell* _focusItem = _focusItem = getCellByIndex(m_startFocusCellIndex); if (_focusItem) { _focusItem->onBlur(); } m_startFocusCellIndex = -1; } } while (0); }
/*! Builds local hash and reorders particle index table. */ void physx::Pt::SpatialHash::buildLocalHash(const Particle* particles, PxU32 numParticles, ParticleCell* cells, PxU32* particleIndices, PxU16* hashKeyArray, PxU32 numHashBuckets, PxF32 cellSizeInv, const PxVec3& packetCorner) { PX_ASSERT(particles); PX_ASSERT(cells); PX_ASSERT(particleIndices); PX_ASSERT(numHashBuckets > numParticles); // Needs to be larger to have at least one empty hash bucket (required to // detect invalid cells). // Mark packet cell entries as empty. for(PxU32 c = 0; c < numHashBuckets; c++) cells[c].numParticles = PX_INVALID_U32; PX_ALIGN(16, Particle fakeParticle); fakeParticle.position = PxVec3(FLT_MAX, FLT_MAX, FLT_MAX); PxU32 numParticles4 = ((numParticles + 3) & ~0x3) + 4; // ceil up to multiple of four + 4 for save unrolling // Add particles to cell hash const Particle* prt0 = particles; const Particle* prt1 = (1 < numParticles) ? particles + 1 : &fakeParticle; const Particle* prt2 = (2 < numParticles) ? particles + 2 : &fakeParticle; const Particle* prt3 = (3 < numParticles) ? particles + 3 : &fakeParticle; struct Int32Vec3 { PX_FORCE_INLINE void set(const PxVec3& realVec, const PxF32 scale) { x = static_cast<PxI32>(Ps::floor(realVec.x * scale)); y = static_cast<PxI32>(Ps::floor(realVec.y * scale)); z = static_cast<PxI32>(Ps::floor(realVec.z * scale)); } PxI32 x; PxI32 y; PxI32 z; }; PX_ALIGN(16, Int32Vec3 cellCoords[8]); cellCoords[0].set(prt0->position - packetCorner, cellSizeInv); cellCoords[1].set(prt1->position - packetCorner, cellSizeInv); cellCoords[2].set(prt2->position - packetCorner, cellSizeInv); cellCoords[3].set(prt3->position - packetCorner, cellSizeInv); for(PxU32 p = 0; p < numParticles4; p += 4) { const Particle* prt0_N = (p + 4 < numParticles) ? particles + p + 4 : &fakeParticle; const Particle* prt1_N = (p + 5 < numParticles) ? particles + p + 5 : &fakeParticle; const Particle* prt2_N = (p + 6 < numParticles) ? particles + p + 6 : &fakeParticle; const Particle* prt3_N = (p + 7 < numParticles) ? particles + p + 7 : &fakeParticle; PxU32 wIndex = (p + 4) & 7; cellCoords[wIndex].set(prt0_N->position - packetCorner, cellSizeInv); cellCoords[wIndex + 1].set(prt1_N->position - packetCorner, cellSizeInv); cellCoords[wIndex + 2].set(prt2_N->position - packetCorner, cellSizeInv); cellCoords[wIndex + 3].set(prt3_N->position - packetCorner, cellSizeInv); PxU32 rIndex = p & 7; for(PxU32 i = 0; i < 4; ++i) { if(p + i < numParticles) { const Int32Vec3& int32Vec3 = cellCoords[rIndex + i]; const GridCellVector cellCoord(PxI16(int32Vec3.x), PxI16(int32Vec3.y), PxI16(int32Vec3.z)); PxU32 hashKey = getCellIndex(cellCoord, cells, numHashBuckets); PX_ASSERT(hashKey < PT_PARTICLE_SYSTEM_HASH_KEY_LIMIT); ParticleCell* cell = &cells[hashKey]; hashKeyArray[p + i] = Ps::to16(hashKey); PX_ASSERT(cell); if(cell->numParticles == PX_INVALID_U32) { // Entry is empty -> Initialize new entry cell->coords = cellCoord; cell->numParticles = 1; // this avoids some LHS } else { cell->numParticles++; // this avoids some LHS } PX_ASSERT(cell->numParticles != PX_INVALID_U32); } } } // Set for each cell the starting index of the associated particle index interval. PxU32 cellFirstParticle = 0; for(PxU32 c = 0; c < numHashBuckets; c++) { ParticleCell& cell = cells[c]; if(cell.numParticles == PX_INVALID_U32) continue; cell.firstParticle = cellFirstParticle; cellFirstParticle += cell.numParticles; } reorderParticleIndicesToCells(particles, numParticles, cells, particleIndices, numHashBuckets, hashKeyArray); }
PointArray* AStar::getShortPath(Point start, Point end) { _shortPath = PointArray::create(20); /*if (start == end) { LOG("You're already there! :P"); return; }*/ /*if (!_layer->isValidTileCoord(end) || _layer->isWallAtTileCoord(end)){ SimpleAudioEngine::getInstance()->playEffect("hitWall.wav"); return; }*/ //LOG("Start: %f, %f", start.x, start.y); //LOG("End: %f, %f", end.x, end.y); //bool pathFound = false; _openCells.clear(); _closeCells.clear(); // 首先,添加起始坐标到open列表 this->insertInOpenCells(Cell::createWithPosition(start)); do{ // 得到最小的F值步骤 // 因为是有序列表,第一个步骤总是最小的F值 Cell *curCell = _openCells.at(0); // 添加当前步骤到closed列表 _closeCells.pushBack(curCell); // 将它从open列表里面移除 // 需要注意的是,如果想要先从open列表里面移除,应小心对象的内存 _openCells.erase(0); // 如果当前步骤是目标方块坐标,那么就完成了 if (curCell->getPosition() == end) { buildPath(curCell); _openCells.clear(); _closeCells.clear(); break; } // 得到当前步骤的相邻方块坐标 getBordorPoints(curCell->getPosition()); for (ssize_t i = 0; i < _points->count(); ++i) { Cell *cell = Cell::createWithPosition(_points->getControlPointAtIndex(i)); // 检查步骤是不是已经在closed列表 if(getCellIndex(_closeCells, cell) != -1) { continue; } // 计算从当前步骤到此步骤的成本 int cost = this->cellCost(curCell, cell); // 检查此步骤是否已经在open列表 ssize_t index = getCellIndex(_openCells, cell); // 不在open列表,添加它 if (index == -1) { // 设置当前步骤作为上一步操作 cell->setParent(curCell); // G值等同于上一步的G值 + 从上一步到这里的成本 cell->setGScore(curCell->getGScore() + cost); // H值即是从此步骤到目标方块坐标的移动量估算值 cell->setHScore(this->computeHScore(cell->getPosition(), end)); // 按序添加到open列表 this->insertInOpenCells(cell); } else { // 获取旧的步骤,其值已经计算过 cell = _openCells.at(index); // 检查G值是否低于当前步骤到此步骤的值 if ((curCell->getGScore() + cost) < cell->getGScore()) { // 设置当前步骤作为上一步操作 cell->setParent(curCell); // G值等同于上一步的G值 + 从上一步到这里的成本 cell->setGScore(curCell->getGScore() + cost); // 因为G值改变了,F值也会跟着改变 // 所以为了保持open列表有序,需要将此步骤移除,再重新按序插入 // 在移除之前,需要先保持引用 cell->retain(); // 现在可以放心移除,不用担心被释放 _openCells.erase(index); // 重新按序插入 this->insertInOpenCells(cell); // 现在可以释放它了,因为open列表应该持有它 cell->release(); } } } } while (_openCells.size() > 0); _shortPath->insertControlPoint(start,0); /*for(int l = 0; l < _shortPath->count(); l++) { auto v = _shortPath->getControlPointAtIndex(l); log("%f %f", v.x, v.y); }*/ return _shortPath; }
template <typename PointNT> bool pcl::GridProjection<PointNT>::reconstructPolygons (std::vector<pcl::Vertices> &polygons) { data_.reset (new pcl::PointCloud<PointNT> (*input_)); getBoundingBox (); // Store the point cloud data into the voxel grid, and at the same time // create a hash map to store the information for each cell cell_hash_map_.max_load_factor (2.0); cell_hash_map_.rehash (data_->points.size () / static_cast<long unsigned int> (cell_hash_map_.max_load_factor ())); // Go over all points and insert them into the right leaf for (int cp = 0; cp < static_cast<int> (data_->points.size ()); ++cp) { // Check if the point is invalid if (!pcl_isfinite (data_->points[cp].x) || !pcl_isfinite (data_->points[cp].y) || !pcl_isfinite (data_->points[cp].z)) continue; Eigen::Vector3i index_3d; getCellIndex (data_->points[cp].getVector4fMap (), index_3d); int index_1d = getIndexIn1D (index_3d); if (cell_hash_map_.find (index_1d) == cell_hash_map_.end ()) { Leaf cell_data; cell_data.data_indices.push_back (cp); getCellCenterFromIndex (index_3d, cell_data.pt_on_surface); cell_hash_map_[index_1d] = cell_data; occupied_cell_list_[index_1d] = 1; } else { Leaf cell_data = cell_hash_map_.at (index_1d); cell_data.data_indices.push_back (cp); cell_hash_map_[index_1d] = cell_data; } } Eigen::Vector3i index; int numOfFilledPad = 0; for (int i = 0; i < data_size_; ++i) { for (int j = 0; j < data_size_; ++j) { for (int k = 0; k < data_size_; ++k) { index[0] = i; index[1] = j; index[2] = k; if (occupied_cell_list_[getIndexIn1D (index)]) { fillPad (index); numOfFilledPad++; } } } } // Update the hashtable and store the vector and point BOOST_FOREACH (typename HashMap::value_type entry, cell_hash_map_) { getIndexIn3D (entry.first, index); std::vector <int> pt_union_indices; getDataPtsUnion (index, pt_union_indices); // Needs at least 10 points (?) // NOTE: set as parameter later if (pt_union_indices.size () > 10) { storeVectAndSurfacePoint (entry.first, index, pt_union_indices, entry.second); //storeVectAndSurfacePointKNN(entry.first, index, entry.second); occupied_cell_list_[entry.first] = 1; } }
void CellPartitioning<T>::addEntity(const T& t) { size_t i = getCellIndex(t->getPosition()); mCells.at(i).insert(t); }