Salome HOME
Copyright update 2022
[tools/medcoupling.git] / src / INTERP_KERNEL / Geometric2D / InterpKernelGeo2DNode.hxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #ifndef __INTERPKERNELGEO2DNODE_HXX__
22 #define __INTERPKERNELGEO2DNODE_HXX__
23
24 #include "InterpKernelGeo2DPrecision.hxx"
25 #include "INTERPKERNELDefines.hxx"
26 #include "MCIdType.hxx"
27
28 #include <map>
29 #include <cmath>
30 #include <vector>
31 #include <iostream>
32
33 namespace INTERP_KERNEL
34 {
35   typedef enum
36   {
37     IN_1      =  7,
38     ON_1      =  8,
39     ON_LIM_1  = 12,
40     ON_TANG_1 =  9,
41     OUT_1     = 10,
42     UNKNOWN   = 11
43   } TypeOfLocInPolygon;
44
45   class Bounds;
46
47   /*!
48    * Representation of a 2D point, and potentially its location relative to a polygon.
49    * As nodes can be shared between edges it is handled with ref counting.
50    */
51   class INTERPKERNEL_EXPORT Node
52   {
53   public:
54     Node(double x, double y);
55     Node(const double *coords);
56     Node(std::istream& stream);
57     void incrRef() const { _cnt++; }
58     bool decrRef();
59     void initHitStatus() const { _hit=0; }
60     char getHitStatus() const { return _hit; }
61     void hitMeAlone(double xBary, double yBary, double dimChar) { if(_hit==0) { _hit=1; applySimilarity(xBary,yBary,dimChar); } }
62     void unHitMeAlone(double xBary, double yBary, double dimChar) { if(_hit==0) { _hit=1; unApplySimilarity(xBary,yBary,dimChar); } }
63     void hitMeAfter(double xBary, double yBary, double dimChar) { if(_hit==0) { hitMeAlone(xBary,yBary,dimChar); _hit=2; } else if(_hit==1) declareOn(); }
64     void unHitMeAfter(double xBary, double yBary, double dimChar) { if(_hit==0) { unHitMeAlone(xBary,yBary,dimChar); _hit=2; } }
65     void initLocs() const { _loc=UNKNOWN; }
66     void setLoc(TypeOfLocInPolygon loc) const { _loc=loc; }
67     TypeOfLocInPolygon getLoc() const { return _loc; }
68     void declareIn() const { if(_loc==UNKNOWN) _loc=IN_1; }
69     void declareOn() const { if(_loc==UNKNOWN) _loc=ON_1; }
70     void declareOnLim() const { if(_loc==UNKNOWN || _loc==ON_1) _loc=ON_LIM_1; }
71     void declareOut() { if(_loc==UNKNOWN) _loc=OUT_1; }
72     void declareOnTangent() { _loc=ON_TANG_1; }
73     operator const double*() const { return _coords; }
74     bool isEqual(const Node& other) const;
75     //returns an angle in -Pi/2;Pi/2.
76     double getSlope(const Node& other) const;
77     bool isEqualAndKeepTrack(const Node& other, std::vector<Node *>& track) const;
78     void dumpInXfigFile(std::ostream& stream, int resolution, const Bounds& box) const;
79     double distanceWithSq(const Node& other) const;
80     double operator[](int i) const { return _coords[i]; }
81     //! use with caution
82     void setNewCoords(double x, double y) { _coords[0]=x; _coords[1]=y; }
83     //returns an angle in -Pi/2;Pi/2.
84     static double computeSlope(const double *pt1, const double *pt2);
85     //returns an angle in -Pi;Pi
86     static double computeAngle(const double *pt1, const double *pt2);
87     void applySimilarity(double xBary, double yBary, double dimChar);
88     void unApplySimilarity(double xBary, double yBary, double dimChar);
89     static double dot(const double *vect1, const double *vect2) { return vect1[0]*vect2[0]+vect1[1]*vect2[1]; }
90     static double sign(double val) { if(val>=0) return 1.; else return -1.; }
91     static double norm(const double *vect) { return sqrt(vect[0]*vect[0]+vect[1]*vect[1]); }
92     static bool areDoubleEquals(double a, double b) { return fabs(a-b) < QuadraticPlanarPrecision::getPrecision(); }
93     //! idem areDoubleEquals except that precision of comparison is modified.
94     static bool areDoubleEqualsWPLeft(double a, double b, double k) { return k*fabs(a-b) < QuadraticPlanarPrecision::getPrecision(); }
95     //! idem areDoubleEquals except that precision of comparison is modified.
96     static bool areDoubleEqualsWPRight(double a, double b, double k) { return fabs(a-b) < k*QuadraticPlanarPrecision::getPrecision(); }
97     static double distanceBtw2Pt(const double *a, const double *b) { return sqrt((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1])); }
98     static double distanceBtw2PtSq(const double *a, const double *b) { return (a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1]); }
99     //
100     void fillGlobalInfoAbs(const std::map<INTERP_KERNEL::Node *,mcIdType>& mapThis, const std::map<INTERP_KERNEL::Node *,mcIdType>& mapOther, mcIdType offset1, mcIdType offset2, double fact, double baryX, double baryY,
101                            std::vector<double>& addCoo, std::map<INTERP_KERNEL::Node *,mcIdType>& mapAddCoo, mcIdType *nodeId) const;
102     void fillGlobalInfoAbs2(const std::map<INTERP_KERNEL::Node *,mcIdType>& mapThis, const std::map<INTERP_KERNEL::Node *,mcIdType>& mapOther, mcIdType offset1, mcIdType offset2, double fact, double baryX, double baryY,
103                             std::vector<double>& addCoo, std::map<INTERP_KERNEL::Node *,mcIdType>& mapAddCoo, std::vector<mcIdType>& pointsOther) const;
104   protected:
105     ~Node();
106   protected:
107     mutable char _hit;
108     mutable unsigned char _cnt;
109     mutable TypeOfLocInPolygon _loc;
110     double _coords[2];
111   };
112 }
113
114 #endif