source: issm/trunk/src/c/shared/Exp/IsInPoly.cpp@ 12706

Last change on this file since 12706 was 12706, checked in by Mathieu Morlighem, 13 years ago

merged trunk-jpl and trunk for revision 12703

File size: 3.0 KB
RevLine 
[1]1/*!\file IsInPoly.c
2 * \brief: from a contour, determine which nodes are in domain.
3 */
4
5#include <math.h>
6#include "../../toolkits/toolkits.h"
7#include "./exp.h"
[291]8#include "../shared.h"
[1]9
[2587]10#ifdef HAVE_CONFIG_H
[9320]11 #include <config.h>
[2587]12#else
13#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
14#endif
[1]15
[12706]16/*IsInPoly {{{*/
[11995]17int IsInPoly(Vector* in,double* xc,double* yc,int numvertices,double* x,double* y,int i0,int i1, int edgevalue){
[2587]18
[291]19 int i;
[1]20 double x0,y0;
21 double value;
[6965]22 double xmin=xc[0];
23 double xmax=xc[0];
24 double ymin=yc[0];
25 double ymax=yc[0];
[1]26
[6965]27 /*Get extrema*/
[8301]28 for (i=1;i<numvertices;i++){
[6965]29 if(xc[i]<xmin) xmin=xc[i];
30 if(xc[i]>xmax) xmax=xc[i];
31 if(yc[i]<ymin) ymin=yc[i];
32 if(yc[i]>ymax) ymax=yc[i];
33 }
34
[8301]35 /*Go through all vertices of the mesh:*/
[2587]36 for (i=i0;i<i1;i++){
[291]37
38 //Get current value of value[i] -> do not change it if != 0
[11995]39 in->GetValue(&value,i);
[291]40 if (value){
[8301]41 /*this vertex already is inside one of the contours, continue*/
[291]42 continue;
[1]43 }
[291]44
[8301]45 /*pick up vertex (x[i],y[i]) and figure out if located inside contour (xc,yc)*/
[291]46 x0=x[i]; y0=y[i];
[6965]47 if(x0<xmin || x0>xmax || y0<ymin || y0>ymax){
48 value=0;
49 }
50 else{
[8301]51 value=pnpoly(numvertices,xc,yc,x0,y0,edgevalue);
[6965]52 }
[11995]53 in->SetValue(i,value,INS_VAL);
[1]54 }
[5016]55 return 1;
[8270]56}/*}}}*/
[12706]57/*IsOutsidePoly {{{*/
[11995]58int IsOutsidePoly(Vector* in,double* xc,double* yc,int numvertices,double* x,double* y,int i0,int i1, int edgevalue){
[1]59
60 int i,j;
61 double x0,y0;
62 double value;
[8270]63 double xmin=xc[0];
64 double xmax=xc[0];
65 double ymin=yc[0];
66 double ymax=yc[0];
[1]67
[8270]68 /*Get extrema*/
[8301]69 for (i=1;i<numvertices;i++){
[8270]70 if(xc[i]<xmin) xmin=xc[i];
71 if(xc[i]>xmax) xmax=xc[i];
72 if(yc[i]<ymin) ymin=yc[i];
73 if(yc[i]>ymax) ymax=yc[i];
74 }
75
[8301]76 /*Go through all vertices of the mesh:*/
[8270]77 for (i=i0;i<i1;i++){
78
79 //Get current value of value[i] -> do not change it if != 0
[11995]80 in->GetValue(&value,i);
[8270]81 if (value){
[8301]82 /*this vertex already is inside one of the contours, continue*/
[8270]83 continue;
[1]84 }
[8270]85
[8301]86 /*pick up vertex (x[i],y[i]) and figure out if located inside contour (xc,yc)*/
[8270]87 x0=x[i]; y0=y[i];
88 if(x0<xmin || x0>xmax || y0<ymin || y0>ymax){
[1]89 value=1;
90 }
[8270]91 else{
[8301]92 value=1-pnpoly(numvertices,xc,yc,x0,y0,edgevalue);
[8270]93 }
[11995]94 in->SetValue(i,value,INS_VAL);
[1]95 }
[5016]96 return 1;
[8270]97}/*}}}*/
[12706]98/*pnpoly{{{*/
[1]99int pnpoly(int npol, double *xp, double *yp, double x, double y, int edgevalue) {
100 int i, j, c = 0;
101 double n1, n2, normp, scalar;
102
103 /*first test, are they colinear? if yes, is the point in the middle of the segment*/
104 if (edgevalue != 2 ){
105 for (i = 0, j = npol-1; i < npol; j = i++) {
106 n1=pow(yp[i]-yp[j],2.0)+pow(xp[i]-xp[j],2.0);
107 n2=pow(y-yp[j],2.0)+pow(x-xp[j],2.0);
108 normp=pow(n1*n2,0.5);
109 scalar=(yp[i]-yp[j])*(y-yp[j])+(xp[i]-xp[j])*(x-xp[j]);
110
111 if (scalar == normp){
112 if (n2<=n1){
113 c = edgevalue;
114 return c;
115 }
116 }
117 }
118 }
119 /*second test : point is neither on a vertex, nor on a side, where is it ?*/
120 for (i = 0, j = npol-1; i < npol; j = i++) {
121 if ((((yp[i]<=y) && (y<yp[j])) ||
122 ((yp[j]<=y) && (y<yp[i]))) &&
123 (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])){
124 c = !c;
125 }
126 }
127 return c;
[8270]128}/*}}}*/
Note: See TracBrowser for help on using the repository browser.