From 09d6e976df66d7c7d2010f1357c4653bdb7ef56d Mon Sep 17 00:00:00 2001 From: Stefano Ceccherini Date: Thu, 25 Oct 2007 11:46:06 +0000 Subject: [PATCH] Looks like I deleted too much... copied Region.h back from the os folder, updated Region.cpp and RegionSupport.cpp accordingly. Also implemented empty BStatable destructor. Needed, since it's virtual now. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22713 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/build/os/interface/Region.h | 95 + src/build/libbe/interface/Region.cpp | 623 ++--- src/build/libbe/interface/RegionSupport.cpp | 2450 ++++++++++++------- src/build/libbe/storage/Statable.cpp | 5 + 4 files changed, 2007 insertions(+), 1166 deletions(-) create mode 100644 headers/build/os/interface/Region.h diff --git a/headers/build/os/interface/Region.h b/headers/build/os/interface/Region.h new file mode 100644 index 0000000000..668c107c0a --- /dev/null +++ b/headers/build/os/interface/Region.h @@ -0,0 +1,95 @@ +/* + * Copyright 2007, Haiku. All rights reserved. + * Distributed under the terms of the MIT License. + */ + +#ifndef _REGION_H +#define _REGION_H + +#include +#include + +namespace BPrivate { + class ServerLink; + class LinkReceiver; +}; + +/* Integer rect used to define a clipping rectangle. All bounds are inclusive. */ +/* Moved from DirectWindow.h */ +typedef struct { + int32 left; + int32 top; + int32 right; + int32 bottom; +} clipping_rect; + + +class BRegion { + public: + BRegion(); + BRegion(const BRegion& region); + BRegion(const BRect rect); + virtual ~BRegion(); + + BRegion &operator=(const BRegion &from); + + void Set(BRect newBounds); + void Set(clipping_rect newBounds); + + BRect Frame() const; + clipping_rect FrameInt() const; + + BRect RectAt(int32 index) /*const*/; + clipping_rect RectAtInt(int32 index) /*const*/; + + int32 CountRects() /*const*/; + + bool Intersects(BRect rect) const; + bool Intersects(clipping_rect rect) const; + + bool Contains(BPoint point) const; + bool Contains(int32 x, int32 y) /*const*/; + + void PrintToStream() const; + + void OffsetBy(int32 x, int32 y); + + void MakeEmpty(); + + void Include(BRect rect); + void Include(clipping_rect rect); + void Include(const BRegion*); + + void Exclude(BRect r); + void Exclude(clipping_rect r); + void Exclude(const BRegion* region); + + void IntersectWith(const BRegion* region); + + private: + friend class BDirectWindow; + friend class BPrivate::ServerLink; + friend class BPrivate::LinkReceiver; + + class Support; + friend class Support; + + private: + BRegion(const clipping_rect& rect); + + void _AdoptRegionData(BRegion& region); + bool _SetSize(long newSize); + + clipping_rect _Convert(const BRect& rect) const; + clipping_rect _ConvertToInternal(const BRect& rect) const; + clipping_rect _ConvertToInternal( + const clipping_rect& rect) const; + + private: + long fCount; + long fDataSize; + clipping_rect fBounds; + clipping_rect* fData; +}; + +#endif // _REGION_H diff --git a/src/build/libbe/interface/Region.cpp b/src/build/libbe/interface/Region.cpp index 1a00a75382..b69b581947 100644 --- a/src/build/libbe/interface/Region.cpp +++ b/src/build/libbe/interface/Region.cpp @@ -1,85 +1,50 @@ -//------------------------------------------------------------------------------ -// Copyright (c) 2003-2005, Haiku, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// -// File Name: Region.cpp -// Author: Stefano Ceccherini (burton666@libero.it) -// Description: Region class consisting of multiple rectangles -// -//------------------------------------------------------------------------------ - -// Notes: As now, memory is always allocated and never freed (except on destruction, -// or sometimes when a copy is made). -// This let us be a bit faster since we don't do many reallocations. -// But that means that even an empty region could "waste" much space, if it contained -// many rects before being emptied. -// I.E: a region which contains 24 rects allocates more than 24 * 4 * sizeof(int32) -// = 96 * sizeof(int32) bytes. If we call MakeEmpty(), that region will contain no rects, -// but it will still keep the allocated memory. -// This shouldnt' be an issue, since usually BRegions are just used for calculations, -// and don't last so long. -// Anyway, we can change that behaviour if we want, but BeOS's BRegion seems to behave exactly -// like this. +/* + * Copyright 2003-2007, Haiku. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Stefano Ceccherini (burton666@libero.it) + * Stephan Aßmus + */ -#include -#include - -#include #include -#include -#include +#include +#include + +#include + +#include "clipping.h" +#include "RegionSupport.h" + + +const static int32 kDataBlockSize = 8; /*! \brief Initializes a region. The region will have no rects, - and its bound will be invalid. + and its fBounds will be invalid. */ BRegion::BRegion() - : - data_size(8), - data(NULL) + : fCount(0) + , fDataSize(0) + , fBounds((clipping_rect){ 0, 0, 0, 0 }) + , fData(NULL) { - data = (clipping_rect *)malloc(data_size * sizeof(clipping_rect)); - - Support::ZeroRegion(*this); + _SetSize(kDataBlockSize); } /*! \brief Initializes a region to be a copy of another. \param region The region to copy. */ -BRegion::BRegion(const BRegion ®ion) - : - data(NULL) +BRegion::BRegion(const BRegion& region) + : fCount(0) + , fDataSize(0) + , fBounds((clipping_rect){ 0, 0, 0, 0 }) + , fData(NULL) { - bound = region.bound; - count = region.count; - data_size = region.data_size; - - if (data_size <= 0) - data_size = 1; - - data = (clipping_rect *)malloc(data_size * sizeof(clipping_rect)); - - memcpy(data, region.data, count * sizeof(clipping_rect)); + *this = region; } @@ -87,13 +52,29 @@ BRegion::BRegion(const BRegion ®ion) \param rect The BRect to set the region to. */ BRegion::BRegion(const BRect rect) - : - data_size(8), - data(NULL) + : fCount(0) + , fDataSize(1) + , fBounds((clipping_rect){ 0, 0, 0, 0 }) + , fData(&fBounds) +{ + if (!rect.IsValid()) + return; + + fBounds = _ConvertToInternal(rect); + fCount = 1; +} + +// NOTE: private constructor +/*! \brief Initializes a region to contain a clipping_rect. + \param rect The clipping_rect to set the region to, already in + internal rect format. +*/ +BRegion::BRegion(const clipping_rect& rect) + : fCount(1) + , fDataSize(1) + , fBounds(rect) + , fData(&fBounds) { - data = (clipping_rect *)malloc(data_size * sizeof(clipping_rect)); - - Set(rect); } @@ -101,68 +82,34 @@ BRegion::BRegion(const BRect rect) */ BRegion::~BRegion() { - free(data); + if (fData != &fBounds) + free(fData); } -/*! \brief Returns the bounds of the region. - \return A BRect which represents the bounds of the region. +// #pragma mark - + + +/*! \brief Modifies the region to be a copy of the given BRegion. + \param region the BRegion to copy. + \return This function always returns \c *this. */ -BRect -BRegion::Frame() const +BRegion & +BRegion::operator=(const BRegion ®ion) { - return to_BRect(bound); -} + if (®ion == this) + return *this; + // handle reallocation if we're too small to contain + // the other region + if (_SetSize(region.fDataSize)) { + memcpy(fData, region.fData, region.fCount * sizeof(clipping_rect)); -/*! \brief Returns the bounds of the region as a clipping_rect (which has integer coordinates). - \return A clipping_rect which represents the bounds of the region. -*/ -clipping_rect -BRegion::FrameInt() const -{ - return bound; -} - - -/*! \brief Returns the regions's BRect at the given index. - \param index The index (zero based) of the wanted rectangle. - \return If the given index is valid, it returns the BRect at that index, - otherwise, it returns an invalid BRect. -*/ -BRect -BRegion::RectAt(int32 index) -{ - if (index >= 0 && index < count) - return to_BRect(data[index]); + fBounds = region.fBounds; + fCount = region.fCount; + } - return BRect(); //An invalid BRect -} - - -/*! \brief Returns the regions's clipping_rect at the given index. - \param index The index (zero based) of the wanted rectangle. - \return If the given index is valid, it returns the clipping_rect at that index, - otherwise, it returns an invalid clipping_rect. -*/ -clipping_rect -BRegion::RectAtInt(int32 index) -{ - if (index >= 0 && index < count) - return data[index]; - - clipping_rect rect = { 1, 1, 0, 0 }; - return rect; -} - - -/*! \brief Counts the region rects. - \return An int32 which is the total number of rects in the region. -*/ -int32 -BRegion::CountRects() -{ - return count; + return *this; } @@ -172,7 +119,7 @@ BRegion::CountRects() void BRegion::Set(BRect newBounds) { - Set(to_clipping_rect(newBounds)); + Set(_Convert(newBounds)); } @@ -182,17 +129,93 @@ BRegion::Set(BRect newBounds) void BRegion::Set(clipping_rect newBounds) { - ASSERT(data_size > 0); + _SetSize(1); - if (valid_rect(newBounds)) { - count = 1; - data[0] = newBounds; - bound = newBounds; + if (valid_rect(newBounds) && fData) { + fCount = 1; + // cheap convert to internal rect format + newBounds.right++; + newBounds.bottom++; + fData[0] = fBounds = newBounds; } else - Support::ZeroRegion(*this); + MakeEmpty(); } +// #pragma mark - + + +/*! \brief Returns the bounds of the region. + \return A BRect which represents the bounds of the region. +*/ +BRect +BRegion::Frame() const +{ + return BRect(fBounds.left, fBounds.top, + fBounds.right - 1, fBounds.bottom - 1); +} + + +/*! \brief Returns the bounds of the region as a clipping_rect (which has integer coordinates). + \return A clipping_rect which represents the bounds of the region. +*/ +clipping_rect +BRegion::FrameInt() const +{ + return (clipping_rect){ fBounds.left, fBounds.top, + fBounds.right - 1, fBounds.bottom - 1 }; +} + + +/*! \brief Returns the regions's BRect at the given index. + \param index The index (zero based) of the wanted rectangle. + \return If the given index is valid, it returns the BRect at that index, + otherwise, it returns an invalid BRect. +*/ +BRect +BRegion::RectAt(int32 index) /*const*/ +{ + if (index >= 0 && index < fCount) { + const clipping_rect& r = fData[index]; + return BRect(r.left, r.top, r.right - 1, r.bottom - 1); + } + + return BRect(); + // an invalid BRect +} + + +/*! \brief Returns the regions's clipping_rect at the given index. + \param index The index (zero based) of the wanted rectangle. + \return If the given index is valid, it returns the clipping_rect at that index, + otherwise, it returns an invalid clipping_rect. +*/ +clipping_rect +BRegion::RectAtInt(int32 index) /*const*/ +{ + if (index >= 0 && index < fCount) { + const clipping_rect& r = fData[index]; + return (clipping_rect){ r.left, r.top, r.right - 1, r.bottom - 1 }; + } + + return (clipping_rect){ 1, 1, 0, 0 }; + // an invalid clipping_rect +} + + +/*! \brief Counts the region rects. + \return An int32 which is the total number of rects in the region. +*/ +int32 +BRegion::CountRects() /*const*/ +{ + return fCount; +} + + +// #pragma mark - + + /*! \brief Check if the region has any area in common with the given BRect. \param rect The BRect to check the region against to. \return \ctrue if the region has any area in common with the BRect, \cfalse if not. @@ -200,7 +223,7 @@ BRegion::Set(clipping_rect newBounds) bool BRegion::Intersects(BRect rect) const { - return Intersects(to_clipping_rect(rect)); + return Intersects(_Convert(rect)); } @@ -211,15 +234,13 @@ BRegion::Intersects(BRect rect) const bool BRegion::Intersects(clipping_rect rect) const { - if (!rects_intersect(rect, bound)) - return false; + // cheap convert to internal rect format + rect.right ++; + rect.bottom ++; - for (long c = 0; c < count; c++) { - if (rects_intersect(data[c], rect)) - return true; - } - - return false; + int result = Support::XRectInRegion(this, rect); + + return result > Support::RectangleOut; } @@ -228,18 +249,9 @@ BRegion::Intersects(clipping_rect rect) const \return \ctrue if the region contains the BPoint, \cfalse if not. */ bool -BRegion::Contains(BPoint pt) const +BRegion::Contains(BPoint point) const { - // If the point doesn't lie within the region's bounds, - // don't even try it against the region's rects. - if (!point_in(bound, pt)) - return false; - - for (long c = 0; c < count; c++) { - if (point_in(data[c], pt)) - return true; - } - return false; + return Support::XPointInRegion(this, (int)point.x, (int)point.y); } @@ -249,17 +261,9 @@ BRegion::Contains(BPoint pt) const \return \ctrue if the region contains the point, \cfalse if not. */ bool -BRegion::Contains(int32 x, int32 y) +BRegion::Contains(int32 x, int32 y) /*const*/ { - // see above - if (!point_in(bound, x, y)) - return false; - - for (long c = 0; c < count; c++) { - if (point_in(data[c], x, y)) - return true; - } - return false; + return Support::XPointInRegion(this, x, y); } @@ -269,27 +273,35 @@ void BRegion::PrintToStream() const { Frame().PrintToStream(); - - for (long c = 0; c < count; c++) { - clipping_rect *rect = &data[c]; - printf("data = BRect(l:%ld.0, t:%ld.0, r:%ld.0, b:%ld.0)\n", - rect->left, rect->top, rect->right, rect->bottom); - } + + for (long i = 0; i < fCount; i++) { + clipping_rect *rect = &fData[i]; + printf("data[%ld] = BRect(l:%ld.0, t:%ld.0, r:%ld.0, b:%ld.0)\n", + i, rect->left, rect->top, rect->right - 1, rect->bottom - 1); + } } +// #pragma mark - + + /*! \brief Offsets all region's rects, and bounds by the given values. \param dh The horizontal offset. \param dv The vertical offset. */ void -BRegion::OffsetBy(int32 dh, int32 dv) +BRegion::OffsetBy(int32 x, int32 y) { - if (count > 0) { - for (long c = 0; c < count; c++) - offset_rect(data[c], dh, dv); + if (x == 0 && y == 0) + return; - offset_rect(bound, dh, dv); + if (fCount > 0) { + if (fData != &fBounds) { + for (long i = 0; i < fCount; i++) + offset_rect(fData[i], x, y); + } + + offset_rect(fBounds, x, y); } } @@ -299,17 +311,21 @@ BRegion::OffsetBy(int32 dh, int32 dv) void BRegion::MakeEmpty() { - Support::ZeroRegion(*this); + fBounds = (clipping_rect){ 0, 0, 0, 0 }; + fCount = 0; } +// #pragma mark - + + /*! \brief Modifies the region, so that it includes the given BRect. \param rect The BRect to be included by the region. */ void BRegion::Include(BRect rect) { - Include(to_clipping_rect(rect)); + Include(_Convert(rect)); } @@ -319,13 +335,17 @@ BRegion::Include(BRect rect) void BRegion::Include(clipping_rect rect) { - BRegion region; - BRegion newRegion; + // convert to internal rect format + rect.right ++; + rect.bottom ++; - region.Set(rect); - - Support::OrRegion(*this, region, newRegion); - Support::CopyRegion(newRegion, *this); + // use private clipping_rect constructor which avoids malloc() + BRegion t(rect); + + BRegion result; + Support::XUnionRegion(this, &t, &result); + + _AdoptRegionData(result); } @@ -333,22 +353,25 @@ BRegion::Include(clipping_rect rect) \param region The region to be included. */ void -BRegion::Include(const BRegion *region) +BRegion::Include(const BRegion* region) { - BRegion newRegion; - - Support::OrRegion(*this, *region, newRegion); - Support::CopyRegion(newRegion, *this); + BRegion result; + Support::XUnionRegion(this, region, &result); + + _AdoptRegionData(result); } +// #pragma mark - + + /*! \brief Modifies the region, excluding the area represented by the given BRect. \param rect The BRect to be excluded. */ void BRegion::Exclude(BRect rect) { - Exclude(to_clipping_rect(rect)); + Exclude(_Convert(rect)); } @@ -358,150 +381,146 @@ BRegion::Exclude(BRect rect) void BRegion::Exclude(clipping_rect rect) { - BRegion region; - BRegion newRegion; - - region.Set(rect); + // convert to internal rect format + rect.right ++; + rect.bottom ++; - Support::SubRegion(*this, region, newRegion); - Support::CopyRegion(newRegion, *this); + // use private clipping_rect constructor which avoids malloc() + BRegion t(rect); + + BRegion result; + Support::XSubtractRegion(this, &t, &result); + + _AdoptRegionData(result); } -/*! \brief Modifies the region, excluding the area contained in the given BRegion. +/*! \brief Modifies the region, excluding the area contained in the given + BRegion. \param region The BRegion to be excluded. */ void -BRegion::Exclude(const BRegion *region) +BRegion::Exclude(const BRegion* region) { - BRegion newRegion; - - Support::SubRegion(*this, *region, newRegion); - Support::CopyRegion(newRegion, *this); + BRegion result; + Support::XSubtractRegion(this, region, &result); + + _AdoptRegionData(result); } -/*! \brief Modifies the region, so that it will contain just the area in common with the given BRegion. +// #pragma mark - + + +/*! \brief Modifies the region, so that it will contain just the area + in common with the given BRegion. \param region the BRegion to intersect to. */ void -BRegion::IntersectWith(const BRegion *region) +BRegion::IntersectWith(const BRegion* region) { - BRegion newRegion; - - Support::AndRegion(*this, *region, newRegion); - Support::CopyRegion(newRegion, *this); + BRegion result; + Support::XIntersectRegion(this, region, &result); + + _AdoptRegionData(result); } -/*! \brief Modifies the region to be a copy of the given BRegion. - \param region the BRegion to copy. - \return This function always returns \c *this. -*/ -BRegion & -BRegion::operator=(const BRegion ®ion) -{ - if (®ion != this) { - free(data); - bound = region.bound; - count = region.count; - data_size = region.data_size; - - if (data_size <= 0) - data_size = 1; - - data = (clipping_rect *)malloc(data_size * sizeof(clipping_rect)); - - memcpy(data, region.data, count * sizeof(clipping_rect)); - } - - return *this; -} +// #pragma mark - -/*! \brief Adds a rect to the region. - \param rect The clipping_rect to be added. - - Adds the given rect to the region, merging it with another already contained in the region, - if possible. Recalculate the region's bounds if needed. +/*! \brief Takes over the data of a region and marks that region empty. + \param region The region to adopt the data from. */ void -BRegion::_AddRect(clipping_rect rect) +BRegion::_AdoptRegionData(BRegion& region) { - ASSERT(count >= 0); - ASSERT(data_size >= 0); - ASSERT(valid_rect(rect)); + fCount = region.fCount; + fDataSize = region.fDataSize; + fBounds = region.fBounds; + if (fData != &fBounds) + free(fData); + if (region.fData != ®ion.fBounds) + fData = region.fData; + else + fData = &fBounds; - // Should we just reallocate the memory and - // copy the rect ? - bool addRect = true; - - if (count > 0) { - // Wait! We could merge the rect with one of the - // existing rectangles, if it's adiacent. - // We just check it against the last rectangle, since - // we are keeping them sorted by their "top" coordinates. - long last = count - 1; - if (rect.left == data[last].left && rect.right == data[last].right - && rect.top == data[last].bottom + 1) { - - data[last].bottom = rect.bottom; - addRect = false; - - } else if (rect.top == data[last].top && rect.bottom == data[last].bottom) { - if (rect.left == data[last].right + 1) { - - data[last].right = rect.right; - addRect = false; - - } else if (rect.right == data[last].left - 1) { - - data[last].left = rect.left; - addRect = false; - } - } - } - - // We weren't lucky.... just add the rect as a new one - if (addRect) { - if (data_size <= count) - set_size(count + 16); - - data[count] = rect; - - count++; - } - - // Recalculate bounds - if (rect.top < bound.top) - bound.top = rect.top; - - if (rect.left < bound.left) - bound.left = rect.left; - - if (rect.right > bound.right) - bound.right = rect.right; - - if (rect.bottom > bound.bottom) - bound.bottom = rect.bottom; + // NOTE: MakeEmpty() is not called since _AdoptRegionData is only + // called with internally allocated regions, so they don't need to + // be left in a valid state. + region.fData = NULL; +// region.MakeEmpty(); } /*! \brief Reallocate the memory in the region. - \param new_size The amount of rectangles that the region could contain. + \param newSize The amount of rectangles that the region should be + able to hold. */ -void -BRegion::set_size(long new_size) +bool +BRegion::_SetSize(long newSize) { - if (new_size <= 0) - new_size = data_size + 16; + // we never shrink the size + newSize = max_c(fDataSize, newSize); + if (newSize == fDataSize) + return true; + + // align newSize to multiple of kDataBlockSize + newSize = ((newSize + kDataBlockSize - 1) / kDataBlockSize) * kDataBlockSize; + + if (newSize > 0) { + if (fData == &fBounds) { + fData = (clipping_rect*)malloc(newSize * sizeof(clipping_rect)); + fData[0] = fBounds; + } else if (fData) { + clipping_rect* resizedData = (clipping_rect*)realloc(fData, + newSize * sizeof(clipping_rect)); + if (!resizedData) { + // failed to resize, but we cannot keep the + // previous state of the object + free(fData); + fData = NULL; + } else + fData = resizedData; + } else + fData = (clipping_rect*)malloc(newSize * sizeof(clipping_rect)); + } else { + // just an empty region, but no error + MakeEmpty(); + return true; + } - data = (clipping_rect *)realloc(data, new_size * sizeof(clipping_rect)); - - if (data == NULL) - debugger("BRegion::set_size realloc error\n"); - - data_size = new_size; - - ASSERT(count <= data_size); + if (!fData) { + // allocation actually failed + fDataSize = 0; + MakeEmpty(); + return false; + } + + fDataSize = newSize; + return true; } + +clipping_rect +BRegion::_Convert(const BRect& rect) const +{ + return (clipping_rect){ (int)floorf(rect.left), (int)floorf(rect.top), + (int)ceilf(rect.right), (int)ceilf(rect.bottom) }; +} + + +clipping_rect +BRegion::_ConvertToInternal(const BRect& rect) const +{ + return (clipping_rect){ (int)floorf(rect.left), (int)floorf(rect.top), + (int)ceilf(rect.right) + 1, (int)ceilf(rect.bottom) + 1 }; +} + + +clipping_rect +BRegion::_ConvertToInternal(const clipping_rect& rect) const +{ + return (clipping_rect){ rect.left, rect.top, + rect.right + 1, rect.bottom + 1 }; +} + diff --git a/src/build/libbe/interface/RegionSupport.cpp b/src/build/libbe/interface/RegionSupport.cpp index 0818408f30..71cf9487a6 100644 --- a/src/build/libbe/interface/RegionSupport.cpp +++ b/src/build/libbe/interface/RegionSupport.cpp @@ -1,915 +1,1637 @@ -//------------------------------------------------------------------------------ -// Copyright (c) 2003-2005, Haiku, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// -// File Name: RegionSupport.cpp -// Author: Stefano Ceccherini (burton666@libero.it) -// Description: Class that does the dirty work for BRegion. -// -//------------------------------------------------------------------------------ +/* $Xorg: Region.c,v 1.6 2001/02/09 02:03:35 xorgcvs Exp $ */ +/************************************************************************ -// TODO: check for possible performance issue in ROr() and RSub(). -// Check if inlining some methods can make us be faster. - -// Standard Includes ----------------------------------------------------------- -#include +Copyright 1987, 1988, 1998 The Open Group + +Permission to use, copy, modify, distribute, and sell this software and its +documentation for any purpose is hereby granted without fee, provided that +the above copyright notice appear in all copies and that both that +copyright notice and this permission notice appear in supporting +documentation. + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of The Open Group shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from The Open Group. + + +Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +************************************************************************/ +/* $XFree86: xc/lib/X11/Region.c,v 1.9 2002/06/04 22:19:57 dawes Exp $ */ +/* + * The functions in this file implement the BRegion* abstraction, similar to one + * used in the X11 sample server. A BRegion* is simply an area, as the name + * implies, and is implemented as a "y-x-banded" array of rectangles. To + * explain: Each BRegion* is made up of a certain number of rectangles sorted + * by y coordinate first, and then by x coordinate. + * + * Furthermore, the rectangles are banded such that every rectangle with a + * given upper-left y coordinate (top) will have the same lower-right y + * coordinate (bottom) and vice versa. If a rectangle has scanlines in a band, it + * will span the entire vertical distance of the band. This means that some + * areas that could be merged into a taller rectangle will be represented as + * several shorter rectangles to account for shorter rectangles to its left + * or right but within its "vertical scope". + * + * An added constraint on the rectangles is that they must cover as much + * horizontal area as possible. E.g. no two rectangles in a band are allowed + * to touch. + * + * Whenever possible, bands will be merged together to cover a greater vertical + * distance (and thus reduce the number of rectangles). Two bands can be merged + * only if the bottom of one touches the top of the other and they have + * rectangles in the same places (of the same width, of course). This maintains + * the y-x-banding that's so nice to have... + */ + +#include "RegionSupport.h" + +#include #include -// System Includes ------------------------------------------------------------- -#include -#include +using std::nothrow; -// Private Includes ------------------------------------------------------------- -#include -#include - -// Constants -------------------------------------------------------------------- -static const int32 kMaxPoints = 1024; -static const int32 kMaxVerticalExtent = 0x10000000; -static const int32 kMaxPositive = 0x7ffffffd; -static const int32 kMaxNegative = 0x80000003; +#include -#define TRACE_REGION 0 -#define ARGS (const char *, ...) -#if TRACE_REGION - #define RTRACE(ARGS) printf ARGS - #define CALLED() printf("%s\n", __PRETTY_FUNCTION__) +#ifdef DEBUG +#include +#define assert(expr) {if (!(expr)) fprintf(stderr,\ +"Assertion failed file %s, line %d: " #expr "\n", __FILE__, __LINE__); } #else - #define RTRACE(ARGS) ; - #define CALLED() +#define assert(expr) #endif -using namespace std; -/*! \brief zeroes the given region, setting its rect count to 0, - and invalidating its bound rectangle. - \param region The region to be zeroed. -*/ -void -BRegion::Support::ZeroRegion(BRegion ®ion) +/* 1 if two clipping_rects overlap. + * 0 if two clipping_rects do not overlap. + * Remember, right and bottom are not in the region + */ +#define EXTENTCHECK(r1, r2) \ + ((r1)->right > (r2)->left && \ + (r1)->left < (r2)->right && \ + (r1)->bottom > (r2)->top && \ + (r1)->top < (r2)->bottom) + +/* + * update region fBounds + */ +#define EXTENTS(r,idRect){\ + if((r)->left < (idRect)->fBounds.left)\ + (idRect)->fBounds.left = (r)->left;\ + if((r)->top < (idRect)->fBounds.top)\ + (idRect)->fBounds.top = (r)->top;\ + if((r)->right > (idRect)->fBounds.right)\ + (idRect)->fBounds.right = (r)->right;\ + if((r)->bottom > (idRect)->fBounds.bottom)\ + (idRect)->fBounds.bottom = (r)->bottom;\ + } + +/* + * Check to see if there is enough memory in the present region. + */ +#define MEMCHECK(reg, rect, firstrect){\ + if ((reg)->fCount >= ((reg)->fDataSize - 1)){\ + (firstrect) = (clipping_rect *) realloc \ + ((char *)(firstrect), (unsigned) (2 * (sizeof(clipping_rect)) * ((reg)->fDataSize)));\ + if ((firstrect) == 0)\ + return(0);\ + (reg)->fDataSize *= 2;\ + (rect) = &(firstrect)[(reg)->fCount];\ + }\ + } + +/* this routine checks to see if the previous rectangle is the same + * or subsumes the new rectangle to add. + */ + +#define CHECK_PREVIOUS(Reg, R, Rx1, Ry1, Rx2, Ry2)\ + (!(((Reg)->fCount > 0)&&\ + ((R-1)->top == (Ry1)) &&\ + ((R-1)->bottom == (Ry2)) &&\ + ((R-1)->left <= (Rx1)) &&\ + ((R-1)->right >= (Rx2)))) + +/* add a rectangle to the given BRegion */ +#define ADDRECT(reg, r, rx1, ry1, rx2, ry2){\ + if (((rx1) < (rx2)) && ((ry1) < (ry2)) &&\ + CHECK_PREVIOUS((reg), (r), (rx1), (ry1), (rx2), (ry2))){\ + (r)->left = (rx1);\ + (r)->top = (ry1);\ + (r)->right = (rx2);\ + (r)->bottom = (ry2);\ + EXTENTS((r), (reg));\ + (reg)->fCount++;\ + (r)++;\ + }\ + } + + + +/* add a rectangle to the given BRegion */ +#define ADDRECTNOX(reg, r, rx1, ry1, rx2, ry2){\ + if ((rx1 < rx2) && (ry1 < ry2) &&\ + CHECK_PREVIOUS((reg), (r), (rx1), (ry1), (rx2), (ry2))){\ + (r)->left = (rx1);\ + (r)->top = (ry1);\ + (r)->right = (rx2);\ + (r)->bottom = (ry2);\ + (reg)->fCount++;\ + (r)++;\ + }\ + } + +#define EMPTY_REGION(pReg) pReg->MakeEmpty() + +#define REGION_NOT_EMPTY(pReg) pReg->fCount + +#define INBOX(r, x, y) \ + ( ( ((r).right > x)) && \ + ( ((r).left <= x)) && \ + ( ((r).bottom > y)) && \ + ( ((r).top <= y)) ) + + + +/* Create a new empty region */ +BRegion* +BRegion::Support::CreateRegion(void) { - CALLED(); - - region.count = 0; - region.bound.left = kMaxPositive; - region.bound.top = kMaxPositive; - region.bound.right = kMaxNegative; - region.bound.bottom = kMaxNegative; + return new (nothrow) BRegion(); +} + +void +BRegion::Support::DestroyRegion(BRegion* r) +{ + delete r; } -/*! \brief clear the given region, setting its rect count to 0, - and setting its bound rectangle to 0xFFFFFFF, 0xFFFFFFF, 0xF0000001, 0xF0000001. - \param region The region to be cleared. -*/ +/*- + *----------------------------------------------------------------------- + * miSetExtents -- + * Reset the fBounds of a region to what they should be. Called by + * miSubtract and miIntersect b/c they can't figure it out along the + * way or do so easily, as miUnion can. + * + * Results: + * None. + * + * Side Effects: + * The region's 'fBounds' structure is overwritten. + * + *----------------------------------------------------------------------- + */ void -BRegion::Support::ClearRegion(BRegion ®ion) +BRegion::Support::miSetExtents(BRegion* pReg) { - CALLED(); + register clipping_rect* pBox; + clipping_rect* pBoxEnd; + clipping_rect* pExtents; - // TODO: What is it used for ? - // Could be that a cleared region represents an infinite one ? - - region.count = 0; - region.bound.left = 0xfffffff; - region.bound.top = 0xfffffff; - region.bound.right = 0xf0000001; - region.bound.bottom = 0xf0000001; -} + if (pReg->fCount == 0) + { + pReg->fBounds.left = 0; + pReg->fBounds.top = 0; + pReg->fBounds.right = 0; + pReg->fBounds.bottom = 0; + return; + } + pExtents = &pReg->fBounds; + pBox = pReg->fData; + pBoxEnd = &pBox[pReg->fCount - 1]; -/*! \brief Copy a region to another. - \param source The region to be copied. - \param dest The destination region. -*/ -void -BRegion::Support::CopyRegion(const BRegion &source, BRegion &dest) -{ - CALLED(); - - // If there is not enough memory, allocate - if (dest.data_size < source.count) { - free(dest.data); - dest.data_size = source.count + 8; - dest.data = (clipping_rect *)malloc(dest.data_size * sizeof(clipping_rect)); + /* + * Since pBox is the first rectangle in the region, it must have the + * smallest top and since pBoxEnd is the last rectangle in the region, + * it must have the largest bottom, because of banding. Initialize left and + * right from pBox and pBoxEnd, resp., as good things to initialize them + * to... + */ + pExtents->left = pBox->left; + pExtents->top = pBox->top; + pExtents->right = pBoxEnd->right; + pExtents->bottom = pBoxEnd->bottom; + + assert(pExtents->top < pExtents->bottom); + while (pBox <= pBoxEnd) + { + if (pBox->left < pExtents->left) + { + pExtents->left = pBox->left; } - - dest.count = source.count; - - // Copy rectangles and bounds. - memcpy(dest.data, source.data, source.count * sizeof(clipping_rect)); - dest.bound = source.bound; + if (pBox->right > pExtents->right) + { + pExtents->right = pBox->right; + } + pBox++; + } + assert(pExtents->left < pExtents->right); } -/*! \brief Modify the destination region to be the intersection of the two given regions. - \param first The first region to be intersected. - \param second The second region to be intersected. - \param dest The destination region. - - This function is a sort of method selector. It checks for some special - cases, then it calls the appropriate specialized function. +/* TranslateRegion(pRegion, x, y) + translates in place + added by raymond */ void -BRegion::Support::AndRegion(const BRegion &first, const BRegion &second, - BRegion &dest) +BRegion::Support::XOffsetRegion( + register BRegion* pRegion, + register int x, + register int y) { - CALLED(); - - clipping_rect intersection = sect_rect(first.bound, second.bound); - - if (first.count == 0 || second.count == 0 || !valid_rect(intersection)) - ZeroRegion(dest); - - else if (first.count == 1 && second.count == 1) { - dest.data[0] = intersection; - dest.bound = intersection; - dest.count = 1; - - } else if (first.count > 1 && second.count == 1) - AndRegion1ToN(second, first, dest); - - else if (first.count == 1 && second.count > 1) - AndRegion1ToN(first, second, dest); - - else - AndRegionComplex(first, second, dest); + register int nbox; + register clipping_rect *pbox; + + pbox = pRegion->fData; + nbox = pRegion->fCount; + + while(nbox--) + { + pbox->left += x; + pbox->right += x; + pbox->top += y; + pbox->bottom += y; + pbox++; + } + pRegion->fBounds.left += x; + pRegion->fBounds.right += x; + pRegion->fBounds.top += y; + pRegion->fBounds.bottom += y; } +/* + Utility procedure Compress: + Replace r by the region r', where + p in r' iff (Quantifer m <= dx) (p + m in r), and + Quantifier is Exists if grow is true, For all if grow is false, and + (x,y) + m = (x+m,y) if xdir is true; (x,y+m) if xdir is false. -/*! \brief Modify the destination region to be the union of the two given regions. - \param first The first region to be merged. - \param second The second region to be merged. - \param dest The destination region. - - This function is a sort of method selector. It checks for some special - cases, then it calls the appropriate specialized function. + Thus, if xdir is true and grow is false, r is replaced by the region + of all points p such that p and the next dx points on the same + horizontal scan line are all in r. We do this using by noting + that p is the head of a run of length 2^i + k iff p is the head + of a run of length 2^i and p+2^i is the head of a run of length + k. Thus, the loop invariant: s contains the region corresponding + to the runs of length shift. r contains the region corresponding + to the runs of length 1 + dxo & (shift-1), where dxo is the original + value of dx. dx = dxo & ~(shift-1). As parameters, s and t are + scratch regions, so that we don't have to allocate them on every + call. */ -void -BRegion::Support::OrRegion(const BRegion &first, const BRegion &second, - BRegion &dest) -{ - CALLED(); - - // A little trick, to save some work... - const BRegion *regionA; - const BRegion *regionB; - if (first.count != 0) { - regionA = &first; - regionB = &second; - } else { - regionA = &second; - regionB = &first; - } - - if (regionB->count == 0) - CopyRegion(*regionA, dest); - else { - if (regionB->bound.top > regionA->bound.bottom) - AppendRegion(*regionA, *regionB, dest); - - else if (regionA->bound.top > regionB->bound.bottom) - AppendRegion(*regionB, *regionA, dest); - - else if (regionA->bound.left > regionB->bound.right) - OrRegionNoX(*regionB, *regionA, dest); - - else if (regionB->bound.left > regionA->bound.right) - OrRegionNoX(*regionA, *regionB, dest); - - else if (regionA->count == 1) - OrRegion1ToN(*regionA, *regionB, dest); - - else if (regionB->count == 1) - OrRegion1ToN(*regionB, *regionA, dest); - - else - OrRegionComplex(*regionA, *regionB, dest); - } -} +#if 0 +#define ZOpRegion(a,b,c) if (grow) BRegion::Support::XUnionRegion(a,b,c); \ + else BRegion::Support::XIntersectRegion(a,b,c) +#define ZShiftRegion(a,b) if (xdir) BRegion::Support::XOffsetRegion(a,b,0); \ + else BRegion::Support::XOffsetRegion(a,0,b) +#define ZCopyRegion(a,b) BRegion::Support::XUnionRegion(a,a,b) -/*! \brief Modify the destination region to be the difference of the two given regions. - \param first The subtraend region. - \param second The minuend region. - \param dest The destination region. - - This function is a sort of method selector. It checks for some special - cases, then it calls the appropriate specialized function. -*/ -void -BRegion::Support::SubRegion(const BRegion &first, const BRegion &second, - BRegion &dest) -{ - CALLED(); - if (first.count == 0) - ZeroRegion(dest); - - else if (second.count == 0 || !rects_intersect(first.bound, second.bound)) - CopyRegion(first, dest); - - else - SubRegionComplex(second, first, dest); -} - - -/*! \brief Cleanup the region, by merging rects that can be merged. - \param region The region to be cleaned. -*/ -void -BRegion::Support::CleanupRegion(BRegion ®ion) -{ - CALLED(); - - long oldCount; - - do { - oldCount = region.count; - CleanupRegionVertical(region); - CleanupRegionHorizontal(region); - } while (region.count < oldCount); -} - - -/*! \brief Cleanup the region vertically. - \param region The region to be cleaned. -*/ -void -BRegion::Support::CleanupRegionVertical(BRegion ®ion) -{ - CALLED(); - - clipping_rect testRect = { 1, 1, -1, -2 }; - long newCount = -1; - - for (long x = 0; x < region.count; x++) { - clipping_rect &rect = region.data[x]; - - if (rect.left == testRect.left && rect.right == testRect.right - && rect.top == testRect.bottom + 1) { - - ASSERT(newCount >= 0); - region.data[newCount].bottom = rect.bottom; - - } else { - newCount++; - region.data[newCount] = region.data[x]; - testRect = region.data[x]; - } - } - region.count = newCount + 1; -} - - -/*! \brief Cleanup the region horizontally. - \param region The region to be cleaned. -*/ -void -BRegion::Support::CleanupRegionHorizontal(BRegion ®ion) -{ - CALLED(); - - clipping_rect testRect = { 1, 1, -2, -1 }; - long newCount = -1; - - for (long x = 0; x < region.count; x++) { - clipping_rect &rect = region.data[x]; - - if (rect.top == testRect.top && rect.bottom == testRect.bottom - && rect.left == testRect.right + 1) { - - ASSERT(newCount >= 0); - region.data[newCount].right = rect.right; - - } else { - newCount++; - region.data[newCount] = rect; - } - testRect = region.data[newCount]; - } - region.count = newCount + 1; -} - - -// Helper method to swap two rects -static inline void -SwapRects(clipping_rect &rect, clipping_rect &anotherRect) -{ - clipping_rect tmpRect; - tmpRect = rect; - rect = anotherRect; - anotherRect = tmpRect; -} - - -/*! \brief Sorts the given rects by their top value. - \param rects A pointer to an array of clipping_rects. - \param count The number of rectangles in the array. -*/ -void -BRegion::Support::SortRects(clipping_rect *rects, long count) -{ - CALLED(); - - bool again; //flag that tells we changed rects positions - - if (count == 2) { - if (rects[0].top > rects[1].top) - SwapRects(rects[0], rects[1]); - - } else if (count > 2) { - do { - again = false; - for (long c = 1; c < count; c++) { - if (rects[c - 1].top > rects[c].top) { - SwapRects(rects[c - 1], rects[c]); - again = true; - } - } - } while (again); - } -} - - -// Helper methods to swap transition points in two given arrays -static inline void -SwapTrans(int32 *leftPoints, int32 *rightPoints, long index1, long index2) -{ - // First, swap the left points - long tmp = leftPoints[index1]; - leftPoints[index1] = leftPoints[index2]; - leftPoints[index2] = tmp; - - // then the right points - tmp = rightPoints[index1]; - rightPoints[index1] = rightPoints[index2]; - rightPoints[index2] = tmp; -} - - -void -BRegion::Support::SortTrans(int32 *lptr1, int32 *lptr2, long count) -{ - CALLED(); - - bool again; //flag that tells we changed trans positions - - if (count == 2) { - if (lptr1[0] > lptr1[1]) - SwapTrans(lptr1, lptr2, 0, 1); - - } else if (count > 2) { - do { - again = false; - for (long c = 1; c < count; c++) { - if (lptr1[c - 1] > lptr1[c]) { - SwapTrans(lptr1, lptr2, c - 1, c); - again = true; - } - } - } while (again); - } -} - - -/*! \brief Copy a region to another, allocating some additional memory in the destination region. - \param source The region to be copied. - \param dest The destination region. - \param count Amount of additional memory to be allocated in the destination region. -*/ -void -BRegion::Support::CopyRegionMore(const BRegion &source, BRegion &dest, - long count) -{ - CALLED(); - - // If there is not enough memory, allocate - if (dest.data_size < source.count) { - free(dest.data); - dest.data_size = source.count + count; - dest.data = (clipping_rect *)malloc(dest.data_size * sizeof(clipping_rect)); - } - - dest.count = source.count; - - // Copy rectangles and bounds. - memcpy(dest.data, source.data, source.count * sizeof(clipping_rect)); - dest.bound = source.bound; -} - - -/*! \brief Modify the destination region to be the intersection of the two given regions. - \param first The first region to be intersected. - \param second The second region to be intersected. - \param dest The destination region. - - Called by and_region() when the intersection is complex. -*/ -void -BRegion::Support::AndRegionComplex(const BRegion &first, const BRegion &second, - BRegion &dest) -{ - CALLED(); - ZeroRegion(dest); - - for (long f = 0; f < first.count; f++) { - for (long s = 0; s < second.count; s++) { - clipping_rect testRect = sect_rect(first.data[f], second.data[s]); - if (valid_rect(testRect)) - dest._AddRect(testRect); - } - } - - if (dest.count > 1) - SortRects(dest.data, dest.count); -} - - -/*! \brief Modify the destination region to be the intersection of the two given regions. - \param first The first region to be intersected. - \param second The second region to be intersected. - \param dest The destination region. - - Called by and_region() when one of the two region contains just one rect. -*/ -void -BRegion::Support::AndRegion1ToN(const BRegion &first, const BRegion &second, - BRegion &dest) -{ - CALLED(); - // The easy case first: We already know that the regions intersect, - // so we check if the first region contains the second. - // If it's the case, the intersection is exactly the second region. - if (first.bound.top <= second.bound.top - && first.bound.bottom >= second.bound.bottom - && first.bound.left <= second.bound.left - && first.bound.right >= second.bound.right) - CopyRegion(second, dest); - else { - // Otherwise, we check the rect of the first region against the rects - // of the second, and we add their intersections to the destination region - ZeroRegion(dest); - for (long x = 0; x < second.count; x++) { - clipping_rect testRect = sect_rect(first.data[0], second.data[x]); - if (valid_rect(testRect)) - dest._AddRect(testRect); - } - } -} - - -/*! \brief Modify the destination region to be the union of the two given regions. - \param first The first region to be or-ed. - \param second The second region to be or-ed. - \param dest The destination region. - - This function is called by or_region when the two regions don't intersect, - and when the second region top coordinate is bigger than first region's bottom - coordinate. -*/ -void -BRegion::Support::AppendRegion(const BRegion &first, const BRegion &second, - BRegion &dest) -{ - CALLED(); - CopyRegion(first, dest); - for (long c = 0; c < second.count; c++) - dest._AddRect(second.data[c]); -} - - -void -BRegion::Support::ROr(long top, long bottom, const BRegion &first, - const BRegion &second, BRegion &dest, long *indexA, long *indexB) -{ - CALLED(); - - int32 stackLefts[kMaxPoints]; - int32 stackRights[kMaxPoints]; - - int32 *lefts = stackLefts; - int32 *rights = stackRights; - - long i1 = *indexA; - long i2 = *indexB; - - *indexA = -1; - *indexB = -1; - - long foundCount = 0; - long x = 0; - - // allocate arrays on the heap, if the ones one the stack are too small - int32 *allocatedBuffer = NULL; - int32 maxCount = first.count - i1 + second.count - i2; - - if (maxCount > kMaxPoints) { - RTRACE(("Stack space isn't sufficient. Allocating %ld bytes on the heap...\n", - 2 * maxCount)); - lefts = allocatedBuffer = new(nothrow) int32[2 * maxCount]; - if (!allocatedBuffer) - return; - rights = allocatedBuffer + maxCount; - } - - // Store left and right points to the appropriate array - for (x = i1; x < first.count; x++) { - - // Look if this rect can be used next time we are called, - // thus correctly maintaining the "index" parameters. - if (first.data[x].bottom >= top && *indexA == -1) - *indexA = x; - - if (first.data[x].top <= top && first.data[x].bottom >= bottom) { - lefts[foundCount] = first.data[x].left; - rights[foundCount] = first.data[x].right; - foundCount++; - } else if (first.data[x].top > bottom) - break; - } - - if (*indexA == -1) - *indexA = i1; - - for (x = i2; x < second.count; x++) { - if (second.data[x].bottom >= top && *indexB == -1) - *indexB = x; - - if (second.data[x].top <= top && second.data[x].bottom >= bottom) { - lefts[foundCount] = second.data[x].left; - rights[foundCount] = second.data[x].right; - foundCount++; - } else if (second.data[x].top > bottom) - break; - } - - if (*indexB == -1) - *indexB = i2; - - if (foundCount > 1) - SortTrans(lefts, rights, foundCount); - - ASSERT(foundCount > 0); - - clipping_rect rect; - rect.top = top; - rect.bottom = bottom; - - // Check if a rect intersects with the next one. - // If so, merge the two rects, if not, just add the rect. - long current = 0; - while (current < foundCount) { - long next = current + 1; - - rect.left = lefts[current]; - rect.right = rights[current]; - - while (next < foundCount && rect.right >= lefts[next]) { - if (rect.right < rights[next]) - rect.right = rights[next]; - next++; - } - - dest._AddRect(rect); - current = next; - } - - if (allocatedBuffer) { - RTRACE(("Freeing heap...\n")); - delete[] allocatedBuffer; - } -} - - -/*! \brief Divides the plane into horizontal bands, then passes those bands to r_or - which does the real work. - \param first The first region to be or-ed. - \param second The second region to be or-ed. - \param dest The destination region. -*/ -void -BRegion::Support::OrRegionComplex(const BRegion &first, const BRegion &second, - BRegion &dest) -{ - CALLED(); - long a = 0, b = 0; - - int32 top; - int32 bottom = min_c(first.bound.top, second.bound.top) - 1; - do { - long x; - top = bottom + 1; - bottom = kMaxVerticalExtent; - - for (x = a; x < first.count; x++) { - int32 n = first.data[x].top - 1; - if (n >= top && n < bottom) - bottom = n; - if (first.data[x].bottom >= top && first.data[x].bottom < bottom) - bottom = first.data[x].bottom; - } - - for (x = b; x < second.count; x++) { - int32 n = second.data[x].top - 1; - if (n >= top && n < bottom) - bottom = n; - if (second.data[x].bottom >= top && second.data[x].bottom < bottom) - bottom = second.data[x].bottom; - } - - // We can stand a region which extends to kMaxVerticalExtent, not more - if (bottom >= kMaxVerticalExtent) - break; - - ROr(top, bottom, first, second, dest, &a, &b); - - } while (true); - - CleanupRegion(dest); -} - - -/*! \brief Modify the destination region to be the union of the two given regions. - \param first The first region to be or-ed. - \param second The second region to be or-ed. - \param dest The destination region. - - This function is called by or_region when one of the two regions contains just - one rect. -*/ -void -BRegion::Support::OrRegion1ToN(const BRegion &first, const BRegion &second, - BRegion &dest) -{ - CALLED(); - // The easy case first: if the first region contains the second, - // the union is exactly the first region, since its bound is the - // only rectangle. - if (first.bound.top <= second.bound.top - && first.bound.bottom >= second.bound.bottom - && first.bound.left <= second.bound.left - && first.bound.right >= second.bound.right) - CopyRegion(first, dest); - else - OrRegionComplex(first, second, dest); -} - - -/*! \brief Modify the destination region to be the union of the two given regions. - \param first The first region to be or-ed. - \param second The second region to be or-ed. - \param dest The destination region. - - This function is called by or_region when the two regions don't intersect. -*/ -void -BRegion::Support::OrRegionNoX(const BRegion &first, const BRegion &second, - BRegion &dest) -{ - CALLED(); - ZeroRegion(dest); - - long x; - - if (first.count == 0) { - for (x = 0; x < second.count; x++) - dest._AddRect(second.data[x]); - - } else if (second.count == 0) { - for (x = 0; x < first.count; x++) - dest._AddRect(first.data[x]); - - } else { - long f = 0, s = 0; - - while (f < first.count && s < second.count) { - - if (first.data[f].top < second.data[s].top) { - dest._AddRect(first.data[f]); - f++; - - } else { - dest._AddRect(second.data[s]); - s++; - } - } - - if (f == first.count) - for (; s < second.count; s++) - dest._AddRect(second.data[s]); - - else if (s == second.count) - for (; f < first.count; f++) - dest._AddRect(first.data[f]); - } -} - - -/*! \brief Divides the plane into horizontal bands, then passes those bands to r_sub - which does the real work. - \param first The subtraend region. - \param second The minuend region. - \param dest The destination region. -*/ -void -BRegion::Support::SubRegionComplex(const BRegion &first, const BRegion &second, - BRegion &dest) -{ - CALLED(); - long a = 0, b = 0; - - int32 top; - int32 bottom = min_c(first.bound.top, second.bound.top) - 1; - - do { - long x; - top = bottom + 1; - bottom = kMaxVerticalExtent; - - for (x = a; x < first.count; x++) { - int32 n = first.data[x].top - 1; - if (n >= top && n < bottom) - bottom = n; - if (first.data[x].bottom >= top && first.data[x].bottom < bottom) - bottom = first.data[x].bottom; - } - - for (x = b; x < second.count; x++) { - int32 n = second.data[x].top - 1; - if (n >= top && n < bottom) - bottom = n; - if (second.data[x].bottom >= top && second.data[x].bottom < bottom) - bottom = second.data[x].bottom; - } - - if (bottom >= kMaxVerticalExtent) - break; - - RSub(top, bottom, first, second, dest, &a, &b); - - } while (true); - - CleanupRegion(dest); -} - - -/*! \brief Converts the empty spaces between rectangles to rectangles, - and the rectangles to empty spaces. - - Watch out!!! We write 1 element more than count, so be sure that the passed - arrays have the needed space - // TODO: Find a better name for this function -*/ static void -InvertRectangles(int32 *lefts, int32 *rights, long count) +Compress( + BRegion* r, BRegion* s, BRegion* t, + register unsigned dx, + register int xdir, register int grow) { - int32 tmpLeft, tmpRight = kMaxNegative; - - for (int i = 0; i <= count; i++) { - tmpLeft = lefts[i] - 1; - - lefts[i] = (i == 0) ? kMaxNegative : tmpRight; - tmpRight = rights[i] + 1; - - rights[i] = (i == count) ? kMaxPositive : tmpLeft; - } + register unsigned shift = 1; + + ZCopyRegion(r, s); + while (dx) { + if (dx & shift) { + ZShiftRegion(r, -(int)shift); + ZOpRegion(r, s, r); + dx -= shift; + if (!dx) break; + } + ZCopyRegion(s, t); + ZShiftRegion(s, -(int)shift); + ZOpRegion(s, t, s); + shift <<= 1; + } } +#undef ZOpRegion +#undef ZShiftRegion +#undef ZCopyRegion + +int +XShrinkRegion( + BRegion* r, + int dx, int dy) +{ + BRegion* s; + BRegion* t; + int grow; + + if (!dx && !dy) return 0; + if ((! (s = CreateRegion())) || (! (t = CreateRegion()))) return 0; + if ((grow = (dx < 0))) dx = -dx; + if (dx) Compress(r, s, t, (unsigned) 2*dx, true, grow); + if ((grow = (dy < 0))) dy = -dy; + if (dy) Compress(r, s, t, (unsigned) 2*dy, false, grow); + XOffsetRegion(r, dx, dy); + DestroyRegion(s); + DestroyRegion(t); + return 0; +} + +#ifdef notdef +/*********************************************************** + * Bop down the array of fData until we have passed + * scanline y. fCount is the fDataSize of the array. + ***********************************************************/ + +static clipping_rect* +IndexRects( + register clipping_rect *rect, + register int rectCount, + register int y) +{ + while ((rectCount--) && (rect->bottom <= y)) + rect++; + return(rect); +} +#endif +#endif // 0 + +/*====================================================================== + * BRegion* Intersection + *====================================================================*/ +/*- + *----------------------------------------------------------------------- + * miIntersectO -- + * Handle an overlapping band for miIntersect. + * + * Results: + * None. + * + * Side Effects: + * Rectangles may be added to the region. + * + *----------------------------------------------------------------------- + */ +int +BRegion::Support::miIntersectO ( + register BRegion* pReg, + register clipping_rect* r1, + clipping_rect* r1End, + register clipping_rect* r2, + clipping_rect* r2End, + int top, + int bottom) +{ + register int left; + register int right; + register clipping_rect* pNextRect; + + pNextRect = &pReg->fData[pReg->fCount]; + + while ((r1 != r1End) && (r2 != r2End)) + { + left = max_c(r1->left,r2->left); + right = min_c(r1->right,r2->right); + + /* + * If there's any overlap between the two rectangles, add that + * overlap to the new region. + * There's no need to check for subsumption because the only way + * such a need could arise is if some region has two rectangles + * right next to each other. Since that should never happen... + */ + if (left < right) + { + assert(topfData); + pNextRect->left = left; + pNextRect->top = top; + pNextRect->right = right; + pNextRect->bottom = bottom; + pReg->fCount += 1; + pNextRect++; + assert(pReg->fCount <= pReg->fDataSize); + } + + /* + * Need to advance the pointers. Shift the one that extends + * to the right the least, since the other still has a chance to + * overlap with that region's next rectangle, if you see what I mean. + */ + if (r1->right < r2->right) + { + r1++; + } + else if (r2->right < r1->right) + { + r2++; + } + else + { + r1++; + r2++; + } + } + return 0; /* lint */ +} + +int +BRegion::Support::XIntersectRegion( + const BRegion* reg1, + const BRegion* reg2, /* source regions */ + register BRegion* newReg) /* destination BRegion* */ +{ + /* check for trivial reject */ + if ( (!(reg1->fCount)) || (!(reg2->fCount)) || + (!EXTENTCHECK(®1->fBounds, ®2->fBounds))) + newReg->fCount = 0; + else + miRegionOp (newReg, reg1, reg2, + miIntersectO, NULL, NULL); + + /* + * Can't alter newReg's fBounds before we call miRegionOp because + * it might be one of the source regions and miRegionOp depends + * on the fBounds of those regions being the same. Besides, this + * way there's no checking against rectangles that will be nuked + * due to coalescing, so we have to examine fewer rectangles. + */ + miSetExtents(newReg); + return 1; +} void -BRegion::Support::RSub(long top, long bottom, const BRegion &first, - const BRegion &second, BRegion &dest, long *indexA, long *indexB) +BRegion::Support::miRegionCopy( + register BRegion* dstrgn, + register const BRegion* rgn) + { - CALLED(); - - // TODO: This function is really messy, although it does its work - // well enough: try to cleanup, and review especially heap/stack management - int32 stackLeftsA[kMaxPoints / 2]; - int32 stackLeftsB[kMaxPoints / 2]; - int32 stackRightsA[kMaxPoints / 2]; - int32 stackRightsB[kMaxPoints / 2]; + *dstrgn = *rgn; +} - int32 *leftsA = stackLeftsA; - int32 *leftsB = stackLeftsB; - int32 *rightsA = stackRightsA; - int32 *rightsB = stackRightsB; - - long i1 = *indexA; - long i2 = *indexB; - - *indexA = -1; - *indexB = -1; - - long foundA = 0; - long foundB = 0; - long x = 0; - - // allocate arrays on the heap, if the ones one the stack are too small - int32 *allocatedBuffer = NULL; - - // The +1 is needed here, see InvertRectangles() - int32 maxCountA = first.count - i1 + 1; - int32 maxCountB = second.count - i2 + 1; - - if (maxCountA + maxCountB > kMaxPoints) { - RTRACE(("Stack space isn't sufficient. Allocating %ld bytes on the heap...\n", - 2 * (maxCountA + maxCountB))); - leftsA = allocatedBuffer = new(nothrow) int32[2 * (maxCountA + maxCountB)]; - if (!allocatedBuffer) - return; - rightsA = allocatedBuffer + maxCountA; - leftsB = rightsA + maxCountA; - rightsB = leftsB + maxCountB; - } +#if 0 +#ifdef notdef - // Store left and right points to the appropriate array - for (x = i1; x < first.count; x++) { - // Look if this rect can be used next time we are called, - // thus correctly maintaining the "index" parameters. - if (first.data[x].bottom >= top && *indexA == -1) - *indexA = x; - - if (first.data[x].top <= top && first.data[x].bottom >= bottom) { - leftsA[foundA] = first.data[x].left; - rightsA[foundA] = first.data[x].right; - foundA++; - } else if (first.data[x].top > bottom) - break; +/* + * combinRegs(newReg, reg1, reg2) + * if one region is above or below the other. +*/ + +static void +combineRegs( + register BRegion* newReg, + BRegion* reg1, + BRegion* reg2) +{ + register BRegion* tempReg; + register clipping_rect *rects_; + register clipping_rect *rects1; + register clipping_rect *rects2; + register int total; + + rects1 = reg1->fData; + rects2 = reg2->fData; + + total = reg1->fCount + reg2->fCount; + if (! (tempReg = CreateRegion())) + return; + tempReg->fDataSize = total; + /* region 1 is below region 2 */ + if (reg1->fBounds.top > reg2->fBounds.top) + { + miRegionCopy(tempReg, reg2); + rects_ = &tempReg->fData[tempReg->fCount]; + total -= tempReg->fCount; + while (total--) + *rects_++ = *rects1++; + } + else + { + miRegionCopy(tempReg, reg1); + rects_ = &tempReg->fData[tempReg->fCount]; + total -= tempReg->fCount; + while (total--) + *rects_++ = *rects2++; + } + tempReg->fBounds = reg1->fBounds; + tempReg->fCount = reg1->fCount + reg2->fCount; + EXTENTS(®2->fBounds, tempReg); + miRegionCopy(newReg, tempReg); + + DestroyRegion(tempReg); +} + +/* + * QuickCheck checks to see if it does not have to go through all the + * the ugly code for the region call. It returns 1 if it did all + * the work for Union, otherwise 0 - still work to be done. +*/ + +static int +QuickCheck(BRegion* newReg, BRegion* reg1, BRegion* reg2) +{ + + /* if unioning with itself or no fData to union with */ + if ( (reg1 == reg2) || (!(reg1->fCount)) ) + { + miRegionCopy(newReg, reg2); + return true; + } + + /* if nothing to union */ + if (!(reg2->fCount)) + { + miRegionCopy(newReg, reg1); + return true; + } + + /* could put an extent check to see if add above or below */ + + if ((reg1->fBounds.top >= reg2->fBounds.bottom) || + (reg2->fBounds.top >= reg1->fBounds.bottom) ) + { + combineRegs(newReg, reg1, reg2); + return true; + } + return false; +} + +/* TopRects(fData, reg1, reg2) + * N.B. We now assume that reg1 and reg2 intersect. Therefore we are + * NOT checking in the two while loops for stepping off the end of the + * region. + */ + +static int +TopRects( + register BRegion* newReg, + register clipping_rect *rects_, + register BRegion* reg1, + register BRegion* reg2, + clipping_rect *FirstRect) +{ + register clipping_rect *tempRects; + + /* need to add some fData from region 1 */ + if (reg1->fBounds.top < reg2->fBounds.top) + { + tempRects = reg1->fData; + while(tempRects->top < reg2->fBounds.top) + { + MEMCHECK(newReg, rects_, FirstRect); + ADDRECTNOX(newReg,rects_, tempRects->left, tempRects->top, + tempRects->right, min_c(tempRects->bottom, reg2->fBounds.top)); + tempRects++; } - - for (x = i2; x < second.count; x++) { - if (second.data[x].bottom >= top && *indexB == -1) - *indexB = x; - - if (second.data[x].top <= top && second.data[x].bottom >= bottom) { - leftsB[foundB] = second.data[x].left; - rightsB[foundB] = second.data[x].right; - foundB++; - } else if (second.data[x].top > bottom) - break; + } + /* need to add some fData from region 2 */ + if (reg2->fBounds.top < reg1->fBounds.top) + { + tempRects = reg2->fData; + while (tempRects->top < reg1->fBounds.top) + { + MEMCHECK(newReg, rects_, FirstRect); + ADDRECTNOX(newReg, rects_, tempRects->left,tempRects->top, + tempRects->right, min_c(tempRects->bottom, reg1->fBounds.top)); + tempRects++; } + } + return 1; +} +#endif // notdef +#endif // 0 + +/*====================================================================== + * Generic BRegion* Operator + *====================================================================*/ + +/*- + *----------------------------------------------------------------------- + * miCoalesce -- + * Attempt to merge the boxes in the current band with those in the + * previous one. Used only by miRegionOp. + * + * Results: + * The new index for the previous band. + * + * Side Effects: + * If coalescing takes place: + * - rectangles in the previous band will have their bottom fields + * altered. + * - pReg->fCount will be decreased. + * + *----------------------------------------------------------------------- + */ +int +BRegion::Support::miCoalesce( + register BRegion* pReg, /* BRegion* to coalesce */ + int prevStart, /* Index of start of previous band */ + int curStart) /* Index of start of current band */ +{ + register clipping_rect* pPrevBox; /* Current box in previous band */ + register clipping_rect* pCurBox; /* Current box in current band */ + register clipping_rect* pRegEnd; /* End of region */ + int curNumRects; /* Number of rectangles in current + * band */ + int prevNumRects; /* Number of rectangles in previous + * band */ + int bandY1; /* Y1 coordinate for current band */ + + pRegEnd = &pReg->fData[pReg->fCount]; + + pPrevBox = &pReg->fData[prevStart]; + prevNumRects = curStart - prevStart; + + /* + * Figure out how many rectangles are in the current band. Have to do + * this because multiple bands could have been added in miRegionOp + * at the end when one region has been exhausted. + */ + pCurBox = &pReg->fData[curStart]; + bandY1 = pCurBox->top; + for (curNumRects = 0; + (pCurBox != pRegEnd) && (pCurBox->top == bandY1); + curNumRects++) + { + pCurBox++; + } + + if (pCurBox != pRegEnd) + { + /* + * If more than one band was added, we have to find the start + * of the last band added so the next coalescing job can start + * at the right place... (given when multiple bands are added, + * this may be pointless -- see above). + */ + pRegEnd--; + while (pRegEnd[-1].top == pRegEnd->top) + { + pRegEnd--; + } + curStart = pRegEnd - pReg->fData; + pRegEnd = pReg->fData + pReg->fCount; + } - if (*indexA == -1) - *indexA = i1; - if (*indexB == -1) - *indexB = i2; - - if (foundA > 1) - SortTrans(leftsA, rightsA, foundA); - - if (foundB > 1) - SortTrans(leftsB, rightsB, foundB); - - // No minuend's rect, just add all the subtraend's rects. - if (foundA == 0) { - for (x = 0; x < foundB; x++) { - clipping_rect rect = { leftsB[x], top, rightsB[x], bottom }; - dest._AddRect(rect); - } - } else if (foundB > 0) { - InvertRectangles(leftsA, rightsA, foundA); - - clipping_rect A, B; - A.top = B.top = top; - A.bottom = B.bottom = bottom; - - // Note: the <= is not an error. - for (long f = 0; f <= foundA; f++) { - for (long s = 0; s < foundB; s++) { - A.left = leftsA[f]; - A.right = rightsA[f]; - - B.left = leftsB[s]; - B.right = rightsB[s]; - if (rects_intersect(A, B)) - dest._AddRect(sect_rect(A, B)); - } + if ((curNumRects == prevNumRects) && (curNumRects != 0)) { + pCurBox -= curNumRects; + /* + * The bands may only be coalesced if the bottom of the previous + * matches the top scanline of the current. + */ + if (pPrevBox->bottom == pCurBox->top) + { + /* + * Make sure the bands have boxes in the same places. This + * assumes that boxes have been added in such a way that they + * cover the most area possible. I.e. two boxes in a band must + * have some horizontal space between them. + */ + do + { + if ((pPrevBox->left != pCurBox->left) || + (pPrevBox->right != pCurBox->right)) + { + /* + * The bands don't line up so they can't be coalesced. + */ + return (curStart); } + pPrevBox++; + pCurBox++; + prevNumRects -= 1; + } while (prevNumRects != 0); + + pReg->fCount -= curNumRects; + pCurBox -= curNumRects; + pPrevBox -= curNumRects; + + /* + * The bands may be merged, so set the bottom y of each box + * in the previous band to that of the corresponding box in + * the current band. + */ + do + { + pPrevBox->bottom = pCurBox->bottom; + pPrevBox++; + pCurBox++; + curNumRects -= 1; + } while (curNumRects != 0); + + /* + * If only one band was added to the region, we have to backup + * curStart to the start of the previous band. + * + * If more than one band was added to the region, copy the + * other bands down. The assumption here is that the other bands + * came from the same region as the current one and no further + * coalescing can be done on them since it's all been done + * already... curStart is already in the right place. + */ + if (pCurBox == pRegEnd) + { + curStart = prevStart; + } + else + { + do + { + *pPrevBox++ = *pCurBox++; + } while (pCurBox != pRegEnd); + } + + } + } + return (curStart); +} + +/*- + *----------------------------------------------------------------------- + * miRegionOp -- + * Apply an operation to two regions. Called by miUnion, miInverse, + * miSubtract, miIntersect... + * + * Results: + * None. + * + * Side Effects: + * The new region is overwritten. + * + * Notes: + * The idea behind this function is to view the two regions as sets. + * Together they cover a rectangle of area that this function divides + * into horizontal bands where points are covered only by one region + * or by both. For the first case, the nonOverlapFunc is called with + * each the band and the band's upper and lower fBounds. For the + * second, the overlapFunc is called to process the entire band. It + * is responsible for clipping the rectangles in the band, though + * this function provides the boundaries. + * At the end of each band, the new region is coalesced, if possible, + * to reduce the number of rectangles in the region. + * + *----------------------------------------------------------------------- + */ +void +BRegion::Support::miRegionOp( + register BRegion* newReg, /* Place to store result */ + const BRegion* reg1, /* First region in operation */ + const BRegion* reg2, /* 2d region in operation */ + int (*overlapFunc)( + register BRegion* pReg, + register clipping_rect* r1, + clipping_rect* r1End, + register clipping_rect* r2, + clipping_rect* r2End, + int top, + int bottom), /* Function to call for over- + * lapping bands */ + int (*nonOverlap1Func)( + register BRegion* pReg, + register clipping_rect* r, + clipping_rect* rEnd, + register int top, + register int bottom), /* Function to call for non- + * overlapping bands in region + * 1 */ + int (*nonOverlap2Func)( + register BRegion* pReg, + register clipping_rect* r, + clipping_rect* rEnd, + register int top, + register int bottom)) /* Function to call for non- + * overlapping bands in region + * 2 */ +{ + register clipping_rect* r1; /* Pointer into first region */ + register clipping_rect* r2; /* Pointer into 2d region */ + clipping_rect* r1End; /* End of 1st region */ + clipping_rect* r2End; /* End of 2d region */ + register int ybot; /* Bottom of intersection */ + register int ytop; /* Top of intersection */ + // clipping_rect* oldRects; /* Old fData for newReg */ + int prevBand; /* Index of start of + * previous band in newReg */ + int curBand; /* Index of start of current + * band in newReg */ + register clipping_rect* r1BandEnd; /* End of current band in r1 */ + register clipping_rect* r2BandEnd; /* End of current band in r2 */ + int top; /* Top of non-overlapping + * band */ + int bot; /* Bottom of non-overlapping + * band */ + + /* + * Initialization: + * set r1, r2, r1End and r2End appropriately, preserve the important + * parts of the destination region until the end in case it's one of + * the two source regions, then mark the "new" region empty, allocating + * another array of rectangles for it to use. + */ + r1 = reg1->fData; + r2 = reg2->fData; + r1End = r1 + reg1->fCount; + r2End = r2 + reg2->fCount; + +// oldRects = newReg->fData; + + EMPTY_REGION(newReg); + + /* + * Allocate a reasonable number of rectangles for the new region. The idea + * is to allocate enough so the individual functions don't need to + * reallocate and copy the array, which is time consuming, yet we don't + * have to worry about using too much memory. I hope to be able to + * nuke the realloc() at the end of this function eventually. + */ + if (!newReg->_SetSize(max_c(reg1->fCount,reg2->fCount) * 2)) { + return; + } + + /* + * Initialize ybot and ytop. + * In the upcoming loop, ybot and ytop serve different functions depending + * on whether the band being handled is an overlapping or non-overlapping + * band. + * In the case of a non-overlapping band (only one of the regions + * has points in the band), ybot is the bottom of the most recent + * intersection and thus clips the top of the rectangles in that band. + * ytop is the top of the next intersection between the two regions and + * serves to clip the bottom of the rectangles in the current band. + * For an overlapping band (where the two regions intersect), ytop clips + * the top of the rectangles of both regions and ybot clips the bottoms. + */ + if (reg1->fBounds.top < reg2->fBounds.top) + ybot = reg1->fBounds.top; + else + ybot = reg2->fBounds.top; + + /* + * prevBand serves to mark the start of the previous band so rectangles + * can be coalesced into larger rectangles. qv. miCoalesce, above. + * In the beginning, there is no previous band, so prevBand == curBand + * (curBand is set later on, of course, but the first band will always + * start at index 0). prevBand and curBand must be indices because of + * the possible expansion, and resultant moving, of the new region's + * array of rectangles. + */ + prevBand = 0; + + do + { + curBand = newReg->fCount; + + /* + * This algorithm proceeds one source-band (as opposed to a + * destination band, which is determined by where the two regions + * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the + * rectangle after the last one in the current band for their + * respective regions. + */ + r1BandEnd = r1; + while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top)) + { + r1BandEnd++; + } + + r2BandEnd = r2; + while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top)) + { + r2BandEnd++; + } + + /* + * First handle the band that doesn't intersect, if any. + * + * Note that attention is restricted to one band in the + * non-intersecting region at once, so if a region has n + * bands between the current position and the next place it overlaps + * the other, this entire loop will be passed through n times. + */ + if (r1->top < r2->top) + { + top = max_c(r1->top,ybot); + bot = min_c(r1->bottom,r2->top); + + if ((top != bot) && (nonOverlap1Func != NULL)) + { + (* nonOverlap1Func) (newReg, r1, r1BandEnd, top, bot); + } + + ytop = r2->top; + } + else if (r2->top < r1->top) + { + top = max_c(r2->top,ybot); + bot = min_c(r2->bottom,r1->top); + + if ((top != bot) && (nonOverlap2Func != NULL)) + { + (* nonOverlap2Func) (newReg, r2, r2BandEnd, top, bot); + } + + ytop = r1->top; + } + else + { + ytop = r1->top; } - if (allocatedBuffer) { - RTRACE(("Freeing heap...\n")); - delete[] allocatedBuffer; + /* + * If any rectangles got added to the region, try and coalesce them + * with rectangles from the previous band. Note we could just do + * this test in miCoalesce, but some machines incur a not + * inconsiderable cost for function calls, so... + */ + if (newReg->fCount != curBand) + { + prevBand = miCoalesce (newReg, prevBand, curBand); } + + /* + * Now see if we've hit an intersecting band. The two bands only + * intersect if ybot > ytop + */ + ybot = min_c(r1->bottom, r2->bottom); + curBand = newReg->fCount; + if (ybot > ytop) + { + (* overlapFunc) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot); + + } + + if (newReg->fCount != curBand) + { + prevBand = miCoalesce (newReg, prevBand, curBand); + } + + /* + * If we've finished with a band (bottom == ybot) we skip forward + * in the region to the next band. + */ + if (r1->bottom == ybot) + { + r1 = r1BandEnd; + } + if (r2->bottom == ybot) + { + r2 = r2BandEnd; + } + } while ((r1 != r1End) && (r2 != r2End)); + + /* + * Deal with whichever region still has rectangles left. + */ + curBand = newReg->fCount; + if (r1 != r1End) + { + if (nonOverlap1Func != NULL) + { + do + { + r1BandEnd = r1; + while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top)) + { + r1BandEnd++; + } + (* nonOverlap1Func) (newReg, r1, r1BandEnd, + max_c(r1->top,ybot), r1->bottom); + r1 = r1BandEnd; + } while (r1 != r1End); + } + } + else if ((r2 != r2End) && (nonOverlap2Func != NULL)) + { + do + { + r2BandEnd = r2; + while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top)) + { + r2BandEnd++; + } + (* nonOverlap2Func) (newReg, r2, r2BandEnd, + max_c(r2->top,ybot), r2->bottom); + r2 = r2BandEnd; + } while (r2 != r2End); + } + + if (newReg->fCount != curBand) + { + (void) miCoalesce (newReg, prevBand, curBand); + } + + /* + * A bit of cleanup. To keep regions from growing without bound, + * we shrink the array of rectangles to match the new number of + * rectangles in the region. This never goes to 0, however... + * + * Only do this stuff if the number of rectangles allocated is more than + * twice the number of rectangles in the region (a simple optimization...). + */ +// if (newReg->fCount < (newReg->fDataSize >> 1)) +// { +// if (REGION_NOT_EMPTY(newReg)) +// { +// clipping_rect* prev_rects = newReg->fData; +// newReg->fDataSize = newReg->fCount; +// newReg->fData = (clipping_rect*) realloc ((char *) newReg->fData, +// (unsigned) (sizeof(clipping_rect) * newReg->fDataSize)); +// if (! newReg->fData) +// newReg->fData = prev_rects; +// } +// else +// { +// /* +// * No point in doing the extra work involved in an realloc if +// * the region is empty +// */ +// newReg->fDataSize = 1; +// free((char *) newReg->fData); +// newReg->fData = (clipping_rect*) malloc(sizeof(clipping_rect)); +// } +// } +// free ((char *) oldRects); + return; } -#undef TRACE_REGION +/*====================================================================== + * BRegion* Union + *====================================================================*/ + +/*- + *----------------------------------------------------------------------- + * miUnionNonO -- + * Handle a non-overlapping band for the union operation. Just + * Adds the rectangles into the region. Doesn't have to check for + * subsumption or anything. + * + * Results: + * None. + * + * Side Effects: + * pReg->fCount is incremented and the final rectangles overwritten + * with the rectangles we're passed. + * + *----------------------------------------------------------------------- + */ +int +BRegion::Support::miUnionNonO(register BRegion* pReg, + register clipping_rect* r, clipping_rect* rEnd, + register int top, register int bottom) +{ + register clipping_rect* pNextRect = &pReg->fData[pReg->fCount]; + + assert(top < bottom); + + while (r != rEnd) { + assert(r->left < r->right); + MEMCHECK(pReg, pNextRect, pReg->fData); + pNextRect->left = r->left; + pNextRect->top = top; + pNextRect->right = r->right; + pNextRect->bottom = bottom; + pReg->fCount += 1; + pNextRect++; + + assert(pReg->fCount<=pReg->fDataSize); + r++; + } + return 0; +} + + +/*- + *----------------------------------------------------------------------- + * miUnionO -- + * Handle an overlapping band for the union operation. Picks the + * left-most rectangle each time and merges it into the region. + * + * Results: + * None. + * + * Side Effects: + * Rectangles are overwritten in pReg->fData and pReg->fCount will + * be changed. + * + *----------------------------------------------------------------------- + */ + +int +BRegion::Support::miUnionO ( + register BRegion* pReg, + register clipping_rect* r1, + clipping_rect* r1End, + register clipping_rect* r2, + clipping_rect* r2End, + register int top, + register int bottom) +{ + register clipping_rect* pNextRect; + + pNextRect = &pReg->fData[pReg->fCount]; + +#define MERGERECT(r) \ + if ((pReg->fCount != 0) && \ + (pNextRect[-1].top == top) && \ + (pNextRect[-1].bottom == bottom) && \ + (pNextRect[-1].right >= r->left)) \ + { \ + if (pNextRect[-1].right < r->right) \ + { \ + pNextRect[-1].right = r->right; \ + assert(pNextRect[-1].leftfData); \ + pNextRect->top = top; \ + pNextRect->bottom = bottom; \ + pNextRect->left = r->left; \ + pNextRect->right = r->right; \ + pReg->fCount += 1; \ + pNextRect += 1; \ + } \ + assert(pReg->fCount<=pReg->fDataSize);\ + r++; + + assert (topleft < r2->left) + { + MERGERECT(r1); + } + else + { + MERGERECT(r2); + } + } + + if (r1 != r1End) + { + do + { + MERGERECT(r1); + } while (r1 != r1End); + } + else while (r2 != r2End) + { + MERGERECT(r2); + } + return 0; /* lint */ +} + +int +BRegion::Support::XUnionRegion( + const BRegion* reg1, + const BRegion* reg2, /* source regions */ + BRegion* newReg) /* destination BRegion* */ +{ + /* checks all the simple cases */ + + /* + * BRegion* 1 and 2 are the same or region 1 is empty + */ + if ( (reg1 == reg2) || (!(reg1->fCount)) ) + { + if (newReg != reg2) + miRegionCopy(newReg, reg2); + return 1; + } + + /* + * if nothing to union (region 2 empty) + */ + if (!(reg2->fCount)) + { + if (newReg != reg1) + miRegionCopy(newReg, reg1); + return 1; + } + + /* + * BRegion* 1 completely subsumes region 2 + */ + if ((reg1->fCount == 1) && + (reg1->fBounds.left <= reg2->fBounds.left) && + (reg1->fBounds.top <= reg2->fBounds.top) && + (reg1->fBounds.right >= reg2->fBounds.right) && + (reg1->fBounds.bottom >= reg2->fBounds.bottom)) + { + if (newReg != reg1) + miRegionCopy(newReg, reg1); + return 1; + } + + /* + * BRegion* 2 completely subsumes region 1 + */ + if ((reg2->fCount == 1) && + (reg2->fBounds.left <= reg1->fBounds.left) && + (reg2->fBounds.top <= reg1->fBounds.top) && + (reg2->fBounds.right >= reg1->fBounds.right) && + (reg2->fBounds.bottom >= reg1->fBounds.bottom)) + { + if (newReg != reg2) + miRegionCopy(newReg, reg2); + return 1; + } + + miRegionOp (newReg, reg1, reg2, miUnionO, + miUnionNonO, miUnionNonO); + + newReg->fBounds.left = min_c(reg1->fBounds.left, reg2->fBounds.left); + newReg->fBounds.top = min_c(reg1->fBounds.top, reg2->fBounds.top); + newReg->fBounds.right = max_c(reg1->fBounds.right, reg2->fBounds.right); + newReg->fBounds.bottom = max_c(reg1->fBounds.bottom, reg2->fBounds.bottom); + + return 1; +} + + +/*====================================================================== + * BRegion* Subtraction + *====================================================================*/ + +/*- + *----------------------------------------------------------------------- + * miSubtractNonO -- + * Deal with non-overlapping band for subtraction. Any parts from + * region 2 we discard. Anything from region 1 we add to the region. + * + * Results: + * None. + * + * Side Effects: + * pReg may be affected. + * + *----------------------------------------------------------------------- + */ +int +BRegion::Support::miSubtractNonO1 ( + register BRegion* pReg, + register clipping_rect* r, + clipping_rect* rEnd, + register int top, + register int bottom) +{ + register clipping_rect* pNextRect; + + pNextRect = &pReg->fData[pReg->fCount]; + + assert(topleftright); + MEMCHECK(pReg, pNextRect, pReg->fData); + pNextRect->left = r->left; + pNextRect->top = top; + pNextRect->right = r->right; + pNextRect->bottom = bottom; + pReg->fCount += 1; + pNextRect++; + + assert(pReg->fCount <= pReg->fDataSize); + + r++; + } + return 0; /* lint */ +} + +/*- + *----------------------------------------------------------------------- + * miSubtractO -- + * Overlapping band subtraction. left is the left-most point not yet + * checked. + * + * Results: + * None. + * + * Side Effects: + * pReg may have rectangles added to it. + * + *----------------------------------------------------------------------- + */ +int +BRegion::Support::miSubtractO( + register BRegion* pReg, + register clipping_rect* r1, + clipping_rect* r1End, + register clipping_rect* r2, + clipping_rect* r2End, + register int top, + register int bottom) +{ + register clipping_rect* pNextRect; + register int left; + + left = r1->left; + + assert(topfData[pReg->fCount]; + + while ((r1 != r1End) && (r2 != r2End)) + { + if (r2->right <= left) + { + /* + * Subtrahend missed the boat: go to next subtrahend. + */ + r2++; + } + else if (r2->left <= left) + { + /* + * Subtrahend preceeds minuend: nuke left edge of minuend. + */ + left = r2->right; + if (left >= r1->right) + { + /* + * Minuend completely covered: advance to next minuend and + * reset left fence to edge of new minuend. + */ + r1++; + if (r1 != r1End) + left = r1->left; + } + else + { + /* + * Subtrahend now used up since it doesn't extend beyond + * minuend + */ + r2++; + } + } + else if (r2->left < r1->right) + { + /* + * Left part of subtrahend covers part of minuend: add uncovered + * part of minuend to region and skip to next subtrahend. + */ + assert(leftleft); + MEMCHECK(pReg, pNextRect, pReg->fData); + pNextRect->left = left; + pNextRect->top = top; + pNextRect->right = r2->left; + pNextRect->bottom = bottom; + pReg->fCount += 1; + pNextRect++; + + assert(pReg->fCount<=pReg->fDataSize); + + left = r2->right; + if (left >= r1->right) + { + /* + * Minuend used up: advance to new... + */ + r1++; + if (r1 != r1End) + left = r1->left; + } + else + { + /* + * Subtrahend used up + */ + r2++; + } + } + else + { + /* + * Minuend used up: add any remaining piece before advancing. + */ + if (r1->right > left) + { + MEMCHECK(pReg, pNextRect, pReg->fData); + pNextRect->left = left; + pNextRect->top = top; + pNextRect->right = r1->right; + pNextRect->bottom = bottom; + pReg->fCount += 1; + pNextRect++; + assert(pReg->fCount<=pReg->fDataSize); + } + r1++; + if (r1 != r1End) + left = r1->left; + } + } + + /* + * Add remaining minuend rectangles to region. + */ + while (r1 != r1End) + { + assert(leftright); + MEMCHECK(pReg, pNextRect, pReg->fData); + pNextRect->left = left; + pNextRect->top = top; + pNextRect->right = r1->right; + pNextRect->bottom = bottom; + pReg->fCount += 1; + pNextRect++; + + assert(pReg->fCount<=pReg->fDataSize); + + r1++; + if (r1 != r1End) + { + left = r1->left; + } + } + return 0; /* lint */ +} + +/*- + *----------------------------------------------------------------------- + * miSubtract -- + * Subtract regS from regM and leave the result in regD. + * S stands for subtrahend, M for minuend and D for difference. + * + * Results: + * true. + * + * Side Effects: + * regD is overwritten. + * + *----------------------------------------------------------------------- + */ + +int +BRegion::Support::XSubtractRegion( + const BRegion* regM, + const BRegion* regS, + register BRegion* regD) +{ + /* check for trivial reject */ + if ( (!(regM->fCount)) || (!(regS->fCount)) || + (!EXTENTCHECK(®M->fBounds, ®S->fBounds)) ) + { + miRegionCopy(regD, regM); + return 1; + } + + miRegionOp (regD, regM, regS, miSubtractO, + miSubtractNonO1, NULL); + + /* + * Can't alter newReg's fBounds before we call miRegionOp because + * it might be one of the source regions and miRegionOp depends + * on the fBounds of those regions being the unaltered. Besides, this + * way there's no checking against rectangles that will be nuked + * due to coalescing, so we have to examine fewer rectangles. + */ + miSetExtents (regD); + return 1; +} + +int +BRegion::Support::XXorRegion(const BRegion* sra, const BRegion* srb, + BRegion* dr) +{ + BRegion* tra; + BRegion* trb; + + if ((! (tra = CreateRegion())) || (! (trb = CreateRegion()))) + return 0; + (void) XSubtractRegion(sra,srb,tra); + (void) XSubtractRegion(srb,sra,trb); + (void) XUnionRegion(tra,trb,dr); + DestroyRegion(tra); + DestroyRegion(trb); + return 0; +} + + +int +BRegion::Support::XPointInRegion( + const BRegion* pRegion, + int x, int y) +{ + // TODO: binary search by "y"! + int i; + + if (pRegion->fCount == 0) + return false; + if (!INBOX(pRegion->fBounds, x, y)) + return false; + for (i=0; ifCount; i++) + { + if (INBOX (pRegion->fData[i], x, y)) + return true; + } + return false; +} + +int +BRegion::Support::XRectInRegion( + register const BRegion* region, + const clipping_rect& rect) +{ + register clipping_rect* pbox; + register clipping_rect* pboxEnd; + register const clipping_rect* prect = ▭ + int partIn, partOut; + + int rx = prect->left; + int ry = prect->top; + + /* this is (just) a useful optimization */ + if ((region->fCount == 0) || !EXTENTCHECK(®ion->fBounds, prect)) + return(RectangleOut); + + partOut = false; + partIn = false; + + /* can stop when both partOut and partIn are true, or we reach prect->bottom */ + for (pbox = region->fData, pboxEnd = pbox + region->fCount; + pbox < pboxEnd; + pbox++) + { + + if (pbox->bottom <= ry) + continue; /* getting up to speed or skipping remainder of band */ + + if (pbox->top > ry) + { + partOut = true; /* missed part of rectangle above */ + if (partIn || (pbox->top >= prect->bottom)) + break; + ry = pbox->top; /* x guaranteed to be == prect->left */ + } + + if (pbox->right <= rx) + continue; /* not far enough over yet */ + + if (pbox->left > rx) + { + partOut = true; /* missed part of rectangle to left */ + if (partIn) + break; + } + + if (pbox->left < prect->right) + { + partIn = true; /* definitely overlap */ + if (partOut) + break; + } + + if (pbox->right >= prect->right) + { + ry = pbox->bottom; /* finished with this band */ + if (ry >= prect->bottom) + break; + rx = prect->left; /* reset x out to left again */ + } else + { + /* + * Because boxes in a band are maximal width, if the first box + * to overlap the rectangle doesn't completely cover it in that + * band, the rectangle must be partially out, since some of it + * will be uncovered in that band. partIn will have been set true + * by now... + */ + break; + } + + } + + return(partIn ? ((ry < prect->bottom) ? RectanglePart : RectangleIn) : + RectangleOut); +} diff --git a/src/build/libbe/storage/Statable.cpp b/src/build/libbe/storage/Statable.cpp index 8f5357938c..4b648857fe 100644 --- a/src/build/libbe/storage/Statable.cpp +++ b/src/build/libbe/storage/Statable.cpp @@ -24,6 +24,11 @@ - \c B_NOT_ALLOWED: Read only node or volume. */ + +BStatable::~BStatable() +{ +} + /*! \brief Returns if the current node is a file. \return \c true, if the BNode is properly initialized and is a file, \c false otherwise.