Salome HOME
Copyright update 2020
[tools/medcoupling.git] / src / INTERP_KERNEL / Geometric2D / InterpKernelGeo2DQuadraticPolygon.hxx
1 // Copyright (C) 2007-2020  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 __INTERPKERNELGEO2DQUADRATICPOLYGON_HXX__
22 #define __INTERPKERNELGEO2DQUADRATICPOLYGON_HXX__
23
24 #include "INTERPKERNELDefines.hxx"
25
26 #include "InterpKernelGeo2DComposedEdge.hxx"
27 #include "InterpKernelGeo2DAbstractEdge.hxx"
28 #include "InterpKernelGeo2DElementaryEdge.hxx"
29
30 #include <list>
31 #include <vector>
32
33 namespace INTERP_KERNEL
34 {
35   class Edge;
36   class MergePoints;
37
38   enum NodeUsage { USAGE_UNKNOWN, USAGE_LINEAR, USAGE_QUADRATIC_ONLY };
39   typedef std::pair<INTERP_KERNEL::Node *,NodeUsage> NodeWithUsage;
40
41   /**
42    * A set of quadratic or linear edges, not necessarily connected to form a closed polygon.
43    * Some methods however requires a closed form.
44    * Class ComposedEdge focuses more on connectivity aspect.
45    */
46   class QuadraticPolygon : public ComposedEdge
47   {
48   public:
49     INTERPKERNEL_EXPORT QuadraticPolygon() { }
50     INTERPKERNEL_EXPORT QuadraticPolygon(const QuadraticPolygon& other):ComposedEdge(other) { }
51     INTERPKERNEL_EXPORT QuadraticPolygon(const char *fileName);
52     INTERPKERNEL_EXPORT static QuadraticPolygon *BuildLinearPolygon(std::vector<Node *>& nodes);
53     INTERPKERNEL_EXPORT static QuadraticPolygon *BuildArcCirclePolygon(std::vector<Node *>& nodes);
54     INTERPKERNEL_EXPORT static Edge *BuildLinearEdge(std::vector<Node *>& nodes);
55     INTERPKERNEL_EXPORT static Edge *BuildArcCircleEdge(std::vector<Node *>& nodes);
56     INTERPKERNEL_EXPORT static void BuildDbgFile(const std::vector<Node *>& nodes, const char *fileName);
57     INTERPKERNEL_EXPORT ~QuadraticPolygon();
58     INTERPKERNEL_EXPORT void closeMe() const;
59     INTERPKERNEL_EXPORT void circularPermute();
60     INTERPKERNEL_EXPORT bool isButterflyAbs();
61     INTERPKERNEL_EXPORT bool isButterfly() const;
62     INTERPKERNEL_EXPORT void dumpInXfigFile(const char *fileName) const;
63     INTERPKERNEL_EXPORT void dumpInXfigFileWithOther(const ComposedEdge& other, const char *fileName) const;
64     //! Before intersecting as intersectWith a normalization is done.
65     INTERPKERNEL_EXPORT double intersectWithAbs(QuadraticPolygon& other);
66     INTERPKERNEL_EXPORT double intersectWithAbs1D(QuadraticPolygon& other, bool& isColinear);
67     //! Before intersecting as intersectWith a normalization is done.
68     INTERPKERNEL_EXPORT double intersectWithAbs(QuadraticPolygon& other, double* barycenter);
69     INTERPKERNEL_EXPORT void splitAbs(QuadraticPolygon& other, const std::map<INTERP_KERNEL::Node *,mcIdType>& mapThis, const std::map<INTERP_KERNEL::Node *,mcIdType>& mapOther, mcIdType offset1, mcIdType offset2, const std::vector<mcIdType>& otherEdgeIds,
70                                       std::vector<mcIdType>& edgesThis, mcIdType cellIdThis, std::vector< std::vector<mcIdType> >& edgesInOtherColinearWithThis, std::vector< std::vector<mcIdType> >& subDivOther, std::vector<double>& addCoo, std::map<mcIdType,mcIdType>& mergedNodes);
71     INTERPKERNEL_EXPORT void buildFromCrudeDataArray(const std::map<mcIdType,INTERP_KERNEL::Node *>& mapp, bool isQuad, const mcIdType *nodalBg, const double *coords,
72                                                      const mcIdType *descBg, const mcIdType *descEnd, const std::vector<std::vector<mcIdType> >& intersectEdges);
73     INTERPKERNEL_EXPORT void buildFromCrudeDataArray2(const std::map<mcIdType,INTERP_KERNEL::Node *>& mapp, bool isQuad, const mcIdType *nodalBg, const double *coords, const mcIdType *descBg, const mcIdType *descEnd, const std::vector<std::vector<mcIdType> >& intersectEdges,
74                                                       const INTERP_KERNEL::QuadraticPolygon& pol1, const mcIdType *descBg1, const mcIdType *descEnd1, const std::vector<std::vector<mcIdType> >& intersectEdges1,
75                                                       const std::vector< std::vector<mcIdType> >& colinear1,
76                                                       std::map<mcIdType,std::vector<INTERP_KERNEL::ElementaryEdge *> >& alreadyExistingIn2);
77     INTERPKERNEL_EXPORT void updateLocOfEdgeFromCrudeDataArray2(const mcIdType *descBg, const mcIdType *descEnd, const std::vector<std::vector<mcIdType> >& intersectEdges, const INTERP_KERNEL::QuadraticPolygon& pol1, const mcIdType *descBg1, const mcIdType *descEnd1, const std::vector<std::vector<mcIdType> >& intersectEdges1, const std::vector< std::vector<mcIdType> >& colinear1) const;
78     INTERPKERNEL_EXPORT void appendEdgeFromCrudeDataArray(std::size_t edgeId, const std::map<mcIdType,INTERP_KERNEL::Node *>& mapp, bool isQuad, const mcIdType *nodalBg, const double *coords,
79                                                           const mcIdType *descBg,  const mcIdType *descEnd, const std::vector<std::vector<mcIdType> >& intersectEdges);
80     INTERPKERNEL_EXPORT void appendSubEdgeFromCrudeDataArray(Edge *baseEdge, std::size_t j, bool direct, mcIdType edgeId, const std::vector<mcIdType>& subEdge, const std::map<mcIdType,INTERP_KERNEL::Node *>& mapp);
81     INTERPKERNEL_EXPORT void appendCrudeData(const std::map<INTERP_KERNEL::Node *,mcIdType>& mapp, double xBary, double yBary, double fact, mcIdType offset, std::vector<double>& addCoordsQuadratic, std::vector<mcIdType>& conn, std::vector<mcIdType>& connI) const;
82     INTERPKERNEL_EXPORT void buildPartitionsAbs(QuadraticPolygon& other, std::set<INTERP_KERNEL::Edge *>& edgesThis, std::set<INTERP_KERNEL::Edge *>& edgesBoundaryOther, const std::map<INTERP_KERNEL::Node *,mcIdType>& mapp, mcIdType idThis, mcIdType idOther, mcIdType offset,
83                                                 std::vector<double>& addCoordsQuadratic, std::vector<mcIdType>& conn, std::vector<mcIdType>& connI, std::vector<mcIdType>& nb1, std::vector<mcIdType>& nb2);
84     //
85     INTERPKERNEL_EXPORT double intersectWith(const QuadraticPolygon& other) const;
86     INTERPKERNEL_EXPORT double intersectWith(const QuadraticPolygon& other, double* barycenter) const;
87     INTERPKERNEL_EXPORT std::vector<QuadraticPolygon *> intersectMySelfWith(const QuadraticPolygon& other) const;
88     INTERPKERNEL_EXPORT void intersectForPerimeter(const QuadraticPolygon& other, double& perimeterThisPart, double& perimeterOtherPart, double& perimeterCommonPart) const;
89     INTERPKERNEL_EXPORT void intersectForPerimeterAdvanced(const QuadraticPolygon& other, std::vector< double >& polThis, std::vector< double >& polOther) const;
90     INTERPKERNEL_EXPORT void intersectForPoint(const QuadraticPolygon& other, std::vector< int >& numberOfCreatedPointsPerEdge) const;
91   public://Only public for tests reasons
92     INTERPKERNEL_EXPORT void performLocatingOperation(QuadraticPolygon& pol2) const;
93     INTERPKERNEL_EXPORT void performLocatingOperationSlow(QuadraticPolygon& pol2) const;
94     INTERPKERNEL_EXPORT static void SplitPolygonsEachOther(QuadraticPolygon& pol1, QuadraticPolygon& pol2, int& nbOfSplits);
95     INTERPKERNEL_EXPORT std::vector<QuadraticPolygon *> buildIntersectionPolygons(const QuadraticPolygon& pol1, const QuadraticPolygon& pol2) const;
96     INTERPKERNEL_EXPORT bool haveIAChanceToBeCompletedBy(const QuadraticPolygon& pol1NotSplitted, const QuadraticPolygon& pol2Splitted,
97                                                          bool& direction, bool& needCleaning) const;
98     INTERPKERNEL_EXPORT static void ComputeResidual(const QuadraticPolygon& pol1, const std::set<Edge *>& notUsedInPol1, const std::set<Edge *>& edgesInPol2OnBoundary, const std::map<INTERP_KERNEL::Node *,mcIdType>& mapp, mcIdType offset, mcIdType idThis,
99                                                     std::vector<double>& addCoordsQuadratic, std::vector<mcIdType>& conn, std::vector<mcIdType>& connI, std::vector<mcIdType>& nb1, std::vector<mcIdType>& nb2);
100     INTERPKERNEL_EXPORT void cleanDegeneratedConsecutiveEdges();
101   protected:
102     std::list<QuadraticPolygon *> zipConsecutiveInSegments() const;
103     void dumpInXfigFile(std::ostream& stream, int resolution, const Bounds& box) const;
104     static void ClosePolygons(std::list<QuadraticPolygon *>& pol1Zip, const QuadraticPolygon& pol1, const QuadraticPolygon& pol2,
105                               std::vector<QuadraticPolygon *>& results);
106     template<class EDGES>
107     static void UpdateNeighbours(const MergePoints& merger, IteratorOnComposedEdge it1, IteratorOnComposedEdge it2,
108                                  const EDGES *e1, const EDGES *e2);
109     std::list<QuadraticPolygon *>::iterator fillAsMuchAsPossibleWith(const QuadraticPolygon& pol2Splitted,
110                                                                      std::list<QuadraticPolygon *>::iterator iStart,
111                                                                      std::list<QuadraticPolygon *>::iterator iEnd,
112                                                                      bool direction);
113     static std::list<QuadraticPolygon *>::iterator CheckInList(Node *n, std::list<QuadraticPolygon *>::iterator iStart,
114                                                                std::list<QuadraticPolygon *>::iterator iEnd);
115   };
116 }
117
118 namespace INTERP_KERNEL
119 {
120   template<class EDGES>
121   void QuadraticPolygon::UpdateNeighbours(const MergePoints& merger, IteratorOnComposedEdge it1, IteratorOnComposedEdge it2,
122                                           const EDGES *e1, const EDGES *e2)
123   {
124     it1.previousLoop(); it2.previousLoop();
125     ElementaryEdge *curE1=it1.current(); ElementaryEdge *curE2=it2.current();
126     curE1->changeEndNodeWith(e1->getStartNode()); curE2->changeEndNodeWith(e2->getStartNode());
127     it1.nextLoop(); it1.nextLoop(); it2.nextLoop(); it2.nextLoop();
128     curE1->changeStartNodeWith(e1->getEndNode()); curE2->changeStartNodeWith(e2->getEndNode());
129   }
130 }
131
132 #endif