Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Implement an interval class #777

Closed
6 tasks done
hollasch opened this issue Oct 27, 2020 · 9 comments
Closed
6 tasks done

Implement an interval class #777

hollasch opened this issue Oct 27, 2020 · 9 comments
Assignees
Milestone

Comments

@hollasch
Copy link
Collaborator

hollasch commented Oct 27, 2020

A class that operates on ranges of double values. Use cases:

Here's a sketch of an interval class (not all methods below need be implemented; only those actually needed):

struct interval {
    double min, max;

    interval() : min(0), max(0) {}
    interval(double x) : min(x), max(x) {}
    interval(double _min, double _max) : min(_min), max(_max) {}

    double size() const {
        return max - min;
    }

    bool contains(double x) const {
        return min <= x && x <= max;
    }

    double clamp(double x) const {
        return (x < min) ? min
             : (x > max) ? max
             : x;
    }

    interval hull(const interval& other) const {
        return interval(std::min(min, other.min), std::max(max, other.max));
    }

    interval intersect(const interval& other) const {
        return interval(std::max(min, other.min), std::min(max, other.max));
    }

    static interval empty    = interval(+std::numeric_limits::infinity, -std::numeric_limits::infinity);
    static interval universe = interval(-std::numeric_limits::infinity, +std::numeric_limits::infinity);
}

This class could also support transforms, such as scale, translate, whatever.

@hollasch hollasch added this to the v4.0.0 milestone Oct 27, 2020
@hollasch hollasch self-assigned this Oct 27, 2020
@hollasch
Copy link
Collaborator Author

Hmmm. It appears that "range" is unconventional / inaccurate. Looks like "interval" is more correct.

@hollasch hollasch changed the title Consider implementing a range class Consider implementing an interval class Oct 27, 2020
@hollasch hollasch changed the title Consider implementing an interval class Implement an interval class Oct 29, 2020
@hollasch
Copy link
Collaborator Author

From Slack discussion, constant values are interval.empty and interval.universe.

@trevordblack
Copy link
Collaborator

Suggest defining const infinity to std::numeric_limits::infinity

@hollasch
Copy link
Collaborator Author

I like it. The better way would be:

using std::numeric_limits::infinity;

@trevordblack
Copy link
Collaborator

trevordblack commented Oct 29, 2020

Yeah, but qualify it's a double:

using std::numeric_limits<double>::infinity;

Edit: that variable is a function

@hollasch
Copy link
Collaborator Author

Oif. Ok,

const auto infinity = std::numeric_limits<double>::infinity();

@hollasch
Copy link
Collaborator Author

hollasch commented Oct 30, 2020

ARRRRGGGHHH!!! That looks exactly like the line we already have in src/common/rtweekend.h. X^P

@hollasch
Copy link
Collaborator Author

The remaining three tasks all have to do with time, which is more broadly covered in #799. We'll use interval as appropriate when revisiting time.

@hollasch
Copy link
Collaborator Author

Also see specific issues for the tasks given above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants