* Indentation cleanup

* Code cleanup
* Refactoring (the unit tests still pass)
* Disabled experimental API (BAffineTransform, which is not yet publically
  available anyway)

+alphabranch


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32723 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2009-08-26 18:40:04 +00:00
parent d5a6847722
commit 33d568ae09
2 changed files with 133 additions and 127 deletions
+36 -30
View File
@@ -1,9 +1,6 @@
/* /*
* Copyright 2001-2007, Haiku, Inc. All Rights Reserved. * Copyright 2001-2009, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT license.
*
* Authors:
* Marc Flerackers, [email protected]
*/ */
#ifndef _POLYGON_H #ifndef _POLYGON_H
#define _POLYGON_H #define _POLYGON_H
@@ -13,39 +10,48 @@
#include <InterfaceDefs.h> #include <InterfaceDefs.h>
#include <Rect.h> #include <Rect.h>
namespace BPrivate { class BAffineTransform; } //namespace BPrivate { class BAffineTransform; }
using namespace BPrivate; //using namespace BPrivate;
class BPolygon { class BPolygon {
public: public:
BPolygon(const BPoint *ptArray, int32 numPoints); BPolygon(const BPoint* points, int32 count);
BPolygon(const BPolygon *polygon); BPolygon(const BPolygon& other);
BPolygon(); BPolygon(const BPolygon* other);
virtual ~BPolygon(); BPolygon();
virtual ~BPolygon();
BPolygon &operator=(const BPolygon &from); BPolygon& operator=(const BPolygon& other);
BRect Frame() const; BRect Frame() const;
void AddPoints(const BPoint *ptArray, int32 numPoints); void AddPoints(const BPoint* points, int32 count);
int32 CountPoints() const; int32 CountPoints() const;
void MapTo(BRect srcRect, BRect dstRect); void MapTo(BRect srcRect, BRect dstRect);
void PrintToStream() const; void PrintToStream() const;
void Transform(const BAffineTransform& transform); // void TransformBy(const BAffineTransform& transform);
BPolygon& TransformBySelf(const BAffineTransform& transform); // BPolygon& TransformBySelf(
BPolygon* TransformByCopy(const BAffineTransform& transform) const; // const BAffineTransform& transform);
// BPolygon TransformByCopy(
// const BAffineTransform& transform) const;
private: private:
friend class BView; friend class BView;
void _ComputeBounds(); bool _AddPoints(const BPoint* points, int32 count,
void _MapPoint(BPoint *point, BRect srcRect, BRect dstRect); bool computeBounds);
void _MapRectangle(BRect *rect, BRect srcRect, BRect dstRect); void _ComputeBounds();
void _MapPoint(BPoint* point, const BRect& srcRect,
const BRect& dstRect);
void _MapRectangle(BRect* rect,
const BRect& srcRect,
const BRect& dstRect);
private: private:
BRect fBounds; BRect fBounds;
uint32 fCount; uint32 fCount;
BPoint *fPoints; BPoint* fPoints;
}; };
#endif // _POLYGON_H_ #endif // _POLYGON_H_
+97 -97
View File
@@ -1,70 +1,59 @@
/* /*
* Copyright 2001-2007, Haiku, Inc. All Rights Reserved. * Copyright 2001-2009, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT license.
* *
* Authors: * Authors:
* Marc Flerackers, [email protected] * Marc Flerackers, [email protected]
* Marcus Overhagen * Marcus Overhagen
* Stephan Aßmus <[email protected]>
*/ */
#include <AffineTransform.h>
#include <Polygon.h> #include <Polygon.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <AffineTransform.h>
// Limit to avoid integer overflow when calculating the size to allocate // Limit to avoid integer overflow when calculating the size to allocate
#define MAX_POINT_COUNT 10000000 #define MAX_POINT_COUNT 10000000
BPolygon::BPolygon(const BPoint *ptArray, int32 numPoints) BPolygon::BPolygon(const BPoint* points, int32 count)
: :
fBounds(0.0, 0.0, 0.0, 0.0), fBounds(0.0f, 0.0f, -1.0f, -1.0f),
fCount(numPoints),
fPoints(NULL)
{
if (fCount) {
if (fCount > MAX_POINT_COUNT)
debugger("BPolygon::BPolygon too many points");
// 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
// operator. This breaks the containment of BPoint but will result in better
// performance. An example where the memcpy will fail would be if BPoint begins
// 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.
// Luckily, BPoint is a very simple class which isn't likely to change much.
//
// Similar use of memcpy appears later in this implementation also.
size_t size = fCount * sizeof(BPoint);
fPoints = (BPoint *)malloc(size);
if (!fPoints) {
fprintf(stderr, "BPolygon::BPolygon out of memory\n");
fCount = 0;
return;
}
memcpy(fPoints, ptArray, size);
_ComputeBounds();
}
}
BPolygon::BPolygon(const BPolygon *poly)
:
fBounds(0.0, 0.0, 0.0, 0.0),
fCount(0), fCount(0),
fPoints(NULL) fPoints(NULL)
{ {
*this = *poly; _AddPoints(points, count, true);
}
BPolygon::BPolygon(const BPolygon& other)
:
fBounds(0.0f, 0.0f, -1.0f, -1.0f),
fCount(0),
fPoints(NULL)
{
*this = other;
}
BPolygon::BPolygon(const BPolygon* other)
:
fBounds(0.0f, 0.0f, -1.0f, -1.0f),
fCount(0),
fPoints(NULL)
{
*this = *other;
} }
BPolygon::BPolygon() BPolygon::BPolygon()
: :
fBounds(0.0, 0.0, 0.0, 0.0), fBounds(0.0f, 0.0f, -1.0f, -1.0f),
fCount(0), fCount(0),
fPoints(NULL) fPoints(NULL)
{ {
@@ -77,28 +66,21 @@ BPolygon::~BPolygon()
} }
BPolygon & BPolygon&
BPolygon::operator=(const BPolygon &from) BPolygon::operator=(const BPolygon& other)
{ {
// 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 == &other)
free(fPoints); return *this;
fBounds = from.fBounds;
fCount = from.fCount; free(fPoints);
if (fCount) { fPoints = NULL;
if (fCount > MAX_POINT_COUNT) fCount = 0;
debugger("BPolygon::operator= too many points"); fBounds.Set(0.0f, 0.0f, -1.0f, -1.0f);
fPoints = (BPoint *)malloc(fCount * sizeof(BPoint));
if (!fPoints) { if (_AddPoints(other.fPoints, other.fCount, false))
fprintf(stderr, "BPolygon::operator= out of memory\n"); fBounds = other.fBounds;
fCount = 0;
} else {
memcpy(fPoints, from.fPoints, fCount * sizeof(BPoint));
}
} else {
fPoints = NULL;
}
}
return *this; return *this;
} }
@@ -111,24 +93,9 @@ BPolygon::Frame() const
void void
BPolygon::AddPoints(const BPoint *ptArray, int32 numPoints) BPolygon::AddPoints(const BPoint* points, int32 count)
{ {
if (numPoints < 0) _AddPoints(points, count, true);
debugger("BPolygon::AddPoints negative points");
if (numPoints > MAX_POINT_COUNT || (fCount + numPoints) > MAX_POINT_COUNT)
debugger("BPolygon::AddPoints too many points");
if (numPoints > 0) {
BPoint *points = (BPoint *)realloc(fPoints, (fCount + numPoints) * sizeof(BPoint));
if (!points) {
fprintf(stderr, "BPolygon::AddPoints out of memory\n");
} else {
fPoints = points;
memcpy(fPoints + fCount, ptArray, numPoints * sizeof(BPoint));
fCount += numPoints;
_ComputeBounds();
}
}
} }
@@ -156,28 +123,60 @@ BPolygon::PrintToStream () const
} }
void //void
BPolygon::Transform(const BAffineTransform& transform) //BPolygon::TransformBy(const BAffineTransform& transform)
//{
// transform.Apply(fPoints, (int32)fCount);
// _ComputeBounds();
//}
//
//
//BPolygon&
//BPolygon::TransformBySelf(const BAffineTransform& transform)
//{
// TransformBy(transform);
// return *this;
//}
//
//
//BPolygon
//BPolygon::TransformByCopy(const BAffineTransform& transform) const
//{
// BPolygon copy(this);
// copy.TransformBy(transform);
// return copy;
//}
// #pragma mark -
bool
BPolygon::_AddPoints(const BPoint* points, int32 count, bool computeBounds)
{ {
transform.Apply(fPoints, (int32)fCount); if (points == NULL || count <= 0)
_ComputeBounds(); return false;
} if (count > MAX_POINT_COUNT || (fCount + count) > MAX_POINT_COUNT) {
fprintf(stderr, "BPolygon::_AddPoints(%ld) - too many points\n",
count);
return false;
}
BPoint* newPoints = (BPoint*)realloc(fPoints, (fCount + count)
* sizeof(BPoint));
if (newPoints == NULL) {
fprintf(stderr, "BPolygon::_AddPoints(%ld) out of memory\n", count);
return false;
}
BPolygon& fPoints = newPoints;
BPolygon::TransformBySelf(const BAffineTransform& transform) memcpy(fPoints + fCount, points, count * sizeof(BPoint));
{ fCount += count;
Transform(transform);
return *this;
}
if (computeBounds)
_ComputeBounds();
BPolygon* return true;
BPolygon::TransformByCopy(const BAffineTransform& transform) const
{
BPolygon* copy = new BPolygon(this);
copy->Transform(transform);
return copy;
} }
@@ -185,7 +184,7 @@ void
BPolygon::_ComputeBounds() BPolygon::_ComputeBounds()
{ {
if (fCount == 0) { if (fCount == 0) {
fBounds = BRect(0.0, 0.0, 0.0, 0.0); fBounds = BRect(0.0, 0.0, -1.0f, -1.0f);
return; return;
} }
@@ -205,7 +204,7 @@ BPolygon::_ComputeBounds()
void void
BPolygon::_MapPoint(BPoint *point, BRect srcRect, BRect dstRect) BPolygon::_MapPoint(BPoint* point, const BRect& srcRect, const 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;
@@ -215,7 +214,8 @@ BPolygon::_MapPoint(BPoint *point, BRect srcRect, BRect dstRect)
void void
BPolygon::_MapRectangle(BRect *rect, BRect srcRect, BRect dstRect) BPolygon::_MapRectangle(BRect* rect, const BRect& srcRect,
const BRect& dstRect)
{ {
BPoint leftTop = rect->LeftTop(); BPoint leftTop = rect->LeftTop();
BPoint bottomRight = rect->RightBottom(); BPoint bottomRight = rect->RightBottom();