Skip to content

Commit

Permalink
fix(RodViewer): Update rod viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Apr 30, 2018
1 parent 46eec0f commit 4c47ac4
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 13 deletions.
73 changes: 70 additions & 3 deletions src/simput/RodEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import PropTypes from 'prop-types';

import Rod2DPreview from '../widgets/Rod2DPreview';
import EditableList from '../widgets/EditableList';
import VTKWidget from '../widgets/VTKWidget';

import vtkRodVTKViewer from '../utils/RodVTKViewer';

import style from './RodEditor.mcss';

Expand All @@ -15,11 +18,13 @@ export default class RodEditor extends React.Component {
this.onLengthChange = this.onLengthChange.bind(this);
this.addLayer = this.addLayer.bind(this);
this.delLayer = this.delLayer.bind(this);

this.rodViewer = vtkRodVTKViewer.newInstance();
}

onCellChange(layer, value) {
const data = this.props.data;
const cells = this.props.ui.domain;
const cells = this.props.ui.domain.cells;
if (data.value && data.value.length) {
const stack = data.value[0].stack;
if (value in cells) {
Expand Down Expand Up @@ -47,7 +52,7 @@ export default class RodEditor extends React.Component {

addLayer(idx) {
const data = this.props.data;
const cells = Object.keys(this.props.ui.domain);
const cells = Object.keys(this.props.ui.domain.cells);
if (data.value && data.value.length && cells.length) {
const stack = data.value[0].stack;
const afterIdx = idx + 1;
Expand All @@ -72,7 +77,7 @@ export default class RodEditor extends React.Component {
}

render() {
const cells = Object.keys(this.props.ui.domain);
const cells = Object.keys(this.props.ui.domain.cells);

const columns = [
{
Expand Down Expand Up @@ -120,9 +125,71 @@ export default class RodEditor extends React.Component {

const totalLength = Number(this.props.viewData.rodInfo.height.value[0]);

// rod = {
// name: '',
// pitch: 1.26,
// totalLength: 400,
// colors: {
// mod: [0, 0, 0.5],
// he: [0, 0.5, 0.3],
// zirc: [0.5, 0.5, 0.3],
// ss: [0.4, 0.5, 0.4],
// },
// cells: {
// A: [
// { material: 'mod', radius: 0.2 },
// { material: 'he', radius: 0.3 },
// { material: 'zirc', radius: 0.4 },
// { material: 'ss', radius: 0.5 },
// ],
// B: [],
// C: [],
// },
// layers: [
// { cell: 'A', length: 10 },
// { cell: 'B', length: 200 },
// { cell: 'A', length: 5 },
// { cell: 'C', length: 10 },
// ],
// }
const colors = {};
Object.keys(this.props.ui.domain.materials).forEach((mat) => {
colors[mat] = this.props.ui.domain.materials[mat].color;
});
const layers = this.props.data.value[0].stack.map((l) => ({
cell: l.label,
length: l.length,
}));
const cellData = {};
Object.keys(this.props.ui.domain.cells).forEach((cellName) => {
cellData[cellName] = this.props.ui.domain.cells[
cellName
].cell[0].mats.map((material, i) => ({
material,
radius: this.props.ui.domain.cells[cellName].cell[0].radii[i],
}));
});
const rodData = {
pitch: 1.26, // FIXME...
totalLength,
colors,
cells: cellData,
layers,
};

return (
<div>
<Rod2DPreview stack={items} totalLength={totalLength} />
<div className={style.preview3d}>
<VTKWidget
viewer={this.rodViewer}
data={rodData}
orientation={[0, 1000, 0]}
viewUp={[1, 0, 0]}
zoom={10}
zScaling={0.1}
/>
</div>
<EditableList
columns={columns}
data={items}
Expand Down
7 changes: 6 additions & 1 deletion src/simput/RodEditor.mcss
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@

.cellSelect {
width: 100%;
}
}

.preview3d {
height: 100px;
margin-bottom: 20px;
}
31 changes: 27 additions & 4 deletions src/utils/RodVTKViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import macro from 'vtk.js/Sources/macro';
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkColorTransferFunction from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction';
import vtkConcentricCylinderSource from 'vtk.js/Sources/Filters/Sources/ConcentricCylinderSource';
import vtkCubeSource from 'vtk.js/Sources/Filters/Sources/CubeSource';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';

import vtkVTKViewer from './VTKViewer';
Expand All @@ -13,13 +14,14 @@ function createCell(model, center, height, radius, cellFields) {

const source = vtkConcentricCylinderSource.newInstance({
resolution: 60,
startTheta: 0,
endTheta: 180,
startTheta: 180,
endTheta: 360,
height,
center,
radius,
cellFields,
});

const mapper = vtkMapper.newInstance({
lookupTable,
useLookupTableScalarRange: true,
Expand Down Expand Up @@ -63,7 +65,7 @@ function processCells(cells, materialIds) {
const cellFields = [];

for (let j = 0; j < cell.length; j++) {
const { material, radius } = cell[i];
const { material, radius } = cell[j];
radii.push(radius);
cellFields.push(materialIds.indexOf(material));
}
Expand All @@ -81,12 +83,26 @@ function vtkRodVTKViewer(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkRodVTKViewer');

// 2D navigation
publicAPI.setParallelRendering(true);

// Internal pipeline
model.lookupTable = vtkColorTransferFunction.newInstance();
model.stack = [];

model.sourceCtx = vtkCubeSource.newInstance();
model.mapperCtx = vtkMapper.newInstance({ scalarVisibility: false });
model.actorCtx = vtkActor.newInstance();

model.actorCtx
.getProperty()
.set(Object.assign({ representation: 1 }, vtkVTKViewer.PROPERTY_SETTINGS));
model.actorCtx.setMapper(model.mapperCtx);
model.mapperCtx.setInputConnection(model.sourceCtx.getOutputPort());

// rod = {
// name: '',
// pitch: 1.26,
// totalLength: 400,
// colors: {
// mod: [0, 0, 0.5],
Expand Down Expand Up @@ -131,10 +147,17 @@ function vtkRodVTKViewer(publicAPI, model) {
cellFields
);
model.stack.push(cellPipeline);
offset += length / 2;
offset += length;
publicAPI.addActor(cellPipeline.actor);
}

// Update context
model.sourceCtx.setZLength(rod.totalLength);
model.sourceCtx.setXLength(rod.pitch);
model.sourceCtx.setYLength(rod.pitch);
model.sourceCtx.setCenter(0, 0, rod.totalLength * 0.5);
publicAPI.addActor(model.actorCtx);

publicAPI.renderLater();
};
}
Expand Down
17 changes: 14 additions & 3 deletions src/utils/VTKViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ function vtkVTKViewer(publicAPI, model) {

publicAPI.setZScale = (zScaling) => {
model.zScaling = zScaling;
let count = model.actors.length;
let count = model.actors.length || 0;
while (count--) {
model.pipeline.actors[count].setScale(1, 1, zScaling);
model.actors[count].setScale(1, 1, zScaling);
}

model.renderer.resetCameraClippingRange();
Expand Down Expand Up @@ -128,17 +128,28 @@ function vtkVTKViewer(publicAPI, model) {

// --------------------------------------------------------------------------

publicAPI.render = () => {
model.renderWindow.render();
};

// --------------------------------------------------------------------------

publicAPI.renderLater = () => {
setTimeout(model.renderWindow.render, 0);
};

// --------------------------------------------------------------------------

publicAPI.resetCamera = (orientation = [0, 0, 1], viewUp = [0, 1, 0]) => {
publicAPI.resetCamera = (
orientation = [0, 0, 1],
viewUp = [0, 1, 0],
zoom = 1
) => {
const camera = model.renderer.getActiveCamera();
camera.setPosition(...orientation);
camera.setViewUp(...viewUp);
model.renderer.resetCamera();
camera.zoom(zoom);

model.interactorStyle3D.setCenterOfRotation(camera.getFocalPoint());
model.interactorStyle2D.setCenterOfRotation(camera.getFocalPoint());
Expand Down
16 changes: 14 additions & 2 deletions src/widgets/VTKWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ export default class VTKWidget extends React.Component {

// Push data on first load
this.componentWillReceiveProps(this.props);
this.props.viewer.resetCamera(this.props.orientation);
this.resetCamera();
setTimeout(this.resetCamera, 0);
setTimeout(this.resetCamera, 10);
}

componentWillReceiveProps(nextProps) {
this.props.viewer.setZScale(this.props.zScaling);
this.props.viewer.setData(nextProps.data);
}

Expand All @@ -54,7 +57,12 @@ export default class VTKWidget extends React.Component {
}

resetCamera() {
this.props.viewer.resetCamera(this.props.orientation, this.props.viewUp);
this.props.viewer.render();
this.props.viewer.resetCamera(
this.props.orientation,
this.props.viewUp,
this.props.zoom
);
}

render() {
Expand All @@ -77,10 +85,14 @@ VTKWidget.propTypes = {
data: PropTypes.object,
orientation: PropTypes.array,
viewUp: PropTypes.array,
zoom: PropTypes.number,
zScaling: PropTypes.number,
};

VTKWidget.defaultProps = {
data: null,
orientation: [0, 0, 1000],
viewUp: [0, 1, 0],
zoom: 1,
zScaling: 1,
};

0 comments on commit 4c47ac4

Please sign in to comment.