home *** CD-ROM | disk | FTP | other *** search
- /*
- The Fixer - A GIMP plug-in for fixing borders and applying themes to images
- Copyright (C) 2000 Paul Francis Harrison
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
- /* Nearest vector classifier */
-
- template<class t>
- struct Classifier {
- struct Node {
- int radius;
- t branchChildItem;
- Node *mainChild, *branchChild;
-
- Node() { mainChild = branchChild = 0; }
- ~Node() { delete mainChild; delete branchChild; }
-
- void nearest(t &find,
- int yourDist,
- t &best,
- int &bestDist) {
- if (!this)
- return;
-
- if (bestDist == 0 || yourDist-radius > bestDist)
- return;
-
- int branchDist = _distance(find,branchChildItem);
-
- if (branchDist < bestDist) {
- bestDist = branchDist;
- best = branchChildItem;
- }
-
- if (yourDist < branchDist) {
- mainChild->nearest(find, yourDist, best, bestDist);
- branchChild->nearest(find, branchDist, best, bestDist);
- } else {
- branchChild->nearest(find, branchDist, best, bestDist);
- mainChild->nearest(find, yourDist, best, bestDist);
- }
- }
- };
-
- Node *buildNode(int nItems, t *items, t &myItem) {
- if (nItems == 1) {
- myItem = items[0];
- return 0;
- }
-
- Node *nu = new Node;
-
- t means[2];
- int partitionPoint;
-
- /* Pick two starting means arbitrarily */
- means[0] = items[0];
- means[1] = items[nItems/2];
-
- int iterations = 0;
- for(;;) {
- /* Partition, ala quicksort. Test this!!!!!!!!!!!! */
- int lower=0, upper=nItems-1;
- bool any = false;
- for(;;) {
- while(lower <= upper &&
- _distance(items[lower],means[0]) >
- _distance(items[lower],means[1]))
- lower++;
- while(lower <= upper &&
- _distance(items[upper],means[1]) >
- _distance(items[upper],means[0]))
- upper--;
-
- if (lower > upper) break;
-
- t temp = items[lower];
- items[lower] = items[upper];
- items[upper] = temp;
- lower++; upper--;
- any = true;
- }
-
- partitionPoint = lower;
-
- /* Stop if partition is stable, or we have been iterating
- for too long and don't seem to be getting anywhere, or
- the partition has somehow become degenerate, all in one and
- none in the other. */
- if (!any || iterations > 8 ||
- partitionPoint == 0 || partitionPoint == nItems) break;
-
- iterations++;
-
- /* Calculate means */
- means[0] = items[0];
- for(int i=1;i<partitionPoint;i++)
- means[0] = means[0] + items[i];
- means[0] = means[0] / partitionPoint;
-
- means[1] = items[partitionPoint];
- for(int i=partitionPoint+1;i<nItems;i++)
- means[1] = means[1] + items[i];
- means[1] = means[1] / (nItems-partitionPoint);
- }
-
- /* Did k-means fail utterly? If so just divide arbitrarily */
- if (partitionPoint == nItems || partitionPoint == 0) {
- //printf("Bah.\n");
- partitionPoint = nItems/2;
- }
-
- /* Build main child with larger partition, branch child with smaller */
- if (partitionPoint > nItems - partitionPoint) {
- nu->mainChild = buildNode(partitionPoint,items, myItem);
- nu->branchChild = buildNode(nItems-partitionPoint,items+partitionPoint,
- nu->branchChildItem);
- } else {
- nu->mainChild = buildNode(nItems-partitionPoint,items+partitionPoint,
- myItem);
- nu->branchChild = buildNode(partitionPoint,items,nu->branchChildItem);
- }
-
- /* Calculate radius */
- nu->radius = 0;
- for(int i=0;i<nItems;i++) {
- int d = _distance(myItem,items[i]);
- if (d > nu->radius)
- nu->radius = d;
- }
-
- return nu;
- }
-
- Node *tree;
- t rootItem;
-
- Classifier() : tree(0) { }
- ~Classifier() { delete tree; }
-
- void build(int nItems,t *items) {
- delete tree;
- tree = buildNode(nItems, items, rootItem);
- }
-
- t nearest(t &find, t hint, int &bestDist) {
- t best = hint;
- bestDist = _distance(hint,find);
-
- int rootDist = _distance(find,rootItem);
- if (rootDist < bestDist) {
- bestDist = rootDist;
- best = rootItem;
- }
-
- tree->nearest(find,
- rootDist,
- best,
- bestDist);
- return best;
- }
- };
-
-
-
-