void Mapper2d:: InitAddmask() { m_addmask_x0 = 0; m_addmask_y0 = 0; m_addmask_x1 = 0; m_addmask_y1 = 0; const ssize_t offset(static_cast<ssize_t>(ceil(grown_safe_distance / gridframe.Delta()))); for (ssize_t ix(-offset); ix <= offset; ++ix) { const double x2(sqr(ix * gridframe.Delta())); for (ssize_t iy(-offset); iy <= offset; ++iy) { int const cost(ComputeCost(sqrt(sqr(iy * gridframe.Delta()) + x2))); if (cost > m_travmap->freespace) { m_addmask.insert(make_pair(index_t(ix, iy), cost)); // update the addmask's bounding box if (ix < m_addmask_x0) m_addmask_x0 = ix; if (ix > m_addmask_x1) m_addmask_x1 = ix; if (iy < m_addmask_y0) m_addmask_y0 = iy; if (iy > m_addmask_y1) m_addmask_y1 = iy; } } } }
flat_playlist::index_optional_t flat_playlist::get_entry_index(uri const &uri_) const { entry_sequence_t const &entry_sequence = entries.get < sequence_tag > (); entry_sequence_t::const_iterator seq_iter = entries.project < sequence_tag > (get_uri_iterator_for(uri_)); if (seq_iter == entry_sequence.end()) return boost::none; return index_t(seq_iter - entry_sequence.begin()); }
void doeda_kanske(int x, int y) { /* Slumpa fram om personen dör eller inte */ if (Matris.doedsrisk < slumptal()) return; /* Personen dör */ Matris.person[index_t(x, y)].doed = 1; /* Uppdatera statistik */ Matris.doeda_idag++; Matris.doeda_totalt++; return; }
/* Simulerar en dag */ void simulera_dag() { int i, j; /* Rensa dagsstaistiken */ Matris.friska_idag = Matris.sjuka_idag = Matris.doeda_idag = Matris.smittade_idag = 0; for (i = 0; i < Matris.person_h; i++) for (j = 0; j < Matris.person_w; j++) { Matris.person[index_t(j, i)].smittad--; Matris.person[index_t(j, i)].cooldown--; /* Smittad och smittande? */ if (Matris.person[index_t(j, i)].smittad > 0 && Matris.person[index_t(j, i)].cooldown < 1 && !Matris.person[index_t(j, i)].doed) { smitta_kanske(j, i); /* Singla slant om personen ska dö eller inte */ doeda_kanske(j, i); } /* Personen blev frisk i dag */ if (!Matris.person[index_t(j, i)].smittad) Matris.friska_idag++; else if (Matris.person[index_t(j, i)].smittad > 0) Matris.sjuka_idag++; /* TODO: skriv ut cell */ } return; }
void GenerationalGarbageCollector::removeFromOldAddToNew( const Collectable *obj, CollectableArray &cs_new ) { assert( obj ); assert( obj->getGCIndex() < m_cs.size() ); assert( m_cs[ obj->getGCIndex() ] != 0 ); m_cs[ obj->getGCIndex() ] = 0; setGCIndex( obj, index_t( cs_new.size() ) ); cs_new.push_back( obj ); if( obj->getGCGeneration() < m_num_generations-1 ) { incGCGeneration( obj ); } }
void GenerationalGarbageCollector::add( const Collectable *c ) { if( 0 == c ) return; if( c->getGC() == this ) return; if( c->getGC() ) c->getGC()->remove( c ); { Mutex::ScopedLock lock( &m_lock ); setGC( c, this ); setGCIndex( c, index_t( m_cs.size() ) ); setGCGeneration( c, 0 ); m_cs.push_back( c ); } // m_lock #ifdef GC_DEBUG std::cerr << "add " << c << " to " << this << ", index = " << c->gc_index << std::endl; #endif }
void reorder_destructive_( order_iterator order_begin, order_iterator order_end, value_iterator v ) { using value_t = typename std::iterator_traits< value_iterator >::value_type; using index_t = typename std::iterator_traits< order_iterator >::value_type; using diff_t = typename std::iterator_traits< order_iterator >::difference_type; diff_t remaining = order_end - 1 - order_begin; for ( index_t s = index_t(); remaining > 0; ++ s ) { index_t d = order_begin[s]; if ( d == (diff_t) -1 ) continue; -- remaining; value_t temp = v[s]; for ( index_t d2; d != s; d = d2 ) { std::swap( temp, v[d] ); std::swap( order_begin[d], d2 = (diff_t) -1 ); -- remaining; } v[s] = temp; } }
/* Försöker smitta en närliggande person */ void smitta_kanske(int x, int y) { float n; int dx, dy, t, s; n = slumptal(); /* Smitta endast om personen har sådan otur */ if (Matris.smitta_risk < n) return; do { /* Slumpa fram en närliggande cell att smitta till */ dx = rand() % 3 - 1; dy = rand() % 3 - 1; } while (!dx && !dy); /* Personen kan inte smitta sig själv */ if ((t = index_t(x + dx, y + dy)) < 0) { /* Cellen är utanför kanterna, blir ingen smitta */ return; } /* Personen har redan smittats */ if (Matris.person[t].immun) return; /* slumpa fram antal dagar s som personen kommer vara sjuk */ s = rand() % (Matris.smitta_t_max - Matris.smitta_t_min + 1) + Matris.smitta_t_min; Matris.person[t].smittad = s; Matris.person[t].cooldown = 1; Matris.person[t].immun = 1; /* Uppdatera smittostatistik */ Matris.smittade_idag++; Matris.smittade_totalt++; return; }
void Mapper2d:: InitRemovemask() { m_removemask_x0 = 0; m_removemask_y0 = 0; m_removemask_x1 = 0; m_removemask_y1 = 0; for (addmask_t::const_iterator ii(m_addmask.begin()); ii != m_addmask.end(); ++ii) for (addmask_t::const_iterator jj(m_addmask.begin()); jj != m_addmask.end(); ++jj) { ssize_t const ix(ii->first.v0 - jj->first.v0); // "minus" because it's a convolution ssize_t const iy(ii->first.v1 - jj->first.v1); m_removemask.insert(make_pair(index_t(ix, iy), false)); if (ix < m_removemask_x0) m_removemask_x0 = ix; if (ix > m_removemask_x1) m_removemask_x1 = ix; if (iy < m_removemask_y0) m_removemask_y0 = iy; if (iy > m_removemask_y1) m_removemask_y1 = iy; } for (addmask_t::const_iterator ii(m_addmask.begin()); ii != m_addmask.end(); ++ii) m_removemask[ii->first] = true; }
index_t operator-(char const c) const { return index_t(i - as_i(c), j - as_j(c)); }
index_t operator+(char const c) const { return index_t(i + as_i(c), j + as_j(c)); }
void Polygon::push(const Ring &v, int beg, bool within) { int sz = v.size(); double coords[2]; mbr(v.xa(), v.ya(), v.xb(), v.yb()); int f = 0, offset = 0; bool turn = false; double xa, ya, xb, yb; for (int i = beg; i < beg + sz; ) { int ind = i % sz; OuterRing rng; xa = xb = v[ind].x(); ya = yb = v[ind].y(); rng.push(xa, ya); if (turn && 0 == offset) { if (0 == f % 2) b_[f].push_back(index_t(xa, -f)); else b_[f].push_back(index_t(ya, -f)); } offset = 0; turn = false; for (int j = (ind+1) % sz; true; j = (j+1) % sz) { const point_t &p = v[j]; rng.push(p.x(), p.y()); if (p.x() < xa) xa = p.x(); if (p.x() > xb) xb = p.x(); if (p.y() < ya) ya = p.y(); if (p.y() > yb) yb = p.y(); coords[(f+1)%2] = p.x(); coords[f%2] = p.y(); if (fuzzy_eq(coords[0], mbr_[f])) { turn = fuzzy_eq(coords[1], mbr_[(f+1)%4]); break; } else if (fuzzy_eq(coords[1], mbr_[(f+1)%4])) { rng.push(corner_[f].x(), corner_[f].y()); offset = 1; turn = true; break; } } int bak = rng.size() - 1 - offset; if (within) { int t = (f + offset) % 4; int kk = (1 == offset || !turn) ? outer_.size() : -f-1; if (0 == t % 2) b_[t].push_back(index_t(rng[bak].x(), kk)); else b_[t].push_back(index_t(rng[bak].y(), kk)); if (1 == offset) rng.corner(); } if (turn) f = (f+1) % 4; i += bak; rng.mbr(xa, ya, xb, yb); outer_.push_back(rng); } if (within) _sort(f%4); }
void Mapper2d::buffered_obstacle_adder:: operator () (ssize_t ix, ssize_t iy) { count += m2d->AddOneObstacle(index_t(ix, iy), force, cb); }
int main(int argc, char **argv) { int i = 0; int paus = 0, n, x, y, t; srand(time(NULL)); if (argc < 7) { fprintf(stderr, "Usage: %s <individmatrisstorlek (N×N)> <smittotid minimum> <smittotid maximum> <smittorisk [0,1]> <dödsrisk [0,1]> <långsam simulering>\n", argv[0]); return -1; } n = atoi(argv[1]); Matris.smitta_t_min = atoi(argv[2]); Matris.smitta_t_max = atoi(argv[3]); sscanf(argv[4], "%f", &Matris.smitta_risk); sscanf(argv[5], "%f", &Matris.doedsrisk); paus = atoi(argv[6]); matris_init(n); fprintf(stderr, "Ange koordinater for nysmittade. Markera att du är färdig genom att ange -1 som X-koordinat\n"); do { x = -1; fprintf(stderr, "X-koordinat i %i×%i-matris att placera smittad: ", n, n); fscanf(stdin, "%i", &x); if (x < 0) break; fprintf(stderr, "Y-koordinat i %i×%i-matris att placera smittad: ", n, n); fscanf(stdin, "%i", &y); t = index_t(x, y); if (t < 0) { fprintf(stderr, "Ogiltig koordinat!\n"); continue; } fprintf(stderr, "Dagar personen kommer vara sjuk: "); fscanf(stdin, "%i", &Matris.person[t].smittad); Matris.person[t].immun = 1; Matris.person[t].cooldown = 1; Matris.smittade_totalt++; } while (1); do { simulera_dag(); i++; /* Skriv ut statistik */ fprintf(stdout, "Antal insjuknade i dag : %.5i\n", Matris.smittade_idag); fprintf(stdout, "Antal tillfrisknade i dag : %.5i\n", Matris.friska_idag); fprintf(stdout, "Antal sjuka totalt i dag : %.5i\n", Matris.sjuka_idag); fprintf(stdout, "Antal smittade totalt : %.5i\n", Matris.smittade_totalt); fprintf(stdout, "Antal dödsfall i dag : %.5i\n", Matris.doeda_idag); fprintf(stdout, "Antal dödsfall totalt : %.5i\n", Matris.doeda_totalt); fprintf(stdout, "---------------------------------\n"); /* Om simuleringen skall köra en dag varannan sekund, pausa en stund */ if (paus) sleep(2); } while (Matris.sjuka_idag > 0); fprintf(stdout, "Antalet sjuka är nu noll, stoppar simulering. Det har gått %i dag(ar)\n", i); fprintf(stdout, "Totalt smittades %i individer i populationen (%i %%)\n", Matris.smittade_totalt, Matris.smittade_totalt * 100 / (Matris.person_w * Matris.person_h)); return 0; }
/// \returns The given [ts::index_t]() advanced backwards by the given [ts::distance_t](). /// \module types constexpr index_t operator-(const index_t& lhs, const difference_t& rhs) noexcept { return index_t(make_unsigned(make_signed(get(lhs)) - get(rhs))); }
void Curve::intersect(StandardScene *scene, osg::Plane plane, osg::Polytope polytope, osg::Quat quat, bool culling) { if (!scene) return; this->m_segments.clear(); osgUtil::PlaneIntersector *picker = new osgUtil::PlaneIntersector( osgUtil::Intersector::CoordinateFrame::WINDOW, plane, polytope); osgUtil::IntersectionVisitor iv(picker); iv.setTraversalMask(StandardScene::NodeMask::Pickable); scene->screen().camera()->accept(iv); if (picker->containsIntersections()) { osgUtil::PlaneIntersector::Intersections &raws(picker->getIntersections()); // Detects and culls occlusion. if (culling) { // Rotates polylines to xy-plane. std::vector<std::vector<bool> > flags(raws.size()); for (size_t i = 0; i < raws.size(); ++i) { size_t size = raws[i].polyline.size(); flags[i].resize(size, true); for (size_t j = 0; j < size; ++j) { raws[i].polyline[j] = quat * raws[i].polyline[j]; raws[i].polyline[j].z() = 0; } } // Sorts knots with respect to x-coordinate. std::vector<std::pair<real_t, index_t> > knots; for (size_t i = 0; i < raws.size(); ++i) { for (size_t j = 0; j < raws[i].polyline.size(); ++j) { knots.push_back(std::pair<real_t, index_t>(raws[i].polyline[j].x(), index_t(i, j))); } } std::sort(knots.begin(), knots.end(), pred_t()); // Marks occluded knots. for (size_t i = 0; i < raws.size(); ++i) { for (size_t j = 0; j < raws[i].polyline.size() - 1; ++j) { real_t x1 = raws[i].polyline[j].x(), x2 = raws[i].polyline[j + 1].x(); real_t y1 = raws[i].polyline[j].y(), y2 = raws[i].polyline[j + 1].y(); auto beg = std::lower_bound(knots.begin(), knots.end(), std::make_pair(std::min<real_t>(x1, x2), index_t()), pred_t()) + 1; auto end = std::upper_bound(knots.begin(), knots.end(), std::make_pair(std::max<real_t>(x1, x2), index_t()), pred_t()) - 1; for (auto iter = beg; iter < end; ++iter) { real_t x = raws[iter->second.line].polyline[iter->second.knot].x(); real_t y = raws[iter->second.line].polyline[iter->second.knot].y(); real_t det = ((x - x1) * (y2 - y) - (y - y1) * (x2 - x)) * (x2 - x1); if (det > 0) flags[iter->second.line][iter->second.knot] = false; } } } // Reconstructs polylines. std::vector<std::vector<osg::Vec3d> > polylines; for (size_t i = 0; i < raws.size(); ++i) { polylines.push_back(std::vector<osg::Vec3d>()); for (size_t j = 0; j < raws[i].polyline.size(); ++j) { if (flags[i][j]) polylines.back().push_back(raws[i].polyline[j]); else { if (polylines.back().size() < 2) polylines.back().clear(); else polylines.push_back(std::vector<osg::Vec3d>()); } } if (polylines.back().size() < 2) polylines.pop_back(); } // Translates to matrix storage. for (size_t i = 0; i < polylines.size(); ++i) { size_t size = polylines[i].size(); this->m_segments.push_back(Segment(3, size)); for (size_t j = 0; j < size; ++j) { this->m_segments.back().col(j) = algo::Util::toVector(polylines[i][j]); } this->m_segments.back().row(2).setZero(); } } else { // Translates to matrix storage. for (size_t i = 0; i < raws.size(); ++i) { size_t size = raws[i].polyline.size(); this->m_segments.push_back(Segment(3, size)); for (size_t j = 0; j < size; ++j) { this->m_segments.back().col(j) = algo::Util::toVector(quat * raws[i].polyline[j]); } this->m_segments.back().row(2).setZero(); } } } }
size_t Mapper2d:: UpdateObstacles(index_buffer_t const * add, bool force_add, index_buffer_t * remove, draw_callback * cb) { size_t count(0); m_swipe_hole_buffer.clear(); m_swipe_repair_buffer.clear(); if (remove) { // remove former W-obstacles index_buffer_t spurious; for (index_buffer_t::const_iterator is(remove->begin()); is != remove->end(); ++is) { ssize_t const ix0(is->v0); ssize_t const iy0(is->v1); if ( ! m_travmap->IsWObst(ix0, iy0)) { PDEBUG("skip source %zd %zd because not W-obst\n", ix0, iy0); spurious.insert(*is); continue; } if (m_travmap->IsValid(ix0, iy0)) { PDEBUG("wipe source %zd %zd\n", ix0, iy0); ++count; m_travmap->SetFree(ix0, iy0, cb); } else PDEBUG("source %zd %zd is out of bounds\n", ix0, iy0); } // should normally be a no-op for (index_buffer_t::const_iterator is(spurious.begin()); is != spurious.end(); ++is) remove->erase(*is); // determine the areas influenced by the former W-obstacles: // inner areas are later wiped, outer areas only scanned for // already existing W-obstacles for (index_buffer_t::const_iterator is(remove->begin()); is != remove->end(); ++is) { ssize_t const ix0(is->v0); ssize_t const iy0(is->v1); for (removemask_t::const_iterator ir(m_removemask.begin()); ir != m_removemask.end(); ++ir) { ssize_t const tx(ix0 + ir->first.v0); ssize_t const ty(iy0 + ir->first.v1); if (m_travmap->IsWObst(tx, ty)) { PDEBUG("add again target %zd %zd because W-obst\n", tx, ty); m_swipe_repair_buffer.insert(index_t(tx, ty)); } else if (ir->second && ( ! m_travmap->IsFree(tx, ty))) { PDEBUG("wipe target %zd %zd\n", tx, ty); m_swipe_hole_buffer.insert(index_t(tx, ty)); } } } // wipe out the region of influence of each removed W-obstacle for (index_buffer_t::const_iterator iw(m_swipe_hole_buffer.begin()); iw != m_swipe_hole_buffer.end(); ++iw) m_travmap->SetFree(iw->v0, iw->v1, cb); count += m_swipe_hole_buffer.size(); // add back all the W-obstacles which have a region of influence // that intersects the set of cells that were just wiped (set // force add to true, because we did not set these cells to // W-obstacle cost) for (index_buffer_t::const_iterator ia(m_swipe_repair_buffer.begin()); ia != m_swipe_repair_buffer.end(); ++ia) count += AddOneObstacle(*ia, true, cb); } // forcing optional, depends on user if (add) for (index_buffer_t::const_iterator ia(add->begin()); ia != add->end(); ++ia) count += AddOneObstacle(*ia, force_add, cb); return count; }