1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkRect.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,168 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#include "SkRect.h" 1.14 + 1.15 +void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) { 1.16 + // do nothing if the params are empty 1.17 + if (left >= right || top >= bottom) { 1.18 + return; 1.19 + } 1.20 + 1.21 + // if we are empty, just assign 1.22 + if (fLeft >= fRight || fTop >= fBottom) { 1.23 + this->set(left, top, right, bottom); 1.24 + } else { 1.25 + if (left < fLeft) fLeft = left; 1.26 + if (top < fTop) fTop = top; 1.27 + if (right > fRight) fRight = right; 1.28 + if (bottom > fBottom) fBottom = bottom; 1.29 + } 1.30 +} 1.31 + 1.32 +void SkIRect::sort() { 1.33 + if (fLeft > fRight) { 1.34 + SkTSwap<int32_t>(fLeft, fRight); 1.35 + } 1.36 + if (fTop > fBottom) { 1.37 + SkTSwap<int32_t>(fTop, fBottom); 1.38 + } 1.39 +} 1.40 + 1.41 +///////////////////////////////////////////////////////////////////////////// 1.42 + 1.43 +void SkRect::sort() { 1.44 + if (fLeft > fRight) { 1.45 + SkTSwap<SkScalar>(fLeft, fRight); 1.46 + } 1.47 + if (fTop > fBottom) { 1.48 + SkTSwap<SkScalar>(fTop, fBottom); 1.49 + } 1.50 +} 1.51 + 1.52 +void SkRect::toQuad(SkPoint quad[4]) const { 1.53 + SkASSERT(quad); 1.54 + 1.55 + quad[0].set(fLeft, fTop); 1.56 + quad[1].set(fRight, fTop); 1.57 + quad[2].set(fRight, fBottom); 1.58 + quad[3].set(fLeft, fBottom); 1.59 +} 1.60 + 1.61 +bool SkRect::setBoundsCheck(const SkPoint pts[], int count) { 1.62 + SkASSERT((pts && count > 0) || count == 0); 1.63 + 1.64 + bool isFinite = true; 1.65 + 1.66 + if (count <= 0) { 1.67 + sk_bzero(this, sizeof(SkRect)); 1.68 + } else { 1.69 + SkScalar l, t, r, b; 1.70 + 1.71 + l = r = pts[0].fX; 1.72 + t = b = pts[0].fY; 1.73 + 1.74 + // If all of the points are finite, accum should stay 0. If we encounter 1.75 + // a NaN or infinity, then accum should become NaN. 1.76 + float accum = 0; 1.77 + accum *= l; accum *= t; 1.78 + 1.79 + for (int i = 1; i < count; i++) { 1.80 + SkScalar x = pts[i].fX; 1.81 + SkScalar y = pts[i].fY; 1.82 + 1.83 + accum *= x; accum *= y; 1.84 + 1.85 + // we use if instead of if/else, so we can generate min/max 1.86 + // float instructions (at least on SSE) 1.87 + if (x < l) l = x; 1.88 + if (x > r) r = x; 1.89 + 1.90 + if (y < t) t = y; 1.91 + if (y > b) b = y; 1.92 + } 1.93 + 1.94 + SkASSERT(!accum || !SkScalarIsFinite(accum)); 1.95 + if (accum) { 1.96 + l = t = r = b = 0; 1.97 + isFinite = false; 1.98 + } 1.99 + this->set(l, t, r, b); 1.100 + } 1.101 + 1.102 + return isFinite; 1.103 +} 1.104 + 1.105 +bool SkRect::intersect(SkScalar left, SkScalar top, SkScalar right, 1.106 + SkScalar bottom) { 1.107 + if (left < right && top < bottom && !this->isEmpty() && // check for empties 1.108 + fLeft < right && left < fRight && fTop < bottom && top < fBottom) 1.109 + { 1.110 + if (fLeft < left) fLeft = left; 1.111 + if (fTop < top) fTop = top; 1.112 + if (fRight > right) fRight = right; 1.113 + if (fBottom > bottom) fBottom = bottom; 1.114 + return true; 1.115 + } 1.116 + return false; 1.117 +} 1.118 + 1.119 +bool SkRect::intersect(const SkRect& r) { 1.120 + SkASSERT(&r); 1.121 + return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom); 1.122 +} 1.123 + 1.124 +bool SkRect::intersect2(const SkRect& r) { 1.125 + SkASSERT(&r); 1.126 + SkScalar L = SkMaxScalar(fLeft, r.fLeft); 1.127 + SkScalar R = SkMinScalar(fRight, r.fRight); 1.128 + if (L >= R) { 1.129 + return false; 1.130 + } 1.131 + SkScalar T = SkMaxScalar(fTop, r.fTop); 1.132 + SkScalar B = SkMinScalar(fBottom, r.fBottom); 1.133 + if (T >= B) { 1.134 + return false; 1.135 + } 1.136 + this->set(L, T, R, B); 1.137 + return true; 1.138 +} 1.139 + 1.140 +bool SkRect::intersect(const SkRect& a, const SkRect& b) { 1.141 + SkASSERT(&a && &b); 1.142 + 1.143 + if (!a.isEmpty() && !b.isEmpty() && 1.144 + a.fLeft < b.fRight && b.fLeft < a.fRight && 1.145 + a.fTop < b.fBottom && b.fTop < a.fBottom) { 1.146 + fLeft = SkMaxScalar(a.fLeft, b.fLeft); 1.147 + fTop = SkMaxScalar(a.fTop, b.fTop); 1.148 + fRight = SkMinScalar(a.fRight, b.fRight); 1.149 + fBottom = SkMinScalar(a.fBottom, b.fBottom); 1.150 + return true; 1.151 + } 1.152 + return false; 1.153 +} 1.154 + 1.155 +void SkRect::join(SkScalar left, SkScalar top, SkScalar right, 1.156 + SkScalar bottom) { 1.157 + // do nothing if the params are empty 1.158 + if (left >= right || top >= bottom) { 1.159 + return; 1.160 + } 1.161 + 1.162 + // if we are empty, just assign 1.163 + if (fLeft >= fRight || fTop >= fBottom) { 1.164 + this->set(left, top, right, bottom); 1.165 + } else { 1.166 + if (left < fLeft) fLeft = left; 1.167 + if (top < fTop) fTop = top; 1.168 + if (right > fRight) fRight = right; 1.169 + if (bottom > fBottom) fBottom = bottom; 1.170 + } 1.171 +}