Salome HOME
a78d233dd145dbc2e88c072bb7041ead5e1195cb
[tools/medcoupling.git] / src / INTERP_KERNEL / Geometric2DIntersector.txx
1 // Copyright (C) 2007-2012  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.
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 #ifndef __GEOMETRIC2DINTERSECTOR_TXX__
20 #define __GEOMETRIC2DINTERSECTOR_TXX__
21
22 #include "Geometric2DIntersector.hxx"
23 #include "PlanarIntersectorP0P0.txx"
24 #include "Planar2D1DIntersectorP0P0.txx"
25 #include "PlanarIntersectorP0P1.txx"
26 #include "PlanarIntersectorP1P0.txx"
27 #include "PlanarIntersectorP1P1.txx"
28 #include "PlanarIntersectorP1P0Bary.txx"
29 #include "CellModel.hxx"
30
31 #include "InterpKernelGeo2DQuadraticPolygon.hxx"
32 #include "InterpKernelGeo2DEdgeArcCircle.hxx"
33 #include "InterpKernelGeo2DEdgeLin.hxx"
34 #include "InterpKernelGeo2DNode.hxx"
35
36 #define GEO2D_INTERSECTOR    Geometric2DIntersector<MyMeshType,MyMatrix,InterpType>
37 #define INTERSECTOR_TEMPLATE template<class MyMeshType, class MyMatrix, template <class MeshType, class TheMatrix, class ThisIntersector> class InterpType>
38
39 namespace INTERP_KERNEL
40 {
41   INTERSECTOR_TEMPLATE
42   GEO2D_INTERSECTOR::Geometric2DIntersector(const MyMeshType& meshT, const MyMeshType& meshS,
43                                             double dimCaracteristic, double md3DSurf, double medianPlane,
44                                             double precision, int orientation):
45     InterpType<MyMeshType,MyMatrix,GEO2D_INTERSECTOR >(meshT,meshS,dimCaracteristic, precision, md3DSurf, medianPlane, true, orientation, 0)
46   {
47     QUADRATIC_PLANAR::_precision=precision;
48   }
49   
50   INTERSECTOR_TEMPLATE
51   double GEO2D_INTERSECTOR::intersectGeometry(ConnType icellT,   ConnType icellS,
52                                               ConnType nbNodesT, ConnType nbNodesS)
53   {
54     int orientation = 1;
55     std::vector<double> CoordsT;
56     std::vector<double> CoordsS;
57     PlanarIntersector<MyMeshType,MyMatrix>::getRealCoordinates(icellT,icellS,nbNodesT,nbNodesS,CoordsT,CoordsS,orientation);
58     NormalizedCellType tT=PlanarIntersector<MyMeshType,MyMatrix>::_meshT.getTypeOfElement(icellT);
59     NormalizedCellType tS=PlanarIntersector<MyMeshType,MyMatrix>::_meshS.getTypeOfElement(icellS);
60     QuadraticPolygon *p1=buildPolygonFrom(CoordsT,tT);
61     QuadraticPolygon *p2=buildPolygonFrom(CoordsS,tS);
62     double ret=p1->intersectWithAbs(*p2);
63     delete p1; delete p2;
64     return ret;
65   }
66
67   INTERSECTOR_TEMPLATE
68   double GEO2D_INTERSECTOR::intersectGeometry1D(ConnType icellT,   ConnType icellS,
69                                                 ConnType nbNodesT, ConnType nbNodesS,
70                                                 bool& isColinear)
71   {
72     int orientation = 1;
73     std::vector<double> CoordsT;
74     std::vector<double> CoordsS;
75     PlanarIntersector<MyMeshType,MyMatrix>::getRealCoordinates(icellT,icellS,nbNodesT,nbNodesS,CoordsT,CoordsS,orientation);
76     NormalizedCellType tT=PlanarIntersector<MyMeshType,MyMatrix>::_meshT.getTypeOfElement(icellT);
77     NormalizedCellType tS=PlanarIntersector<MyMeshType,MyMatrix>::_meshS.getTypeOfElement(icellS);
78     QuadraticPolygon *p1=buildPolygonFrom(CoordsT,tT);
79     QuadraticPolygon *p2=buildPolygonOfOneEdgeFrom(CoordsS,tS);
80     double ret=p1->intersectWithAbs1D(*p2, isColinear);
81     delete p1; delete p2;
82     return ret;
83   }
84
85   INTERSECTOR_TEMPLATE
86   double GEO2D_INTERSECTOR::intersectGeometryWithQuadrangle(const double             * quadrangle,
87                                                             const std::vector<double>& sourceCoords,
88                                                             bool                       isSourceQuad)
89   {
90     std::vector<Node *> nodes(4);
91     nodes[0]=new Node(quadrangle[0],quadrangle[1]);
92     nodes[1]=new Node(quadrangle[SPACEDIM],quadrangle[SPACEDIM+1]);
93     nodes[2]=new Node(quadrangle[2*SPACEDIM],quadrangle[2*SPACEDIM+1]);
94     nodes[3]=new Node(quadrangle[3*SPACEDIM],quadrangle[3*SPACEDIM+1]);
95     int nbOfSourceNodes=sourceCoords.size()/SPACEDIM;
96     std::vector<Node *> nodes2(nbOfSourceNodes);
97     for(int i=0;i<nbOfSourceNodes;i++)
98       nodes2[i]=new Node(sourceCoords[i*SPACEDIM],sourceCoords[i*SPACEDIM+1]);
99     QuadraticPolygon *p1=QuadraticPolygon::BuildLinearPolygon(nodes);
100     QuadraticPolygon *p2;
101     if(!isSourceQuad)
102       p2=QuadraticPolygon::BuildLinearPolygon(nodes2);
103     else
104       p2=QuadraticPolygon::BuildArcCirclePolygon(nodes2);
105     double ret=p1->intersectWithAbs(*p2);
106     delete p1; delete p2;
107     return ret;
108   }
109
110   INTERSECTOR_TEMPLATE
111   double GEO2D_INTERSECTOR::intersectGeometryGeneral(const std::vector<double>& targetCoords,
112                                                      const std::vector<double>& sourceCoords)
113   {
114     int nbOfTargetNodes=targetCoords.size()/SPACEDIM;
115     std::vector<Node *> nodes(nbOfTargetNodes);
116     for(int i=0;i<nbOfTargetNodes;i++)
117       nodes[i]=new Node(targetCoords[i*SPACEDIM],targetCoords[i*SPACEDIM+1]);
118     int nbOfSourceNodes=sourceCoords.size()/SPACEDIM;
119     std::vector<Node *> nodes2(nbOfSourceNodes);
120     for(int i=0;i<nbOfSourceNodes;i++)
121       nodes2[i]=new Node(sourceCoords[i*SPACEDIM],sourceCoords[i*SPACEDIM+1]);
122     QuadraticPolygon *p1=QuadraticPolygon::BuildLinearPolygon(nodes);
123     QuadraticPolygon *p2=QuadraticPolygon::BuildLinearPolygon(nodes2);
124     double ret=p1->intersectWithAbs(*p2);
125     delete p1; delete p2;
126     return ret;
127   }
128
129   //================================================================================
130   /*!
131    * \brief Intersect a triangle and a polygon for P1P0 barycentric algorithm
132    *  \param targetCell - list of coordinates of target polygon in full interlace
133    *  \param targetCellQuadratic - specifies if target polygon is quadratic or not
134    *  \param sourceTria - list of coordinates of source triangle
135    *  \param res - coefficients a,b and c associated to nodes of sourceTria
136    */
137   //================================================================================
138
139   INTERSECTOR_TEMPLATE
140   double GEO2D_INTERSECTOR::intersectGeoBary(const std::vector<double>& targetCell,
141                                              bool                       targetCellQuadratic,
142                                              const double *             sourceTria,
143                                              std::vector<double>&       res)
144   {
145     std::vector<Node *> nodes(3);
146     nodes[0]=new Node(sourceTria[0*SPACEDIM],sourceTria[0*SPACEDIM+1]);
147     nodes[1]=new Node(sourceTria[1*SPACEDIM],sourceTria[1*SPACEDIM+1]);
148     nodes[2]=new Node(sourceTria[2*SPACEDIM],sourceTria[2*SPACEDIM+1]);
149     int nbOfTargetNodes=targetCell.size()/SPACEDIM;
150     std::vector<Node *> nodes2(nbOfTargetNodes);
151     for(int i=0;i<nbOfTargetNodes;i++)
152       nodes2[i]=new Node(targetCell[i*SPACEDIM],targetCell[i*SPACEDIM+1]);
153     QuadraticPolygon *p1=QuadraticPolygon::BuildLinearPolygon(nodes);
154     QuadraticPolygon *p2;
155     if(!targetCellQuadratic)
156       p2=QuadraticPolygon::BuildLinearPolygon(nodes2);
157     else
158       p2=QuadraticPolygon::BuildArcCirclePolygon(nodes2);
159     double barycenter[2];
160     double ret=p1->intersectWithAbs(*p2,barycenter);
161     delete p1; delete p2;
162     if ( ret > std::numeric_limits<double>::min() )
163     {
164       std::vector<const double* > sourceCell(3);
165       sourceCell[0] = &sourceTria[0];
166       sourceCell[1] = &sourceTria[SPACEDIM];
167       sourceCell[2] = &sourceTria[SPACEDIM*2];
168       res.resize(3);
169       barycentric_coords( sourceCell, barycenter, &res[0]);
170       res[0] *= ret;
171       res[1] *= ret;
172       res[2] *= ret;
173     }
174     else
175     {
176       ret = 0;
177     }
178     return ret;
179   }
180
181   INTERSECTOR_TEMPLATE
182   QuadraticPolygon *GEO2D_INTERSECTOR::buildPolygonFrom(const std::vector<double>& coords, NormalizedCellType type)
183   {
184     int nbNodes=coords.size()/SPACEDIM;
185     std::vector<Node *> nodes(nbNodes);
186     for(int i=0;i<nbNodes;i++)
187       nodes[i]=new Node(coords[i*SPACEDIM],coords[i*SPACEDIM+1]);
188     if(!CellModel::GetCellModel(type).isQuadratic())
189       return QuadraticPolygon::BuildLinearPolygon(nodes);
190     else
191       return QuadraticPolygon::BuildArcCirclePolygon(nodes);
192   }
193
194   INTERSECTOR_TEMPLATE
195   QuadraticPolygon *GEO2D_INTERSECTOR::buildPolygonOfOneEdgeFrom(const std::vector<double>& coords, NormalizedCellType type)
196   {
197     if(type==NORM_SEG2)
198       {
199         Node *node0=new Node(coords[0],coords[1]);
200         Node *node1=new Node(coords[SPACEDIM],coords[SPACEDIM+1]);
201         QuadraticPolygon *ret=new QuadraticPolygon;
202         ret->pushBack(new EdgeLin(node0,node1));
203         node0->decrRef(); node1->decrRef();
204         return ret;
205       }
206     else if(type==NORM_SEG3)
207       {
208         Node *nodeBg=new Node(coords[0],coords[1]);
209         Node *nodeEnd=new Node(coords[SPACEDIM],coords[SPACEDIM+1]);
210         Node *nodeMiddle=new Node(coords[2*SPACEDIM],coords[2*SPACEDIM+1]);
211         QuadraticPolygon *ret=new QuadraticPolygon;
212         ret->pushBack(new EdgeArcCircle(nodeBg,nodeMiddle,nodeEnd));
213         nodeBg->decrRef(); nodeEnd->decrRef(); nodeMiddle->decrRef();
214         return ret;
215       }
216     else
217       throw INTERP_KERNEL::Exception("buildPolygonOfOneEdgeFrom : trying to build such non close QuadraticPolygon with 1D type !");
218   }
219
220   INTERSECTOR_TEMPLATE
221   QuadraticPolygon *GEO2D_INTERSECTOR::buildPolygonAFrom(ConnType cell, int nbOfPoints, NormalizedCellType type)
222   {
223     const ConnType *startOfCellNodeConn=PlanarIntersector<MyMeshType,MyMatrix>::_connectT+OTT<ConnType,numPol>::conn2C(PlanarIntersector<MyMeshType,MyMatrix>::_connIndexT[OTT<ConnType,numPol>::ind2C(cell)]);
224     std::vector<Node *> nodes(nbOfPoints);
225     for(int i=0;i<nbOfPoints;i++)
226       nodes[i]=new Node(PlanarIntersector<MyMeshType,MyMatrix>::_coordsT+OTT<ConnType,numPol>::coo2C(startOfCellNodeConn[i])*SPACEDIM);
227     if(CellModel::GetCellModel(type).isQuadratic())
228       return QuadraticPolygon::BuildLinearPolygon(nodes);
229     else
230       return QuadraticPolygon::BuildArcCirclePolygon(nodes);
231   }
232
233   INTERSECTOR_TEMPLATE
234   QuadraticPolygon *GEO2D_INTERSECTOR::buildPolygonBFrom(ConnType cell, int nbOfPoints, NormalizedCellType type)
235   {
236     const ConnType *startOfCellNodeConn=PlanarIntersector<MyMeshType,MyMatrix>::_connectS+OTT<ConnType,numPol>::conn2C(PlanarIntersector<MyMeshType,MyMatrix>::_connIndexS[OTT<ConnType,numPol>::ind2C(cell)]);
237     std::vector<Node *> nodes(nbOfPoints);
238     for(int i=0;i<nbOfPoints;i++)
239       nodes[i]=new Node(PlanarIntersector<MyMeshType,MyMatrix>::_coordsS+OTT<ConnType,numPol>::coo2C(startOfCellNodeConn[i])*SPACEDIM);
240     const CellModel& cm=CellModel::GetCellModel(type);
241     if(!cm.isQuadratic())
242       return QuadraticPolygon::BuildLinearPolygon(nodes);
243     else
244       return QuadraticPolygon::BuildArcCirclePolygon(nodes);
245   }
246 }
247
248 #endif