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

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