diff options
Diffstat (limited to 'source/blender/blenlib/intern/rct.c')
-rw-r--r-- | source/blender/blenlib/intern/rct.c | 173 |
1 files changed, 105 insertions, 68 deletions
diff --git a/source/blender/blenlib/intern/rct.c b/source/blender/blenlib/intern/rct.c index fb767f54e55..91fcb5a5b83 100644 --- a/source/blender/blenlib/intern/rct.c +++ b/source/blender/blenlib/intern/rct.c @@ -48,23 +48,23 @@ * * \return True if \a rect is empty. */ -int BLI_rcti_is_empty(const rcti *rect) +bool BLI_rcti_is_empty(const rcti *rect) { return ((rect->xmax <= rect->xmin) || (rect->ymax <= rect->ymin)); } -int BLI_rctf_is_empty(const rctf *rect) +bool BLI_rctf_is_empty(const rctf *rect) { return ((rect->xmax <= rect->xmin) || (rect->ymax <= rect->ymin)); } -int BLI_rcti_isect_pt(const rcti *rect, const int x, const int y) +bool BLI_rcti_isect_pt(const rcti *rect, const int x, const int y) { - if (x < rect->xmin) return 0; - if (x > rect->xmax) return 0; - if (y < rect->ymin) return 0; - if (y > rect->ymax) return 0; - return 1; + if (x < rect->xmin) return false; + if (x > rect->xmax) return false; + if (y < rect->ymin) return false; + if (y > rect->ymax) return false; + return true; } /** @@ -74,31 +74,31 @@ int BLI_rcti_isect_pt(const rcti *rect, const int x, const int y) * * \return True if \a rect is empty. */ -int BLI_rcti_isect_pt_v(const rcti *rect, const int xy[2]) +bool BLI_rcti_isect_pt_v(const rcti *rect, const int xy[2]) { - if (xy[0] < rect->xmin) return 0; - if (xy[0] > rect->xmax) return 0; - if (xy[1] < rect->ymin) return 0; - if (xy[1] > rect->ymax) return 0; - return 1; + if (xy[0] < rect->xmin) return false; + if (xy[0] > rect->xmax) return false; + if (xy[1] < rect->ymin) return false; + if (xy[1] > rect->ymax) return false; + return true; } -int BLI_rctf_isect_pt(const rctf *rect, const float x, const float y) +bool BLI_rctf_isect_pt(const rctf *rect, const float x, const float y) { - if (x < rect->xmin) return 0; - if (x > rect->xmax) return 0; - if (y < rect->ymin) return 0; - if (y > rect->ymax) return 0; - return 1; + if (x < rect->xmin) return false; + if (x > rect->xmax) return false; + if (y < rect->ymin) return false; + if (y > rect->ymax) return false; + return true; } -int BLI_rctf_isect_pt_v(const rctf *rect, const float xy[2]) +bool BLI_rctf_isect_pt_v(const rctf *rect, const float xy[2]) { - if (xy[0] < rect->xmin) return 0; - if (xy[0] > rect->xmax) return 0; - if (xy[1] < rect->ymin) return 0; - if (xy[1] > rect->ymax) return 0; - return 1; + if (xy[0] < rect->xmin) return false; + if (xy[0] > rect->xmax) return false; + if (xy[1] < rect->ymin) return false; + if (xy[1] > rect->ymax) return false; + return true; } /* based closely on 'isect_line_line_v2_int', but in modified so corner cases are treated as intersections */ @@ -127,17 +127,17 @@ static int isect_segments_fl(const float v1[2], const float v2[2], const float v } } -int BLI_rcti_isect_segment(const rcti *rect, const int s1[2], const int s2[2]) +bool BLI_rcti_isect_segment(const rcti *rect, const int s1[2], const int s2[2]) { /* first do outside-bounds check for both points of the segment */ - if (s1[0] < rect->xmin && s2[0] < rect->xmin) return 0; - if (s1[0] > rect->xmax && s2[0] > rect->xmax) return 0; - if (s1[1] < rect->ymin && s2[1] < rect->ymin) return 0; - if (s1[1] > rect->ymax && s2[1] > rect->ymax) return 0; + if (s1[0] < rect->xmin && s2[0] < rect->xmin) return false; + if (s1[0] > rect->xmax && s2[0] > rect->xmax) return false; + if (s1[1] < rect->ymin && s2[1] < rect->ymin) return false; + if (s1[1] > rect->ymax && s2[1] > rect->ymax) return false; /* if either points intersect then we definetly intersect */ if (BLI_rcti_isect_pt_v(rect, s1) || BLI_rcti_isect_pt_v(rect, s2)) { - return 1; + return true; } else { /* both points are outside but may insersect the rect */ @@ -147,32 +147,32 @@ int BLI_rcti_isect_segment(const rcti *rect, const int s1[2], const int s2[2]) tvec1[0] = rect->xmin; tvec1[1] = rect->ymin; tvec2[0] = rect->xmin; tvec2[1] = rect->ymax; if (isect_segments_i(s1, s2, tvec1, tvec2)) { - return 1; + return true; } /* diagonal: [\] */ tvec1[0] = rect->xmin; tvec1[1] = rect->ymax; tvec2[0] = rect->xmax; tvec2[1] = rect->ymin; if (isect_segments_i(s1, s2, tvec1, tvec2)) { - return 1; + return true; } /* no intersection */ - return 0; + return false; } } -int BLI_rctf_isect_segment(const rctf *rect, const float s1[2], const float s2[2]) +bool BLI_rctf_isect_segment(const rctf *rect, const float s1[2], const float s2[2]) { /* first do outside-bounds check for both points of the segment */ - if (s1[0] < rect->xmin && s2[0] < rect->xmin) return 0; - if (s1[0] > rect->xmax && s2[0] > rect->xmax) return 0; - if (s1[1] < rect->ymin && s2[1] < rect->ymin) return 0; - if (s1[1] > rect->ymax && s2[1] > rect->ymax) return 0; + if (s1[0] < rect->xmin && s2[0] < rect->xmin) return false; + if (s1[0] > rect->xmax && s2[0] > rect->xmax) return false; + if (s1[1] < rect->ymin && s2[1] < rect->ymin) return false; + if (s1[1] > rect->ymax && s2[1] > rect->ymax) return false; /* if either points intersect then we definetly intersect */ if (BLI_rctf_isect_pt_v(rect, s1) || BLI_rctf_isect_pt_v(rect, s2)) { - return 1; + return true; } else { /* both points are outside but may insersect the rect */ @@ -182,18 +182,18 @@ int BLI_rctf_isect_segment(const rctf *rect, const float s1[2], const float s2[2 tvec1[0] = rect->xmin; tvec1[1] = rect->ymin; tvec2[0] = rect->xmin; tvec2[1] = rect->ymax; if (isect_segments_fl(s1, s2, tvec1, tvec2)) { - return 1; + return true; } /* diagonal: [\] */ tvec1[0] = rect->xmin; tvec1[1] = rect->ymax; tvec2[0] = rect->xmax; tvec2[1] = rect->ymin; if (isect_segments_fl(s1, s2, tvec1, tvec2)) { - return 1; + return true; } /* no intersection */ - return 0; + return false; } } @@ -296,6 +296,19 @@ void BLI_rctf_translate(rctf *rect, float x, float y) rect->ymax += y; } +void BLI_rcti_recenter(rcti *rect, int x, int y) +{ + const int dx = x - BLI_rcti_cent_x(rect); + const int dy = y - BLI_rcti_cent_y(rect); + BLI_rcti_translate(rect, dx, dy); +} +void BLI_rctf_recenter(rctf *rect, float x, float y) +{ + const float dx = x - BLI_rctf_cent_x(rect); + const float dy = y - BLI_rctf_cent_y(rect); + BLI_rctf_translate(rect, dx, dy); +} + /* change width & height around the central location */ void BLI_rcti_resize(rcti *rect, int x, int y) { @@ -317,6 +330,30 @@ void BLI_rctf_resize(rctf *rect, float x, float y) rect->ymax = rect->ymin + y; } +void BLI_rcti_scale(rcti *rect, const float scale) +{ + const int cent_x = BLI_rcti_cent_x(rect); + const int cent_y = BLI_rcti_cent_y(rect); + const int size_x_half = BLI_rcti_size_x(rect) * (scale * 0.5f); + const int size_y_half = BLI_rcti_size_y(rect) * (scale * 0.5f); + rect->xmin = cent_x - size_x_half; + rect->ymin = cent_y - size_y_half; + rect->xmax = cent_x + size_x_half; + rect->ymax = cent_y + size_y_half; +} + +void BLI_rctf_scale(rctf *rect, const float scale) +{ + const float cent_x = BLI_rctf_cent_x(rect); + const float cent_y = BLI_rctf_cent_y(rect); + const float size_x_half = BLI_rctf_size_x(rect) * (scale * 0.5f); + const float size_y_half = BLI_rctf_size_y(rect) * (scale * 0.5f); + rect->xmin = cent_x - size_x_half; + rect->ymin = cent_y - size_y_half; + rect->xmax = cent_x + size_x_half; + rect->ymax = cent_y + size_y_half; +} + void BLI_rctf_interp(rctf *rect, const rctf *rect_a, const rctf *rect_b, const float fac) { const float ifac = 1.0f - fac; @@ -329,49 +366,49 @@ void BLI_rctf_interp(rctf *rect, const rctf *rect_a, const rctf *rect_b, const f /* BLI_rcti_interp() not needed yet */ -int BLI_rctf_clamp_pt_v(const struct rctf *rect, float xy[2]) +bool BLI_rctf_clamp_pt_v(const struct rctf *rect, float xy[2]) { - int change = 0; - if (xy[0] < rect->xmin) { xy[0] = rect->xmin; change = 1; } - if (xy[0] > rect->xmax) { xy[0] = rect->xmax; change = 1; } - if (xy[1] < rect->ymin) { xy[1] = rect->ymin; change = 1; } - if (xy[1] > rect->ymax) { xy[1] = rect->ymax; change = 1; } + bool change = false; + if (xy[0] < rect->xmin) { xy[0] = rect->xmin; change = true; } + if (xy[0] > rect->xmax) { xy[0] = rect->xmax; change = true; } + if (xy[1] < rect->ymin) { xy[1] = rect->ymin; change = true; } + if (xy[1] > rect->ymax) { xy[1] = rect->ymax; change = true; } return change; } -int BLI_rcti_clamp_pt_v(const struct rcti *rect, int xy[2]) +bool BLI_rcti_clamp_pt_v(const struct rcti *rect, int xy[2]) { - int change = 0; - if (xy[0] < rect->xmin) { xy[0] = rect->xmin; change = 1; } - if (xy[0] > rect->xmax) { xy[0] = rect->xmax; change = 1; } - if (xy[1] < rect->ymin) { xy[1] = rect->ymin; change = 1; } - if (xy[1] > rect->ymax) { xy[1] = rect->ymax; change = 1; } + bool change = false; + if (xy[0] < rect->xmin) { xy[0] = rect->xmin; change = true; } + if (xy[0] > rect->xmax) { xy[0] = rect->xmax; change = true; } + if (xy[1] < rect->ymin) { xy[1] = rect->ymin; change = true; } + if (xy[1] > rect->ymax) { xy[1] = rect->ymax; change = true; } return change; } -int BLI_rctf_compare(const struct rctf *rect_a, const struct rctf *rect_b, const float limit) +bool BLI_rctf_compare(const struct rctf *rect_a, const struct rctf *rect_b, const float limit) { if (fabsf(rect_a->xmin - rect_b->xmin) < limit) if (fabsf(rect_a->xmax - rect_b->xmax) < limit) if (fabsf(rect_a->ymin - rect_b->ymin) < limit) if (fabsf(rect_a->ymax - rect_b->ymax) < limit) - return 1; + return true; - return 0; + return false; } -int BLI_rcti_compare(const struct rcti *rect_a, const struct rcti *rect_b) +bool BLI_rcti_compare(const struct rcti *rect_a, const struct rcti *rect_b) { if (rect_a->xmin == rect_b->xmin) if (rect_a->xmax == rect_b->xmax) if (rect_a->ymin == rect_b->ymin) if (rect_a->ymax == rect_b->ymax) - return 1; + return true; - return 0; + return false; } -int BLI_rctf_isect(const rctf *src1, const rctf *src2, rctf *dest) +bool BLI_rctf_isect(const rctf *src1, const rctf *src2, rctf *dest) { float xmin, xmax; float ymin, ymax; @@ -388,7 +425,7 @@ int BLI_rctf_isect(const rctf *src1, const rctf *src2, rctf *dest) dest->ymin = ymin; dest->ymax = ymax; } - return 1; + return true; } else { if (dest) { @@ -397,11 +434,11 @@ int BLI_rctf_isect(const rctf *src1, const rctf *src2, rctf *dest) dest->ymin = 0; dest->ymax = 0; } - return 0; + return false; } } -int BLI_rcti_isect(const rcti *src1, const rcti *src2, rcti *dest) +bool BLI_rcti_isect(const rcti *src1, const rcti *src2, rcti *dest) { int xmin, xmax; int ymin, ymax; @@ -418,7 +455,7 @@ int BLI_rcti_isect(const rcti *src1, const rcti *src2, rcti *dest) dest->ymin = ymin; dest->ymax = ymax; } - return 1; + return true; } else { if (dest) { @@ -427,7 +464,7 @@ int BLI_rcti_isect(const rcti *src1, const rcti *src2, rcti *dest) dest->ymin = 0; dest->ymax = 0; } - return 0; + return false; } } |