44 */
55
66import { describe , expect , test , vi } from "vitest" ;
7- import { Grid , GridEdge , GridVertex , SortedCorners } from "./Grid" ;
8- import { PlaceholderTile , Tile , TileType } from "./Tile" ;
7+ import { Grid } from "./Grid" ;
8+ import { GridEdge } from "./GridEdge" ;
9+ import { GridVertex } from "./GridVertex" ;
910import { Polygon } from "../geom/Polygon" ;
1011import { GridEventType } from "./GridEvent" ;
1112import { mergeBBox , P , weightedSumPoint } from "../geom/math" ;
@@ -17,135 +18,6 @@ import { SnubSquareSourceGrid } from "./source/SnubSquareSourceGrid";
1718
1819const TRIANGLE = TrianglesAtlas . shapes [ 0 ] ;
1920
20- describe ( "SortedCorners" , ( ) => {
21- test ( "can be created" , ( ) => {
22- const sortedCorners = new SortedCorners ( ) ;
23- expect ( sortedCorners . length ) . toBe ( 0 ) ;
24- } ) ;
25-
26- // define three triangles around (0, 0)
27- const tile1 = new Tile (
28- TRIANGLE ,
29- TRIANGLE . constructPolygonAB ( P ( 0 , 0 ) , P ( 0 , 1 ) , 0 ) ,
30- ) ;
31- const tile2 = new Tile (
32- TRIANGLE ,
33- TRIANGLE . constructPolygonEdge ( tile1 . polygon . outsideEdges [ 0 ] , 0 ) ,
34- ) ;
35- const tile3 = new Tile (
36- TRIANGLE ,
37- TRIANGLE . constructPolygonEdge ( tile2 . polygon . outsideEdges [ 1 ] , 0 ) ,
38- ) ;
39- test . each ( [
40- [
41- [
42- [ tile1 , 0 ] ,
43- [ tile2 , 1 ] ,
44- [ tile3 , 1 ] ,
45- ] ,
46- [ tile2 , tile1 , tile3 ] ,
47- ] ,
48- [
49- [
50- [ tile2 , 1 ] ,
51- [ tile3 , 1 ] ,
52- [ tile1 , 0 ] ,
53- ] ,
54- [ tile2 , tile1 , tile3 ] ,
55- ] ,
56- ] ) ( "can add and remove tiles" , ( input , output ) => {
57- const sortedCorners = new SortedCorners ( ) ;
58- expect ( sortedCorners . length ) . toBe ( 0 ) ;
59- sortedCorners . addTile ( input [ 0 ] [ 0 ] as Tile , input [ 0 ] [ 1 ] as number ) ;
60- expect ( sortedCorners . length ) . toBe ( 1 ) ;
61- sortedCorners . addTile ( input [ 1 ] [ 0 ] as Tile , input [ 1 ] [ 1 ] as number ) ;
62- expect ( sortedCorners . length ) . toBe ( 2 ) ;
63- sortedCorners . addTile ( input [ 2 ] [ 0 ] as Tile , input [ 2 ] [ 1 ] as number ) ;
64- expect ( sortedCorners . length ) . toBe ( 3 ) ;
65- expect ( [ ...sortedCorners . map ( ( c ) => c . tile . centroid ) ] ) . toStrictEqual (
66- output . map ( ( c ) => c . centroid ) ,
67- ) ;
68- sortedCorners . removeTile ( tile2 ) ;
69- expect ( sortedCorners . length ) . toBe ( 2 ) ;
70- expect ( [ ...sortedCorners . map ( ( c ) => c . tile . centroid ) ] ) . toStrictEqual ( [
71- tile1 . centroid ,
72- tile3 . centroid ,
73- ] ) ;
74- } ) ;
75-
76- test ( "can get tiles" , ( ) => {
77- const sortedCorners = new SortedCorners ( ) ;
78- sortedCorners . addTile ( tile1 , 0 ) ;
79- sortedCorners . addTile ( tile2 , 1 ) ;
80- sortedCorners . addTile ( tile3 , 1 ) ;
81- expect ( sortedCorners . tiles ) . toContain ( tile1 ) ;
82- expect ( sortedCorners . tiles ) . toContain ( tile2 ) ;
83- expect ( sortedCorners . tiles ) . toContain ( tile3 ) ;
84- } ) ;
85-
86- test ( "can be cloned" , ( ) => {
87- const sortedCorners = new SortedCorners ( ) ;
88- sortedCorners . addTile ( tile1 , 0 ) ;
89- const clone = sortedCorners . clone ( ) ;
90- expect ( sortedCorners [ 0 ] === clone [ 0 ] ) . toBe ( true ) ;
91- } ) ;
92-
93- test ( "can compute the next corner" , ( ) => {
94- const sortedCorners = new SortedCorners ( ) ;
95- expect ( sortedCorners . findNextCorner ( tile1 ) ) . toBeUndefined ( ) ;
96- sortedCorners . addTile ( tile1 , 0 ) ;
97- expect ( sortedCorners . findNextCorner ( tile1 ) ) . toBeUndefined ( ) ;
98- expect ( sortedCorners . findNextCorner ( tile2 ) ) . toBeUndefined ( ) ;
99- sortedCorners . addTile ( tile2 , 1 ) ;
100- expect ( sortedCorners . findNextCorner ( tile1 ) ! . tile ) . toBe ( tile2 ) ;
101- expect ( sortedCorners . findNextCorner ( tile2 ) ! . tile ) . toBe ( tile1 ) ;
102- expect ( sortedCorners . findNextCorner ( tile3 ) ) . toBeUndefined ( ) ;
103- sortedCorners . addTile ( tile3 , 1 ) ;
104- expect ( sortedCorners . findNextCorner ( tile1 ) ! . tile ) . toBe ( tile3 ) ;
105- expect ( sortedCorners . findNextCorner ( tile3 ) ! . tile ) . toBe ( tile2 ) ;
106- expect ( sortedCorners . findNextCorner ( tile2 ) ! . tile ) . toBe ( tile1 ) ;
107- } ) ;
108- } ) ;
109-
110- describe ( "GridVertex" , ( ) => {
111- test ( "can be constructed" , ( ) => {
112- const point = P ( 0 , 2 ) ;
113- const vertex = new GridVertex ( point ) ;
114- expect ( vertex . point ) . toBe ( point ) ;
115- expect ( vertex . corners . length ) . toBe ( 0 ) ;
116- expect ( vertex . tiles . length ) . toBe ( 0 ) ;
117- } ) ;
118-
119- test ( "can add and remove a tile" , ( ) => {
120- const tile = new Tile (
121- TRIANGLE ,
122- TRIANGLE . constructPolygonAB ( P ( 0 , 0 ) , P ( 0 , 1 ) , 0 ) ,
123- ) ;
124- const point = P ( 0 , 1 ) ;
125- const vertex = new GridVertex ( point ) ;
126- vertex . addTile ( tile , 1 ) ;
127- expect ( vertex . tiles ) . toEqual ( [ tile ] ) ;
128- expect ( vertex . corners [ 0 ] . tile ) . toBe ( tile ) ;
129-
130- vertex . removeTile ( tile ) ;
131- expect ( vertex . tiles ) . toEqual ( [ ] ) ;
132- } ) ;
133-
134- test ( "can add and remove a placeholder" , ( ) => {
135- const placeholder = new PlaceholderTile (
136- TRIANGLE ,
137- TRIANGLE . constructPolygonAB ( P ( 0 , 0 ) , P ( 0 , 1 ) , 0 ) ,
138- ) ;
139- const point = P ( 0 , 1 ) ;
140- const vertex = new GridVertex ( point ) ;
141- vertex . addTile ( placeholder , 1 ) ;
142- expect ( [ ...vertex . placeholders ] ) . toStrictEqual ( [ placeholder ] ) ;
143-
144- vertex . removeTile ( placeholder ) ;
145- expect ( vertex . placeholders . size ) . toBe ( 0 ) ;
146- } ) ;
147- } ) ;
148-
14921describe ( "GridEdge" , ( ) => {
15022 const a = new GridVertex ( P ( 0 , 0 ) ) ;
15123 const b = new GridVertex ( P ( 0 , 1 ) ) ;
0 commit comments