OpendTect  6.6
linerectangleclipper.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: A.H.Bril
8  Date: Dec 2006
9  RCS: $Id$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "geometry.h"
15 #include <math.h>
16 
23 template <class T>
25 {
26 public:
28 
29  inline void setLine(const Geom::Point2D<T>& start,
30  const Geom::Point2D<T>& stop);
31 
32  inline bool isIntersecting() const;
33  inline bool isStartChanged() const;
34  inline bool isStopChanged() const;
35  inline const Geom::Point2D<T>& getStart() const;
36  inline const Geom::Point2D<T>& getStop() const;
37 
38 protected:
39  inline const T castDouble2T(double) const;
40 
46 
48 };
49 
50 
51 template <class T> inline
52 void clipPolyLine( const Geom::Rectangle<T>& rect,
53  const TypeSet<Geom::Point2D<T> >& polyline,
54  ObjectSet<TypeSet<Geom::Point2D<T> > >& result )
55 {
56  deepErase( result );
57 
58  LineRectangleClipper<T> clipper( rect );
59 
60  TypeSet<Geom::Point2D<T> >* currentline = 0;
61 
62  for ( int idx=1; idx<polyline.size(); idx++ )
63  {
64  clipper.setLine( polyline[idx-1], polyline[idx] );
65  if ( !clipper.isIntersecting() )
66  {
67  currentline = 0;
68  continue;
69  }
70 
71  if ( currentline && clipper.isStartChanged() )
72  currentline = 0;
73 
74  if ( !currentline )
75  {
76  currentline = new TypeSet<Geom::Point2D<T> >;
77  result += currentline;
78  (*currentline) += clipper.getStart();
79  }
80 
81  (*currentline) += clipper.getStop();
82  if ( clipper.isStopChanged() )
83  currentline = 0;
84  }
85 }
86 
87 
88 template <class T> inline
90  : rect_( r )
91  , isintersecting_( true )
92  , startchanged_( false )
93  , stopchanged_( false )
94 {
95  rect_.sortCorners();
96 }
97 
98 
99 template <class T> inline
100 const T LineRectangleClipper<T>::castDouble2T( double d ) const
101 {
102  const T t1 = (T) d;
103  const T t2 = (T) Math::Floor( d + 0.5 );
104  return fabs(d-t1) < fabs(d-t2) ? t1 : t2;
105 }
106 
107 
108 #define mBoundaryClip( delta, offset ) \
109 { \
110  if ( delta ) \
111  { \
112  double tnew = (double) (offset) / (delta); \
113  if ( (delta) < 0 ) \
114  { \
115  if ( tnew > tstop ) return; \
116  if ( tnew > tstart ) tstart = tnew; \
117  } \
118  else \
119  { \
120  if ( tnew < tstart ) return; \
121  if ( tnew < tstop ) tstop = tnew; \
122  } \
123  } \
124  else \
125  if ( (offset) < 0 ) return; \
126 }
127 
128 #define mAdjustPoint( which ) \
129 { \
130  Geom::Point2D<T> newpoint; \
131  newpoint.x = start.x + castDouble2T( dx * t##which ); \
132  newpoint.y = start.y + castDouble2T( dy * t##which ); \
133  which##changed_ = which##_ != newpoint; \
134  which##_ = newpoint; \
135 }
136 
137 /* LineRectangleClipper applies the Liang&Barsky line-clipping algorithm */
138 template <class T> inline
140  const Geom::Point2D<T>& stop)
141 {
142  isintersecting_ = false;
143  start_ = start;
144  stop_ = stop;
145  startchanged_ = false;
146  stopchanged_ = false;
147 
148  double tstart = 0.0;
149  double tstop = 1.0;
150 
151  const double dx = stop.x - start.x;
152  mBoundaryClip( -dx, start.x - rect_.left() );
153  mBoundaryClip( dx, rect_.right() - start.x );
154 
155  const double dy = stop.y - start.y;
156  mBoundaryClip( -dy, start.y - rect_.top() );
157  mBoundaryClip( dy, rect_.bottom() - start.y );
158 
159  isintersecting_ = true;
160 
161  if ( tstart > 0.0 )
162  mAdjustPoint( start );
163 
164  if ( tstop < 1.0 )
165  mAdjustPoint( stop );
166 }
167 
168 
169 template <class T> inline
171 { return isintersecting_; }
172 
173 
174 template <class T> inline
176 { return startchanged_; }
177 
178 
179 template <class T> inline
181 { return stopchanged_; }
182 
183 
184 template <class T> inline
186 { return start_; }
187 
188 
189 template <class T> inline
191 { return stop_; }
192 
193 
mAdjustPoint
#define mAdjustPoint(which)
Definition: linerectangleclipper.h:128
geometry.h
LineRectangleClipper::getStart
const Geom::Point2D< T > & getStart() const
Definition: linerectangleclipper.h:185
ObjectSet
Set of pointers to objects.
Definition: commontypes.h:31
LineRectangleClipper::rect_
Geom::Rectangle< T > rect_
Definition: linerectangleclipper.h:47
LineRectangleClipper::castDouble2T
const T castDouble2T(double) const
Definition: linerectangleclipper.h:100
LineRectangleClipper::start_
Geom::Point2D< T > start_
Definition: linerectangleclipper.h:44
LineRectangleClipper::isStartChanged
bool isStartChanged() const
Definition: linerectangleclipper.h:175
Math::Floor
float Floor(float)
mBoundaryClip
#define mBoundaryClip(delta, offset)
Definition: linerectangleclipper.h:108
LineRectangleClipper::getStop
const Geom::Point2D< T > & getStop() const
Definition: linerectangleclipper.h:190
LineRectangleClipper::startchanged_
bool startchanged_
Definition: linerectangleclipper.h:42
LineRectangleClipper::stopchanged_
bool stopchanged_
Definition: linerectangleclipper.h:43
setLine
void setLine(uiLineItem *, const LineParameters< float > &, const uiAxisHandler *xah, const uiAxisHandler *yah, const Interval< float > *xvalrg=0)
draws line not outside box defined by X and Y value ranges
mClass
#define mClass(module)
Definition: commondefs.h:181
LineRectangleClipper::stop_
Geom::Point2D< T > stop_
Definition: linerectangleclipper.h:45
LineRectangleClipper::isStopChanged
bool isStopChanged() const
Definition: linerectangleclipper.h:180
LineRectangleClipper::setLine
void setLine(const Geom::Point2D< T > &start, const Geom::Point2D< T > &stop)
Definition: linerectangleclipper.h:139
clipPolyLine
void clipPolyLine(const Geom::Rectangle< T > &rect, const TypeSet< Geom::Point2D< T > > &polyline, ObjectSet< TypeSet< Geom::Point2D< T > > > &result)
Definition: linerectangleclipper.h:52
LineRectangleClipper::isIntersecting
bool isIntersecting() const
Definition: linerectangleclipper.h:170
deepErase
void deepErase(BufferStringSet &)
Geom::Point2D
Basic point class.
Definition: geometry.h:27
LineRectangleClipper::isintersecting_
bool isintersecting_
Definition: linerectangleclipper.h:41
Geom::Point2D::y
T y
Definition: geometry.h:68
Geom::Point2D::x
T x
Definition: geometry.h:67
LineRectangleClipper::LineRectangleClipper
LineRectangleClipper(const Geom::Rectangle< T > &)
Definition: linerectangleclipper.h:89
Geom::Rectangle
Basic 2D rectangle class.
Definition: geometry.h:118
LineRectangleClipper
Clips a line between two points by a rectangle. The line may be completely outside,...
Definition: linerectangleclipper.h:25
TypeSet
Sets of (small) copyable elements.
Definition: commontypes.h:29

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B.V. 1995-2021