Changeset 11158 in ntrip for trunk/BNC/qwt/qwt_clipper.cpp
- Timestamp:
- Nov 3, 2026, 10:53:35 AM (3 weeks ago)
- File:
-
- 1 edited
-
trunk/BNC/qwt/qwt_clipper.cpp (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/BNC/qwt/qwt_clipper.cpp
r9383 r11158 1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*-*****************************1 /****************************************************************************** 2 2 * Qwt Widget Library 3 3 * Copyright (C) 1997 Josef Wilgen … … 10 10 #include "qwt_clipper.h" 11 11 #include "qwt_point_polar.h" 12 #include "qwt_interval.h" 13 #include "qwt_math.h" 14 15 #include <qpolygon.h> 12 16 #include <qrect.h> 13 #include <string.h> 14 #include <stdlib.h> 15 16 #if QT_VERSION < 0x040601 17 #define qAtan(x) ::atan(x) 18 #endif 17 18 #include <algorithm> 19 19 20 20 namespace QwtClip 21 21 { 22 22 // some templates used for inlining 23 template <class Point, typename T> class LeftEdge; 24 template <class Point, typename T> class RightEdge; 25 template <class Point, typename T> class TopEdge; 26 template <class Point, typename T> class BottomEdge; 27 28 template <class Point> class PointBuffer; 29 } 30 31 template <class Point, typename Value> 23 template< class Point, typename T > class LeftEdge; 24 template< class Point, typename T > class RightEdge; 25 template< class Point, typename T > class TopEdge; 26 template< class Point, typename T > class BottomEdge; 27 } 28 29 template< class Point, typename Value > 32 30 class QwtClip::LeftEdge 33 31 { 34 public: 32 public: 35 33 inline LeftEdge( Value x1, Value, Value, Value ): 36 d_x1( x1 )37 { 38 } 39 40 inline bool isInside( const Point &p ) const41 { 42 return p.x() >= d_x1;43 } 44 45 inline Point intersection( const Point &p1, const Point&p2 ) const34 m_x1( x1 ) 35 { 36 } 37 38 inline bool isInside( const Point& p ) const 39 { 40 return p.x() >= m_x1; 41 } 42 43 inline Point intersection( const Point& p1, const Point& p2 ) const 46 44 { 47 45 double dy = ( p1.y() - p2.y() ) / double( p1.x() - p2.x() ); 48 return Point( d_x1, static_cast< Value >( p2.y() + (d_x1 - p2.x() ) * dy ) );49 } 50 private: 51 const Value d_x1;52 }; 53 54 template <class Point, typename Value>46 return Point( m_x1, static_cast< Value >( p2.y() + ( m_x1 - p2.x() ) * dy ) ); 47 } 48 private: 49 const Value m_x1; 50 }; 51 52 template< class Point, typename Value > 55 53 class QwtClip::RightEdge 56 54 { 57 public: 55 public: 58 56 inline RightEdge( Value, Value x2, Value, Value ): 59 d_x2( x2 )60 { 61 } 62 63 inline bool isInside( const Point &p ) const64 { 65 return p.x() <= d_x2;66 } 67 68 inline Point intersection( const Point &p1, const Point&p2 ) const57 m_x2( x2 ) 58 { 59 } 60 61 inline bool isInside( const Point& p ) const 62 { 63 return p.x() <= m_x2; 64 } 65 66 inline Point intersection( const Point& p1, const Point& p2 ) const 69 67 { 70 68 double dy = ( p1.y() - p2.y() ) / double( p1.x() - p2.x() ); 71 return Point( d_x2, static_cast<Value>( p2.y() + (d_x2 - p2.x() ) * dy ) );72 } 73 74 private: 75 const Value d_x2;76 }; 77 78 template <class Point, typename Value>69 return Point( m_x2, static_cast< Value >( p2.y() + ( m_x2 - p2.x() ) * dy ) ); 70 } 71 72 private: 73 const Value m_x2; 74 }; 75 76 template< class Point, typename Value > 79 77 class QwtClip::TopEdge 80 78 { 81 public: 79 public: 82 80 inline TopEdge( Value, Value, Value y1, Value ): 83 d_y1( y1 )84 { 85 } 86 87 inline bool isInside( const Point &p ) const88 { 89 return p.y() >= d_y1;90 } 91 92 inline Point intersection( const Point &p1, const Point&p2 ) const81 m_y1( y1 ) 82 { 83 } 84 85 inline bool isInside( const Point& p ) const 86 { 87 return p.y() >= m_y1; 88 } 89 90 inline Point intersection( const Point& p1, const Point& p2 ) const 93 91 { 94 92 double dx = ( p1.x() - p2.x() ) / double( p1.y() - p2.y() ); 95 return Point( static_cast< Value>( p2.x() + (d_y1 - p2.y() ) * dx ),d_y1 );96 } 97 98 private: 99 const Value d_y1;100 }; 101 102 template <class Point, typename Value>93 return Point( static_cast< Value >( p2.x() + ( m_y1 - p2.y() ) * dx ), m_y1 ); 94 } 95 96 private: 97 const Value m_y1; 98 }; 99 100 template< class Point, typename Value > 103 101 class QwtClip::BottomEdge 104 102 { 105 public: 103 public: 106 104 inline BottomEdge( Value, Value, Value, Value y2 ): 107 d_y2( y2 )108 { 109 } 110 111 inline bool isInside( const Point &p ) const112 { 113 return p.y() <= d_y2;114 } 115 116 inline Point intersection( const Point &p1, const Point&p2 ) const105 m_y2( y2 ) 106 { 107 } 108 109 inline bool isInside( const Point& p ) const 110 { 111 return p.y() <= m_y2; 112 } 113 114 inline Point intersection( const Point& p1, const Point& p2 ) const 117 115 { 118 116 double dx = ( p1.x() - p2.x() ) / double( p1.y() - p2.y() ); 119 return Point( static_cast<Value>( p2.x() + ( d_y2 - p2.y() ) * dx ), d_y2 ); 120 } 121 122 private: 123 const Value d_y2; 124 }; 125 126 template<class Point> 127 class QwtClip::PointBuffer 128 { 129 public: 130 PointBuffer( int capacity = 0 ): 131 m_capacity( 0 ), 132 m_size( 0 ), 133 m_buffer( NULL ) 134 { 135 if ( capacity > 0 ) 136 reserve( capacity ); 137 } 138 139 ~PointBuffer() 140 { 141 if ( m_buffer ) 142 ::free( m_buffer ); 143 } 144 145 inline void setPoints( int numPoints, const Point *points ) 146 { 147 reserve( numPoints ); 148 149 m_size = numPoints; 150 ::memcpy( m_buffer, points, m_size * sizeof( Point ) ); 151 } 152 153 inline void reset() 154 { 155 m_size = 0; 156 } 157 158 inline int size() const 159 { 160 return m_size; 161 } 162 163 inline Point *data() const 164 { 165 return m_buffer; 166 } 167 168 inline Point &operator[]( int i ) 169 { 170 return m_buffer[i]; 171 } 172 173 inline const Point &operator[]( int i ) const 174 { 175 return m_buffer[i]; 176 } 177 178 inline void add( const Point &point ) 179 { 180 if ( m_capacity <= m_size ) 181 reserve( m_size + 1 ); 182 183 m_buffer[m_size++] = point; 184 } 185 186 private: 187 inline void reserve( int size ) 188 { 189 if ( m_capacity == 0 ) 190 m_capacity = 1; 191 192 while ( m_capacity < size ) 193 m_capacity *= 2; 194 195 m_buffer = static_cast<Point *>( 196 ::realloc( m_buffer, m_capacity * sizeof( Point ) ) ); 197 } 198 199 int m_capacity; 200 int m_size; 201 Point *m_buffer; 117 return Point( static_cast< Value >( p2.x() + ( m_y2 - p2.y() ) * dx ), m_y2 ); 118 } 119 120 private: 121 const Value m_y2; 202 122 }; 203 123 204 124 using namespace QwtClip; 205 125 206 template <class Polygon, class Rect,class Point,typename T>126 template< class Polygon, class Rect, typename T > 207 127 class QwtPolygonClipper 208 128 { 209 public: 210 QwtPolygonClipper( const Rect &clipRect ): 211 d_clipRect( clipRect ) 212 { 213 } 214 215 Polygon clipPolygon( const Polygon &polygon, bool closePolygon ) const 129 typedef typename Polygon::value_type Point; 130 public: 131 explicit QwtPolygonClipper( const Rect& clipRect ): 132 m_clipRect( clipRect ) 133 { 134 } 135 136 void clipPolygon( Polygon& points1, bool closePolygon ) const 216 137 { 217 138 #if 0 218 if ( d_clipRect.contains( polygon.boundingRect() ) )139 if ( m_clipRect.contains( points1.boundingRect() ) ) 219 140 return polygon; 220 141 #endif 221 142 222 PointBuffer<Point> points1; 223 PointBuffer<Point> points2( qMin( 256, polygon.size() ) ); 224 225 points1.setPoints( polygon.size(), polygon.data() ); 226 227 clipEdge< LeftEdge<Point, T> >( closePolygon, points1, points2 ); 228 clipEdge< RightEdge<Point, T> >( closePolygon, points2, points1 ); 229 clipEdge< TopEdge<Point, T> >( closePolygon, points1, points2 ); 230 clipEdge< BottomEdge<Point, T> >( closePolygon, points2, points1 ); 231 232 Polygon p; 233 p.resize( points1.size() ); 234 ::memcpy( p.data(), points1.data(), points1.size() * sizeof( Point ) ); 235 236 return p; 237 } 238 239 private: 240 template <class Edge> 143 Polygon points2; 144 points2.reserve( qMin( 256, points1.size() ) ); 145 146 clipEdge< LeftEdge< Point, T > >( closePolygon, points1, points2 ); 147 clipEdge< RightEdge< Point, T > >( closePolygon, points2, points1 ); 148 clipEdge< TopEdge< Point, T > >( closePolygon, points1, points2 ); 149 clipEdge< BottomEdge< Point, T > >( closePolygon, points2, points1 ); 150 } 151 152 private: 153 template< class Edge > 241 154 inline void clipEdge( bool closePolygon, 242 PointBuffer<Point> &points, PointBuffer<Point> &clippedPoints ) const243 { 244 clippedPoints. reset();155 const Polygon& points, Polygon& clippedPoints ) const 156 { 157 clippedPoints.clear(); 245 158 246 159 if ( points.size() < 2 ) 247 160 { 248 161 if ( points.size() == 1 ) 249 clippedPoints.add( points[0] ); 162 clippedPoints += points[0]; 163 250 164 return; 251 165 } 252 166 253 const Edge edge( d_clipRect.x(), d_clipRect.x() + d_clipRect.width(), 254 d_clipRect.y(), d_clipRect.y() + d_clipRect.height() ); 255 256 int lastPos, start; 257 if ( closePolygon ) 258 { 259 start = 0; 260 lastPos = points.size() - 1; 167 const Edge edge( m_clipRect.x(), m_clipRect.x() + m_clipRect.width(), 168 m_clipRect.y(), m_clipRect.y() + m_clipRect.height() ); 169 170 if ( !closePolygon ) 171 { 172 const Point& p1 = points.first(); 173 174 if ( edge.isInside( p1 ) ) 175 clippedPoints += p1; 261 176 } 262 177 else 263 178 { 264 start = 1; 265 lastPos = 0; 266 267 if ( edge.isInside( points[0] ) ) 268 clippedPoints.add( points[0] ); 269 } 270 271 const uint nPoints = points.size(); 272 for ( uint i = start; i < nPoints; i++ ) 273 { 274 const Point &p1 = points[i]; 275 const Point &p2 = points[lastPos]; 179 const Point& p1 = points.first(); 180 const Point& p2 = points.last(); 276 181 277 182 if ( edge.isInside( p1 ) ) 278 183 { 279 if ( edge.isInside( p2 ) ) 280 { 281 clippedPoints.add( p1 ); 282 } 283 else 284 { 285 clippedPoints.add( edge.intersection( p1, p2 ) ); 286 clippedPoints.add( p1 ); 287 } 184 if ( !edge.isInside( p2 ) ) 185 clippedPoints += edge.intersection( p1, p2 ); 186 187 clippedPoints += p1; 288 188 } 289 else 189 else if ( edge.isInside( p2 ) ) 290 190 { 291 if ( edge.isInside( p2 ) ) 292 { 293 clippedPoints.add( edge.intersection( p1, p2 ) ); 294 } 191 clippedPoints += edge.intersection( p1, p2 ); 295 192 } 296 lastPos = i; 297 } 298 } 299 300 const Rect d_clipRect; 193 } 194 195 const uint nPoints = points.size(); 196 const Point* p = points.constData(); 197 198 for ( uint i = 1; i < nPoints; i++ ) 199 { 200 const Point& p1 = p[i]; 201 const Point& p2 = p[i - 1]; 202 203 if ( edge.isInside( p1 ) ) 204 { 205 if ( !edge.isInside( p2 ) ) 206 clippedPoints += edge.intersection( p1, p2 ); 207 208 clippedPoints += p1; 209 } 210 else if ( edge.isInside( p2 ) ) 211 { 212 clippedPoints += edge.intersection( p1, p2 ); 213 } 214 } 215 } 216 217 const Rect m_clipRect; 301 218 }; 302 219 303 220 class QwtCircleClipper 304 221 { 305 public: 306 QwtCircleClipper( const QRectF &r );307 QVector<QwtInterval> clipCircle( const QPointF &, double radius ) const;308 309 private: 222 public: 223 explicit QwtCircleClipper( const QRectF& r ); 224 QVector< QwtInterval > clipCircle( const QPointF&, double radius ) const; 225 226 private: 310 227 enum Edge 311 228 { … … 318 235 }; 319 236 320 QList<QPointF> cuttingPoints( 321 Edge, const QPointF &pos, double radius ) const; 322 323 double toAngle( const QPointF &, const QPointF & ) const; 324 325 const QRectF d_rect; 326 }; 327 328 329 QwtCircleClipper::QwtCircleClipper( const QRectF &r ): 330 d_rect( r ) 331 { 332 } 333 334 QVector<QwtInterval> QwtCircleClipper::clipCircle( 335 const QPointF &pos, double radius ) const 336 { 337 QList<QPointF> points; 237 QVector< QPointF > cuttingPoints( 238 Edge, const QPointF& pos, double radius ) const; 239 240 double toAngle( const QPointF&, const QPointF& ) const; 241 242 const QRectF m_rect; 243 }; 244 245 246 QwtCircleClipper::QwtCircleClipper( const QRectF& r ) 247 : m_rect( r ) 248 { 249 } 250 251 QVector< QwtInterval > QwtCircleClipper::clipCircle( 252 const QPointF& pos, double radius ) const 253 { 254 // using QVarLengthArray TODO ... 255 256 QVector< QPointF > points; 338 257 for ( int edge = 0; edge < NEdges; edge++ ) 339 points += cuttingPoints( static_cast< Edge>(edge), pos, radius );340 341 QVector<QwtInterval> intv; 258 points += cuttingPoints( static_cast< Edge >( edge ), pos, radius ); 259 260 QVector< QwtInterval > intv; 342 261 if ( points.size() <= 0 ) 343 262 { 344 263 QRectF cRect( 0, 0, 2 * radius, 2 * radius ); 345 264 cRect.moveCenter( pos ); 346 if ( d_rect.contains( cRect ) )265 if ( m_rect.contains( cRect ) ) 347 266 intv += QwtInterval( 0.0, 2 * M_PI ); 348 267 } 349 268 else 350 269 { 351 QList<double> angles; 270 QVector< double > angles; 271 angles.reserve( points.size() ); 272 352 273 for ( int i = 0; i < points.size(); i++ ) 353 274 angles += toAngle( pos, points[i] ); 354 qSort( angles ); 355 356 const int in = d_rect.contains( qwtPolar2Pos( pos, radius, 275 276 std::sort( angles.begin(), angles.end() ); 277 278 const int in = m_rect.contains( qwtPolar2Pos( pos, radius, 357 279 angles[0] + ( angles[1] - angles[0] ) / 2 ) ); 358 280 281 intv.reserve( angles.size() / 2 ); 359 282 if ( in ) 360 283 { 361 284 for ( int i = 0; i < angles.size() - 1; i += 2 ) 362 intv += QwtInterval( angles[i], angles[i +1] );285 intv += QwtInterval( angles[i], angles[i + 1] ); 363 286 } 364 287 else 365 288 { 366 289 for ( int i = 1; i < angles.size() - 1; i += 2 ) 367 intv += QwtInterval( angles[i], angles[i+1] ); 290 intv += QwtInterval( angles[i], angles[i + 1] ); 291 368 292 intv += QwtInterval( angles.last(), angles.first() ); 369 293 } … … 374 298 375 299 double QwtCircleClipper::toAngle( 376 const QPointF &from, const QPointF&to ) const300 const QPointF& from, const QPointF& to ) const 377 301 { 378 302 if ( from.x() == to.x() ) … … 381 305 const double m = qAbs( ( to.y() - from.y() ) / ( to.x() - from.x() ) ); 382 306 383 double angle = qAtan( m );307 double angle = std::atan( m ); 384 308 if ( to.x() > from.x() ) 385 309 { … … 398 322 } 399 323 400 Q List<QPointF> QwtCircleClipper::cuttingPoints(401 Edge edge, const QPointF &pos, double radius ) const402 { 403 Q List<QPointF> points;324 QVector< QPointF > QwtCircleClipper::cuttingPoints( 325 Edge edge, const QPointF& pos, double radius ) const 326 { 327 QVector< QPointF > points; 404 328 405 329 if ( edge == Left || edge == Right ) 406 330 { 407 const double x = ( edge == Left ) ? d_rect.left() :d_rect.right();331 const double x = ( edge == Left ) ? m_rect.left() : m_rect.right(); 408 332 if ( qAbs( pos.x() - x ) < radius ) 409 333 { 410 const double off = qSqrt( qwtSqr( radius ) - qwtSqr( pos.x() - x ) );334 const double off = std::sqrt( qwtSqr( radius ) - qwtSqr( pos.x() - x ) ); 411 335 const double m_y1 = pos.y() + off; 412 if ( m_y1 >= d_rect.top() && m_y1 <=d_rect.bottom() )336 if ( m_y1 >= m_rect.top() && m_y1 <= m_rect.bottom() ) 413 337 points += QPointF( x, m_y1 ); 414 338 415 339 const double m_y2 = pos.y() - off; 416 if ( m_y2 >= d_rect.top() && m_y2 <=d_rect.bottom() )340 if ( m_y2 >= m_rect.top() && m_y2 <= m_rect.bottom() ) 417 341 points += QPointF( x, m_y2 ); 418 342 } … … 420 344 else 421 345 { 422 const double y = ( edge == Top ) ? d_rect.top() :d_rect.bottom();346 const double y = ( edge == Top ) ? m_rect.top() : m_rect.bottom(); 423 347 if ( qAbs( pos.y() - y ) < radius ) 424 348 { 425 const double off = qSqrt( qwtSqr( radius ) - qwtSqr( pos.y() - y ) );349 const double off = std::sqrt( qwtSqr( radius ) - qwtSqr( pos.y() - y ) ); 426 350 const double x1 = pos.x() + off; 427 if ( x1 >= d_rect.left() && x1 <=d_rect.right() )351 if ( x1 >= m_rect.left() && x1 <= m_rect.right() ) 428 352 points += QPointF( x1, y ); 429 353 430 354 const double m_x2 = pos.x() - off; 431 if ( m_x2 >= d_rect.left() && m_x2 <=d_rect.right() )355 if ( m_x2 >= m_rect.left() && m_x2 <= m_rect.right() ) 432 356 points += QPointF( m_x2, y ); 433 357 } … … 440 364 441 365 \param clipRect Clip rectangle 442 \param polygon Polygon 443 \param closePolygon True, when the polygon is closed 444 445 \return Clipped polygon 446 */ 447 QPolygon QwtClipper::clipPolygon( 448 const QRectF &clipRect, const QPolygon &polygon, bool closePolygon ) 366 \param polygon Polygon IN/OUT 367 \param closePolygon True, when the polygon is closed 368 */ 369 void QwtClipper::clipPolygon( 370 const QRectF& clipRect, QPolygon& polygon, bool closePolygon ) 449 371 { 450 372 const int minX = qCeil( clipRect.left() ); … … 455 377 const QRect r( minX, minY, maxX - minX, maxY - minY ); 456 378 457 QwtPolygonClipper<QPolygon, QRect, QPoint, int> clipper( r ); 458 return clipper.clipPolygon( polygon, closePolygon ); 459 } 379 QwtPolygonClipper< QPolygon, QRect, int > clipper( r ); 380 clipper.clipPolygon( polygon, closePolygon ); 381 } 382 383 /*! 384 Sutherland-Hodgman polygon clipping 385 386 \param clipRect Clip rectangle 387 \param polygon Polygon IN/OUT 388 \param closePolygon True, when the polygon is closed 389 */ 390 void QwtClipper::clipPolygon( 391 const QRect& clipRect, QPolygon& polygon, bool closePolygon ) 392 { 393 QwtPolygonClipper< QPolygon, QRect, int > clipper( clipRect ); 394 clipper.clipPolygon( polygon, closePolygon ); 395 } 396 397 /*! 398 Sutherland-Hodgman polygon clipping 399 400 \param clipRect Clip rectangle 401 \param polygon Polygon IN/OUT 402 \param closePolygon True, when the polygon is closed 403 */ 404 void QwtClipper::clipPolygonF( 405 const QRectF& clipRect, QPolygonF& polygon, bool closePolygon ) 406 { 407 QwtPolygonClipper< QPolygonF, QRectF, double > clipper( clipRect ); 408 clipper.clipPolygon( polygon, closePolygon ); 409 } 410 460 411 /*! 461 412 Sutherland-Hodgman polygon clipping … … 466 417 467 418 \return Clipped polygon 468 */ 469 QPolygon QwtClipper::clipPolygon( 470 const QRect &clipRect, const QPolygon &polygon, bool closePolygon ) 471 { 472 QwtPolygonClipper<QPolygon, QRect, QPoint, int> clipper( clipRect ); 473 return clipper.clipPolygon( polygon, closePolygon ); 474 } 475 419 */ 420 QPolygon QwtClipper::clippedPolygon( 421 const QRectF& clipRect, const QPolygon& polygon, bool closePolygon ) 422 { 423 QPolygon points( polygon ); 424 clipPolygon( clipRect, points, closePolygon ); 425 426 return points; 427 } 476 428 /*! 477 429 Sutherland-Hodgman polygon clipping … … 482 434 483 435 \return Clipped polygon 484 */ 485 QPolygonF QwtClipper::clipPolygonF( 486 const QRectF &clipRect, const QPolygonF &polygon, bool closePolygon ) 487 { 488 QwtPolygonClipper<QPolygonF, QRectF, QPointF, double> clipper( clipRect ); 489 return clipper.clipPolygon( polygon, closePolygon ); 436 */ 437 QPolygon QwtClipper::clippedPolygon( 438 const QRect& clipRect, const QPolygon& polygon, bool closePolygon ) 439 { 440 QPolygon points( polygon ); 441 clipPolygon( clipRect, points, closePolygon ); 442 443 return points; 444 } 445 446 /*! 447 Sutherland-Hodgman polygon clipping 448 449 \param clipRect Clip rectangle 450 \param polygon Polygon 451 \param closePolygon True, when the polygon is closed 452 453 \return Clipped polygon 454 */ 455 QPolygonF QwtClipper::clippedPolygonF( 456 const QRectF& clipRect, const QPolygonF& polygon, bool closePolygon ) 457 { 458 QPolygonF points( polygon ); 459 clipPolygonF( clipRect, points, closePolygon ); 460 461 return points; 490 462 } 491 463 … … 502 474 503 475 \return Arcs of the circle 504 */ 505 QVector<QwtInterval> QwtClipper::clipCircle( const QRectF &clipRect,506 const QPointF ¢er, double radius )476 */ 477 QVector< QwtInterval > QwtClipper::clipCircle( const QRectF& clipRect, 478 const QPointF& center, double radius ) 507 479 { 508 480 QwtCircleClipper clipper( clipRect );
Note:
See TracChangeset
for help on using the changeset viewer.
