|
| 1 | +import test from 'tape'; |
| 2 | +import vtkMergePoints from 'vtk.js/Sources/Common/DataModel/MergePoints'; |
| 3 | +import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData'; |
| 4 | + |
| 5 | +// Test 1: Insert unique point |
| 6 | +test('vtkMergePoints: insertUniquePoint', (t) => { |
| 7 | + const mergePoints = vtkMergePoints.newInstance(); |
| 8 | + const output = vtkPolyData.newInstance(); |
| 9 | + const points = new Float32Array(0); |
| 10 | + output.getPoints().setData(points); |
| 11 | + mergePoints.setDataSet(output); |
| 12 | + mergePoints.setDivisions([10, 10, 10]); |
| 13 | + mergePoints.initPointInsertion(output.getPoints(), output.getBounds()); |
| 14 | + mergePoints.buildLocator(); |
| 15 | + |
| 16 | + const p1 = [1.0, 2.0, 3.0]; // First unique point |
| 17 | + const res1 = mergePoints.insertUniquePoint(p1); |
| 18 | + t.ok(res1.inserted, 'First insertion should be successful'); |
| 19 | + t.equal(res1.id, 0, 'First point ID should be 0'); |
| 20 | + |
| 21 | + const p2 = [1.0, 2.0, 3.0]; // Duplicate point |
| 22 | + const res2 = mergePoints.insertUniquePoint(p2); |
| 23 | + t.notOk(res2.inserted, 'Second insertion should not insert duplicate'); |
| 24 | + t.equal(res2.id, 0, 'Should return same ID for duplicate point'); |
| 25 | + |
| 26 | + const p3 = [2.0, 3.0, 4.0]; // Different point |
| 27 | + const res3 = mergePoints.insertUniquePoint(p3); |
| 28 | + t.ok(res3.inserted, 'Different point should insert successfully'); |
| 29 | + t.equal(res3.id, 1, 'Should return next ID'); |
| 30 | + |
| 31 | + const p4 = [5.0, 6.0, 7.0]; // Different point |
| 32 | + const res4 = mergePoints.insertUniquePoint(p4); |
| 33 | + t.ok(res4.inserted, 'Different point should insert successfully'); |
| 34 | + t.equal(res4.id, 2, 'Should return next ID'); |
| 35 | + t.equal( |
| 36 | + mergePoints.getPoints().getNumberOfPoints(), |
| 37 | + 3, |
| 38 | + 'Three unique points inserted' |
| 39 | + ); |
| 40 | + |
| 41 | + t.end(); |
| 42 | +}); |
| 43 | + |
| 44 | +// Test 2: isInsertedPoint |
| 45 | +test('vtkMergePoints: isInsertedPoint', (t) => { |
| 46 | + const mergePoints = vtkMergePoints.newInstance(); |
| 47 | + const output = vtkPolyData.newInstance(); |
| 48 | + const points = new Float32Array(0); |
| 49 | + output.getPoints().setData(points); |
| 50 | + mergePoints.setDataSet(output); |
| 51 | + mergePoints.setDivisions([10, 10, 10]); |
| 52 | + mergePoints.initPointInsertion(output.getPoints(), output.getBounds()); |
| 53 | + mergePoints.buildLocator(); |
| 54 | + |
| 55 | + t.equal( |
| 56 | + mergePoints.isInsertedPoint([0, 0, 0]), |
| 57 | + -1, |
| 58 | + 'Non-inserted point returns -1' |
| 59 | + ); |
| 60 | + |
| 61 | + const res = mergePoints.insertUniquePoint([4.0, 5.0, 6.0]); |
| 62 | + t.equal( |
| 63 | + mergePoints.isInsertedPoint([4.0, 5.0, 6.0]), |
| 64 | + res.id, |
| 65 | + 'Should return correct ID for inserted point' |
| 66 | + ); |
| 67 | + |
| 68 | + t.end(); |
| 69 | +}); |
| 70 | + |
| 71 | +// Test 3: Duplicate insertions |
| 72 | +test('vtkMergePoints: Duplicate insertions', (t) => { |
| 73 | + const mergePoints = vtkMergePoints.newInstance(); |
| 74 | + const output = vtkPolyData.newInstance(); |
| 75 | + const points = new Float32Array(0); |
| 76 | + output.getPoints().setData(points); |
| 77 | + mergePoints.setDataSet(output); |
| 78 | + mergePoints.setDivisions([10, 10, 10]); |
| 79 | + mergePoints.initPointInsertion(output.getPoints(), output.getBounds()); |
| 80 | + mergePoints.buildLocator(); |
| 81 | + |
| 82 | + const p1 = [1.0, 2.0, 3.0]; |
| 83 | + const p2 = [1.0, 2.0, 3.0]; // Duplicate |
| 84 | + const p3 = [2.0, 3.0, 4.0]; // Unique |
| 85 | + |
| 86 | + const res1 = mergePoints.insertUniquePoint(p1); |
| 87 | + t.ok(res1.inserted, 'First insertion should succeed'); |
| 88 | + t.equal(res1.id, 0, 'First point ID should be 0'); |
| 89 | + |
| 90 | + const res2 = mergePoints.insertUniquePoint(p2); |
| 91 | + t.notOk(res2.inserted, 'Duplicate insertion should not insert again'); |
| 92 | + t.equal(res2.id, res1.id, 'Should return same ID for duplicate point'); |
| 93 | + |
| 94 | + const res3 = mergePoints.insertUniquePoint(p3); |
| 95 | + t.ok(res3.inserted, 'Unique point should insert successfully'); |
| 96 | + t.equal(res3.id, 1, 'Should return next ID for unique point'); |
| 97 | + |
| 98 | + t.end(); |
| 99 | +}); |
| 100 | + |
| 101 | +// Test 4: Fails cleanly on invalid input |
| 102 | +test('vtkMergePoints: Invalid input handling', (t) => { |
| 103 | + const mergePoints = vtkMergePoints.newInstance(); |
| 104 | + const output = vtkPolyData.newInstance(); |
| 105 | + const points = new Float32Array(0); |
| 106 | + output.getPoints().setData(points); |
| 107 | + mergePoints.setDataSet(output); |
| 108 | + mergePoints.setDivisions([10, 10, 10]); |
| 109 | + mergePoints.initPointInsertion(output.getPoints(), output.getBounds()); |
| 110 | + mergePoints.buildLocator(); |
| 111 | + |
| 112 | + const res1 = mergePoints.insertUniquePoint(null); |
| 113 | + t.notOk(res1.inserted, 'Should not insert null point'); |
| 114 | + t.equal(res1.id, -1, 'Should return -1 for null point'); |
| 115 | + |
| 116 | + const res2 = mergePoints.insertUniquePoint(undefined); |
| 117 | + t.notOk(res2.inserted, 'Should not insert undefined point'); |
| 118 | + t.equal(res2.id, -1, 'Should return -1 for undefined point'); |
| 119 | + |
| 120 | + const res3 = mergePoints.insertUniquePoint([1, 2]); // Invalid point |
| 121 | + t.notOk(res3.inserted, 'Should not insert malformed point'); |
| 122 | + t.equal(res3.id, -1, 'Should return -1 for malformed point'); |
| 123 | + |
| 124 | + t.end(); |
| 125 | +}); |
| 126 | + |
| 127 | +// Test 5: Spatial hashing with similar points |
| 128 | +test('vtkMergePoints: Bucket hashing', (t) => { |
| 129 | + const mergePoints = vtkMergePoints.newInstance(); |
| 130 | + const output = vtkPolyData.newInstance(); |
| 131 | + const points = new Float32Array(0); |
| 132 | + |
| 133 | + output.getPoints().setData(points); |
| 134 | + mergePoints.setDataSet(output); |
| 135 | + mergePoints.setDivisions([10, 10, 10]); |
| 136 | + mergePoints.initPointInsertion(output.getPoints(), output.getBounds()); |
| 137 | + mergePoints.buildLocator(); |
| 138 | + |
| 139 | + const p1 = [1.4, 2.6, 3.9]; |
| 140 | + const p2 = [1.5, 2.7, 3.1]; |
| 141 | + const p3 = [1.0, 2.0, 4.0]; |
| 142 | + const p4 = [1.0, 2.0, 4.0]; |
| 143 | + |
| 144 | + const id1 = mergePoints.insertUniquePoint(p1).id; |
| 145 | + const id2 = mergePoints.insertUniquePoint(p2).id; |
| 146 | + const id3 = mergePoints.insertUniquePoint(p3).id; |
| 147 | + const id4 = mergePoints.insertUniquePoint(p4).id; |
| 148 | + |
| 149 | + t.notEqual(id1, id2, 'Different point should return different ID'); |
| 150 | + t.equal(id3, id4, 'Similar points should return same ID'); |
| 151 | + |
| 152 | + t.end(); |
| 153 | +}); |
0 commit comments