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