23#include "grid_utils.h"
24#include "tree_utils.h"
32struct Node *nodeList=NULL;
40 if(!nodeList) nodeList = (
struct Node *)malloc(MAXNODELIST*
sizeof(
struct Node));
41 for(n=0; n<MAXNODELIST; n++) initNode(nodeList+n);
47 struct Node *temp=NULL;
51 nodeList = (
struct Node *)malloc(MAXNODELIST*
sizeof(
struct Node));
52 for(n=0; n<MAXNODELIST; n++) initNode(nodeList+n);
55 temp = nodeList+curListPos;
57 if(curListPos > MAXNODELIST) error_handler(
"getNext: curListPos >= MAXNODELIST");
63void initNode(
struct Node *node)
77void addEnd(
struct Node *list,
double x,
double y,
double z,
int intersect,
double u,
int inbound,
int inside)
80 struct Node *temp=NULL;
82 if(list == NULL) error_handler(
"addEnd: list is NULL");
84 if(list->initialized) {
89 if(samePoint(temp->x, temp->y, temp->z, x, y, z))
return;
97 temp->Next = getNext();
108 temp->intersect = intersect;
109 temp->inbound = inbound;
111 temp->isInside = inside;
116int addIntersect(
struct Node *list,
double x,
double y,
double z,
int intersect,
double u1,
double u2,
int inbound,
117 int is1,
int ie1,
int is2,
int ie2)
120 double u1_cur, u2_cur;
122 struct Node *temp=NULL;
124 if(list == NULL) error_handler(
"addEnd: list is NULL");
140 if(list->initialized) {
143 if( temp->u == u1_cur && temp->subj_index == i1_cur)
return 0;
144 if( temp->u_clip == u2_cur && temp->clip_index == i2_cur)
return 0;
145 if( !temp->Next )
break;
150 temp->Next = getNext();
160 temp->intersect = intersect;
161 temp->inbound = inbound;
165 temp->subj_index = i1_cur;
166 temp->u_clip = u2_cur;
167 temp->clip_index = i2_cur;
173int length(
struct Node *list)
175 struct Node *cur_ptr=NULL;
182 if(cur_ptr->initialized ==0)
break;
183 cur_ptr=cur_ptr->Next;
190int samePoint(
double x1,
double y1,
double z1,
double x2,
double y2,
double z2)
192 if( fabs(x1-x2) > EPSLN10 || fabs(y1-y2) > EPSLN10 || fabs(z1-z2) > EPSLN10 )
199int sameNode(
struct Node node1,
struct Node node2)
201 if( node1.x == node2.x && node1.y == node2.y && node1.z==node2.z )
208void addNode(
struct Node *list,
struct Node inNode)
211 addEnd(list, inNode.x, inNode.y, inNode.z, inNode.intersect, inNode.u, inNode.inbound, inNode.isInside);
215struct Node *getNode(
struct Node *list,
struct Node inNode)
217 struct Node *thisNode=NULL;
218 struct Node *temp=NULL;
222 if( sameNode( *temp, inNode ) ) {
233struct Node *getNextNode(
struct Node *list)
238void copyNode(
struct Node *node_out,
struct Node node_in)
241 node_out->x = node_in.x;
242 node_out->y = node_in.y;
243 node_out->z = node_in.z;
244 node_out->u = node_in.u;
245 node_out->intersect = node_in.intersect;
246 node_out->inbound = node_in.inbound;
247 node_out->Next = NULL;
248 node_out->initialized = node_in.initialized;
249 node_out->isInside = node_in.isInside;
252void printNode(
struct Node *list,
char *str)
256 if(list == NULL) error_handler(
"printNode: list is NULL");
257 if(str) printf(
" %s \n", str);
260 if(temp->initialized ==0)
break;
261 printf(
" (x, y, z, interset, inbound, isInside) = (%19.15f,%19.15f,%19.15f,%d,%d,%d)\n",
262 temp->x, temp->y, temp->z, temp->intersect, temp->inbound, temp->isInside);
268int intersectInList(
struct Node *list,
double x,
double y,
double z)
276 if( temp->x == x && temp->y == y && temp->z == z ) {
282 if (!found) error_handler(
"intersectInList: point (x,y,z) is not found in the list");
283 if( temp->intersect == 2 )
295void insertIntersect(
struct Node *list,
double x,
double y,
double z,
double u1,
double u2,
int inbound,
296 double x2,
double y2,
double z2)
298 struct Node *temp1=NULL, *temp2=NULL;
306 if( temp1->x == x2 && temp1->y == y2 && temp1->z == z2 ) {
312 if (!found) error_handler(
"inserAfter: point (x,y,z) is not found in the list");
319 if(!temp1) temp1 = list;
322 temp1->intersect = 2;
332 if(u2 != 0 && u2 != 1) {
336 if(!temp2) temp2 = list;
337 while(temp2->intersect) {
339 if(!temp2) temp2 = list;
344 else if(inbound ==2) {
351 if( temp2->intersect == 1 ) {
352 if( temp2->u > u_cur ) {
369 temp->inbound = inbound;
371 temp->initialized = 1;
377double gridArea(
struct Node *grid) {
378 double x[20], y[20], z[20];
379 struct Node *temp=NULL;
393 area = great_circle_area(n, x, y, z);
399int isIntersect(
struct Node node) {
401 return node.intersect;
406int getInbound(
struct Node node )
411struct Node *getLast(
struct Node *list)
417 while( temp1->Next ) {
426int getFirstInbound(
struct Node *list,
struct Node *nodeOut)
428 struct Node *temp=NULL;
433 if( temp->inbound == 2 ) {
434 copyNode(nodeOut, *temp);
443void getCoordinate(
struct Node node,
double *x,
double *y,
double *z)
453void getCoordinates(
struct Node *node,
double *p)
463void setCoordinate(
struct Node *node,
double x,
double y,
double z)
477void setInbound(
struct Node *interList,
struct Node *list)
480 struct Node *temp1=NULL, *temp=NULL;
481 struct Node *temp1_prev=NULL, *temp1_next=NULL;
482 int prev_is_inside, next_is_inside;
486 if(length(interList) == 0)
return;
491 if( !temp->inbound) {
498 if(sameNode(*temp1, *temp)) {
499 if(!temp1_prev) temp1_prev = getLast(list);
500 temp1_next = temp1->Next;
501 if(!temp1_next) temp1_next = list;
507 if(!temp1_next) error_handler(
"Error from create_xgrid.c: temp is not in list1");
508 if( temp1_prev->isInside == 0 && temp1_next->isInside == 1)
517int isInside(
struct Node *node) {
519 if(node->isInside == -1) error_handler(
"Error from mosaic_util.c: node->isInside is not set");
520 return(node->isInside);
527int insidePolygon(
struct Node *node,
struct Node *list)
530 double pnt0[3], pnt1[3], pnt2[3];
532 struct Node *p1=NULL, *p2=NULL;
552 if( samePoint(pnt0[0], pnt0[1], pnt0[2], pnt1[0], pnt1[1], pnt1[2]) ){
555 anglesum += spherical_angle(pnt0, pnt2, pnt1);
563 if( fabs(anglesum - 2*M_PI) < EPSLN8 ){
real(r8_kind), dimension(:,:), allocatable area
area of each grid box