float interp_floatvec( floatvec *fv , float x ) { int ix , im1,ip1,ip2 , itop ; float fx , val , abot,atop ; if( fv == NULL || fv->ar == NULL ) return 0.0f ; itop = fv->nar - 1 ; if( itop <= 1 || fv->dx == 0.0 ) return(fv->ar[0]) ; /* if input x is out of range, return the edge value */ fx = (x - fv->x0) / fv->dx ; if( fx <= 0.0f ) return(fv->ar[0]) ; else if( fx >= itop ) return(fv->ar[itop]) ; /* input x is between point #ix and #ix+1 */ /* fractional offset between them is fx */ ix = (int)fx ; fx = fx - ix ; /* get indexes below (im1) and above (ip1 and ip2) */ im1 = ix-1 ; if( im1 < 0 ) im1 = 0 ; ip1 = ix+1 ; if( ip1 > itop ){ ip1 = ip2 = itop ; } else { ip2 = ip1+1 ; if( ip2 > itop ) ip2 = itop ; } /* cubic interpolation between these 4 points */ val = P_M1(fx)*fv->ar[im1] + P_00(fx)*fv->ar[ix] + P_P1(fx)*fv->ar[ip1] + P_P2(fx)*fv->ar[ip2] ; /* make sure result lies in the local range of values */ abot = fv->ar[ix] ; atop = fv->ar[ip1] ; if( abot > atop ){ fx = abot; abot = atop; atop = fx; } if( val < abot ) val = abot; else if( val > atop ) val = atop; return(val) ; }
void cub_shift( int n , float af , float * f ) { int ii , ia , ix ; float wt_m1 , wt_00 , wt_p1 , wt_p2 , aa ; #ifdef SEPARATE_FINS int ibot,itop ; #endif ENTRY("cub_shift") ; af = -af ; ia = (int) af ; if( af < 0 ) ia-- ; /* ia = floor */ /* 15 Mar 2001: if shift is too large, return all zeros */ if( ia <= -n || ia >= n ){ for( ii=0 ; ii < n ; ii++ ) f[ii] = 0.0 ; EXRETURN ; } aa = af - ia ; wt_m1 = P_M1(aa) ; wt_00 = P_00(aa) ; wt_p1 = P_P1(aa) ; wt_p2 = P_P2(aa) ; if( n > nlcbuf ){ if( lcbuf != NULL ) free(lcbuf) ; lcbuf = (float *) malloc( sizeof(float) * n ) ; nlcbuf = n ; } #ifdef SEPARATE_FINS ibot = 1-ia ; if( ibot < 0 ) ibot = 0 ; itop = n-3-ia ; if( itop > n-1 ) itop = n-1 ; for( ii=ibot ; ii <= itop ; ii++ ){ ix = ii + ia ; lcbuf[ii] = wt_m1 * f[ix-1] + wt_00 * f[ix] + wt_p1 * f[ix+1] + wt_p2 * f[ix+2] ; } if( ibot > n ) ibot = n ; /* 15 Mar 2001 */ for( ii=0 ; ii < ibot ; ii++ ){ ix = ii + ia ; lcbuf[ii] = wt_m1 * FINS(ix-1) + wt_00 * FINS(ix) + wt_p1 * FINS(ix+1) + wt_p2 * FINS(ix+2) ; } if( itop < 0 ) itop = -1 ; /* 15 Mar 2001 */ for( ii=itop+1 ; ii < n ; ii++ ){ ix = ii + ia ; lcbuf[ii] = wt_m1 * FINS(ix-1) + wt_00 * FINS(ix) + wt_p1 * FINS(ix+1) + wt_p2 * FINS(ix+2) ; } #else /* not SEPARATE_FINS */ for( ii=0 ; ii < n ; ii++ ){ ix = ii + ia ; if( ix > 0 && ix < n-2 ) lcbuf[ii] = wt_m1 * f[ix-1] + wt_00 * f[ix] + wt_p1 * f[ix+1] + wt_p2 * f[ix+2] ; else lcbuf[ii] = wt_m1 * FINS(ix-1) + wt_00 * FINS(ix) + wt_p1 * FINS(ix+1) + wt_p2 * FINS(ix+2) ; } #endif /* SEPARATE_FINS */ memcpy( f , lcbuf , sizeof(float)*n ) ; EXRETURN ; }
void algebraic_number_test() { typedef Coefficient_ Coefficient; typedef Rational_ Rational; typedef CGAL::internal::Algebraic_real_d_1<Coefficient,Rational, CGAL::Handle_policy_no_union, RepClass > Algebraic_real_d_1; typedef typename CGAL::Polynomial_type_generator<Coefficient,1>::Type Poly; CGAL::test_real_embeddable<Algebraic_real_d_1>(); // general test of comparable functionality // TODO generates a precondition error in Algebraic_real_rep //NiX::test_real_comparable<Algebraic_real_d_1>(); // test of constructors Poly P_00(Coefficient(0)); // zero polynomial Poly P_01(Coefficient(1)); // constant polynomial Poly P_1(Coefficient(-1),Coefficient(1)); //(x-1) Poly P_2(Coefficient(-2),Coefficient(1)); //(x-2) Poly P_3(Coefficient(-3),Coefficient(1)); //(x-3) Poly P_4(Coefficient(-4),Coefficient(1)); //(x-4) Poly P_12=P_1*P_2; //(x-1)(x-2) Poly P_123=P_1*P_2*P_3; //(x-1)(x-2)(x-3) Poly P_s2(Coefficient(-2),Coefficient(0),Coefficient(1)); //(x^2-2) Poly P_s3(Coefficient(-3),Coefficient(0),Coefficient(1)); //(x^2-3) Poly P_s5(-Coefficient(5),Coefficient(0),Coefficient(1)); Poly P_s10(-Coefficient(10),Coefficient(0),Coefficient(1)); Poly P_s30(-Coefficient(30),Coefficient(0),Coefficient(1)); Poly P_s2510= P_s2*P_s5*P_s10; Poly P_s530= P_s5 * P_s30; Algebraic_real_d_1 tmp; Algebraic_real_d_1 tmp1,tmp2; Rational m; // general constructors; // default // tmp = IS_Rational_ = 0 tmp = Algebraic_real_d_1(); assert(tmp.is_rational()); assert(tmp.rational()==0); // from int tmp = Algebraic_real_d_1(1); assert(tmp.is_rational()); assert(tmp.rational()==1); tmp = Algebraic_real_d_1(5); assert(tmp.is_rational()); assert(tmp.rational()==5); // from Field tmp = Algebraic_real_d_1(Rational(0)); assert(tmp.is_rational()); assert(tmp.rational()==0); tmp = Algebraic_real_d_1(Rational(1)); assert(tmp.is_rational()); assert(tmp.rational()==1); tmp = Algebraic_real_d_1(Rational(5)/ Rational(2)); assert(tmp.is_rational()); assert(tmp.rational()== Rational(5)/ Rational(2)); // general constructor // tmp = 1 #if 0 tmp = Algebraic_real_d_1(P_1,-2,+2); // TODO different behavior with leda and core assert(!tmp.is_rational()); assert(tmp==Rational(1)); assert(tmp.is_rational()); assert(tmp.rational()==1); #endif // special constructors // from int tmp = Algebraic_real_d_1(2); assert(tmp.is_rational()); assert(tmp.rational()==Rational(2)); //from Rational tmp = Algebraic_real_d_1(Rational(2)); assert(tmp.is_rational()); assert(tmp.rational()==Rational(2)); // member functions // tmp IS_GENERAL == 2; tmp = Algebraic_real_d_1(P_123,Rational(3)/2,Rational(5)/2); assert(!tmp.is_rational()); assert(tmp.polynomial()==P_123); assert(tmp.low()==Rational(3)/2); assert(tmp.high()==Rational(5)/2); assert(tmp.sign_at_low()==P_123.sign_at(Rational(3)/2)); // refine tmp = Algebraic_real_d_1(P_123,Rational(3)/2,Rational(5)/2); tmp.refine(); assert(tmp.is_rational()); assert(tmp.rational()==Rational(2)); // tmp IS_GENERAL = sqrt 2 tmp = Algebraic_real_d_1(P_s2*P_3,Rational(1),Rational(2)); tmp.refine(); assert(tmp.low() >= Rational(1)); assert(tmp.high() <= Rational(3)/2); // strong_refine // tmp IS_GENERAL == 2; tmp = Algebraic_real_d_1(P_123,Rational(3)/2,Rational(5)/2); m = Rational(2); tmp.strong_refine(m); assert(tmp.is_rational()); assert(tmp.rational()==Rational(2)); // tmp IS_GENERAL = sqrt 2 tmp = Algebraic_real_d_1(P_s2*P_3,Rational(1),Rational(2)); m = Rational(3)/2; tmp.strong_refine(m); assert(tmp.low()!=m); assert(tmp.high()!=m); // refine_to(a,b) // tmp IS_GENERAL = sqrt 2 tmp = Algebraic_real_d_1(P_s2*P_4,Rational(0),Rational(3)); assert(!tmp.is_rational()); tmp.refine_to(Rational(1), Rational(2)); assert(tmp.low() >= Rational(1)); assert(tmp.high() <= Rational(2)); // tmp IS_REAL = sqrt 2 tmp = Algebraic_real_d_1(P_s2,Rational(0),Rational(3)); assert(!tmp.is_rational()); tmp.refine_to(Rational(1), Rational(2)); assert(tmp.low() >= Rational(1)); assert(tmp.high() <= Rational(2)); // compare(rat) // tmp IS_GENERAL = sqrt 2 tmp = Algebraic_real_d_1(P_s2*P_3,Rational(1),Rational(2)); m = Rational(1); assert(tmp.compare(m)==1); m = Rational(2); assert(tmp.compare(m)==-1); // tmp IS_GENERAL = 3 tmp = Algebraic_real_d_1(P_s2*P_3,Rational(2),Rational(4)); m = Rational(3); assert(tmp.compare(m)==0); assert(tmp.is_rational()); assert(tmp.rational()==Rational(3)); assert(CGAL::degree(tmp.polynomial()) == 1); assert(tmp.polynomial().evaluate(Coefficient(3)) == Coefficient(0)); // compare_distinct() tmp1 = Algebraic_real_d_1(P_s530, Rational(2), Rational(3)); // sqrt(5) = 2.236... tmp2 = Algebraic_real_d_1(P_s530, Rational(5), Rational(6)); // sqrt(30) = 5.477... assert(tmp1.compare_distinct(tmp2) == CGAL::SMALLER); assert(tmp2.compare_distinct(tmp1) == CGAL::LARGER); //member functions // is_root_of tmp1 = Algebraic_real_d_1(P_s2510,Rational(1)/2,Rational(3)/2); assert(tmp1.is_root_of(P_s530*P_s2)); tmp1 = Algebraic_real_d_1(P_s2510,Rational(1)/2,Rational(3)/2); assert(!tmp1.is_root_of(P_s530)); //rational_between { Rational r; tmp1 = Algebraic_real_d_1(P_s2,Rational(1),Rational(2)); //sqrt2 tmp2 = Algebraic_real_d_1(P_s3,Rational(1),Rational(3)); //sqrt3 r = tmp1.rational_between(tmp2); assert(tmp1.compare(r)==CGAL::SMALLER); assert(tmp2.compare(r)==CGAL::LARGER); r = tmp2.rational_between(tmp1); assert(tmp1.compare(r)==CGAL::SMALLER); assert(tmp2.compare(r)==CGAL::LARGER); } // to_double() tmp = Algebraic_real_d_1(P_1*P_3*P_4, Rational(0), Rational(2)); assert(fabs(tmp.to_double() - 1.0) < 1e-10); tmp = Algebraic_real_d_1(P_1*P_3, Rational(0), Rational(2)); assert(fabs(tmp.to_double() - 1.0) < 1e-10); tmp = Algebraic_real_d_1(P_1, Rational(0), Rational(2)); assert(fabs(tmp.to_double() - 1.0) < 1e-10); //IO tested in _test_algebraic_kernel_1.h // test for Handle with union { typedef CGAL::internal::Algebraic_real_d_1 <Coefficient,Rational,::CGAL::Handle_policy_union> Int; Int i(5); Int j(5); Int k(6); assert( ! i.identical( j)); assert( ! i.identical( k)); assert( ! j.identical( k)); assert( i == j); assert( ! (i == k)); assert( i.identical( j)); assert( ! i.identical( k)); assert( ! j.identical( k)); // code coverage assert( i == j); } // test for Handle without union { typedef CGAL::internal::Algebraic_real_d_1 <Coefficient,Rational,::CGAL::Handle_policy_no_union> Int; Int i(5); Int j(5); Int k(6); assert( ! i.identical( j)); assert( ! i.identical( k)); assert( ! j.identical( k)); assert( i == j); assert( ! (i == k)); assert( ! i.identical( j)); assert( ! i.identical( k)); assert( ! j.identical( k)); } // to_interval // { // Algebraic_real_d_1 TMP; // assert(CGAL::in(25.0,CGAL::to_interval(Algebraic_real_d_1(25)))); // assert(CGAL::in(sqrt(2),CGAL::to_interval(Algebraic_real_d_1(P_s2,1,2)))); // assert(CGAL::in(sqrt(2),CGAL::to_interval(Algebraic_real_d_1(P_s2510,1,2)))); // assert(CGAL::in(-sqrt(2),CGAL::to_interval(Algebraic_real_d_1(P_s2510,-2,-1)))); // assert(CGAL::in(sqrt(5),CGAL::to_interval(Algebraic_real_d_1(P_s2510,2,3)))); // assert(CGAL::in(-sqrt(5),CGAL::to_interval(Algebraic_real_d_1(P_s2510,-3,-2)))); // assert(CGAL::in(sqrt(10),CGAL::to_interval(Algebraic_real_d_1(P_s2510,3,4)))); // assert(CGAL::in(-sqrt(10),CGAL::to_interval(Algebraic_real_d_1(P_s2510,-4,-3)))); // } //simplify { // just a synatx check Algebraic_real_d_1(P_s2510,1,2).simplify(); } }
MRI_IMAGE *mri_rota( MRI_IMAGE *im, float aa, float bb, float phi ) { float rot_dx , rot_dy , rot_cph , rot_sph , top,bot,val ; MRI_IMAGE *imfl , *newImg ; MRI_IMARR *impair ; float *far , *nar ; float xx,yy , fx,fy ; int ii,jj, nx,ny , ix,jy , ifx,jfy ; float f_jm1,f_j00,f_jp1,f_jp2 , wt_m1,wt_00,wt_p1,wt_p2 ; #ifdef USE_CGRID if( p_first ){ p_first = 0 ; xx = 1.0 / CGRID ; for( ii=0 ; ii <= CGRID ; ii++ ){ yy = ii * xx ; p_m1[ii] = P_M1(yy) ; p_00[ii] = P_00(yy) ; p_p1[ii] = P_P1(yy) ; p_p2[ii] = P_P2(yy) ; } } #endif if( im == NULL || ! MRI_IS_2D(im) ){ fprintf(stderr,"*** mri_rota only works on 2D images!\n") ; EXIT(1) ; } /** if complex image, break into pairs, do each separately, put back together **/ if( im->kind == MRI_complex ){ MRI_IMARR *impair ; MRI_IMAGE * rim , * iim , * tim ; impair = mri_complex_to_pair( im ) ; if( impair == NULL ){ fprintf(stderr,"*** mri_complex_to_pair fails in mri_rota!\n") ; EXIT(1) ; } rim = IMAGE_IN_IMARR(impair,0) ; iim = IMAGE_IN_IMARR(impair,1) ; FREE_IMARR(impair) ; tim = mri_rota( rim , aa,bb,phi ) ; mri_free( rim ) ; rim = tim ; tim = mri_rota( iim , aa,bb,phi ) ; mri_free( iim ) ; iim = tim ; newImg = mri_pair_to_complex( rim , iim ) ; mri_free( rim ) ; mri_free( iim ) ; MRI_COPY_AUX(newImg,im) ; return newImg ; } /** rotation params **/ rot_cph = cos(phi) ; rot_sph = sin(phi) ; rot_dx = (0.5 * im->nx) * (1.0-rot_cph) - aa*rot_cph - bb*rot_sph -(0.5 * im->ny) * rot_sph ; rot_dy = (0.5 * im->nx) * rot_sph + aa*rot_sph - bb*rot_cph +(0.5 * im->ny) * (1.0-rot_cph) ; /** other initialization **/ nx = im->nx ; /* image dimensions */ ny = im->ny ; if( im->kind == MRI_float ) imfl = im ; else imfl = mri_to_float( im ) ; far = MRI_FLOAT_PTR(imfl) ; /* access to float data */ newImg = mri_new( nx , nx , MRI_float ) ; /* output image */ nar = MRI_FLOAT_PTR(newImg) ; /* output image data */ bot = top = far[0] ; for( ii=0 ; ii < nx*ny ; ii++ ) if( far[ii] < bot ) bot = far[ii] ; else if( far[ii] > top ) top = far[ii] ; /*** loop over output points and warp to them ***/ for( jj=0 ; jj < nx ; jj++ ){ xx = rot_sph * jj + rot_dx - rot_cph ; yy = rot_cph * jj + rot_dy + rot_sph ; for( ii=0 ; ii < nx ; ii++ ){ xx += rot_cph ; /* get x,y in original image */ yy -= rot_sph ; ix = (xx >= 0.0) ? ((int) xx) : ((int) xx)-1 ; /* floor */ jy = (yy >= 0.0) ? ((int) yy) : ((int) yy)-1 ; #ifdef USE_CGRID ifx = (xx-ix)*CGRID + 0.499 ; wt_m1 = p_m1[ifx] ; wt_00 = p_00[ifx] ; wt_p1 = p_p1[ifx] ; wt_p2 = p_p2[ifx] ; #else fx = xx-ix ; wt_m1 = P_M1(fx) ; wt_00 = P_00(fx) ; wt_p1 = P_P1(fx) ; wt_p2 = P_P2(fx) ; #endif if( ix > 0 && ix < nx-2 && jy > 0 && jy < ny-2 ){ float * fym1, *fy00 , *fyp1 , *fyp2 ; fym1 = far + (ix-1 + (jy-1)*nx) ; fy00 = fym1 + nx ; fyp1 = fy00 + nx ; fyp2 = fyp1 + nx ; f_jm1 = wt_m1 * fym1[0] + wt_00 * fym1[1] + wt_p1 * fym1[2] + wt_p2 * fym1[3] ; f_j00 = wt_m1 * fy00[0] + wt_00 * fy00[1] + wt_p1 * fy00[2] + wt_p2 * fy00[3] ; f_jp1 = wt_m1 * fyp1[0] + wt_00 * fyp1[1] + wt_p1 * fyp1[2] + wt_p2 * fyp1[3] ; f_jp2 = wt_m1 * fyp2[0] + wt_00 * fyp2[1] + wt_p1 * fyp2[2] + wt_p2 * fyp2[3] ; } else { f_jm1 = wt_m1 * FINS(ix-1,jy-1) + wt_00 * FINS(ix ,jy-1) + wt_p1 * FINS(ix+1,jy-1) + wt_p2 * FINS(ix+2,jy-1) ; f_j00 = wt_m1 * FINS(ix-1,jy) + wt_00 * FINS(ix ,jy) + wt_p1 * FINS(ix+1,jy) + wt_p2 * FINS(ix+2,jy) ; f_jp1 = wt_m1 * FINS(ix-1,jy+1) + wt_00 * FINS(ix ,jy+1) + wt_p1 * FINS(ix+1,jy+1) + wt_p2 * FINS(ix+2,jy+1) ; f_jp2 = wt_m1 * FINS(ix-1,jy+2) + wt_00 * FINS(ix ,jy+2) + wt_p1 * FINS(ix+1,jy+2) + wt_p2 * FINS(ix+2,jy+2) ; } #define THIRTYSIX 2.7777778e-2 /* 1./36.0, actually */ #ifdef USE_CGRID jfy = (yy-jy)*CGRID + 0.499 ; val = ( p_m1[jfy] * f_jm1 + p_00[jfy] * f_j00 + p_p1[jfy] * f_jp1 + p_p2[jfy] * f_jp2 ) * THIRTYSIX ; #else fy = yy-jy ; val = ( P_M1(fy) * f_jm1 + P_00(fy) * f_j00 + P_P1(fy) * f_jp1 + P_P2(fy) * f_jp2 ) * THIRTYSIX ; #endif if( val < bot ) nar[ii+jj*nx] = bot ; /* too small! */ else if( val > top ) nar[ii+jj*nx] = top ; /* too big! */ else nar[ii+jj*nx] = val ; /* just right */ } } /*** cleanup and return ***/ if( im != imfl ) mri_free(imfl) ; /* throw away unneeded workspace */ MRI_COPY_AUX(newImg,im) ; return newImg ; }
template <class Type> void Bench_PackB_IM<Type>::verif() { INT def = (INT)(NRrandom3() * 1000); verif( rectangle(Pt2di(1,0),Pt2di(2,1))); for (INT k=0; k<10 ; k++) { verif( rectangle(Pt2di(k,0),Pt2di(k+1,10) ),def); verif( rectangle(Pt2di(k,0),Pt2di(k+10,10) ),def); verif( rectangle(Pt2di(k,0),Pt2di(k+100,10)),def); verif( rectangle(Pt2di(k,0),Pt2di(k+200,10)),def); } verif( im1.all_pts()); verif( rectangle(Pt2di(-10,-20),sz+Pt2di(30,40)),def); verif( disc(sz/2.0,euclid(sz)/1.8),def); { for (INT k=0 ; k<10 ; k++) { Pt2dr c = sz/2.0 + Pt2dr(NRrandom3(),NRrandom3())*20; REAL ray = 1+NRrandom3()*100; verif(disc(c,ray),def); } } ELISE_COPY(disc(CentreRand(),RayonRand()),1,im1.out()| pck.out()); verif(im1.all_pts()); ELISE_COPY(disc(CentreRand(),RayonRand()),frandr()*8,im1.out()| pck.out()); verif(im1.all_pts()); INT NbPts = (INT)(3 + NRrandom3()*10); ElList<Pt2di> Lpt; { for (INT k=0; k<NbPts ; k++) Lpt = Lpt+Pt2di(CentreRand()); } ELISE_COPY(polygone(Lpt),NRrandom3()<0.1,im1.out()| pck.out()); verif(im1.all_pts()); ModifCSte(rectangle(Pt2di(5,0),Pt2di(10,10)),2); verif(im1.all_pts()); ModifLut(rectangle(Pt2di(0,5),Pt2di(12,12)),FX&3); verif(im1.all_pts()); //ModifCSte(disc(Pt2di(50,50),20),3); ModifCSte(disc(Pt2dr(50,50),20),3); // __NEW verif(im1.all_pts()); for (INT NbC =0 ; NbC < 20 ; NbC++) { ElList<Pt2di> lPt; for (INT iPt =0 ; iPt < 20; iPt ++) { lPt = lPt + Pt2di(CentreRand()); } ModifCSte(polygone(lPt),INT(NRrandom3() * 3)); verif(im1.all_pts()); } Pt2di P_00 (0,0); Pt2di P_10 (sz.x,0); Pt2di P_01 (0,sz.y); ElList<Pt2di> lP1; lP1 = lP1 + P_00; lP1 = lP1 + P_01; lP1 = lP1 + P_10; ModifCSte(polygone(lP1),7); verif(im1.all_pts()); TiffVerif(); }