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,27 @@ class Channel {
8081 }
8182 }
8283
84+ //Project string on the portals
85+ if ( string . length == 1 ) pts . push ( string [ 0 ] [ "point" ] ) ;
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