Skip to content

Commit

Permalink
Range for loop instead of explicit indexing (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy authored Jan 16, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 0bf848c commit 0324b26
Showing 14 changed files with 141 additions and 141 deletions.
2 changes: 1 addition & 1 deletion apps/yimage/yimage.cpp
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion apps/ymesh/ymesh.cpp
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion apps/yscene/yscene.cpp
Original file line number Diff line number Diff line change
@@ -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,
20 changes: 10 additions & 10 deletions libs/yocto/yocto_bvh.cpp
Original file line number Diff line number Diff line change
@@ -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;
@@ -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]]);
}
}
@@ -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 {
@@ -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(
@@ -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,
@@ -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,
@@ -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,
@@ -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];
@@ -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 {
4 changes: 2 additions & 2 deletions libs/yocto/yocto_cli.h
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
6 changes: 3 additions & 3 deletions libs/yocto/yocto_gui.cpp
Original file line number Diff line number Diff line change
@@ -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]);
}
@@ -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);
}
@@ -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();
44 changes: 22 additions & 22 deletions libs/yocto/yocto_image.cpp
Original file line number Diff line number Diff line change
@@ -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]);
}
@@ -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;
}
}
@@ -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;
@@ -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];
}
@@ -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)];
}
@@ -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;
@@ -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]);
}
}
@@ -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;
@@ -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);
}
}
@@ -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);
}
@@ -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) {
@@ -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;
}
@@ -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);
}
@@ -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 ||
@@ -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;
}
Loading

0 comments on commit 0324b26

Please sign in to comment.