Changeset 12213


Ignore:
Timestamp:
05/04/12 16:53:41 (13 years ago)
Author:
Mathieu Morlighem
Message:

Fixed quadtree

Location:
issm/trunk-jpl/src/c
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/Container/Observations.cpp

    r12210 r12213  
    2828/*FUNCTION Observations::Observations(){{{*/
    2929Observations::Observations(){
     30        this->quadtree = NULL;
    3031        return;
    3132}
     
    3435Observations::Observations(double* observations_list,double* x,double* y,int n){
    3536
     37        const int MaxICoor = 1073741823; // 2^30-1 =111...111 (29 times)
     38
     39        /*Intermediaries*/
     40        int          i;
     41        int          xi,yi;
     42        double       offset;
     43        Observation *observation = NULL;
     44
     45        /*Initialize Quadtree*/
     46        this->quadtree = new Quadtree();
     47
     48        /*Get extrema*/
     49        this->xmin=x[0]; this->ymin=y[0];
     50        this->xmax=x[0]; this->ymax=y[0];
     51        for(i=1;i<n;i++){
     52                xmin=min(xmin,x[i]); ymin=min(ymin,y[i]);
     53                xmax=max(xmax,x[i]); ymax=max(ymax,y[i]);
     54        }
     55        offset=0.05*(xmax-xmin); xmin-=offset; xmax+=offset;
     56        offset=0.05*(ymax-ymin); ymin-=offset; ymax+=offset;
     57
     58        /*coeffIcoor is the coefficient used for integer coordinates:
     59         *                (x-xmin)
     60         * xi = (2^30 -1) ------------
     61         *                   D
     62         * coefficient = (2^30 -1)/D
     63         */
     64        this->coefficient = double(MaxICoor)/max(xmax-xmin,ymax-ymin); _assert_(coefficient>=0);
     65
    3666        /*Add observations one by one*/
    37         for(int i=0;i<n;i++){
    38                 this->AddObject(new Observation(x[i],y[i],0,0,observations_list[i]));
     67        for(i=0;i<n;i++){
     68                xi=int(coefficient*(x[i]-xmin));
     69                yi=int(coefficient*(y[i]-ymin));
     70                observation = new Observation(x[i],y[i],xi,yi,observations_list[i]);
     71                this->quadtree->Add(observation);
     72                this->AddObject(observation);
    3973        }
    40 
    4174}
    4275/*}}}*/
    4376/*FUNCTION Observations::~Observations(){{{*/
    4477Observations::~Observations(){
     78        delete quadtree;
    4579        return;
    4680}
     
    4983/*Methods*/
    5084/*FUNCTION Observations::ObservationList{{{*/
    51 void Observations::ObservationList(double **px,double **py,double **pobs,int* pnobs,double x_interp,double y_interp){/*{{{*/
     85void Observations::ObservationList(double **px,double **py,double **pobs,int* pnobs,double x_interp,double y_interp){
    5286
    5387        /*Output and Intermediaries*/
     
    78112        *pnobs=nobs;
    79113}/*}}}*/
     114/*FUNCTION Observations::QuadtreeColoring{{{*/
     115void Observations::QuadtreeColoring(double* A,double *x,double *y,int n){
     116
     117        /*Convert to integer coordinates*/
     118        int *xi = (int*)xmalloc(n*sizeof(int));
     119        int *yi = (int*)xmalloc(n*sizeof(int));
     120        for(int i=0;i<n;i++){
     121                xi[i]=int(coefficient*(x[i]-xmin));
     122                yi[i]=int(coefficient*(y[i]-ymin));
     123        }
     124
     125        /*Call quadtree method*/
     126        this->quadtree->QuadtreeColoring(A,xi,yi,n);
     127
     128        /*clean-up*/
     129        xfree((void**)&xi);
     130        xfree((void**)&yi);
     131
     132}/*}}}*/
  • issm/trunk-jpl/src/c/Container/Observations.h

    r12210 r12213  
    77
    88class Obsevration;
     9class Quadtree;
    910
    1011class Observations: public DataSet{
     12
     13        private:
     14                Quadtree* quadtree;
     15                double    coefficient; //For integer coordinate conversion
     16                double    xmin;
     17                double    ymin;
     18                double    xmax;
     19                double    ymax;
    1120
    1221        public:
     
    1928                /*Methods*/
    2029                void ObservationList(double **px,double **py,double **pobs,int* pnobs,double x_interp,double y_interp);
     30                void QuadtreeColoring(double* A,double *x,double *y,int n);
    2131
    2232};
  • issm/trunk-jpl/src/c/Container/Vertices.h

    r10522 r12213  
    3232};
    3333
    34 
    35 
    3634#endif //ifndef _VERTICES_H_
    37 
  • issm/trunk-jpl/src/c/modules/Krigingx/Krigingx.cpp

    r12210 r12213  
    4444        /*Allocation output*/
    4545        predictions =(double*)xmalloc(n_interp*sizeof(double));
     46
     47        for(i=0;i<n_interp;i++) predictions[i]=0;
     48        observations->QuadtreeColoring(predictions,x_interp,y_interp,n_interp);
     49        *ppredictions=predictions;
     50        return 1;
    4651
    4752        /*Loop over all interpolations*/
     
    101106        delete observations;
    102107        *ppredictions=predictions;
     108        return 1;
    103109}
    104110
  • issm/trunk-jpl/src/c/objects/Kriging/Observation.cpp

    r12207 r12213  
    33 */
    44
     5#include <stdlib.h>
    56#include "../objects.h"
    67
     
    3132/*FUNCTION Observation::Echo {{{1*/
    3233void Observation::Echo(void){
     34
     35        int  bit;
     36
    3337        printf("Observation\n");
    3438        printf("   x     : %g\n",this->x);
    3539        printf("   y     : %g\n",this->y);
    36         printf("   xi    : %i\n",this->xi);
    37         printf("   yi    : %i\n",this->yi);
     40        printf("   xi    : "); printbinary(this->xi); printf("\n");
     41        printf("   yi    : "); printbinary(this->yi); printf("\n");
    3842        printf("   value : %g\n",this->value);
    3943}
  • issm/trunk-jpl/src/c/objects/Kriging/Quadtree.cpp

    r12210 r12213  
    110110        /*Intermediaries*/
    111111        const int     MaxDeep  = 30;
    112         int           k,ij;
    113         long          i,j,level;
     112        int          xi,yi,ij,level;
    114113        QuadtreeBox **pb = NULL;
    115114        QuadtreeBox  *b  = NULL;
     115        QuadtreeBox  *bb = NULL;
     116        Observation  *obs[4];
    116117
    117118        /*Get integer coodinates*/
    118         i = observation->xi;
    119         j = observation->yi;
     119        xi = observation->xi;
     120        yi = observation->yi;
    120121
    121122        /*Initialize level*/
     
    128129        while((b=*pb) && (b->nbitems<0)){
    129130
    130                 /*shift b->nbitems by -1 as a counter*/
    131                 b->nbitems--;
    132 
    133131                /*Go down one level = 00100 -> 00010*/
    134132                level>>=1;
    135133
    136134                /*Get next subbox according to the bit value (level)*/
    137                 pb = &b->box[IJ(i,j,level)];
     135                pb = &b->box[IJ(xi,yi,level)];
    138136        }
    139137        _assert_(level>0);
     
    143141
    144142                /*Copy the 4 observation in the current Quadtreebox*/
    145                 Observation* obs[4];
    146143                obs[0] = b->obs[0];
    147144                obs[1] = b->obs[1];
     
    162159
    163160                /*Put the four observations in the new boxes*/
    164                 for (k=0;k<4;k++){
     161                for (int k=0;k<4;k++){
    165162
    166163                        /*Get box for observation number k*/
    167                         ij=IJ(obs[k]->xi,obs[k]->yi,level);
    168                         QuadtreeBox *bb = b->box[ij];
     164                        ij = IJ(obs[k]->xi,obs[k]->yi,level);
     165                        bb = b->box[ij];
    169166                        if(!bb){
    170167                                b->box[ij]=NewQuadtreeBox();
    171168                                bb=b->box[ij];
    172169                        }
    173 
    174                         /*Copy current observations*/
    175170                        bb->obs[bb->nbitems++] = obs[k];
    176171                }
    177172
    178173                /*Get the subbox where the current observation is located*/
    179                 ij=IJ(i,j,level);
     174                ij=IJ(xi,yi,level);
    180175                pb=&b->box[ij];
    181176        }
     
    183178        /*alloc the QuadtreeBox if necessary and add current observation*/
    184179        b=*pb;
    185         if(!b) b=NewQuadtreeBox();
     180        if(!b){
     181                b=*pb=NewQuadtreeBox();
     182        }
    186183        b->obs[b->nbitems++]=observation;
    187184        NbObs++;
    188185
    189186}/*}}}*/
    190         /*FUNCTION Quadtree::NewQuadtreeBox {{{1*/
    191         Quadtree::QuadtreeBox* Quadtree::NewQuadtreeBox(void){
    192 
    193                 /*Output*/
    194                 QuadtreeBox* newbox=NULL;
    195 
    196                 /*Create and initialize a new box*/
    197                 newbox=new QuadtreeBox();
    198                 newbox->nbitems=0;
    199                 newbox->box[0]=NULL;
    200                 newbox->box[1]=NULL;
    201                 newbox->box[2]=NULL;
    202                 newbox->box[3]=NULL;
    203 
    204                 /*Add root to the container*/
    205                 boxcontainer->AddObject(newbox);
    206 
    207                 /*Increase counter*/
    208                 NbQuadtreeBox++;
    209 
    210                 /*currentbox now points toward next quadtree box*/
    211                 return newbox;
    212         }/*}}}*/
     187/*FUNCTION Quadtree::Echo{{{1*/
     188void  Quadtree::Echo(void){
     189
     190        printf("Quadtree:\n");
     191        printf("   NbQuadtreeBox = %i\n",NbQuadtreeBox);
     192        printf("   NbObs         = %i\n",NbObs);
     193        printf("   root          = %p\n",root);
     194
     195}/*}}}*/
     196/*FUNCTION Quadtree::NewQuadtreeBox {{{1*/
     197Quadtree::QuadtreeBox* Quadtree::NewQuadtreeBox(void){
     198
     199        /*Output*/
     200        QuadtreeBox* newbox=NULL;
     201
     202        /*Create and initialize a new box*/
     203        newbox=new QuadtreeBox();
     204        newbox->nbitems=0;
     205        newbox->box[0]=NULL;
     206        newbox->box[1]=NULL;
     207        newbox->box[2]=NULL;
     208        newbox->box[3]=NULL;
     209
     210        /*Add to container*/
     211        this->boxcontainer->AddObject(newbox);
     212        NbQuadtreeBox++;
     213
     214        /*currentbox now points toward next quadtree box*/
     215        return newbox;
     216}/*}}}*/
     217/*FUNCTION Quadtree::QuadtreeColoring{{{1*/
     218void Quadtree::QuadtreeColoring(double* A,int *xi,int *yi,int n){
     219
     220        const int     MaxDeep  = 30;
     221        QuadtreeBox **pb = NULL;
     222        QuadtreeBox  *b  = NULL;
     223        int          level;
     224
     225        for(int i=0;i<n;i++){
     226
     227                /*Initialize level*/
     228                level=(1L<<MaxDeep);// = 2^30
     229
     230                /*Get inital box (the largest)*/
     231                pb=&root;
     232
     233                /*Find the smallest box where the observation is located*/
     234                while((b=*pb) && (b->nbitems<0)){
     235
     236                        /*Color matrix onces more*/
     237                        A[i]+=1;
     238
     239                        /*Go to one box deeper*/
     240                        level>>=1;
     241                        pb = &b->box[IJ(xi[i],yi[i],level)];
     242                }
     243        }
     244}/*}}}*/
  • issm/trunk-jpl/src/c/objects/Kriging/Quadtree.h

    r12210 r12213  
    4141                Quadtree();
    4242                ~Quadtree();
    43                 void Add(Observation* observation);
    44                 QuadtreeBox* NewQuadtreeBox(void);
     43                void         Echo(void);
     44                void         Add(Observation *observation);
     45                void         QuadtreeColoring(double *A,int *xi,int *yi,int n);
     46                QuadtreeBox *NewQuadtreeBox(void);
    4547};
    4648#endif //_QUADTREEK_H
  • issm/trunk-jpl/src/c/shared/Elements/elements.h

    r11656 r12213  
    4747        printf("\n");
    4848}
     49inline void printbinary(int n) {
     50        unsigned int i;
     51        i=1<<(sizeof(n)*8-1);
     52
     53        while (i>0) {
     54                if (n&i)
     55                 printf("1");
     56                else
     57                 printf("0");
     58                i>>=1;
     59        }
     60}
    4961
    5062#endif //ifndef _SHARED_ELEMENTS_H_
Note: See TracChangeset for help on using the changeset viewer.