Skip to content

Commit

Permalink
Use KDTree to find nearest points in paint constraint data
Browse files Browse the repository at this point in the history
  • Loading branch information
annehaley committed Dec 13, 2023
1 parent 48530dd commit 810ca32
Show file tree
Hide file tree
Showing 5 changed files with 857 additions and 720 deletions.
1 change: 1 addition & 0 deletions web/shapeworks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"kd-tree-javascript": "1.0.0",
"sass": "~1.32.0",
"sass-loader": "^10.0.0",
"typescript": "~4.1.5",
Expand Down
34 changes: 27 additions & 7 deletions web/shapeworks/src/components/ShapeViewer/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { AttributeTypes } from 'vtk.js/Sources/Common/DataModel/DataSetAttribute
import { ColorMode, ScalarMode } from 'vtk.js/Sources/Rendering/Core/Mapper/Constants';
import { FieldDataTypes } from 'vtk.js/Sources/Common/DataModel/DataSet/Constants';

import { kdTree } from 'kd-tree-javascript';
import { distance } from '@/helper'

import {
layers, layersShown, orientationIndicator,
cachedMarchingCubes, cachedParticleComparisonColors, vtkShapesByType,
Expand Down Expand Up @@ -248,7 +251,7 @@ export default {
const newColorArray = Array.from(
{ length: allPoints.getNumberOfPoints() }, () => 0
)
if (allSetConstraints.value[label]) {
if (allSetConstraints.value && allSetConstraints.value[label] && allSetConstraints.value[label][inputDataDomain]) {
const currShapeConstraints = Object.values(allSetConstraints.value[label][inputDataDomain])
currShapeConstraints.forEach((cData) => {
if (cData.type === 'plane') {
Expand All @@ -269,14 +272,26 @@ export default {
}
}
}

} else if (cData.type === 'paint') {
const { scalars } = cData.data.field
// TODO: this takes too long with many actors
const { scalars, points } = cData.data.field
const scalarPoints = points.map((p, i) => ({ x: p[0], y: p[1], z: p[2], s: scalars[i] }))
const tree = new kdTree(scalarPoints, distance, ['x', 'y', 'z', 's']);
if (scalars.length === allPoints.getNumberOfPoints()) {
for (let i = 0; i < scalars.length; i++) {
// scalar assignment is swapped in stored constraint data
if (scalars[i] === 0) {
newColorArray[i] = 1
const currentPoint = allPoints.getPoint(i)
const currentPointObj = {
x: currentPoint[0],
y: currentPoint[1],
z: currentPoint[2],
}
const nearests = tree.nearest(currentPointObj, 1)
if (nearests.length) {
const [nearest,] = nearests[0]
// scalar assignment is swapped in stored constraint data
if (nearest.s === 0) {
newColorArray[i] = 1
}
}
}
}
Expand All @@ -297,14 +312,19 @@ export default {
let widgetHandle;
if (cData.type === 'plane') {
const { origin, normal } = cData.data
// TODO: plane/shape overlap appears to change when rotating
// (but only in viewers without the mouse)
widget = vtkImplicitPlaneWidget.newInstance()
widget.placeWidget(bounds);
widget.setPlaceFactor(2);
widgetState = widget.getWidgetState()
widgetState.setOrigin(origin);
widgetState.setNormal(normal)
widgetHandle = widgetManager.addWidget(widget);

// TODO: this doesn't disappear until after first interaction
widgetHandle.setOutlineVisible(false)

widgetHandle.getRepresentations()[0].setLabels({
'subject': label,
'domain': inputDataDomain
Expand All @@ -324,9 +344,9 @@ export default {
// TODO create paint widget
}
}
updateColors()
}
})
updateColors()
})
},
addPoints(label, renderer, points, i) {
Expand Down
4 changes: 4 additions & 0 deletions web/shapeworks/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ export function hexToRgb(hex: string) {
parseInt(result[3], 16)
] : [0, 0, 0];
}

export function distance(a, b){
return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2) + Math.pow(a.z - b.z, 2);
}
1 change: 1 addition & 0 deletions web/shapeworks/src/reader/constraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function convertConstraintDataForDB(constraintData) {
const p1 = origin;
const p2 = addVectors(origin, v2)
const p3 = addVectors(origin, v1)
// TODO: sometimes normals are flipped

constraintJSON.planes.push({points: [p1, p2, p3]})
} else if (cData.type === 'paint') {
Expand Down
Loading

0 comments on commit 810ca32

Please sign in to comment.