libeblearn
/home/rex/ebltrunk/core/libeblearn/include/bbox.hpp
00001 /***************************************************************************
00002  *   Copyright (C) 2011 by Pierre Sermanet *
00003  *   pierre.sermanet@gmail.com *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source
00014  *       Initiative (http://www.opensource.org) must display the
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef BBOX_HPP_
00034 #define BBOX_HPP_
00035 
00036 namespace ebl {
00037 
00038   // sorting ///////////////////////////////////////////////////////////////////
00039 
00040   template <typename T>
00041   void bboxes::sort_by_difference(svector<midx<T> >& inputs) {
00042     if (inputs.size() != this->size())
00043       eblerror("expected same number as input as bboxes but got "
00044                << inputs.size() << " and " << this->size());
00045     // note: inputs and boxes are assumned to be sorted by confidence already
00046     double dmax, smax;
00047     for (int i = 0; i < (int) inputs.size() - 1; ++i) {
00048       int id = i;
00049       dmax = 0, smax = 0;
00050       // find most different sample below
00051       for (int j = i + 1; j < (int) inputs.size(); j++) {
00052         // compute total distance of input j with all previous samples
00053         midx<T> e1 = inputs[j];
00054         double d = 0, s = 0;
00055         for (int k = 0; k < i + 1; k++) {
00056           midx<T> e2 = inputs[k];
00057           if (!e1.same_dim(e2)) d += idx_l2common(e1, e2);
00058           else d += idx_l2(e1, e2);
00059           // accumulate dimensions differences
00060           for (uint l = 0; l < e1.dim(0); ++l) {
00061             idx<T> e11 = e1.get(l), e22 = e2.get(l);
00062             for (int m = 0; m < e11.order(); ++m)
00063               s += abs(e11.dim(m) - e22.dim(m));
00064           }
00065         }
00066         if (d > dmax || s > smax) {
00067           id = j;
00068           dmax = d;
00069           smax = s;
00070         }
00071       }
00072       // swap
00073       if (id != i + 1) {
00074         // swap inputs
00075         midx<T> etmp = inputs[id];
00076         inputs[id] = inputs[i + 1];
00077         inputs[i + 1] = etmp;
00078         // swap boxes
00079         this->swap(id, i + 1);
00080       }
00081     }
00082   }
00083 
00084 } // end namespace ebl
00085 
00086 #endif /* BBOX_HPP_ */