"Fossies" - the Fresh Open Source Software Archive

Member "gama-2.05/lib/gnu_gama/local/median/g2d_helper.h" (10 May 2019, 9564 Bytes) of package /linux/privat/gama-2.05.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "g2d_helper.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.04_vs_2.05.

    1 /*
    2     GNU Gama C++ library
    3     Copyright (C) 1999  Jiri Vesely <vesely@gama.fsv.cvut.cz>
    4                   2001, 2018  Ales Cepek <cepek@gnu.org>
    5 
    6     This file is part of the GNU Gama C++ library.
    7 
    8     This library is free software; you can redistribute it and/or modify
    9     it under the terms of the GNU General Public License as published by
   10     the Free Software Foundation; either version 3 of the License, or
   11     (at your option) any later version.
   12 
   13     This library is distributed in the hope that it will be useful,
   14     but WITHOUT ANY WARRANTY; without even the implied warranty of
   15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16     GNU General Public License for more details.
   17 
   18     You should have received a copy of the GNU General Public License
   19     along with GNU Gama.  If not, see <http://www.gnu.org/licenses/>.
   20 */
   21 
   22 /*********************************************************************
   23  * helper functions and classes:                                     *
   24  * ----------------------------------------------------------------- *
   25  * - typedef std::vector<LocalPoint> Helper_list;                    *
   26  * - inline int signum(const double& d1)                             *
   27  * - inline double g2d_sqr(const double& d)                          *
   28  * - enum Solution_state_tag                                         *
   29  * - enum Observation_types                                          *
   30  * - inline Observation_types ObservationType(Observation* m)        *
   31  * - inline double g2d_distance(const LocalPoint& b1, const Bod& b2) *
   32  * - inline bool g2d_even(std::vector<double>::size_type& x)         *
   33  * - class Select_solution_g2d                                       *
   34  * - class Statistics_g2d                                            *
   35  * - class Transformation_g2d                                        *
   36  * - class SimilarityTr2D : public Transformation_g2d                *
   37  *********************************************************************/
   38 
   39 #ifndef gama_local_g2d_helper_h_GNU_gama_local_Median_G_fce_H
   40 #define gama_local_g2d_helper_h_GNU_gama_local_Median_G_fce_H
   41 
   42 #include <algorithm>
   43 #include <gnu_gama/local/gamadata.h>
   44 #include <gnu_gama/local/median/g2d_exception.h>
   45 
   46 namespace GNU_gama { namespace local {
   47 
   48   // --------------------------------------------------------------
   49 
   50   typedef std::vector<LocalPoint> Helper_list;
   51 
   52   // --------------------------------------------------------------
   53   // inline int signum(const double& d1) wass added into
   54   // gnu_gama/local-1.1.61 due to problems with MSC (AC)
   55 
   56   inline int signum(double d1)
   57     {
   58       if(d1 < 0) return -1;
   59       if(d1 > 0) return  1;
   60       return 0;
   61     }
   62 
   63   // --------------------------------------------------------------
   64 
   65   inline double g2d_sqr(const double& d)
   66     {
   67       return (d*d);
   68     }
   69 
   70   // --------------------------------------------------------------
   71 
   72   enum Solution_state_tag
   73   {
   74     missing_init = -2,
   75     calculation_not_done = -1,
   76     no_solution = 0,
   77     unique_solution = 1,
   78     ambiguous_solution = 2,
   79     calculation_done = 3
   80   };
   81 
   82   // --------------------------------------------------------------
   83 
   84   enum Observation_types
   85   {
   86     is_Distance = 1,
   87     is_Direction = 10,
   88     is_Angle = 100
   89   };
   90 
   91   // --------------------------------------------------------------
   92 
   93   inline Observation_types ObservationType(const Observation* m)
   94     {
   95       if(/* const Distance  *d =*/ dynamic_cast<const Distance* >(m) )
   96     return is_Distance;
   97 
   98       if(/* const Direction *s =*/ dynamic_cast<const Direction*>(m) )
   99         return is_Direction;
  100 
  101       return is_Angle;
  102     }
  103 
  104   // --------------------------------------------------------------
  105   inline double g2d_distance(const LocalPoint& b1, const LocalPoint& b2)
  106     {
  107       using namespace std;
  108       return sqrt(g2d_sqr(b1.x()-b2.x())+g2d_sqr(b1.y()-b2.y()));
  109     }
  110 
  111   // --------------------------------------------------------------
  112   inline bool g2d_even(std::vector<double>::size_type& x)
  113     {
  114       // using namespace std;
  115       // return (fmod(x,2) == 0);
  116       return x%2 == 0;
  117     }
  118 
  119 
  120   // --------------------------------------------------------------
  121   // in the case of ambiguous (equivocal) solution, the one is chosen
  122   // to be in accordance with the others
  123 
  124   class Select_solution_g2d
  125     {
  126     private:
  127 
  128       Solution_state_tag state_;
  129       LocalPoint   B1, B2;
  130       PointData* SB;
  131       ObservationList* SM;
  132 
  133     public:
  134       Select_solution_g2d(PointData* sb, ObservationList* sm)
  135         : state_(calculation_not_done), SB(sb), SM(sm)
  136         {
  137         }
  138       Select_solution_g2d(LocalPoint& b1, LocalPoint& b2, PointData* sb,
  139                           ObservationList* sm) :
  140         state_(calculation_not_done), B1(b1), B2(b2), SB(sb), SM(sm)
  141         {
  142         }
  143       void calculation();
  144       void calculation(LocalPoint b1, LocalPoint b2)
  145         {
  146           state_ = calculation_not_done;
  147           B1 = b1; B2 = b2;
  148           calculation();
  149         }
  150       LocalPoint Solution()
  151         {
  152           if(state_ == calculation_not_done)
  153             throw g2d_exc("Select_solution_g2d: calculation not done");
  154           if(state_ == no_solution)
  155             throw g2d_exc("Select_solution_g2d: ambiguous solution");
  156           return B1;
  157         }
  158       int state() const
  159         {
  160           return (state_ > no_solution ? unique_solution : state_);
  161         }
  162 
  163     };  // class Select_solution_g2d
  164 
  165 
  166   // --------------------------------------------------------------
  167   // Statistics_g2d - calculation of medina from coordinate list
  168 
  169   class Statistics_g2d
  170     {
  171     private:
  172 
  173       Helper_list* PS;
  174       LocalPoint median;
  175       Solution_state_tag state_;
  176 
  177     public:
  178       Statistics_g2d() : state_(missing_init)
  179         {
  180         }
  181       Statistics_g2d(Helper_list* ps)
  182         : PS(ps), state_(calculation_not_done)
  183         {
  184         }
  185       void calculation();
  186       void calculation(Helper_list* ps)
  187         {
  188           state_ = calculation_not_done;
  189           PS = ps;
  190           calculation();
  191         }
  192       Solution_state_tag state() const
  193         {
  194           return state_;
  195         }
  196       LocalPoint Median()       // resulting coordinate
  197         {
  198           if(state_ < no_solution)
  199             throw g2d_exc("Statistics_g2d: calculation not done");
  200 
  201           return median;
  202         }
  203 
  204     };  // class Statistics_g2d
  205 
  206 
  207   // --------------------------------------------------------------
  208   // base transfromation class
  209 
  210   class Transformation_g2d
  211     {
  212     protected:
  213 
  214       PointData& SB;              // point list in target syste
  215       PointData& local;           // point list in local system
  216       PointIDList& computed;      // list of computed points
  217       PointData transf_points_;   // points transformed into target system
  218       virtual void reset() = 0;
  219 
  220       Solution_state_tag state_;
  221 
  222     public:
  223       Transformation_g2d(PointData& sb, PointData& locl,
  224                          PointIDList& comp)
  225         : SB(sb), local(locl), computed(comp), state_(calculation_not_done)
  226         {
  227         }
  228       virtual ~Transformation_g2d()
  229         {
  230           transf_points_.erase(transf_points_.begin(), transf_points_.end());
  231         }
  232       void reset(PointData& sb, PointData& locl, PointIDList& comp)
  233         {
  234           SB = sb;
  235           local = locl;
  236           computed = comp;
  237           state_ = calculation_not_done;
  238           transf_points_.erase(transf_points_.begin(), transf_points_.end());
  239           reset();
  240         }
  241       virtual void calculation() = 0;
  242       Solution_state_tag state() const
  243         {
  244           return state_;
  245         }
  246       PointData transf_points() const
  247         {
  248           if(state_ == calculation_not_done)
  249             throw g2d_exc("Transformation_g2d: calculation not done");
  250           if(state_ == no_solution)
  251             throw
  252               g2d_exc("Transformation_g2d: not enough of identical points");
  253 
  254           return transf_points_;
  255         }
  256 
  257     };  // class Transformation_g2d
  258 
  259   // --------------------------------------------------------------
  260   // Transformation in plane
  261   // Transformation from minimal number of point - selects the best tuple
  262   // - similarity transformation
  263   /*
  264    * transformation key for similarity transformation:
  265    *
  266    *  | x |   |tx | |a1  -a2| |x'|
  267    *  |   | = |   |+|       |*|  |
  268    *  | y |   |ty | |a2   a1| |y'|
  269    *
  270    *  transf_key[0] = a2
  271    *  transf_key[1] = a1
  272    *  transf_key[2] = ty
  273    *  transf_key[3] = tx
  274    *
  275    */
  276 
  277   class SimilarityTr2D : public Transformation_g2d
  278     {
  279     private:
  280 
  281       std::vector<double> transf_key_;
  282       void reset();
  283       bool Given_point(const PointID& cb)
  284         {
  285           return (std::find(computed.begin(), computed.end(), cb) ==
  286                   computed.end());
  287         }
  288       void Identical_points(PointData::iterator& b1,
  289                             PointData::iterator& b2);
  290       void transformation_key(PointData::iterator& b1,
  291                               PointData::iterator& b2);
  292 
  293     public:
  294       SimilarityTr2D(PointData& sb, PointData& locl, PointIDList& comp)
  295         : Transformation_g2d(sb, locl, comp)
  296         {
  297           reset();
  298         }
  299       void calculation();
  300       std::vector<double> transf_key() const
  301         {
  302           if(state_ == calculation_not_done)
  303             throw g2d_exc("SimilarityTr2D: calculation not done");
  304           if(state_ == no_solution)
  305             throw g2d_exc("SimilarityTr2D: not enough identical points");
  306           return transf_key_;
  307         }
  308 
  309     };  // class SimilarityTr2D
  310 
  311  }} // namespace GNU_gama::local
  312 
  313 #endif