std::vector<CurveIntersection> Curve::intersectSelf(Coord eps) const { std::vector<CurveIntersection> result; // Monotonic segments cannot have self-intersections. // Thus, we can split the curve at roots and intersect the portions. std::vector<Coord> splits; std::auto_ptr<Curve> deriv(derivative()); splits = deriv->roots(0, X); if (splits.empty()) { return result; } deriv.reset(); splits.push_back(1.); boost::ptr_vector<Curve> parts; Coord previous = 0; for (unsigned i = 0; i < splits.size(); ++i) { if (splits[i] == 0.) continue; parts.push_back(portion(previous, splits[i])); previous = splits[i]; } Coord prev_i = 0; for (unsigned i = 0; i < parts.size()-1; ++i) { Interval dom_i(prev_i, splits[i]); prev_i = splits[i]; Coord prev_j = 0; for (unsigned j = i+1; j < parts.size(); ++j) { Interval dom_j(prev_j, splits[j]); prev_j = splits[j]; std::vector<CurveIntersection> xs = parts[i].intersect(parts[j], eps); for (unsigned k = 0; k < xs.size(); ++k) { // to avoid duplicated intersections, skip values at exactly 1 if (xs[k].first == 1. || xs[k].second == 1.) continue; Coord ti = dom_i.valueAt(xs[k].first); Coord tj = dom_j.valueAt(xs[k].second); CurveIntersection real(ti, tj, xs[k].point()); result.push_back(real); } } } return result; }
int32 SnowView::SnowMakerThread(void *p_this) { SnowView *_this = (SnowView *)p_this; BView *p = _this->Parent(); BRect portion(0,0,(_this->fCachedWsWidth/PORTION_GRAN)-1, (_this->fCachedWsHeight/PORTION_GRAN)-1); int nf = _this->fNumFlakes; BRegion reg(BRect(-1,-1,-1,-1)); while (p && _this->fAttached) { snooze(INTERVAL/(10*(nf?nf:1))); int32 cw = _this->fCurrentWorkspace; bool drawThisOne = false; //printf("processing flake %d...\n", current); //for (; (current%(fNumFlakes/4); current++) if (reg.Intersects(portion)) { for (int i = 0; !drawThisOne && i < nf; i++) { /* if we find at least one flake in this rect, draw it */ if ((_this->fFlakes[cw][i].weight) && ( portion.Intersects(BRect(_this->fFlakes[cw][i].opos - BPoint(4,4), _this->fFlakes[cw][i].opos + BPoint(4,4))) || portion.Intersects(BRect(_this->fFlakes[cw][i].pos - BPoint(4,4), _this->fFlakes[cw][i].pos + BPoint(4,4))))) { drawThisOne = true; } } } //if (!drawThisOne) //printf("!Invalidate(%f, %f, %f, %f)\n", portion.left, portion.top, portion.right, portion.bottom); /* avoid deadlock on exit */ if (drawThisOne && (_this->LockLooperWithTimeout(2000) == B_OK)) { // printf("Invalidate(%f, %f, %f, %f)\n", portion.left, portion.top, portion.right, portion.bottom); p->Invalidate(portion); _this->UnlockLooper(); } portion.OffsetBy(_this->fCachedWsWidth/PORTION_GRAN, 0); if (portion.left >= _this->fCachedWsWidth) { /* right wrap */ //printf("rigth wrap to %ld\n", _this->fCachedWsWidth); portion.OffsetTo(0, portion.top+(_this->fCachedWsHeight/PORTION_GRAN)); } if (portion.top >= _this->fCachedWsHeight) { portion.OffsetTo(0,0); /* avoid deadlock on exit */ if (_this->LockLooperWithTimeout(5000) == B_OK) { //printf("calculating flakes...\n"); _this->Calc(); //printf("done calculating flakes.\n"); _this->GetClippingRegion(®); //printf("Region:\n"); //reg.PrintToStream(); _this->UnlockLooper(); } } } #if 0 BView *p = _this->Parent(); while (p && _this->fAttached) { snooze(INTERVAL/_this->fNumFlakes); //printf("processing flake %d...\n", current); //for (; (current%(fNumFlakes/4); current++) /* avoid deadlock on exit */ if (_this->LockLooperWithTimeout(2000) == B_OK) { if (_this->fFlakes[_this->fCurrentWorkspace][current].weight) { p->Invalidate(BRect(_this->fFlakes[_this->fCurrentWorkspace][current].opos - BPoint(4,4), _this->fFlakes[_this->fCurrentWorkspace][current].opos + BPoint(4,4))); p->Invalidate(BRect(_this->fFlakes[_this->fCurrentWorkspace][current].pos - BPoint(4,4), _this->fFlakes[_this->fCurrentWorkspace][current].pos + BPoint(4,4))); } _this->UnlockLooper(); current++; current %= _this->fNumFlakes; if (!current) { /* avoid deadlock on exit */ if (_this->LockLooperWithTimeout(2000) == B_OK) { printf("calculating flakes...\n"); _this->Calc(); printf("done calculating flakes.\n"); _this->UnlockLooper(); } } } } #endif return B_OK; }
void Linear_Kernel_Kmeans::InitKmeans(const bool labelsInitialized) { if(labelsInitialized==false) { // kmeans++ initialization, details see the kmeans++ paper Array2dC<int> centers(1,k); // index for initial centers Array2dC<double> portion(1,n); Array2dC<double> portion_backup(1,n); std::fill_n(portion.buf,n,DBL_MAX); int added = 0; #if defined ( _WIN32 ) unsigned int rr; rand_s(&rr); while(rr>=n) rand_s(&rr); #else int rr; rr = random(); while(rr>=n) rr=random(); #endif centers.buf[0] = rr; std::fill_n(labels,n,0); added = 1; while(added<=k) { #pragma omp parallel for for(int i=0;i<n;i++) { double v; if(useMedian==false) v = x_square[i] + x_square[centers.buf[added-1]] - 2*std::inner_product(data.p[i],data.p[i]+m,data.p[centers.buf[added-1]],0.0); else { v = 0; for(int j=0;j<m;j++) v += fabs(data.p[i][j]-data.p[centers.buf[added-1]][j]); } if(v<portion.buf[i]) { portion.buf[i] = v; labels[i] = added - 1; } } if(added<k) { for(int i=1;i<n;i++) portion.buf[i] += portion.buf[i-1]; #if defined ( _WIN32 ) if(portion.buf[n-1]<UINT_MAX) std::copy(portion.buf,portion.buf+n,portion_backup.buf); else for(int i=0;i<n;i++) portion_backup.buf[i] = portion.buf[i]/portion.buf[n-1]*UINT_MAX; rand_s(&rr); while(rr>portion_backup.buf[n-1]) rand_s(&rr); #else if(portion.buf[n-1]<RAND_MAX) std::copy(portion.buf,portion.buf+n,portion_backup.buf); else for(int i=0;i<n;i++) portion_backup.buf[i] = portion.buf[i]/portion.buf[n-1]*RAND_MAX; rr = random(); while(rr>portion.buf[n-1]) rr = random(); #endif int pos = 0; while(pos<n && portion_backup.buf[pos]<rr) pos++; centers.buf[added]=pos; for(int i=n-1;i>0;i--) portion.buf[i] -= portion.buf[i-1]; } added++; } } std::fill_n(counts,k,0); for(int i=0;i<n;i++) counts[labels[i]]++; }