GCS.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Konstantinos Poulios      (logari81@gmail.com) 2011     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #ifndef FREEGCS_GCS_H
00024 #define FREEGCS_GCS_H
00025 
00026 #include "SubSystem.h"
00027 
00028 namespace GCS
00029 {
00030 
00032     // Solver
00034 
00035     enum SolveStatus {
00036         Success = 0,   // Found a solution zeroing the error function
00037         Converged = 1, // Found a solution minimizing the error function
00038         Failed = 2     // Failed to find any solution
00039     };
00040 
00041     enum Algorithm {
00042         BFGS = 0,
00043         LevenbergMarquardt = 1,
00044         DogLeg = 2
00045     };
00046 
00047     class System
00048     {
00049     // This is the main class. It holds all constraints and information
00050     // about partitioning into subsystems and solution strategies
00051     private:
00052         std::vector<Constraint *> clist;
00053 
00054         std::map<Constraint *,VEC_pD > c2p; // constraint to parameter adjacency list
00055         std::map<double *,std::vector<Constraint *> > p2c; // parameter to constraint adjacency list
00056 
00057         SubSystem *subsys0; // has the highest priority, always used as the primary subsystem
00058         SubSystem *subsys1; // normally used as secondary subsystem, it is considered primary only if subsys0 is missing
00059         SubSystem *subsys2; // has the lowest priority, always used as secondary system
00060         void clearSubSystems();
00061 
00062         MAP_pD_D reference;
00063         void clearReference();
00064         void resetToReference();
00065 
00066         MAP_pD_pD reductionmap; // for simplification of equality constraints
00067 
00068         bool init;
00069 
00070         int solve_BFGS(SubSystem *subsys, bool isFine);
00071         int solve_LM(SubSystem *subsys);
00072         int solve_DL(SubSystem *subsys);
00073     public:
00074         System();
00075         System(std::vector<Constraint *> clist_);
00076         ~System();
00077 
00078         void clear();
00079         void clearByTag(int tagId);
00080 
00081         int addConstraint(Constraint *constr);
00082         void removeConstraint(Constraint *constr);
00083 
00084         // basic constraints
00085         int addConstraintEqual(double *param1, double *param2, int tagId=0);
00086         int addConstraintDifference(double *param1, double *param2,
00087                                     double *difference, int tagId=0);
00088         int addConstraintP2PDistance(Point &p1, Point &p2, double *distance, int tagId=0);
00089         int addConstraintP2PAngle(Point &p1, Point &p2, double *angle,
00090                                   double incr_angle, int tagId=0);
00091         int addConstraintP2PAngle(Point &p1, Point &p2, double *angle, int tagId=0);
00092         int addConstraintP2LDistance(Point &p, Line &l, double *distance, int tagId=0);
00093         int addConstraintPointOnLine(Point &p, Line &l, int tagId=0);
00094         int addConstraintParallel(Line &l1, Line &l2, int tagId=0);
00095         int addConstraintPerpendicular(Line &l1, Line &l2, int tagId=0);
00096         int addConstraintPerpendicular(Point &l1p1, Point &l1p2,
00097                                        Point &l2p1, Point &l2p2, int tagId=0);
00098         int addConstraintL2LAngle(Line &l1, Line &l2, double *angle, int tagId=0);
00099         int addConstraintL2LAngle(Point &l1p1, Point &l1p2, Point &l2p1, Point &l2p2,
00100                                   double *angle, int tagId=0);
00101         int addConstraintMidpointOnLine(Line &l1, Line &l2, int tagId=0);
00102         int addConstraintMidpointOnLine(Point &l1p1, Point &l1p2, Point &l2p1, Point &l2p2,
00103                                         int tagId=0);
00104 
00105         // derived constraints
00106         int addConstraintP2PCoincident(Point &p1, Point &p2, int tagId=0);
00107         int addConstraintHorizontal(Line &l, int tagId=0);
00108         int addConstraintHorizontal(Point &p1, Point &p2, int tagId=0);
00109         int addConstraintVertical(Line &l, int tagId=0);
00110         int addConstraintVertical(Point &p1, Point &p2, int tagId=0);
00111         int addConstraintCoordinateX(Point &p, double *x, int tagId=0);
00112         int addConstraintCoordinateY(Point &p, double *y, int tagId=0);
00113         int addConstraintArcRules(Arc &a, int tagId=0);
00114         int addConstraintPointOnCircle(Point &p, Circle &c, int tagId=0);
00115         int addConstraintPointOnArc(Point &p, Arc &a, int tagId=0);
00116         int addConstraintTangent(Line &l, Circle &c, int tagId=0);
00117         int addConstraintTangent(Line &l, Arc &a, int tagId=0);
00118         int addConstraintTangentLine2Arc(Point &p1, Point &p2, Arc &a, int tagId=0);
00119         int addConstraintTangentArc2Line(Arc &a, Point &p1, Point &p2, int tagId=0);
00120         int addConstraintCircleRadius(Circle &c, double *radius, int tagId=0);
00121         int addConstraintArcRadius(Arc &a, double *radius, int tagId=0);
00122         int addConstraintEqualLength(Line &l1, Line &l2, double *length, int tagId=0);
00123         int addConstraintEqualRadius(Circle &c1, Circle &c2, int tagId=0);
00124         int addConstraintEqualRadius(Circle &c1, Arc &a2, int tagId=0);
00125         int addConstraintEqualRadius(Arc &a1, Arc &a2, int tagId=0);
00126         int addConstraintP2PSymmetric(Point &p1, Point &p2, Line &l, int tagId=0);
00127         void rescaleConstraint(int id, double coeff);
00128 
00129         void initSolution(VEC_pD &params);
00130 
00131         int solve(bool isFine=true, Algorithm alg=DogLeg);
00132         int solve(VEC_pD &params, bool isFine=true, Algorithm alg=DogLeg);
00133         int solve(SubSystem *subsys, bool isFine=true, Algorithm alg=DogLeg);
00134         int solve(SubSystem *subsysA, SubSystem *subsysB, bool isFine=true);
00135 
00136         void getSubSystems(std::vector<SubSystem *> &subsysvec);
00137         void applySolution();
00138         void undoSolution();
00139 
00140         bool isInit() const { return init; }
00141 
00142         int diagnose(VEC_pD &params, VEC_I &conflicting);
00143     };
00144 
00146     // BFGS Solver parameters
00148     #define XconvergenceRough 1e-8
00149     #define XconvergenceFine  1e-10
00150     #define smallF            1e-20
00151     #define MaxIterations     100 //Note that the total number of iterations allowed is MaxIterations *xLength
00152 
00154     // Helper elements
00156 
00157     void free(VEC_pD &doublevec);
00158     void free(std::vector<Constraint *> &constrvec);
00159     void free(std::vector<SubSystem *> &subsysvec);
00160 
00161 } //namespace GCS
00162 
00163 #endif // FREEGCS_GCS_H

Generated on Wed Nov 23 19:00:15 2011 for FreeCAD by  doxygen 1.6.1