static t_col shoot_ray(t_ray ray, int level_max, t_env *env) { float coef; float t; coef = 1.0; while (coef > 0.0f && level_max--) { t = 20000.0f; get_intersections(env, ray, &t); if (OBJ.cur_sphere != -1) set_val_sphere(env, t, ray); else if (OBJ.cur_tri != -1) set_val_tri(env, t, ray); else if (OBJ.cur_cyl != -1) set_val_cyl(env, t, ray); else if (OBJ.cur_cone != -1) set_val_cone(env, t, ray); else break ; if (env->br == 1) break ; calc_lighting(env, coef); coef *= OBJ.cur_mat.reflection; reflect_ray(env, &ray); } return (OBJ.col); }
/* * primary method to find overlaps among the peak files */ void ProcessPeaks::FindOverlaps() { vector<struct interval> A, B; fprintf(stderr, "Processing peaks - started\n"); parse_files(_genome, &_offsets, _peakA, _peakB, &A, &B); fprintf(stderr, "Processing peaks - Done\n"); uint32_t tot_overlaps = get_intersections(&A[0], A.size(), &B[0], B.size(), &overlap_index_A, &overlap_index_B); }
CONTOURTILER_BEGIN_NAMESPACE // /// Returns true if there are any polygon boundary intersections. // /// Precondition: InputIterator iterates over CGAL::Polygon_2 objects. // template <typename PolyIterator, typename OutputIterator> // void get_boundary_intersections(PolyIterator begin, PolyIterator end, OutputIterator intersecting_points, bool report_endpoints) // { // static log4cplus::Logger logger = log4cplus::Logger::getInstance("boundary_intersections"); // std::list<Segment_2> segments; // for (PolyIterator it = begin; it != end; ++it) // segments.insert(segments.end(), it->edges_begin(), it->edges_end()); // list<SL_intersection> ints; // get_intersections(segments.begin(), segments.end(), back_inserter(ints), report_endpoints, report_endpoints); // boost::unordered_set<Point_2> points; // for (list<SL_intersection>::iterator it = ints.begin(); it != ints.end(); ++it) // points.insert(it->point()); // copy(points.begin(), points.end(), intersecting_points); // } /// Returns true if there are any polygon boundary intersections. /// Precondition: InputIterator iterates over CGAL::Polygon_2 objects. boost::unordered_set<Point_2> get_boundary_intersections(const std::list<Segment_2>& segments, bool report_endpoints) { static log4cplus::Logger logger = log4cplus::Logger::getInstance("boundary_intersections"); list<SL_intersection> ints; get_intersections(segments.begin(), segments.end(), back_inserter(ints), report_endpoints, report_endpoints); boost::unordered_set<Point_2> points; for (list<SL_intersection>::iterator it = ints.begin(); it != ints.end(); ++it) points.insert(it->point()); return points; }
/* * Adds the effect of the given light over the given intersection. * * light: Light that is being applied. * inter: Intersection over which the light is being applied. * normal_vec: Normal vector of the intersection point. It is passed by as a * parameter for optimization purposes. * rev_dir_vec: Reverse vector of the direction of the ray that comes from the * eye. It is passed by as a parameter for optimization purposes. * all_lights_color: Accumulated amount of light sources effect. The effect of * the given light is added to this total. * all_lights_color: Accumulated amount of the specular light effect. The * specular effect of the given light is added to this total. * conf: Configuration of the scene. */ void apply_light_source(Light light, Intersection inter, Vector normal_vec, Vector rev_dir_vec, Color *all_lights_color, long double *all_spec_light, SceneConfig conf) { Vector light_vec; long double light_distance, illum_cos, att_factor, spec_cos; Color light_filter; Intersection* shadow_inter; int shadow_i, shadow_inter_length; Object shadow_obj; // Find the vector that points from the intersection point to the light // source, and normalize it light_vec = subtract_vectors(light.anchor, inter.posn); light_distance = normalize_vector(&light_vec); // We check for any object making a shadow from that light light_filter = (Color){ .red = 1.0, .green = 1.0, .blue = 1.0 };; shadow_inter = get_intersections(inter.posn, light_vec, &shadow_inter_length, conf); // If the intersection is beyond the light source, we ignore it if(shadow_inter) { // Object(s) is/are actually behind the light for(shadow_i = 0; shadow_i < shadow_inter_length && !is_color_empty(light_filter); shadow_i++) { if(shadow_inter[shadow_i].distance < light_distance) { shadow_obj = shadow_inter[shadow_i].obj; if(shadow_obj.translucency_material) { light_filter = multiply_color(shadow_obj.translucency_material, multiply_colors(light_filter, shadow_obj.color)); } else { light_filter = get_empty_color(); } } } if(!is_color_empty(light_filter)) { free(shadow_inter); shadow_inter = NULL; } } // If there aren't any shadows if(!shadow_inter) { illum_cos = do_dot_product(normal_vec, light_vec); // We only take it into account if the angle is lower than 90 degrees if(illum_cos > 0) { Vector light_mirror_vec = subtract_vectors(multiply_vector(2 * illum_cos, normal_vec), light_vec); // Attenuation factor, reduces the light energy depending on the distance att_factor = get_attenuation_factor(light, light_distance); spec_cos = do_dot_product(rev_dir_vec, light_mirror_vec); // We add the light source effect light_filter = multiply_color(illum_cos * inter.obj.light_material * att_factor, light_filter); *all_lights_color = add_colors(*all_lights_color, multiply_colors(light_filter, light.color)); // The specular light, is the white stain on the objects if(spec_cos > 0) { *all_spec_light += pow(spec_cos * inter.obj.specular_material * att_factor, inter.obj.specular_pow); } } } else free(shadow_inter); } /* * Gets the color of the intersection by calculating light intensity, * (which includes specular light, shadows, transparency, etc) * * light: Light for which the attenuation factor is calculated. * distance: Distance between the light and an illuminated spot. * mirror_level: Current level of reflection. * conf: Configuration of the scene. */ Color get_intersection_color(Vector eye, Vector dir_vec, Intersection *inter_list, int inter_length, int mirror_level, int transparency_level, SceneConfig conf) { Intersection inter; int light_index; long double spec_light_factor, mirror_factor, transparency_factor; Vector normal_vec, rev_dir_vec, reflection_vec; Light light; Color all_lights_color, color_found, reflection_color, transparency_color, final_color; inter = inter_list[transparency_level]; // Light intensity all_lights_color = (Color){ .red = 0.0, .green = 0.0, .blue = 0.0 }; // Specular light intensity spec_light_factor = 0.0; normal_vec = get_normal_vector(&inter); // Pick up the normal vector that is pointing to the eye if(do_dot_product(normal_vec, dir_vec) > 0) normal_vec = multiply_vector(-1, normal_vec); // Initialize reverse direction vector for mirrors and specular light rev_dir_vec = multiply_vector(-1, dir_vec); for(light_index = 0; light_index < conf.lights_length; light_index++) { light = conf.lights[light_index]; apply_light_source(light, inter, normal_vec, rev_dir_vec, &all_lights_color, &spec_light_factor, conf); } // We add the environmental light of the scene all_lights_color = add_colors(all_lights_color, multiply_color(inter.obj.light_ambiental, conf.environment_light)); if(spec_light_factor > 1.0) spec_light_factor = 1.0; color_found = multiply_colors(all_lights_color, inter.obj.color); // Specular light gives a color between enlightened color and the light color. color_found.red += (1 - color_found.red) * spec_light_factor; color_found.green += (1 - color_found.green) * spec_light_factor; color_found.blue += (1 - color_found.blue) * spec_light_factor; // Get transparency color transparency_factor = inter.obj.transparency_material; if (transparency_level < conf.max_transparency_level && transparency_factor > 0.0) { if(transparency_level + 1 < inter_length) { transparency_color = get_intersection_color(eye,dir_vec,inter_list,inter_length,0,transparency_level+1, conf); } else { transparency_color = conf.background; } } else { transparency_factor = 0.0; transparency_color = get_empty_color(); } // Get reflection color mirror_factor = inter.obj.mirror_material; if (mirror_level < conf.max_mirror_level && mirror_factor > 0.0) { reflection_vec = subtract_vectors(multiply_vector(2 * do_dot_product(normal_vec, rev_dir_vec), normal_vec), rev_dir_vec); reflection_color = get_color(inter.posn, reflection_vec, mirror_level + 1, conf); } else { mirror_factor = 0; reflection_color = get_empty_color(); } // Calculate final color transparency_color = multiply_color(transparency_factor, transparency_color); reflection_color = multiply_color((1.0-transparency_factor) * mirror_factor, reflection_color); color_found = multiply_color((1.0-transparency_factor) * (1.0-mirror_factor), color_found); final_color = add_colors(transparency_color, add_colors(reflection_color, color_found)); return final_color; } /* * Returns the color that is seen from the position 'eye' when looking at the * tridimensional scene towards the direction 'dir_vec'. * * eye: Position from which the scene is seen. * dir_vec: Direction at which the eye is looking. This vector must be normalized. * mirror_level: Current level of reflection. * conf: Configuration of the scene. */ Color get_color(Vector eye, Vector dir_vec, int mirror_level, SceneConfig conf) { Intersection *inter_list; Color color; // Get intersections on the given direction. Intersections are ordered from the nearest to the farthest. int inter_list_length; inter_list = get_intersections(eye, dir_vec, &inter_list_length, conf); // If we don't find an intersection we return the background, otherwise we check for the intersections's color. if (!inter_list) return conf.background; color = get_intersection_color(eye, dir_vec, inter_list, inter_list_length, mirror_level, 0, conf); free(inter_list); return color; }
double* test_intersection(char *universe_file_name, char *source_file_name, char *target_file_name, int iters) { struct timeval t_start,t_end; int chrom_num = 24; /***********************REPLACE WITH INPUT FILE************************/ char *chrom_names[] = { "chr1", "chr2", "chr3", "chr4", "chr5", "chr6", "chr7", "chr8", "chr9", "chr10", "chr11", "chr12", "chr13", "chr14", "chr15", "chr16", "chr17", "chr18", "chr19", "chr20", "chr21", "chr22", "chrX", "chrY" }; /**********************************************************************/ struct chr_list universe[chrom_num], source[chrom_num], target[chrom_num]; // initialize chrom lists int i; for (i = 0; i < chrom_num; i++) { universe[i] = new_chr_list(chrom_names[i]); source[i] = new_chr_list(chrom_names[i]); target[i] = new_chr_list(chrom_names[i]); } // chr_lists need to be sorted before used qsort(universe, chrom_num, sizeof(struct chr_list), compare_chr_lists); qsort(source, chrom_num, sizeof(struct chr_list), compare_chr_lists); qsort(target, chrom_num, sizeof(struct chr_list), compare_chr_lists); FILE *universe_file = fopen(universe_file_name, "r"); FILE *source_file = fopen(source_file_name, "r"); FILE *target_file = fopen(target_file_name, "r"); if ( (universe_file == NULL) || (source_file == NULL) || (target_file == NULL) ) { fprintf(stderr, "%s\n", strerror(errno)); return 0; } parse_bed_file(universe_file, universe, chrom_num); parse_bed_file(source_file, source, chrom_num); parse_bed_file(target_file, target, chrom_num); fclose(universe_file); fclose(source_file); fclose(target_file); trim(universe, source, chrom_num); trim(universe, target, chrom_num); // Calculate the offsets for each iterval int c = 0; int max = 0; for (i = 0; i < chrom_num; i++) { struct interval_node *curr = universe[i].head; while (curr != NULL) { curr->offset = c; int end = c + curr->end - curr->start; if (end > max) max = end; c += curr->end - curr->start; curr = curr->next; } } int total_size = 0, target_size = 0, source_size = 0; /* * Get the total number of intervals in the source and target sets, we also * store the number of intervals individually to be used later for * randomization */ for (i = 0; i < chrom_num; i++) { total_size += source[i].size; total_size += target[i].size; target_size += target[i].size; source_size += source[i].size; } /* * get an array of just the target intervals, each random permutation will * consist of these intervals and a set of randomly generated intervals */ struct interval *target_intervals = (struct interval *) malloc( 2 * target_size * sizeof(struct interval) ); i = 0; int pushed_targets = push_intervals(chrom_num, &i, universe, target, target_intervals, 0); /* * we need will permute the intervals in target, to manage this we will * create and array of the interval sizes in source */ int *rand_sizes = (int *) malloc( source_size * sizeof(int) ); int j = 0; for (i = 0; i < chrom_num; i++) { struct interval_node *curr = source[i].head; while (curr != NULL) { rand_sizes[j++] = curr->end - curr->start; curr = curr->next; } } /* * set up an array with target and source to find the observed number of * intersections */ struct interval *intervals = (struct interval *) malloc( 2 * total_size * sizeof(struct interval) ); for (i = 0; i < 2 * target_size; i++) intervals[i] = target_intervals[i]; int pushed_sources = push_intervals(chrom_num, &i, universe, source, intervals, 1); gettimeofday(&t_start,0); int obs = get_intersections(intervals, total_size); gettimeofday(&t_end,0); double p_of_source = (double)obs / (double)source_size; double p_of_target = (double)obs / (double)target_size; int r = 0; int sum = 0; // do some random stuff struct interval rand_start, rand_end; rand_start.type = 's'; rand_end.type = 'e'; rand_start.sample = 1; rand_end.sample = 1; for (i = 0; i < iters; i++) { for (j = 0; j < 2 * target_size; j++) intervals[j] = target_intervals[j]; for (j = 0; j < source_size; j++) { rand_start.offset = rand() % (max - rand_sizes[j]); rand_end.offset = rand_start.offset + rand_sizes[j]; intervals[ 2 * target_size + 2 * j ] = rand_start; intervals[ 2 * target_size + 2 * j + 1 ] = rand_end; } int t = get_intersections(intervals, total_size); sum += t; if ( t >= obs ) ++r; } double p = ( (double)(r + 1) ) / ( (double)(iters + 1) ); double mean = ( (double)(sum) ) / ( (double)(iters) ); double *ret = (double *) malloc(5 * sizeof(double)); ret[0] = obs; ret[1] = mean; ret[2] = p; ret[3] = p_of_source; ret[4] = p_of_target; free_chr_list(universe, chrom_num); free_chr_list(source, chrom_num); free_chr_list(target, chrom_num); free(target_intervals); free(rand_sizes); free(intervals); return ret; }