Skip to content
Snippets Groups Projects
Commit c6bf1c67 authored by Alexander Rose's avatar Alexander Rose
Browse files

ply, support for uniform coloring param

parent 611ead8b
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,7 @@ import { ChunkedArray } from 'mol-data/util'; ...@@ -16,6 +16,7 @@ import { ChunkedArray } from 'mol-data/util';
import { arrayMax, fillSerial } from 'mol-util/array'; import { arrayMax, fillSerial } from 'mol-util/array';
import { Column } from 'mol-data/db'; import { Column } from 'mol-data/db';
import { ParamDefinition as PD } from 'mol-util/param-definition'; import { ParamDefinition as PD } from 'mol-util/param-definition';
import { ColorNames } from 'mol-util/color/tables';
// TODO support 'edge' and 'material' elements, see https://www.mathworks.com/help/vision/ug/the-ply-format.html // TODO support 'edge' and 'material' elements, see https://www.mathworks.com/help/vision/ug/the-ply-format.html
...@@ -67,19 +68,30 @@ function getGrouping(count: number, column?: Column<number>) { ...@@ -67,19 +68,30 @@ function getGrouping(count: number, column?: Column<number>) {
async function getShape(ctx: RuntimeContext, plyFile: PlyFile, props: PD.Values<PlyShapeParams>, shape?: Shape<Mesh>) { async function getShape(ctx: RuntimeContext, plyFile: PlyFile, props: PD.Values<PlyShapeParams>, shape?: Shape<Mesh>) {
await ctx.update('async creation of shape from ply file') await ctx.update('async creation of shape from ply file')
const { vertexProperties: vp } = props const { coloring, grouping } = props
const vertex = plyFile.getElement('vertex') as PlyTable const vertex = plyFile.getElement('vertex') as PlyTable
if (!vertex) throw new Error('missing vertex element') if (!vertex) throw new Error('missing vertex element')
const red = vertex.getProperty(vp.red) || Column.ofConst(127, vertex.rowCount, Column.Schema.int) const { rowCount } = vertex
const green = vertex.getProperty(vp.green) || Column.ofConst(127, vertex.rowCount, Column.Schema.int) const int = Column.Schema.int
const blue = vertex.getProperty(vp.blue) || Column.ofConst(127, vertex.rowCount, Column.Schema.int)
let red: Column<number>, green: Column<number>, blue: Column<number>
if (coloring.name === 'vertex') {
red = vertex.getProperty(coloring.params.red) || Column.ofConst(127, rowCount, int)
green = vertex.getProperty(coloring.params.green) || Column.ofConst(127, rowCount, int)
blue = vertex.getProperty(coloring.params.blue) || Column.ofConst(127, rowCount, int)
} else {
const [r, g, b] = Color.toRgb(coloring.params.color)
red = Column.ofConst(r, rowCount, int)
green = Column.ofConst(g, rowCount, int)
blue = Column.ofConst(b, rowCount, int)
}
const face = plyFile.getElement('face') as PlyList const face = plyFile.getElement('face') as PlyList
if (!face) throw new Error('missing face element') if (!face) throw new Error('missing face element')
const { ids, map } = getGrouping(vertex.rowCount, vertex.getProperty(vp.group)) const { ids, map } = getGrouping(vertex.rowCount, grouping.name === 'vertex' ? vertex.getProperty(grouping.params.group) : undefined)
const mesh = await getPlyMesh(ctx, vertex, face, ids, shape && shape.geometry) const mesh = await getPlyMesh(ctx, vertex, face, ids, shape && shape.geometry)
return Shape.create( return Shape.create(
...@@ -95,57 +107,54 @@ async function getShape(ctx: RuntimeContext, plyFile: PlyFile, props: PD.Values< ...@@ -95,57 +107,54 @@ async function getShape(ctx: RuntimeContext, plyFile: PlyFile, props: PD.Values<
) )
} }
export const PlyShapeParams = { function createPlyShapeParams(vertex?: PlyTable) {
...Mesh.Params,
vertexProperties: PD.Group({
group: PD.Select('' as string, [['', '']]),
red: PD.Select('' as string, [['', '']]),
green: PD.Select('' as string, [['', '']]),
blue: PD.Select('' as string, [['', '']]),
}, { isExpanded: true }),
}
export type PlyShapeParams = typeof PlyShapeParams
function setGroupDefault<T>(group: PD.Group<any>, name: string, defaultValue: T) {
group.params[name].defaultValue = defaultValue
group.defaultValue.group = defaultValue
}
function setSelectOptions(select: PD.Select<string>, options: [string, string][]) {
select.options = options;
}
export function getPlyShapeParams(plyFile: PlyFile) {
const params = PD.clone(PlyShapeParams)
const vertex = plyFile.getElement('vertex') as PlyTable
if (vertex) {
const options: [string, string][] = [['', '']] const options: [string, string][] = [['', '']]
const defaultValues = { group: '', red: '', green: '', blue: '' }
if (vertex) {
for (let i = 0, il = vertex.propertyNames.length; i < il; ++i) { for (let i = 0, il = vertex.propertyNames.length; i < il; ++i) {
const name = vertex.propertyNames[i] const name = vertex.propertyNames[i]
options.push([ name, name ]) options.push([ name, name ])
} }
const vp = params.vertexProperties;
setSelectOptions(vp.params.group as PD.Select<string>, options);
setSelectOptions(vp.params.red as PD.Select<string>, options);
setSelectOptions(vp.params.green as PD.Select<string>, options);
setSelectOptions(vp.params.blue as PD.Select<string>, options);
// TODO harcoded as convenience for data provided by MegaMol // TODO harcoded as convenience for data provided by MegaMol
if (vertex.propertyNames.includes('atomid')) setGroupDefault(vp, 'group', 'atomid') if (vertex.propertyNames.includes('atomid')) defaultValues.group = 'atomid'
if (vertex.propertyNames.includes('red')) setGroupDefault(vp, 'red', 'red') if (vertex.propertyNames.includes('red')) defaultValues.red = 'red'
if (vertex.propertyNames.includes('green')) setGroupDefault(vp, 'green', 'green') if (vertex.propertyNames.includes('green')) defaultValues.green = 'green'
if (vertex.propertyNames.includes('blue')) setGroupDefault(vp, 'blue', 'blue') if (vertex.propertyNames.includes('blue')) defaultValues.blue = 'blue'
}
return {
...Mesh.Params,
coloring: PD.MappedStatic(defaultValues.red && defaultValues.green && defaultValues.blue ? 'vertex' : 'uniform', {
vertex: PD.Group({
red: PD.Select(defaultValues.red, options, { label: 'Red Property' }),
green: PD.Select(defaultValues.green, options, { label: 'Green Property' }),
blue: PD.Select(defaultValues.blue, options, { label: 'Blue Property' }),
}, { isFlat: true }),
uniform: PD.Group({
color: PD.Color(ColorNames.grey)
}, { isFlat: true })
}),
grouping: PD.MappedStatic(defaultValues.group ? 'vertex' : 'none', {
vertex: PD.Group({
group: PD.Select(defaultValues.group, options, { label: 'Group Property' }),
}, { isFlat: true }),
none: PD.Group({ })
}),
} }
return params
} }
export const PlyShapeParams = createPlyShapeParams()
export type PlyShapeParams = typeof PlyShapeParams
export function shapeFromPly(source: PlyFile, params?: {}) { export function shapeFromPly(source: PlyFile, params?: {}) {
return Task.create<ShapeProvider<PlyFile, Mesh, PlyShapeParams>>('Shape Provider', async ctx => { return Task.create<ShapeProvider<PlyFile, Mesh, PlyShapeParams>>('Shape Provider', async ctx => {
return { return {
label: 'Mesh', label: 'Mesh',
data: source, data: source,
params: getPlyShapeParams(source), params: createPlyShapeParams(source.getElement('vertex') as PlyTable),
getShape, getShape,
geometryUtils: Mesh.Utils geometryUtils: Mesh.Utils
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment