void Discontinuity::createKDTreePacked () { Constants con; std::cout << "Creating KDTree ( crust ).\n"; crustTree = kd_create (3); int l = 0; KDdatCrust = new int [crust_col_rad[0].size()*crust_lon_rad[0].size()](); crust_col_deg_unpack.reserve ( crust_col_rad[0].size()* crust_lon_rad[0].size() ); crust_lon_deg_unpack.reserve ( crust_col_rad[0].size()* crust_lon_rad[0].size() ); for ( size_t r=0; r!=crust_col_rad.size(); r++ ) { for ( size_t i=0; i!=crust_col_rad[r].size(); i++ ) { for ( size_t j=0; j!=crust_lon_rad[r].size(); j++ ) { KDdatCrust[l] = l; kd_insert3 ( crustTree, crust_col_deg[r][i], crust_lon_deg[r][j], con.R_EARTH, &KDdatCrust[l] ); crust_col_deg_unpack[l] = crust_col_deg[r][i]; crust_lon_deg_unpack[l] = crust_lon_deg[r][j]; l++; } } } }
int main(int argc, char **argv) { int i, vcount = 1000000; void *kd, *set; unsigned int msec, start; if (argc > 1 && isdigit(argv[1][0])) { vcount = atoi(argv[1]); } printf("inserting %d random vectors... ", vcount); fflush(stdout); kd = kd_create(3); start = get_msec(); for (i = 0; i < vcount; i++) { float x, y, z; x = ((float) rand() / RAND_MAX) * 200.0 - 100.0; y = ((float) rand() / RAND_MAX) * 200.0 - 100.0; z = ((float) rand() / RAND_MAX) * 200.0 - 100.0; assert(kd_insert3(kd, x, y, z, 0) == 0); } msec = get_msec() - start; printf("%.3f sec\n", (float) msec / 1000.0); start = get_msec(); set = kd_nearest_range3(kd, 0, 0, 0, 40); msec = get_msec() - start; printf("range query returned %d items in %.5f sec\n", kd_res_size(set), (float) msec / 1000.0); kd_res_free(set); kd_free(kd); return 0; }
int main(int argc, char **argv) { int i, num_pts = DEF_NUM_PTS; void *ptree; char *data, *pch; struct kdres *presults; double pos[3], dist; double pt[3] = { 0, 0, 1 }; double radius = 10; if(argc > 1 && isdigit(argv[1][0])) { num_pts = atoi(argv[1]); } if(!(data = malloc(num_pts))) { perror("malloc failed"); return 1; } srand( time(0) ); /* create a k-d tree for 3-dimensional points */ ptree = kd_create( 3 ); /* add some random nodes to the tree (assert nodes are successfully inserted) */ for( i=0; i<num_pts; i++ ) { data[i] = 'a' + i; assert( 0 == kd_insert3( ptree, rd(), rd(), rd(), &data[i] ) ); } /* find points closest to the origin and within distance radius */ presults = kd_nearest_range( ptree, pt, radius ); /* print out all the points found in results */ printf( "found %d results:\n", kd_res_size(presults) ); while( !kd_res_end( presults ) ) { /* get the data and position of the current result item */ pch = (char*)kd_res_item( presults, pos ); /* compute the distance of the current result from the pt */ dist = sqrt( dist_sq( pt, pos, 3 ) ); /* print out the retrieved data */ printf( "node at (%.3f, %.3f, %.3f) is %.3f away and has data=%c\n", pos[0], pos[1], pos[2], dist, *pch ); /* go to the next entry */ kd_res_next( presults ); } /* free our tree, results set, and other allocated memory */ free( data ); kd_res_free( presults ); kd_free( ptree ); return 0; }
void updateBoids() { unsigned long j; kd_clear(k3); for( j=0;j<simParameters.numberOfBoids;j++) { copyVector(&(boidSet[j].nextPosition), &(boidSet[j].currentPosition)); copyVector(&(boidSet[j].nextVelocity), &(boidSet[j].currentVelocity)); // kdtree update kd_insert3(k3,boidSet[j].currentPosition.x, boidSet[j].currentPosition.y, boidSet[j].currentPosition.z, &boidSet[j]); } }
void simulationInit(SimulationParameters *simParams, RulesParameters *applyingRules,InfoCache *cache, int numberOfDesires) { int j; unsigned long i; Vector acc,vel; firstTime=TRUE; abortSimulation=FALSE; nDesires=numberOfDesires; dt=1/(double)(simParams->fps); simulationProgress = 0; leader=NULL; memcpy(&cacheFileOption,cache,sizeof(InfoCache)); rParameters=(RulesParameters**)malloc(nDesires*sizeof(RulesParameters*)); for(j=0;j<nDesires;j++) { rParameters[j]=(RulesParameters*)malloc(sizeof(RulesParameters)); memcpy(rParameters[j],&applyingRules[j],sizeof(RulesParameters)); } memcpy(&simParameters,simParams,sizeof(SimulationParameters)); actions=(Action*)malloc(sizeof(Action)*nDesires); boidSet=(Boid *)malloc(sizeof(Boid)*simParameters.numberOfBoids); if((nDesires>3) && (applyingRules[FOLLOWRULE].enabled)) { leader=(Boid*)malloc(sizeof(Boid)*(unsigned)(simParameters.fps*simParameters.lenght)); leaderInit(20); } randomVector(&acc, simParams->maxAcceleration, 0, 0); randomVector(&vel, simParams->maxVelocity, 0, 0); k3=kd_create(3); for(i=0; i<simParameters.numberOfBoids; i++) { boidInitialization(&boidSet[i],i+1, 20, 10, 5, &vel, &acc); kd_insert3(k3,boidSet[i].currentPosition.x, boidSet[i].currentPosition.y, boidSet[i].currentPosition.z, &boidSet[i]); } initDesires(leader); //sortingRules(); }
void Discontinuity::createKDTreeUnpacked ( ) { Constants con; std::cout << "Creating KDTree ( model ).\n"; elvTree = kd_create (3); KDdatElv = new int [ lonElv.size() ]; #pragma omp parallel for for ( size_t i=0; i<lonElv.size(); i++ ) { KDdatElv[i] = i; { kd_insert3 ( elvTree, colElv[i], lonElv[i], con.R_EARTH, &KDdatElv[i] ); } } }
int main(int argc, char **argv) { int i,j, vcount = 10; int tot_count = 101; void *kd[tot_count], *set[tot_count]; unsigned int msec, start; /* if(argc > 1 && isdigit(argv[1][0])) { vcount = atoi(argv[1]); } */ printf("inserting random vectors... "); /* Creating Trees correspinding to each age group */ for (i=0;i<tot_count;i++) kd[i]=kd_create(3); start = get_msec(); /* Reading Input File */ FILE *file; char *line = NULL; size_t len = 0; char read; file=fopen(argv[1], "r"); if (file == NULL) return 1; int a; double b; double c; double d; /* Inseting the person's co-ordinates and age in the respective trees */ while ((read = getline(&line, &len, file)) != -1) { convert(line, &a, &b, &c, &d); assert(kd_insert3(kd[a], b, c, d, 0) == 0); } if (line) free(line); msec = get_msec() - start; printf("%.3f sec\n", (float)msec / 1000.0); start = get_msec(); double lat, longitude, latitude; /** printf("range query returned %d items in %.5f sec\n", kd_res_size(set[0]), (float)msec / 1000.0); **/ int reqd_dist,index , ind; while(1){ printf("Enter How many top places you want\n"); scanf("%d",&reqd_dist); int counter = 0; int initial_radius = 100; printf("Enter the age of the person\n"); scanf("%d",&index); printf("Enter the co-ordinates of a person \n"); scanf("%f %f",&longitude,&latitude); double a_x, a_y,a_z; coordinates_to_lat(longitude, latitude, &a_x,&a_y,&a_z); int starting_index[101] ={0}; /* Copmuter Science */ int loop_break = 0; int end_index = index + sqrt(reqd_dist); if(end_index >= 101) end_index = 100; while(counter <= reqd_dist){ for (i=index ; i<= end_index ; i++){ set[i] = kd_nearest_range3(kd[i], a_x, a_y,a_z, initial_radius); /*printf("After %d\n",kd_res_size(set[i])); */ } /* TODO :: change sqrt(reqd_dist) to something else...... */ for(ind = index; ind <= end_index; ind++){ double x = 1, y = 1, z = 1; a = 1; while(starting_index[ind]){ kd_res_next(set[ind]); starting_index[ind]-=1; } while(a){ kd_res_item3(set[ind], &x, &y, &z); /*printf("%f %f %f\n",x,y,z);*/ lat_to_coordinates(x, y, z, &lat, &longitude); printf("%d %0.2f %0.2f\n",ind,lat, longitude); counter += 1; int val = kd_res_end(set[ind]); if(val == 1){ a = 0; break; } else{ a = kd_res_next(set[ind]); /*printf("%d\n",a);*/ } if(counter >= reqd_dist){ loop_break = 1; break; } } if(loop_break ==1) break; } for (i=index ; i<= end_index;i++){ starting_index[i] = kd_res_size(set[i]); } if(loop_break ==1) break; initial_radius += initial_radius; } } msec = get_msec() - start; /* for(i=1;i<tot_count;i++){ kd_res_free(set[i]); kd_free(kd[i]); } */ /** printf("range query returned %d items in %.5f sec\n", kd_res_size(set[i]), (float)msec / 1000.0); **/ /* printf("range query returned %d items in %.5f sec\n", kd_res_size(set), (float)msec / 1000.0); kd_res_free(set); kd_free(kd); */ return 0; }