#include "TObjArray.h"
#include "TGeoPolygon.h"
#include "TMath.h"
ClassImp(TGeoPolygon)
TGeoPolygon::TGeoPolygon()
{
fNvert = 0;
fNconvex = 0;
fInd = 0;
fIndc = 0;
fX = 0;
fY = 0;
fDaughters = 0;
SetConvex(kFALSE);
TObject::SetBit(kGeoFinishPolygon, kFALSE);
}
TGeoPolygon::TGeoPolygon(Int_t nvert)
{
if (nvert<3) {
Fatal("Ctor", "Invalid number of vertices %i", nvert);
return;
}
fNvert = nvert;
fNconvex = 0;
fInd = new Int_t[nvert];
fIndc = 0;
fX = 0;
fY = 0;
fDaughters = 0;
SetConvex(kFALSE);
TObject::SetBit(kGeoFinishPolygon, kFALSE);
SetNextIndex();
}
TGeoPolygon::~TGeoPolygon()
{
if (fInd) delete [] fInd;
if (fIndc) delete [] fIndc;
if (fDaughters) {
fDaughters->Delete();
delete fDaughters;
}
}
Double_t TGeoPolygon::Area() const
{
Int_t ic,i,j;
Double_t area = 0;
for (ic=0; ic<fNconvex; ic++) {
i = fIndc[ic];
j = fIndc[(ic+1)%fNconvex];
area += 0.5*TMath::Abs(fX[i]*fY[j]-fX[j]*fY[i]);
}
if (!fDaughters) return area;
Int_t nd = fDaughters->GetEntriesFast();
TGeoPolygon *poly;
for (i=0; i<nd; i++) {
poly = (TGeoPolygon*)fDaughters->UncheckedAt(i);
area -= poly->Area();
}
return area;
}
Bool_t TGeoPolygon::Contains(Double_t *point) const
{
Int_t i;
TGeoPolygon *poly;
for (i=0; i<fNconvex; i++)
if (!IsRightSided(point, fIndc[i], fIndc[(i+1)%fNconvex])) return kFALSE;
if (!fDaughters) return kTRUE;
Int_t nd = fDaughters->GetEntriesFast();
for (i=0; i<nd; i++) {
poly = (TGeoPolygon*)fDaughters->UncheckedAt(i);
if (poly->Contains(point)) return kFALSE;
}
return kTRUE;
}
void TGeoPolygon::ConvexCheck()
{
if (fNvert==3) {
SetConvex();
return;
}
Int_t j,k;
Double_t point[2];
for (Int_t i=0; i<fNvert; i++) {
j = (i+1)%fNvert;
k = (i+2)%fNvert;
point[0] = fX[fInd[k]];
point[1] = fY[fInd[k]];
if (!IsRightSided(point, fInd[i], fInd[j])) return;
}
SetConvex();
}
void TGeoPolygon::FinishPolygon()
{
TObject::SetBit(kGeoFinishPolygon);
ConvexCheck();
OutscribedConvex();
if (IsConvex()) {
memcpy(fIndc, fInd, fNvert*sizeof(Int_t));
return;
}
if (IsConvex()) return;
if (!fDaughters) fDaughters = new TObjArray();
TGeoPolygon *poly = 0;
Int_t indconv = 0;
Int_t indnext, indback;
Int_t nskip;
while (indconv < fNconvex) {
indnext = (indconv+1)%fNconvex;
nskip = fIndc[indnext]-fIndc[indconv];
if (nskip<0) nskip+=fNvert;
if (nskip==1) {
indconv++;
continue;
}
poly = new TGeoPolygon(nskip+1);
poly->SetXY(fX,fY);
poly->SetNextIndex(fInd[fIndc[indconv]]);
poly->SetNextIndex(fInd[fIndc[indnext]]);
indback = fIndc[indnext]-1;
if (indback < 0) indback+=fNvert;
while (indback != fIndc[indconv]) {
poly->SetNextIndex(fInd[indback]);
indback--;
if (indback < 0) indback+=fNvert;
}
poly->FinishPolygon();
fDaughters->Add(poly);
indconv++;
}
for (indconv=0; indconv<fNconvex; indconv++) fIndc[indconv] = fInd[fIndc[indconv]];
}
Bool_t TGeoPolygon::IsRightSided(Double_t *point, Int_t ind1, Int_t ind2) const
{
Double_t dot = (point[0]-fX[ind1])*(fY[ind2]-fY[ind1]) -
(point[1]-fY[ind1])*(fX[ind2]-fX[ind1]);
if (!IsClockwise()) dot = -dot;
if (dot<0) return kFALSE;
return kTRUE;
}
Bool_t TGeoPolygon::IsSegConvex(Int_t i1, Int_t i2) const
{
if (i2<0) i2=(i1+1)%fNvert;
Double_t point[2];
for (Int_t i=0; i<fNvert; i++) {
if (i==i1 || i==i2) continue;
point[0] = fX[fInd[i]];
point[1] = fY[fInd[i]];
if (!IsRightSided(point, fInd[i1], fInd[i2])) return kFALSE;
}
return kTRUE;
}
void TGeoPolygon::OutscribedConvex()
{
fNconvex = 0;
Int_t iseg = 0;
Int_t ivnew;
Bool_t conv;
Int_t *indconv = new Int_t[fNvert];
memset(indconv, 0, fNvert*sizeof(Int_t));
while (iseg<fNvert) {
if (!IsSegConvex(iseg)) {
if (iseg+2 > fNvert) break;
ivnew = (iseg+2)%fNvert;
conv = kFALSE;
while (ivnew) {
if (IsSegConvex(iseg, ivnew)) {
conv = kTRUE;
break;
}
ivnew = (ivnew+1)%fNvert;
}
if (!conv) {
iseg++;
continue;
}
} else {
ivnew = (iseg+1)%fNvert;
}
if (!fNconvex) indconv[fNconvex++] = iseg;
else if (indconv[fNconvex-1] != iseg) indconv[fNconvex++] = iseg;
if (iseg<fNvert-1) indconv[fNconvex++] = ivnew;
if (ivnew<iseg) break;
iseg = ivnew;
}
if (!fNconvex) {
Fatal("OutscribedConvex","cannot build outscribed convex");
return;
}
fIndc = new Int_t[fNconvex];
memcpy(fIndc, indconv, fNconvex*sizeof(Int_t));
delete [] indconv;
}
Double_t TGeoPolygon::Safety(Double_t *point, Int_t &isegment) const
{
Int_t i1, i2;
Double_t p1[2], p2[3];
Double_t lsq, ssq, dx, dy, dpx, dpy, u;
Double_t safe=1E30;
Int_t isegmin=0;
for (i1=0; i1<fNvert; i1++) {
if (safe==0.) {
isegment = isegmin;
return 0.;
}
i2 = (i1+1)%fNvert;
p1[0] = fX[i1];
p1[1] = fY[i1];
p2[0] = fX[i2];
p2[1] = fY[i2];
dx = p2[0] - p1[0];
dy = p2[1] - p1[1];
dpx = point[0] - p1[0];
dpy = point[1] - p1[1];
lsq = dx*dx + dy*dy;
if (lsq==0) {
ssq = dpx*dpx + dpy*dpy;
if (ssq < safe) {
safe = ssq;
isegmin = i1;
}
continue;
}
u = (dpx*dx + dpy*dy)/lsq;
if (u>1) {
dpx = point[0]-p2[0];
dpy = point[1]-p2[1];
} else {
if (u>=0) {
dpx -= u*dx;
dpy -= u*dy;
}
}
ssq = dpx*dpx + dpy*dpy;
if (ssq < safe) {
safe = ssq;
isegmin = i1;
}
}
isegment = isegmin;
safe = TMath::Sqrt(safe);
return safe;
}
void TGeoPolygon::SetNextIndex(Int_t index)
{
Int_t i;
if (index <0) {
for (i=0; i<fNvert; i++) fInd[i] = i;
return;
}
if (fNconvex >= fNvert) {
Error("SetNextIndex", "all indices already set");
return;
}
fInd[fNconvex++] = index;
if (fNconvex == fNvert) {
if (!fX || !fY) return;
Double_t area = 0.0;
for (i=0; i<fNvert; i++) area += fX[fInd[i]]*fY[fInd[(i+1)%fNvert]]-fX[fInd[(i+1)%fNvert]]*fY[fInd[i]];
if (area<0) TObject::SetBit(kGeoACW, kFALSE);
else TObject::SetBit(kGeoACW, kTRUE);
}
}
void TGeoPolygon::SetXY(Double_t *x, Double_t *y)
{
Int_t i;
fX = x;
fY = y;
Double_t area = 0.0;
for (i=0; i<fNvert; i++) area += fX[fInd[i]]*fY[fInd[(i+1)%fNvert]]-fX[fInd[(i+1)%fNvert]]*fY[fInd[i]];
if (area<0) TObject::SetBit(kGeoACW, kFALSE);
else TObject::SetBit(kGeoACW, kTRUE);
if (!fDaughters) return;
TGeoPolygon *poly;
Int_t nd = fDaughters->GetEntriesFast();
for (i=0; i<nd; i++) {
poly = (TGeoPolygon*)fDaughters->At(i);
if (poly) poly->SetXY(x,y);
}
}
Last change: Wed Jun 25 08:45:22 2008
Last generated: 2008-06-25 08:45
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.