11import { Utils } from './Utils.js' ;
22
33class Channel {
4- constructor ( ) {
4+ constructor ( ) {
55 this . portals = [ ] ;
66 }
77
8- push ( p1 , p2 ) {
8+ push ( p1 , p2 ) {
99 if ( p2 === undefined ) p2 = p1 ;
1010 this . portals . push ( {
1111 left : p1 ,
1212 right : p2
1313 } ) ;
1414 }
1515
16- stringPull ( ) {
16+ stringPull ( ) {
1717 const portals = this . portals ;
1818 const pts = [ ] ;
19+ const string = [ ] ;
1920 // Init scan state
2021 let portalApex , portalLeft , portalRight ;
2122 let apexIndex = 0 ,
@@ -28,6 +29,7 @@ class Channel {
2829
2930 // Add start point.
3031 pts . push ( portalApex ) ;
32+ string . push ( { index : 0 , point : portalApex } ) ;
3133
3234 for ( let i = 1 ; i < portals . length ; i ++ ) {
3335 const left = portals [ i ] . left ;
@@ -41,7 +43,7 @@ class Channel {
4143 rightIndex = i ;
4244 } else {
4345 // Right over left, insert left to path and restart scan from portal left point.
44- pts . push ( portalLeft ) ;
46+ string . push ( { index : leftIndex , point : portalLeft } ) ;
4547 // Make current left the new apex.
4648 portalApex = portalLeft ;
4749 apexIndex = leftIndex ;
@@ -64,7 +66,7 @@ class Channel {
6466 leftIndex = i ;
6567 } else {
6668 // Left over right, insert right to path and restart scan from portal right point.
67- pts . push ( portalRight ) ;
69+ string . push ( { index : rightIndex , point : portalRight } ) ;
6870 // Make current right the new apex.
6971 portalApex = portalRight ;
7072 apexIndex = rightIndex ;
@@ -80,6 +82,26 @@ class Channel {
8082 }
8183 }
8284
85+ //Project string on the portals
86+ for ( let i = 1 ; i < string . length ; i ++ ) {
87+ for ( let j = string [ i - 1 ] [ "index" ] ; j < string [ i ] [ "index" ] ; j ++ ) {
88+ const a = string [ i - 1 ] [ "point" ] ;
89+ const b = string [ i ] [ "point" ] ;
90+ const c = portals [ j ] . left ;
91+ const d = portals [ j ] . right ;
92+
93+ if ( Utils . vequal ( c , d ) ) {
94+ pts . push ( c ) ;
95+ continue ;
96+ }
97+
98+ const project = Utils . p4intersect ( a , b , c , d ) ;
99+
100+ if ( project )
101+ pts . push ( project ) ;
102+ }
103+ }
104+
83105 if ( ( pts . length === 0 ) || ( ! Utils . vequal ( pts [ pts . length - 1 ] , portals [ portals . length - 1 ] . left ) ) ) {
84106 // Append last point to path.
85107 pts . push ( portals [ portals . length - 1 ] . left ) ;
0 commit comments