diff --git a/apps/ymesh/ymesh.cpp b/apps/ymesh/ymesh.cpp index fd2314cd2..8880f0e76 100644 --- a/apps/ymesh/ymesh.cpp +++ b/apps/ymesh/ymesh.cpp @@ -893,10 +893,10 @@ float laplacian_weight(const vector& positions, auto num_neighbors = int(adjacencies[node].size()); - int ind = -1; + auto ind = -1; for (auto i : range(adjacencies[node].size())) { if (adjacencies[node][i] == neighbor) { - ind = i; + ind = (int)i; break; } } diff --git a/libs/yocto/CMakeLists.txt b/libs/yocto/CMakeLists.txt index 753997f02..7c012ee20 100644 --- a/libs/yocto/CMakeLists.txt +++ b/libs/yocto/CMakeLists.txt @@ -47,7 +47,7 @@ endif(YOCTO_DENOISE) # warning flags if(APPLE) - target_compile_options(yocto PUBLIC -Wall -Wconversion -Wno-sign-conversion -Wno-implicit-float-conversion) + target_compile_options(yocto PUBLIC -Wall -Wconversion -Wno-sign-conversion -Wno-implicit-float-conversion -Wno-unused-variable) endif(APPLE) if(MSVC) target_compile_options(yocto PUBLIC /D_CRT_SECURE_NO_WARNINGS) diff --git a/libs/yocto/yocto_bvh.cpp b/libs/yocto/yocto_bvh.cpp index b90798c3a..2384679de 100644 --- a/libs/yocto/yocto_bvh.cpp +++ b/libs/yocto/yocto_bvh.cpp @@ -243,12 +243,11 @@ static void build_bvh(vector& nodes, vector& primitives, // prepare primitives primitives.resize(bboxes.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) primitives[idx] = idx; + for (auto idx : range(bboxes.size())) primitives[idx] = (int)idx; // prepare centers auto centers = vector(bboxes.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) - centers[idx] = center(bboxes[idx]); + for (auto idx : range(bboxes.size())) centers[idx] = center(bboxes[idx]); // push first node onto the stack auto stack = vector{{0, 0, (int)bboxes.size()}}; @@ -322,27 +321,27 @@ shape_bvh make_shape_bvh(const shape_data& shape, bool highquality) { auto bboxes = vector{}; if (!shape.points.empty()) { bboxes = vector(shape.points.size()); - for (auto idx = 0; idx < (int)shape.points.size(); idx++) { + for (auto idx : range(shape.points.size())) { auto& point = shape.points[idx]; bboxes[idx] = point_bounds(shape.positions[point], shape.radius[point]); } } else if (!shape.lines.empty()) { bboxes = vector(shape.lines.size()); - for (auto idx = 0; idx < (int)shape.lines.size(); idx++) { + for (auto idx : range(shape.lines.size())) { auto& line = shape.lines[idx]; bboxes[idx] = line_bounds(shape.positions[line.x], shape.positions[line.y], shape.radius[line.x], shape.radius[line.y]); } } else if (!shape.triangles.empty()) { bboxes = vector(shape.triangles.size()); - for (auto idx = 0; idx < (int)shape.triangles.size(); idx++) { + for (auto idx : range(shape.triangles.size())) { auto& triangle = shape.triangles[idx]; bboxes[idx] = triangle_bounds(shape.positions[triangle.x], shape.positions[triangle.y], shape.positions[triangle.z]); } } else if (!shape.quads.empty()) { bboxes = vector(shape.quads.size()); - for (auto idx = 0; idx < (int)shape.quads.size(); idx++) { + for (auto idx : range(shape.quads.size())) { auto& quad = shape.quads[idx]; bboxes[idx] = quad_bounds(shape.positions[quad.x], shape.positions[quad.y], shape.positions[quad.z], @@ -376,7 +375,7 @@ scene_bvh make_scene_bvh( // instance bboxes auto bboxes = vector(scene.instances.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& instance = scene.instances[idx]; auto& sbvh = bvh.shapes[instance.shape]; bboxes[idx] = sbvh.nodes.empty() @@ -396,27 +395,27 @@ void update_shape_bvh(shape_bvh& bvh, const shape_data& shape) { auto bboxes = vector{}; if (!shape.points.empty()) { bboxes = vector(shape.points.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& p = shape.points[idx]; bboxes[idx] = point_bounds(shape.positions[p], shape.radius[p]); } } else if (!shape.lines.empty()) { bboxes = vector(shape.lines.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& l = shape.lines[idx]; bboxes[idx] = line_bounds(shape.positions[l.x], shape.positions[l.y], shape.radius[l.x], shape.radius[l.y]); } } else if (!shape.triangles.empty()) { bboxes = vector(shape.triangles.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& t = shape.triangles[idx]; bboxes[idx] = triangle_bounds( shape.positions[t.x], shape.positions[t.y], shape.positions[t.z]); } } else if (!shape.quads.empty()) { bboxes = vector(shape.quads.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& q = shape.quads[idx]; bboxes[idx] = quad_bounds(shape.positions[q.x], shape.positions[q.y], shape.positions[q.z], shape.positions[q.w]); @@ -436,7 +435,7 @@ void update_scene_bvh(scene_bvh& bvh, const scene_data& scene, // handle instances auto bboxes = vector(scene.instances.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& instance = scene.instances[idx]; auto& sbvh = bvh.shapes[instance.shape]; bboxes[idx] = transform_bbox(instance.frame, sbvh.nodes[0].bbox); @@ -746,7 +745,7 @@ scene_intersection overlap_scene_bvh(const scene_bvh& bvh, auto& shape = scene.shapes[instance_.shape]; auto& sbvh = bvh.shapes[instance_.shape]; auto inv_pos = transform_point(inverse(instance_.frame, true), pos); - auto sintersection = overlap_shape( + auto sintersection = overlap_shape_bvh( sbvh, shape, inv_pos, max_distance, find_any); if (!sintersection.hit) continue; intersection = {primitive, sintersection.element, sintersection.uv, diff --git a/libs/yocto/yocto_geometry.h b/libs/yocto/yocto_geometry.h index 482d67f3c..1c202808b 100644 --- a/libs/yocto/yocto_geometry.h +++ b/libs/yocto/yocto_geometry.h @@ -303,22 +303,6 @@ inline bool intersect_bbox(const ray3f& ray, const bbox3f& bbox); inline bool intersect_bbox( const ray3f& ray, const vec3f& ray_dinv, const bbox3f& bbox); -// Intersect a ray with a point (approximate) -[[deprecated]] inline bool intersect_point( - const ray3f& ray, const vec3f& p, float r, vec2f& uv, float& dist); - -// Intersect a ray with a line -[[deprecated]] inline bool intersect_line(const ray3f& ray, const vec3f& p0, - const vec3f& p1, float r0, float r1, vec2f& uv, float& dist); - -// Intersect a ray with a triangle -[[deprecated]] inline bool intersect_triangle(const ray3f& ray, const vec3f& p0, - const vec3f& p1, const vec3f& p2, vec2f& uv, float& dist); - -// Intersect a ray with a quad. -[[deprecated]] inline bool intersect_quad(const ray3f& ray, const vec3f& p0, - const vec3f& p1, const vec3f& p2, const vec3f& p3, vec2f& uv, float& dist); - } // namespace yocto // ----------------------------------------------------------------------------- @@ -358,26 +342,6 @@ inline bool overlap_bbox(const vec3f& pos, float dist_max, const bbox3f& bbox); // Check if two bboxes overlap. inline bool overlap_bbox(const bbox3f& bbox1, const bbox3f& bbox2); -// Check if a point overlaps a position pos withint a maximum distance dist_max. -[[deprecated]] inline bool overlap_point(const vec3f& pos, float dist_max, - const vec3f& p, float r, vec2f& uv, float& dist); - -// Check if a line overlaps a position pos withint a maximum distance dist_max. -[[deprecated]] inline bool overlap_line(const vec3f& pos, float dist_max, - const vec3f& p0, const vec3f& p1, float r0, float r1, vec2f& uv, - float& dist); - -// Check if a triangle overlaps a position pos withint a maximum distance -// dist_max. -[[deprecated]] inline bool overlap_triangle(const vec3f& pos, float dist_max, - const vec3f& p0, const vec3f& p1, const vec3f& p2, float r0, float r1, - float r2, vec2f& uv, float& dist); - -// Check if a quad overlaps a position pos withint a maximum distance dist_max. -[[deprecated]] inline bool overlap_quad(const vec3f& pos, float dist_max, - const vec3f& p0, const vec3f& p1, const vec3f& p2, const vec3f& p3, - float r0, float r1, float r2, float r3, vec2f& uv, float& dist); - } // namespace yocto // ----------------------------------------------------------------------------- @@ -718,7 +682,7 @@ inline ray3f camera_ray(const frame3f& frame, float lens, float aspect, } // namespace yocto // ----------------------------------------------------------------------------- -// IMPLEMENRTATION OF RAY-PRIMITIVE INTERSECTION FUNCTIONS +// IMPLEMENTATION OF RAY-PRIMITIVE INTERSECTION FUNCTIONS // ----------------------------------------------------------------------------- namespace yocto { @@ -892,165 +856,6 @@ inline bool intersect_bbox( return t0 <= t1; } -// Intersect a ray with a point (approximate) -[[deprecated]] inline bool intersect_point( - const ray3f& ray, const vec3f& p, float r, vec2f& uv, float& dist) { - // find parameter for line-point minimum distance - auto w = p - ray.o; - auto t = dot(w, ray.d) / dot(ray.d, ray.d); - - // exit if not within bounds - if (t < ray.tmin || t > ray.tmax) return false; - - // test for line-point distance vs point radius - auto rp = ray.o + ray.d * t; - auto prp = p - rp; - if (dot(prp, prp) > r * r) return false; - - // intersection occurred: set params and exit - uv = {0, 0}; - dist = t; - return true; -} - -// Intersect a ray with a line -[[deprecated]] inline bool intersect_line(const ray3f& ray, const vec3f& p0, - const vec3f& p1, float r0, float r1, vec2f& uv, float& dist) { - // setup intersection params - auto u = ray.d; - auto v = p1 - p0; - auto w = ray.o - p0; - - // compute values to solve a linear system - auto a = dot(u, u); - auto b = dot(u, v); - auto c = dot(v, v); - auto d = dot(u, w); - auto e = dot(v, w); - auto det = a * c - b * b; - - // check determinant and exit if lines are parallel - // (could use EPSILONS if desired) - if (det == 0) return false; - - // compute Parameters on both ray and segment - auto t = (b * e - c * d) / det; - auto s = (a * e - b * d) / det; - - // exit if not within bounds - if (t < ray.tmin || t > ray.tmax) return false; - - // clamp segment param to segment corners - s = clamp(s, (float)0, (float)1); - - // compute segment-segment distance on the closest points - auto pr = ray.o + ray.d * t; - auto pl = p0 + (p1 - p0) * s; - auto prl = pr - pl; - - // check with the line radius at the same point - auto d2 = dot(prl, prl); - auto r = r0 * (1 - s) + r1 * s; - if (d2 > r * r) return {}; - - // intersection occurred: set params and exit - uv = {s, sqrt(d2) / r}; - dist = t; - return true; -} - -// Intersect a ray with a sphere -[[deprecated]] inline bool intersect_sphere( - const ray3f& ray, const vec3f& p, float r, vec2f& uv, float& dist) { - // compute parameters - auto a = dot(ray.d, ray.d); - auto b = 2 * dot(ray.o - p, ray.d); - auto c = dot(ray.o - p, ray.o - p) - r * r; - - // check discriminant - auto dis = b * b - 4 * a * c; - if (dis < 0) return false; - - // compute ray parameter - auto t = (-b - sqrt(dis)) / (2 * a); - - // exit if not within bounds - if (t < ray.tmin || t > ray.tmax) return false; - - // try other ray parameter - t = (-b + sqrt(dis)) / (2 * a); - - // exit if not within bounds - if (t < ray.tmin || t > ray.tmax) return false; - - // compute local point for uvs - auto plocal = ((ray.o + ray.d * t) - p) / r; - auto u = atan2(plocal.y, plocal.x) / (2 * pif); - if (u < 0) u += 1; - auto v = acos(clamp(plocal.z, -1.0f, 1.0f)) / pif; - - // intersection occurred: set params and exit - uv = {u, v}; - dist = t; - return true; -} - -// Intersect a ray with a triangle -[[deprecated]] inline bool intersect_triangle(const ray3f& ray, const vec3f& p0, - const vec3f& p1, const vec3f& p2, vec2f& uv, float& dist) { - // compute triangle edges - auto edge1 = p1 - p0; - auto edge2 = p2 - p0; - - // compute determinant to solve a linear system - auto pvec = cross(ray.d, edge2); - auto det = dot(edge1, pvec); - - // check determinant and exit if triangle and ray are parallel - // (could use EPSILONS if desired) - if (det == 0) return false; - auto inv_det = 1.0f / det; - - // compute and check first bricentric coordinated - auto tvec = ray.o - p0; - auto u = dot(tvec, pvec) * inv_det; - if (u < 0 || u > 1) return false; - - // compute and check second bricentric coordinated - auto qvec = cross(tvec, edge1); - auto v = dot(ray.d, qvec) * inv_det; - if (v < 0 || u + v > 1) return false; - - // compute and check ray parameter - auto t = dot(edge2, qvec) * inv_det; - if (t < ray.tmin || t > ray.tmax) return false; - - // intersection occurred: set params and exit - uv = {u, v}; - dist = t; - return true; -} - -// Intersect a ray with a quad. -[[deprecated]] inline bool intersect_quad(const ray3f& ray, const vec3f& p0, - const vec3f& p1, const vec3f& p2, const vec3f& p3, vec2f& uv, float& dist) { - if (p2 == p3) { - return intersect_triangle(ray, p0, p1, p3, uv, dist); - } - auto hit = false; - auto tray = ray; - if (intersect_triangle(tray, p0, p1, p3, uv, dist)) { - hit = true; - tray.tmax = dist; - } - if (intersect_triangle(tray, p2, p3, p1, uv, dist)) { - hit = true; - uv = 1 - uv; - tray.tmax = dist; - } - return hit; -} - } // namespace yocto // ----------------------------------------------------------------------------- @@ -1185,13 +990,70 @@ inline bool overlap_bbox(const bbox3f& bbox1, const bbox3f& bbox2) { return true; } +} // namespace yocto + +// ----------------------------------------------------------------------------- +// BACKWARD COMPATIBILITY +// ----------------------------------------------------------------------------- +namespace yocto { + +// Intersect a ray with a point (approximate) +[[deprecated]] inline bool intersect_point( + const ray3f& ray, const vec3f& p, float r, vec2f& uv, float& dist) { + auto intersection = intersect_point(ray, p, r); + if (!intersection.hit) return false; + uv = intersection.uv; + dist = intersection.distance; + return true; +} + +// Intersect a ray with a line +[[deprecated]] inline bool intersect_line(const ray3f& ray, const vec3f& p0, + const vec3f& p1, float r0, float r1, vec2f& uv, float& dist) { + auto intersection = intersect_line(ray, p0, p1, r0, r1); + if (!intersection.hit) return false; + uv = intersection.uv; + dist = intersection.distance; + return true; +} + +// Intersect a ray with a sphere +[[deprecated]] inline bool intersect_sphere( + const ray3f& ray, const vec3f& p, float r, vec2f& uv, float& dist) { + auto intersection = intersect_sphere(ray, p, r); + if (!intersection.hit) return false; + uv = intersection.uv; + dist = intersection.distance; + return true; +} + +// Intersect a ray with a triangle +[[deprecated]] inline bool intersect_triangle(const ray3f& ray, const vec3f& p0, + const vec3f& p1, const vec3f& p2, vec2f& uv, float& dist) { + auto intersection = intersect_triangle(ray, p0, p1, p2); + if (!intersection.hit) return false; + uv = intersection.uv; + dist = intersection.distance; + return true; +} + +// Intersect a ray with a quad. +[[deprecated]] inline bool intersect_quad(const ray3f& ray, const vec3f& p0, + const vec3f& p1, const vec3f& p2, const vec3f& p3, vec2f& uv, float& dist) { + auto intersection = intersect_quad(ray, p0, p1, p2, p3); + if (!intersection.hit) return false; + uv = intersection.uv; + dist = intersection.distance; + return true; +} + // Check if a point overlaps a position pos withint a maximum distance dist_max. [[deprecated]] inline bool overlap_point(const vec3f& pos, float dist_max, const vec3f& p, float r, vec2f& uv, float& dist) { - auto d2 = dot(pos - p, pos - p); - if (d2 > (dist_max + r) * (dist_max + r)) return false; - uv = {0, 0}; - dist = sqrt(d2); + auto intersection = overlap_point(pos, dist_max, p, r); + if (!intersection.hit) return false; + uv = intersection.uv; + dist = intersection.distance; return true; } @@ -1199,16 +1061,10 @@ inline bool overlap_bbox(const bbox3f& bbox1, const bbox3f& bbox2) { [[deprecated]] inline bool overlap_line(const vec3f& pos, float dist_max, const vec3f& p0, const vec3f& p1, float r0, float r1, vec2f& uv, float& dist) { - auto u = closestuv_line(pos, p0, p1); - // Compute projected position from the clamped t d = a + t * ab; - auto p = p0 + (p1 - p0) * u; - auto r = r0 + (r1 - r0) * u; - auto d2 = dot(pos - p, pos - p); - // check distance - if (d2 > (dist_max + r) * (dist_max + r)) return false; - // done - uv = {u, 0}; - dist = sqrt(d2); + auto intersection = overlap_line(pos, dist_max, p0, p1, r0, r1); + if (!intersection.hit) return false; + uv = intersection.uv; + dist = intersection.distance; return true; } @@ -1217,13 +1073,10 @@ inline bool overlap_bbox(const bbox3f& bbox1, const bbox3f& bbox2) { [[deprecated]] inline bool overlap_triangle(const vec3f& pos, float dist_max, const vec3f& p0, const vec3f& p1, const vec3f& p2, float r0, float r1, float r2, vec2f& uv, float& dist) { - auto cuv = closestuv_triangle(pos, p0, p1, p2); - auto p = p0 * (1 - cuv.x - cuv.y) + p1 * cuv.x + p2 * cuv.y; - auto r = r0 * (1 - cuv.x - cuv.y) + r1 * cuv.x + r2 * cuv.y; - auto dd = dot(p - pos, p - pos); - if (dd > (dist_max + r) * (dist_max + r)) return false; - uv = cuv; - dist = sqrt(dd); + auto intersection = overlap_triangle(pos, dist_max, p0, p1, p2, r0, r1, r2); + if (!intersection.hit) return false; + uv = intersection.uv; + dist = intersection.distance; return true; } @@ -1231,20 +1084,12 @@ inline bool overlap_bbox(const bbox3f& bbox1, const bbox3f& bbox2) { [[deprecated]] inline bool overlap_quad(const vec3f& pos, float dist_max, const vec3f& p0, const vec3f& p1, const vec3f& p2, const vec3f& p3, float r0, float r1, float r2, float r3, vec2f& uv, float& dist) { - if (p2 == p3) { - return overlap_triangle(pos, dist_max, p0, p1, p3, r0, r1, r2, uv, dist); - } - auto hit = false; - if (overlap_triangle(pos, dist_max, p0, p1, p3, r0, r1, r2, uv, dist)) { - hit = true; - dist_max = dist; - } - if (!overlap_triangle(pos, dist_max, p2, p3, p1, r2, r3, r1, uv, dist)) { - hit = true; - uv = 1 - uv; - // dist_max = dist; - } - return hit; + auto intersection = overlap_quad( + pos, dist_max, p0, p1, p2, p3, r0, r1, r2, r3); + if (!intersection.hit) return false; + uv = intersection.uv; + dist = intersection.distance; + return true; } } // namespace yocto diff --git a/libs/yocto/yocto_gui.cpp b/libs/yocto/yocto_gui.cpp index e067aceaf..b7789715d 100644 --- a/libs/yocto/yocto_gui.cpp +++ b/libs/yocto/yocto_gui.cpp @@ -494,13 +494,13 @@ void show_image_gui(const string& title, const vector& names, // callbacks auto callbacks = gui_callbacks{}; callbacks.init = [&](const gui_input& input) { - for (auto idx = 0; idx < (int)images.size(); idx++) { + for (auto idx : range(images.size())) { init_image(glimages[idx]); set_image(glimages[idx], displays[idx]); } }; callbacks.clear = [&](const gui_input& input) { - for (auto idx = 0; idx < (int)images.size(); idx++) { + for (auto idx : range(images.size())) { clear_image(glimages[idx]); } }; @@ -627,7 +627,7 @@ void show_trace_gui(const string& title, const string& name, scene_data& scene, // camera names auto camera_names = scene.camera_names; if (camera_names.empty()) { - for (auto idx = 0; idx < (int)scene.cameras.size(); idx++) { + for (auto idx : range(scene.cameras.size())) { camera_names.push_back("camera" + std::to_string(idx + 1)); } } @@ -810,7 +810,7 @@ void show_shade_gui(const string& title, const string& name, scene_data& scene, // camera names auto camera_names = scene.camera_names; if (camera_names.empty()) { - for (auto idx = 0; idx < (int)scene.cameras.size(); idx++) { + for (auto idx : range((int)scene.cameras.size())) { camera_names.push_back("camera" + std::to_string(idx + 1)); } } @@ -1679,7 +1679,7 @@ static void draw_scene(glscene_state& glscene, const scene_data& scene, glUniform3f(glGetUniformLocation(program, "ambient"), 0, 0, 0); glUniform1i(glGetUniformLocation(program, "lights_num"), (int)lights_direction.size()); - for (auto lid = 0; lid < (int)lights_direction.size(); lid++) { + for (auto lid : range((int)lights_direction.size())) { auto is = std::to_string(lid); auto direction = transform_direction(camera.frame, lights_direction[lid]); glUniform3f(glGetUniformLocation( @@ -2232,7 +2232,7 @@ bool draw_gui_combobox(const char* lbl, int& value, if (value < 0) ImGui::SetItemDefaultFocus(); ImGui::PopID(); } - for (auto i = 0; i < (int)labels.size(); i++) { + for (auto i : range((int)labels.size())) { ImGui::PushID(i); if (ImGui::Selectable(labels[i].c_str(), value == i)) value = i; if (value == i) ImGui::SetItemDefaultFocus(); @@ -2252,8 +2252,8 @@ bool draw_gui_combobox(const char* lbl, string& value, if (value.empty()) ImGui::SetItemDefaultFocus(); ImGui::PopID(); } - for (auto i = 0; i < (int)labels.size(); i++) { - ImGui::PushID(i); + for (auto i : range(labels.size())) { + ImGui::PushID((int)i); if (ImGui::Selectable(labels[i].c_str(), value == labels[i])) value = labels[i]; if (value == labels[i]) ImGui::SetItemDefaultFocus(); diff --git a/libs/yocto/yocto_image.cpp b/libs/yocto/yocto_image.cpp index 67fb90be1..7ec164060 100644 --- a/libs/yocto/yocto_image.cpp +++ b/libs/yocto/yocto_image.cpp @@ -882,30 +882,29 @@ vec4f eval_image(const vector& img, int width, int height, // Conversion from/to floats. void byte_to_float(vector& fl, const vector& bt) { fl.resize(bt.size()); - for (auto i = 0; i < (int)fl.size(); i++) fl[i] = byte_to_float(bt[i]); + for (auto i : range(fl.size())) fl[i] = byte_to_float(bt[i]); } void float_to_byte(vector& bt, const vector& fl) { bt.resize(fl.size()); - for (auto i = 0; i < (int)bt.size(); i++) bt[i] = float_to_byte(fl[i]); + for (auto i : range(bt.size())) bt[i] = float_to_byte(fl[i]); } // Conversion between linear and gamma-encoded images. void srgb_to_rgb(vector& rgb, const vector& srgb) { rgb.resize(srgb.size()); - for (auto i = 0; i < (int)rgb.size(); i++) rgb[i] = srgb_to_rgb(srgb[i]); + for (auto i : range(rgb.size())) rgb[i] = srgb_to_rgb(srgb[i]); } void rgb_to_srgb(vector& srgb, const vector& rgb) { srgb.resize(rgb.size()); - for (auto i = 0; i < (int)srgb.size(); i++) srgb[i] = rgb_to_srgb(rgb[i]); + for (auto i : range(srgb.size())) srgb[i] = rgb_to_srgb(rgb[i]); } void srgb_to_rgb(vector& rgb, const vector& srgb) { rgb.resize(srgb.size()); - for (auto i = 0; i < (int)rgb.size(); i++) - rgb[i] = srgb_to_rgb(byte_to_float(srgb[i])); + for (auto i : range(rgb.size())) rgb[i] = srgb_to_rgb(byte_to_float(srgb[i])); } void rgb_to_srgb(vector& srgb, const vector& rgb) { srgb.resize(rgb.size()); - for (auto i = 0; i < (int)srgb.size(); i++) + for (auto i : range(srgb.size())) srgb[i] = float_to_byte(rgb_to_srgb(rgb[i])); } @@ -913,13 +912,13 @@ void rgb_to_srgb(vector& srgb, const vector& rgb) { void tonemap_image(vector& ldr, const vector& hdr, float exposure, bool filmic, bool srgb) { ldr.resize(hdr.size()); - for (auto i = 0; i < (int)hdr.size(); i++) + for (auto i : range(hdr.size())) ldr[i] = tonemap(hdr[i], exposure, filmic, srgb); } void tonemap_image(vector& ldr, const vector& hdr, float exposure, bool filmic, bool srgb) { ldr.resize(hdr.size()); - for (auto i = 0; i < (int)hdr.size(); i++) + for (auto i : range(hdr.size())) ldr[i] = float_to_byte(tonemap(hdr[i], exposure, filmic, srgb)); } @@ -939,7 +938,7 @@ void tonemap_image_mt(vector& ldr, const vector& hdr, void colorgrade_image(vector& corrected, const vector& img, bool linear, const colorgrade_params& params) { corrected.resize(img.size()); - for (auto i = 0; i < (int)img.size(); i++) + for (auto i : range(img.size())) corrected[i] = colorgrade(img[i], linear, params); } @@ -1002,9 +1001,9 @@ void image_difference(vector& diff, const vector& a, if (a.size() != b.size()) throw std::invalid_argument{"image haev different sizes"}; diff.resize(a.size()); - for (auto i = 0; i < (int)diff.size(); i++) diff[i] = abs(a[i] - b[i]); + for (auto i : range(diff.size())) diff[i] = abs(a[i] - b[i]); if (display) { - for (auto i = 0; i < (int)diff.size(); i++) { + for (auto i : range(diff.size())) { auto d = max(diff[i]); diff[i] = {d, d, d, 1}; } diff --git a/libs/yocto/yocto_modelio.cpp b/libs/yocto/yocto_modelio.cpp index 09deb7343..8c44e0157 100644 --- a/libs/yocto/yocto_modelio.cpp +++ b/libs/yocto/yocto_modelio.cpp @@ -203,87 +203,38 @@ static bool path_exists(const string& filename) { // ----------------------------------------------------------------------------- namespace yocto { -[[maybe_unused]] static array neg(const array& a) { +static array neg(const array& a) { return {-a[0], -a[1], -a[2]}; } -[[maybe_unused]] static array add( - const array& a, const array& b) { - return {a[0] + b[0], a[1] + b[1], a[2] + b[2]}; -} -[[maybe_unused]] static array add(const array& a, float b) { - return {a[0] + b, a[1] + b, a[2] + b}; -} -[[maybe_unused]] static array add(float a, const array& b) { - return {a + b[0], a + b[1], a + b[2]}; -} -[[maybe_unused]] static array sub( - const array& a, const array& b) { +static array sub(const array& a, const array& b) { return {a[0] - b[0], a[1] - b[1], a[2] - b[2]}; } -[[maybe_unused]] static array sub(const array& a, float b) { - return {a[0] - b, a[1] - b, a[2] - b}; -} -[[maybe_unused]] static array sub(float a, const array& b) { - return {a - b[0], a - b[1], a - b[2]}; -} -[[maybe_unused]] static array mul( - const array& a, const array& b) { - return {a[0] * b[0], a[1] * b[1], a[2] * b[2]}; -} -[[maybe_unused]] static array mul(const array& a, float b) { - return {a[0] * b, a[1] * b, a[2] * b}; -} -[[maybe_unused]] static array mul(float a, const array& b) { - return {a * b[0], a * b[1], a * b[2]}; -} -[[maybe_unused]] static array div( - const array& a, const array& b) { - return {a[0] / b[0], a[1] / b[1], a[2] / b[2]}; -} -[[maybe_unused]] static array div(const array& a, float b) { +static array div(const array& a, float b) { return {a[0] / b, a[1] / b, a[2] / b}; } -[[maybe_unused]] static array div(float a, const array& b) { - return {a / b[0], a / b[1], a / b[2]}; -} -[[maybe_unused]] static float dot( - const array& a, const array& b) { +static float dot(const array& a, const array& b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; } -[[maybe_unused]] static array cross( +static array cross( const array& a, const array& b) { return {a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]}; } -[[maybe_unused]] static float length(const array& a) { - return sqrt(dot(a, a)); -} -[[maybe_unused]] static float length_squared(const array& a) { - return dot(a, a); -} -[[maybe_unused]] static array normalize(const array& a) { +static float length(const array& a) { return sqrt(dot(a, a)); } +static array normalize(const array& a) { auto l = length(a); return (l != 0) ? div(a, l) : a; } -[[maybe_unused]] static float distance( - const array& a, const array& b) { - return length(sub(a, b)); -} -[[maybe_unused]] static float distance_squared( - const array& a, const array& b) { - return dot(sub(a, b), sub(a, b)); -} -[[maybe_unused]] static array triangle_normal( - const array& p0, const array& p1, - const array& p2) { +static array triangle_normal(const array& p0, + const array& p1, const array& p2) { return normalize(cross(sub(p1, p0), sub(p2, p0))); } -[[maybe_unused]] static array, 4> lookat_frame( - const array& eye, const array& center, - const array& up, bool inv_xz = false) { +static array, 4> lookat_frame(const array& eye, + const array& center, const array& up, + bool inv_xz = false) { auto w = normalize(sub(eye, center)); auto u = normalize(cross(up, w)); auto v = normalize(cross(w, u)); @@ -294,103 +245,9 @@ namespace yocto { return {u, v, w, eye}; } -static array, 3> mul( - const array, 3>& a, float b) { - return {mul(a[0], b), mul(a[1], b), mul(a[2], b)}; -} -static array mul( - const array, 3>& a, const array& b) { - return add(mul(a[0], b[0]), add(mul(a[1], b[1]), mul(a[2], b[2]))); -} -[[maybe_unused]] static array, 3> mul( - const array, 3>& a, const array, 3>& b) { - return {mul(a, b[0]), mul(a, b[1]), mul(a, b[2])}; -} -static array, 4> mul( - const array, 4>& a, const array, 4>& b) { - auto al = array, 3>{a[0], a[1], a[2]}; - return { - mul(al, b[0]), mul(al, b[1]), mul(al, b[2]), add(mul(al, b[3]), a[3])}; -} - -static array, 4> translation_frame(const array& a) { - return {array{1, 0, 0}, array{0, 1, 0}, - array{0, 0, 1}, a}; -} -static array, 4> scaling_frame(const array& a) { - return {array{a[0], 0, 0}, array{0, a[1], 0}, - array{0, 0, a[2]}, array{0, 0, 0}}; -} -static array, 4> rotation_frame( - const array& axis, float angle) { - auto s = std::sin(angle), c = std::cos(angle); - auto vv = normalize(axis); - return {array{c + (1 - c) * vv[0] * vv[0], - (1 - c) * vv[0] * vv[1] + s * vv[2], - (1 - c) * vv[0] * vv[2] - s * vv[1]}, - array{(1 - c) * vv[0] * vv[1] - s * vv[2], - c + (1 - c) * vv[1] * vv[1], (1 - c) * vv[1] * vv[2] + s * vv[0]}, - array{(1 - c) * vv[0] * vv[2] + s * vv[1], - (1 - c) * vv[1] * vv[2] - s * vv[0], c + (1 - c) * vv[2] * vv[2]}, - array{0, 0, 0}}; -} - -static array, 3> transpose(const array, 3>& a) { - return { - array{a[0][0], a[1][0], a[2][0]}, - array{a[0][1], a[1][1], a[2][1]}, - array{a[0][2], a[1][2], a[2][2]}, - }; -} -static float determinant(const array, 3>& a) { - return dot(a[0], cross(a[1], a[2])); -} -static array, 3> adjoint(const array, 3>& a) { - return transpose(array, 3>{ - cross(a[1], a[2]), cross(a[2], a[0]), cross(a[0], a[1])}); -} -static array, 3> inverse(const array, 3>& a) { - return mul(adjoint(a), (1 / determinant(a))); -} - -static array, 4> inverse( - const array, 4>& a, bool non_rigid = false) { - auto m = array, 3>{a[0], a[1], a[2]}; - auto minv = non_rigid ? inverse(m) : transpose(m); - return {minv[0], minv[1], minv[2], neg(mul(minv, a[3]))}; -} - -// frame/mat conversion -static array, 4> mat_to_frame( - const array, 4>& m) { - return {array{m[0][0], m[0][1], m[0][2]}, - array{m[1][0], m[1][1], m[1][2]}, - array{m[2][0], m[2][1], m[2][2]}, - array{m[3][0], m[3][1], m[3][2]}}; -} -static array, 4> frame_to_mat( - const array, 4>& f) { - return {array{f[0][0], f[0][1], f[0][2], 0}, - array{f[1][0], f[1][1], f[1][2], 0}, - array{f[2][0], f[2][1], f[2][2], 0}, - array{f[3][0], f[3][1], f[3][2], 1}}; -} - static array flatten(const array, 4>& a) { return (const array&)a; } -static array, 4> unflatten(const array& a) { - return (const array, 4>&)a; -} - -[[maybe_unused]] static array flatten( - const array, 4>& a) { - return (const array&)a; -} -[[maybe_unused]] static array, 4> unflatten( - const array& a) { - return (const array, 4>&)a; -} } // namespace yocto @@ -1643,10 +1500,6 @@ bool load_obj(const string& filename, obj_shape& shape, string& error, } // Format values -[[maybe_unused]] static void format_value( - string& str, const obj_texture& value) { - str += value.path.empty() ? "" : value.path; -} static void format_value(string& str, const obj_vertex& value) { format_value(str, value.position); if (value.texcoord != 0) { diff --git a/libs/yocto/yocto_pbrtio.cpp b/libs/yocto/yocto_pbrtio.cpp index 25bda2e04..209edf895 100644 --- a/libs/yocto/yocto_pbrtio.cpp +++ b/libs/yocto/yocto_pbrtio.cpp @@ -120,45 +120,6 @@ static bool save_text( return true; } -// Load a binary file -static bool load_binary( - const string& filename, vector& data, string& error) { - // https://stackoverflow.com/questions/174531/how-to-read-the-content-of-a-file-to-a-string-in-c - auto fs = fopen_utf8(filename.c_str(), "rb"); - if (!fs) { - error = "cannot open " + filename; - return false; - } - fseek(fs, 0, SEEK_END); - auto length = ftell(fs); - fseek(fs, 0, SEEK_SET); - data.resize(length); - if (fread(data.data(), 1, length, fs) != length) { - fclose(fs); - error = "cannot read " + filename; - return false; - } - fclose(fs); - return true; -} - -// Save a binary file -static bool save_binary( - const string& filename, const vector& data, string& error) { - auto fs = fopen_utf8(filename.c_str(), "wb"); - if (!fs) { - error = "cannot create " + filename; - return false; - } - if (fwrite(data.data(), 1, data.size(), fs) != data.size()) { - fclose(fs); - error = "cannot write " + filename; - return false; - } - fclose(fs); - return true; -} - } // namespace yocto // ----------------------------------------------------------------------------- @@ -186,16 +147,6 @@ static string path_join(const string& patha, const string& pathb) { return (make_path(patha) / make_path(pathb)).generic_u8string(); } -// Replaces extensions -static string replace_extension(const string& filename, const string& ext) { - return make_path(filename).replace_extension(ext).u8string(); -} - -// Check if a file can be opened for reading. -static bool path_exists(const string& filename) { - return exists(make_path(filename)); -} - } // namespace yocto // ----------------------------------------------------------------------------- @@ -203,87 +154,39 @@ static bool path_exists(const string& filename) { // ----------------------------------------------------------------------------- namespace yocto { -[[maybe_unused]] static array neg(const array& a) { +static array neg(const array& a) { return {-a[0], -a[1], -a[2]}; } -[[maybe_unused]] static array add( - const array& a, const array& b) { +static array add(const array& a, const array& b) { return {a[0] + b[0], a[1] + b[1], a[2] + b[2]}; } -[[maybe_unused]] static array add(const array& a, float b) { - return {a[0] + b, a[1] + b, a[2] + b}; -} -[[maybe_unused]] static array add(float a, const array& b) { - return {a + b[0], a + b[1], a + b[2]}; -} -[[maybe_unused]] static array sub( - const array& a, const array& b) { +static array sub(const array& a, const array& b) { return {a[0] - b[0], a[1] - b[1], a[2] - b[2]}; } -[[maybe_unused]] static array sub(const array& a, float b) { - return {a[0] - b, a[1] - b, a[2] - b}; -} -[[maybe_unused]] static array sub(float a, const array& b) { - return {a - b[0], a - b[1], a - b[2]}; -} -[[maybe_unused]] static array mul( - const array& a, const array& b) { - return {a[0] * b[0], a[1] * b[1], a[2] * b[2]}; -} -[[maybe_unused]] static array mul(const array& a, float b) { +static array mul(const array& a, float b) { return {a[0] * b, a[1] * b, a[2] * b}; } -[[maybe_unused]] static array mul(float a, const array& b) { - return {a * b[0], a * b[1], a * b[2]}; -} -[[maybe_unused]] static array div( - const array& a, const array& b) { - return {a[0] / b[0], a[1] / b[1], a[2] / b[2]}; -} -[[maybe_unused]] static array div(const array& a, float b) { +static array div(const array& a, float b) { return {a[0] / b, a[1] / b, a[2] / b}; } -[[maybe_unused]] static array div(float a, const array& b) { - return {a / b[0], a / b[1], a / b[2]}; -} -[[maybe_unused]] static float dot( - const array& a, const array& b) { +static float dot(const array& a, const array& b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; } -[[maybe_unused]] static array cross( +static array cross( const array& a, const array& b) { return {a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]}; } -[[maybe_unused]] static float length(const array& a) { - return sqrt(dot(a, a)); -} -[[maybe_unused]] static float length_squared(const array& a) { - return dot(a, a); -} -[[maybe_unused]] static array normalize(const array& a) { +static float length(const array& a) { return sqrt(dot(a, a)); } +static array normalize(const array& a) { auto l = length(a); return (l != 0) ? div(a, l) : a; } -[[maybe_unused]] static float distance( - const array& a, const array& b) { - return length(sub(a, b)); -} -[[maybe_unused]] static float distance_squared( - const array& a, const array& b) { - return dot(sub(a, b), sub(a, b)); -} - -[[maybe_unused]] static array triangle_normal( - const array& p0, const array& p1, - const array& p2) { - return normalize(cross(sub(p1, p0), sub(p2, p0))); -} -[[maybe_unused]] static array, 4> lookat_frame( - const array& eye, const array& center, - const array& up, bool inv_xz = false) { +static array, 4> lookat_frame(const array& eye, + const array& center, const array& up, + bool inv_xz = false) { auto w = normalize(sub(eye, center)); auto u = normalize(cross(up, w)); auto v = normalize(cross(w, u)); @@ -302,10 +205,6 @@ static array mul( const array, 3>& a, const array& b) { return add(mul(a[0], b[0]), add(mul(a[1], b[1]), mul(a[2], b[2]))); } -[[maybe_unused]] static array, 3> mul( - const array, 3>& a, const array, 3>& b) { - return {mul(a, b[0]), mul(a, b[1]), mul(a, b[2])}; -} static array, 4> mul( const array, 4>& a, const array, 4>& b) { auto al = array, 3>{a[0], a[1], a[2]}; @@ -383,12 +282,7 @@ static array, 4> unflatten(const array& a) { return (const array, 4>&)a; } -[[maybe_unused]] static array flatten( - const array, 4>& a) { - return (const array&)a; -} -[[maybe_unused]] static array, 4> unflatten( - const array& a) { +static array, 4> unflatten(const array& a) { return (const array, 4>&)a; } @@ -449,9 +343,6 @@ static void format_value(string& str, T value) { str.append(buffer.data(), result.ptr); } } -static void format_value(string& str, bool value) { - format_value(str, value ? 1 : 0); -} template static void format_value(string& str, const array& value) { for (auto i = 0; i < N; i++) { @@ -568,12 +459,6 @@ template } return true; } -[[nodiscard]] static bool parse_value(string_view& str, bool& value) { - auto valuei = 0; - if (!parse_value(str, valuei)) return false; - value = (bool)valuei; - return true; -} template [[nodiscard]] static bool parse_value(string_view& str, array& value) { for (auto i = 0; i < N; i++) @@ -704,15 +589,6 @@ static bool get_pbrt_value(const pbrt_value& pbrt, float& val) { return false; } } -[[maybe_unused]] static bool get_pbrt_value( - const pbrt_value& pbrt, array& val) { - if (pbrt.type == pbrt_type::point2 || pbrt.type == pbrt_type::vector2) { - val = pbrt.value2f; - return true; - } else { - return false; - } -} static bool get_pbrt_value(const pbrt_value& pbrt, array& val) { if (pbrt.type == pbrt_type::point || pbrt.type == pbrt_type::vector || pbrt.type == pbrt_type::normal || pbrt.type == pbrt_type::color) { @@ -725,19 +601,6 @@ static bool get_pbrt_value(const pbrt_value& pbrt, array& val) { return false; } } -[[maybe_unused]] static bool get_pbrt_value( - const pbrt_value& pbrt, vector& val) { - if (pbrt.type == pbrt_type::real) { - if (!pbrt.vector1f.empty()) { - val = pbrt.vector1f; - } else { - val = {pbrt.value1f}; - } - return true; - } else { - return false; - } -} static bool get_pbrt_value( const pbrt_value& pbrt, vector>& val) { if (pbrt.type == pbrt_type::point2 || pbrt.type == pbrt_type::vector2) { @@ -779,19 +642,6 @@ static bool get_pbrt_value( } } -[[maybe_unused]] static bool get_pbrt_value( - const pbrt_value& pbrt, vector& val) { - if (pbrt.type == pbrt_type::integer) { - if (!pbrt.vector1i.empty()) { - val = pbrt.vector1i; - } else { - val = {pbrt.vector1i}; - } - return true; - } else { - return false; - } -} static bool get_pbrt_value(const pbrt_value& pbrt, vector>& val) { if (pbrt.type == pbrt_type::integer) { if (pbrt.vector1i.empty() || (pbrt.vector1i.size() % 3) != 0) return false; @@ -867,14 +717,6 @@ static pbrt_value make_pbrt_value( pbrt.value1f = val; return pbrt; } -[[maybe_unused]] static pbrt_value make_pbrt_value(const string& name, - const array& val, pbrt_type type = pbrt_type::point2) { - auto pbrt = pbrt_value{}; - pbrt.name = name; - pbrt.type = type; - pbrt.value2f = val; - return pbrt; -} static pbrt_value make_pbrt_value(const string& name, const array& val, pbrt_type type = pbrt_type::color) { auto pbrt = pbrt_value{}; diff --git a/libs/yocto/yocto_scene.cpp b/libs/yocto/yocto_scene.cpp index 0655288ae..a52035cfe 100644 --- a/libs/yocto/yocto_scene.cpp +++ b/libs/yocto/yocto_scene.cpp @@ -646,20 +646,20 @@ void add_sky(scene_data& scene, float sun_angle) { int find_camera(const scene_data& scene, const string& name) { if (scene.cameras.empty()) return invalidid; if (scene.camera_names.empty()) return 0; - for (auto idx = 0; idx < (int)scene.camera_names.size(); idx++) { - if (scene.camera_names[idx] == name) return idx; + for (auto idx : range(scene.camera_names.size())) { + if (scene.camera_names[idx] == name) return (int)idx; } - for (auto idx = 0; idx < (int)scene.camera_names.size(); idx++) { - if (scene.camera_names[idx] == "default") return idx; + for (auto idx : range(scene.camera_names.size())) { + if (scene.camera_names[idx] == "default") return (int)idx; } - for (auto idx = 0; idx < (int)scene.camera_names.size(); idx++) { - if (scene.camera_names[idx] == "camera") return idx; + for (auto idx : range(scene.camera_names.size())) { + if (scene.camera_names[idx] == "camera") return (int)idx; } - for (auto idx = 0; idx < (int)scene.camera_names.size(); idx++) { - if (scene.camera_names[idx] == "camera0") return idx; + for (auto idx : range(scene.camera_names.size())) { + if (scene.camera_names[idx] == "camera0") return (int)idx; } - for (auto idx = 0; idx < (int)scene.camera_names.size(); idx++) { - if (scene.camera_names[idx] == "camera1") return idx; + for (auto idx : range(scene.camera_names.size())) { + if (scene.camera_names[idx] == "camera1") return (int)idx; } return 0; } @@ -917,7 +917,7 @@ vector scene_validation(const scene_data& scene, bool notextures) { } }; auto check_empty_textures = [&errs](const scene_data& scene) { - for (auto idx = 0; idx < (int)scene.textures.size(); idx++) { + for (auto idx : range(scene.textures.size())) { auto& texture = scene.textures[idx]; if (texture.pixelsf.empty() && texture.pixelsb.empty()) { errs.push_back("empty texture " + scene.texture_names[idx]); @@ -1059,8 +1059,8 @@ namespace yocto { // Find the first keyframe value that is greater than the argument. inline int keyframe_index(const vector& times, const float& time) { - for (auto i = 0; i < (int)times.size(); i++) - if (times[i] > time) return i; + for (auto i : range(times.size())) + if (times[i] > time) return (int)i; return (int)times.size(); } diff --git a/libs/yocto/yocto_sceneio.cpp b/libs/yocto/yocto_sceneio.cpp index b69d730fb..36abc27a2 100644 --- a/libs/yocto/yocto_sceneio.cpp +++ b/libs/yocto/yocto_sceneio.cpp @@ -2422,7 +2422,7 @@ scene_data make_test(const test_params& params) { // }; // } break; // } - // for (auto idx = 0; idx < shapes.size(); idx++) { + // for (auto idx : range(shapes.size())) { // if (!shapes[idx]) continue; // if (shapes.size() > 5) { // add_instance(scene, @@ -4560,7 +4560,7 @@ static bool load_gltf_scene( } } else if (gprimitive.type == cgltf_primitive_type_line_loop) { shape.lines.resize(indices.size()); - for (auto i = 0; i < (int)indices.size(); i++) { + for (auto i : range(indices.size())) { shape.lines[i] = { indices[i + 0], indices[i + 1] % (int)indices.size()}; } @@ -4803,7 +4803,7 @@ static bool save_gltf_scene(const string& filename, const scene_data& scene, for (auto idx : range(scene.shapes.size())) { auto& shape = scene.shapes[idx]; auto& gbuffer = cgltf.buffers[idx]; - shape_accessor_start[idx] = cgltf.accessors_count; + shape_accessor_start[idx] = (int)cgltf.accessors_count; gbuffer.uri = copy_string( "shapes/" + get_shape_name(scene, shape) + ".bin"); add_vertex(cgltf, gbuffer, shape.positions.size(), cgltf_type_vec3, @@ -5554,8 +5554,8 @@ image filter_bilateral(const image& img, float spatial_sigma, auto fw = vector(); for (auto feature_sigma : features_sigma) fw.push_back(1 / (2.0f * feature_sigma * feature_sigma)); - for (auto j = 0; j < img.height(); j++) { - for (auto i = 0; i < img.width(); i++) { + for (auto j : range(img.height())) { + for (auto i : range(img.width())) { auto av = zero4f; auto aw = 0.0f; for (auto fj = -filter_width; fj <= filter_width; fj++) { @@ -5567,7 +5567,7 @@ image filter_bilateral(const image& img, float spatial_sigma, auto rgb = img[{i, j}] - img[{i, j}]; auto w = (float)exp(-dot(uv, uv) * sw) * (float)exp(-dot(rgb, rgb) * rw); - for (auto fi = 0; fi < features.size(); fi++) { + for (auto fi : range(features.size())) { auto feat = features[fi][{i, j}] - features[fi][{i, j}]; w *= exp(-dot(feat, feat) * fw[fi]); } @@ -5587,8 +5587,8 @@ image filter_bilateral( auto fwidth = (int)ceil(2.57f * spatial_sigma); auto sw = 1 / (2.0f * spatial_sigma * spatial_sigma); auto rw = 1 / (2.0f * range_sigma * range_sigma); - for (auto j = 0; j < img.height(); j++) { - for (auto i = 0; i < img.width(); i++) { + for (auto j : range(img.height())) { + for (auto i : range(img.width())) { auto av = zero4f; auto aw = 0.0f; for (auto fj = -fwidth; fj <= fwidth; fj++) { diff --git a/libs/yocto/yocto_shape.cpp b/libs/yocto/yocto_shape.cpp index 9a470a260..6e78f64aa 100644 --- a/libs/yocto/yocto_shape.cpp +++ b/libs/yocto/yocto_shape.cpp @@ -607,7 +607,7 @@ shape_data make_bulged_rect(const vec2i& steps, const vec2f& scale, height = min(height, min(scale)); auto radius = (1 + height * height) / (2 * height); auto center = vec3f{0, 0, -radius + height}; - for (auto i = 0; i < (int)shape.positions.size(); i++) { + for (auto i : range(shape.positions.size())) { auto pn = normalize(shape.positions[i] - center); shape.positions[i] = center + pn * radius; shape.normals[i] = pn; @@ -666,7 +666,7 @@ shape_data make_box( // + y qshape = make_rect( {steps.x, steps.z}, {scale.x, scale.z}, {uvscale.x, uvscale.z}); - for (auto i = 0; i < (int)qshape.positions.size(); i++) { + for (auto i : range(qshape.positions.size())) { qshape.positions[i] = { qshape.positions[i].x, scale.y, -qshape.positions[i].y}; qshape.normals[i] = {0, 1, 0}; @@ -675,7 +675,7 @@ shape_data make_box( // - y qshape = make_rect( {steps.x, steps.z}, {scale.x, scale.z}, {uvscale.x, uvscale.z}); - for (auto i = 0; i < (int)qshape.positions.size(); i++) { + for (auto i : range(qshape.positions.size())) { qshape.positions[i] = { qshape.positions[i].x, -scale.y, qshape.positions[i].y}; qshape.normals[i] = {0, -1, 0}; @@ -689,7 +689,7 @@ shape_data make_rounded_box(const vec3i& steps, const vec3f& scale, if (radius != 0) { radius = min(radius, min(scale)); auto c = scale - radius; - for (auto i = 0; i < (int)shape.positions.size(); i++) { + for (auto i : range(shape.positions.size())) { auto pc = vec3f{abs(shape.positions[i].x), abs(shape.positions[i].y), abs(shape.positions[i].z)}; auto ps = vec3f{shape.positions[i].x < 0 ? -1.0f : 1.0f, @@ -751,7 +751,7 @@ shape_data make_bent_floor(const vec2i& steps, const vec2f& scale, radius = min(radius, scale.y); auto start = (scale.y - radius) / 2; auto end = start + radius; - for (auto i = 0; i < (int)shape.positions.size(); i++) { + for (auto i : range(shape.positions.size())) { if (shape.positions[i].z < -end) { shape.positions[i] = { shape.positions[i].x, -shape.positions[i].z - end + radius, -end}; @@ -783,7 +783,7 @@ shape_data make_sphere(int steps, float scale, float uvscale) { shape_data make_uvsphere( const vec2i& steps, float scale, const vec2f& uvscale) { auto shape = make_rect({1, 1}, {1, 1}); - for (auto i = 0; i < (int)shape.positions.size(); i++) { + for (auto i : range(shape.positions.size())) { auto uv = shape.texcoords[i]; auto a = vec2f{2 * pif * uv.x, pif * (1 - uv.y)}; shape.positions[i] = @@ -814,7 +814,7 @@ shape_data make_capped_uvsphere( if (cap != 0) { cap = min(cap, scale / 2); auto zflip = (scale - cap); - for (auto i = 0; i < (int)shape.positions.size(); i++) { + for (auto i : range(shape.positions.size())) { if (shape.positions[i].z > zflip) { shape.positions[i].z = 2 * zflip - shape.positions[i].z; shape.normals[i].x = -shape.normals[i].x; @@ -864,7 +864,7 @@ shape_data make_bulged_disk( height = min(height, scale); auto radius = (1 + height * height) / (2 * height); auto center = vec3f{0, 0, -radius + height}; - for (auto i = 0; i < (int)shape.positions.size(); i++) { + for (auto i : range(shape.positions.size())) { auto pn = normalize(shape.positions[i] - center); shape.positions[i] = center + pn * radius; shape.normals[i] = pn; @@ -876,7 +876,7 @@ shape_data make_bulged_disk( // Make a uv disk shape_data make_uvdisk(const vec2i& steps, float scale, const vec2f& uvscale) { auto shape = make_rect(steps, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)shape.positions.size(); i++) { + for (auto i : range(shape.positions.size())) { auto uv = shape.texcoords[i]; auto phi = 2 * pif * uv.x; shape.positions[i] = vec3f{cos(phi) * uv.y, sin(phi) * uv.y, 0} * scale; @@ -893,7 +893,7 @@ shape_data make_uvcylinder( auto qshape = shape_data{}; // side qshape = make_rect({steps.x, steps.y}, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)qshape.positions.size(); i++) { + for (auto i : range(qshape.positions.size())) { auto uv = qshape.texcoords[i]; auto phi = 2 * pif * uv.x; qshape.positions[i] = { @@ -905,7 +905,7 @@ shape_data make_uvcylinder( merge_shape_inplace(shape, qshape); // top qshape = make_rect({steps.x, steps.z}, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)qshape.positions.size(); i++) { + for (auto i : range(qshape.positions.size())) { auto uv = qshape.texcoords[i]; auto phi = 2 * pif * uv.x; qshape.positions[i] = { @@ -917,7 +917,7 @@ shape_data make_uvcylinder( merge_shape_inplace(shape, qshape); // bottom qshape = make_rect({steps.x, steps.z}, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)qshape.positions.size(); i++) { + for (auto i : range(qshape.positions.size())) { auto uv = qshape.texcoords[i]; auto phi = 2 * pif * uv.x; qshape.positions[i] = { @@ -939,7 +939,7 @@ shape_data make_rounded_uvcylinder(const vec3i& steps, const vec2f& scale, if (radius != 0) { radius = min(radius, min(scale)); auto c = scale - radius; - for (auto i = 0; i < (int)shape.positions.size(); i++) { + for (auto i : range(shape.positions.size())) { auto phi = atan2(shape.positions[i].y, shape.positions[i].x); auto r = length(vec2f{shape.positions[i].x, shape.positions[i].y}); auto z = shape.positions[i].z; @@ -1019,7 +1019,7 @@ shape_data make_points(int num, float uvscale, float radius) { shape.normals.assign(num, {0, 0, 1}); shape.texcoords.assign(num, {0, 0}); shape.radius.assign(num, radius); - for (auto i = 0; i < (int)shape.texcoords.size(); i++) + for (auto i : range(shape.texcoords.size())) shape.texcoords[i] = {(float)i / (float)num, 0}; return shape; } @@ -1029,9 +1029,9 @@ shape_data make_points(const vec2i& steps, const vec2f& size, auto shape = make_rect(steps, size, uvscale); shape.quads = {}; shape.points.resize(shape.positions.size()); - for (auto i = 0; i < (int)shape.positions.size(); i++) shape.points[i] = i; + for (auto i : range(shape.positions.size())) shape.points[i] = (int)i; shape.radius.resize(shape.positions.size()); - for (auto i = 0; i < (int)shape.texcoords.size(); i++) { + for (auto i : range(shape.texcoords.size())) { shape.radius[i] = lerp( radius.x, radius.y, shape.texcoords[i].y / uvscale.y); } @@ -1282,10 +1282,10 @@ shape_data make_hair(const shape_data& base, const vec2i& steps, auto cidx = vector(); if (clump.x > 0) { - for (auto bidx = 0; bidx < (int)bpos.size(); bidx++) { + for (auto bidx : range((int)bpos.size())) { cidx.push_back(0); auto cdist = flt_max; - for (auto c : range(clump.y)) { + for (auto c : range((int)clump.y)) { auto d = length(bpos[bidx] - bpos[c]); if (d < cdist) { cdist = d; @@ -1296,7 +1296,7 @@ shape_data make_hair(const shape_data& base, const vec2i& steps, } auto shape = make_lines(steps, {1, 1}, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)shape.positions.size(); i++) { + for (auto i : range((int)shape.positions.size())) { auto u = shape.texcoords[i].x; auto bidx = i / (steps.x + 1); shape.positions[i] = bpos[bidx] + bnorm[bidx] * u * blen[bidx]; @@ -1585,7 +1585,7 @@ vector triangles_tangent_spaces(const vector& triangles, auto tangent_spaces = vector(positions.size()); for (auto& tangent : tangent_spaces) tangent = zero4f; - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { tangu[i] = orthonormalize(tangu[i], normals[i]); auto s = (dot(cross(normals[i], tangu[i]), tangv[i]) < 0) ? -1.0f : 1.0f; tangent_spaces[i] = {tangu[i].x, tangu[i].y, tangu[i].z, s}; @@ -1599,14 +1599,14 @@ pair, vector> skin_vertices(const vector& positions, const vector& joints, const vector& xforms) { auto skinned_positions = vector{positions.size()}; auto skinned_normals = vector{positions.size()}; - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { skinned_positions[i] = transform_point(xforms[joints[i].x], positions[i]) * weights[i].x + transform_point(xforms[joints[i].y], positions[i]) * weights[i].y + transform_point(xforms[joints[i].z], positions[i]) * weights[i].z + transform_point(xforms[joints[i].w], positions[i]) * weights[i].w; } - for (auto i = 0; i < (int)normals.size(); i++) { + for (auto i : range(normals.size())) { skinned_normals[i] = normalize( transform_direction(xforms[joints[i].x], normals[i]) * weights[i].x + transform_direction(xforms[joints[i].y], normals[i]) * weights[i].y + @@ -1622,7 +1622,7 @@ pair, vector> skin_matrices(const vector& positions, const vector& joints, const vector& xforms) { auto skinned_positions = vector{positions.size()}; auto skinned_normals = vector{positions.size()}; - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { auto xform = xforms[joints[i].x] * weights[i].x + xforms[joints[i].y] * weights[i].y + xforms[joints[i].z] * weights[i].z + @@ -1642,14 +1642,14 @@ void skin_vertices(vector& skinned_positions, skinned_normals.size() != normals.size()) { throw std::out_of_range("arrays should be the same size"); } - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { skinned_positions[i] = transform_point(xforms[joints[i].x], positions[i]) * weights[i].x + transform_point(xforms[joints[i].y], positions[i]) * weights[i].y + transform_point(xforms[joints[i].z], positions[i]) * weights[i].z + transform_point(xforms[joints[i].w], positions[i]) * weights[i].w; } - for (auto i = 0; i < (int)normals.size(); i++) { + for (auto i : range(normals.size())) { skinned_normals[i] = normalize( transform_direction(xforms[joints[i].x], normals[i]) * weights[i].x + transform_direction(xforms[joints[i].y], normals[i]) * weights[i].y + @@ -1667,7 +1667,7 @@ void skin_matrices(vector& skinned_positions, skinned_normals.size() != normals.size()) { throw std::out_of_range("arrays should be the same size"); } - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { auto xform = xforms[joints[i].x] * weights[i].x + xforms[joints[i].y] * weights[i].y + xforms[joints[i].z] * weights[i].z + @@ -1964,12 +1964,12 @@ vector> ordered_boundaries(const vector& triangles, auto boundaries = vector>(); // arrange boundary vertices in loops - for (auto i = 0; i < (int)next_vert.size(); i++) { + for (auto i : range(next_vert.size())) { if (next_vert[i] == -1) continue; // add new empty boundary boundaries.emplace_back(); - auto current = i; + auto current = (int)i; while (true) { auto next = next_vert[current]; @@ -2051,12 +2051,11 @@ static void build_bvh(bvh_tree& bvh, vector& bboxes) { // prepare primitives bvh.primitives.resize(bboxes.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) bvh.primitives[idx] = idx; + for (auto idx : range(bboxes.size())) bvh.primitives[idx] = (int)idx; // prepare centers auto centers = vector(bboxes.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) - centers[idx] = center(bboxes[idx]); + for (auto idx : range(bboxes.size())) centers[idx] = center(bboxes[idx]); // queue up first node auto queue = deque{{0, 0, (int)bboxes.size()}}; @@ -2125,7 +2124,7 @@ bvh_tree make_points_bvh(const vector& points, const vector& positions, const vector& radius) { // build primitives auto bboxes = vector(points.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& p = points[idx]; bboxes[idx] = point_bounds(positions[p], radius[p]); } @@ -2139,7 +2138,7 @@ bvh_tree make_lines_bvh(const vector& lines, const vector& positions, const vector& radius) { // build primitives auto bboxes = vector(lines.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& l = lines[idx]; bboxes[idx] = line_bounds( positions[l.x], positions[l.y], radius[l.x], radius[l.y]); @@ -2154,7 +2153,7 @@ bvh_tree make_triangles_bvh(const vector& triangles, const vector& positions, const vector& radius) { // build primitives auto bboxes = vector(triangles.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& t = triangles[idx]; bboxes[idx] = triangle_bounds( positions[t.x], positions[t.y], positions[t.z]); @@ -2169,7 +2168,7 @@ bvh_tree make_quads_bvh(const vector& quads, const vector& positions, const vector& radius) { // build primitives auto bboxes = vector(quads.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& q = quads[idx]; bboxes[idx] = quad_bounds( positions[q.x], positions[q.y], positions[q.z], positions[q.w]); @@ -2185,7 +2184,7 @@ void update_points_bvh(bvh_tree& bvh, const vector& points, const vector& positions, const vector& radius) { // build primitives auto bboxes = vector(points.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& p = points[idx]; bboxes[idx] = point_bounds(positions[p], radius[p]); } @@ -2197,7 +2196,7 @@ void update_lines_bvh(bvh_tree& bvh, const vector& lines, const vector& positions, const vector& radius) { // build primitives auto bboxes = vector(lines.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& l = lines[idx]; bboxes[idx] = line_bounds( positions[l.x], positions[l.y], radius[l.x], radius[l.y]); @@ -2210,7 +2209,7 @@ void update_triangles_bvh(bvh_tree& bvh, const vector& triangles, const vector& positions) { // build primitives auto bboxes = vector(triangles.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& t = triangles[idx]; bboxes[idx] = triangle_bounds( positions[t.x], positions[t.y], positions[t.z]); @@ -2223,7 +2222,7 @@ void update_quads_bvh( bvh_tree& bvh, const vector& quads, const vector& positions) { // build primitives auto bboxes = vector(quads.size()); - for (auto idx = 0; idx < (int)bboxes.size(); idx++) { + for (auto idx : range(bboxes.size())) { auto& q = quads[idx]; bboxes[idx] = quad_bounds( positions[q.x], positions[q.y], positions[q.z], positions[q.w]); @@ -2235,11 +2234,10 @@ void update_quads_bvh( // Intersect ray with a bvh. template -static bool intersect_elements_bvh(const bvh_tree& bvh, - Intersect&& intersect_element, const ray3f& ray_, int& element, vec2f& uv, - float& distance, bool find_any) { +static shape_intersection intersect_elements_bvh(const bvh_tree& bvh, + Intersect&& intersect_element, const ray3f& ray_, bool find_any) { // check empty - if (bvh.nodes.empty()) return false; + if (bvh.nodes.empty()) return {}; // node stack auto node_stack = array{}; @@ -2247,7 +2245,7 @@ static bool intersect_elements_bvh(const bvh_tree& bvh, node_stack[node_cur++] = 0; // shared variables - auto hit = false; + auto intersection = shape_intersection{}; // copy ray to modify it auto ray = ray_; @@ -2280,94 +2278,78 @@ static bool intersect_elements_bvh(const bvh_tree& bvh, } } else { for (auto idx : range(node.num)) { - auto primitive = bvh.primitives[node.start + idx]; - if (intersect_element(primitive, ray, uv, distance)) { - hit = true; - element = primitive; - ray.tmax = distance; - } + auto primitive = bvh.primitives[node.start + idx]; + auto eintersection = intersect_element(primitive, ray); + if (!eintersection.hit) continue; + intersection = { + primitive, eintersection.uv, eintersection.distance, true}; + ray.tmax = eintersection.distance; } } // check for early exit - if (find_any && hit) return hit; + if (find_any && intersection.hit) return intersection; } - return hit; + return intersection; } // Intersect ray with a bvh. shape_intersection intersect_points_bvh(const bvh_tree& bvh, const vector& points, const vector& positions, const vector& radius, const ray3f& ray, bool find_any) { - auto intersection = shape_intersection{}; - intersection.hit = intersect_elements_bvh( + return intersect_elements_bvh( bvh, - [&points, &positions, &radius]( - int idx, const ray3f& ray, vec2f& uv, float& distance) { + [&points, &positions, &radius](int idx, const ray3f& ray) { auto& p = points[idx]; - return intersect_point(ray, positions[p], radius[p], uv, distance); + return intersect_point(ray, positions[p], radius[p]); }, - ray, intersection.element, intersection.uv, intersection.distance, - find_any); - return intersection; + ray, find_any); } shape_intersection intersect_lines_bvh(const bvh_tree& bvh, const vector& lines, const vector& positions, const vector& radius, const ray3f& ray, bool find_any) { - auto intersection = shape_intersection{}; - intersection.hit = intersect_elements_bvh( + return intersect_elements_bvh( bvh, - [&lines, &positions, &radius]( - int idx, const ray3f& ray, vec2f& uv, float& distance) { + [&lines, &positions, &radius](int idx, const ray3f& ray) { auto& l = lines[idx]; - return intersect_line(ray, positions[l.x], positions[l.y], radius[l.x], - radius[l.y], uv, distance); + return intersect_line( + ray, positions[l.x], positions[l.y], radius[l.x], radius[l.y]); }, - ray, intersection.element, intersection.uv, intersection.distance, - find_any); - return intersection; + ray, find_any); } shape_intersection intersect_triangles_bvh(const bvh_tree& bvh, const vector& triangles, const vector& positions, const ray3f& ray, bool find_any) { - auto intersection = shape_intersection{}; - intersection.hit = intersect_elements_bvh( + return intersect_elements_bvh( bvh, - [&triangles, &positions]( - int idx, const ray3f& ray, vec2f& uv, float& distance) { + [&triangles, &positions](int idx, const ray3f& ray) { auto& t = triangles[idx]; return intersect_triangle( - ray, positions[t.x], positions[t.y], positions[t.z], uv, distance); + ray, positions[t.x], positions[t.y], positions[t.z]); }, - ray, intersection.element, intersection.uv, intersection.distance, - find_any); - return intersection; + ray, find_any); } shape_intersection intersect_quads_bvh(const bvh_tree& bvh, const vector& quads, const vector& positions, const ray3f& ray, bool find_any) { - auto intersection = shape_intersection{}; - intersection.hit = intersect_elements_bvh( + return intersect_elements_bvh( bvh, - [&quads, &positions]( - int idx, const ray3f& ray, vec2f& uv, float& distance) { + [&quads, &positions](int idx, const ray3f& ray) { auto& t = quads[idx]; return intersect_quad(ray, positions[t.x], positions[t.y], - positions[t.z], positions[t.w], uv, distance); + positions[t.z], positions[t.w]); }, - ray, intersection.element, intersection.uv, intersection.distance, - find_any); - return intersection; + ray, find_any); } // Intersect ray with a bvh. template -static bool overlap_elements_bvh(const bvh_tree& bvh, Overlap&& overlap_element, - const vec3f& pos, float max_distance, int& element, vec2f& uv, - float& distance, bool find_any) { +static shape_intersection overlap_elements_bvh(const bvh_tree& bvh, + Overlap&& overlap_element, const vec3f& pos, float max_distance, + bool find_any) { // check if empty - if (bvh.nodes.empty()) return false; + if (bvh.nodes.empty()) return {}; // node stack auto node_stack = array{}; @@ -2375,7 +2357,7 @@ static bool overlap_elements_bvh(const bvh_tree& bvh, Overlap&& overlap_element, node_stack[node_cur++] = 0; // hit - auto hit = false; + auto intersection = shape_intersection{}; // walking stack while (node_cur) { @@ -2393,20 +2375,20 @@ static bool overlap_elements_bvh(const bvh_tree& bvh, Overlap&& overlap_element, node_stack[node_cur++] = node.start + 1; } else { for (auto idx : range(node.num)) { - auto primitive = bvh.primitives[node.start + idx]; - if (overlap_element(primitive, pos, max_distance, uv, distance)) { - hit = true; - element = primitive; - max_distance = distance; - } + auto primitive = bvh.primitives[node.start + idx]; + auto eintersection = overlap_element(primitive, pos, max_distance); + if (!eintersection.hit) continue; + intersection = { + primitive, eintersection.uv, eintersection.distance, true}; + max_distance = eintersection.distance; } } // check for early exit - if (find_any && hit) return hit; + if (find_any && intersection.hit) return intersection; } - return hit; + return intersection; } // Find a shape element that overlaps a point within a given distance @@ -2417,71 +2399,58 @@ shape_intersection overlap_points_bvh(const bvh_tree& bvh, const vector& points, const vector& positions, const vector& radius, const vec3f& pos, float max_distance, bool find_any) { - auto intersection = shape_intersection{}; - intersection.hit = overlap_elements_bvh( + return overlap_elements_bvh( bvh, - [&points, &positions, &radius](int idx, const vec3f& pos, - float max_distance, vec2f& uv, float& distance) { + [&points, &positions, &radius]( + int idx, const vec3f& pos, float max_distance) { auto& p = points[idx]; - return overlap_point( - pos, max_distance, positions[p], radius[p], uv, distance); + return overlap_point(pos, max_distance, positions[p], radius[p]); }, - pos, max_distance, intersection.element, intersection.uv, - intersection.distance, find_any); - return intersection; + pos, max_distance, find_any); } shape_intersection overlap_lines_bvh(const bvh_tree& bvh, const vector& lines, const vector& positions, const vector& radius, const vec3f& pos, float max_distance, bool find_any) { - auto intersection = shape_intersection{}; - intersection.hit = overlap_elements_bvh( + return overlap_elements_bvh( bvh, - [&lines, &positions, &radius](int idx, const vec3f& pos, - float max_distance, vec2f& uv, float& distance) { + [&lines, &positions, &radius]( + int idx, const vec3f& pos, float max_distance) { auto& l = lines[idx]; return overlap_line(pos, max_distance, positions[l.x], positions[l.y], - radius[l.x], radius[l.y], uv, distance); + radius[l.x], radius[l.y]); }, - pos, max_distance, intersection.element, intersection.uv, - intersection.distance, find_any); - return intersection; + pos, max_distance, find_any); } shape_intersection overlap_triangles_bvh(const bvh_tree& bvh, const vector& triangles, const vector& positions, const vector& radius, const vec3f& pos, float max_distance, bool find_any) { - auto intersection = shape_intersection{}; - intersection.hit = overlap_elements_bvh( + return overlap_elements_bvh( bvh, - [&triangles, &positions, &radius](int idx, const vec3f& pos, - float max_distance, vec2f& uv, float& distance) { + [&triangles, &positions, &radius]( + int idx, const vec3f& pos, float max_distance) { auto& t = triangles[idx]; return overlap_triangle(pos, max_distance, positions[t.x], positions[t.y], positions[t.z], radius[t.x], radius[t.y], - radius[t.z], uv, distance); + radius[t.z]); }, - pos, max_distance, intersection.element, intersection.uv, - intersection.distance, find_any); - return intersection; + pos, max_distance, find_any); } shape_intersection overlap_quads_bvh(const bvh_tree& bvh, const vector& quads, const vector& positions, const vector& radius, const vec3f& pos, float max_distance, bool find_any) { - auto intersection = shape_intersection{}; - intersection.hit = overlap_elements_bvh( + return overlap_elements_bvh( bvh, - [&quads, &positions, &radius](int idx, const vec3f& pos, - float max_distance, vec2f& uv, float& distance) { + [&quads, &positions, &radius]( + int idx, const vec3f& pos, float max_distance) { auto& q = quads[idx]; return overlap_quad(pos, max_distance, positions[q.x], positions[q.y], positions[q.z], positions[q.w], radius[q.x], radius[q.y], - radius[q.z], radius[q.w], uv, distance); + radius[q.z], radius[q.w]); }, - pos, max_distance, intersection.element, intersection.uv, - intersection.distance, find_any); - return intersection; + pos, max_distance, find_any); } } // namespace yocto @@ -2603,7 +2572,7 @@ void split_facevarying(vector& split_quads, // make faces unique unordered_map vert_map; split_quads.resize(quadspos.size()); - for (auto fid = 0; fid < (int)quadspos.size(); fid++) { + for (auto fid : range(quadspos.size())) { for (auto c : range(4)) { auto v = vec3i{ (&quadspos[fid].x)[c], @@ -2652,7 +2621,7 @@ pair, vector> weld_vertices( auto welded = vector{}; auto grid = make_hash_grid(threshold); auto neighbors = vector{}; - for (auto vertex = 0; vertex < (int)positions.size(); vertex++) { + for (auto vertex : range(positions.size())) { auto& position = positions[vertex]; find_neighbors(grid, neighbors, position, threshold); if (neighbors.empty()) { @@ -3010,11 +2979,11 @@ static pair, vector> subdivide_catmullclark_impl( acount[vid] += 1; } } - for (auto i = 0; i < (int)tvertices.size(); i++) avert[i] /= (float)acount[i]; + for (auto i : range(tvertices.size())) avert[i] /= (float)acount[i]; // correction pass ---------------------------------- // p = p + (avg_p - p) * (4/avg_count) - for (auto i = 0; i < (int)tvertices.size(); i++) { + for (auto i : range(tvertices.size())) { if (tvert_val[i] != 2) continue; avert[i] = tvertices[i] + (avert[i] - tvertices[i]) * (4 / (float)acount[i]); @@ -3128,13 +3097,11 @@ int sample_points(const vector& cdf, float re) { } vector sample_points_cdf(int npoints) { auto cdf = vector(npoints); - for (auto i = 0; i < (int)cdf.size(); i++) - cdf[i] = 1 + (i != 0 ? cdf[i - 1] : 0); + for (auto i : range(cdf.size())) cdf[i] = 1 + (i != 0 ? cdf[i - 1] : 0); return cdf; } void sample_points_cdf(vector& cdf, int npoints) { - for (auto i = 0; i < (int)cdf.size(); i++) - cdf[i] = 1 + (i != 0 ? cdf[i - 1] : 0); + for (auto i : range(cdf.size())) cdf[i] = 1 + (i != 0 ? cdf[i - 1] : 0); } // Pick a point on lines uniformly. @@ -3144,7 +3111,7 @@ pair sample_lines(const vector& cdf, float re, float ru) { vector sample_lines_cdf( const vector& lines, const vector& positions) { auto cdf = vector(lines.size()); - for (auto i = 0; i < (int)cdf.size(); i++) { + for (auto i : range(cdf.size())) { auto& l = lines[i]; auto w = line_length(positions[l.x], positions[l.y]); cdf[i] = w + (i != 0 ? cdf[i - 1] : 0); @@ -3153,7 +3120,7 @@ vector sample_lines_cdf( } void sample_lines_cdf(vector& cdf, const vector& lines, const vector& positions) { - for (auto i = 0; i < (int)cdf.size(); i++) { + for (auto i : range(cdf.size())) { auto& l = lines[i]; auto w = line_length(positions[l.x], positions[l.y]); cdf[i] = w + (i != 0 ? cdf[i - 1] : 0); @@ -3168,7 +3135,7 @@ pair sample_triangles( vector sample_triangles_cdf( const vector& triangles, const vector& positions) { auto cdf = vector(triangles.size()); - for (auto i = 0; i < (int)cdf.size(); i++) { + for (auto i : range(cdf.size())) { auto& t = triangles[i]; auto w = triangle_area(positions[t.x], positions[t.y], positions[t.z]); cdf[i] = w + (i != 0 ? cdf[i - 1] : 0); @@ -3177,7 +3144,7 @@ vector sample_triangles_cdf( } void sample_triangles_cdf(vector& cdf, const vector& triangles, const vector& positions) { - for (auto i = 0; i < (int)cdf.size(); i++) { + for (auto i : range(cdf.size())) { auto& t = triangles[i]; auto w = triangle_area(positions[t.x], positions[t.y], positions[t.z]); cdf[i] = w + (i != 0 ? cdf[i - 1] : 0); @@ -3201,7 +3168,7 @@ pair sample_quads(const vector& quads, vector sample_quads_cdf( const vector& quads, const vector& positions) { auto cdf = vector(quads.size()); - for (auto i = 0; i < (int)cdf.size(); i++) { + for (auto i : range(cdf.size())) { auto& q = quads[i]; auto w = quad_area( positions[q.x], positions[q.y], positions[q.z], positions[q.w]); @@ -3211,7 +3178,7 @@ vector sample_quads_cdf( } void sample_quads_cdf(vector& cdf, const vector& quads, const vector& positions) { - for (auto i = 0; i < (int)cdf.size(); i++) { + for (auto i : range(cdf.size())) { auto& q = quads[i]; auto w = quad_area( positions[q.x], positions[q.y], positions[q.z], positions[q.w]); @@ -3331,7 +3298,7 @@ void make_bulged_rect(vector& quads, vector& positions, height = min(height, min(scale)); auto radius = (1 + height * height) / (2 * height); auto center = vec3f{0, 0, -radius + height}; - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { auto pn = normalize(positions[i] - center); positions[i] = center + pn * radius; normals[i] = pn; @@ -3402,7 +3369,7 @@ void make_box(vector& quads, vector& positions, // + y make_rect(qquads, qpositions, qnormals, qtexturecoords, {steps.x, steps.z}, {scale.x, scale.z}, {uvscale.x, uvscale.z}); - for (auto i = 0; i < (int)qpositions.size(); i++) { + for (auto i : range(qpositions.size())) { qpositions[i] = {qpositions[i].x, scale.y, -qpositions[i].y}; qnormals[i] = {0, 1, 0}; } @@ -3411,7 +3378,7 @@ void make_box(vector& quads, vector& positions, // - y make_rect(qquads, qpositions, qnormals, qtexturecoords, {steps.x, steps.z}, {scale.x, scale.z}, {uvscale.x, uvscale.z}); - for (auto i = 0; i < (int)qpositions.size(); i++) { + for (auto i : range(qpositions.size())) { qpositions[i] = {qpositions[i].x, -scale.y, qpositions[i].y}; qnormals[i] = {0, -1, 0}; } @@ -3426,7 +3393,7 @@ void make_rounded_box(vector& quads, vector& positions, if (radius != 0) { radius = min(radius, min(scale)); auto c = scale - radius; - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { auto pc = vec3f{ abs(positions[i].x), abs(positions[i].y), abs(positions[i].z)}; auto ps = vec3f{positions[i].x < 0 ? -1.0f : 1.0f, @@ -3489,7 +3456,7 @@ void make_bent_floor(vector& quads, vector& positions, radius = min(radius, scale.y); auto start = (scale.y - radius) / 2; auto end = start + radius; - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { if (positions[i].z < -end) { positions[i] = {positions[i].x, -positions[i].z - end + radius, -end}; normals[i] = {0, 0, 1}; @@ -3520,7 +3487,7 @@ void make_uvsphere(vector& quads, vector& positions, vector& normals, vector& texcoords, const vec2i& steps, float scale, const vec2f& uvscale) { make_rect(quads, positions, normals, texcoords, steps, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { auto uv = texcoords[i]; auto a = vec2f{2 * pif * uv.x, pif * (1 - uv.y)}; positions[i] = vec3f{cos(a.x) * sin(a.y), sin(a.x) * sin(a.y), cos(a.y)} * @@ -3537,7 +3504,7 @@ void make_capped_uvsphere(vector& quads, vector& positions, if (cap != 0) { cap = min(cap, scale / 2); auto zflip = (scale - cap); - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { if (positions[i].z > zflip) { positions[i].z = 2 * zflip - positions[i].z; normals[i].x = -normals[i].x; @@ -3599,7 +3566,7 @@ void make_bulged_disk(vector& quads, vector& positions, height = min(height, scale); auto radius = (1 + height * height) / (2 * height); auto center = vec3f{0, 0, -radius + height}; - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { auto pn = normalize(positions[i] - center); positions[i] = center + pn * radius; normals[i] = pn; @@ -3612,7 +3579,7 @@ void make_uvdisk(vector& quads, vector& positions, vector& normals, vector& texcoords, const vec2i& steps, float scale, const vec2f& uvscale) { make_rect(quads, positions, normals, texcoords, steps, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { auto uv = texcoords[i]; auto phi = 2 * pif * uv.x; positions[i] = vec3f{cos(phi) * uv.y, sin(phi) * uv.y, 0} * scale; @@ -3632,7 +3599,7 @@ void make_uvcylinder(vector& quads, vector& positions, // side make_rect(qquads, qpositions, qnormals, qtexcoords, {steps.x, steps.y}, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)qpositions.size(); i++) { + for (auto i : range(qpositions.size())) { auto uv = qtexcoords[i]; auto phi = 2 * pif * uv.x; qpositions[i] = { @@ -3646,7 +3613,7 @@ void make_uvcylinder(vector& quads, vector& positions, // top make_rect(qquads, qpositions, qnormals, qtexcoords, {steps.x, steps.z}, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)qpositions.size(); i++) { + for (auto i : range(qpositions.size())) { auto uv = qtexcoords[i]; auto phi = 2 * pif * uv.x; qpositions[i] = {cos(phi) * uv.y * scale.x, sin(phi) * uv.y * scale.x, 0}; @@ -3659,7 +3626,7 @@ void make_uvcylinder(vector& quads, vector& positions, // bottom make_rect(qquads, qpositions, qnormals, qtexcoords, {steps.x, steps.z}, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)qpositions.size(); i++) { + for (auto i : range(qpositions.size())) { auto uv = qtexcoords[i]; auto phi = 2 * pif * uv.x; qpositions[i] = {cos(phi) * uv.y * scale.x, sin(phi) * uv.y * scale.x, 0}; @@ -3681,7 +3648,7 @@ void make_rounded_uvcylinder(vector& quads, vector& positions, if (radius != 0) { radius = min(radius, min(scale)); auto c = scale - radius; - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range(positions.size())) { auto phi = atan2(positions[i].y, positions[i].x); auto r = length(vec2f{positions[i].x, positions[i].y}); auto z = positions[i].z; @@ -3760,7 +3727,7 @@ void make_points(vector& points, vector& positions, normals.assign(num, {0, 0, 1}); texcoords.assign(num, {0, 0}); radius.assign(num, point_radius); - for (auto i = 0; i < (int)texcoords.size(); i++) + for (auto i : range(texcoords.size())) texcoords[i] = {(float)i / (float)num, 0}; } @@ -3772,9 +3739,9 @@ void make_points(vector& points, vector& positions, auto quads = vector{}; make_rect(quads, positions, normals, texcoords, steps, size, uvscale); points.resize(positions.size()); - for (auto i = 0; i < (int)positions.size(); i++) points[i] = i; + for (auto i : range(positions.size())) points[i] = (int)i; radius.resize(positions.size()); - for (auto i = 0; i < (int)texcoords.size(); i++) + for (auto i : range(texcoords.size())) radius[i] = lerp(rad.x, rad.y, texcoords[i].y / uvscale.y); } @@ -4043,10 +4010,10 @@ void make_hair(vector& lines, vector& positions, auto cidx = vector(); if (clump.x > 0) { - for (auto bidx = 0; bidx < (int)bpos.size(); bidx++) { + for (auto bidx : range((int)bpos.size())) { cidx.push_back(0); auto cdist = flt_max; - for (auto c : range(clump.y)) { + for (auto c : range((int)clump.y)) { auto d = length(bpos[bidx] - bpos[c]); if (d < cdist) { cdist = d; @@ -4058,7 +4025,7 @@ void make_hair(vector& lines, vector& positions, make_lines(lines, positions, normals, texcoords, radius, steps, {1, 1}, {1, 1}, {1, 1}); - for (auto i = 0; i < (int)positions.size(); i++) { + for (auto i : range((int)positions.size())) { auto u = texcoords[i].x; auto bidx = i / (steps.x + 1); positions[i] = bpos[bidx] + bnorm[bidx] * u * blen[bidx]; diff --git a/libs/yocto/yocto_trace.cpp b/libs/yocto/yocto_trace.cpp index 013c7e078..d481492fa 100644 --- a/libs/yocto/yocto_trace.cpp +++ b/libs/yocto/yocto_trace.cpp @@ -1443,18 +1443,18 @@ trace_lights make_trace_lights( const scene_data& scene, const trace_params& params) { auto lights = trace_lights{}; - for (auto handle = 0; handle < (int)scene.instances.size(); handle++) { + for (auto handle : range(scene.instances.size())) { auto& instance = scene.instances[handle]; auto& material = scene.materials[instance.material]; if (material.emission == vec3f{0, 0, 0}) continue; auto& shape = scene.shapes[instance.shape]; if (shape.triangles.empty() && shape.quads.empty()) continue; auto& light = add_light(lights); - light.instance = handle; + light.instance = (int)handle; light.environment = invalidid; if (!shape.triangles.empty()) { light.elements_cdf = vector(shape.triangles.size()); - for (auto idx = 0; idx < (int)light.elements_cdf.size(); idx++) { + for (auto idx : range(light.elements_cdf.size())) { auto& t = shape.triangles[idx]; light.elements_cdf[idx] = triangle_area( shape.positions[t.x], shape.positions[t.y], shape.positions[t.z]); @@ -1463,7 +1463,7 @@ trace_lights make_trace_lights( } if (!shape.quads.empty()) { light.elements_cdf = vector(shape.quads.size()); - for (auto idx = 0; idx < (int)light.elements_cdf.size(); idx++) { + for (auto idx : range(light.elements_cdf.size())) { auto& t = shape.quads[idx]; light.elements_cdf[idx] = quad_area(shape.positions[t.x], shape.positions[t.y], shape.positions[t.z], shape.positions[t.w]); @@ -1471,17 +1471,17 @@ trace_lights make_trace_lights( } } } - for (auto handle = 0; handle < (int)scene.environments.size(); handle++) { + for (auto handle : range(scene.environments.size())) { auto& environment = scene.environments[handle]; if (environment.emission == vec3f{0, 0, 0}) continue; auto& light = add_light(lights); light.instance = invalidid; - light.environment = handle; + light.environment = (int)handle; if (environment.emission_tex != invalidid) { auto& texture = scene.textures[environment.emission_tex]; light.elements_cdf = vector(texture.width * texture.height); - for (auto idx = 0; idx < (int)light.elements_cdf.size(); idx++) { - auto ij = vec2i{idx % texture.width, idx / texture.width}; + for (auto idx : range(light.elements_cdf.size())) { + auto ij = vec2i{(int)idx % texture.width, (int)idx / texture.width}; auto th = (ij.y + 0.5f) * pif / texture.height; auto value = lookup_texture(texture, ij.x, ij.y); light.elements_cdf[idx] = max(value) * sin(th);