Skip to content

Commit 0662219

Browse files
committed
Change default camera position when 3d plot significantly resized
1 parent cd912ba commit 0662219

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

modules/hist/hist3d.mjs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -331,43 +331,59 @@ function create3DCamera(fp, orthographic) {
331331
fp.scene.add(fp.camera);
332332
}
333333

334-
/** @summary Set default camera position
334+
/** @summary Returns camera default position
335335
* @private */
336-
function setCameraPosition(fp, first_time) {
336+
function getCameraDefaultPosition(fp, first_time) {
337337
const pad = fp.getPadPainter().getRootPad(true),
338338
kz = fp.camera.isOrthographicCamera ? 1 : 1.4;
339339
let max3dx = Math.max(0.75*fp.size_x3d, fp.size_z3d),
340-
max3dy = Math.max(0.75*fp.size_y3d, fp.size_z3d);
340+
max3dy = Math.max(0.75*fp.size_y3d, fp.size_z3d),
341+
pos = null;
341342

342343
if (first_time) {
344+
pos = new THREE.Vector3();
343345
if (max3dx === max3dy)
344-
fp.camera.position.set(-1.6*max3dx, -3.5*max3dy, kz*fp.size_z3d);
346+
pos.set(-1.6*max3dx, -3.5*max3dy, kz*fp.size_z3d);
345347
else if (max3dx > max3dy)
346-
fp.camera.position.set(-2*max3dx, -3.5*max3dy, kz*fp.size_z3d);
348+
pos.set(-2*max3dx, -3.5*max3dy, kz*fp.size_z3d);
347349
else
348-
fp.camera.position.set(-3.5*max3dx, -2*max3dy, kz*fp.size_z3d);
350+
pos.set(-3.5*max3dx, -2*max3dy, kz*fp.size_z3d);
349351
}
350352

351353
if (pad && (first_time || !fp.zoomChangedInteractive())) {
352354
if (Number.isFinite(pad.fTheta) && Number.isFinite(pad.fPhi) && ((pad.fTheta !== fp.camera_Theta) || (pad.fPhi !== fp.camera_Phi))) {
353-
fp.camera_Phi = pad.fPhi;
354-
fp.camera_Theta = pad.fTheta;
355+
if (!pos)
356+
pos = new THREE.Vector3();
355357
max3dx = 3*Math.max(fp.size_x3d, fp.size_z3d);
356358
max3dy = 3*Math.max(fp.size_y3d, fp.size_z3d);
357-
const phi = (270 - pad.fPhi)/180*Math.PI, theta = (pad.fTheta - 10)/180*Math.PI;
358-
fp.camera.position.set(max3dx*Math.cos(phi)*Math.cos(theta),
359-
max3dy*Math.sin(phi)*Math.cos(theta),
360-
fp.size_z3d + (kz-0.9)*(max3dx+max3dy)*Math.sin(theta));
361-
first_time = true;
359+
const phi = (270 - pad.fPhi) / 180 * Math.PI,
360+
theta = (pad.fTheta - 10) / 180 * Math.PI;
361+
pos.set(max3dx*Math.cos(phi)*Math.cos(theta),
362+
max3dy*Math.sin(phi)*Math.cos(theta),
363+
fp.size_z3d + (kz-0.9)*(max3dx+max3dy)*Math.sin(theta));
362364
}
363365
}
364366

367+
return pos;
368+
}
369+
370+
/** @summary Set default camera position
371+
* @private */
372+
function setCameraPosition(fp, first_time) {
373+
const pos = getCameraDefaultPosition(fp, first_time);
374+
375+
if (pos) {
376+
fp.camera.position.copy(pos);
377+
first_time = true;
378+
}
379+
365380
if (first_time)
366381
fp.camera.lookAt(fp.lookat);
367382

368383
if (first_time && fp.camera.isOrthographicCamera && fp.scene_width && fp.scene_height) {
369384
const screen_ratio = fp.scene_width / fp.scene_height,
370-
szx = fp.camera.right - fp.camera.left, szy = fp.camera.top - fp.camera.bottom;
385+
szx = fp.camera.right - fp.camera.left,
386+
szy = fp.camera.top - fp.camera.bottom;
371387

372388
if (screen_ratio > szx / szy) {
373389
// screen wider than actual geometry
@@ -390,7 +406,7 @@ function getCameraPosition(fp) {
390406
dist = p.distanceTo(p0),
391407
dist_xy = Math.sqrt((p.x-p0.x)**2 + (p.y-p0.y)**2),
392408
new_theta = Math.atan2((p.z - p0.z)/dist, dist_xy/dist) / Math.PI * 180,
393-
new_phi = 270 - Math.atan2((p.y - p0.y)/dist_xy, (p.x - p0.x)/dist_xy)/ Math.PI * 180,
409+
new_phi = 270 - Math.atan2((p.y - p0.y)/dist_xy, (p.x - p0.x)/dist_xy) / Math.PI * 180,
394410
pad = fp.getPadPainter()?.getRootPad(true);
395411

396412
fp.camera_Phi = new_phi >= 360 ? new_phi - 360 : new_phi;
@@ -430,7 +446,8 @@ function create3DControl(fp) {
430446
const delta_x = 1e-4*frame_painter.size_x3d,
431447
delta_y = 1e-4*frame_painter.size_y3d,
432448
delta_z = 1e-4*frame_painter.size_z3d;
433-
if ((tip.x1 > tip.x2) || (tip.y1 > tip.y2) || (tip.z1 > tip.z2)) console.warn('check 3D hints coordinates');
449+
if ((tip.x1 > tip.x2) || (tip.y1 > tip.y2) || (tip.z1 > tip.z2))
450+
console.warn('check 3D hints coordinates');
434451
tip.x1 -= delta_x; tip.x2 += delta_x;
435452
tip.y1 -= delta_y; tip.y2 += delta_y;
436453
tip.z1 -= delta_z; tip.z2 += delta_z;
@@ -738,6 +755,9 @@ function resize3D() {
738755
if ((Math.abs(x3d - this.size_x3d) > 0.1*this.size_z3d) || (Math.abs(y3d - this.size_y3d) > 0.1*this.size_z3d)) {
739756
this.size_x3d = x3d;
740757
this.size_y3d = y3d;
758+
const pos = getCameraDefaultPosition(this, true);
759+
if (pos)
760+
this.control.position0.copy(pos);
741761
return 1; // indicate significant resize
742762
}
743763

0 commit comments

Comments
 (0)