Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / INTERP_KERNEL / ConvexIntersector.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 __CONVEXINTERSECTOR_TXX__
20 #define __CONVEXINTERSECTOR_TXX__
21
22 #include "ConvexIntersector.hxx"
23 #include "PlanarIntersectorP0P0.txx"
24 #include "PlanarIntersectorP0P1.txx"
25 #include "PlanarIntersectorP1P0.txx"
26 #include "PlanarIntersectorP1P1.txx"
27 #include "PlanarIntersectorP1P0Bary.txx"
28
29 #include "PolygonAlgorithms.txx"
30
31 #include <iostream>
32
33 #define CONVINTERSECTOR_TEMPLATE template<class MyMeshType, class MyMatrix, template <class MeshType, class TheMatrix, class ThisIntersector> class InterpType>
34 #define CONVEX_INTERSECTOR_ ConvexIntersector<MyMeshType,MyMatrix,InterpType>
35
36 namespace INTERP_KERNEL
37 {
38   CONVINTERSECTOR_TEMPLATE
39   CONVEX_INTERSECTOR_::ConvexIntersector(const MyMeshType& meshT, const MyMeshType& meshS, 
40                                          double dimCaracteristic, double precision, double md3DSurf,
41                                          double medianPlane, bool doRotate , int oriantation, int printLevel)
42     :InterpType<MyMeshType,MyMatrix,CONVEX_INTERSECTOR_ >(meshT,meshS,dimCaracteristic, precision, md3DSurf, medianPlane, doRotate, oriantation, printLevel),
43      _epsilon(precision*dimCaracteristic)
44   {
45     if(PlanarIntersector<MyMeshType,MyMatrix>::_print_level >= 1)
46       {        
47         std::cout << " - intersection type = convex " << std::endl;
48         if(SPACEDIM==3){
49           if(PlanarIntersector<MyMeshType,MyMatrix>::_do_rotate) std::cout << "  _do_rotate = true" << std::endl;
50           else std::cout << "  _do_rotate = false" << std::endl;
51         }
52       }
53   }
54
55   CONVINTERSECTOR_TEMPLATE
56   double CONVEX_INTERSECTOR_::intersectGeometry(ConnType icellT,   ConnType icellS,
57                                                 ConnType nbNodesT, ConnType nbNodesS)
58   {
59     double result = 0;
60     int orientation = 1;
61     
62     /*** Obtain the coordinates of T and S ***/
63     std::vector<double> CoordsT;
64     std::vector<double> CoordsS;
65     PlanarIntersector<MyMeshType,MyMatrix>::getRealCoordinates(icellT,icellS,nbNodesT,nbNodesS,CoordsT,CoordsS,orientation);
66     /*** Compute the intersection area ***/
67     INTERP_KERNEL::PolygonAlgorithms<SPACEDIM> P(_epsilon, PlanarIntersector<MyMeshType,MyMatrix>::_precision);
68     std::deque<double> inter =  P.intersectConvexPolygons(&CoordsT[0], &CoordsS[0],
69                                                             CoordsT.size()/SPACEDIM, CoordsS.size()/SPACEDIM);
70     double area[SPACEDIM];
71     int nb_inter =((int)inter.size())/SPACEDIM;
72     for(int i = 1; i<nb_inter-1; i++)
73       {
74         INTERP_KERNEL::crossprod<SPACEDIM>(&inter[0],&inter[SPACEDIM*i],&inter[SPACEDIM*(i+1)],area);
75         result +=0.5*norm<SPACEDIM>(area);
76       }
77
78     //DEBUG prints
79     if(PlanarIntersector<MyMeshType,MyMatrix>::_print_level >= 3)
80       {       
81         std::cout << std::endl << "Number of nodes of the intersection = "<<  nb_inter << std::endl;
82         for(int i=0; i<  nb_inter; i++)
83           {for (int idim=0; idim<SPACEDIM; idim++) std::cout << inter[SPACEDIM*i+idim]<< " "; std::cout << std::endl;}
84         std::cout << std::endl <<"Intersection area = " << result << std::endl;
85       }
86     
87     return orientation*result;
88   }
89
90   CONVINTERSECTOR_TEMPLATE
91   double CONVEX_INTERSECTOR_::intersectGeometryWithQuadrangle(const double             * quadrangle,
92                                                               const std::vector<double>& sourceCoords,
93                                                               bool                       isSourceQuad)
94   {
95     double result = 0;
96     int nbOfNodesS=sourceCoords.size()/SPACEDIM;
97
98     /*** Compute the intersection area ***/
99     INTERP_KERNEL::PolygonAlgorithms<SPACEDIM> P(_epsilon, PlanarIntersector<MyMeshType,MyMatrix>::_precision);
100     std::deque<double> inter =  P.intersectConvexPolygons(quadrangle, &sourceCoords[0],
101                                                             4, nbOfNodesS);
102     double area[SPACEDIM];
103     int nb_inter =((int)inter.size())/SPACEDIM;
104     for(int i = 1; i<nb_inter-1; i++)
105       {
106         INTERP_KERNEL::crossprod<SPACEDIM>(&inter[0],&inter[SPACEDIM*i],&inter[SPACEDIM*(i+1)],area);
107         result +=0.5*norm<SPACEDIM>(area);
108       }
109
110     //DEBUG prints
111     if(PlanarIntersector<MyMeshType,MyMatrix>::_print_level >= 3)
112       {       
113         std::cout << std::endl << "Number of nodes of the intersection = "<<  nb_inter << std::endl;
114         for(int i=0; i<  nb_inter; i++)
115           {for (int idim=0; idim<SPACEDIM; idim++) std::cout << inter[SPACEDIM*i+idim]<< " "; std::cout << std::endl;}
116         std::cout << std::endl <<"Intersection area = " << result << std::endl;
117       }
118     
119     return result;
120   }
121
122   CONVINTERSECTOR_TEMPLATE
123   double CONVEX_INTERSECTOR_::intersectGeometryGeneral(const std::vector<double>& targetCoords,
124                                                        const std::vector<double>& sourceCoords)
125   {
126     double result = 0;
127     int nbOfNodesS=sourceCoords.size()/SPACEDIM;
128     int nbOfNodesT=targetCoords.size()/SPACEDIM;
129     /*** Compute the intersection area ***/
130     INTERP_KERNEL::PolygonAlgorithms<SPACEDIM> P(_epsilon, PlanarIntersector<MyMeshType,MyMatrix>::_precision);
131     std::deque<double> inter =  P.intersectConvexPolygons(&targetCoords[0], &sourceCoords[0],
132                                                           nbOfNodesT, nbOfNodesS);
133     double area[SPACEDIM];
134     int nb_inter =((int)inter.size())/SPACEDIM;
135     for(int i = 1; i<nb_inter-1; i++)
136       {
137         INTERP_KERNEL::crossprod<SPACEDIM>(&inter[0],&inter[SPACEDIM*i],&inter[SPACEDIM*(i+1)],area);
138         result +=0.5*norm<SPACEDIM>(area);
139       }
140     return result;
141   }
142
143   //================================================================================
144   /*!
145    * \brief Intersect a triangle and a polygon for P1P0 barycentric algorithm
146    *  \param targetCell - list of coordinates of target polygon in full interlace
147    *  \param targetCellQuadratic - specifies if target polygon is quadratic or not
148    *  \param sourceTria - list of coordinates of source triangle
149    *  \param res - coefficients a,b and c associated to nodes of sourceTria
150    */
151   //================================================================================
152
153   CONVINTERSECTOR_TEMPLATE
154   double CONVEX_INTERSECTOR_::intersectGeoBary(const std::vector<double>& targetCell,
155                                                bool                       targetCellQuadratic,
156                                                const double *             sourceTria,
157                                                std::vector<double>&       res)
158   {
159     double area = 0;
160     double barycenter[SPACEDIM] = {0., 0.};
161     int nbOfNodesT=targetCell.size()/SPACEDIM;
162
163     /*** Compute the intersection area ***/
164     INTERP_KERNEL::PolygonAlgorithms<SPACEDIM> P(_epsilon, PlanarIntersector<MyMeshType,MyMatrix>::_precision);
165     std::deque<double> inter =  P.intersectConvexPolygons(sourceTria, &targetCell[0], 3, nbOfNodesT);
166     double cross[SPACEDIM];
167     int nb_inter =((int)inter.size())/SPACEDIM;
168     for(int i = 1; i<nb_inter-1; i++)
169     {
170       INTERP_KERNEL::crossprod<SPACEDIM>(&inter[0],&inter[SPACEDIM*i],&inter[SPACEDIM*(i+1)],cross);
171       area += 0.5*norm<SPACEDIM>(cross);
172       barycenter[0] += inter[SPACEDIM*i];
173       barycenter[1] += inter[SPACEDIM*i+1];
174     }
175     if ( area > std::numeric_limits<double>::min() )
176     {
177       barycenter[0] = ( barycenter[0] + inter[0] + inter[SPACEDIM*(nb_inter-1)]  ) / nb_inter;
178       barycenter[1] = ( barycenter[1] + inter[1] + inter[SPACEDIM*(nb_inter-1)+1]) / nb_inter;
179       res.resize(3);
180       barycentric_coords<2>( sourceTria, &barycenter[0], &res[0]);
181       res[0] *= area;
182       res[1] *= area;
183       res[2] *= area;
184     }
185     else
186     {
187       area = 0;
188     }
189     return area;
190   }
191 }
192
193 #endif