Changeset 12225
- Timestamp:
- 05/08/12 11:10:50 (13 years ago)
- Location:
- issm/trunk-jpl/src/c
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/Container/Observations.cpp
r12213 r12225 35 35 Observations::Observations(double* observations_list,double* x,double* y,int n){ 36 36 37 const int MaxICoor = 1073741823; // 2^30-1 =111...111 (29 times)38 39 37 /*Intermediaries*/ 40 38 int i; 41 39 int xi,yi; 40 double xmin,xmax,ymin,ymax; 42 41 double offset; 43 42 Observation *observation = NULL; 44 43 45 /*Initialize Quadtree*/46 this->quadtree = new Quadtree();47 48 44 /*Get extrema*/ 49 this->xmin=x[0]; this->ymin=y[0];50 this->xmax=x[0]; this->ymax=y[0];45 xmin=x[0]; ymin=y[0]; 46 xmax=x[0]; ymax=y[0]; 51 47 for(i=1;i<n;i++){ 52 48 xmin=min(xmin,x[i]); ymin=min(ymin,y[i]); … … 56 52 offset=0.05*(ymax-ymin); ymin-=offset; ymax+=offset; 57 53 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); 54 /*Initialize Quadtree*/ 55 this->quadtree = new Quadtree(xmin,xmax,ymin,ymax,30); 65 56 66 57 /*Add observations one by one*/ 67 58 for(i=0;i<n;i++){ 68 xi=int(coefficient*(x[i]-xmin)); 69 yi=int(coefficient*(y[i]-ymin)); 59 this->quadtree->IntergerCoordinates(&xi,&yi,x[i],y[i]); 70 60 observation = new Observation(x[i],y[i],xi,yi,observations_list[i]); 71 61 this->quadtree->Add(observation); … … 115 105 void Observations::QuadtreeColoring(double* A,double *x,double *y,int n){ 116 106 117 /*Convert to integer coordinates*/ 118 int *xi = (int*)xmalloc(n*sizeof(int)); 119 int *yi = (int*)xmalloc(n*sizeof(int)); 107 int xi,yi; 108 120 109 for(int i=0;i<n;i++){ 121 xi[i]=int(coefficient*(x[i]-xmin));122 yi[i]=int(coefficient*(y[i]-ymin));110 this->quadtree->IntergerCoordinates(&xi,&yi,x[i],y[i]); 111 this->quadtree->QuadtreeColoring(&A[i],xi,yi); 123 112 } 124 113 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 114 }/*}}}*/ -
issm/trunk-jpl/src/c/Container/Observations.h
r12213 r12225 13 13 private: 14 14 Quadtree* quadtree; 15 double coefficient; //For integer coordinate conversion16 double xmin;17 double ymin;18 double xmax;19 double ymax;20 15 21 16 public: -
issm/trunk-jpl/src/c/matlab/io/FetchMatlabData.cpp
r12052 r12225 404 404 else if (mxIsClass(dataref,"char") ){ 405 405 406 /*Check dataref is not pointing to NaN: */ 407 if ( mxIsNaN(*(mxGetPr(dataref))) && (mxGetNumberOfElements(dataref)==1) ){ 408 outmatrix_numel=0; 409 outmatrix_ndims=0; 410 outmatrix_size =NULL; 411 outmatrix=NULL; 412 } 413 else{ 414 415 /*Convert matlab n-dim array to char* matrix: */ 416 MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref); 417 } 406 /*Convert matlab n-dim array to char* matrix: */ 407 MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref); 418 408 } 419 409 else{ -
issm/trunk-jpl/src/c/matlab/io/OptionParse.cpp
r12013 r12225 41 41 /*check and parse the name */ 42 42 ological=new OptionLogical; 43 ological->name =(char *)xmalloc((strlen(name)+1)*sizeof(char));43 ological->name =(char*)xmalloc((strlen(name)+1)*sizeof(char)); 44 44 memcpy(ological->name,name,(strlen(name)+1)*sizeof(char)); 45 45 … … 59 59 60 60 /*check and parse the name */ 61 ochar=new OptionChar ;62 ochar->name =(char *)xmalloc((strlen(name)+1)*sizeof(char));61 ochar=new OptionChar(); 62 ochar->name =(char*)xmalloc((strlen(name)+1)*sizeof(char)); 63 63 memcpy(ochar->name,name,(strlen(name)+1)*sizeof(char)); 64 64 -
issm/trunk-jpl/src/c/objects/Kriging/Quadtree.cpp
r12216 r12225 80 80 81 81 /*Constructors/Destructors*/ 82 /*FUNCTION Quadtree::Quadtree(){{{1*/ 83 Quadtree::Quadtree(){ 84 85 /*Initialize fields*/ 86 this->MaxDepth=30; 87 this->NbQuadtreeBox=0; 88 this->NbObs=0; 89 90 /*Create container*/ 91 this->boxcontainer=new DataSet(); 92 93 /*Create Root, pointer toward the main box*/ 94 this->root=NewQuadtreeBox(); 95 96 } 97 /*}}}1*/ 82 /*FUNCTION Quadtree::Quadtree(){{{1*/ 83 Quadtree::Quadtree(){ 84 _error_("Constructor not supported"); 85 86 } 87 /*}}}1*/ 88 /*FUNCTION Quadtree::Quadtree(double xmin,double xmax,double ymin,double ymax,int maxdepth){{{1*/ 89 Quadtree::Quadtree(double xmin,double xmax,double ymin,double ymax,int maxdepth){ 90 91 /*Intermediaries*/ 92 double length; 93 94 /*Initialize fields*/ 95 this->MaxDepth=maxdepth; 96 this->NbQuadtreeBox=0; 97 this->NbObs=0; 98 99 /*Create container*/ 100 this->boxcontainer=new DataSet(); 101 102 /*Create Root, pointer toward the main box*/ 103 length=max(xmax-xmin,ymax-ymin); 104 this->root=NewQuadtreeBox(xmin+length/2,ymin+length/2,length); 105 } 106 /*}}}1*/ 98 107 /*FUNCTION Quadtree::~Quadtree(){{{1*/ 99 108 Quadtree::~Quadtree(){ … … 110 119 111 120 /*Intermediaries*/ 112 int xi,yi,ij,level; 113 QuadtreeBox **pb = NULL; 114 QuadtreeBox *b = NULL; 115 QuadtreeBox *bb = NULL; 121 int xi,yi,ij,level,levelbin; 122 QuadtreeBox **pbox = NULL; // pointer toward current box b 123 QuadtreeBox **pmaster = NULL; // pointer toward master of b 124 QuadtreeBox *box = NULL; // current box b 125 QuadtreeBox *slave = NULL; // suslaveox of b (if necessary) 116 126 Observation *obs[4]; 117 127 … … 120 130 yi = observation->yi; 121 131 122 /*Initialize level*/ 123 level=(1L<<this->MaxDepth);// = 2^30 132 /*Initialize levels*/ 133 level = 0; 134 levelbin = (1L<<this->MaxDepth);// = 2^30 124 135 125 136 /*Get inital box (the largest)*/ 126 pb=&root; 137 pmaster = &root; 138 pbox = &root; 127 139 128 140 /*Find the smallest box where the observation is located*/ 129 while((b=*pb) && (b->nbitems<0)){ 130 131 /*Go down one level = 00100 -> 00010*/ 132 level>>=1; 133 134 /*Get next subbox according to the bit value (level)*/ 135 pb = &b->box[IJ(xi,yi,level)]; 136 } 137 _assert_(level>0); 141 while((box=*pbox) && (box->nbitems<0)){ 142 143 /*Go down one level (levelbin = 00100 -> 00010)*/ 144 levelbin>>=1; level+=1; _assert_(level<this->MaxDepth); 145 146 /*Get next box according to the bit value (levelbin)*/ 147 pmaster = pbox; 148 pbox = &box->box[IJ(xi,yi,levelbin)]; 149 } 150 _assert_(levelbin>0); 138 151 139 152 /*Now, try to add the vertex, if the box is full (nbitems=4), we have to divide it in 4 new boxes*/ 140 while((b =*pb) && (b->nbitems==4)){153 while((box=*pbox) && (box->nbitems==4)){ 141 154 142 155 /*Copy the 4 observation in the current Quadtreebox*/ 143 obs[0] = b->obs[0]; 144 obs[1] = b->obs[1]; 145 obs[2] = b->obs[2]; 146 obs[3] = b->obs[3]; 147 148 /*set nbitems as negative (now holding boxes instead of observations)*/ 149 b->nbitems = -b->nbitems; 150 151 /*Initialize the 4 pointers toward the 4 subboxes*/ 152 b->box[0]=NULL; 153 b->box[1]=NULL; 154 b->box[2]=NULL; 155 b->box[3]=NULL; 156 157 /*level = 00100 -> 00010*/ 158 level>>=1; 156 obs[0] = box->obs[0]; 157 obs[1] = box->obs[1]; 158 obs[2] = box->obs[2]; 159 obs[3] = box->obs[3]; 160 161 /*set nbitems as -1 (now holding boxes instead of observations)*/ 162 box->nbitems = -1; 163 box->box[0] = NULL; 164 box->box[1] = NULL; 165 box->box[2] = NULL; 166 box->box[3] = NULL; 167 168 /*Go down one level (levelbin = 00010 -> 00001)*/ 169 levelbin>>=1; level+=1; _assert_(level<this->MaxDepth); 159 170 160 171 /*Put the four observations in the new boxes*/ … … 162 173 163 174 /*Get box for observation number k*/ 164 ij = IJ(obs[k]->xi,obs[k]->yi,level);165 bb = b->box[ij];166 if(! bb){167 b ->box[ij]=NewQuadtreeBox();168 bb=b->box[ij];175 ij = IJ(obs[k]->xi,obs[k]->yi,levelbin); 176 slave = box->box[ij]; 177 if(!slave){ 178 box->box[ij] = NewQuadtreeBox(box,ij); 179 slave = box->box[ij]; 169 180 } 170 bb->obs[bb->nbitems++] = obs[k];181 slave->obs[slave->nbitems++] = obs[k]; 171 182 } 172 183 173 /*Get the subbox where the current observation is located*/ 174 ij=IJ(xi,yi,level); 175 pb=&b->box[ij]; 184 /*Get the suslaveox where the current observation is located*/ 185 ij = IJ(xi,yi,levelbin); 186 pmaster = pbox; 187 pbox = &box->box[ij]; 176 188 } 177 189 178 190 /*alloc the QuadtreeBox if necessary and add current observation*/ 179 b=*pb; 180 if(!b){ 181 b=*pb=NewQuadtreeBox(); 182 } 183 b->obs[b->nbitems++]=observation; 191 box = *pbox; 192 if(!box){ 193 ij = IJ(xi,yi,levelbin); 194 box = *pbox = NewQuadtreeBox(*pmaster,ij); 195 } 196 box->obs[box->nbitems++]=observation; 184 197 NbObs++; 185 198 … … 189 202 190 203 printf("Quadtree:\n"); 191 printf(" MaxDepth = %i\n",MaxDepth); 192 printf(" NbQuadtreeBox = %i\n",NbQuadtreeBox); 193 printf(" NbObs = %i\n",NbObs); 194 printf(" root = %p\n",root); 195 196 }/*}}}*/ 197 /*FUNCTION Quadtree::NewQuadtreeBox {{{1*/ 198 Quadtree::QuadtreeBox* Quadtree::NewQuadtreeBox(void){ 204 printf(" MaxDepth = %i\n",this->MaxDepth); 205 printf(" NbQuadtreeBox = %i\n",this->NbQuadtreeBox); 206 printf(" NbObs = %i\n",this->NbObs); 207 printf(" root = %p\n",this->root); 208 209 }/*}}}*/ 210 /*FUNCTION Quadtree::DeepEcho{{{1*/ 211 void Quadtree::DeepEcho(void){ 212 213 printf("Quadtree:\n"); 214 printf(" MaxDepth = %i\n",this->MaxDepth); 215 printf(" NbQuadtreeBox = %i\n",this->NbQuadtreeBox); 216 printf(" NbObs = %i\n",this->NbObs); 217 printf(" root = %p\n",this->root); 218 boxcontainer->Echo(); 219 220 }/*}}}*/ 221 /*FUNCTION Quadtree::IntergerCoordinates{{{1*/ 222 void Quadtree::IntergerCoordinates(int *xi,int *yi,double x,double y){ 223 224 /*Intermediaries*/ 225 double coefficient; 226 double xmin,ymin; 227 228 /*Checks in debugging mode*/ 229 _assert_(xi && yi); 230 _assert_(this->root); 231 232 /*coeffIcoor is the coefficient used for integer coordinates: 233 * (x-xmin) 234 * xi = (2^30 -1) --------- 235 * length 236 * coefficient = (2^30 -1)/length 237 */ 238 coefficient = double((1L<<this->MaxDepth)-1)/(this->root->length); 239 xmin = this->root->xcenter - this->root->length/2; 240 ymin = this->root->ycenter - this->root->length/2; 241 242 *xi=int(coefficient*(x - xmin)); 243 *yi=int(coefficient*(y - ymin)); 244 }/*}}}*/ 245 /*FUNCTION Quadtree::NewQuadtreeBox(double xcenter,double ycenter,double length){{{1*/ 246 Quadtree::QuadtreeBox* Quadtree::NewQuadtreeBox(double xcenter,double ycenter,double length){ 199 247 200 248 /*Output*/ 201 249 QuadtreeBox* newbox=NULL; 250 251 /*Create and initialize a new box*/ 252 newbox=new QuadtreeBox(); 253 newbox->nbitems=0; 254 newbox->xcenter=xcenter; 255 newbox->ycenter=ycenter; 256 newbox->length=length; 257 newbox->box[0]=NULL; 258 newbox->box[1]=NULL; 259 newbox->box[2]=NULL; 260 newbox->box[3]=NULL; 261 262 /*Add to container*/ 263 this->boxcontainer->AddObject(newbox); 264 NbQuadtreeBox++; 265 266 /*currentbox now points toward next quadtree box*/ 267 return newbox; 268 }/*}}}*/ 269 /*FUNCTION Quadtree::NewQuadtreeBox(QuadtreeBox* master,int index) {{{1*/ 270 Quadtree::QuadtreeBox* Quadtree::NewQuadtreeBox(QuadtreeBox* master,int index){ 271 272 /*Output*/ 273 QuadtreeBox* newbox=NULL; 274 275 /*Checks in debugging mode*/ 276 _assert_(master); 202 277 203 278 /*Create and initialize a new box*/ … … 208 283 newbox->box[2]=NULL; 209 284 newbox->box[3]=NULL; 285 switch(index){ 286 case 0: 287 newbox->xcenter=master->xcenter - master->length/4; 288 newbox->ycenter=master->ycenter - master->length/4; 289 break; 290 case 1: 291 newbox->xcenter=master->xcenter + master->length/4; 292 newbox->ycenter=master->ycenter - master->length/4; 293 break; 294 case 2: 295 newbox->xcenter=master->xcenter - master->length/4; 296 newbox->ycenter=master->ycenter + master->length/4; 297 break; 298 case 3: 299 newbox->xcenter=master->xcenter + master->length/4; 300 newbox->ycenter=master->ycenter + master->length/4; 301 break; 302 default: 303 _error_("Case %i not supported",index); 304 } 305 newbox->length=master->length/2; 210 306 211 307 /*Add to container*/ … … 217 313 }/*}}}*/ 218 314 /*FUNCTION Quadtree::QuadtreeColoring{{{1*/ 219 void Quadtree::QuadtreeColoring(double* A,int *xi,int *yi,int n){ 220 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<<this->MaxDepth);// = 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 }/*}}}*/ 315 void Quadtree::QuadtreeColoring(double* A,int xi,int yi){ 316 317 QuadtreeBox **pbox = NULL; 318 QuadtreeBox *box = NULL; 319 int level,levelbin; 320 321 /*Initialize levels*/ 322 level = 0; 323 levelbin = (1L<<this->MaxDepth);// = 2^30 324 325 /*Get inital box (the largest)*/ 326 pbox=&root; 327 328 /*Find the smallest box where this point is located*/ 329 while((box=*pbox) && (box->nbitems<0)){ 330 331 levelbin>>=1; level+=1; _assert_(level<this->MaxDepth); 332 333 pbox = &box->box[IJ(xi,yi,levelbin)]; 334 } 335 if(box && box->nbitems>0){ 336 /*This box is not empty, add one level*/ 337 level+=1; 338 } 339 340 *A=level; 341 }/*}}}*/ 342 343 /*QuadtreeBox methos*/ 344 /*FUNCTION QuadtreeBox::Echo{{{1*/ 345 void Quadtree::QuadtreeBox::Echo(void){ 346 347 printf("QuadtreeBox:\n"); 348 printf(" nbitems = %i\n",this->nbitems); 349 printf(" xcenter = %g\n",this->xcenter); 350 printf(" ycenter = %g\n",this->ycenter); 351 printf(" length = %g\n",this->length); 352 353 }/*}}}*/ -
issm/trunk-jpl/src/c/objects/Kriging/Quadtree.h
r12216 r12225 16 16 class QuadtreeBox: public Object{ 17 17 public: 18 int nbitems; // number of current vertices in the box 18 int nbitems; // number of current vertices in the box 19 double xcenter; // x position of the center (double) 20 double ycenter; // x position of the center (double) 21 double length; // width of the box 19 22 union{ 20 23 QuadtreeBox *box[4]; … … 23 26 24 27 /*Object functions (Needed because the Quadtree uses a Container*/ 25 void Echo() {_error_("not implemented yet"); };28 void Echo(); 26 29 void DeepEcho() {_error_("not implemented yet"); }; 27 30 int Id() {_error_("not implemented yet"); }; … … 35 38 36 39 public: 37 int MaxDepth; // maximum number of subdivision 38 QuadtreeBox *root; // main box 39 long NbQuadtreeBox; // total number of boxes 40 long NbObs; // number of points 40 int MaxDepth; // maximum number of subdivision 41 double coordconversion; // Coefficient to convert coordinates to integer 42 QuadtreeBox *root; // main box 43 long NbQuadtreeBox; // total number of boxes 44 long NbObs; // number of points 41 45 42 46 Quadtree(); 47 Quadtree(double xmin,double xmax,double ymin,double ymax,int maxdepth_in); 43 48 ~Quadtree(); 49 void Add(Observation *observation); 50 void DeepEcho(void); 44 51 void Echo(void); 45 void Add(Observation *observation); 46 void QuadtreeColoring(double *A,int *xi,int *yi,int n); 47 QuadtreeBox *NewQuadtreeBox(void); 52 void IntergerCoordinates(int *xi,int *yi,double x,double y); 53 QuadtreeBox *NewQuadtreeBox(double xcenter,double ycenter,double length); 54 QuadtreeBox *NewQuadtreeBox(QuadtreeBox* master,int index); 55 void QuadtreeColoring(double *A,int xi,int yi); 48 56 }; 49 57 #endif //_QUADTREEK_H
Note:
See TracChangeset
for help on using the changeset viewer.