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 ,
@@ -27,7 +28,7 @@ class Channel {
2728 portalRight = portals [ 0 ] . right ;
2829
2930 // Add start point.
30- pts . push ( portalApex ) ;
31+ string . push ( { index : 0 , point : portalApex } ) ;
3132
3233 for ( let i = 1 ; i < portals . length ; i ++ ) {
3334 const left = portals [ i ] . left ;
@@ -41,7 +42,7 @@ class Channel {
4142 rightIndex = i ;
4243 } else {
4344 // Right over left, insert left to path and restart scan from portal left point.
44- pts . push ( portalLeft ) ;
45+ string . push ( { index : leftIndex , point : portalLeft } ) ;
4546 // Make current left the new apex.
4647 portalApex = portalLeft ;
4748 apexIndex = leftIndex ;
@@ -64,7 +65,7 @@ class Channel {
6465 leftIndex = i ;
6566 } else {
6667 // Left over right, insert right to path and restart scan from portal right point.
67- pts . push ( portalRight ) ;
68+ string . push ( { index : rightIndex , point : portalRight } ) ;
6869 // Make current right the new apex.
6970 portalApex = portalRight ;
7071 apexIndex = rightIndex ;
@@ -80,6 +81,26 @@ class Channel {
8081 }
8182 }
8283
84+ //Project string on the portals
85+ for ( let i = 1 ; i < string . length ; i ++ ) {
86+ for ( let j = string [ i - 1 ] [ "index" ] ; j < string [ i ] [ "index" ] ; j ++ ) {
87+ const a = string [ i - 1 ] [ "point" ] ;
88+ const b = string [ i ] [ "point" ] ;
89+ const c = portals [ j ] . left ;
90+ const d = portals [ j ] . right ;
91+
92+ if ( Utils . vequal ( c , d ) ) {
93+ pts . push ( c ) ;
94+ continue ;
95+ }
96+
97+ const project = Utils . p4intersect ( a , b , c , d ) ;
98+
99+ if ( project )
100+ pts . push ( project ) ;
101+ }
102+ }
103+
83104 if ( ( pts . length === 0 ) || ( ! Utils . vequal ( pts [ pts . length - 1 ] , portals [ portals . length - 1 ] . left ) ) ) {
84105 // Append last point to path.
85106 pts . push ( portals [ portals . length - 1 ] . left ) ;
0 commit comments