1 // Copyright (C) 2007-2019 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, or (at your option) any later version.
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 __POINTLOCATORALGOS_TXX__
21 #define __POINTLOCATORALGOS_TXX__
23 #include "InterpolationUtils.hxx"
24 #include "CellModel.hxx"
31 namespace INTERP_KERNEL
33 class GenericPointLocatorAlgos
36 virtual ~GenericPointLocatorAlgos() { }
37 virtual std::list<mcIdType> locates(const double* x, double eps) = 0;
40 template<class MyMeshType>
41 class PointLocatorAlgos: public GenericPointLocatorAlgos
45 BBTree<MyMeshType::MY_SPACEDIM,typename MyMeshType::MyConnType>* _tree;
46 const MyMeshType& _mesh;
48 PointLocatorAlgos(const MyMeshType& mesh):_mesh(mesh)
50 typedef typename MyMeshType::MyConnType ConnType;
51 const int SPACEDIM=MyMeshType::MY_SPACEDIM;
52 const NumberingPolicy numPol=MyMeshType::My_numPol;
53 ConnType nelem = _mesh.getNumberOfElements();
54 _bb = new double[SPACEDIM*2*nelem];
55 const ConnType* conn = _mesh.getConnectivityPtr();
56 const ConnType* conn_index = _mesh.getConnectivityIndexPtr();
57 const double* coords=_mesh.getCoordinatesPtr();
58 for (ConnType i=0; i<nelem; i++)
60 for (int idim=0; idim<SPACEDIM; idim++)
62 _bb[2*(i*SPACEDIM+idim)]=std::numeric_limits<double>::max();
63 _bb[2*(i*SPACEDIM+idim)+1]=-std::numeric_limits<double>::max();
65 for (ConnType index= conn_index[i]; index < conn_index[i+1];index++)
67 //coordelem points to the coordinates of the current node of the i-th element
68 const double* coordelem = coords+OTT<ConnType,numPol>::ind2C(conn[OTT<ConnType,numPol>::ind2C(index)])*SPACEDIM;
70 //the bounding box is updated by checking wheher the node is at the min/max in exach dimension
71 for (int idim=0; idim<SPACEDIM;idim++)
73 _bb[2*(i*SPACEDIM+idim)]=(coordelem[idim]<_bb[2*(i*SPACEDIM+idim)])?coordelem[idim]:_bb[2*(i*SPACEDIM+idim)];
74 _bb[2*(i*SPACEDIM+idim)+1]=(coordelem[idim]>_bb[2*(i*SPACEDIM+idim)+1])?coordelem[idim]:_bb[2*(i*SPACEDIM+idim)+1];
78 _tree=new BBTree<SPACEDIM,typename MyMeshType::MyConnType>(_bb,0,0,nelem);
87 //returns the list of elements that contains
88 //the point pointed to by x
89 std::list<typename MyMeshType::MyConnType> locates(const double* x, double eps)
91 typedef typename MyMeshType::MyConnType ConnType;
92 const NumberingPolicy numPol=MyMeshType::My_numPol;
93 std::vector<ConnType> candidates;
94 _tree->getElementsAroundPoint(x,candidates);
95 std::list<ConnType> retlist;
96 for(unsigned int i=0; i< candidates.size(); i++)
98 ConnType ielem=candidates[i];
99 if (elementContainsPoint(ielem,x,eps))
100 retlist.push_back(OTT<ConnType,numPol>::indFC(ielem));
105 static bool isElementContainsPointAlg2D(const double *ptToTest, const double *cellPts, mcIdType nbEdges, double eps)
107 /* with dimension 2, it suffices to check all the edges
108 and see if the sign of double products from the point
116 here XA^XC and XC^XB have different signs*/
117 const int SPACEDIM=MyMeshType::MY_SPACEDIM;
118 int* sign = new int[nbEdges];
119 for (mcIdType iedge=0; iedge<nbEdges; iedge++)
121 const double* A=cellPts+SPACEDIM*iedge;
122 const double* B=cellPts+SPACEDIM*((iedge+1)%nbEdges);
123 double a=mon_determinant(ptToTest, A, B);
131 bool ret=decideFromSign(sign, nbEdges);
136 static bool isElementContainsPointAlg3D(const double *ptToTest, const typename MyMeshType::MyConnType *conn_elem, typename MyMeshType::MyConnType conn_elem_sz, const double *coords, const CellModel& cmType, double eps)
138 const int SPACEDIM=MyMeshType::MY_SPACEDIM;
139 typedef typename MyMeshType::MyConnType ConnType;
140 const NumberingPolicy numPol=MyMeshType::My_numPol;
142 int nbfaces = cmType.getNumberOfSons2(conn_elem,conn_elem_sz);
143 int *sign = new int[nbfaces];
144 ConnType *connOfSon = new ConnType[conn_elem_sz];
145 for (int iface=0; iface<nbfaces; iface++)
147 NormalizedCellType typeOfSon;
148 cmType.fillSonCellNodalConnectivity2(iface,conn_elem,conn_elem_sz,connOfSon,typeOfSon);
149 const double* AA=coords+SPACEDIM*(OTT<ConnType,numPol>::coo2C(connOfSon[0]));
150 const double* BB=coords+SPACEDIM*(OTT<ConnType,numPol>::coo2C(connOfSon[1]));
151 const double* CC=coords+SPACEDIM*(OTT<ConnType,numPol>::coo2C(connOfSon[2]));
152 double Vol=triple_product(AA,BB,CC,ptToTest);
160 bool ret=decideFromSign(sign, nbfaces);
167 * Precondition : spacedim==meshdim. To be checked upstream to this call.
169 static bool isElementContainsPoint(const double *ptToTest, NormalizedCellType type, const double *coords, const typename MyMeshType::MyConnType *conn_elem, typename MyMeshType::MyConnType conn_elem_sz, double eps)
171 const int SPACEDIM=MyMeshType::MY_SPACEDIM;
172 typedef typename MyMeshType::MyConnType ConnType;
173 const NumberingPolicy numPol=MyMeshType::My_numPol;
175 const CellModel& cmType=CellModel::GetCellModel(type);
179 int nbEdges=cmType.getNumberOfSons();
180 double *pts = new double[nbEdges*SPACEDIM];
181 for (int iedge=0; iedge<nbEdges; iedge++)
183 const double* a=coords+SPACEDIM*(OTT<ConnType,numPol>::ind2C(conn_elem[iedge]));
184 std::copy(a,a+SPACEDIM,pts+iedge*SPACEDIM);
186 bool ret=isElementContainsPointAlg2D(ptToTest,pts,nbEdges,eps);
193 return isElementContainsPointAlg3D(ptToTest,conn_elem,conn_elem_sz,coords,cmType,eps);
198 double p1=coords[(OTT<ConnType,numPol>::ind2C(conn_elem[0]))];
199 double p2=coords[(OTT<ConnType,numPol>::ind2C(conn_elem[1]))];
200 double delta=fabs(p1-p2)+eps;
201 double val=*ptToTest-std::min(p1,p2);
202 return val>-eps && val<delta;
204 throw INTERP_KERNEL::Exception("Invalid spacedim detected ! Managed spaceDim are 2 and 3 !");
207 bool elementContainsPoint(typename MyMeshType::MyConnType i, const double* x, double eps)
209 //as i is extracted from the BBTRee, it is already in C numbering
210 //it is not necessary to convert it from F to C
211 typedef typename MyMeshType::MyConnType ConnType;
212 const NumberingPolicy numPol=MyMeshType::My_numPol;
214 const double* coords= _mesh.getCoordinatesPtr();
215 const ConnType* conn=_mesh.getConnectivityPtr();
216 const ConnType* conn_index= _mesh.getConnectivityIndexPtr();
217 const ConnType* conn_elem=conn+OTT<ConnType,numPol>::ind2C(conn_index[i]);
218 int conn_elem_sz=conn_index[i+1]-conn_index[i];
219 NormalizedCellType type=_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(i));
220 return isElementContainsPoint(x,type,coords,conn_elem,conn_elem_sz,eps);
223 static bool decideFromSign(const int* sign, mcIdType nbelem)
227 for (int i=0; i<nbelem;i++)
229 min_sign=(sign[i]<min_sign)?sign[i]:min_sign;
230 max_sign=(sign[i]>max_sign)?sign[i]:max_sign;
232 return (min_sign!=-1 || max_sign!=1);
236 template<class MyMeshType>
237 class PointLocatorInSimplex : public PointLocatorAlgos<MyMeshType>
239 const MyMeshType& _mesh;
241 PointLocatorInSimplex(const MyMeshType& mesh)
242 :PointLocatorAlgos<MyMeshType>(mesh),_mesh(mesh)
246 //================================================================================
248 * \brief Returns nodes composing the simplex the point x is in
250 //================================================================================
252 virtual std::list<typename MyMeshType::MyConnType> locates(const double* x, double eps)
254 typedef typename MyMeshType::MyConnType ConnType;
255 const NumberingPolicy numPol=MyMeshType::My_numPol;
257 std::list<ConnType> simplexNodes;
258 std::list<ConnType> candidates = PointLocatorAlgos<MyMeshType>::locates(x,eps);
259 typename std::list<ConnType>::iterator eIt = candidates.begin();
260 for ( ; eIt != candidates.end(); ++eIt )
262 const ConnType i = OTT<ConnType,numPol>::ind2C( *eIt );
263 const double* coords= _mesh.getCoordinatesPtr();
264 const ConnType* conn=_mesh.getConnectivityPtr();
265 const ConnType* conn_index= _mesh.getConnectivityIndexPtr();
266 const ConnType* conn_elem=conn+OTT<ConnType,numPol>::ind2C(conn_index[i]);
267 int conn_elem_sz=conn_index[i+1]-conn_index[i];
268 NormalizedCellType type=_mesh.getTypeOfElement(OTT<ConnType,numPol>::indFC(i));
269 CellModel cell = CellModel::GetCellModel(type);
271 if ( cell.isQuadratic() )
272 throw Exception("P2 not implemented yet");
274 if ( cell.isSimplex())
276 for ( int n = 0; n < conn_elem_sz; ++n )
277 simplexNodes.push_back( conn_elem[ n ]);
281 NormalizedCellType simlexType = cell.getDimension()==3 ? NORM_TETRA4 : NORM_TRI3;
282 std::vector<mcIdType> sonNodes;
283 NormalizedCellType sonType;
284 const unsigned nbSons = cell.getNumberOfSons2( conn_elem, conn_elem_sz );
285 for ( unsigned s = 0; s < nbSons; ++s )
287 sonNodes.resize( cell.getNumberOfNodesConstituentTheSon2( s, conn_elem, conn_elem_sz ));
288 cell.fillSonCellNodalConnectivity2( s, conn_elem, conn_elem_sz, &sonNodes[0], sonType );
289 std::set<mcIdType> sonNodesSet( sonNodes.begin(), sonNodes.end() );
291 std::set< std::set< ConnType > > checkedSonSimplex;
292 for ( unsigned sn = 0; sn < sonNodes.size(); ++sn )
294 std::vector< ConnType > simplexConn( cell.getDimension() + 1 );
296 for ( n = 0; n < cell.getDimension()-1; ++n )
297 simplexConn[n] = sonNodes[ (sn+n) % sonNodes.size() ];
299 for ( unsigned n2 = 0; n2 < sonNodes.size()-cell.getDimension()+1; ++n2 )
301 simplexConn[n] = sonNodes[ (sn+n+n2) % sonNodes.size() ];
302 std::set< ConnType > sonSimplex( simplexConn.begin(), --simplexConn.end());
303 if ( checkedSonSimplex.insert( sonSimplex ).second )
305 for ( unsigned cn = 0; cn < conn_elem_sz; ++cn )
306 if ( !sonNodesSet.count( conn_elem[cn] ))
308 simplexConn.back() = conn_elem[cn];
309 if ( this->isElementContainsPoint( x, simlexType, coords,
310 &simplexConn[0], simplexConn.size(), eps ))
312 simplexNodes.insert( simplexNodes.end(),
313 simplexConn.begin(), simplexConn.end());