Sweep line algorithm applied to segment intersection

Hello! I need advice on updating binary search three. I am working on implementation of Bentley-Ottoman algorithm for finding intersections of line segments in the plane. Code I designed works properly for all, but certain types of triangles. What is happening is that one intersection inside the triangle is never detected due to a fact that segments which intersect in that point never become neighbors in binary search tree. Anyone got an idea how to resolve this problem? Thank you :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
class segment{
  double x1,y1,x2,y2;
  int name;
  double nosac[3];
public:
  segment():x1(0),y1(0),x2(0),y2(0),name(0){nosac[0]=nosac[1]=nosac[2]=0;}
  segment(double X1,double Y1,double X2,double Y2);
  void asign_name(int NAME){name=NAME;}
  int return_name(){return name;}
  int position(vector<double> point);
  vector<double> left(){vector<double> P(2); P[0]=x1; P[1]=y1; return P;}
  vector<double> right(){vector<double> K(2); K[0]=x2; K[1]=y2; return K;}
  friend vector<double> intersect(segment a, segment b);
  friend segment vrati_segment(vector<segment> S, int name);
  friend bool kriterij_yY(segment a, segment b);
  friend class kriterij_xXYy;
};

segment::segment(double X1,double Y1,double X2,double Y2):name(0){
  if(X1<X2){x1=X1; y1=Y1; x2=X2; y2=Y2;}
  else if(X1>X2){x1=X2; y1=Y2; x2=X1; y2=Y1;}
  else{
    if(Y1>Y2){x1=X1; y1=Y1; x2=X2; y2=Y2;}
    else if (Y1<Y2){x1=X1; y1=Y2; x2=X2; y2=Y1;}
    else throw "Segment cannot have length 0!\n";
  } 
  nosac[0]=y1-y2;
  nosac[1]=x2-x1;
  nosac[2]=x1*y2-y1*x2;
}

int segment::position(vector<double> t){
  if(x1==t[0] && y1==t[1])return 0;
  else if(x2==t[0] && y2==t[1])return 2;
  else if(min(x1,x2)<=t[0] && t[0]<=max(x1,x2) && min(y1,y2)<=t[1] && 
          t[1]<=max(y1,y2) && (t[1]-y1)*(x2-x1)-(t[0]-x1)*(y2-y1)==0)return 1;
  else return -1;
}    

vector<double> intersect(segment a, segment b){
  vector<double> N;
  double p=b.nosac[0]*a.nosac[1]-a.nosac[0]*b.nosac[1];
  if(!p){
    N.push_back(0); 
    N.push_back(0);
    N.push_back(0);
  }
  else{
    N.push_back((b.nosac[1]*a.nosac[2]-a.nosac[1]*b.nosac[2])/p); 
    N.push_back((a.nosac[0]*b.nosac[2]-b.nosac[0]*a.nosac[2])/p);
    if(min(a.x1,a.x2)<=N[0] && min(b.x1,b.x2)<=N[0] && max(a.x1,a.x2)>=N[0] &&
       max(b.x1,b.x2)>=N[0] && min(a.y1,a.y2)<=N[1] && min(b.y1,b.y2)<=N[1] &&
       max(a.y1,a.y2)>=N[1] && max(b.y1,b.y2)>=N[1])N.push_back(1);
    else N.push_back(0);
  }
  return N; 
} 

segment vrati_segment(vector<segment> S, int NAME){
  segment pom;
  for(int i=0; i<S.size(); i++)if((S[i]).name==NAME)pom=S[i];
  return pom;        
}

class kriterij_xXYy{
public:
  bool operator()(segment a, segment b)const
    {return ((a.x1<b.x1 || (a.x1==b.x1 && a.y1>b.y1)) || 
            ((a.y1==b.y1 && a.x1==b.x1) && (a.x2!=b.x2 || a.y2!=b.y2)));}
};

class prioritet{
public:
  bool operator()(vector<double> a, vector<double> b)const
    {return a[0]>b[0] || (a[0]==b[0] && a[1]<b[1]);}
};   

bool kriterij_yY(segment a, segment b){
  return ((a.y1<b.y1 || (a.y1==b.y1 && a.x1>b.x1)) || 
         ((a.y1==b.y1 && a.x1==b.x1) && (a.x2!=b.x2 || a.y2!=b.y2)));
}

bool uQ (priority_queue <vector<double>,vector <vector<double> >,prioritet> Q, vector<double> point){
  bool nalazise(false);
  while(!Q.empty() && !nalazise){
    vector<double> pom;
    pom=Q.top();
    Q.pop();
    if(pom==point)nalazise=true;
  }    
  return nalazise;
}

void napuniQ(vector<segment> &S, priority_queue<vector<double>,vector <vector<double> >,prioritet> &Q){
  for(int i=0; i<S.size(); i++){
    (S[i]).asign_name(i+1);
    vector<double> rub;
    rub=(S[i]).left();
    if(!uQ(Q,rub))Q.push(rub);
    rub=(S[i]).right();
    if(!uQ(Q,rub))Q.push(rub);
  }  
}  

vector< vector<double> > sweep_line(vector <segment> &S){
  vector <vector<double> > P;
  priority_queue <vector<double>,vector <vector<double> >,prioritet> Q;
  napuniQ(S,Q);
  set <segment,kriterij_xXYy> T;
  set <segment,kriterij_xXYy> :: iterator Ti;
  while(!Q.empty()){
    vector <double> point;
    point=Q.top();
    Q.pop();
    vector <segment> Lr,Tr,Dr;
    for(int i=0; i<S.size(); i++){
      int pt=(S[i]).position(point);
      if(pt==0)Lr.push_back(S[i]);
      else if(pt==2)Dr.push_back(S[i]);
      else if(pt==1)Tr.push_back(S[i]);  
    }
    sort(Lr.begin(),Lr.end(),kriterij_yY);
    sort(Dr.begin(),Dr.end(),kriterij_yY);
    sort(Tr.begin(),Tr.end(),kriterij_yY);
    
    vector <segment> lUt,lUtUd,dUt; 
    merge(Lr.begin(),Lr.end(),Tr.begin(),Tr.end(),inserter(lUt,lUt.begin()),kriterij_yY);
    merge(lUt.begin(),lUt.end(),Dr.begin(),Dr.end(),inserter(lUtUd,lUtUd.begin()),kriterij_yY);
    merge(Dr.begin(),Dr.end(),Tr.begin(),Tr.end(),inserter(dUt,dUt.begin()),kriterij_yY);

  if(lUtUd.size()>1){
      P.push_back(point); // ---> intersection detected
    } 
    vector <vector<int> > za_azuriranje;
    vector <int> az;
    int rbr_s, rbr_pr, rbr_slj;
    for(int i=0; i<dUt.size(); i++){
      Ti=T.lower_bound(dUt[i]);
      segment pom;
      if(Ti!=T.begin()){
        Ti--;
        pom=(*Ti);
        rbr_pr=pom.return_name();
      } 
      else rbr_pr=0;
      Ti=T.upper_bound(dUt[i]);
      if(Ti!=T.end()){
        pom=(*Ti);
        rbr_slj=pom.return_name();
      }    
      else rbr_slj=0; 
      az.push_back(rbr_pr);
      az.push_back(rbr_slj);
      za_azuriranje.push_back(az);
    } 
    
    vector <vector <int> > azuriraj;
    for(int i=0; i<dUt.size(); i++){     
      try {
        int ob;
        ob=T.erase(dUt[i]);
        if(ob && za_azuriranje[i][0] && za_azuriranje[i][1])
          azuriraj.push_back(za_azuriranje[i]);
      }
      catch(...){}    
    }    
   
    for(int i=0; i<lUt.size(); i++){T.insert(lUt[i]);}    
    vector <double> pres;
    if(lUt.empty()){
      for(int i=0; i<azuriraj.size(); i++){ 
        pres=intersect(vrati_segment(S,azuriraj[i][0]),vrati_segment(S,azuriraj[i][1]));
        if(pres[2] && (pres[0]>point[0] || (pres[0]==point[0] && pres[1]<point[1]))){
          pres.pop_back();
          if(!(uQ(Q,pres)))Q.push(pres);
        } //endIF   
      }  //endFOR   
    }  //endIF
    else{
      segment preth,sljedb;
      for(int i=0; i<lUt.size(); i++){
        if(T.lower_bound(lUt[i])!=T.begin()){
          Ti=T.lower_bound(lUt[i]);
          Ti--;
          preth=(*Ti);
          pres=intersect(preth,lUt[i]);
          if(pres[2] && (pres[0]>point[0] || (pres[0]==point[0] && pres[1]<point[1]))){
            pres.pop_back();
            if(!(uQ(Q,pres)))Q.push(pres);
          }   //endIF    
        }    //endIF
        if(T.upper_bound(lUt[i])!=T.end()){
          Ti=T.upper_bound(lUt[i]);
          sljedb=(*Ti);
          pres=intersect(lUt[i],sljedb);
          if(pres[2] && (pres[0]>point[0] || (pres[0]==point[0] && pres[1]<point[1]))){
            pres.pop_back();
            if(!(uQ(Q,pres)))Q.push(pres);
          } //endIF    
        }  //endIF
      }   //endFOR 
    }    //endELSE
  }  //endWHILE
  return P;
}
Last edited on
Isn't there anyone who was working on this problem out there? I could really use some insight in what is going wrong here.
Topic archived. No new replies allowed.