Parallel Plane Sweep  0.1
Shared memory multithreaded version of the plane sweep algorithm
parPlaneSweep.h
Go to the documentation of this file.
1 /*
2  * The MIT License (MIT)
3  * Copyright (c) <2016> <Mark McKenney>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6  *
7  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10  * */
11 
12 #include <omp.h>
13 #include <vector>
14 #include <algorithm>
15 #include <limits>
16 #include <iostream>
17 #include <cstdlib>
18 extern "C" {
19 #include "avl.h"
20 }
21 
22 #ifndef PARSESWEEP_H
23 #define PARSESWEEP_H
24 
25 
26 
36 using namespace std;
37 
38 //#define DEBUG_PRINT
39 
40 
50 inline bool leftHandturn( double p1x, double p1y, double p2x, double p2y, double p3x, double p3y )
51 {
52  // returns true if when you start at p1, and walk towards p2, you have to make a left turn
53  // at p2 in order to continue walking to p3
54  return( ( ((p3y - p1y) * (p2x - p1x)) - ((p2y - p1y) * (p3x - p1x)) ) > 0 );
55 }
56 
57 
76 struct halfsegment {
77  // dominating and submissive points
79  double dx, dy, sx, sy;
81  int la, lb;
83  int stripID;
85  int regionID;
87  int ola, olb;
88 
92 halfsegment():dx( 0 ), dy(0), sx(0), sy(0), la(-1), lb(-1),
93  stripID(-1), regionID( -1 ), ola( -1 ), olb( -1 )
94  { }
95 
99 halfsegment( const halfsegment &rhs ): dx(rhs.dx), dy(rhs.dy),
100  sx(rhs.sx), sy(rhs.sy),
101  la(rhs.la), lb(rhs.lb),
102  stripID( rhs.stripID), regionID( rhs.regionID ),
103  ola( rhs.ola ), olb( rhs.olb )
104  { }
108  bool isLeft( ) const {
109  return( dx < sx || (dx == sx && dy < sy ) );
110  }
111 
116  halfsegment tmp( *this );
117  tmp.dx = sx;
118  tmp.dy = sy;
119  tmp.sx = dx;
120  tmp.sy = dy;
121  return tmp;
122  }
123 
131  bool operator==( const halfsegment &rhs ) const {
132  return dx == rhs.dx && dy == rhs.dy && sx == rhs.sx && sy == rhs.sy;
133  }
134 
143  bool operator<( const halfsegment &rhs ) const {
144  if( dx < rhs.dx || (dx == rhs.dx && dy < rhs.dy ) ) return true;
145  else if( dx == rhs.dx && dy == rhs.dy ) {
146  // dom points the same
147  if( isLeft() != rhs.isLeft( ) ) return !isLeft();
148  // both left or right hsegs
149  else if( this->colinear( rhs ) && (sx < rhs.sx || (sx == rhs.sx && sy < rhs.sy)) ) return true;
150  else if( leftHandturn( dx, dy, sx, sy, rhs.sx, rhs.sy ) ) return true;
151  }
152  return false;
153  }
154 
160  inline bool colinear( const halfsegment &rhs) const {
161  return 0 == ( ((rhs.dy - dy) * (sx - dx)) - ((sy - dy) * (rhs.dx - dx)) ) &&
162  0 == ( ((rhs.sy - dy) * (sx - dx)) - ((sy - dy) * (rhs.sx - dx)) ) ;
163  }
164 
174  double getYvalAtX( const double x ) const
175  {
176  if( x == dx ) return dy;
177  else if( x == sx ) return sy;
178  return( ( (sy*x - sy*dx - dy*x + dy*dx) / float((sx-dx))) + dy );
179  }
180 
184  friend ostream & operator<<( ostream & out, const halfsegment & rhs ) {
185  cerr <<"[(" << rhs.dx << "," << rhs.dy << ")(" << rhs.sx << "," << rhs.sy << ") " << rhs.la << ", " << rhs.lb << ", " << rhs.regionID << " <" << rhs.ola <<","<<rhs.olb<<">"<< "]";
186  return out;
187  }
188 
189 };
190 
191 
202 void parallelOverlay( vector<halfsegment> &r1, vector<halfsegment> &r2, vector<halfsegment> &result,
203  int numSplits=-1, int numWorkerThreads = -1);
204 
216 void overlayPlaneSweep( const halfsegment r1[], int r1Size,
217  const halfsegment r2[], int r2Size,
218  vector<halfsegment>& result );
219 #endif
220 
221 
int ola
overlap labels.
Definition: parPlaneSweep.h:87
bool colinear(const halfsegment &rhs) const
holds a single halfsegment, along with labeling information and information indicating if the opposin...
Definition: parPlaneSweep.h:76
bool leftHandturn(double p1x, double p1y, double p2x, double p2y, double p3x, double p3y)
Definition: parPlaneSweep.h:50
int regionID
The region the seg belongs to.
Definition: parPlaneSweep.h:85
double getYvalAtX(const double x) const
bool operator<(const halfsegment &rhs) const
void overlayPlaneSweep(const halfsegment r1[], int r1Size, const halfsegment r2[], int r2Size, vector< halfsegment > &result)
int stripID
strip ID
Definition: parPlaneSweep.h:83
bool isLeft() const
double dx
dominating and submissive points
Definition: parPlaneSweep.h:79
int la
label above, label below
Definition: parPlaneSweep.h:81
friend ostream & operator<<(ostream &out, const halfsegment &rhs)
halfsegment(const halfsegment &rhs)
Definition: parPlaneSweep.h:99
halfsegment getBrother() const
bool operator==(const halfsegment &rhs) const
void parallelOverlay(vector< halfsegment > &r1, vector< halfsegment > &r2, vector< halfsegment > &result, int numSplits=-1, int numWorkerThreads=-1)