@@ -13,29 +13,42 @@ import { Shape } from 'three';
1313import { BuildingShapeUtils } from '../src/extras/BuildingShapeUtils.js' ;
1414// import { JSDOM } from 'jsdom';
1515
16+
17+ /** Test createShape */
18+ // test('', () => {
19+ //
20+ // });
21+
22+ /** Test isClosed */
1623test ( 'Test Closed Way' , ( ) => {
1724 var way = '<way id="1"><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="5"/><nd ref="2"/></way>' ;
1825 let parser = new window . DOMParser ( ) ;
1926 let xmlData = parser . parseFromString ( way , 'text/xml' ) ;
2027 expect ( BuildingShapeUtils . isClosed ( xmlData ) ) . toBe ( true ) ;
2128} ) ;
2229
23- test ( 'Reverse way' , ( ) => {
24- var way1 = '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>' ;
25- var way2 = '<way id="1"><nd ref="3"/><nd ref="2"/><nd ref="1"/></way>' ;
26- let parser = new window . DOMParser ( ) ;
27- let xml1 = parser . parseFromString ( way1 , 'text/xml' ) . getElementsByTagName ( 'way' ) [ 0 ] ;
28- let result = BuildingShapeUtils . reverseWay ( xml1 ) ;
29- expect ( result . outerHTML ) . toBe ( way2 ) ;
30- } ) ;
31-
3230test ( 'Test Open Way' , ( ) => {
3331 var way = '<way id="1"><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="5"/><nd ref="6"/></way>' ;
3432 let parser = new window . DOMParser ( ) ;
3533 let xmlData = parser . parseFromString ( way , 'text/xml' ) ;
3634 expect ( BuildingShapeUtils . isClosed ( xmlData ) ) . toBe ( false ) ;
3735} ) ;
3836
37+ /** Test isSelfIntersecting */
38+ describe . each ( [
39+ [ '<way id="1"><nd ref="1"/><nd ref="2"/></way>' , false , 'open non-intersecting' ] ,
40+ [ '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="1"/></way>' , false , 'closed non-intersecting' ] ,
41+ [ '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="2"/></way>' , true , 'open intersecting' ] ,
42+ [ '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="3"/><nd ref="1"/></way>' , true , 'closed intersecting' ] ,
43+ ] ) ( 'isSelfIntersecting' , ( way , expected , description ) => {
44+ test ( `${ description } ` , ( ) => {
45+ let parser = new window . DOMParser ( ) ;
46+ let xml = parser . parseFromString ( way , 'text/xml' ) . getElementsByTagName ( 'way' ) [ 0 ] ;
47+ expect ( BuildingShapeUtils . isSelfIntersecting ( xml ) ) . toBe ( expected ) ;
48+ } ) ;
49+ } ) ;
50+
51+ /** Test joinWays */
3952test ( 'Test joining 2 ways' , ( ) => {
4053 var way1 = '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>' ;
4154 var way2 = '<way id="2"><nd ref="3"/><nd ref="4"/><nd ref="1"/></way>' ;
@@ -47,6 +60,44 @@ test('Test joining 2 ways', () => {
4760 expect ( result . outerHTML ) . toBe ( way3 ) ;
4861} ) ;
4962
63+ /** Test joinAllWays */
64+ test ( 'Test joining 2 ways' , ( ) => {
65+ var way1 = '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>' ;
66+ var way2 = '<way id="2"><nd ref="3"/><nd ref="4"/><nd ref="1"/></way>' ;
67+ var way3 = '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="1"/></way>' ;
68+ let parser = new window . DOMParser ( ) ;
69+ let xml1 = parser . parseFromString ( way1 , 'text/xml' ) . getElementsByTagName ( 'way' ) [ 0 ] ;
70+ let xml2 = parser . parseFromString ( way2 , 'text/xml' ) . getElementsByTagName ( 'way' ) [ 0 ] ;
71+ let result = BuildingShapeUtils . joinAllWays ( [ xml1 , xml2 ] ) ;
72+ expect ( result . outerHTML ) . toBe ( way3 ) ;
73+ } ) ;
74+
75+ /** Test reverseWay */
76+ test ( 'Reverse way' , ( ) => {
77+ var way1 = '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/></way>' ;
78+ var way2 = '<way id="1"><nd ref="3"/><nd ref="2"/><nd ref="1"/></way>' ;
79+ let parser = new window . DOMParser ( ) ;
80+ let xml1 = parser . parseFromString ( way1 , 'text/xml' ) . getElementsByTagName ( 'way' ) [ 0 ] ;
81+ let result = BuildingShapeUtils . reverseWay ( xml1 ) ;
82+ expect ( result . outerHTML ) . toBe ( way2 ) ;
83+ } ) ;
84+
85+ /** Test center */
86+ test ( 'Center' , ( ) => {
87+ expect ( BuildingShapeUtils . center ( rightTriangle ) ) . toStrictEqual ( [ 0 , 0 ] ) ;
88+ } ) ;
89+
90+ /** Test getWidth */
91+ test ( 'Get Width' , ( ) => {
92+ expect ( BuildingShapeUtils . getWidth ( rightTriangle ) ) . toBe ( 2 ) ;
93+ } ) ;
94+
95+ /** Test combineCoordinates */
96+ test ( 'Combine Coordinates' , ( ) => {
97+ expect ( BuildingShapeUtils . combineCoordinates ( rightTriangle ) ) . toStrictEqual ( [ [ 1 , 1 , - 1 ] , [ 1 , - 1 , 1 ] ] ) ;
98+ } ) ;
99+
100+ /** Test extents */
50101const rightTriangle = new Shape ( ) ;
51102rightTriangle . moveTo ( 1 , 1 ) ;
52103rightTriangle . lineTo ( 1 , - 1 ) ;
@@ -67,51 +118,31 @@ test('Extents Rotation', () => {
67118 expect ( BuildingShapeUtils . extents ( rightTriangle , angle ) ) . toBeDeepCloseTo ( [ - sqrt2 , 0 , sqrt2 , sqrt2 ] , 10 ) ;
68119} ) ;
69120
121+ /** Test edgeLength */
70122test ( 'Edge Lengths' , ( ) => {
71123 expect ( BuildingShapeUtils . edgeLength ( rightTriangle ) ) . toBeDeepCloseTo ( [ 2 , Math . sqrt ( 2 ) * 2 , 2 ] ) ;
72124} ) ;
73125
74- test ( 'Edge Direction' , ( ) => {
75- expect ( BuildingShapeUtils . edgeDirection ( rightTriangle ) ) . toBeDeepCloseTo ( [ - Math . PI / 2 , 3 * Math . PI / 4 , 0 ] ) ;
76- } ) ;
77-
78- test ( 'Edge Direction2' , ( ) => {
79- expect ( BuildingShapeUtils . edgeDirection ( rightTriangle2 ) ) . toBeDeepCloseTo ( [ - Math . PI , - Math . PI / 4 , Math . PI / 2 ] ) ;
126+ /** Test vertexAngle */
127+ test ( 'Vertex Angles' , ( ) => {
128+ expect ( BuildingShapeUtils . vertexAngle ( rightTriangle ) ) . toStrictEqual ( [ Math . PI / 2 , Math . PI / 4 , Math . PI / 4 ] ) ;
80129} ) ;
81130
82- test ( 'Longest side angle ' , ( ) => {
83- expect ( BuildingShapeUtils . longestSideAngle ( rightTriangle ) ) . toBe ( 3 * Math . PI / 4 ) ;
131+ test ( 'Vertex Angles counterclockwise ' , ( ) => {
132+ expect ( BuildingShapeUtils . vertexAngle ( rightTriangle2 ) ) . toStrictEqual ( [ - Math . PI / 2 , - Math . PI / 4 , - Math . PI / 4 ] ) ;
84133} ) ;
85134
135+ /** Test edgeDirection */
86136describe . each ( [
87- [ '<way id="1"><nd ref="1"/><nd ref="2"/></way>' , false , 'open non-intersecting' ] ,
88- [ '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="1"/></way>' , false , 'closed non-intersecting' ] ,
89- [ '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="2"/></way>' , true , 'open intersecting' ] ,
90- [ '<way id="1"><nd ref="1"/><nd ref="2"/><nd ref="3"/><nd ref="4"/><nd ref="3"/><nd ref="1"/></way>' , true , 'closed intersecting' ] ,
91- ] ) ( 'isSelfIntersecting' , ( way , expected , description ) => {
137+ [ rightTriangle , [ - Math . PI / 2 , 3 * Math . PI / 4 , 0 ] , 'CW' ] ,
138+ [ rightTriangle2 , [ - Math . PI , - Math . PI / 4 , Math . PI / 2 ] , 'CCW' ] ,
139+ ] ) ( 'Edge Direction' , ( shape , expected , description ) => {
92140 test ( `${ description } ` , ( ) => {
93- let parser = new window . DOMParser ( ) ;
94- let xml = parser . parseFromString ( way , 'text/xml' ) . getElementsByTagName ( 'way' ) [ 0 ] ;
95- expect ( BuildingShapeUtils . isSelfIntersecting ( xml ) ) . toBe ( expected ) ;
141+ expect ( BuildingShapeUtils . edgeDirection ( shape ) ) . toBeDeepCloseTo ( expected ) ;
96142 } ) ;
97143} ) ;
98144
99- test ( 'Center' , ( ) => {
100- expect ( BuildingShapeUtils . center ( rightTriangle ) ) . toStrictEqual ( [ 0 , 0 ] ) ;
101- } ) ;
102-
103- test ( 'Get Width' , ( ) => {
104- expect ( BuildingShapeUtils . getWidth ( rightTriangle ) ) . toBe ( 2 ) ;
105- } ) ;
106-
107- test ( 'Vertex Angles' , ( ) => {
108- expect ( BuildingShapeUtils . vertexAngle ( rightTriangle ) ) . toStrictEqual ( [ Math . PI / 2 , Math . PI / 4 , Math . PI / 4 ] ) ;
109- } ) ;
110-
111- test ( 'Vertex Angles counterclockwise' , ( ) => {
112- expect ( BuildingShapeUtils . vertexAngle ( rightTriangle2 ) ) . toStrictEqual ( [ - Math . PI / 2 , - Math . PI / 4 , - Math . PI / 4 ] ) ;
113- } ) ;
114-
145+ /** Test surrounds */
115146describe . each ( [
116147 [ [ - 1 , - 1 ] , false , 'Outside' ] ,
117148 [ [ 1 , 1 ] , true , 'Share Node' ] ,
@@ -123,7 +154,20 @@ describe.each([
123154 } ) ;
124155} ) ;
125156
157+ /** Test calculateRadius */
126158test ( 'Calculate Radius' , ( ) => {
127159 expect ( BuildingShapeUtils . calculateRadius ( rightTriangle ) ) . toBe ( 1 ) ;
128160} ) ;
129161
162+ /** Test longestSideAngle */
163+ test ( 'Longest side angle' , ( ) => {
164+ expect ( BuildingShapeUtils . longestSideAngle ( rightTriangle ) ) . toBe ( 3 * Math . PI / 4 ) ;
165+ } ) ;
166+
167+ /** Test repositionPoint */
168+ test ( 'Reposition Point' , ( ) => {
169+ const point = [ 11.0155721 , 49.583313 ] ;
170+ const home = [ 11.015512 , 49.5833659 ] ;
171+ const expected = [ 4.332747472234555 , - 5.882209888874915 ] ;
172+ expect ( BuildingShapeUtils . repositionPoint ( point , home ) ) . toStrictEqual ( expected ) ;
173+ } ) ;
0 commit comments