From 910394cfae3c234795e89139b18824230f54bf31 Mon Sep 17 00:00:00 2001 From: Michal Dybizbanski Date: Thu, 23 Jan 2025 19:50:50 +0100 Subject: [PATCH 1/2] XEOK-197 Implement SectionPlane::quaternion (sync'd with ::dir) --- src/plugins/SectionPlanesPlugin/Control.js | 4 +- src/viewer/scene/sectionPlane/SectionPlane.js | 53 ++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/plugins/SectionPlanesPlugin/Control.js b/src/plugins/SectionPlanesPlugin/Control.js index 8904c74c27..0c138dd56d 100644 --- a/src/plugins/SectionPlanesPlugin/Control.js +++ b/src/plugins/SectionPlanesPlugin/Control.js @@ -337,7 +337,7 @@ class Control { rootNode.rotate(rgb, (rotation - lastRotation) * 180 / Math.PI); if (this._sectionPlane) { ignoreNextSectionPlaneDirUpdate = true; - this._sectionPlane.dir = math.vec3ApplyQuaternion(rootNode.quaternion, [0, 0, 1], tempVec3); + this._sectionPlane.quaternion = rootNode.quaternion; } lastRotation = rotation; }; @@ -591,7 +591,7 @@ class Control { this.id = sectionPlane.id; this._sectionPlane = sectionPlane; const setPosFromSectionPlane = () => setPos(sectionPlane.pos); - const setDirFromSectionPlane = () => rootNode.quaternion = math.vec3PairToQuaternion(zeroVec, sectionPlane.dir, quat); + const setDirFromSectionPlane = () => rootNode.quaternion = sectionPlane.quaternion; setPosFromSectionPlane(); setDirFromSectionPlane(); const onSectionPlanePos = sectionPlane.on("pos", setPosFromSectionPlane); diff --git a/src/viewer/scene/sectionPlane/SectionPlane.js b/src/viewer/scene/sectionPlane/SectionPlane.js index 92fa91d548..79b9efbc55 100644 --- a/src/viewer/scene/sectionPlane/SectionPlane.js +++ b/src/viewer/scene/sectionPlane/SectionPlane.js @@ -3,6 +3,11 @@ import {RenderState} from '../webgl/RenderState.js'; import {math} from "../math/math.js"; const tempVec3a = math.vec3(); +const tempVec3b = math.vec3(); +const tempVec4a = math.vec4(); +const front = math.vec3([0, 0, -1]); +const back = math.vec3([0, 0, 1]); +const up = math.vec3([0, 1, 0]); /** * @desc An arbitrarily-aligned World-space clipping plane. @@ -74,6 +79,8 @@ class SectionPlane extends Component { this._state = new RenderState({ active: true, pos: math.vec3(), + quaternion: math.vec4(), + roll: 0, dir: math.vec3(), dist: 0 }); @@ -135,6 +142,36 @@ class SectionPlane extends Component { return this._state.pos; } + /** + * Sets the quaternion of this SectionPlane's plane. + * + * Default value is ````[0, -1, 0, 0]````. + * + * @param {Number[]} value New quaternion. + */ + set quaternion(value) { + this._state.quaternion.set(value || [0, 0, 0, -1]); + math.vec3ApplyQuaternion(this._state.quaternion, back, this._state.dir); + const quatUp = math.vec3ApplyQuaternion(this._state.quaternion, up, tempVec3a); + const dirOnlyQ = math.vec3PairToQuaternion(back, this._state.dir, tempVec4a); + const dirOnlyUp = math.vec3ApplyQuaternion(dirOnlyQ, up, tempVec3b); + const angle = Math.acos(Math.min(1, math.dotVec3(quatUp, dirOnlyUp))); + const sign = Math.sign(math.dotVec3(this._state.dir, math.cross3Vec3(quatUp, dirOnlyUp, tempVec3b))); + this._state.roll = sign * angle; + this._onDirUpdated(); + } + + /** + * Gets the quaternion of this SectionPlane's plane. + * + * Default value is ````[0, -1, 0, 0]````. + * + * @returns {Number[]} value Current quaternion. + */ + get quaternion() { + return this._state.quaternion; + } + /** * Sets the direction of this SectionPlane's plane. * @@ -143,7 +180,21 @@ class SectionPlane extends Component { * @param {Number[]} value New direction. */ set dir(value) { - this._state.dir.set(value || [0, 0, -1]); + this._state.dir.set(value || front); + math.vec3PairToQuaternion(back, this._state.dir, this._state.quaternion); + + tempVec4a[0] = 0; + tempVec4a[1] = 0; + tempVec4a[2] = -1; + tempVec4a[3] = this._state.roll; + math.angleAxisToQuaternion(tempVec4a, tempVec4a); + + math.mulQuaternions(this._state.quaternion, tempVec4a, this._state.quaternion); + + this._onDirUpdated(); + } + + _onDirUpdated() { this._state.dist = (-math.dotVec3(this._state.pos, this._state.dir)); this.glRedraw(); this.fire("dir", this._state.dir); From f1f9554f150561a79f253fd347861174ba994f14 Mon Sep 17 00:00:00 2001 From: Michal Dybizbanski Date: Thu, 23 Jan 2025 20:22:00 +0100 Subject: [PATCH 2/2] XEOK-197 Implement SectionPlane::roll (sync'd with ::quaternion and ::dir) --- src/viewer/scene/sectionPlane/SectionPlane.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/viewer/scene/sectionPlane/SectionPlane.js b/src/viewer/scene/sectionPlane/SectionPlane.js index 79b9efbc55..ae2988ef1d 100644 --- a/src/viewer/scene/sectionPlane/SectionPlane.js +++ b/src/viewer/scene/sectionPlane/SectionPlane.js @@ -172,6 +172,29 @@ class SectionPlane extends Component { return this._state.quaternion; } + /** + * Sets the roll of this SectionPlane's plane. + * + * Default value is ````0````. + * + * @param {Number[]} value New roll. + */ + set roll(value) { + this._state.roll = value || 0; + this._onDirRollUpdated(); + } + + /** + * Gets the roll of this SectionPlane's plane. + * + * Default value is ````0````. + * + * @returns {Number[]} value Current roll. + */ + get roll() { + return this._state.roll; + } + /** * Sets the direction of this SectionPlane's plane. * @@ -181,6 +204,10 @@ class SectionPlane extends Component { */ set dir(value) { this._state.dir.set(value || front); + this._onDirRollUpdated(); + } + + _onDirRollUpdated() { math.vec3PairToQuaternion(back, this._state.dir, this._state.quaternion); tempVec4a[0] = 0;