string simplifyPath(string path) { string local("/."); string prev_dir("/.."); string cur_dir; int pos = 0; int n = path.size(); vector<string> absolute_path; while (pos < n){ while (pos < n-1 && (path[pos] != '/' || path[pos+1] =='/')) pos++; if (pos == n-1 && path[pos] == '/'){ break; } int nxt = pos + 1; while (nxt < n && path[nxt] != '/'){ nxt++; } cur_dir = path.substr(pos, nxt - pos ); cout << cur_dir << endl; if (cur_dir == local){ pos = nxt; continue; }else if (cur_dir == prev_dir){ if (!absolute_path.empty()) absolute_path.pop_back(); pos = nxt; } else { absolute_path.push_back(cur_dir); pos = nxt; } } if (absolute_path.empty()){ return "/"; } string res; for (int i = 0; i != absolute_path.size(); ++i){ res += absolute_path[i]; } return res; }
int copy(const char *in_object) { char object[PATH_SIZE]; strcpy(object, in_object); struct stat buf; // log_add(DEBUGGING, "In copy %s\n", object); if (stat(object, &buf) != 0) { log_add(FATAL, "stat failed\n"); return errno; } if(S_ISDIR(buf.st_mode)) { DIR *dir; struct dirent *entry; if ((dir = opendir(object)) == 0) { log_add(FATAL, "Can't open directory\n"); return errno; } set_dir(get_filename(object), buf.st_mode); while ((entry = readdir(dir)) != NULL) { // log_add(DEBUGGING, "In while\n"); // log_add(DEBUGGING, "object is %s\n", object); //log_add(DEBUGGING, "file %s\n", entry->d_name); if (is_good_file(entry->d_name)) { strcat(object, "/"); strcat(object, entry->d_name); copy(object); object[strlen(object)-strlen(entry->d_name)-1] = '\0'; } } closedir(dir); prev_dir(); } else { // log_add(DEBUGGING, "It's file: %s\n", object); copy_file(object, curr_out_dir()); } return 0; }
static void term_handle_key(mpg123_handle *fr, out123_handle *ao, char val) { debug1("term_handle_key: %c", val); switch(tolower(val)) { case MPG123_BACK_KEY: out123_pause(ao); out123_drop(ao); if(paused) pause_cycle=(int)(LOOP_CYCLES/mpg123_tpf(fr)); if(mpg123_seek_frame(fr, 0, SEEK_SET) < 0) error1("Seek to begin failed: %s", mpg123_strerror(fr)); framenum=0; break; case MPG123_NEXT_KEY: out123_pause(ao); out123_drop(ao); next_track(); break; case MPG123_NEXT_DIR_KEY: out123_pause(ao); out123_drop(ao); next_dir(); break; case MPG123_QUIT_KEY: debug("QUIT"); if(stopped) { stopped = 0; out123_pause(ao); /* no chance for annoying underrun warnings */ out123_drop(ao); } set_intflag(); offset = 0; break; case MPG123_PAUSE_KEY: paused=1-paused; out123_pause(ao); /* underrun awareness */ out123_drop(ao); if(paused) { /* Not really sure if that is what is wanted This jumps in audio output, but has direct reaction to pausing loop. */ out123_param_float(ao, OUT123_PRELOAD, 0.); pause_recycle(fr); } else out123_param_float(ao, OUT123_PRELOAD, param.preload); if(stopped) stopped=0; if(param.verbose) print_stat(fr, 0, ao); else fprintf(stderr, "%s", (paused) ? MPG123_PAUSED_STRING : MPG123_EMPTY_STRING); break; case MPG123_STOP_KEY: case ' ': /* TODO: Verify/ensure that there is no "chirp from the past" when seeking while stopped. */ stopped=1-stopped; if(paused) { paused=0; offset -= pause_cycle; } if(stopped) out123_pause(ao); else { if(offset) /* If position changed, old is outdated. */ out123_drop(ao); /* No out123_continue(), that's triggered by out123_play(). */ } if(param.verbose) print_stat(fr, 0, ao); else fprintf(stderr, "%s", (stopped) ? MPG123_STOPPED_STRING : MPG123_EMPTY_STRING); break; case MPG123_FINE_REWIND_KEY: seekmode(fr, ao); offset--; break; case MPG123_FINE_FORWARD_KEY: seekmode(fr, ao); offset++; break; case MPG123_REWIND_KEY: seekmode(fr, ao); offset-=10; break; case MPG123_FORWARD_KEY: seekmode(fr, ao); offset+=10; break; case MPG123_FAST_REWIND_KEY: seekmode(fr, ao); offset-=50; break; case MPG123_FAST_FORWARD_KEY: seekmode(fr, ao); offset+=50; break; case MPG123_VOL_UP_KEY: mpg123_volume_change(fr, 0.02); break; case MPG123_VOL_DOWN_KEY: mpg123_volume_change(fr, -0.02); break; case MPG123_PITCH_UP_KEY: case MPG123_PITCH_BUP_KEY: case MPG123_PITCH_DOWN_KEY: case MPG123_PITCH_BDOWN_KEY: case MPG123_PITCH_ZERO_KEY: { double new_pitch = param.pitch; switch(val) /* Not tolower here! */ { case MPG123_PITCH_UP_KEY: new_pitch += MPG123_PITCH_VAL; break; case MPG123_PITCH_BUP_KEY: new_pitch += MPG123_PITCH_BVAL; break; case MPG123_PITCH_DOWN_KEY: new_pitch -= MPG123_PITCH_VAL; break; case MPG123_PITCH_BDOWN_KEY: new_pitch -= MPG123_PITCH_BVAL; break; case MPG123_PITCH_ZERO_KEY: new_pitch = 0.0; break; } set_pitch(fr, ao, new_pitch); fprintf(stderr, "New pitch: %f\n", param.pitch); } break; case MPG123_VERBOSE_KEY: param.verbose++; if(param.verbose > VERBOSE_MAX) { param.verbose = 0; clear_stat(); } mpg123_param(fr, MPG123_VERBOSE, param.verbose, 0); break; case MPG123_RVA_KEY: if(++param.rva > MPG123_RVA_MAX) param.rva = 0; if(param.verbose) fprintf(stderr, "\n"); mpg123_param(fr, MPG123_RVA, param.rva, 0); mpg123_volume_change(fr, 0.); break; case MPG123_PREV_KEY: out123_pause(ao); out123_drop(ao); prev_track(); break; case MPG123_PREV_DIR_KEY: out123_pause(ao); out123_drop(ao); prev_dir(); break; case MPG123_PLAYLIST_KEY: fprintf(stderr, "%s\nPlaylist (\">\" indicates current track):\n", param.verbose ? "\n" : ""); print_playlist(stderr, 1); fprintf(stderr, "\n"); break; case MPG123_TAG_KEY: fprintf(stderr, "%s\n", param.verbose ? "\n" : ""); print_id3_tag(fr, param.long_id3, stderr); fprintf(stderr, "\n"); break; case MPG123_MPEG_KEY: if(param.verbose) print_stat(fr,0,ao); /* Make sure that we are talking about the correct frame. */ fprintf(stderr, "\n"); if(param.verbose > 1) print_header(fr); else print_header_compact(fr); fprintf(stderr, "\n"); break; case MPG123_HELP_KEY: { /* This is more than the one-liner before, but it's less spaghetti. */ int i; fprintf(stderr,"\n\n -= terminal control keys =-\n"); for(i=0; i<(sizeof(term_help)/sizeof(struct keydef)); ++i) { if(term_help[i].key2) fprintf(stderr, "[%c] or [%c]", term_help[i].key, term_help[i].key2); else fprintf(stderr, "[%c]", term_help[i].key); fprintf(stderr, "\t%s\n", term_help[i].desc); } fprintf(stderr, "\nAlso, the number row (starting at 1, ending at 0) gives you jump points into the current track at 10%% intervals.\n"); fprintf(stderr, "\n"); } break; case MPG123_FRAME_INDEX_KEY: case MPG123_VARIOUS_INFO_KEY: if(param.verbose) fprintf(stderr, "\n"); switch(val) /* because of tolower() ... */ { case MPG123_FRAME_INDEX_KEY: print_index(fr); { long accurate; if(mpg123_getstate(fr, MPG123_ACCURATE, &accurate, NULL) == MPG123_OK) fprintf(stderr, "Accurate position: %s\n", (accurate == 0 ? "no" : "yes")); else error1("Unable to get state: %s", mpg123_strerror(fr)); } break; case MPG123_VARIOUS_INFO_KEY: { const char* curdec = mpg123_current_decoder(fr); if(curdec == NULL) fprintf(stderr, "Cannot get decoder info!\n"); else fprintf(stderr, "Active decoder: %s\n", curdec); } } break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { off_t len; int num; num = val == '0' ? 10 : val - '0'; --num; /* from 0 to 9 */ /* Do not swith to seekmode() here, as we are jumping once to a specific position. Dropping buffer contents is enough and there is no race filling the buffer or waiting for more incremental seek orders. */ len = mpg123_length(fr); out123_pause(ao); out123_drop(ao); if(len > 0) mpg123_seek(fr, (off_t)( (num/10.)*len ), SEEK_SET); } break; case MPG123_BOOKMARK_KEY: continue_msg("BOOKMARK"); break; default: ; } }
glm::vec3 CTracer::TraceRay(SRay ray) { int max_iter = 400; int straight_iters_num = 7; double time_step = 4; // in seconds double eps = 1e-5; double ztol = 1e-7; glm::vec4 disk_color(-1, -1, -1, -1); bool crossed_disk = false; bool crossed_other = false; glm::vec3 color(0, 0, 1); glm::vec4 other_color; glm::dvec3 cur_pos(m_camera.m_pos), cur_speed(ray.m_dir / glm::length(ray.m_dir) * light_speed); int iter = 0; glm::dvec3 cur_dir(0.0, 0.0, 0.0); glm::dvec3 prev_dir(0.0, 0.0, 0.0); Segment segm; SRay changed; double cam_to_bh = glm::length(m_camera.m_pos - black_hole.center); glm::dvec3 accel; int last_iter_straight = -1; double cur_pos_abs_sq; //while (iter < max_iter) { while (true) { // simple: //cur_pos += ray.m_dir * light_speed * time_step; //cur_speed = ray.m_dir / glm::length(ray.m_dir) * light_speed; // with acceleration: cur_pos_abs_sq = glm::dot(cur_pos, cur_pos); accel = -(cur_pos / glm::length(cur_pos)) * grav_const * black_hole.mass / cur_pos_abs_sq; // assuming coordinates center is in center of black hole. //fout << "accel: " << accel.x << ' ' << accel.y << ' ' << accel.z << ", module: " << glm::length(accel) << std::endl; segm.start = cur_pos; //changed.m_start = cur_pos; cur_pos += (cur_speed + accel * time_step / 2.0) * time_step; cur_speed += accel * time_step; cur_speed *= light_speed / glm::length(cur_speed); // choosing non-uniform time step //time_step = 2 / glm::length(accel); // Checking intersection for segment. segm.end = cur_pos; auto intersn = intersection(segm); if (intersn.first == SPHERE_INTERSN) { other_color = intersn.second; crossed_other = true; if (!alpha_blending_enable) { return rgb_cut(other_color); } break; } else if (intersn.first == DISK_INTERSN && intersn.second.w > ztol) { if (!crossed_disk) { disk_color = intersn.second; crossed_disk = true; if (!alpha_blending_enable) { return rgb_cut(disk_color); } } else { // encountered disk previously. // It is impossible to come to this point if alpha blending is disabled. disk_color = alpha_blend(disk_color, intersn.second); } } // another way is to compute cross product //if (glm::dot(cur_pos, cur_pos) > 3 * (cam_to_bh + black_hole.radius) * (cam_to_bh + black_hole.radius) || //if (glm::dot(cur_speed, prev_dir) < (1 - eps) * light_speed * light_speed) { // speeds must be of length light_speed // has put light_speed^2 in the right side of equation if (glm::length(glm::cross(cur_speed, prev_dir)) < eps * light_speed * light_speed && glm::dot(cur_pos, cur_pos) > 2 * (cam_to_bh + black_hole.radius) * (cam_to_bh + black_hole.radius)) { if (last_iter_straight == -1) { last_iter_straight = iter; } else if (iter - last_iter_straight > straight_iters_num) { glm::dvec2 spher(0, 0); glm::dvec3 dir_normd = cur_speed / glm::length(cur_speed); spher.x = atan2(dir_normd.x, dir_normd.y) + PI; // (at least in MS library) atan2 returns value in [-pi, pi] spher.y = asin(dir_normd.z); //fout << "final iter no: " << iter << std::endl; //fout << "spher coords: " << spher.y << ' ' << spher.x << std::endl; std::pair<unsigned, unsigned> stars_shape = img_shape(1); double H = stars_shape.second; spher.x *= H / PI; // transforming [0, 2pi) to [0, 2H) spher.y = (spher.y + PI / 2.0) * H / PI; // transforming [-pi/2, pi/2] to [0, H] spher.x = glm::clamp(spher.x, 0.0, 2 * H - 1); spher.y = glm::clamp(spher.y, 0.0, H - 1); //fout << "ray start: " << ray.m_start.x << ' ' << ray.m_start.y << ' ' << ray.m_start.z << std::endl; //fout << "ray dir: " << ray.m_dir.x << ' ' << ray.m_dir.y << ' ' << ray.m_dir.z << std::endl; //fout << "spher coords: " << spher.y << ' ' << spher.x << std::endl; other_color = img_get_pxl_rgba(1, int(spher.x), int(spher.y)); other_color.w = 1; crossed_other = true; break; } } prev_dir = cur_speed; iter++; } //fout << "min dist to black hole: " << *min_element(dist_to_bh.begin(), dist_to_bh.end()) << std::endl; //std::cout << alpha_blending_enable << std::endl; if (!alpha_blending_enable) { color = rgb_cut(other_color); } else { if (crossed_disk) { if (!crossed_other) { color = rgb_cut(disk_color); } else { // Alpha-blending color = rgb_cut(alpha_blend(disk_color, other_color)); } } else { color = rgb_cut(other_color); } } return color; }