void create_random_indices( IndexContainer &idx , size_t N , size_t n ) { idx.resize( N ); for( size_t i = 0 ; i < N ; ++i ) idx[i]; std::random_shuffle( idx.begin() , idx.end() ); idx.resize( n ); }
typename IndexContainer::iterator sort_indices( const Container &v , IndexContainer &idx ) { idx.resize( v.size() ); for( size_t i = 0 ; i != idx.size() ; ++i ) idx[i] = i; auto iter = std::partition( idx.begin() , idx.end() , [&v]( size_t i ) { return std::isfinite( v[i] ); } ); std::sort( idx.begin() , iter , [&v]( size_t i1 , size_t i2 ) { return ( v[i1] < v[i2] ); } ); return iter; }
/// @warning all elements are destroyed void resize(size_type nrows, size_type ncols, T const& val) { m_data.resize(nrows*ncols, val); m_offsets.resize(nrows+1); for (size_type i = 0; i < nrows+1; ++i) { m_offsets[i] = i*ncols; } }
// remove all elements = val void erase(T const& val) { size_type counter; typename std::vector<T>::iterator pos; for (difference_type i = m_offsets.size()-2; i>=0; --i) { for (difference_type j = m_offsets[i+1]-m_offsets[i]-1; j>=0; --j) { pos = m_data.begin() + m_offsets[i] + j; if (*pos == val) { m_data.erase(pos); for (int k = i+1; k < m_offsets.size(); ++k) --m_offsets[k]; } } } }
void construct_component_index(IndexContainer& index, HeaderContainer& header) { build_components_header(index.begin(), std::back_inserter(header), index.end() - index.begin()); link_components(index.begin(), header.begin(), index.end() - index.begin(), header.end() - header.begin()); }
void construct_component_index(IndexContainer& index, HeaderContainer& header) { typedef typename IndexContainer::value_type Integer; build_components_header(index.begin(), std::back_inserter(header), Integer(index.end() - index.begin())); link_components(index.begin(), header.begin(), Integer(index.end() - index.begin()), Integer(header.end() - header.begin())); }
// insert new column if necessary T& operator() (size_type row, size_type col) { size_type const sr = size(row); if (col < sr) return m_data[ m_offsets[row] + col]; else { Int const n_new = col - sr + 1; typename std::vector<T>::iterator pos = m_data.begin() + m_offsets[row+1]; if (m_data.end()-pos < 0) // pos never can be greater than m_data.end(), otherwise m_offsets is wrong throw; m_data.insert(pos, n_new, T()); for (size_type k = row+1; k < m_offsets.size(); ++k) m_offsets[k] += n_new; return m_data[ m_offsets[row] + col]; } }
template<int dim> double ttt::AdherensJunctionSegmentationDijkstraCommand<dim>::ComputePath( const SkeletonVertexType & a, const SkeletonVertexType & b) { double weight = 0; typedef std::deque<IndexContainer<Index> > HeapType; typename DistanceImageType::Pointer distances = DistanceImageType::New(); distances->SetRegions(m_Plateness->GetLargestPossibleRegion()); distances->SetSpacing(m_Plateness->GetSpacing()); distances->SetOrigin(m_Plateness->GetOrigin()); distances->Allocate(); distances->FillBuffer(INFINITY); typename ColorImageType::Pointer colors = ColorImageType::New(); colors->SetRegions(m_Plateness->GetLargestPossibleRegion()); colors->SetSpacing(m_Plateness->GetSpacing()); colors->SetOrigin(m_Plateness->GetOrigin()); colors->Allocate(); colors->FillBuffer(COLOR_WHITE); Index sourceIndex = m_IndexToVertex.right.find(a)->second->GetPosition(); Index targetIndex = m_IndexToVertex.right.find(b)->second->GetPosition(); IndexContainer<Index> sourceIndexContainer; sourceIndexContainer.SetValue(0); sourceIndexContainer.SetIndex(sourceIndex); HeapType trialHeap; trialHeap.push_back(sourceIndexContainer); std::make_heap(trialHeap.begin(), trialHeap.end()); distances->SetPixel(sourceIndex, 0); bool found = false; int exploredNodes = 0; while (!trialHeap.empty() && !found) { IndexContainer<Index> current = trialHeap.front(); Index currentIndex = current.GetIndex(); double currentDistance = current.GetValue(); std::pop_heap(trialHeap.begin(), trialHeap.end()); trialHeap.pop_back(); colors->SetPixel(current.GetIndex(), COLOR_BLACK); exploredNodes++; if (exploredNodes % 10000 == 0) { //std::cout << exploredNodes << "/" << " Pendientes: " << trialHeap.size() << " " << currentDistance << std::endl; } if (currentIndex == targetIndex) { found = true; } else { std::vector<Index> neighs; this->GetNeighbors(currentIndex, neighs); for (typename std::vector<Index>::iterator it = neighs.begin(); it != neighs.end(); it++) { Index neigh = *it; char neighcolor = colors->GetPixel(neigh); if (neighcolor == COLOR_BLACK) continue; if (m_Labels->GetPixel(neigh) == a || m_Labels->GetPixel(neigh) == b) { double speedValue = m_Speed->GetPixel(neigh); double nextDistance = 1.0 / speedValue; double candDist = currentDistance + nextDistance; double neighDist = distances->GetPixel(neigh); if (candDist < neighDist) { IndexContainer<Index> current; current.SetValue(candDist); current.SetIndex(neigh); if (neighcolor == COLOR_GRAY) { typename HeapType::iterator findres = std::find( trialHeap.begin(), trialHeap.end(), current); findres->SetValue(candDist); std::update_heap_pos(trialHeap.begin(), trialHeap.end(), findres); } else { trialHeap.push_back(current); std::push_heap(trialHeap.begin(), trialHeap.end()); colors->SetPixel(neigh, COLOR_GRAY); } distances->SetPixel(neigh, candDist); } } } } } if (found) { bool backtrackingFinished = false; std::list<Index> path; path.clear(); Index currentIndex = targetIndex; int length = 0; while (!backtrackingFinished) { //PATH = PATH + current if (currentIndex == sourceIndex) { backtrackingFinished = true; } else { double currentValue = distances->GetPixel(currentIndex); weight += m_Speed->GetPixel(currentIndex); length++; path.push_back(currentIndex); Index minNeigh; double minValue = currentValue; std::vector<Index> neighbors; this->GetNeighbors(currentIndex, neighbors); for (typename std::vector<Index>::iterator itNeigh = neighbors.begin(); itNeigh != neighbors.end(); ++itNeigh) { Index neigh = *itNeigh; double neighValue = distances->GetPixel(neigh); if (neighValue < minValue) { minValue = neighValue; minNeigh = neigh; } } assert(currentValue > minValue); currentIndex = minNeigh; } } return weight / length; } else { return -1; } }
bool operator==(const IndexContainer & other) const { return other.GetIndex()[0] == m_Index[0] && other.GetIndex()[1] == m_Index[1] && other.GetIndex()[2] == m_Index[2]; }
bool operator<(const IndexContainer & other) const { return m_Time > other.GetValue(); }
IndexContainer(const IndexContainer<IndexType> & other) { m_Index = other.GetIndex(); m_Time = other.GetValue(); }
// returns the size of the row i size_type size(Int i) const { return m_offsets.at(i+1) - m_offsets[i]; }
size_type numRows() const { return m_offsets.size()-1; }
size_type capacityIndices() const { return m_offsets.capacity(); }
// reserve memory for IndexContainer void reserveIndices(size_type s) { m_offsets.reserve(s); }
/// @warning all elements are destroyed void resize(size_type nrows) { m_data.clear(); //m_data.reserve(1); // this is ti define m_data.begin() m_offsets.resize(nrows+1, Int(0)); }