void KCleaf::sampleCtr( // sample from leaf node KMpoint c, // the sampled point (returned) KMorthRect &bnd_box) // bounding box for current node { int ri = kmRanInt(n_data); // generate random index kmCopyPt(kcDim, kcPoints[bkt[ri]], c); // copy to destination }
void KMdata::sampleCtrs( // sample points randomly KMcenterArray sample, // where to store sample int k, // number of points to sample bool allowDuplicate) // sample with replacement? { if (!allowDuplicate) // duplicates not allowed assert(k <= nPts); // can't do more than nPts int* sampIdx = new int[k]; // allocate index array for (int i = 0; i < k; i++) { // sample each point of sample int ri = kmRanInt(nPts); // random index in pts if (!allowDuplicate) { // duplicates not allowed? bool dupFound; // duplicate found flag do { // repeat until successful dupFound = false; for (int j = 0; j < i; j++) { // search for duplicates if (sampIdx[j] == ri) { // duplicate found dupFound = true; ri = kmRanInt(nPts); // try again break; } } } while (dupFound); } kmCopyPt(dim, pts[ri], sample[i]); // copy sample point sampIdx[i] = ri; // save index } delete [] sampIdx; }
//---------------------------------------------------------------------- // swapOneCenter // Swaps one center point with a sample point. Optionally we make // sure that the new point is not a duplicate of any of the centers // (including the point being replaced). //---------------------------------------------------------------------- void KMfilterCenters::swapOneCenter( // swap one center bool allowDuplicate) // allow duplicate centers { int rj = kmRanInt(kCtrs); // index of center to replace int dim = getDim(); KMpoint p = kmAllocPt(dim); // alloc replacement point pts->sampleCtr(p); // sample a replacement if (!allowDuplicate) { // duplicates not allowed? bool dupFound; // was a duplicate found? do { // repeat until successful dupFound = false; for (int j = 0; j < kCtrs; j++) { // search for duplicates if (kmEqualPts(dim, p, ctrs[j])) { dupFound = true; pts->sampleCtr(p); // try again break; } } } while (dupFound); } kmCopyPt(dim, p, ctrs[rj]); // copy sampled point if (kmStatLev >= STEP) { // output swap info *kmOut << "\tswapping: "; kmPrintPt(p, getDim(), true); *kmOut << "<-->Center[" << rj << "]\n"; } kmDeallocPt(p); // deallocate point storage invalidate(); // distortions now invalid }
void KMdata::sampleCtr( // sample a center point KMcenter sample) // where to store sample { int ri = kmRanInt(nPts); // generate random index kmCopyPt(dim, pts[ri], sample); // copy to destination }