From e6b5b52838c1f63e514e7f38d2566f4f7efcf6a9 Mon Sep 17 00:00:00 2001
From: Jonathan Lambrechts <jonathan.lambrechts@uclouvain.be>
Date: Tue, 26 Oct 2010 17:25:56 +0000
Subject: [PATCH] GModel : getElementByCoord for a given dim (this fix
 dgFunctionevaluator when the point is on the boundary)

---
 Geo/GModel.cpp         |  4 ++--
 Geo/GModel.h           |  2 +-
 Geo/MElementOctree.cpp | 14 ++++++++++++--
 Geo/MElementOctree.h   |  2 +-
 4 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/Geo/GModel.cpp b/Geo/GModel.cpp
index c6ad48039e..64326f4fb0 100644
--- a/Geo/GModel.cpp
+++ b/Geo/GModel.cpp
@@ -625,13 +625,13 @@ int GModel::getNumMeshElements(unsigned c[5])
   return 0;
 }
 
-MElement *GModel::getMeshElementByCoord(SPoint3 &p)
+MElement *GModel::getMeshElementByCoord(SPoint3 &p, int dim)
 {
   if(!_octree){
     Msg::Debug("Rebuilding mesh element octree");
     _octree = new MElementOctree(this);
   }
-  return _octree->find(p.x(), p.y(), p.z());
+  return _octree->find(p.x(), p.y(), p.z(), dim);
 }
 
 MVertex *GModel::getMeshVertexByTag(int n)
diff --git a/Geo/GModel.h b/Geo/GModel.h
index e9babea454..fffd49eec1 100644
--- a/Geo/GModel.h
+++ b/Geo/GModel.h
@@ -296,7 +296,7 @@ class GModel
   int getNumMeshElements(unsigned c[5]);
 
   // access a mesh element by coordinates (using an octree search)
-  MElement *getMeshElementByCoord(SPoint3 &p);
+  MElement *getMeshElementByCoord(SPoint3 &p, int dim = -1);
 
   // access a mesh element by tag, using the element cache
   MElement *getMeshElementByTag(int n);
diff --git a/Geo/MElementOctree.cpp b/Geo/MElementOctree.cpp
index a1dd5a13da..c74568b713 100644
--- a/Geo/MElementOctree.cpp
+++ b/Geo/MElementOctree.cpp
@@ -110,9 +110,19 @@ MElementOctree::~MElementOctree()
   Octree_Delete(_octree);
 }
 
-MElement *MElementOctree::find(double x, double y, double z)
+MElement *MElementOctree::find(double x, double y, double z, int dim)
 {
   double P[3] = {x, y, z};
-  return (MElement*)Octree_Search(P, _octree);
+  MElement *e = (MElement*)Octree_Search(P, _octree);
+  if (dim == -1 || !e || e->getDim() == dim)
+    return e;
+  std::list<void*> l;
+  Octree_SearchAll (P, _octree, &l);
+  for (std::list<void*>::iterator it = l.begin(); it != l.end(); it++) {
+    MElement *el = (MElement*) *it;
+    if (el->getDim() == dim)
+      return el;
+  }
+  return NULL;
 }
 
diff --git a/Geo/MElementOctree.h b/Geo/MElementOctree.h
index 262fef6fa3..112589611f 100644
--- a/Geo/MElementOctree.h
+++ b/Geo/MElementOctree.h
@@ -19,7 +19,7 @@ class MElementOctree{
   MElementOctree(GModel *);
   MElementOctree(std::vector<MElement*> &);
   ~MElementOctree();
-  MElement *find(double x, double y, double z);
+  MElement *find(double x, double y, double z, int dim = -1);
   Octree *getInternalOctree(){ return _octree; }
 };
 
-- 
GitLab