void Discontinuity::deallocate ( ) { kd_free ( crustTree ); kd_free ( elvTree ); delete [] KDdatCrust; delete [] KDdatElv; }
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; }
/** * vu_finalize_lat_lon_tz_lookup: * * Clear memory used by the lookup. * only call on program exit */ void vu_finalize_lat_lon_tz_lookup () { if ( kd ) { kd_data_destructor ( kd, g_free ); kd_free ( kd ); } }
void kd_free_down(kd_t *kd) { if(kd == NULL) return; kd_free_down(kd->c[0]); kd_free_down(kd->c[1]); kd_free(kd); }
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 PhotonMap::CleanupPhotons() { if ( causticsMap ) { kd_free(causticsMap); causticsMap = NULL; } if ( indirectMap ) { kd_free(indirectMap); indirectMap = NULL; } if ( volumeMap ) { kd_free(volumeMap); volumeMap = NULL; } for ( uint32_t i = 0; i < photons.size(); i++ ) { delete photons[i]; } photons.clear(); }
// update simulation void update() { unsigned int simulationLenght,progress; progress=0; simulationLenght=(unsigned int)ceil(simParameters.fps * simParameters.lenght); _Output(&cacheFileOption); while((!abortSimulation) && (progress<=simulationLenght)) { Channel *channels; channels=(Channel*)malloc(sizeof(Channel)*info.option); // compute Boids' new positions, velocities, accelerations compute(); // data management cachingData(channels); // write data writeData(progress,channels); // update Boids properties and kdtree updateBoids(); // update the index job progress simulationProgress = ((int)(100*progress)/simulationLenght); //advance to the next frame progress++; // free channels memory freeChannel(channels); } simulationProgress=100; closeMethod(); if(abortSimulation) { printf("Simulation interrupted\n"); deleteData(); } // restoring abortSimulation flag abortSimulation=FALSE; // free resources free(boidSet); kd_free(k3); }
kd_t *kd_split(kd_t *kd, float split) { // is the split in range? if(split <= kd->v[0] || split >= kd->v[1]) // nope. return. return kd; // make split. kd_t *p = kd_new(kd->axis, kd->v[0], kd->v[1], kd->box, kd->obox[0], kd->obox[1]); p->p = kd->p; // duplicate. kd_t *dup = kd_dup(kd, p); // set axes. dup->v[0] = kd->v[1] = split; // contract down. dup = kd_contract_down(dup, dup); kd = kd_contract_down(kd, kd); // check if children are NULL. if(kd != NULL && dup != NULL) { // set children. p->c[0] = kd; p->c[1] = dup; p->c[0]->p = p; p->c[1]->p = p; // return "parent". return p; } else { // find a node to return. kd_free(p); if(kd != NULL) return kd; else if(dup != NULL) return dup; else return NULL; } }
void item_free(item *it) { size_t ntotal = ITEM_ntotal(it); unsigned int clsid; assert((it->it_flags & ITEM_LINKED) == 0); assert(it != heads[it->slabs_clsid]); assert(it != tails[it->slabs_clsid]); assert(it->refcount == 0); /* so slab size changer can tell later if item is already free or not */ clsid = it->slabs_clsid; it->slabs_clsid = 0; it->it_flags |= ITEM_SLABBED; DEBUG_REFCNT(it, 'F'); if ( it->kdtree ) { kd_free(it->kdtree); it->kdtree = 0; } slabs_free(it, ntotal, clsid); }
int main(int argc,char **argv) { unsigned int width, height,seed; double pos[2] = {0,0},target_pos[2], diff[3],min_col_diff; struct kdres *neigh; void *kd; //kd-tree int i,x,y,c,num_cells=500,random; size_t row_size;//imagemagick wants this char source_filename[50] = "lisa.png"; char dest_filename[59];//I set to 59 so user could have 49 chars + 10 in the default case of NNNNNN-sourcefilename int oflag=0,sflag=0,vflag=0,cflag=0; //imagemagick stuff MagickWand *source,*dest; PixelWand *neigh_color,*color,**pmw; PixelIterator *imw; while((c = getopt(argc,argv,"vc:n:s:r:d:")) != -1) { switch (c) { case 'v': vflag=1; break; case 'n': num_cells=atoi(optarg); break; case 's': strcpy(source_filename,optarg); break; case 'd': oflag=1; strcpy(dest_filename,optarg); break; case 'r': if((seed=atoi(optarg))!=0) sflag=1; break; case 'c': cflag=1; min_col_diff = atof(optarg); break; default: printf("Option %s not recognized and ignored.\n",c); } } if(!oflag) sprintf(dest_filename,"%d-%s",num_cells,source_filename); MagickWandGenesis(); source=NewMagickWand(); dest = NewMagickWand(); color = NewPixelWand(); neigh_color = NewPixelWand(); pmw = NewPixelWand(); MagickReadImage(source,source_filename); if (source==MagickFalse) { printf("Error reading file. Usage: vor filename\n"); return 1; } width = MagickGetImageWidth(source); height = MagickGetImageHeight(source); printf("File has width %d and height %d\n", width, height); if(!sflag) { //seed the algorithm with /dev/random if a seed wasn't specified random = open("/dev/random", 'r'); read(random, &seed, sizeof (seed)); close(random); } if(vflag) printf("seed : %d\n",seed); srand(seed); kd = kd_create(2); if(cflag) { for(i = 0; i < num_cells; i++) { pos[0]= (double)random_in_range(0,width); pos[1]= (double)random_in_range(0,height); if(pixel_compare_NN(min_col_diff,source,kd,pos,color,neigh_color)) kd_insert(kd,pos,0); } } else { for(i = 0; i < num_cells; i++) { pos[0]= (double)random_in_range(0,width); pos[1]= (double)random_in_range(0,height); kd_insert(kd,pos,0); } } MagickSetSize(dest,width,height); MagickReadImage(dest,"xc:none"); imw = NewPixelIterator(dest); for (y=0; y < height; y++) { pos[1] = y; pmw = PixelGetNextIteratorRow(imw, &row_size); //we iterate through the rows, grabbing one at a time for (x=0; x < (long) width; x++) { pos[0] =x; neigh = kd_nearest(kd,pos);//this is the query kd_res_item(neigh, target_pos);//then we pull out the result into target_pos kd_res_free(neigh);//need to free the memory used for the query MagickGetImagePixelColor(source,target_pos[0],target_pos[1],color); PixelSetColorFromWand(pmw[x],color); } PixelSyncIterator(imw);//this will write to the image (MagickWand) } if(vflag)printf("Writing to file %s.\n",dest_filename); if(MagickWriteImage(dest,dest_filename)==MagickFalse) { printf("Error writing to file %s.\n",dest_filename); } source=DestroyMagickWand(source); dest=DestroyMagickWand(dest); MagickWandTerminus(); kd_free(kd); return 0; }
/** * @function ~B1RRT() * @brief Destructor */ B1RRT::~B1RRT() { kd_free( kdTree ); }