Applied patch by Vasilis Kaoutsis:
* replaced new[] with malloc()/realloc() where appropriate - since we're messing with the bits anyway, this makes the code slightly faster. * however, we might want to throw some std::bad_alloc exceptions to deal correctly with low memory situations. * cleanup. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20065 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,55 +1,25 @@
|
|||||||
//------------------------------------------------------------------------------
|
/*
|
||||||
// Copyright (c) 2001-2002, OpenBeOS
|
* Copyright 2001-2007, Haiku, Inc. All Rights Reserved.
|
||||||
//
|
* Distributed under the terms of the MIT License.
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
*
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
* Authors:
|
||||||
// to deal in the Software without restriction, including without limitation
|
* Marc Flerackers, [email protected]
|
||||||
// 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: Polygon.h
|
|
||||||
// Author: Marc Flerackers ([email protected])
|
|
||||||
// Description: BPolygon represents a n-sided area.
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#ifndef _POLYGON_H
|
#ifndef _POLYGON_H
|
||||||
#define _POLYGON_H
|
#define _POLYGON_H
|
||||||
|
|
||||||
// Standard Includes -----------------------------------------------------------
|
|
||||||
|
|
||||||
// System Includes -------------------------------------------------------------
|
|
||||||
#include <BeBuild.h>
|
#include <BeBuild.h>
|
||||||
#include <InterfaceDefs.h>
|
#include <InterfaceDefs.h>
|
||||||
#include <Rect.h>
|
#include <Rect.h>
|
||||||
|
|
||||||
// Project Includes ------------------------------------------------------------
|
|
||||||
|
|
||||||
// Local Includes --------------------------------------------------------------
|
|
||||||
|
|
||||||
// Local Defines ---------------------------------------------------------------
|
|
||||||
|
|
||||||
// Globals ---------------------------------------------------------------------
|
|
||||||
|
|
||||||
// BPolygon class --------------------------------------------------------------
|
|
||||||
class BPolygon {
|
class BPolygon {
|
||||||
|
public:
|
||||||
public:
|
|
||||||
BPolygon(const BPoint *ptArray, int32 numPoints);
|
BPolygon(const BPoint *ptArray, int32 numPoints);
|
||||||
|
BPolygon(const BPolygon *polygon);
|
||||||
BPolygon();
|
BPolygon();
|
||||||
BPolygon(const BPolygon *poly);
|
virtual ~BPolygon();
|
||||||
virtual ~BPolygon();
|
|
||||||
|
|
||||||
BPolygon &operator=(const BPolygon &from);
|
BPolygon &operator=(const BPolygon &from);
|
||||||
|
|
||||||
@@ -59,26 +29,17 @@ virtual ~BPolygon();
|
|||||||
void MapTo(BRect srcRect, BRect dstRect);
|
void MapTo(BRect srcRect, BRect dstRect);
|
||||||
void PrintToStream() const;
|
void PrintToStream() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend class BView;
|
||||||
|
|
||||||
friend class BView;
|
void _ComputeBounds();
|
||||||
|
void _MapPoint(BPoint *point, BRect srcRect, BRect dstRect);
|
||||||
void compute_bounds();
|
void _MapRectangle(BRect *rect, BRect srcRect, BRect dstRect);
|
||||||
void map_pt(BPoint *point, BRect srcRect, BRect dstRect);
|
|
||||||
void map_rect(BRect *rect, BRect srcRect, BRect dstRect);
|
|
||||||
|
|
||||||
|
private:
|
||||||
BRect fBounds;
|
BRect fBounds;
|
||||||
int32 fCount;
|
int32 fCount;
|
||||||
BPoint *fPts;
|
BPoint *fPoints;
|
||||||
};
|
};
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#endif // _POLYGON_H_
|
#endif // _POLYGON_H_
|
||||||
|
|
||||||
/*
|
|
||||||
* $Log $
|
|
||||||
*
|
|
||||||
* $Id $
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|||||||
+86
-108
@@ -1,49 +1,26 @@
|
|||||||
//------------------------------------------------------------------------------
|
/*
|
||||||
// Copyright (c) 2001-2002, OpenBeOS
|
* Copyright 2001-2007, Haiku, Inc. All Rights Reserved.
|
||||||
//
|
* Distributed under the terms of the MIT License.
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
*
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
* Authors:
|
||||||
// to deal in the Software without restriction, including without limitation
|
* Marc Flerackers, [email protected]
|
||||||
// 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: Polygon.h
|
|
||||||
// Author: Marc Flerackers ([email protected])
|
|
||||||
// Description: BPolygon represents a n-sided area.
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// Standard Includes -----------------------------------------------------------
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
// System Includes -------------------------------------------------------------
|
|
||||||
#include <Polygon.h>
|
#include <Polygon.h>
|
||||||
|
|
||||||
// Project Includes ------------------------------------------------------------
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
// Local Includes --------------------------------------------------------------
|
|
||||||
|
|
||||||
// Local Defines ---------------------------------------------------------------
|
BPolygon::BPolygon(const BPoint *ptArray, int32 numPoints)
|
||||||
|
:
|
||||||
// Globals ---------------------------------------------------------------------
|
fBounds(0.0, 0.0, 0.0, 0.0),
|
||||||
|
fCount(numPoints),
|
||||||
//------------------------------------------------------------------------------
|
fPoints(NULL)
|
||||||
BPolygon::BPolygon(const BPoint *ptArray, int32 numPoints) :
|
|
||||||
fBounds(0.0, 0.0, 0.0, 0.0), fCount(numPoints), fPts(NULL)
|
|
||||||
{
|
{
|
||||||
if (fCount > 0) {
|
if (fCount > 0) {
|
||||||
fPts = new BPoint[numPoints];
|
fPoints = (BPoint*)malloc(numPoints * sizeof(BPoint));
|
||||||
|
|
||||||
// Note the use of memcpy here. The assumption is that an array of BPoints can
|
// Note the use of memcpy here. The assumption is that an array of BPoints can
|
||||||
// be copied bit by bit and not use a copy constructor or an assignment
|
// be copied bit by bit and not use a copy constructor or an assignment
|
||||||
@@ -52,135 +29,136 @@ BPolygon::BPolygon(const BPoint *ptArray, int32 numPoints) :
|
|||||||
// to do lazy copying through reference counting. By copying the bits, we will
|
// to do lazy copying through reference counting. By copying the bits, we will
|
||||||
// copy reference counting state which will not be relevant at the destination.
|
// copy reference counting state which will not be relevant at the destination.
|
||||||
// Luckily, BPoint is a very simple class which isn't likely to change much.
|
// Luckily, BPoint is a very simple class which isn't likely to change much.
|
||||||
// However, it is a risk of this implementation.
|
|
||||||
//
|
|
||||||
// If necessary, this code can be changed to iterate over the input array of
|
|
||||||
// BPoints and use the assignment operator to copy from the source to the
|
|
||||||
// destination array, one element at a time.
|
|
||||||
//
|
//
|
||||||
// Similar use of memcpy appears later in this implementation also.
|
// Similar use of memcpy appears later in this implementation also.
|
||||||
//
|
|
||||||
memcpy(fPts, ptArray, numPoints * sizeof(BPoint));
|
memcpy(fPoints, ptArray, numPoints * sizeof(BPoint));
|
||||||
compute_bounds();
|
_ComputeBounds();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
BPolygon::BPolygon(const BPolygon *poly)
|
BPolygon::BPolygon(const BPolygon *poly)
|
||||||
{
|
{
|
||||||
*this = *poly;
|
*this = *poly;
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
BPolygon::BPolygon ()
|
|
||||||
: fBounds(0.0, 0.0, 0.0, 0.0),
|
BPolygon::BPolygon()
|
||||||
|
:
|
||||||
|
fBounds(0.0, 0.0, 0.0, 0.0),
|
||||||
fCount(0),
|
fCount(0),
|
||||||
fPts(NULL)
|
fPoints(NULL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
BPolygon::~BPolygon ()
|
|
||||||
|
BPolygon::~BPolygon()
|
||||||
{
|
{
|
||||||
if (fPts)
|
free(fPoints);
|
||||||
delete[] fPts;
|
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
BPolygon &BPolygon::operator=(const BPolygon &from)
|
|
||||||
|
BPolygon &
|
||||||
|
BPolygon::operator=(const BPolygon &from)
|
||||||
{
|
{
|
||||||
// Make sure we aren't trying to perform a "self assignment".
|
// Make sure we aren't trying to perform a "self assignment".
|
||||||
if (this != &from) {
|
if (this != &from) {
|
||||||
fBounds = from.fBounds;
|
fBounds = from.fBounds;
|
||||||
fCount = from.fCount;
|
fCount = from.fCount;
|
||||||
if (fCount > 0) {
|
if (fCount > 0) {
|
||||||
fPts = new BPoint[fCount];
|
fPoints = (BPoint*)malloc(fCount * sizeof(BPoint));
|
||||||
memcpy(fPts, from.fPts, fCount * sizeof(BPoint));
|
memcpy(fPoints, from.fPoints, fCount * sizeof(BPoint));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
BRect BPolygon::Frame() const
|
|
||||||
|
BRect
|
||||||
|
BPolygon::Frame() const
|
||||||
{
|
{
|
||||||
return fBounds;
|
return fBounds;
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
void BPolygon::AddPoints(const BPoint *ptArray, int32 numPoints)
|
|
||||||
|
void
|
||||||
|
BPolygon::AddPoints(const BPoint *ptArray, int32 numPoints)
|
||||||
{
|
{
|
||||||
if (numPoints > 0) {
|
if (numPoints > 0) {
|
||||||
BPoint *newPts = new BPoint[fCount + numPoints];
|
fPoints = (BPoint*)realloc(fPoints, (fCount + numPoints) * sizeof(BPoint));
|
||||||
if (fPts) {
|
memcpy(fPoints + fCount + numPoints, ptArray, numPoints * sizeof(BPoint));
|
||||||
memcpy(newPts, fPts, fCount * sizeof(BPoint));
|
|
||||||
delete fPts;
|
|
||||||
}
|
|
||||||
memcpy(newPts + fCount, ptArray, numPoints * sizeof(BPoint));
|
|
||||||
fPts = newPts;
|
|
||||||
fCount += numPoints;
|
fCount += numPoints;
|
||||||
compute_bounds();
|
_ComputeBounds();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
int32 BPolygon::CountPoints() const
|
|
||||||
|
int32
|
||||||
|
BPolygon::CountPoints() const
|
||||||
{
|
{
|
||||||
return fCount;
|
return fCount;
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
void BPolygon::MapTo(BRect srcRect, BRect dstRect)
|
|
||||||
|
void
|
||||||
|
BPolygon::MapTo(BRect srcRect, BRect dstRect)
|
||||||
{
|
{
|
||||||
for (int32 i = 0; i < fCount; i++)
|
for (int32 i = 0; i < fCount; i++)
|
||||||
map_pt(fPts + i, srcRect, dstRect);
|
_MapPoint(fPoints + i, srcRect, dstRect);
|
||||||
map_rect(&fBounds, srcRect, dstRect);
|
_MapRectangle(&fBounds, srcRect, dstRect);
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
void BPolygon::PrintToStream () const
|
|
||||||
|
void
|
||||||
|
BPolygon::PrintToStream () const
|
||||||
{
|
{
|
||||||
for (int32 i = 0; i < fCount; i++)
|
for (int32 i = 0; i < fCount; i++)
|
||||||
fPts[i].PrintToStream();
|
fPoints[i].PrintToStream();
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
void BPolygon::compute_bounds()
|
|
||||||
|
void
|
||||||
|
BPolygon::_ComputeBounds()
|
||||||
{
|
{
|
||||||
if (fCount == 0) {
|
if (fCount == 0) {
|
||||||
fBounds = BRect(0.0, 0.0, 0.0, 0.0);
|
fBounds = BRect(0.0, 0.0, 0.0, 0.0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fBounds = BRect(fPts[0], fPts[0]);
|
fBounds = BRect(fPoints[0], fPoints[0]);
|
||||||
|
|
||||||
for (int32 i = 1; i < fCount; i++)
|
for (int32 i = 1; i < fCount; i++) {
|
||||||
{
|
if (fPoints[i].x < fBounds.left)
|
||||||
if (fPts[i].x < fBounds.left)
|
fBounds.left = fPoints[i].x;
|
||||||
fBounds.left = fPts[i].x;
|
if (fPoints[i].y < fBounds.top)
|
||||||
if (fPts[i].y < fBounds.top)
|
fBounds.top = fPoints[i].y;
|
||||||
fBounds.top = fPts[i].y;
|
if (fPoints[i].x > fBounds.right)
|
||||||
if (fPts[i].x > fBounds.right)
|
fBounds.right = fPoints[i].x;
|
||||||
fBounds.right = fPts[i].x;
|
if (fPoints[i].y > fBounds.bottom)
|
||||||
if (fPts[i].y > fBounds.bottom)
|
fBounds.bottom = fPoints[i].y;
|
||||||
fBounds.bottom = fPts[i].y;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
void BPolygon::map_pt(BPoint *point, BRect srcRect, BRect dstRect)
|
|
||||||
|
void
|
||||||
|
BPolygon::_MapPoint(BPoint *point, BRect srcRect, BRect dstRect)
|
||||||
{
|
{
|
||||||
point->x = (point->x - srcRect.left) * dstRect.Width() / srcRect.Width()
|
point->x = (point->x - srcRect.left) * dstRect.Width() / srcRect.Width()
|
||||||
+ dstRect.left;
|
+ dstRect.left;
|
||||||
point->y = (point->y - srcRect.top) * dstRect.Height() / srcRect.Height()
|
point->y = (point->y - srcRect.top) * dstRect.Height() / srcRect.Height()
|
||||||
+ dstRect.top;
|
+ dstRect.top;
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
void BPolygon::map_rect(BRect *rect, BRect srcRect, BRect dstRect)
|
|
||||||
|
void
|
||||||
|
BPolygon::_MapRectangle(BRect *rect, BRect srcRect, BRect dstRect)
|
||||||
{
|
{
|
||||||
BPoint leftTop = rect->LeftTop();
|
BPoint leftTop = rect->LeftTop();
|
||||||
BPoint bottomRight = rect->RightBottom();
|
BPoint bottomRight = rect->RightBottom();
|
||||||
|
|
||||||
map_pt(&leftTop, srcRect, dstRect);
|
_MapPoint(&leftTop, srcRect, dstRect);
|
||||||
map_pt(&bottomRight, srcRect, dstRect);
|
_MapPoint(&bottomRight, srcRect, dstRect);
|
||||||
|
|
||||||
*rect = BRect(leftTop, bottomRight);
|
*rect = BRect(leftTop, bottomRight);
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/*
|
|
||||||
* $Log $
|
|
||||||
*
|
|
||||||
* $Id $
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|||||||
@@ -2583,7 +2583,7 @@ BView::StrokePolygon(const BPolygon *polygon, bool closed, pattern p)
|
|||||||
if (!polygon)
|
if (!polygon)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
StrokePolygon(polygon->fPts, polygon->fCount, polygon->Frame(), closed, p);
|
StrokePolygon(polygon->fPoints, polygon->fCount, polygon->Frame(), closed, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -2592,7 +2592,7 @@ BView::StrokePolygon(const BPoint *ptArray, int32 numPoints, bool closed, patter
|
|||||||
{
|
{
|
||||||
BPolygon polygon(ptArray, numPoints);
|
BPolygon polygon(ptArray, numPoints);
|
||||||
|
|
||||||
StrokePolygon(polygon.fPts, polygon.fCount, polygon.Frame(), closed, p);
|
StrokePolygon(polygon.fPoints, polygon.fCount, polygon.Frame(), closed, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -2617,7 +2617,7 @@ BView::StrokePolygon(const BPoint *ptArray, int32 numPoints, BRect bounds,
|
|||||||
fOwner->fLink->Attach<BRect>(polygon.Frame());
|
fOwner->fLink->Attach<BRect>(polygon.Frame());
|
||||||
fOwner->fLink->Attach<bool>(closed);
|
fOwner->fLink->Attach<bool>(closed);
|
||||||
fOwner->fLink->Attach<int32>(polygon.fCount);
|
fOwner->fLink->Attach<int32>(polygon.fCount);
|
||||||
fOwner->fLink->Attach(polygon.fPts, polygon.fCount * sizeof(BPoint));
|
fOwner->fLink->Attach(polygon.fPoints, polygon.fCount * sizeof(BPoint));
|
||||||
|
|
||||||
_FlushIfNotInTransaction();
|
_FlushIfNotInTransaction();
|
||||||
} else {
|
} else {
|
||||||
@@ -2642,7 +2642,7 @@ BView::FillPolygon(const BPolygon *polygon, ::pattern pattern)
|
|||||||
polygon->fCount * sizeof(BPoint) + sizeof(BRect) + sizeof(int32)) == B_OK) {
|
polygon->fCount * sizeof(BPoint) + sizeof(BRect) + sizeof(int32)) == B_OK) {
|
||||||
fOwner->fLink->Attach<BRect>(polygon->Frame());
|
fOwner->fLink->Attach<BRect>(polygon->Frame());
|
||||||
fOwner->fLink->Attach<int32>(polygon->fCount);
|
fOwner->fLink->Attach<int32>(polygon->fCount);
|
||||||
fOwner->fLink->Attach(polygon->fPts, polygon->fCount * sizeof(BPoint));
|
fOwner->fLink->Attach(polygon->fPoints, polygon->fCount * sizeof(BPoint));
|
||||||
|
|
||||||
_FlushIfNotInTransaction();
|
_FlushIfNotInTransaction();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user