📄 polar.c
字号:
/* Copyright (c) Colorado School of Mines, 2003.*//* All rights reserved. *//*********************** self documentation **********************//**************************************************************************POLAR - Functions to map data in rectangular coordinates to polar and vise versarecttopolar convert a function p(x,y) to a function q(a,r)polartorect convert a function q(a,r) to a function p(x,y)******************************************************************************Function Prototypes:void recttopolar ( int nx, float dx, float fx, int ny, float dy, float fy, float **p, int na, float da, float fa, int nr, float dr, float fr, float **q);void polartorect ( int na, float da, float fa, int nr, float dr, float fr, float **q, int nx, float dx, float fx, int ny, float dy, float fy, float **p)******************************************************************************recttopolar:Input:nx number of x samplesdx x sampling intervalfx first x sampleny number of y samplesdy y sampling intervalfy first y samplep array[ny][nx] containing samples of p(x,y)na number of a samplesda a sampling intervalfa first a samplenr number of r samplesdr r sampling intervalfr first r sampleOutput:q array[nr][na] containing samples of q(a,r)******************************************************************************polartorect:Input:na number of a samplesda a sampling intervalfa first a samplenr number of r samplesdr r sampling intervalfr first r samplenx number of x samplesdx x sampling intervalfx first x sampleny number of y samplesdy y sampling intervalfy first y sampleq array[nr][na] containing samples of q(a,r)Output:p array[ny][nx] containing samples of p(x,y)******************************************************************************Notes:The polar angle a is measured in radians, x = r*cos(a) and y = r*sin(a).recttopolar:Linear extrapolation is used to determine the value of p(x,y) forx and y coordinates not in the range corresponding to nx, dx, ....polartorect:Linear extrapolation is used to determine the value of q(a,r) fora and r coordinates not in the range corresponding to na, da, ....******************************************************************************Author: Dave Hale, Colorado School of Mines, 06/15/90******************************************************************************//**************** end self doc ********************************/#include "cwp.h"void recttopolar ( int nx, float dx, float fx, int ny, float dy, float fy, float **p, int na, float da, float fa, int nr, float dr, float fr, float **q)/*****************************************************************************Convert a function of p(x,y) to q(a,r), where x = r*cos(a) and y = r*sin(a)******************************************************************************Input:nx number of x samplesdx x sampling intervalfx first x sampleny number of y samplesdy y sampling intervalfy first y samplep array[ny][nx] containing samples of p(x,y)na number of a samplesda a sampling intervalfa first a samplenr number of r samplesdr r sampling intervalfr first r sampleOutput:q array[nr][na] containing samples of q(a,r)******************************************************************************Notes:The polar angle a is measured in radians.Linear extrapolation is used to determine the value of p(x,y) forx and y coordinates not in the range corresponding to nx, dx, ....******************************************************************************Author: Dave Hale, Colorado School of Mines, 06/15/90******************************************************************************/{ int ia,ir,ix,iy; float a,r,x,y,xi,yi,sx,sy; /* for all r */ for (ir=0,r=fr; ir<nr; ++ir,r+=dr) { /* for all a */ for (ia=0,a=fa; ia<na; ++ia,a+=da) { /* determine x and y */ x = (float)(r*cos(a)); y = (float)(r*sin(a)); /* determine sample indices */ xi = (x-fx)/dx; ix = (int)xi; if (ix<0 ) {xi =0; ix = 0; } if (ix>nx-2) {ix = nx-2; xi = nx-1.0f;} yi = (y-fy)/dy; iy = (int)yi; if (iy<0) {yi = 0;iy = 0;} if (iy>ny-2) {iy = ny-2; yi = ny-1.0f;} /* bilinear interpolation */ sx = xi-ix; sy = yi-iy; q[ir][ia] = (1.0f-sy)*((1.0f-sx)*p[iy][ix] + sx*p[iy][ix+1]) + sy*((1.0f-sx)*p[iy+1][ix] + sx*p[iy+1][ix+1]); } }}void polartorect ( int na, float da, float fa, int nr, float dr, float fr, float **q, int nx, float dx, float fx, int ny, float dy, float fy, float **p)/*****************************************************************************Convert a function of q(a,r) to p(x,y), where x = r*cos(a) and y = r*sin(a)******************************************************************************Input:na number of a samplesda a sampling intervalfa first a samplenr number of r samplesdr r sampling intervalfr first r samplenx number of x samplesdx x sampling intervalfx first x sampleny number of y samplesdy y sampling intervalfy first y sampleq array[nr][na] containing samples of q(a,r)Output:p array[ny][nx] containing samples of p(x,y)******************************************************************************Notes:The polar angle a is measured in radians.Linear extrapolation is used to determine the value of q(a,r) fora and r coordinates not in the range corresponding to na, da, ....******************************************************************************Author: Dave Hale, Colorado School of Mines, 06/15/90******************************************************************************/{ int ix,iy,ia,ir; float x,y,a=0.0,r,ai,ri,sa,sr; /* for all y */ for (iy=0,y=fy; iy<ny; ++iy,y+=dy) { /* for all x */ for (ix=0,x=fx; ix<nx; ++ix,x+=dx) { /* determine a and r */ if (x !=0.0) a = (float)atan2((double) y,(double) x); else if (y>0.0) a = (float)(PI/2.0); else if (y<0.0) a = (float)(-PI/2.0); else if (y==0.0) a = 0.0; r = (float)sqrt(x*x+y*y); /* determine sample indices */ ai = (a-fa)/da; ia = (int)ai; if (ia<0) {ai = 0;ia = 0;} if (ia>na-2) {ai = na-1.0f; ia = na-2;} ri = (r-fr)/dr; ir = (int)ri; if (ir<0) {ri =0; ir = 0;} if (ir>nr-2) {ri = nr-1.0f; ir = nr-2;} /* bilinear interpolation */ sa = ai-ia; sr = ri-ir; p[iy][ix] = (1.0f-sr)*((1.0f-sa)*q[ir][ia] + sa*q[ir][ia+1]) + sr*((1.0f-sa)*q[ir+1][ia] + sa*q[ir+1][ia+1]); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -