1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
20 #ifndef __CONVEXINTERSECTOR_TXX__
21 #define __CONVEXINTERSECTOR_TXX__
23 #include "ConvexIntersector.hxx"
24 #include "PlanarIntersectorP0P0.txx"
25 #include "PlanarIntersectorP0P1.txx"
26 #include "PlanarIntersectorP1P0.txx"
27 #include "PlanarIntersectorP1P1.txx"
28 #include "PlanarIntersectorP1P0Bary.txx"
30 #include "PolygonAlgorithms.txx"
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>
37 namespace INTERP_KERNEL
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)
46 if(PlanarIntersector<MyMeshType,MyMatrix>::_print_level >= 1)
48 std::cout << " - intersection type = convex " << std::endl;
50 if(PlanarIntersector<MyMeshType,MyMatrix>::_do_rotate) std::cout << " _do_rotate = true" << std::endl;
51 else std::cout << " _do_rotate = false" << std::endl;
56 CONVINTERSECTOR_TEMPLATE
57 double CONVEX_INTERSECTOR_::intersectGeometry(ConnType icellT, ConnType icellS,
58 ConnType nbNodesT, ConnType nbNodesS)
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++)
75 INTERP_KERNEL::crossprod<SPACEDIM>(&inter[0],&inter[SPACEDIM*i],&inter[SPACEDIM*(i+1)],area);
76 result +=0.5*norm<SPACEDIM>(area);
80 if(PlanarIntersector<MyMeshType,MyMatrix>::_print_level >= 3)
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;
88 return orientation*result;
91 CONVINTERSECTOR_TEMPLATE
92 double CONVEX_INTERSECTOR_::intersectGeometryWithQuadrangle(const double * quadrangle,
93 const std::vector<double>& sourceCoords,
97 int nbOfNodesS=sourceCoords.size()/SPACEDIM;
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],
103 double area[SPACEDIM];
104 int nb_inter =((int)inter.size())/SPACEDIM;
105 for(int i = 1; i<nb_inter-1; i++)
107 INTERP_KERNEL::crossprod<SPACEDIM>(&inter[0],&inter[SPACEDIM*i],&inter[SPACEDIM*(i+1)],area);
108 result +=0.5*norm<SPACEDIM>(area);
112 if(PlanarIntersector<MyMeshType,MyMatrix>::_print_level >= 3)
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;
123 CONVINTERSECTOR_TEMPLATE
124 double CONVEX_INTERSECTOR_::intersectGeometryGeneral(const std::vector<double>& targetCoords,
125 const std::vector<double>& sourceCoords)
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++)
138 INTERP_KERNEL::crossprod<SPACEDIM>(&inter[0],&inter[SPACEDIM*i],&inter[SPACEDIM*(i+1)],area);
139 result +=0.5*norm<SPACEDIM>(area);
144 //================================================================================
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
152 //================================================================================
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)
161 double barycenter[SPACEDIM] = {0., 0.};
162 int nbOfNodesT=targetCell.size()/SPACEDIM;
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++)
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];
176 if ( area > std::numeric_limits<double>::min() )
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;
181 barycentric_coords<2>( sourceTria, &barycenter[0], &res[0]);