Skip to content
Snippets Groups Projects
Commit 645600fe authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

don;t crash on error

parent 91550977
No related branches found
No related tags found
No related merge requests found
...@@ -12,23 +12,23 @@ ...@@ -12,23 +12,23 @@
int initializeOctantBuckets(double *_orig, double *_size, int _maxElem, int initializeOctantBuckets(double *_orig, double *_size, int _maxElem,
octantBucket **buckets_head, globalInfo **globalPara) octantBucket **buckets_head, globalInfo **globalPara)
// Initialize the buckets // Initialize the buckets
// Given by user: orig and size -- information about the domain // Given by user: orig and size -- information about the domain
// maxElem -- maximum number of elements per bucket // maxElem -- maximum number of elements per bucket
// Return: buckets -- pointer to the begin of buckets // Return: buckets -- pointer to the begin of buckets
// globalPara -- some info about the buckets // globalPara -- some info about the buckets
// At last, 1 will be returned if succeed, otherwise, return 0 // At last, 1 will be returned if succeed, otherwise, return 0
{ {
int i, j, k, tmp1; int i, j, k, tmp1;
int p = 1; int p = 1;
int initial_buckets_num; // which is a number of 8^p form for integer p int initial_buckets_num; // which is a number of 8^p form for integer p
double tmp[3], error[3]; double tmp[3], error[3];
octantBucket *buckets; octantBucket *buckets;
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
error[i]= _size[i]*0.01; error[i]= _size[i]*0.01;
initial_buckets_num = (int)pow(8., p); // it is actually 8 initial_buckets_num = (int)pow(8., p); // it is actually 8
(*globalPara) = new globalInfo; (*globalPara) = new globalInfo;
(*globalPara)->maxPrecision = 1; (*globalPara)->maxPrecision = 1;
...@@ -45,10 +45,10 @@ int initializeOctantBuckets(double *_orig, double *_size, int _maxElem, ...@@ -45,10 +45,10 @@ int initializeOctantBuckets(double *_orig, double *_size, int _maxElem,
if (!(*buckets_head)) { if (!(*buckets_head)) {
Msg::Error("initializeOctantBuckets could not allocate enough space"); Msg::Error("initializeOctantBuckets could not allocate enough space");
return (0); return (0);
} // if could not allocate buckets } // if could not allocate buckets
buckets = new octantBucket[8]; buckets = new octantBucket[8];
if (!buckets) { if (!buckets) {
Msg::Error("initializeOctantBuckets could not allocate enough space"); Msg::Error("initializeOctantBuckets could not allocate enough space");
return (0); return (0);
} }
...@@ -69,8 +69,8 @@ int initializeOctantBuckets(double *_orig, double *_size, int _maxElem, ...@@ -69,8 +69,8 @@ int initializeOctantBuckets(double *_orig, double *_size, int _maxElem,
buckets[i].next = NULL; buckets[i].next = NULL;
buckets[i].parent = *buckets_head; buckets[i].parent = *buckets_head;
buckets[i].precision = 1; buckets[i].precision = 1;
} }
tmp1 = (int)(pow(2., p)); tmp1 = (int)(pow(2., p));
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
tmp[i] = (double)(_size[i]+2*error[i])/tmp1; tmp[i] = (double)(_size[i]+2*error[i])/tmp1;
...@@ -94,7 +94,7 @@ int initializeOctantBuckets(double *_orig, double *_size, int _maxElem, ...@@ -94,7 +94,7 @@ int initializeOctantBuckets(double *_orig, double *_size, int _maxElem,
printf(" bucket %d : min[0]=%f, min[1]=%f, min[2]=%f, max[0]= %f, max[1]=%f, max[3]=%f\n", printf(" bucket %d : min[0]=%f, min[1]=%f, min[2]=%f, max[0]= %f, max[1]=%f, max[3]=%f\n",
i,buckets[i].minPt[0],buckets[i].minPt[1], i,buckets[i].minPt[0],buckets[i].minPt[1],
buckets[i].minPt[2], buckets[i].maxPt[0], buckets[i].maxPt[1], buckets[i].minPt[2], buckets[i].maxPt[0], buckets[i].maxPt[1],
buckets[i].maxPt[2]); buckets[i].maxPt[2]);
printf("bucket elements link list: bucket->lhead = %d\n", buckets[i].lhead); printf("bucket elements link list: bucket->lhead = %d\n", buckets[i].lhead);
} }
#endif #endif
...@@ -102,56 +102,56 @@ int initializeOctantBuckets(double *_orig, double *_size, int _maxElem, ...@@ -102,56 +102,56 @@ int initializeOctantBuckets(double *_orig, double *_size, int _maxElem,
return (1); return (1);
} }
int addElement2Bucket(octantBucket *_bucket, void * _element, int addElement2Bucket(octantBucket *_bucket, void * _element,
double *_minBB, double *_maxBB, double *_minBB, double *_maxBB,
double *_ele_centroid, globalInfo *_globalPara) double *_ele_centroid, globalInfo *_globalPara)
// Add another element to the octant bucket's list. // Add another element to the octant bucket's list.
// If the bucket contains too many elements after adding this element, // If the bucket contains too many elements after adding this element,
// refine this bucket and reallocate the elements of this bucket // refine this bucket and reallocate the elements of this bucket
// Given:- the octant bucket, - the element // Given:- the octant bucket, - the element
// - the element's minimum and maximum x,y,z // - the element's minimum and maximum x,y,z
// - the element's centroid, - global information // - the element's centroid, - global information
// Check if element has already been added - if not, return 1 // Check if element has already been added - if not, return 1
// for successfully adding, otherwise return -1 // for successfully adding, otherwise return -1
{ {
int i, flag = 1; int i, flag = 1;
ELink ptr1, ptr2; ELink ptr1, ptr2;
octantBucket *ptrBucket; octantBucket *ptrBucket;
// check for duplicates
if ( checkElementInBucket(_bucket, _element) == 1) return -1;
// printf("\n addToBucket...\n"); // check for duplicates
if ( checkElementInBucket(_bucket, _element) == 1) return -1;
// printf("\n addToBucket...\n");
// ptr1 = (ELink) malloc(sizeof(Elem)); // ptr1 = (ELink) malloc(sizeof(Elem));
ptr1 = new Elem; ptr1 = new Elem;
(_globalPara->listAllElements).push_back(_element); (_globalPara->listAllElements).push_back(_element);
ptr1-> next = _bucket->lhead; ptr1-> next = _bucket->lhead;
ptr1-> region = _element; ptr1-> region = _element;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
ptr1->minPt[i] = _minBB[i]; ptr1->minPt[i] = _minBB[i];
ptr1->maxPt[i] = _maxBB[i]; ptr1->maxPt[i] = _maxBB[i];
ptr1->centroid[i] = _ele_centroid[i]; ptr1->centroid[i] = _ele_centroid[i];
// printf(" %7.2f->%-7.2f",ptr1->minPt[i], ptr1->maxPt[i]); // printf(" %7.2f->%-7.2f",ptr1->minPt[i], ptr1->maxPt[i]);
} }
_bucket->lhead = ptr1; _bucket->lhead = ptr1;
(_bucket->numElements)++; (_bucket->numElements)++;
#if 0 #if 0
printf("bucket element list: bucket->lhead = %d", _bucket->lhead); printf("bucket element list: bucket->lhead = %d", _bucket->lhead);
printf(" numElements = %d\n",_bucket->numElements); printf(" numElements = %d\n",_bucket->numElements);
printf("the element is add to this bucket: (%f, %f, %f) to (%f, %f, %f)\n", printf("the element is add to this bucket: (%f, %f, %f) to (%f, %f, %f)\n",
_bucket->minPt[0],_bucket->minPt[1], _bucket->minPt[2], _bucket->maxPt[0], _bucket->minPt[0],_bucket->minPt[1], _bucket->minPt[2], _bucket->maxPt[0],
_bucket->maxPt[1], _bucket->maxPt[2]); _bucket->maxPt[1], _bucket->maxPt[2]);
#endif #endif
// check whether the number of elements in the bucket > maxElements // check whether the number of elements in the bucket > maxElements
// if true, refine the bucket and reallocate the elements // if true, refine the bucket and reallocate the elements
while ( flag == 1) { while ( flag == 1) {
flag = 0; flag = 0;
if (_bucket->numElements > _globalPara->maxElements) { if (_bucket->numElements > _globalPara->maxElements) {
// printf(" going to subdivide\n"); // printf(" going to subdivide\n");
...@@ -164,17 +164,19 @@ int addElement2Bucket(octantBucket *_bucket, void * _element, ...@@ -164,17 +164,19 @@ int addElement2Bucket(octantBucket *_bucket, void * _element,
ptrBucket = findElementBucket(_bucket, ptr1->centroid); ptrBucket = findElementBucket(_bucket, ptr1->centroid);
ptr2 = ptr1; ptr2 = ptr1;
ptr1 = ptr1->next; ptr1 = ptr1->next;
if (ptrBucket == NULL) if (ptrBucket == NULL){
Msg::Error("Wrong , ptrBucket = NULL. A bug here!"); Msg::Error("Wrong , ptrBucket = NULL. A bug here!");
return 0;
}
ptr2->next = ptrBucket->lhead; ptr2->next = ptrBucket->lhead;
ptrBucket->lhead = ptr2; ptrBucket->lhead = ptr2;
(ptrBucket->numElements)++; (ptrBucket->numElements)++;
if (ptrBucket->numElements > _globalPara->maxElements) { if (ptrBucket->numElements > _globalPara->maxElements) {
flag = 1; flag = 1;
_bucket->lhead = NULL; _bucket->lhead = NULL;
_bucket = ptrBucket; _bucket = ptrBucket;
} }
} }
if (flag == 0) _bucket->lhead = NULL; if (flag == 0) _bucket->lhead = NULL;
} }
} }
...@@ -182,25 +184,25 @@ int addElement2Bucket(octantBucket *_bucket, void * _element, ...@@ -182,25 +184,25 @@ int addElement2Bucket(octantBucket *_bucket, void * _element,
} }
int checkElementInBucket(octantBucket *_bucket, void* _element) int checkElementInBucket(octantBucket *_bucket, void* _element)
// Given an elememt and an octant bucket, check if the element // Given an elememt and an octant bucket, check if the element
// exists in the bucket's element list. return 1 if already exits, // exists in the bucket's element list. return 1 if already exits,
// otherwise, return 0 // otherwise, return 0
{ {
ELink ptr; ELink ptr;
for (ptr = _bucket->lhead; ptr != NULL; ptr = ptr->next) { for (ptr = _bucket->lhead; ptr != NULL; ptr = ptr->next) {
// changed ****, compare the objected pointed by the void *. // changed ****, compare the objected pointed by the void *.
if (ptr->region == _element) return 1; if (ptr->region == _element) return 1;
} }
return 0; return 0;
} }
octantBucket *findElementBucket(octantBucket *_buckets_head, double *_pt) octantBucket *findElementBucket(octantBucket *_buckets_head, double *_pt)
// Find the leaf bucket which contains the point _pt // Find the leaf bucket which contains the point _pt
// given parameter: _buckets --- the point to buckets head // given parameter: _buckets --- the point to buckets head
// _pt --- the point to find // _pt --- the point to find
// Return the pointer to the bucket contains the point // Return the pointer to the bucket contains the point
// if fail, return NULL // if fail, return NULL
{ {
int i, j; int i, j;
int num = 8; int num = 8;
...@@ -209,8 +211,8 @@ octantBucket *findElementBucket(octantBucket *_buckets_head, double *_pt) ...@@ -209,8 +211,8 @@ octantBucket *findElementBucket(octantBucket *_buckets_head, double *_pt)
while (tmpbucket != NULL) { while (tmpbucket != NULL) {
for (i = 0; i < num; i ++) { for (i = 0; i < num; i ++) {
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
if (tmpbucket[i].minPt[j] > _pt[j] || if (tmpbucket[i].minPt[j] > _pt[j] ||
tmpbucket[i].maxPt[j] < _pt[j]) tmpbucket[i].maxPt[j] < _pt[j])
break; break;
} }
...@@ -218,26 +220,26 @@ octantBucket *findElementBucket(octantBucket *_buckets_head, double *_pt) ...@@ -218,26 +220,26 @@ octantBucket *findElementBucket(octantBucket *_buckets_head, double *_pt)
prevbucket = tmpbucket+i; prevbucket = tmpbucket+i;
tmpbucket = tmpbucket[i].next; tmpbucket = tmpbucket[i].next;
break; break;
} }
} // for loop i } // for loop i
if (i == num) { if (i == num) {
// Msg::Error("No bucket contains the given point!"); // Msg::Error("No bucket contains the given point!");
return NULL; return NULL;
} }
} // for while loop } // for while loop
return prevbucket; return prevbucket;
} }
int subdivideOctantBucket(octantBucket *_bucket, globalInfo *_globalPara) int subdivideOctantBucket(octantBucket *_bucket, globalInfo *_globalPara)
// To many elements are in this octant bucket, so try to refine // To many elements are in this octant bucket, so try to refine
// Returns 1 for success, 0 for failure (no memory left). // Returns 1 for success, 0 for failure (no memory left).
{ {
int i, j, k, tmp1; int i, j, k, tmp1;
int numBuck = 8; int numBuck = 8;
double tmp[3]; double tmp[3];
_bucket->next = new octantBucket[8]; _bucket->next = new octantBucket[8];
// _bucket->next = (octantBucket *) calloc(numBuck,sizeof(octantBucket)); // _bucket->next = (octantBucket *) calloc(numBuck,sizeof(octantBucket));
if (!_bucket->next) { if (!_bucket->next) {
...@@ -254,8 +256,8 @@ int subdivideOctantBucket(octantBucket *_bucket, globalInfo *_globalPara) ...@@ -254,8 +256,8 @@ int subdivideOctantBucket(octantBucket *_bucket, globalInfo *_globalPara)
(_bucket->next[i]).next = NULL; (_bucket->next[i]).next = NULL;
(_bucket->next[i]).parent = _bucket; (_bucket->next[i]).parent = _bucket;
(_bucket->next[i]).precision = _bucket->precision+1; (_bucket->next[i]).precision = _bucket->precision+1;
} }
tmp1 = 2; tmp1 = 2;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
tmp[i] = ((double)(_bucket->maxPt[i]-_bucket->minPt[i]))/tmp1; tmp[i] = ((double)(_bucket->maxPt[i]-_bucket->minPt[i]))/tmp1;
...@@ -278,11 +280,11 @@ int subdivideOctantBucket(octantBucket *_bucket, globalInfo *_globalPara) ...@@ -278,11 +280,11 @@ int subdivideOctantBucket(octantBucket *_bucket, globalInfo *_globalPara)
} }
void *searchElement(octantBucket *_buckets_head, double *_pt, globalInfo *_globalPara, void *searchElement(octantBucket *_buckets_head, double *_pt, globalInfo *_globalPara,
BBFunction BBElement, InEleFunction xyzInElement) BBFunction BBElement, InEleFunction xyzInElement)
{ {
int flag; int flag;
octantBucket *ptrBucket; octantBucket *ptrBucket;
ELink ptr1; ELink ptr1;
std::vector<void*>::iterator iter; std::vector<void*>::iterator iter;
void * ptrToEle = _globalPara->ptrToPrevElement; void * ptrToEle = _globalPara->ptrToPrevElement;
...@@ -292,13 +294,13 @@ void *searchElement(octantBucket *_buckets_head, double *_pt, globalInfo *_globa ...@@ -292,13 +294,13 @@ void *searchElement(octantBucket *_buckets_head, double *_pt, globalInfo *_globa
flag = xyzInElement(ptrToEle, _pt); flag = xyzInElement(ptrToEle, _pt);
if (flag == 1) return ptrToEle; if (flag == 1) return ptrToEle;
} }
ptrBucket = findElementBucket(_buckets_head, _pt); ptrBucket = findElementBucket(_buckets_head, _pt);
if (ptrBucket == NULL) { if (ptrBucket == NULL) {
// this is not an error // this is not an error
Msg::Debug("The point is not in the domain"); Msg::Debug("The point is not in the domain");
return NULL; return NULL;
} }
ptr1 = ptrBucket->lhead; ptr1 = ptrBucket->lhead;
...@@ -313,16 +315,16 @@ void *searchElement(octantBucket *_buckets_head, double *_pt, globalInfo *_globa ...@@ -313,16 +315,16 @@ void *searchElement(octantBucket *_buckets_head, double *_pt, globalInfo *_globa
while (ptr1 != NULL){ while (ptr1 != NULL){
flag = xyzInElementBB(_pt, ptr1->region, BBElement); flag = xyzInElementBB(_pt, ptr1->region, BBElement);
if (flag == 1) if (flag == 1)
flag = xyzInElement(ptr1->region, _pt); flag = xyzInElement(ptr1->region, _pt);
if (flag == 1) { if (flag == 1) {
_globalPara->ptrToPrevElement = ptr1->region; _globalPara->ptrToPrevElement = ptr1->region;
return ptr1->region; return ptr1->region;
} }
ptr1 = ptr1->next; ptr1 = ptr1->next;
} }
for (iter = (ptrBucket->listBB).begin(); for (iter = (ptrBucket->listBB).begin();
iter != (ptrBucket->listBB).end(); iter++){ iter != (ptrBucket->listBB).end(); iter++){
flag = xyzInElementBB(_pt, *iter, BBElement); flag = xyzInElementBB(_pt, *iter, BBElement);
if (flag == 1) if (flag == 1)
...@@ -333,17 +335,17 @@ void *searchElement(octantBucket *_buckets_head, double *_pt, globalInfo *_globa ...@@ -333,17 +335,17 @@ void *searchElement(octantBucket *_buckets_head, double *_pt, globalInfo *_globa
} }
} }
// printf("This point is not found in all elements! It is not in the domain \n"); // printf("This point is not found in all elements! It is not in the domain \n");
return NULL; return NULL;
} }
int xyzInElementBB(double *_xyz, void * _region, BBFunction _BBElement) int xyzInElementBB(double *_xyz, void * _region, BBFunction _BBElement)
// Check if xyz is in the region's bounding box, return 1 if true, 0 otherwise // Check if xyz is in the region's bounding box, return 1 if true, 0 otherwise
// BBElement is the function given by user to find the bounding box // BBElement is the function given by user to find the bounding box
{ {
int i; int i;
double minPt[3]; // corner with smallest x,y,z coords double minPt[3]; // corner with smallest x,y,z coords
double maxPt[3]; // corner with largest x,y,z coords double maxPt[3]; // corner with largest x,y,z coords
(*_BBElement)(_region, minPt, maxPt); (*_BBElement)(_region, minPt, maxPt);
...@@ -369,18 +371,18 @@ void insertOneBB(void* _region, double *_minPt, double *_maxPt, octantBucket *_b ...@@ -369,18 +371,18 @@ void insertOneBB(void* _region, double *_minPt, double *_maxPt, octantBucket *_b
ptr = ptr->next; ptr = ptr->next;
} }
//_bucket->listBB.insert(_bucket->listBB.end(),_region); //_bucket->listBB.insert(_bucket->listBB.end(),_region);
_bucket->listBB.push_back(_region); _bucket->listBB.push_back(_region);
return; return;
} }
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
insertOneBB(_region, _minPt, _maxPt, _bucket->next+i); insertOneBB(_region, _minPt, _maxPt, _bucket->next+i);
return; return;
} }
void *searchAllElements(octantBucket *_buckets_head, double *_pt, globalInfo *_globalPara, void *searchAllElements(octantBucket *_buckets_head, double *_pt, globalInfo *_globalPara,
BBFunction BBElement, InEleFunction xyzInElement, BBFunction BBElement, InEleFunction xyzInElement,
std::vector<void*> *_elements) std::vector<void*> *_elements)
{ {
int flag, flag1; int flag, flag1;
...@@ -397,7 +399,7 @@ void *searchAllElements(octantBucket *_buckets_head, double *_pt, globalInfo *_g ...@@ -397,7 +399,7 @@ void *searchAllElements(octantBucket *_buckets_head, double *_pt, globalInfo *_g
printf("point %lf %lf %lf has been found in bucket %lf %lf %fl -> %lf %lf %lf %p\n", printf("point %lf %lf %lf has been found in bucket %lf %lf %fl -> %lf %lf %lf %p\n",
_pt[0],_pt[1],_pt[2], ptrBucket->minPt[0],ptrBucket->minPt[1],ptrBucket->minPt[2], _pt[0],_pt[1],_pt[2], ptrBucket->minPt[0],ptrBucket->minPt[1],ptrBucket->minPt[2],
ptrBucket->maxPt[0],ptrBucket->maxPt[1],ptrBucket->maxPt[2], ptr1); ptrBucket->maxPt[0],ptrBucket->maxPt[1],ptrBucket->maxPt[2], ptr1);
if (ptr1 == NULL) { if (ptr1 == NULL) {
printf("empty element list for centroid list!?\n, possible!"); printf("empty element list for centroid list!?\n, possible!");
} }
...@@ -416,20 +418,20 @@ void *searchAllElements(octantBucket *_buckets_head, double *_pt, globalInfo *_g ...@@ -416,20 +418,20 @@ void *searchAllElements(octantBucket *_buckets_head, double *_pt, globalInfo *_g
ptr1 = ptr1->next; ptr1 = ptr1->next;
} }
for (iter = (ptrBucket->listBB).begin(); for (iter = (ptrBucket->listBB).begin();
iter != (ptrBucket->listBB).end(); iter++){ iter != (ptrBucket->listBB).end(); iter++){
flag = xyzInElementBB(_pt, *iter, BBElement); flag = xyzInElementBB(_pt, *iter, BBElement);
if (flag == 1) if (flag == 1)
flag = xyzInElement(*iter, _pt); flag = xyzInElement(*iter, _pt);
if (flag == 1) { if (flag == 1) {
_elements->push_back(*iter); _elements->push_back(*iter);
flag1 = 1; flag1 = 1;
} }
} }
if (flag1) if (flag1)
return (void *)(_elements); return (void *)(_elements);
// Msg::Warning("This point is not found in any element! It is not in the domain"); // Msg::Warning("This point is not found in any element! It is not in the domain");
return NULL; return NULL;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment