Skip to content

Commit 92ee3a3

Browse files
committed
feat(cubeaxesactor): expose generateTicks() internal function
This lets users control the ticks of the grid
1 parent cebd291 commit 92ee3a3

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

Examples/Geometry/CubeAxes/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import '@kitware/vtk.js/favicon';
33
// Load the rendering pieces we want to use (for both WebGL and WebGPU)
44
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
55

6+
import * as d3scale from 'd3-scale';
7+
import * as d3array from 'd3-array';
8+
69
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
710
import vtkCubeAxesActor from '@kitware/vtk.js/Rendering/Core/CubeAxesActor';
811
import vtkConeSource from '@kitware/vtk.js/Filters/Sources/ConeSource';
@@ -40,6 +43,18 @@ renderer.addActor(actor);
4043
const cubeAxes = vtkCubeAxesActor.newInstance();
4144
cubeAxes.setCamera(renderer.getActiveCamera());
4245
cubeAxes.setDataBounds(actor.getBounds());
46+
// Replace ticks from axis 0
47+
function myGenerateTicks(defaultGenerateTicks) {
48+
return (dataBounds) => {
49+
const res = defaultGenerateTicks(dataBounds);
50+
const scale = d3scale.scaleLinear().domain([dataBounds[0], dataBounds[1]]);
51+
res.ticks[0] = d3array.range(dataBounds[0], dataBounds[1], 0.1);
52+
const format = scale.tickFormat(res.ticks[0].length);
53+
res.tickStrings[0] = res.ticks[0].map(format);
54+
return res;
55+
};
56+
}
57+
cubeAxes.setGenerateTicks(myGenerateTicks(cubeAxes.getGenerateTicks()));
4358
renderer.addActor(cubeAxes);
4459

4560
renderer.resetCamera();

Sources/Rendering/Core/CubeAxesActor/index.js

+37-19
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ function applyTextStyle(ctx, style) {
8181
ctx.font = `${style.fontStyle} ${style.fontSize}px ${style.fontFamily}`;
8282
}
8383

84+
function defaultGenerateTicks(publicApi, model) {
85+
return (dataBounds) => {
86+
const ticks = [];
87+
const tickStrings = [];
88+
for (let i = 0; i < 3; i++) {
89+
const scale = d3
90+
.scaleLinear()
91+
.domain([dataBounds[i * 2], dataBounds[i * 2 + 1]]);
92+
ticks[i] = scale.ticks(5);
93+
const format = scale.tickFormat(5);
94+
tickStrings[i] = ticks[i].map(format);
95+
}
96+
return { ticks, tickStrings };
97+
};
98+
}
99+
84100
// many properties of this actor depend on the API specific view The main
85101
// dependency being the resolution as that drives what font sizes to use.
86102
// Bacause of this we need to do some of the calculations in a API specific
@@ -643,27 +659,23 @@ function vtkCubeAxesActor(publicAPI, model) {
643659
}
644660

645661
// compute tick marks for axes
646-
const ticks = [];
647-
const tickStrings = [];
648-
for (let i = 0; i < 3; i++) {
649-
const scale = d3
650-
.scaleLinear()
651-
.domain([model.dataBounds[i * 2], model.dataBounds[i * 2 + 1]]);
652-
ticks[i] = scale.ticks(5);
653-
const format = scale.tickFormat(5);
654-
tickStrings[i] = ticks[i].map(format);
655-
}
662+
const t = model.generateTicks(model.dataBounds);
656663

657664
// update gridlines / edge lines
658-
publicAPI.updatePolyData(facesToDraw, edgesToDraw, ticks);
665+
publicAPI.updatePolyData(facesToDraw, edgesToDraw, t.ticks);
659666

660667
// compute label world coords and text
661-
publicAPI.updateTextData(facesToDraw, edgesToDraw, ticks, tickStrings);
668+
publicAPI.updateTextData(
669+
facesToDraw,
670+
edgesToDraw,
671+
t.ticks,
672+
t.tickStrings
673+
);
662674

663675
// rebuild the texture only when force or changed bounds, face
664676
// visibility changes do to change the atlas
665677
if (boundsChanged || model.forceUpdate) {
666-
publicAPI.updateTextureAtlas(tickStrings);
678+
publicAPI.updateTextureAtlas(t.tickStrings);
667679
}
668680
}
669681

@@ -802,7 +814,7 @@ function vtkCubeAxesActor(publicAPI, model) {
802814
// Object factory
803815
// ----------------------------------------------------------------------------
804816

805-
function defaultValues(initialValues) {
817+
function defaultValues(publicAPI, model, initialValues) {
806818
return {
807819
boundsScaleFactor: 1.3,
808820
camera: null,
@@ -811,30 +823,35 @@ function defaultValues(initialValues) {
811823
gridLines: true,
812824
axisLabels: null,
813825
axisTitlePixelOffset: 35.0,
826+
tickLabelPixelOffset: 12.0,
827+
generateTicks: defaultGenerateTicks(publicAPI, model),
828+
...initialValues,
814829
axisTextStyle: {
815830
fontColor: 'white',
816831
fontStyle: 'normal',
817832
fontSize: 18,
818833
fontFamily: 'serif',
834+
...initialValues?.axisTextStyle,
819835
},
820-
tickLabelPixelOffset: 12.0,
821836
tickTextStyle: {
822837
fontColor: 'white',
823838
fontStyle: 'normal',
824839
fontSize: 14,
825840
fontFamily: 'serif',
841+
...initialValues?.tickTextStyle,
826842
},
827-
...initialValues,
828843
};
829844
}
830845

831846
// ----------------------------------------------------------------------------
832847

833848
export function extend(publicAPI, model, initialValues = {}) {
834-
Object.assign(model, defaultValues(initialValues));
835-
836849
// Inheritance
837-
vtkActor.extend(publicAPI, model, initialValues);
850+
vtkActor.extend(
851+
publicAPI,
852+
model,
853+
defaultValues(publicAPI, model, initialValues)
854+
);
838855

839856
// internal variables
840857
model.lastFacesToDraw = [false, false, false, false, false, false];
@@ -870,6 +887,7 @@ export function extend(publicAPI, model, initialValues = {}) {
870887
'faceVisibilityAngle',
871888
'gridLines',
872889
'tickLabelPixelOffset',
890+
'generateTicks',
873891
]);
874892

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

0 commit comments

Comments
 (0)