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 __INTERPOLATION3D_TXX__
21 #define __INTERPOLATION3D_TXX__
23 #include "Interpolation3D.hxx"
24 #include "Interpolation.txx"
25 #include "MeshElement.txx"
26 #include "TransformedTriangle.hxx"
27 #include "PolyhedronIntersectorP0P0.txx"
28 #include "PointLocator3DIntersectorP0P0.txx"
29 #include "PolyhedronIntersectorP0P1.txx"
30 #include "PointLocator3DIntersectorP0P1.txx"
31 #include "PolyhedronIntersectorP1P0.txx"
32 #include "PolyhedronIntersectorP1P0Bary.txx"
33 #include "PointLocator3DIntersectorP1P0.txx"
34 #include "PolyhedronIntersectorP1P1.txx"
35 #include "PointLocator3DIntersectorP1P1.txx"
37 /// If defined, use recursion to traverse the binary search tree, else use the BBTree class
38 //#define USE_RECURSIVE_BBOX_FILTER
40 #ifdef USE_RECURSIVE_BBOX_FILTER
41 #include "MeshRegion.txx"
42 #include "RegionNode.hxx"
45 #else // use BBTree class
51 namespace INTERP_KERNEL
54 * Calculates the matrix of volumes of intersection between the elements of srcMesh and the elements of targetMesh.
55 * The calculation is done in two steps. First a filtering process reduces the number of pairs of elements for which the
56 * calculation must be carried out by eliminating pairs that do not intersect based on their bounding boxes. Then, the
57 * volume of intersection is calculated by an object of type Intersector3D for the remaining pairs, and entered into the
58 * intersection matrix.
60 * The matrix is partially sparse : it is a vector of maps of integer - double pairs.
61 * It can also be an INTERP_KERNEL::Matrix object.
62 * The length of the vector is equal to the number of target elements - for each target element there is a map, regardless
63 * of whether the element intersects any source elements or not. But in the maps there are only entries for those source elements
64 * which have a non-zero intersection volume with the target element. The vector has indices running from
65 * 0 to (nb target elements - 1), meaning that the map for target element i is stored at index i - 1. In the maps, however,
66 * the indexing is more natural : the intersection volume of the target element i with source element j is found at matrix[i-1][j].
69 * @param srcMesh 3-dimensional source mesh
70 * @param targetMesh 3-dimesional target mesh, containing only tetraedra
71 * @param result matrix in which the result is stored
74 template<class MyMeshType, class MatrixType>
75 int Interpolation3D::interpolateMeshes(const MyMeshType& srcMesh, const MyMeshType& targetMesh, MatrixType& result, const char *method)
77 typedef typename MyMeshType::MyConnType ConnType;
78 // create MeshElement objects corresponding to each element of the two meshes
79 const unsigned long numSrcElems = srcMesh.getNumberOfElements();
80 const unsigned long numTargetElems = targetMesh.getNumberOfElements();
82 LOG(2, "Source mesh has " << numSrcElems << " elements and target mesh has " << numTargetElems << " elements ");
84 std::vector<MeshElement<ConnType>*> srcElems(numSrcElems);
85 std::vector<MeshElement<ConnType>*> targetElems(numTargetElems);
87 std::map<MeshElement<ConnType>*, int> indices;
89 for(unsigned long i = 0 ; i < numSrcElems ; ++i)
90 srcElems[i] = new MeshElement<ConnType>(i, srcMesh);
92 for(unsigned long i = 0 ; i < numTargetElems ; ++i)
93 targetElems[i] = new MeshElement<ConnType>(i, targetMesh);
95 Intersector3D<MyMeshType,MatrixType>* intersector=0;
96 std::string methC = InterpolationOptions::filterInterpolationMethod(method);
99 switch(InterpolationOptions::getIntersectionType())
102 intersector=new PolyhedronIntersectorP0P0<MyMeshType,MatrixType>(targetMesh, srcMesh, getSplittingPolicy());
105 intersector=new PointLocator3DIntersectorP0P0<MyMeshType,MatrixType>(targetMesh, srcMesh, getPrecision());
108 throw INTERP_KERNEL::Exception("Invalid 3D intersection type for P0P0 interp specified : must be Triangle or PointLocator.");
111 else if(methC=="P0P1")
113 switch(InterpolationOptions::getIntersectionType())
116 intersector=new PolyhedronIntersectorP0P1<MyMeshType,MatrixType>(targetMesh, srcMesh, getSplittingPolicy());
119 intersector=new PointLocator3DIntersectorP0P1<MyMeshType,MatrixType>(targetMesh, srcMesh, getPrecision());
122 throw INTERP_KERNEL::Exception("Invalid 3D intersection type for P0P1 interp specified : must be Triangle or PointLocator.");
125 else if(methC=="P1P0")
127 switch(InterpolationOptions::getIntersectionType())
130 intersector=new PolyhedronIntersectorP1P0<MyMeshType,MatrixType>(targetMesh, srcMesh, getSplittingPolicy());
133 intersector=new PointLocator3DIntersectorP1P0<MyMeshType,MatrixType>(targetMesh, srcMesh, getPrecision());
136 intersector=new PolyhedronIntersectorP1P0Bary<MyMeshType,MatrixType>(targetMesh, srcMesh, getSplittingPolicy());
139 throw INTERP_KERNEL::Exception("Invalid 3D intersection type for P1P0 interp specified : must be Triangle, PointLocator or Barycentric.");
142 else if(methC=="P1P1")
144 switch(InterpolationOptions::getIntersectionType())
147 intersector=new PolyhedronIntersectorP1P1<MyMeshType,MatrixType>(targetMesh, srcMesh, getSplittingPolicy());
150 intersector=new PointLocator3DIntersectorP1P1<MyMeshType,MatrixType>(targetMesh, srcMesh, getPrecision());
153 throw INTERP_KERNEL::Exception("Invalid 3D intersection type for P1P1 interp specified : must be Triangle or PointLocator.");
157 throw Exception("Invalid method choosed must be in \"P0P0\", \"P0P1\", \"P1P0\" or \"P1P1\".");
158 // create empty maps for all source elements
159 result.resize(intersector->getNumberOfRowsOfResMatrix());
161 #ifdef USE_RECURSIVE_BBOX_FILTER
164 * Performs a depth-first search over srcMesh, using bounding boxes to recursively eliminate the elements of targetMesh
165 * which cannot intersect smaller and smaller regions of srcMesh. At each level, each region is divided in two, forming
166 * a binary search tree with leaves consisting of only one element of the source mesh together with the elements of the
167 * target mesh that can intersect it. The recursion is implemented with a stack of RegionNodes, each one containing a
168 * source region and a target region. Each region has an associated bounding box and a vector of pointers to the elements
169 * that belong to it. Each MeshElement contains a bounding box and the global number of the corresponding element in the mesh.
172 // create initial RegionNode and fill up its source region with all the source mesh elements and
173 // its target region with all the target mesh elements whose bounding box
174 // intersects that of the source region
176 RegionNode<ConnType>* firstNode = new RegionNode<ConnType>();
178 MeshRegion<ConnType>& srcRegion = firstNode->getSrcRegion();
180 for(unsigned long i = 0 ; i < numSrcElems ; ++i)
182 srcRegion.addElement(srcElems[i], srcMesh);
185 MeshRegion<ConnType>& targetRegion = firstNode->getTargetRegion();
187 for(unsigned long i = 0 ; i < numTargetElems ; ++i)
189 if(!srcRegion.isDisjointWithElementBoundingBox( *(targetElems[i]) ))
191 targetRegion.addElement(targetElems[i], targetMesh);
195 // Using a stack, descend recursively, creating at each step two new RegionNodes having as source region the left and
196 // right part of the source region of the current node (created using MeshRegion::split()) and as target region all the
197 // elements of the target mesh whose bounding box intersects the corresponding part
198 // Continue until the source region contains only one element, at which point the intersection volumes are
199 // calculated with all the remaining target mesh elements and stored in the matrix if they are non-zero.
201 std::stack< RegionNode<ConnType>* > nodes;
202 nodes.push(firstNode);
204 while(!nodes.empty())
206 RegionNode<ConnType>* currNode = nodes.top();
208 LOG(4, "Popping node ");
210 if(currNode->getTargetRegion().getNumberOfElements() == 1)
213 LOG(4, " - One element");
215 MeshElement<ConnType>* targetElement = *(currNode->getTargetRegion().getBeginElements());
216 std::vector<ConnType> intersectElems;
217 for(typename std::vector< MeshElement<ConnType>* >::const_iterator iter = currNode->getSrcRegion().getBeginElements();iter != currNode->getSrcRegion().getEndElements();++iter)
218 intersectElems.push_back((*iter)->getIndex());
219 intersector->intersectCells(targetElement->getIndex(),intersectElems,result);
224 LOG(4, " - Recursion");
226 RegionNode<ConnType>* leftNode = new RegionNode<ConnType>();
227 RegionNode<ConnType>* rightNode = new RegionNode<ConnType>();
229 // split current source region
231 static BoundingBox::BoxCoord axis = BoundingBox::XMAX;
233 currNode->getTargetRegion().split(leftNode->getTargetRegion(), rightNode->getTargetRegion(), axis, targetMesh);
235 LOG(5, "After split, left target region has " << leftNode->getTargetRegion().getNumberOfElements()
236 << " elements and right target region has " << rightNode->getTargetRegion().getNumberOfElements()
239 // ugly hack to avoid problem with enum which does not start at 0
240 // I guess I ought to implement ++ for it instead ...
241 // Anyway, it basically chooses the next axis, cyclically
242 axis = (axis != BoundingBox::ZMAX) ? static_cast<BoundingBox::BoxCoord>(axis + 1) : BoundingBox::XMAX;
244 // add source elements of current node that overlap the target regions of the new nodes
245 LOG(5, " -- Adding source elements");
246 int numLeftElements = 0;
247 int numRightElements = 0;
248 for(typename std::vector<MeshElement<ConnType>*>::const_iterator iter = currNode->getSrcRegion().getBeginElements() ;
249 iter != currNode->getSrcRegion().getEndElements() ; ++iter)
251 LOG(6, " --- New target node");
253 if(!leftNode->getTargetRegion().isDisjointWithElementBoundingBox(**iter))
255 leftNode->getSrcRegion().addElement(*iter, srcMesh);
259 if(!rightNode->getTargetRegion().isDisjointWithElementBoundingBox(**iter))
261 rightNode->getSrcRegion().addElement(*iter, srcMesh);
267 LOG(5, "Left src region has " << numLeftElements << " elements and right src region has "
268 << numRightElements << " elements");
270 // push new nodes on stack
271 if(numLeftElements != 0)
273 nodes.push(leftNode);
280 if(numRightElements != 0)
282 nodes.push(rightNode);
290 // all nodes are deleted here
293 LOG(4, "Next iteration. Nodes left : " << nodes.size());
298 // create BBTree structure
299 // - get bounding boxes
300 double* bboxes = new double[6 * numSrcElems];
301 int* srcElemIdx = new int[numSrcElems];
302 for(unsigned long i = 0; i < numSrcElems ; ++i)
304 // get source bboxes in right order
305 const BoundingBox* box = srcElems[i]->getBoundingBox();
306 bboxes[6*i+0] = box->getCoordinate(BoundingBox::XMIN);
307 bboxes[6*i+1] = box->getCoordinate(BoundingBox::XMAX);
308 bboxes[6*i+2] = box->getCoordinate(BoundingBox::YMIN);
309 bboxes[6*i+3] = box->getCoordinate(BoundingBox::YMAX);
310 bboxes[6*i+4] = box->getCoordinate(BoundingBox::ZMIN);
311 bboxes[6*i+5] = box->getCoordinate(BoundingBox::ZMAX);
313 // source indices have to begin with zero for BBox, I think
314 srcElemIdx[i] = srcElems[i]->getIndex();
317 BBTree<3,ConnType> tree(bboxes, srcElemIdx, 0, numSrcElems);
319 // for each target element, get source elements with which to calculate intersection
320 // - calculate intersection by calling intersectCells
321 for(unsigned long i = 0; i < numTargetElems; ++i)
323 const BoundingBox* box = targetElems[i]->getBoundingBox();
324 const int targetIdx = targetElems[i]->getIndex();
326 // get target bbox in right order
328 targetBox[0] = box->getCoordinate(BoundingBox::XMIN);
329 targetBox[1] = box->getCoordinate(BoundingBox::XMAX);
330 targetBox[2] = box->getCoordinate(BoundingBox::YMIN);
331 targetBox[3] = box->getCoordinate(BoundingBox::YMAX);
332 targetBox[4] = box->getCoordinate(BoundingBox::ZMIN);
333 targetBox[5] = box->getCoordinate(BoundingBox::ZMAX);
335 std::vector<ConnType> intersectElems;
337 tree.getIntersectingElems(targetBox, intersectElems);
339 if ( !intersectElems.empty() )
340 intersector->intersectCells(targetIdx,intersectElems,result);
344 delete [] srcElemIdx;
347 // free allocated memory
348 int ret=intersector->getNumberOfColsOfResMatrix();
352 for(unsigned long i = 0 ; i < numSrcElems ; ++i)
356 for(unsigned long i = 0 ; i < numTargetElems ; ++i)
358 delete targetElems[i];