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

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