31 : closed_(true), udf_(
Geom::Point2D<T>::udf())
35 : poly_(plg), closed_(true)
36 , udf_(
Geom::Point2D<T>::udf())
40 bool isEmpty()
const {
return poly_.isEmpty(); }
42 int size()
const {
return poly_.size(); }
43 bool validIdx(
int idx)
const {
return poly_.validIdx(idx); }
51 bool inclborder,T eps)
const;
53 int isInside(
const ODPolygon& testpoly,T eps=0)
const;
77 void removeZeroLengths();
78 bool isUTurn(
int idx)
const;
79 bool isSelfIntersecting()
const;
82 void keepBendPoints(
float eps);
85 double area()
const {
return fabs(sgnArea2<double>()/2.0); }
92 double* fractionptr=0)
const;
94 double maxDistToBorderEstimate(
double maxrelerr=0.001)
const;
98 {
return poly_.size()>plg.
size(); }
139 template <
class ST> ST sgnArea2()
const;
144 double* fractionptr=0 );
155 template <
class T>
inline 158 if ( data() != poly.
data() )
return false;
163 template <
class T>
inline 166 for (
int idx=0; idx<size(); idx++ )
169 ts += forx ? vtx.
x : vtx.
y;
174 template <
class T>
inline 187 template <
class T>
inline 204 for (
int idx=0; idx<sz; idx++ )
209 template <
class T>
inline 212 if ( idx>=0 && idx<=size() )
213 poly_.insert( idx, vtx );
219 template <
class T>
inline 228 template <
class T>
inline 237 template <
class T>
inline 240 if ( poly_.validIdx(idx) )
241 poly_.removeSingle( idx );
247 template <
class T>
inline 249 {
return poly_.validIdx(idx) ? poly_[idx] : udf_; }
252 template <
class T>
inline 254 {
return getVertex( idx+1<size() ? idx+1 : 0 ); }
257 template <
class T>
inline 259 {
return getVertex( idx ? idx-1 : size()-1 ); }
262 template <
class T>
inline 265 if ( poly_.isEmpty() )
return Interval<T>( udf_.x, udf_.y );
271 for (
int idx=1; idx<size(); idx++ )
274 const T val = forx ? vtx.
x : vtx.
y;
276 else if ( val > ret.
stop ) ret.
stop = val;
286 template <
class T>
inline 288 bool inclborder, T eps )
const 290 const T abseps = eps<0 ? -eps : eps;
292 (xrg_.start>point.
x+abseps || xrg_.stop<point.
x-abseps)) ||
294 (yrg_.start>point.
y+abseps || yrg_.stop<point.
y-abseps)) )
299 bool nrcrossingsodd =
false;
300 for (
int idx=0; idx<size(); idx++ )
305 if ( isOnSegment(point, vtxcurr, vtxnext, eps) )
307 if ( isEdgeCrossing(arbitrarydir, point, vtxcurr, vtxnext) )
308 nrcrossingsodd = !nrcrossingsodd;
311 return nrcrossingsodd;
315 template <
class T>
inline 320 for (
int idx=0; idx<size(); idx++ )
325 if ( doSegmentsMeet(pt1, pt2, vtxcurr, vtxnext, eps) )
333 template <
class T>
inline 344 return isInside( window, eps );
348 template <
class T>
inline 354 for (
int idx=0; idx<size(); idx++ )
363 if ( isInside(testpoly.
poly_[0],
false, eps) )
366 return testpoly.
isInside(poly_[0],
false, eps) ? 1 : 0;
370 template <
class T>
inline 373 const int startidx = isClosed() ? size()-1 : size()-2;
374 for (
int idx=startidx; idx>=0; idx-- )
376 if ( poly_[idx]==nextVertex(idx) && size()>1 )
382 template <
class T>
inline 385 if ( !validIdx(idx) || ( !isClosed() && (idx==0 || idx==size()-1) ) )
391 return vec1.
x*vec2.
y-vec1.
y*vec2.
x==0 && vec1.
x*vec2.
x+vec1.
y*vec2.
y>0;
395 template <
class T>
inline 402 for (
int idx=0; idx<stopidx; idx++ )
410 for (
int idy=0; idy<stopidx; idy++ )
412 const int dif = abs( idx-idy );
413 if ( dif<=1 || dif>=plg.
size()-1 )
419 if ( vtxcurr==pt1 || vtxcurr==pt2 )
422 if ( isEdgeCrossing(vtxnext-vtxcurr, vtxcurr, pt1, pt2) &&
423 isEdgeCrossing(vtxcurr-vtxnext, vtxnext, pt1, pt2) )
431 template <
class T>
inline 438 if ( isOnSegment(p1, q1, q2, eps) || isOnSegment(p2, q1, q2, eps) ||
439 isOnSegment(q1, p1, p2, eps) || isOnSegment(q2, p1, p2, eps) )
442 if ( p1==p2 || q1==q2 || !isEdgeCrossing(p2-p1, p1, q1, q2) ||
443 !isEdgeCrossing(p1-p2, p2, q1, q2) )
450 template <
class T>
inline 456 return isOnHalfLine( pt, pt1-pt0, pt0, eps ) &&
457 isOnHalfLine( pt, pt0-pt1, pt1, eps );
461 template <
class T>
inline 467 if ( doCoincide(point, endvec, eps) )
469 if ( !isOnLine(point, dirvec, endvec, eps) )
472 return isRightOfLine( point, rot90dirvec, endvec );
476 template <
class T>
inline 482 const bool vtx1right = isRightOfLine( vtx1, raydir, raysrc );
483 const bool vtx2right = isRightOfLine( vtx2, raydir, raysrc );
485 if ( vtx1right && !vtx2right )
486 return !isRightOfLine( raysrc, vtx2-vtx1, vtx1 );
487 if ( !vtx1right && vtx2right )
488 return !isRightOfLine( raysrc, vtx1-vtx2, vtx2 );
493 template <
class T>
inline 499 const double signeddist = sgnDistToLine( point, dirvec, posvec );
500 return signeddist * signeddist <= eps * eps;
503 template <
class T>
inline 508 return sgnDistToLine( point, dirvec, posvec ) > 0;
512 template <
class T>
inline 517 return point1.
sqDistTo( point2 ) <= eps * eps;
521 template <
class T>
inline 526 const double nolinedist = 0;
531 const double substpointinlineeqn =
532 dirvec.
y * ( point.
x - posvec.
x )-dirvec.
x * ( point.
y - posvec.
y );
533 return substpointinlineeqn / dirveclen;
538 template <
class ST>
inline 544 for (
int idx=1; idx<size()-1; idx++ )
548 area2 += (ST) ( (pt1.
x-pt0.
x) * (pt2.
y-pt0.
y) -
549 (pt2.
x-pt0.
x) * (pt1.
y-pt0.
y) );
556 template <
class T>
inline 567 for (
int idx=1; idx<size(); idx++ )
570 if ( vtx.
x<pivot.
x || (vtx.
x==pivot.
x && vtx.
y<pivot.
y) )
575 for (
int idx=size()-1; idx>=0; idx-- )
577 if ( pivot == poly_[idx] )
579 poly_.removeSingle( idx );
583 for (
int idx=size()-2; idx>=0; idx-- )
586 for (
int idy=size()-1; idy>idx; idy-- )
589 const double dist = sgnDistToLine( vty, vtx-pivot, pivot );
595 poly_.insert( idy+1, vtx );
599 poly_.removeSingle( idx );
605 for (
int idx=size()-3; idx>=0; idx-- )
608 while ( idx<size()-2 )
612 if ( isRightOfLine(vtz, vty-vtx, vtx) )
615 poly_.removeSingle( idx+1 );
626 template <
class T>
inline 629 const int sz = poly_.size();
630 for (
int idx=0; idx<sz/2-1; idx++ )
633 poly_[idx] = poly_[sz-1-idx];
634 poly_[sz-1-idx] = temp;
642 template <
class T>
inline 646 double* fractionptr )
653 const double numerator = dif.
x*(refpt.
x-p1.x) + dif.
y*(refpt.
y-p1.y);
654 frac = numerator / (dif.
x*dif.
x + dif.
y*dif.
y);
666 (T)(p1.
y * (1-frac) + p2.
y * frac) );
667 return refpt.
distTo( pointonseg );
671 template <
class T>
inline 673 int* segmentidxptr,
double* fractionptr )
const 675 const int sz = size();
681 for (
int idx=(isClosed() ? sz-1 : sz-2); idx>=0; idx-- )
686 const double dist = distToSegment( pt1, pt2, refpt, &frac );
688 if ( mindist >= dist )
692 *segmentidxptr = idx;
702 template <
class T>
inline 705 if ( maxrelerr <= 0.0 )
711 const double upperbound =
mMIN( 0.5 * getRange(
true).width(),
712 0.5 * getRange(
false).width() );
717 for (
int idx=0; idx<size(); idx++ )
723 double maxdist = 0.0;
724 for (
int idx=0; idx<poly.
size(); idx++ )
732 double curdist = poly.
distTo( curpt );
733 double gamma = 0.1 * upperbound;
735 for (
int step=0; step<100; step++ )
737 if ( curdist > maxdist )
743 const double delta = 0.0001 * upperbound;
746 const double dist1 = poly.
distTo( pt1 );
747 const double dist2 = poly.
distTo( pt2 );
749 nextpt *= gamma/delta;
758 double nextdist = poly.
distTo( nextpt );
760 if ( nextdist <= curdist )
764 if ( curpt.
distTo(nextpt) <= 2*maxrelerr*curdist )
776 template <
class T>
inline 781 const int sz = size();
785 const int extra = closed_ ? 1 : 0;
788 for (
int idx=-extra; idx<sz+extra; idx++ )
791 coords +=
Coord( vtx.
x, vtx.
y );
798 int bpidx = bendpoints.
size()-extra-1;
802 for (
int vtxidx=sz-1; vtxidx>=0; vtxidx-- )
804 if ( bpidx>=0 && vtxidx==bendpoints[bpidx]-extra )
810 if ( closed_ && poly_.size()>1 && poly_.first() != poly_.last() )
811 poly_ += poly_.first();
const Geom::Point2D< T > & getVertex(int idx) const
Definition: polygon.h:248
bool isInside(const Geom::Point2D< T > &, bool inclborder, T eps) const
Definition: polygon.h:287
void remove(int idx)
Definition: polygon.h:238
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:287
void erase()
Definition: polygon.h:220
static bool isEdgeCrossing(const Geom::Point2D< T > &raydir, const Geom::Point2D< T > &raysrc, const Geom::Point2D< T > &vtx1, const Geom::Point2D< T > &vtx2)
Definition: polygon.h:477
void keepBendPoints(float eps)
Definition: polygon.h:777
bool isEmpty() const
Definition: polygon.h:40
bool clockwise() const
Definition: polygon.h:86
bool operator==(const ArrayNDInfo &a1, const ArrayNDInfo &a2)
Definition: arrayndinfo.h:53
const TypeSet< int > & bendPoints() const
Definition: bendpointfinder.h:33
static bool doCoincide(const Geom::Point2D< T > &point1, const Geom::Point2D< T > &point2, T eps=mDefEps)
Definition: polygon.h:513
bool windowOverlaps(const Interval< T > &xrange, const Interval< T > &yrange, T eps) const
Definition: polygon.h:334
Interval< T > getRange(bool of_x) const
Definition: polygon.h:263
#define mIsZero(x, eps)
Definition: commondefs.h:53
bool closed_
Definition: polygon.h:147
void convexHull()
Definition: polygon.h:557
void usePar(const IOPar &iop, ODPolygon< T > &poly, const char *inpkey)
Definition: polygon.h:188
void getData(bool of_x, TypeSet< T > &) const
Definition: polygon.h:164
static bool isOnLine(const Geom::Point2D< T > &point, const Geom::Point2D< T > &dirvec, const Geom::Point2D< T > &posvec, T eps)
Definition: polygon.h:494
Interval< T > yrg_
Definition: polygon.h:151
static bool isRightOfLine(const Geom::Point2D< T > &point, const Geom::Point2D< T > &dirvec, const Geom::Point2D< T > &posvec)
Definition: polygon.h:504
(Closed) sequence of connected 2-D coordinates.
Definition: polygon.h:27
const Geom::Point2D< T > & nextVertex(int idx) const
Definition: polygon.h:253
Used to find bendpoints in set of XY Coordinates.
Definition: bendpointfinder.h:82
TypeSet< Geom::Point2D< T > > poly_
Definition: polygon.h:146
void setUdf(Geom::Point2D< T > pt)
Definition: polygon.h:70
Geom::Point2D< T > udf_
Definition: polygon.h:148
double distTo(const Geom::Point2D< T > &refpt, int *segmentidxptr=0, double *fractionptr=0) const
Definition: polygon.h:672
T sqDistTo(const Point2D< T > &) const
Definition: geometry.h:379
A cartesian coordinate in 2D space.
Definition: coord.h:25
bool get(const char *, int &) const
ODPolygon(const TypeSet< Geom::Point2D< T > > &plg)
Definition: polygon.h:34
double distTo(const Point2D< T > &) const
Definition: geometry.h:374
static const char * compKey(const char *, const char *)
The composite key: (a,b) -> a.b.
void add(const Geom::Point2D< T > &vtx)
Definition: polygon.h:229
void setClosed(bool yn)
Definition: polygon.h:68
#define mMIN(x, y)
Definition: commondefs.h:49
Interval of values.
Definition: commontypes.h:31
double area() const
Definition: polygon.h:85
bool validIdx(int idx) const
Definition: polygon.h:43
bool segmentMeetsBorder(const Geom::Point2D< T > &pt1, const Geom::Point2D< T > &pt2, T eps) const
Definition: polygon.h:316
bool operator>(const ODPolygon< T > &plg) const
Definition: polygon.h:97
Set of (small) copyable elements.
Definition: commontypes.h:30
Generalized set of parameters of the keyword-value type.
Definition: iopar.h:47
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
T y
Definition: geometry.h:67
bool execute()
Definition: paralleltask.h:71
const char * find(const char *) const
returns null if not found
bool isClosed() const
Definition: polygon.h:69
bool anticlockwise() const
Definition: polygon.h:87
void removeZeroLengths()
Definition: polygon.h:371
void setYN(const char *, bool)
void fillPar(IOPar &iop, const ODPolygon< T > &poly, const char *inpkey)
Definition: polygon.h:175
#define mDefEps
Definition: commondefs.h:58
const char * buf() const
Definition: odstring.h:47
static double distToSegment(const Geom::Point2D< T > &p1, const Geom::Point2D< T > &p2, const Geom::Point2D< T > &refpt, double *fractionptr=0)
Definition: polygon.h:643
static bool isOnSegment(const Geom::Point2D< T > &pt, const Geom::Point2D< T > &pt0, const Geom::Point2D< T > &pt1, T eps)
Definition: polygon.h:451
bool isUTurn(int idx) const
Definition: polygon.h:383
T stop
Definition: ranges.h:93
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size...
Definition: bufstring.h:40
bool isSelfIntersecting() const
Definition: polygon.h:396
void insert(int idx, const Geom::Point2D< T > &vtx)
Definition: polygon.h:210
T x
Definition: geometry.h:66
int size() const
Definition: polygon.h:42
void reverse()
Definition: polygon.h:627
bool operator==(const ODPolygon< T > &) const
Definition: polygon.h:156
const Geom::Point2D< T > & prevVertex(int idx) const
Definition: polygon.h:258
static int doSegmentsMeet(const Geom::Point2D< T > &p1, const Geom::Point2D< T > &p2, const Geom::Point2D< T > &q1, const Geom::Point2D< T > &q2, T eps)
Definition: polygon.h:432
static double sgnDistToLine(const Geom::Point2D< T > &point, const Geom::Point2D< T > &dirvec, const Geom::Point2D< T > &posvec)
Definition: polygon.h:522
T start
Definition: ranges.h:92
Interval< T > xrg_
Definition: polygon.h:150
ODPolygon()
Definition: polygon.h:30
Geom::Point2D< T > getUdf() const
Definition: polygon.h:71
const TypeSet< Geom::Point2D< T > > & data() const
Definition: polygon.h:72
ST sgnArea2() const
Definition: polygon.h:539
#define MAXDOUBLE
Definition: commondefs.h:107
Definition: geometry.h:19
#define mClass(module)
Definition: commondefs.h:164
Basic point class.
Definition: geometry.h:27
bool isEmpty(const NLAModel *mdl)
void set(const char *ky, const char *val)
double maxDistToBorderEstimate(double maxrelerr=0.001) const
Definition: polygon.h:703
void setEmpty()
Definition: polygon.h:44
static bool isOnHalfLine(const Geom::Point2D< T > &point, const Geom::Point2D< T > &dirvec, const Geom::Point2D< T > &endvec, T eps)
Definition: polygon.h:462
bool getYN(const char *, bool &) const