1 // Copyright (C) 2007-2012 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 #ifndef __CONVEXINTERSECTOR_TXX__
20 #define __CONVEXINTERSECTOR_TXX__
22 #include "ConvexIntersector.hxx"
23 #include "PlanarIntersectorP0P0.txx"
24 #include "PlanarIntersectorP0P1.txx"
25 #include "PlanarIntersectorP1P0.txx"
26 #include "PlanarIntersectorP1P1.txx"
27 #include "PlanarIntersectorP1P0Bary.txx"
29 #include "PolygonAlgorithms.txx"
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>
36 namespace INTERP_KERNEL
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)
45 if(PlanarIntersector<MyMeshType,MyMatrix>::_print_level >= 1)
47 std::cout << " - intersection type = convex " << std::endl;
49 if(PlanarIntersector<MyMeshType,MyMatrix>::_do_rotate) std::cout << " _do_rotate = true" << std::endl;
50 else std::cout << " _do_rotate = false" << std::endl;
55 CONVINTERSECTOR_TEMPLATE
56 double CONVEX_INTERSECTOR_::intersectGeometry(ConnType icellT, ConnType icellS,
57 ConnType nbNodesT, ConnType nbNodesS)
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++)
74 INTERP_KERNEL::crossprod<SPACEDIM>(&inter[0],&inter[SPACEDIM*i],&inter[SPACEDIM*(i+1)],area);
75 result +=0.5*norm<SPACEDIM>(area);
79 if(PlanarIntersector<MyMeshType,MyMatrix>::_print_level >= 3)
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;
87 return orientation*result;
90 CONVINTERSECTOR_TEMPLATE
91 double CONVEX_INTERSECTOR_::intersectGeometryWithQuadrangle(const double * quadrangle,
92 const std::vector<double>& sourceCoords,
96 int nbOfNodesS=sourceCoords.size()/SPACEDIM;
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],
102 double area[SPACEDIM];
103 int nb_inter =((int)inter.size())/SPACEDIM;
104 for(int i = 1; i<nb_inter-1; i++)
106 INTERP_KERNEL::crossprod<SPACEDIM>(&inter[0],&inter[SPACEDIM*i],&inter[SPACEDIM*(i+1)],area);
107 result +=0.5*norm<SPACEDIM>(area);
111 if(PlanarIntersector<MyMeshType,MyMatrix>::_print_level >= 3)
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;
122 CONVINTERSECTOR_TEMPLATE
123 double CONVEX_INTERSECTOR_::intersectGeometryGeneral(const std::vector<double>& targetCoords,
124 const std::vector<double>& sourceCoords)
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++)
137 INTERP_KERNEL::crossprod<SPACEDIM>(&inter[0],&inter[SPACEDIM*i],&inter[SPACEDIM*(i+1)],area);
138 result +=0.5*norm<SPACEDIM>(area);
143 //================================================================================
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
151 //================================================================================
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)
160 double barycenter[SPACEDIM] = {0., 0.};
161 int nbOfNodesT=targetCell.size()/SPACEDIM;
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++)
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];
175 if ( area > std::numeric_limits<double>::min() )
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;
180 barycentric_coords<2>( sourceTria, &barycenter[0], &res[0]);