/** * \brief Handles user interaction with display image window to bring image on region of mouse click into "selected" window view. */ void onTileSelect(int event, int x, int y, int flags, void* param) { cvNamedWindow("Selected", CV_WINDOW_NORMAL); if(event == CV_EVENT_LBUTTONDOWN) { int i = (frame->width)/3; int j = (frame->height)/3; isUpdating = 1; selectedSubImage=0; if(x < i && y < j) { selectedSubImage=0; printf("Selected 1\n"); } else if(x > i && x < 2*i && y < j) selectedSubImage = 1; else if(x > 2*i && y < j) selectedSubImage = 2; else if(x < i && y > j && y < 2*j) selectedSubImage = 3; else if(x > i && x < 2*i && y > j && y < 2*j) selectedSubImage = 4; else if(x > 2*i && y > j && y < 2*j) selectedSubImage = 5; else if(x < i && y > 2*j) selectedSubImage = 6; else if(x > i && x < 2*j && y > 2*j) selectedSubImage = 7; else if(x > 2*i && y > 2*j) selectedSubImage = 8; else isUpdating = 0; int x1, y1, x2, y2; find_intersections(slope[0], yIntercept[0], &x1, &y1, &x2, &y2); cvLine(args[selectedSubImage], cvPoint(x1, y1), cvPoint(x2, y2), cvScalar(255,0,0), 3); find_intersections(slope[1], yIntercept[1], &x1, &y1, &x2, &y2); cvLine(args[selectedSubImage], cvPoint(x1, y1), cvPoint(x2, y2), cvScalar(0,255,0), 3); if (selectedSubImage >= 0) cvShowImage("Selected", args[selectedSubImage]); updateTile = args[selectedSubImage]; updateSelectedTile(); } }
static void calc_either_side(Crystal *cr, double incr_val, int *valid, long double *vals[3], int refine, PartialityModel pmodel) { RefList *compare; struct image *image = crystal_get_image(cr); if ( (refine != REF_DIV) ) { Crystal *cr_new; /* Crystal properties */ cr_new = new_shifted_crystal(cr, refine, -incr_val); compare = find_intersections(image, cr_new, pmodel); scan_partialities(crystal_get_reflections(cr), compare, valid, vals, 0, pmodel); cell_free(crystal_get_cell(cr_new)); crystal_free(cr_new); reflist_free(compare); cr_new = new_shifted_crystal(cr, refine, +incr_val); compare = find_intersections(image, cr_new, pmodel); scan_partialities(crystal_get_reflections(cr), compare, valid, vals, 2, pmodel); cell_free(crystal_get_cell(cr_new)); crystal_free(cr_new); reflist_free(compare); } else { struct image im_moved; /* "Image" properties */ im_moved = *image; shift_parameter(&im_moved, refine, -incr_val); compare = find_intersections(&im_moved, cr, pmodel); scan_partialities(crystal_get_reflections(cr), compare, valid, vals, 0, pmodel); reflist_free(compare); im_moved = *image; shift_parameter(&im_moved, refine, +incr_val); compare = find_intersections(&im_moved, cr, pmodel); scan_partialities(crystal_get_reflections(cr), compare, valid, vals, 2, pmodel); reflist_free(compare); } }
void create_rays(Raycreator* rc, Ray* rays){ //Hack... Coord screen_center = rc->screen_center; Coord up = rc->up; Coord right = rc->right; Coord eye = rc->eye; real_t pixel_width = rc->pixel_width; //#ifdef USE_CUDA // launch_kernel(rays, rc, screen_center, pixel_width); //#else Color color = {-1,-1,-1,-1}; int half_res = rc->resolution/2; #pragma omp parallel for for(int c = -half_res; c < half_res; c++){ for(int d = -half_res; d < half_res; d++){ int i = (c+half_res)*rc->resolution + (d + half_res); Coord end; end.x = screen_center.x + c*pixel_width*up.x + d*pixel_width*right.x; end.y = screen_center.y + c*pixel_width*up.y + d*pixel_width*right.y; end.z = screen_center.z + c*pixel_width*up.z + d*pixel_width*right.z; rays[i].start = end; rays[i].dir = sub_Coord(end, eye); rays[i].color = color; find_intersections(&rays[i], rc->ranges); } } //#endif }
void reconstruct() { holger_time_start(1, "Reconstruction"); memset(volume, 0, sizeof(unsigned char)*volume_w*volume_h*volume_n); printf("bscan_w/h: %d %d\n", bscan_w, bscan_h); printf("volume_w/h/n: %d %d %d\n", volume_w, volume_h, volume_n); printf("volume_spacing: %f\n", volume_spacing); printf("volume_origo: %f %f %f\n", volume_origo.x, volume_origo.y, volume_origo.z); printf("\n"); holger_time(1, "Initialization"); // Fill up queue for (int i = 0; i < BSCAN_WINDOW; i++) { shift_queues(); wait_for_input(); if (end_of_data()) break; } int counter = BSCAN_WINDOW; while(true) { // Retrieve ultrasound data and perform ye olde switcheroo shift_queues(); wait_for_input(); if (end_of_data()) break; holger_time(1, "Retrieve ultrasound data"); printf("Reconstructing %d ...\n", counter); calibrate_pos_matrix(pos_matrices_queue[BSCAN_WINDOW-1], cal_matrix); holger_time(1, "Calibrate"); printf("Calibrate\n"); insert_plane_points(pos_matrices_queue[BSCAN_WINDOW-1]); holger_time(1, "Fill, transform and translate plane_points"); printf("plane_points\n"); insert_plane_eq(); holger_time(1, "Fill bscan_plane_equation"); printf("bscan_plane_equation\n"); //int axis = find_orthogonal_axis(bscan_plane_equation); // TODO: Function that finds axis most orthogonal to bscan plane int axis = 0; // Actually, turns out that the output is pretty much equal for any axis, // but the computation time varies (1X - 2X) int intersection_counter = find_intersections(axis); holger_time(1, "Find ray-plane intersections"); printf("intersections\n"); fill_voxels(intersection_counter); holger_time(1, "Fill voxels"); printf("voxels\n"); counter++; } holger_time_print(1); }
unsigned intersect_ray_boundary (intersect_buffer_t *dst, const vec_t &line_0, const vec_t &line_dir, Boundary *b) { RayIntersectCollector st (dst); find_intersections (&st, *b, line_0, line_dir); sort_intersections (dst); return (int)dst->size (); }
static void calc_either_side(Crystal *cr, double incr_val, int *valid, long double *vals[3], int refine, PartialityModel pmodel) { RefList *compare; struct image *image = crystal_get_image(cr); struct image im_moved; im_moved = *image; shift_parameter(&im_moved, refine, -incr_val); compare = find_intersections(&im_moved, cr, pmodel); scan_partialities(crystal_get_reflections(cr), compare, valid, vals, 0); reflist_free(compare); im_moved = *image; shift_parameter(&im_moved, refine, +incr_val); compare = find_intersections(&im_moved, cr, pmodel); scan_partialities(crystal_get_reflections(cr), compare, valid, vals, 2); reflist_free(compare); }
void find_self_intersections(std::vector<std::pair<double, double> > &xs, D2<SBasis> const & A) { vector<double> dr = roots(derivative(A[X])); { vector<double> dyr = roots(derivative(A[Y])); dr.insert(dr.begin(), dyr.begin(), dyr.end()); } dr.push_back(0); dr.push_back(1); // We want to be sure that we have no empty segments sort(dr.begin(), dr.end()); vector<double>::iterator new_end = unique(dr.begin(), dr.end()); dr.resize( new_end - dr.begin() ); vector<vector<Point> > pieces; { vector<Point> in, l, r; sbasis_to_bezier(in, A); for(unsigned i = 0; i < dr.size()-1; i++) { split(in, (dr[i+1]-dr[i]) / (1 - dr[i]), l, r); pieces.push_back(l); in = r; } } for(unsigned i = 0; i < dr.size()-1; i++) { for(unsigned j = i+1; j < dr.size()-1; j++) { std::vector<std::pair<double, double> > section; find_intersections( section, pieces[i], pieces[j]); for(unsigned k = 0; k < section.size(); k++) { double l = section[k].first; double r = section[k].second; // XXX: This condition will prune out false positives, but it might create some false negatives. Todo: Confirm it is correct. if(j == i+1) //if((l == 1) && (r == 0)) if( ( l > 1-1e-4 ) && (r < 1e-4) )//FIXME: what precision should be used here??? continue; xs.push_back(std::make_pair((1-l)*dr[i] + l*dr[i+1], (1-r)*dr[j] + r*dr[j+1])); } } } // Because i is in order, xs should be roughly already in order? //sort(xs.begin(), xs.end()); //unique(xs.begin(), xs.end()); }
void separate_obstacles (vector<Mesh*> &obs_meshes, const vector<Mesh*> &meshes) { SO::xold.clear(); SO::nold.clear(); build_node_lookup(SO::xold, obs_meshes); build_face_normal_lookup(SO::nold, obs_meshes); vector<AccelStruct*> obs_accs = create_accel_structs(obs_meshes, false), accs = create_accel_structs(meshes, false); vector<Ixn> ixns; int iter; for (iter = 0; iter < max_iter; iter++) { if (!ixns.empty()) update_active(accs, ixns); vector<Ixn> new_ixns = find_intersections(accs, obs_accs); if (new_ixns.empty()) break; append(ixns, new_ixns); solve_ixns(ixns); for (int m = 0; m < (int)obs_meshes.size(); m++) { compute_ws_data(*obs_meshes[m]); update_accel_struct(*obs_accs[m]); } } if (iter == max_iter) { cerr << "Initial separation failed to converge!" << endl; exit(1); } for (int m = 0; m < (int)obs_meshes.size(); m++) { compute_ws_data(*obs_meshes[m]); update_x0(*obs_meshes[m]); } destroy_accel_structs(accs); destroy_accel_structs(obs_accs); }
static double test_gradients(Crystal *cr, double incr_val, int refine, const char *str, const char *file, PartialityModel pmodel, int quiet, int plot) { Reflection *refl; RefListIterator *iter; long double *vals[3]; int i; int *valid; int nref; int n_good, n_invalid, n_small, n_nan, n_bad; RefList *reflections; FILE *fh = NULL; int ntot = 0; double total = 0.0; char tmp[32]; double *vec1; double *vec2; int n_line; double cc; reflections = find_intersections(crystal_get_image(cr), cr, pmodel); crystal_set_reflections(cr, reflections); nref = num_reflections(reflections); if ( nref < 10 ) { ERROR("Too few reflections found. Failing test by default.\n"); return 0.0; } vals[0] = malloc(nref*sizeof(long double)); vals[1] = malloc(nref*sizeof(long double)); vals[2] = malloc(nref*sizeof(long double)); if ( (vals[0] == NULL) || (vals[1] == NULL) || (vals[2] == NULL) ) { ERROR("Couldn't allocate memory.\n"); return 0.0; } valid = malloc(nref*sizeof(int)); if ( valid == NULL ) { ERROR("Couldn't allocate memory.\n"); return 0.0; } for ( i=0; i<nref; i++ ) valid[i] = 1; scan_partialities(reflections, reflections, valid, vals, 1, pmodel); calc_either_side(cr, incr_val, valid, vals, refine, pmodel); if ( plot ) { snprintf(tmp, 32, "gradient-test-%s.dat", file); fh = fopen(tmp, "w"); } vec1 = malloc(nref*sizeof(double)); vec2 = malloc(nref*sizeof(double)); if ( (vec1 == NULL) || (vec2 == NULL) ) { ERROR("Couldn't allocate memory.\n"); return 0.0; } n_invalid = 0; n_good = 0; n_nan = 0; n_small = 0; n_bad = 0; n_line = 0; i = 0; for ( refl = first_refl(reflections, &iter); refl != NULL; refl = next_refl(refl, iter) ) { long double grad1, grad2, grad; double cgrad; signed int h, k, l; get_indices(refl, &h, &k, &l); if ( !valid[i] ) { n_invalid++; i++; } else { double r1, r2, p; grad1 = (vals[1][i] - vals[0][i]) / incr_val; grad2 = (vals[2][i] - vals[1][i]) / incr_val; grad = (grad1 + grad2) / 2.0; i++; cgrad = p_gradient(cr, refine, refl, pmodel); get_partial(refl, &r1, &r2, &p); if ( isnan(cgrad) ) { n_nan++; continue; } if ( plot ) { fprintf(fh, "%e %Le\n", cgrad, grad); } vec1[n_line] = cgrad; vec2[n_line] = grad; n_line++; if ( (fabs(cgrad) < 5e-8) && (fabs(grad) < 5e-8) ) { n_small++; continue; } total += fabs(cgrad - grad); ntot++; if ( !within_tolerance(grad, cgrad, 5.0) || !within_tolerance(cgrad, grad, 5.0) ) { if ( !quiet ) { STATUS("!- %s %3i %3i %3i" " %10.2Le %10.2e ratio = %5.2Lf" " %10.2e %10.2e\n", str, h, k, l, grad, cgrad, cgrad/grad, r1, r2); } n_bad++; } else { //STATUS("OK %s %3i %3i %3i" // " %10.2Le %10.2e ratio = %5.2Lf" // " %10.2e %10.2e\n", // str, h, k, l, grad, cgrad, cgrad/grad, // r1, r2); n_good++; } } } STATUS("%3s: %3i within 5%%, %3i outside, %3i nan, %3i invalid, " "%3i small. ", str, n_good, n_bad, n_nan, n_invalid, n_small); if ( plot ) { fclose(fh); } cc = gsl_stats_correlation(vec1, 1, vec2, 1, n_line); STATUS("CC = %+f\n", cc); return cc; }