22 #include "grid_utils.h"
23 #include "tree_utils.h"
31 struct Node *nodeList=NULL;
39 if(!nodeList) nodeList = (
struct Node *)malloc(MAXNODELIST*
sizeof(
struct Node));
40 for(n=0; n<MAXNODELIST; n++) initNode(nodeList+n);
44 struct Node *getNext()
46 struct Node *temp=NULL;
50 nodeList = (
struct Node *)malloc(MAXNODELIST*
sizeof(
struct Node));
51 for(n=0; n<MAXNODELIST; n++) initNode(nodeList+n);
54 temp = nodeList+curListPos;
56 if(curListPos > MAXNODELIST) error_handler(
"getNext: curListPos >= MAXNODELIST");
62 void initNode(
struct Node *node)
76 void addEnd(
struct Node *list,
double x,
double y,
double z,
int intersect,
double u,
int inbound,
int inside)
79 struct Node *temp=NULL;
81 if(list == NULL) error_handler(
"addEnd: list is NULL");
83 if(list->initialized) {
88 if(samePoint(temp->x, temp->y, temp->z, x, y, z))
return;
96 temp->Next = getNext();
107 temp->intersect = intersect;
108 temp->inbound = inbound;
110 temp->isInside = inside;
115 int addIntersect(
struct Node *list,
double x,
double y,
double z,
int intersect,
double u1,
double u2,
int inbound,
116 int is1,
int ie1,
int is2,
int ie2)
119 double u1_cur, u2_cur;
121 struct Node *temp=NULL;
123 if(list == NULL) error_handler(
"addEnd: list is NULL");
139 if(list->initialized) {
142 if( temp->u == u1_cur && temp->subj_index == i1_cur)
return 0;
143 if( temp->u_clip == u2_cur && temp->clip_index == i2_cur)
return 0;
144 if( !temp->Next )
break;
149 temp->Next = getNext();
159 temp->intersect = intersect;
160 temp->inbound = inbound;
164 temp->subj_index = i1_cur;
165 temp->u_clip = u2_cur;
166 temp->clip_index = i2_cur;
172 int length(
struct Node *list)
174 struct Node *cur_ptr=NULL;
181 if(cur_ptr->initialized ==0)
break;
182 cur_ptr=cur_ptr->Next;
189 int samePoint(
double x1,
double y1,
double z1,
double x2,
double y2,
double z2)
191 if( fabs(x1-x2) > EPSLN10 || fabs(y1-y2) > EPSLN10 || fabs(z1-z2) > EPSLN10 )
198 int sameNode(
struct Node node1,
struct Node node2)
200 if( node1.x == node2.x && node1.y == node2.y && node1.z==node2.z )
207 void addNode(
struct Node *list,
struct Node inNode)
210 addEnd(list, inNode.x, inNode.y, inNode.z, inNode.intersect, inNode.u, inNode.inbound, inNode.isInside);
214 struct Node *getNode(
struct Node *list,
struct Node inNode)
216 struct Node *thisNode=NULL;
217 struct Node *temp=NULL;
221 if( sameNode( *temp, inNode ) ) {
232 struct Node *getNextNode(
struct Node *list)
237 void copyNode(
struct Node *node_out,
struct Node node_in)
240 node_out->x = node_in.x;
241 node_out->y = node_in.y;
242 node_out->z = node_in.z;
243 node_out->u = node_in.u;
244 node_out->intersect = node_in.intersect;
245 node_out->inbound = node_in.inbound;
246 node_out->Next = NULL;
247 node_out->initialized = node_in.initialized;
248 node_out->isInside = node_in.isInside;
251 void printNode(
struct Node *list,
char *str)
255 if(list == NULL) error_handler(
"printNode: list is NULL");
256 if(str) printf(
" %s \n", str);
259 if(temp->initialized ==0)
break;
260 printf(
" (x, y, z, interset, inbound, isInside) = (%19.15f,%19.15f,%19.15f,%d,%d,%d)\n",
261 temp->x, temp->y, temp->z, temp->intersect, temp->inbound, temp->isInside);
267 int intersectInList(
struct Node *list,
double x,
double y,
double z)
275 if( temp->x == x && temp->y == y && temp->z == z ) {
281 if (!found) error_handler(
"intersectInList: point (x,y,z) is not found in the list");
282 if( temp->intersect == 2 )
294 void insertIntersect(
struct Node *list,
double x,
double y,
double z,
double u1,
double u2,
int inbound,
295 double x2,
double y2,
double z2)
297 struct Node *temp1=NULL, *temp2=NULL;
305 if( temp1->x == x2 && temp1->y == y2 && temp1->z == z2 ) {
311 if (!found) error_handler(
"inserAfter: point (x,y,z) is not found in the list");
318 if(!temp1) temp1 = list;
321 temp1->intersect = 2;
331 if(u2 != 0 && u2 != 1) {
335 if(!temp2) temp2 = list;
336 while(temp2->intersect) {
338 if(!temp2) temp2 = list;
343 else if(inbound ==2) {
350 if( temp2->intersect == 1 ) {
351 if( temp2->u > u_cur ) {
368 temp->inbound = inbound;
370 temp->initialized = 1;
376 double gridArea(
struct Node *grid) {
377 double x[20], y[20], z[20];
378 struct Node *temp=NULL;
392 area = great_circle_area(n, x, y, z);
398 int isIntersect(
struct Node node) {
400 return node.intersect;
405 int getInbound(
struct Node node )
410 struct Node *getLast(
struct Node *list)
416 while( temp1->Next ) {
425 int getFirstInbound(
struct Node *list,
struct Node *nodeOut)
427 struct Node *temp=NULL;
432 if( temp->inbound == 2 ) {
433 copyNode(nodeOut, *temp);
442 void getCoordinate(
struct Node node,
double *x,
double *y,
double *z)
452 void getCoordinates(
struct Node *node,
double *p)
462 void setCoordinate(
struct Node *node,
double x,
double y,
double z)
476 void setInbound(
struct Node *interList,
struct Node *list)
479 struct Node *temp1=NULL, *temp=NULL;
480 struct Node *temp1_prev=NULL, *temp1_next=NULL;
481 int prev_is_inside, next_is_inside;
485 if(length(interList) == 0)
return;
490 if( !temp->inbound) {
497 if(sameNode(*temp1, *temp)) {
498 if(!temp1_prev) temp1_prev = getLast(list);
499 temp1_next = temp1->Next;
500 if(!temp1_next) temp1_next = list;
506 if(!temp1_next) error_handler(
"Error from create_xgrid.c: temp is not in list1");
507 if( temp1_prev->isInside == 0 && temp1_next->isInside == 1)
516 int isInside(
struct Node *node) {
518 if(node->isInside == -1) error_handler(
"Error from mosaic_util.c: node->isInside is not set");
519 return(node->isInside);
526 int insidePolygon(
struct Node *node,
struct Node *list)
529 double pnt0[3], pnt1[3], pnt2[3];
531 struct Node *p1=NULL, *p2=NULL;
551 if( samePoint(pnt0[0], pnt0[1], pnt0[2], pnt1[0], pnt1[1], pnt1[2]) ){
554 anglesum += spherical_angle(pnt0, pnt2, pnt1);
562 if( fabs(anglesum - 2*M_PI) < EPSLN8 ){
real(r8_kind), dimension(:,:), allocatable area
area of each grid box