Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range for loop instead of explicit indexing #1326

Merged
merged 5 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/yimage/yimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void run_setalpha(const setalpha_params& params) {

// edit alpha
auto out = make_image(image.width, image.height, image.linear);
for (auto idx = (size_t)0; idx < image.pixels.size(); idx++) {
for (auto idx : range(image.pixels.size())) {
auto calpha = alpha.pixels[idx];
auto alpha_ = params.from_color ? mean(xyz(calpha))
: params.from_black ? (mean(xyz(calpha)) > 0.01 ? 1.0f : 0.0f)
Expand Down
2 changes: 1 addition & 1 deletion apps/ymesh/ymesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ static pair<vector<shape_point>, vec2f> sample_stroke(const bvh_tree& bvh,
auto update_uv = (mouse_uv - last_uv) * stroke_dist / delta_pos;
auto cur_uv = last_uv;
auto samples = vector<shape_point>{};
for (auto step = 0; step < steps; step++) {
for (auto step : range(steps)) {
cur_uv += update_uv;
auto isec = intersect_shape(cur_uv);
if (!isec.hit) continue;
Expand Down
2 changes: 1 addition & 1 deletion apps/yscene/yscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void run_render(const render_params& params_) {

// render
timer = simple_timer{};
for (auto sample = 0; sample < params.samples; sample++) {
for (auto sample : range(params.samples)) {
auto sample_timer = simple_timer{};
trace_samples(state, scene, bvh, lights, params);
print_info("render sample {}/{}: {}", sample, params.samples,
Expand Down
20 changes: 10 additions & 10 deletions libs/yocto/yocto_bvh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static pair<int, int> split_sah(vector<int>& primitives,
return 1e-12f + 2 * size.x * size.y + 2 * size.x * size.z +
2 * size.y * size.z;
};
for (auto saxis = 0; saxis < 3; saxis++) {
for (auto saxis : range(3)) {
for (auto b = 1; b < nbins; b++) {
auto bsplit = cbbox.min[saxis] + b * csize[saxis] / nbins;
auto left_bbox = invalidb3f, right_bbox = invalidb3f;
Expand Down Expand Up @@ -303,11 +303,11 @@ static void refit_bvh(vector<bvh_node>& nodes, const vector<int>& primitives,
auto& node = nodes[nodeid];
node.bbox = invalidb3f;
if (node.internal) {
for (auto idx = 0; idx < 2; idx++) {
for (auto idx : range(2)) {
node.bbox = merge(node.bbox, nodes[node.start + idx].bbox);
}
} else {
for (auto idx = 0; idx < node.num; idx++) {
for (auto idx : range(node.num)) {
node.bbox = merge(node.bbox, bboxes[primitives[node.start + idx]]);
}
}
Expand Down Expand Up @@ -365,7 +365,7 @@ scene_bvh make_scene_bvh(
// build shape bvh
bvh.shapes.resize(scene.shapes.size());
if (noparallel) {
for (auto idx = (size_t)0; idx < scene.shapes.size(); idx++) {
for (auto idx : range(scene.shapes.size())) {
bvh.shapes[idx] = make_shape_bvh(scene.shapes[idx], highquality);
}
} else {
Expand Down Expand Up @@ -654,7 +654,7 @@ shape_intersection overlap_shape_bvh(const shape_bvh& bvh,
node_stack[node_cur++] = node.start + 0;
node_stack[node_cur++] = node.start + 1;
} else if (!shape.points.empty()) {
for (auto idx = 0; idx < node.num; idx++) {
for (auto idx : range(node.num)) {
auto primitive = bvh.primitives[node.start + idx];
auto& p = shape.points[primitive];
auto eintersection = overlap_point(
Expand All @@ -665,7 +665,7 @@ shape_intersection overlap_shape_bvh(const shape_bvh& bvh,
max_distance = eintersection.distance;
}
} else if (!shape.lines.empty()) {
for (auto idx = 0; idx < node.num; idx++) {
for (auto idx : range(node.num)) {
auto primitive = bvh.primitives[node.start + idx];
auto& l = shape.lines[primitive];
auto eintersection = overlap_line(pos, max_distance,
Expand All @@ -677,7 +677,7 @@ shape_intersection overlap_shape_bvh(const shape_bvh& bvh,
max_distance = eintersection.distance;
}
} else if (!shape.triangles.empty()) {
for (auto idx = 0; idx < node.num; idx++) {
for (auto idx : range(node.num)) {
auto primitive = bvh.primitives[node.start + idx];
auto& t = shape.triangles[primitive];
auto eintersection = overlap_triangle(pos, max_distance,
Expand All @@ -689,7 +689,7 @@ shape_intersection overlap_shape_bvh(const shape_bvh& bvh,
max_distance = eintersection.distance;
}
} else if (!shape.quads.empty()) {
for (auto idx = 0; idx < node.num; idx++) {
for (auto idx : range(node.num)) {
auto primitive = bvh.primitives[node.start + idx];
auto& q = shape.quads[primitive];
auto eintersection = overlap_quad(pos, max_distance,
Expand Down Expand Up @@ -740,7 +740,7 @@ scene_intersection overlap_scene_bvh(const scene_bvh& bvh,
node_stack[node_cur++] = node.start + 0;
node_stack[node_cur++] = node.start + 1;
} else {
for (auto idx = 0; idx < node.num; idx++) {
for (auto idx : range(node.num)) {
auto primitive = bvh.primitives[node.start + idx];
auto& instance_ = scene.instances[primitive];
auto& shape = scene.shapes[instance_.shape];
Expand Down Expand Up @@ -973,7 +973,7 @@ scene_embree_bvh make_scene_embree_bvh(
// shape bvhs
bvh.shapes.resize(scene.shapes.size());
if (noparallel) {
for (auto idx = (size_t)0; idx < scene.shapes.size(); idx++) {
for (auto idx : range(scene.shapes.size())) {
bvh.shapes[idx] = make_shape_embree_bvh(scene.shapes[idx], highquality);
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions libs/yocto/yocto_cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ inline void add_option(cli_command& cli, const string& name, vector<T>& value,
cli.options[name] = [&value](const vector<string>& args, string& error) {
if (!_cli_parse_size(args, 1, 1024, error)) return false;
value.resize(args.size());
for (auto idx = (size_t)0; idx < args.size(); idx++) {
for (auto idx = 0; idx < (int)args.size(); idx++) {
if (!_cli_parse_value(args[idx], value[idx], error)) return false;
}
return true;
Expand All @@ -449,7 +449,7 @@ inline void add_option(cli_command& cli, const string& name, array<T, N>& value,
cli.usage_options += _cli_usage_option(name, value, usage);
cli.options[name] = [&value](const vector<string>& args, string& error) {
if (!_cli_parse_size(args, N, N, error)) return false;
for (auto idx = (size_t)0; idx < args.size(); idx++) {
for (auto idx = 0; idx < (int)args.size(); idx++) {
if (!_cli_parse_value(args[idx], value[idx], error)) return false;
}
return true;
Expand Down
6 changes: 3 additions & 3 deletions libs/yocto/yocto_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ void show_image_gui(const string& title, const vector<string>& names,
auto displays = vector<image_data>(images.size());
auto exposures = vector<float>(images.size(), 0);
auto filmics = vector<bool>(images.size(), false);
for (auto idx = 0; idx < (int)images.size(); idx++) {
for (auto idx : range((int)images.size())) {
displays[idx] = make_image(images[idx].width, images[idx].height, false);
tonemap_image_mt(displays[idx], images[idx], exposures[idx], filmics[idx]);
}
Expand Down Expand Up @@ -678,7 +678,7 @@ void show_trace_gui(const string& title, const string& name, scene_data& scene,
for (auto sample = 0; sample < params.samples; sample += params.batch) {
if (render_stop) return;
parallel_for(state.width, state.height, [&](int i, int j) {
for (auto s = 0; s < params.batch; s++) {
for (auto s : range(params.batch)) {
if (render_stop) return;
trace_sample(state, scene, bvh, lights, i, j, params);
}
Expand Down Expand Up @@ -2275,7 +2275,7 @@ bool draw_gui_combobox(const char* lbl, int& idx, int num,
if (idx < 0) ImGui::SetItemDefaultFocus();
ImGui::PopID();
}
for (auto i = 0; i < num; i++) {
for (auto i : range(num)) {
ImGui::PushID(i);
if (ImGui::Selectable(labels(i).c_str(), idx == i)) idx = i;
if (idx == i) ImGui::SetItemDefaultFocus();
Expand Down
44 changes: 22 additions & 22 deletions libs/yocto/yocto_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void convert_image(image_data& result, const image_data& image) {
if (image.linear == result.linear) {
result.pixels = image.pixels;
} else {
for (auto idx = (size_t)0; idx < image.pixels.size(); idx++) {
for (auto idx : range(image.pixels.size())) {
result.pixels[idx] = image.linear ? rgb_to_srgb(image.pixels[idx])
: srgb_to_rgb(image.pixels[idx]);
}
Expand Down Expand Up @@ -198,12 +198,12 @@ void tonemap_image(
throw std::invalid_argument{"image should be the same size"};
if (result.linear) throw std::invalid_argument{"ldr expected"};
if (image.linear) {
for (auto idx = (size_t)0; idx < image.pixels.size(); idx++) {
for (auto idx : range(image.pixels.size())) {
result.pixels[idx] = tonemap(image.pixels[idx], exposure, filmic);
}
} else {
auto scale = vec4f{pow(2, exposure), pow(2, exposure), pow(2, exposure), 1};
for (auto idx = (size_t)0; idx < image.pixels.size(); idx++) {
for (auto idx : range(image.pixels.size())) {
result.pixels[idx] = image.pixels[idx] * scale;
}
}
Expand Down Expand Up @@ -265,7 +265,7 @@ image_data image_difference(

// compute diff
auto difference = make_image(image1.width, image1.height, image1.linear);
for (auto idx = (size_t)0; idx < difference.pixels.size(); idx++) {
for (auto idx : range(difference.pixels.size())) {
auto diff = abs(image1.pixels[idx] - image2.pixels[idx]);
difference.pixels[idx] = display ? vec4f{max(diff), max(diff), max(diff), 1}
: diff;
Expand All @@ -274,8 +274,8 @@ image_data image_difference(
}

void set_region(image_data& image, const image_data& region, int x, int y) {
for (auto j = 0; j < region.height; j++) {
for (auto i = 0; i < region.width; i++) {
for (auto j : range(region.height)) {
for (auto i : range(region.width)) {
image.pixels[(j + y) * image.width + (i + x)] =
region.pixels[j * region.width + i];
}
Expand All @@ -287,8 +287,8 @@ void get_region(image_data& region, const image_data& image, int x, int y,
if (region.width != width || region.height != height) {
region = make_image(width, height, image.linear);
}
for (auto j = 0; j < height; j++) {
for (auto i = 0; i < width; i++) {
for (auto j : range(height)) {
for (auto i : range(width)) {
region.pixels[j * region.width + i] =
image.pixels[(j + y) * image.width + (i + x)];
}
Expand All @@ -303,7 +303,7 @@ image_data composite_image(
if (image_a.linear != image_b.linear)
throw std::invalid_argument{"image should be of the same type"};
auto result = make_image(image_a.width, image_a.height, image_a.linear);
for (auto idx = (size_t)0; idx < result.pixels.size(); idx++) {
for (auto idx : range(result.pixels.size())) {
result.pixels[idx] = composite(image_a.pixels[idx], image_b.pixels[idx]);
}
return result;
Expand All @@ -320,7 +320,7 @@ void composite_image(
throw std::invalid_argument{"image should be the same size"};
if (image_a.linear != result.linear)
throw std::invalid_argument{"image should be of the same type"};
for (auto idx = (size_t)0; idx < result.pixels.size(); idx++) {
for (auto idx : range(result.pixels.size())) {
result.pixels[idx] = composite(image_a.pixels[idx], image_b.pixels[idx]);
}
}
Expand Down Expand Up @@ -365,7 +365,7 @@ vec4f colorgradeb(
image_data colorgrade_image(
const image_data& image, const colorgrade_params& params) {
auto result = make_image(image.width, image.height, false);
for (auto idx = (size_t)0; idx < image.pixels.size(); idx++) {
for (auto idx : range(image.pixels.size())) {
result.pixels[idx] = colorgrade(image.pixels[idx], image.linear, params);
}
return result;
Expand All @@ -378,7 +378,7 @@ void colorgrade_image(image_data& result, const image_data& image,
if (image.width != result.width || image.height != result.height)
throw std::invalid_argument{"image should be the same size"};
if (!!result.linear) throw std::invalid_argument{"non linear expected"};
for (auto idx = (size_t)0; idx < image.pixels.size(); idx++) {
for (auto idx : range(image.pixels.size())) {
result.pixels[idx] = colorgrade(image.pixels[idx], image.linear, params);
}
}
Expand Down Expand Up @@ -452,8 +452,8 @@ static image_data make_proc_image(
int width, int height, bool linear, Shader&& shader) {
auto image = make_image(width, height, linear);
auto scale = 1.0f / max(width, height);
for (auto j = 0; j < height; j++) {
for (auto i = 0; i < width; i++) {
for (auto j : range(height)) {
for (auto i : range(width)) {
auto uv = vec2f{i * scale, j * scale};
image.pixels[j * width + i] = shader(uv);
}
Expand Down Expand Up @@ -637,8 +637,8 @@ image_data add_border(
const image_data& image, float width, const vec4f& color) {
auto result = image;
auto scale = 1.0f / max(image.width, image.height);
for (auto j = 0; j < image.height; j++) {
for (auto i = 0; i < image.width; i++) {
for (auto j : range(image.height)) {
for (auto i : range(image.width)) {
auto uv = vec2f{i * scale, j * scale};
if (uv.x < width || uv.y < width || uv.x > image.width * scale - width ||
uv.y > image.height * scale - width) {
Expand Down Expand Up @@ -798,7 +798,7 @@ image_data make_lights(int width, int height, const vec3f& le, int nlights,
for (int i = 0; i < width; i++) {
auto phi = 2 * pif * (float(i + 0.5f) / width);
auto inlight = false;
for (auto l = 0; l < nlights; l++) {
for (auto l : range(nlights)) {
auto lphi = 2 * pif * (l + 0.5f) / nlights;
inlight = inlight || fabs(phi - lphi) < lwidth / 2;
}
Expand Down Expand Up @@ -1045,8 +1045,8 @@ static void make_proc_image(
vector<vec4f>& pixels, int width, int height, Shader&& shader) {
pixels.resize((size_t)width * (size_t)height);
auto scale = 1.0f / max(width, height);
for (auto j = 0; j < height; j++) {
for (auto i = 0; i < width; i++) {
for (auto j : range(height)) {
for (auto i : range(width)) {
auto uv = vec2f{i * scale, j * scale};
pixels[j * width + i] = shader(uv);
}
Expand Down Expand Up @@ -1231,8 +1231,8 @@ void add_border(vector<vec4f>& pixels, const vector<vec4f>& source, int width,
int height, float thickness, const vec4f& color) {
pixels = source;
auto scale = 1.0f / max(width, height);
for (auto j = 0; j < height; j++) {
for (auto i = 0; i < width; i++) {
for (auto j : range(height)) {
for (auto i : range(width)) {
auto uv = vec2f{i * scale, j * scale};
if (uv.x < thickness || uv.y < thickness ||
uv.x > width * scale - thickness ||
Expand Down Expand Up @@ -1389,7 +1389,7 @@ void make_lights(vector<vec4f>& pixels, int width, int height, const vec3f& le,
for (int i = 0; i < width; i++) {
auto phi = 2 * pif * (float(i + 0.5f) / width);
auto inlight = false;
for (auto l = 0; l < nlights; l++) {
for (auto l : range(nlights)) {
auto lphi = 2 * pif * (l + 0.5f) / nlights;
inlight = inlight || fabs(phi - lphi) < lwidth / 2;
}
Expand Down
Loading