Skip to content

Commit

Permalink
feat(cubeaxesactor): expose generateTicks() internal function
Browse files Browse the repository at this point in the history
This lets users control the ticks of the grid
  • Loading branch information
finetjul committed Apr 4, 2024
1 parent cebd291 commit 92ee3a3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
15 changes: 15 additions & 0 deletions Examples/Geometry/CubeAxes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import '@kitware/vtk.js/favicon';
// Load the rendering pieces we want to use (for both WebGL and WebGPU)
import '@kitware/vtk.js/Rendering/Profiles/Geometry';

import * as d3scale from 'd3-scale';
import * as d3array from 'd3-array';

import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
import vtkCubeAxesActor from '@kitware/vtk.js/Rendering/Core/CubeAxesActor';
import vtkConeSource from '@kitware/vtk.js/Filters/Sources/ConeSource';
Expand Down Expand Up @@ -40,6 +43,18 @@ renderer.addActor(actor);
const cubeAxes = vtkCubeAxesActor.newInstance();
cubeAxes.setCamera(renderer.getActiveCamera());
cubeAxes.setDataBounds(actor.getBounds());
// Replace ticks from axis 0
function myGenerateTicks(defaultGenerateTicks) {
return (dataBounds) => {
const res = defaultGenerateTicks(dataBounds);
const scale = d3scale.scaleLinear().domain([dataBounds[0], dataBounds[1]]);
res.ticks[0] = d3array.range(dataBounds[0], dataBounds[1], 0.1);
const format = scale.tickFormat(res.ticks[0].length);
res.tickStrings[0] = res.ticks[0].map(format);
return res;
};
}
cubeAxes.setGenerateTicks(myGenerateTicks(cubeAxes.getGenerateTicks()));
renderer.addActor(cubeAxes);

renderer.resetCamera();
Expand Down
56 changes: 37 additions & 19 deletions Sources/Rendering/Core/CubeAxesActor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ function applyTextStyle(ctx, style) {
ctx.font = `${style.fontStyle} ${style.fontSize}px ${style.fontFamily}`;
}

function defaultGenerateTicks(publicApi, model) {
return (dataBounds) => {
const ticks = [];
const tickStrings = [];
for (let i = 0; i < 3; i++) {
const scale = d3
.scaleLinear()
.domain([dataBounds[i * 2], dataBounds[i * 2 + 1]]);
ticks[i] = scale.ticks(5);
const format = scale.tickFormat(5);
tickStrings[i] = ticks[i].map(format);
}
return { ticks, tickStrings };
};
}

// many properties of this actor depend on the API specific view The main
// dependency being the resolution as that drives what font sizes to use.
// Bacause of this we need to do some of the calculations in a API specific
Expand Down Expand Up @@ -643,27 +659,23 @@ function vtkCubeAxesActor(publicAPI, model) {
}

// compute tick marks for axes
const ticks = [];
const tickStrings = [];
for (let i = 0; i < 3; i++) {
const scale = d3
.scaleLinear()
.domain([model.dataBounds[i * 2], model.dataBounds[i * 2 + 1]]);
ticks[i] = scale.ticks(5);
const format = scale.tickFormat(5);
tickStrings[i] = ticks[i].map(format);
}
const t = model.generateTicks(model.dataBounds);

// update gridlines / edge lines
publicAPI.updatePolyData(facesToDraw, edgesToDraw, ticks);
publicAPI.updatePolyData(facesToDraw, edgesToDraw, t.ticks);

// compute label world coords and text
publicAPI.updateTextData(facesToDraw, edgesToDraw, ticks, tickStrings);
publicAPI.updateTextData(
facesToDraw,
edgesToDraw,
t.ticks,
t.tickStrings
);

// rebuild the texture only when force or changed bounds, face
// visibility changes do to change the atlas
if (boundsChanged || model.forceUpdate) {
publicAPI.updateTextureAtlas(tickStrings);
publicAPI.updateTextureAtlas(t.tickStrings);
}
}

Expand Down Expand Up @@ -802,7 +814,7 @@ function vtkCubeAxesActor(publicAPI, model) {
// Object factory
// ----------------------------------------------------------------------------

function defaultValues(initialValues) {
function defaultValues(publicAPI, model, initialValues) {
return {
boundsScaleFactor: 1.3,
camera: null,
Expand All @@ -811,30 +823,35 @@ function defaultValues(initialValues) {
gridLines: true,
axisLabels: null,
axisTitlePixelOffset: 35.0,
tickLabelPixelOffset: 12.0,
generateTicks: defaultGenerateTicks(publicAPI, model),
...initialValues,
axisTextStyle: {
fontColor: 'white',
fontStyle: 'normal',
fontSize: 18,
fontFamily: 'serif',
...initialValues?.axisTextStyle,
},
tickLabelPixelOffset: 12.0,
tickTextStyle: {
fontColor: 'white',
fontStyle: 'normal',
fontSize: 14,
fontFamily: 'serif',
...initialValues?.tickTextStyle,
},
...initialValues,
};
}

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

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, defaultValues(initialValues));

// Inheritance
vtkActor.extend(publicAPI, model, initialValues);
vtkActor.extend(
publicAPI,
model,
defaultValues(publicAPI, model, initialValues)
);

// internal variables
model.lastFacesToDraw = [false, false, false, false, false, false];
Expand Down Expand Up @@ -870,6 +887,7 @@ export function extend(publicAPI, model, initialValues = {}) {
'faceVisibilityAngle',
'gridLines',
'tickLabelPixelOffset',
'generateTicks',
]);

macro.setGetArray(publicAPI, model, ['dataBounds'], 6);
Expand Down

0 comments on commit 92ee3a3

Please sign in to comment.