Edge* nearestEdgeInModel (Model *m, Edge *start, float x, float y) /***************************************************************************** nearestEdgeInModel - Return pointer to edge in model nearest to specified (x,y) coordinates ****************************************************************************** Input: m model start edge to look at first (NULL to begin looking anywhere) x x-coordinate y y-coordinate Returns: pointer to nearest Edge ****************************************************************************** Author: Dave Hale, Colorado School of Mines, 09/11/90 ******************************************************************************/ { float d,dmin=0.0; Edge *emin=NULL; EdgeUse *eu; Tri *t; /* find triangle containing point */ if (start!=NULL) { t = (start->eu->f!=NULL?start->eu->f:start->eu->euMate->f); t = insideTriInModel(m,t,x,y); } else { t = insideTriInModel(m,NULL,x,y); } /* loop over edges in triangle */ eu = t->eu; do { /* compute distance to edge */ d = distanceToEdge(eu->e,x,y); /* update minimum distance */ if (eu==t->eu || d<dmin) { dmin = d; emin = eu->e; } /* next edge */ eu = eu->euCW; } while (eu!=t->eu); /* return edge corresponding to minimum distance */ return emin; }
/* the main program */ int main (int argc, char **argv) { int nx,nz,ix,iz; float dx,fx,dz,fz,x,z,xmin,xmax,zmin,zmax; float **a1111,**a3333,**a1133,**a1313,**a1113; float **a1212,**a2323,**a1223,**rho; float **a3313; Tri *t; TriAttributes *ta; Model *m; char *a1111file, *a3333file, *a1133file; char *a1313file, *a1113file, *a3313file; char *a1212file, *a1223file, *a2323file; char *rhofile; FILE *a1111fp=NULL, *a3333fp=NULL, *a1133fp=NULL; FILE *a1313fp=NULL, *a1113fp=NULL, *a3313fp=NULL; FILE *a1212fp=NULL, *a1223fp=NULL, *a2323fp=NULL; FILE *rhofp=NULL; /* hook up getpar to handle the parameters */ initargs(argc,argv); requestdoc(0); /* get required parameters */ if (!getparint("nx",&nx)) err("must specify nx!"); if (!getparint("nz",&nz)) err("must specify nz!"); /* get optional parameters */ if (!getparfloat("dx",&dx)) dx = 1.0; if (!getparfloat("dz",&dz)) dz = 1.0; if (!getparfloat("fx",&fx)) fx = 0.0; if (!getparfloat("fz",&fz)) fz = 0.0; if(getparstring("a1111file",&a1111file)) a1111fp = efopen(a1111file,"w"); else a1111fp = efopen("a1111.bin","w"); if(getparstring("a3333file",&a3333file)) a3333fp = efopen(a3333file,"w"); else a3333fp = efopen("a3333.bin","w"); if(getparstring("a1133file",&a1133file)) a1133fp = efopen(a1133file,"w"); else a1133fp = efopen("a1133.bin","w"); if(getparstring("a1313file",&a1313file)) a1313fp = efopen(a1313file,"w"); else a1313fp = efopen("a1313.bin","w"); if(getparstring("a1113file",&a1113file)) a1113fp = efopen(a1113file,"w"); else a1113fp = efopen("a1113.bin","w"); if(getparstring("a3313file",&a3313file)) a3313fp = efopen(a3313file,"w"); else a3313fp = efopen("a3313.bin","w"); if(getparstring("a1212file",&a1212file)) a1212fp = efopen(a1212file,"w"); else a1212fp = efopen("a1212.bin","w"); if(getparstring("a2323file",&a2323file)) a2323fp = efopen(a2323file,"w"); else a2323fp = efopen("a2323.bin","w"); if(getparstring("a1223file",&a1223file)) a1223fp = efopen(a1223file,"w"); else a1223fp = efopen("a1223.bin","w"); if(getparstring("rhofile",&rhofile)) rhofp = efopen(rhofile,"w"); else rhofp = efopen("rho.bin","w"); checkpars(); /* read input triangulated sloth model */ m = readModel(stdin); /* determine min and max x and z coordinates */ xmin = m->ymin; xmax = m->ymax; zmin = m->xmin; zmax = m->xmax; /* allocate space for uniformly sampled stiffnesses */ a1111 = ealloc2float(nz,nx); a3333 = ealloc2float(nz,nx); a1133 = ealloc2float(nz,nx); a1313 = ealloc2float(nz,nx); a1113 = ealloc2float(nz,nx); a3313 = ealloc2float(nz,nx); a1212 = ealloc2float(nz,nx); a1223 = ealloc2float(nz,nx); a2323 = ealloc2float(nz,nx); rho = ealloc2float(nz,nx); /* loop over all samples */ for (ix=0,x=fx,t=NULL; ix<nx; ++ix,x+=dx) { if (x<xmin || x>xmax) err("x=%g must be between xmin=%g and xmax=%g", x,xmin,xmax); for (iz=0,z=fz; iz<nz; ++iz,z+=dz) { if (z<zmin || z>zmax) err("z=%g must be between zmin=%g and zmax=%g", z,zmin,zmax); t = insideTriInModel(m,t,z,x); ta = (TriAttributes*)t->fa; a1111[ix][iz] = ta->a1111; a3333[ix][iz] = ta->a3333; a1133[ix][iz] = ta->a1133; a1313[ix][iz] = ta->a1313; a1113[ix][iz] = ta->a1113; a3313[ix][iz] = ta->a3313; a1212[ix][iz] = ta->a1212; a2323[ix][iz] = ta->a2323; a1223[ix][iz] = ta->a1223; rho[ix][iz] = ta->rho; } } /* write uniformly sampled sloth */ fwrite(a1111[0],sizeof(float),nz*nx,a1111fp); fwrite(a3333[0],sizeof(float),nz*nx,a3333fp); fwrite(a1133[0],sizeof(float),nz*nx,a1133fp); fwrite(a1313[0],sizeof(float),nz*nx,a1313fp); fwrite(a1113[0],sizeof(float),nz*nx,a1113fp); fwrite(a3313[0],sizeof(float),nz*nx,a3313fp); fwrite(a1212[0],sizeof(float),nz*nx,a1212fp); fwrite(a2323[0],sizeof(float),nz*nx,a2323fp); fwrite(a1223[0],sizeof(float),nz*nx,a1223fp); fwrite(rho[0],sizeof(float),nz*nx,rhofp); return 1; }
static void drawimage (Display *dpy, Drawable dbl, Region region, FGC fgc, Model *m, float fmin, float fmax, int style) { int scr=-1; int x,y,width,height; int i,j,k,line,iline,jline,widthpad; unsigned long pmin,pmax,p; float fx,fy,s,base,scale; Tri *t=NULL; TriAttributes *ta; XRectangle rect; XImage *image=NULL; int bitmap_pad=0; int nbpr=0; #if 0 /* OLD VERSION . See JG fix below */ unsigned char *data=NULL; scr=DefaultScreen(dpy); /* Kludge to fix problem with XCreateImage introduced in */ /* Xorg 7.0 update for security */ if (BitmapPad(dpy)>16) { bitmap_pad = 16; } else if (BitmapPad(dpy) < 16) { bitmap_pad = 8; } /* determine smallest box enclosing region */ XClipBox(region,&rect); x = rect.x; y = rect.y; width = rect.width; height = rect.height; if (width==0 || height==0) return; /* allocate memory for image data */ widthpad = (1+(width-1)/bitmap_pad)*bitmap_pad; nbpr = widthpad-1; data = alloc1(widthpad*height,sizeof(unsigned char)); if (data==NULL) err("width,widthpad,height = %d %d %d", width,widthpad,height); warn("nbpr = %d widthpad = %d height = %d bitmap_pad = %d ", nbpr,widthpad,height,bitmap_pad); /* determine min and max pixels from standard colormap */ pmin = XtcwpGetFirstPixel(dpy); pmax = XtcwpGetLastPixel(dpy); /* determine base and scale factor */ scale = (fmax!=fmin) ? ((float) (pmax-pmin))/(fmax-fmin) : 0.0; base = ((float) pmin)-fmin*scale; /* loop over scan lines */ for (line=0; line<height; line++) { iline = line*width; jline = line*widthpad; /* loop over pixels in scan line */ for (i=iline,j=jline,k=0; k<width; ++i,++j,++k) { /* determine float x and y coordinates */ if (style==XtcwpNORMAL) { fx = MapX(fgc,x+k); fy = MapY(fgc,y+line); } else { fx = MapY(fgc,y+line); fy = MapX(fgc,x+k); } /* determine sloth */ t = insideTriInModel(m,t,fx,fy); ta = (TriAttributes*)t->fa; s = ta->s00+fy*ta->dsdx+fx*ta->dsdz; /* convert to pixel and put in image */ p = (unsigned long) (base+s*scale); if (p<pmin) p = pmin; if (p>pmax) p = pmax; data[j] = (unsigned char) p; } for (j=jline+width,k=width; k<widthpad; ++j,++k) data[j] = data[jline+width-1]; } /* create, put, and destroy image */ image = XCreateImage( (Display *) dpy, (Visual *) DefaultVisual(dpy,scr), (unsigned int) DefaultDepth(dpy,scr), (int)ZPixmap, (int) 0, (char*)data, (unsigned int) widthpad, (unsigned int) height, (int) bitmap_pad, (int) nbpr); #else char *data=NULL; char noCmap; scr=DefaultScreen(dpy); /* JG: get bitmap_pad from X */ bitmap_pad = BitmapPad(dpy); /* determine smallest box enclosing region */ XClipBox(region,&rect); x = rect.x; y = rect.y; width = rect.width; height = rect.height; if (width==0 || height==0) return; /* allocate memory for image data */ widthpad = (1+(width-1)/bitmap_pad)*bitmap_pad; nbpr = widthpad-1; /* create image & determine alloc size from X */ image = XCreateImage( (Display *) dpy, (Visual *) DefaultVisual(dpy,scr), (unsigned int) DefaultDepth(dpy,scr), (int)ZPixmap, (int) 0, NULL , (unsigned int) widthpad, (unsigned int) height, (int) bitmap_pad, 0); /* JG XCreateImage(....,0) gets X to compute the size it needs. Then we alloc. */ image->data = (char *)calloc(image->bytes_per_line, image->height); if (image->data==NULL) err("width,widthpad,height = %d %d %d", width,widthpad,height); /* warn("nbpr = %d widthpad = %d height = %d bitmap_pad = %d ", nbpr,widthpad,height,bitmap_pad); */ /* determine min and max pixels from standard colormap */ pmin = XtcwpGetFirstPixel(dpy); pmax = XtcwpGetLastPixel(dpy); /* JGHACK ... When colormap fails, we get pmax=pmin=0 */ noCmap = (pmax==0 && pmin==0) ? 1:0; if (noCmap) { pmax = 255; warn("No colormap found...."); } /* ...JGHACK */ /* determine base and scale factor */ scale = (fmax!=fmin) ? ((float) (pmax-pmin))/(fmax-fmin) : 0.0; base = ((float) pmin)-fmin*scale; data = (char *)image->data ; /* loop over scan lines */ for (line=0; line<height; line++) { iline = line*width; jline = line*widthpad; /* loop over pixels in scan line */ for (i=iline,j=jline,k=0; k<width; ++i,++j,++k) { /* determine float x and y coordinates */ if (style==XtcwpNORMAL) { fx = MapX(fgc,x+k); fy = MapY(fgc,y+line); } else { fx = MapY(fgc,y+line); fy = MapX(fgc,x+k); } /* determine sloth */ t = insideTriInModel(m,t,fx,fy); ta = (TriAttributes*)t->fa; s = ta->s00+fy*ta->dsdx+fx*ta->dsdz; /* convert to pixel and put in image */ p = (unsigned long) (base+s*scale); if (p<pmin) p = pmin; if (p>pmax) p = pmax; if (noCmap) { /* JG. Can't get colormap. Might as well write RGB pixels as grayscale */ XPutPixel(image,k,line, p | (p<<8)| (p<<16)); } else { /* original */ /* data[j] = (unsigned char) p; */ XPutPixel(image,k,line,p); } } /* original. Not sure this is needed JG */ /* for (j=jline+width,k=width; k<widthpad; ++j,++k) data[j] = data[jline+width-1]; */ } #endif XPutImage(dpy,dbl,fgc->gc,image,0,0,x,y,image->width,image->height); /* free(data); */ XDestroyImage(image); }
/* the main program */ int main (int argc, char **argv) { /* int want,mindex=0; */ int want; float x,z,a1111,a3333,a1133,a1313,a1113,a3313,v_p,v_s; float a1212,a2323,a1223,rho; Model *m; Face *t; FaceAttributes *fa; char *mfile; FILE *mfp; want=1; /* hook up getpar to handle the parameters */ initargs(argc,argv); requestdoc(0); if (!getparstring("file",&mfile)) err("ERROR: No modelfile defined"); mfp = efopen(mfile,"r"); checkpars(); a1111=a3333=a1133=a1313=a1113=a3313=v_p=v_s=0; a1212=a1223=a2323=rho=0; /* read model */ m = readModel(mfp); fprintf (stderr," **********************************************\n"); fprintf (stderr," *********check stiffness coefficients*********\n"); fprintf (stderr," **********************************************\n\n"); do { want = 1; fprintf (stderr," Enter x-coordinate \n"); scanf ("%f", &x); fprintf (stderr," Enter z-coordinate \n"); scanf ("%f", &z); /* determine triangle containing point (x,z) */ if(x > m->ymax || x < m->ymin || z > m->xmax || z < m->xmin){ fprintf (stderr," Coordinate outside of model \n\n"); continue; } t = insideTriInModel(m,NULL,z,x); if ((fa=t->fa)!=NULL){ a1111 = fa->a1111; a3333 = fa->a3333; a1133 = fa->a1133; a1313 = fa->a1313; a1113 = fa->a1113; a3313 = fa->a3313; v_p = sqrt(a3333); v_s = sqrt(a1313); a1212 = fa->a1212; a1223 = fa->a1223; a2323 = fa->a2323; rho = fa->rho; /* mindex = fa->mindex; */ } fprintf(stderr,"\n ----Coordinates x=%g \t z=%g ----\n\n",x,z); /* fprintf(stderr,"\t\t mindex=%i \n ",mindex); */ fprintf(stderr,"\t\t a1111=%g \n",a1111); fprintf(stderr,"\t\t a3333=%g \n",a3333); fprintf(stderr,"\t\t a1133=%g \n",a1133); fprintf(stderr,"\t\t a1313=%g \n",a1313); fprintf(stderr,"\t\t a1113=%g \n",a1113); fprintf(stderr,"\t\t a3313=%g \n",a3313); fprintf(stderr,"\t\t a1212=%g \n",a1212); fprintf(stderr,"\t\t a1223=%g \n",a1223); fprintf(stderr,"\t\t a2323=%g \n",a2323); fprintf(stderr,"\t\t rho=%g \n",rho); fprintf(stderr,"\t\t v_p=%g \n",v_p); fprintf(stderr,"\t\t v_s=%g \n\n",v_s); fprintf (stderr," More checks type: 1 if yes, 0 if no \n"); scanf ("%i", &want); } while (want!=0); return 1; }