-
Notifications
You must be signed in to change notification settings - Fork 75
Lazy source transforms for DataTable #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
slimbuck
wants to merge
12
commits into
playcanvas:main
Choose a base branch
from
slimbuck:trans-dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1723aab
latest
slimbuck de77614
latest
slimbuck bdf899b
latest
slimbuck a2afb96
latest
slimbuck 4c16b16
latest
slimbuck 8205c38
latest
slimbuck fa49060
latest
slimbuck 79f8d7a
latest
slimbuck ec4fd0b
latesr
slimbuck 1825a63
Merge remote-tracking branch 'upstream/main' into trans-dev
slimbuck 56f6bf2
latest
slimbuck f83a0eb
latest
slimbuck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,85 +1,209 @@ | ||
| import { Mat3, Mat4, Quat, Vec3 } from 'playcanvas'; | ||
| import { Mat3, Quat, Vec3 } from 'playcanvas'; | ||
|
|
||
| import { DataTable } from './data-table'; | ||
| import { Column, DataTable, TypedArray } from './data-table'; | ||
| import { Transform } from '../utils/math'; | ||
| import { RotateSH } from '../utils/rotate-sh'; | ||
|
|
||
| const shNames = new Array(45).fill('').map((_, i) => `f_rest_${i}`); | ||
|
|
||
| const v = new Vec3(); | ||
| const q = new Quat(); | ||
| const _v = new Vec3(); | ||
| const _q = new Quat(); | ||
|
|
||
| // -- Helpers for on-demand column generation -- | ||
|
|
||
| /** | ||
| * Applies a spatial transformation to splat data in-place. | ||
| * | ||
| * Transforms position, rotation, scale, and spherical harmonics data. | ||
| * The transformation is applied as: scale first, then rotation, then translation. | ||
| * | ||
| * @param dataTable - The DataTable to transform (modified in-place). | ||
| * @param t - Translation vector. | ||
| * @param r - Rotation quaternion. | ||
| * @param s - Uniform scale factor. | ||
| * Computes the delta transform needed to convert raw data from its current | ||
| * coordinate system into the output format's coordinate system. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { Vec3, Quat } from 'playcanvas'; | ||
| * @param transform - The DataTable's current source transform. | ||
| * @param outputFormatTransform - The output format's expected transform. | ||
| * @returns The delta transform to apply to raw data, or null if it is identity. | ||
| */ | ||
| const computeWriteTransform = (transform: Transform, outputFormatTransform: Transform): Transform | null => { | ||
| const delta = outputFormatTransform.clone().invert().mul(transform); | ||
| return delta.isIdentity() ? null : delta; | ||
| }; | ||
|
|
||
| /** | ||
| * Detects how many SH bands (0-3) the DataTable has. | ||
| * @ignore | ||
| */ | ||
| const detectSHBands = (dataTable: DataTable): number => { | ||
| return ({ '9': 1, '24': 2, '-1': 3 } as Record<string, number>)[String(shNames.findIndex(n => !dataTable.hasColumn(n)))] ?? 0; | ||
| }; | ||
|
|
||
| /** | ||
| * Generates transformed typed arrays for requested columns, applying the given | ||
| * transform. Columns unaffected by the transform return references to the | ||
| * original arrays (zero copy). | ||
| * | ||
| * // Scale by 2x, rotate 90° around Y, translate up | ||
| * transform(dataTable, new Vec3(0, 5, 0), new Quat().setFromEulerAngles(0, 90, 0), 2.0); | ||
| * ``` | ||
| * @param dataTable - The source DataTable. | ||
| * @param columnNames - Which columns to produce. | ||
| * @param delta - The transform to apply. If identity or null, original arrays are returned. | ||
| * @returns A map of column name to typed array. | ||
| */ | ||
| const transform = (dataTable: DataTable, t: Vec3, r: Quat, s: number): void => { | ||
| const mat = new Mat4().setTRS(t, r, new Vec3(s, s, s)); | ||
| const mat3 = new Mat3().setFromQuat(r); | ||
| const rotateSH = new RotateSH(mat3); | ||
|
|
||
| const hasTranslation = ['x', 'y', 'z'].every(c => dataTable.hasColumn(c)); | ||
| const hasRotation = ['rot_0', 'rot_1', 'rot_2', 'rot_3'].every(c => dataTable.hasColumn(c)); | ||
| const hasScale = ['scale_0', 'scale_1', 'scale_2'].every(c => dataTable.hasColumn(c)); | ||
| const shBands = { '9': 1, '24': 2, '-1': 3 }[shNames.findIndex(v => !dataTable.hasColumn(v))] ?? 0; | ||
| const shCoeffs = new Float32Array([0, 3, 8, 15][shBands]); | ||
|
|
||
| const row: any = {}; | ||
| for (let i = 0; i < dataTable.numRows; ++i) { | ||
| dataTable.getRow(i, row); | ||
|
|
||
| if (hasTranslation) { | ||
| v.set(row.x, row.y, row.z); | ||
| mat.transformPoint(v, v); | ||
| row.x = v.x; | ||
| row.y = v.y; | ||
| row.z = v.z; | ||
| const transformColumns = (dataTable: DataTable, columnNames: string[], delta: Transform | null): Map<string, TypedArray> => { | ||
| const result = new Map<string, TypedArray>(); | ||
|
|
||
| if (!delta || delta.isIdentity()) { | ||
| for (const name of columnNames) { | ||
| const col = dataTable.getColumnByName(name); | ||
| if (col) result.set(name, col.data); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| const numRows = dataTable.numRows; | ||
| const r = delta.rotation; | ||
| const s = delta.scale; | ||
|
|
||
| // Categorize requested columns | ||
| const posNames = ['x', 'y', 'z']; | ||
| const rotNames = ['rot_0', 'rot_1', 'rot_2', 'rot_3']; | ||
| const scaleNames = ['scale_0', 'scale_1', 'scale_2']; | ||
|
|
||
| const hasPos = posNames.every(n => dataTable.hasColumn(n)); | ||
| const needPos = hasPos && posNames.some(n => columnNames.includes(n)); | ||
| const needRot = rotNames.every(n => columnNames.includes(n) && dataTable.hasColumn(n)); | ||
| const needScale = scaleNames.some(n => columnNames.includes(n) && dataTable.hasColumn(n)) && s !== 1; | ||
|
|
||
| const shBands = detectSHBands(dataTable); | ||
| const shCoeffsPerChannel = [0, 3, 8, 15][shBands]; | ||
| const rotIsIdentity = Math.abs(Math.abs(r.w) - 1) < 1e-6; | ||
| const requestedSH = shBands > 0 && !rotIsIdentity && shNames.slice(0, shCoeffsPerChannel * 3).some(n => columnNames.includes(n)); | ||
|
|
||
| // Position columns | ||
| if (needPos) { | ||
| const srcX = dataTable.getColumnByName('x')!.data; | ||
| const srcY = dataTable.getColumnByName('y')!.data; | ||
| const srcZ = dataTable.getColumnByName('z')!.data; | ||
| const dstX = new Float32Array(numRows); | ||
| const dstY = new Float32Array(numRows); | ||
| const dstZ = new Float32Array(numRows); | ||
|
|
||
slimbuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (let i = 0; i < numRows; ++i) { | ||
| _v.set(srcX[i], srcY[i], srcZ[i]); | ||
| delta.transformPoint(_v, _v); | ||
| dstX[i] = _v.x; | ||
| dstY[i] = _v.y; | ||
| dstZ[i] = _v.z; | ||
| } | ||
|
|
||
| if (columnNames.includes('x')) result.set('x', dstX); | ||
| if (columnNames.includes('y')) result.set('y', dstY); | ||
| if (columnNames.includes('z')) result.set('z', dstZ); | ||
| } | ||
|
|
||
| // Rotation columns | ||
| if (needRot) { | ||
| const src0 = dataTable.getColumnByName('rot_0')!.data; | ||
| const src1 = dataTable.getColumnByName('rot_1')!.data; | ||
| const src2 = dataTable.getColumnByName('rot_2')!.data; | ||
| const src3 = dataTable.getColumnByName('rot_3')!.data; | ||
| const dst0 = new Float32Array(numRows); | ||
| const dst1 = new Float32Array(numRows); | ||
| const dst2 = new Float32Array(numRows); | ||
| const dst3 = new Float32Array(numRows); | ||
|
|
||
| for (let i = 0; i < numRows; ++i) { | ||
| _q.set(src1[i], src2[i], src3[i], src0[i]).mul2(r, _q); | ||
| dst0[i] = _q.w; | ||
| dst1[i] = _q.x; | ||
| dst2[i] = _q.y; | ||
| dst3[i] = _q.z; | ||
| } | ||
|
|
||
| if (hasRotation) { | ||
| q.set(row.rot_1, row.rot_2, row.rot_3, row.rot_0).mul2(r, q); | ||
| row.rot_0 = q.w; | ||
| row.rot_1 = q.x; | ||
| row.rot_2 = q.y; | ||
| row.rot_3 = q.z; | ||
| result.set('rot_0', dst0); | ||
| result.set('rot_1', dst1); | ||
| result.set('rot_2', dst2); | ||
| result.set('rot_3', dst3); | ||
| } | ||
|
|
||
| // Scale columns (only affected when uniform scale != 1) | ||
| if (needScale) { | ||
| const logS = Math.log(s); | ||
| for (const name of scaleNames) { | ||
| if (!columnNames.includes(name) || !dataTable.hasColumn(name)) continue; | ||
| const src = dataTable.getColumnByName(name)!.data; | ||
| const dst = new Float32Array(numRows); | ||
| for (let i = 0; i < numRows; ++i) { | ||
| dst[i] = src[i] + logS; | ||
| } | ||
| result.set(name, dst); | ||
| } | ||
| } | ||
|
|
||
| // SH columns | ||
| if (requestedSH) { | ||
| const mat3 = new Mat3().setFromQuat(r); | ||
| const rotateSH = new RotateSH(mat3); | ||
| const shCoeffs = new Float32Array(shCoeffsPerChannel); | ||
|
|
||
slimbuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (hasScale && s !== 1) { | ||
| row.scale_0 = Math.log(Math.exp(row.scale_0) * s); | ||
| row.scale_1 = Math.log(Math.exp(row.scale_1) * s); | ||
| row.scale_2 = Math.log(Math.exp(row.scale_2) * s); | ||
| const shSrc: Float32Array[][] = []; | ||
| const shDst: Float32Array[][] = []; | ||
| for (let j = 0; j < 3; ++j) { | ||
| const src: Float32Array[] = []; | ||
| const dst: Float32Array[] = []; | ||
| for (let k = 0; k < shCoeffsPerChannel; ++k) { | ||
| const name = shNames[k + j * shCoeffsPerChannel]; | ||
| src.push(dataTable.getColumnByName(name)!.data as Float32Array); | ||
| dst.push(new Float32Array(numRows)); | ||
| } | ||
| shSrc.push(src); | ||
| shDst.push(dst); | ||
| } | ||
|
|
||
| if (shBands > 0) { | ||
| for (let i = 0; i < numRows; ++i) { | ||
| for (let j = 0; j < 3; ++j) { | ||
| for (let k = 0; k < shCoeffs.length; ++k) { | ||
| shCoeffs[k] = row[shNames[k + j * shCoeffs.length]]; | ||
| for (let k = 0; k < shCoeffsPerChannel; ++k) { | ||
| shCoeffs[k] = shSrc[j][k][i]; | ||
| } | ||
|
|
||
| rotateSH.apply(shCoeffs); | ||
| for (let k = 0; k < shCoeffsPerChannel; ++k) { | ||
| shDst[j][k][i] = shCoeffs[k]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (let k = 0; k < shCoeffs.length; ++k) { | ||
| row[shNames[k + j * shCoeffs.length]] = shCoeffs[k]; | ||
| for (let j = 0; j < 3; ++j) { | ||
| for (let k = 0; k < shCoeffsPerChannel; ++k) { | ||
| const name = shNames[k + j * shCoeffsPerChannel]; | ||
| if (columnNames.includes(name)) { | ||
| result.set(name, shDst[j][k]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| dataTable.setRow(i, row); | ||
| // All remaining requested columns: return original array references | ||
| for (const name of columnNames) { | ||
| if (!result.has(name)) { | ||
| const col = dataTable.getColumnByName(name); | ||
| if (col) result.set(name, col.data); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| export { transform }; | ||
| /** | ||
| * Returns a new DataTable with column data converted to the target coordinate | ||
| * space. If the DataTable is already in that space, returns it unchanged. | ||
| * | ||
| * @param dataTable - The source DataTable. | ||
| * @param targetTransform - The desired coordinate-space transform. | ||
| * @returns A DataTable whose raw data is in the target coordinate space. | ||
| */ | ||
| const convertToSpace = (dataTable: DataTable, targetTransform: Transform): DataTable => { | ||
| const delta = computeWriteTransform(dataTable.transform, targetTransform); | ||
| if (!delta) return dataTable; | ||
| const allNames = dataTable.columnNames; | ||
| const cols = transformColumns(dataTable, allNames, delta); | ||
| return new DataTable(allNames.map(name => new Column(name, cols.get(name)!)), targetTransform); | ||
| }; | ||
|
|
||
| export { | ||
| transformColumns, | ||
| computeWriteTransform, | ||
| convertToSpace | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.