Skip to content

Commit

Permalink
New shape: Icosahedron
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-daff-cresset committed Nov 1, 2024
1 parent 25d45d2 commit 8503aec
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
68 changes: 68 additions & 0 deletions src/buffer/icosahedron-buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @file Icosahedron Buffer
* @private
*/

import { IcosahedronGeometry, Vector3, Matrix4 } from 'three'

import { BufferRegistry } from '../globals'
import GeometryBuffer from './geometry-buffer'
import { BufferData, BufferParameters } from './buffer'

const scale = new Vector3()
const target = new Vector3()
const up = new Vector3()
const eye = new Vector3(0, 0, 0)

export interface IcosahedronBufferData extends BufferData {
heightAxis: Float32Array
depthAxis: Float32Array
size: Float32Array
}

/**
* Icosahedron buffer. Draws Icosahedrons.
*
* @example
* var icosahedronBuffer = new IcosahedronBuffer({
* position: new Float32Array([ 0, 3, 0, -2, 0, 0 ]),
* color: new Float32Array([ 1, 0, 1, 0, 1, 0 ]),
* size: new Float32Array([ 2, 1.5 ]),
* heightAxis: new Float32Array([ 0, 1, 1, 0, 2, 0 ]),
* depthAxis: new Float32Array([ 1, 0, 1, 0, 0, 2 ])
* })
*/
class IcosahedronBuffer extends GeometryBuffer {
updateNormals = true

_heightAxis: Float32Array
_depthAxis: Float32Array
_size: Float32Array

constructor (data: IcosahedronBufferData, params: Partial<BufferParameters> = {}) {
super(data, params, new IcosahedronGeometry(1, 0))

this.setAttributes(data, true)
}

applyPositionTransform (matrix: Matrix4, i: number, i3: number) {
target.fromArray(this._heightAxis as any, i3)
up.fromArray(this._depthAxis as any, i3)
matrix.lookAt(eye, target, up)

scale.set(this._size[ i ], up.length(), target.length())
matrix.scale(scale)
}

setAttributes (data: Partial<IcosahedronBufferData> = {}, initNormals?: boolean) {
if (data.size) this._size = data.size
if (data.heightAxis) this._heightAxis = data.heightAxis
if (data.depthAxis) this._depthAxis = data.depthAxis

super.setAttributes(data, initNormals)
}
}

BufferRegistry.add('icosahedron', IcosahedronBuffer)

export default IcosahedronBuffer
6 changes: 6 additions & 0 deletions src/controls/picking-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ class PickingProxy {
* @type {Object}
*/
get ellipsoid () { return this._objectIfType('ellipsoid') as ShapePrimitive }
/**
* @type {Object}
*/
get icosahedron () { return this._objectIfType('icosahedron') as ShapePrimitive }
/**
* @type {Object}
*/
Expand Down Expand Up @@ -326,6 +330,8 @@ class PickingProxy {
msg = `distance: ${this.distance.atom1.qualifiedName()} - ${this.distance.atom2.qualifiedName()} (${this.distance.structure.name})`
} else if (this.ellipsoid) {
msg = this.ellipsoid.name
} else if (this.icosahedron) {
msg = this.icosahedron.name
} else if (this.octahedron) {
msg = this.octahedron.name
} else if (this.point) {
Expand Down
7 changes: 7 additions & 0 deletions src/geometry/primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ export class TetrahedronPrimitive extends BoxPrimitive {
static type = 'tetrahedron'
}

/**
* Icosahedron geometry primitive
*/
export class IcosahedronPrimitive extends BoxPrimitive {
static type = 'icosahedron'
}

/**
* Cylinder geometry primitive
*/
Expand Down
24 changes: 22 additions & 2 deletions src/geometry/shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createParams, ensureFloat32Array, getUintArray } from '../utils'
import {
ArrowPrimitive, BoxPrimitive, ConePrimitive, CylinderPrimitive, EllipsoidPrimitive,
OctahedronPrimitive, SpherePrimitive, TetrahedronPrimitive, TextPrimitive,
TorusPrimitive, PointPrimitive, WidelinePrimitive
TorusPrimitive, PointPrimitive, WidelinePrimitive, IcosahedronPrimitive
} from './primitive'
import { MeshPicker } from '../utils/picker'
import Buffer from '../buffer/buffer'
Expand All @@ -23,7 +23,7 @@ const tmpBox = new Box3()
const Primitives = [
ArrowPrimitive, BoxPrimitive, ConePrimitive, CylinderPrimitive,
EllipsoidPrimitive, OctahedronPrimitive, SpherePrimitive, TetrahedronPrimitive,
TextPrimitive, TorusPrimitive, PointPrimitive, WidelinePrimitive
TextPrimitive, TorusPrimitive, PointPrimitive, WidelinePrimitive, IcosahedronPrimitive
]

export const ShapeDefaultParameters = {
Expand Down Expand Up @@ -329,6 +329,26 @@ class Shape {
return this
}

/**
* Add an icosahedron
* @example
* shape.addIcosahedron([ 0, 3, 0 ], [ 1, 0, 1 ], 2, [ 0, 1, 1 ], [ 1, 0, 1 ]);
*
* @param {Vector3|Array} position - position vector or array
* @param {Color|Array} color - color object or array
* @param {Float} size - size value
* @param {Vector3|Array} heightAxis - height axis vector or array
* @param {Vector3|Array} depthAxis - depth axis vector or array
* @param {String} [name] - text
* @return {Shape} this object
*/
addIcosahedron (position: Vector3|[number, number, number], color: Color|[number, number, number], size: number, heightAxis: Vector3|[number, number, number], depthAxis: Vector3|[number, number, number], name: string) {
IcosahedronPrimitive.objectToShape(
this, { position, color, size, heightAxis, depthAxis, name }
)
return this
}

/**
* Add text
* @example
Expand Down
2 changes: 2 additions & 0 deletions src/ngl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import BoxBuffer from './buffer/box-buffer'
import ConeBuffer from './buffer/cone-buffer'
import CylinderBuffer from './buffer/cylinder-buffer'
import EllipsoidBuffer from './buffer/ellipsoid-buffer'
import IcosahedronBuffer from './buffer/icosahedron-buffer'
import MeshBuffer from './buffer/mesh-buffer'
import OctahedronBuffer from './buffer/octahedron-buffer'
import PointBuffer from './buffer/point-buffer'
Expand Down Expand Up @@ -298,6 +299,7 @@ export {
ConeBuffer,
CylinderBuffer,
EllipsoidBuffer,
IcosahedronBuffer,
MeshBuffer,
OctahedronBuffer,
PointBuffer,
Expand Down
9 changes: 8 additions & 1 deletion src/utils/picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import Selection from '../selection/selection'
import {
ArrowPrimitive, BoxPrimitive, ConePrimitive, CylinderPrimitive,
EllipsoidPrimitive, OctahedronPrimitive, SpherePrimitive,
TetrahedronPrimitive, TorusPrimitive, PointPrimitive, WidelinePrimitive
TetrahedronPrimitive, TorusPrimitive, PointPrimitive, WidelinePrimitive,
IcosahedronPrimitive
} from '../geometry/primitive'
import { contactTypeName, Contacts } from '../chemistry/interactions/contact'
import { TypedArray } from '../types';
Expand Down Expand Up @@ -283,6 +284,10 @@ class EllipsoidPicker extends ShapePicker {
get primitive () { return EllipsoidPrimitive }
}

class IcosahedronPicker extends ShapePicker {
get primitive () { return IcosahedronPrimitive }
}

class OctahedronPicker extends ShapePicker {
get primitive () { return OctahedronPrimitive }
}
Expand Down Expand Up @@ -442,6 +447,7 @@ PickerRegistry.add('box', BoxPicker)
PickerRegistry.add('cone', ConePicker)
PickerRegistry.add('cylinder', CylinderPicker)
PickerRegistry.add('ellipsoid', EllipsoidPicker)
PickerRegistry.add('icosahedron', IcosahedronPicker)
PickerRegistry.add('octahedron', OctahedronPicker)
PickerRegistry.add('sphere', SpherePicker)
PickerRegistry.add('tetrahedron', TetrahedronPicker)
Expand All @@ -463,6 +469,7 @@ export {
ClashPicker,
DistancePicker,
EllipsoidPicker,
IcosahedronPicker,
IgnorePicker,
OctahedronPicker,
MeshPicker,
Expand Down

0 comments on commit 8503aec

Please sign in to comment.