|
| 1 | +import test from 'tape'; |
| 2 | +import testUtils from 'vtk.js/Sources/Testing/testUtils'; |
| 3 | + |
| 4 | +import 'vtk.js/Sources/Rendering/Misc/RenderingAPIs'; |
| 5 | +import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor'; |
| 6 | +import vtkConeSource from 'vtk.js/Sources/Filters/Sources/ConeSource'; |
| 7 | +import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray'; |
| 8 | +import vtkGlyph3DMapper from 'vtk.js/Sources/Rendering/Core/Glyph3DMapper'; |
| 9 | +import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper'; |
| 10 | +import vtkMath from 'vtk.js/Sources/Common/Core/Math'; |
| 11 | +import vtkPlane from 'vtk.js/Sources/Common/DataModel/Plane'; |
| 12 | +import vtkRenderWindow from 'vtk.js/Sources/Rendering/Core/RenderWindow'; |
| 13 | +import vtkRenderer from 'vtk.js/Sources/Rendering/Core/Renderer'; |
| 14 | +import vtkSphereSource from 'vtk.js/Sources/Filters/Sources/SphereSource'; |
| 15 | + |
| 16 | +import baseline from './testGlyph3DMapperClip.png'; |
| 17 | + |
| 18 | +test.onlyIfWebGL('Test vtkGlyph3DMapper Clipping', (t) => { |
| 19 | + const gc = testUtils.createGarbageCollector(); |
| 20 | + t.ok('rendering', 'vtkGlyph3DMapper Clipping'); |
| 21 | + |
| 22 | + // Create some control UI |
| 23 | + const container = document.querySelector('body'); |
| 24 | + const renderWindowContainer = gc.registerDOMElement( |
| 25 | + document.createElement('div') |
| 26 | + ); |
| 27 | + container.appendChild(renderWindowContainer); |
| 28 | + |
| 29 | + // create what we will view |
| 30 | + const renderWindow = gc.registerResource(vtkRenderWindow.newInstance()); |
| 31 | + const renderer = gc.registerResource(vtkRenderer.newInstance()); |
| 32 | + renderWindow.addRenderer(renderer); |
| 33 | + renderer.setBackground(0.32, 0.34, 0.43); |
| 34 | + |
| 35 | + const coneSource = vtkConeSource.newInstance({ |
| 36 | + resolution: 12, |
| 37 | + }); |
| 38 | + const sphereSource = vtkSphereSource.newInstance({ |
| 39 | + radius: 5.0, |
| 40 | + phiResolution: 8, |
| 41 | + thetaResolution: 8, |
| 42 | + }); |
| 43 | + sphereSource.update(); |
| 44 | + |
| 45 | + const points = sphereSource.getOutputData(); |
| 46 | + const numPoints = points.getNumberOfPoints(); |
| 47 | + |
| 48 | + // Create scalars to color the glyphs |
| 49 | + const scalars = new Float32Array(numPoints); |
| 50 | + |
| 51 | + vtkMath.randomSeed(9290391); |
| 52 | + for (let i = 0; i < numPoints; i++) { |
| 53 | + // Assign a random scalar value for coloring |
| 54 | + scalars[i] = vtkMath.random(); |
| 55 | + } |
| 56 | + |
| 57 | + // Add the scalar array to the point data |
| 58 | + points.getPointData().setScalars( |
| 59 | + vtkDataArray.newInstance({ |
| 60 | + name: 'scalars', |
| 61 | + values: scalars, |
| 62 | + }) |
| 63 | + ); |
| 64 | + const mapper = vtkGlyph3DMapper.newInstance(); |
| 65 | + const actor = vtkActor.newInstance(); |
| 66 | + |
| 67 | + mapper.setInputConnection(sphereSource.getOutputPort(), 0); |
| 68 | + |
| 69 | + mapper.setInputConnection(coneSource.getOutputPort(), 1); |
| 70 | + mapper.setOrientationArray('Normals'); |
| 71 | + mapper.setScaleFactor(2); |
| 72 | + |
| 73 | + const pdMapper = vtkMapper.newInstance(); |
| 74 | + pdMapper.setInputConnection(sphereSource.getOutputPort()); |
| 75 | + const pdActor = vtkActor.newInstance(); |
| 76 | + pdActor.setMapper(pdMapper); |
| 77 | + pdActor.getProperty().setOpacity(0.6); |
| 78 | + |
| 79 | + // clipping planes |
| 80 | + const plane = vtkPlane.newInstance({ |
| 81 | + origin: [0, 0, 0], |
| 82 | + normal: [0.5, -0.5, -0.5], |
| 83 | + }); |
| 84 | + mapper.setClippingPlanes(plane); |
| 85 | + pdMapper.setClippingPlanes(plane); |
| 86 | + |
| 87 | + actor.setMapper(mapper); |
| 88 | + renderer.addActor(actor); |
| 89 | + renderer.addActor(pdActor); |
| 90 | + renderer.resetCamera(); |
| 91 | + renderer.getActiveCamera().zoom(1.3); |
| 92 | + |
| 93 | + // now create something to view it, in this case webgl |
| 94 | + const glwindow = gc.registerResource(renderWindow.newAPISpecificView()); |
| 95 | + glwindow.setContainer(renderWindowContainer); |
| 96 | + renderWindow.addView(glwindow); |
| 97 | + glwindow.setSize(400, 400); |
| 98 | + |
| 99 | + renderWindow.render(); |
| 100 | + |
| 101 | + const promise = glwindow |
| 102 | + .captureNextImage() |
| 103 | + .then((image) => |
| 104 | + testUtils.compareImages( |
| 105 | + image, |
| 106 | + [baseline], |
| 107 | + 'Rendering/Core/Glyph3DMapper/testGlyph3DMapperClip', |
| 108 | + t, |
| 109 | + 1.0 |
| 110 | + ) |
| 111 | + ) |
| 112 | + .finally(gc.releaseResources); |
| 113 | + renderWindow.render(); |
| 114 | + return promise; |
| 115 | +}); |
0 commit comments