|
| 1 | +import test from 'tape'; |
| 2 | +import vtkPLYReader from 'vtk.js/Sources/IO/Geometry/PLYReader'; |
| 3 | + |
| 4 | +const plyFile = `ply |
| 5 | +format ascii 1.0 |
| 6 | +comment Created by vtk.js with normals and UVs |
| 7 | +element vertex 8 |
| 8 | +property float x |
| 9 | +property float y |
| 10 | +property float z |
| 11 | +property uchar red |
| 12 | +property uchar green |
| 13 | +property uchar blue |
| 14 | +property float nx |
| 15 | +property float ny |
| 16 | +property float nz |
| 17 | +property float u |
| 18 | +property float v |
| 19 | +element face 6 |
| 20 | +property list uchar uint vertex_indices |
| 21 | +end_header |
| 22 | +2.000000 0.000000 -2.000000 255 255 0 0.577350 -0.577350 -0.577350 1.0 0.0 |
| 23 | +2.000000 0.000000 0.000000 255 0 0 0.577350 -0.577350 0.577350 1.0 0.0 |
| 24 | +0.000000 0.000000 0.000000 0 0 0 -0.577350 -0.577350 0.577350 0.0 0.0 |
| 25 | +0.000000 0.000000 -2.000000 0 255 0 -0.577350 -0.577350 -0.577350 0.0 0.0 |
| 26 | +2.000000 2.000000 -2.000000 255 255 255 0.577350 0.577350 -0.577350 1.0 1.0 |
| 27 | +0.000000 2.000000 -2.000000 0 255 255 -0.577350 0.577350 -0.577350 0.0 1.0 |
| 28 | +0.000000 2.000000 0.000000 0 0 255 -0.577350 0.577350 0.577350 0.0 1.0 |
| 29 | +2.000000 2.000000 0.000000 255 0 255 0.577350 0.577350 0.577350 1.0 1.0 |
| 30 | +4 0 1 2 3 |
| 31 | +4 4 5 6 7 |
| 32 | +4 0 4 7 1 |
| 33 | +4 1 7 6 2 |
| 34 | +4 2 6 5 3 |
| 35 | +4 4 0 3 5`; |
| 36 | + |
| 37 | +const pointCloudPLY = `ply |
| 38 | +format ascii 1.0 |
| 39 | +element vertex 4 |
| 40 | +property float x |
| 41 | +property float y |
| 42 | +property float z |
| 43 | +end_header |
| 44 | +0 0 0 |
| 45 | +1 1 1 |
| 46 | +2 2 2 |
| 47 | +3 3 3`; |
| 48 | + |
| 49 | +test('PLYReader: Parse ASCII PLY file', (t) => { |
| 50 | + const plyReader = vtkPLYReader.newInstance({ |
| 51 | + duplicatePointsForFaceTexture: false, |
| 52 | + }); |
| 53 | + plyReader.parseAsText(plyFile); |
| 54 | + const output = plyReader.getOutputData(); |
| 55 | + t.equal(output.getNumberOfPoints(), 8, 'Should parse 8 vertices'); |
| 56 | + t.deepEqual( |
| 57 | + output.getPoints().getData(), |
| 58 | + new Float32Array([ |
| 59 | + 2, 0, -2, 2, 0, 0, 0, 0, 0, 0, 0, -2, 2, 2, -2, 0, 2, -2, 0, 2, 0, 2, 2, |
| 60 | + 0, |
| 61 | + ]), |
| 62 | + 'Should parse vertex positions correctly' |
| 63 | + ); |
| 64 | + t.deepEqual( |
| 65 | + output.getPolys().getData(), |
| 66 | + new Uint32Array([ |
| 67 | + 4, 0, 1, 2, 3, 4, 4, 5, 6, 7, 4, 0, 4, 7, 1, 4, 1, 7, 6, 2, 4, 2, 6, 5, 3, |
| 68 | + 4, 4, 0, 3, 5, |
| 69 | + ]), |
| 70 | + 'Should parse face indices correctly' |
| 71 | + ); |
| 72 | + t.end(); |
| 73 | +}); |
| 74 | + |
| 75 | +test('PLYReader: Parse Point Cloud PLY file', (t) => { |
| 76 | + const plyReader = vtkPLYReader.newInstance({ |
| 77 | + duplicatePointsForFaceTexture: false, |
| 78 | + }); |
| 79 | + plyReader.parseAsText(pointCloudPLY); |
| 80 | + const output = plyReader.getOutputData(); |
| 81 | + t.equal(output.getNumberOfPoints(), 4, 'Should parse 4 vertices'); |
| 82 | + t.deepEqual( |
| 83 | + output.getPoints().getData(), |
| 84 | + new Float32Array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3]), |
| 85 | + 'Should parse vertex positions correctly' |
| 86 | + ); |
| 87 | + t.end(); |
| 88 | +}); |
| 89 | + |
| 90 | +test('PLYReader: Parse PLY with color', (t) => { |
| 91 | + const plyReader = vtkPLYReader.newInstance({ |
| 92 | + duplicatePointsForFaceTexture: false, |
| 93 | + }); |
| 94 | + plyReader.parseAsText(plyFile); |
| 95 | + const output = plyReader.getOutputData(); |
| 96 | + t.deepEqual( |
| 97 | + output.getPointData().getScalars().getData(), |
| 98 | + new Uint8Array([ |
| 99 | + 255, 255, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 255, 255, 255, 0, 255, 255, 0, |
| 100 | + 0, 255, 255, 0, 255, |
| 101 | + ]), |
| 102 | + 'Should parse vertex colors correctly' |
| 103 | + ); |
| 104 | + t.end(); |
| 105 | +}); |
| 106 | + |
| 107 | +test('PLYReader: Parse PLY with normals', (t) => { |
| 108 | + const plyReader = vtkPLYReader.newInstance({ |
| 109 | + duplicatePointsForFaceTexture: false, |
| 110 | + }); |
| 111 | + plyReader.parseAsText(plyFile); |
| 112 | + const output = plyReader.getOutputData(); |
| 113 | + t.deepEqual( |
| 114 | + output.getPointData().getNormals().getData(), |
| 115 | + new Float32Array([ |
| 116 | + 0.5773500204086304, -0.5773500204086304, -0.5773500204086304, |
| 117 | + 0.5773500204086304, -0.5773500204086304, 0.5773500204086304, |
| 118 | + -0.5773500204086304, -0.5773500204086304, 0.5773500204086304, |
| 119 | + -0.5773500204086304, -0.5773500204086304, -0.5773500204086304, |
| 120 | + 0.5773500204086304, 0.5773500204086304, -0.5773500204086304, |
| 121 | + -0.5773500204086304, 0.0, -0.5773500204086304, 0.0, 0.0, |
| 122 | + 0.5773500204086304, 0.0, 0.0, 0.5773500204086304, |
| 123 | + ]), |
| 124 | + 'Should parse vertex normals correctly' |
| 125 | + ); |
| 126 | + t.end(); |
| 127 | +}); |
| 128 | + |
| 129 | +test('PLYReader: Parse PLY with texture coordinates', (t) => { |
| 130 | + const plyReader = vtkPLYReader.newInstance(); |
| 131 | + plyReader.parse(plyFile); |
| 132 | + const output = plyReader.getOutputData(); |
| 133 | + t.deepEqual( |
| 134 | + output.getPointData().getTCoords().getData(), |
| 135 | + new Float32Array([1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1]), |
| 136 | + 'Should parse texture coordinates correctly' |
| 137 | + ); |
| 138 | + t.end(); |
| 139 | +}); |
0 commit comments