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