Skip to content

Commit

Permalink
Merge pull request #719 from adithyaakrishna/feat/types-upgrade
Browse files Browse the repository at this point in the history
[Feat] - Added More Types for Parsers and WebGL and GL Files
  • Loading branch information
dkoes committed Sep 3, 2023
2 parents 8cd97e0 + 60d9e8d commit 657c2a2
Show file tree
Hide file tree
Showing 24 changed files with 202 additions and 179 deletions.
17 changes: 12 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@
"dependencies": {
"clean-jsdoc-theme": "^4.2.9",
"iobuffer": "^5.3.1",
"netcdfjs": "^2.0.2",
"netcdfjs": "^3.0.0",
"pako": "^2.1.0",
"upng-js": "^2.1.0"
},
"devDependencies": {
"@babel/core": "^7.20.7",
"@babel/eslint-parser": "^7.19.1",
"@babel/plugin-transform-typescript": "^7.20.7",
"@types/pako": "^2.0.0",
"better-docs": "github:3dmol/better-docs#master",
"canvas": "^2.11.0",
"codecov": "^3.8.3",
Expand Down
87 changes: 49 additions & 38 deletions src/GLDraw.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Vector3 } from "./WebGL/math";
import { Geometry } from "./WebGL";
import { Color, Colored } from "colors";


//define enum values
Expand All @@ -17,6 +18,11 @@ export enum CAP {
ROUND = 2
};

export interface Point {
x: number;
y: number;
z: number;
}

/**
* Lower level utilities for creating WebGL shape geometries.
Expand Down Expand Up @@ -124,7 +130,7 @@ export namespace GLDraw {
this.basisVectors = nvecs;
};

getVerticesForRadius(radius, cap, capType) {
getVerticesForRadius(radius: any, cap: CAP, capType: any) {
if (typeof (this.cache) !== "undefined" && this.cache[radius] !== undefined)
if (this.cache[radius][cap + capType] !== undefined)
return this.cache[radius][cap + capType];
Expand Down Expand Up @@ -272,31 +278,30 @@ export namespace GLDraw {

var cylVertexCache = new CylVertexCache();


/**
* Create a cylinder
* @memberof GLDraw
* @param {geometry}
* @param {Geometry}
* geo
* @param {Point}
* from
* @param {Point}
* to
* @param {float}
* @param {number}
* radius
* @param {Color}
* color
* @param {CAP} fromCap - 0 for none, 1 for flat, 2 for round
* @param {CAP} toCap = 0 for none, 1 for flat, 2 for round
*
* */
export function drawCylinder(geo: Geometry, from, to, radius: number, color, fromCap:CAP = 0, toCap:CAP = 0) {
export function drawCylinder(geo: Geometry, from: any, to: any, radius: number, color: Color | Color[], fromCap:CAP = 0, toCap:CAP = 0) {
if (!from || !to)
return;

// vertices
var drawcaps = toCap || fromCap;
color = color || { r: 0, g: 0, b: 0 };
color = color || ({ r: 0, g: 0, b: 0 } as Color);

var e = getRotationMatrix(to.x-from.x, to.y-from.y, to.z-from.z);
// get orthonormal vectors from cache
Expand Down Expand Up @@ -355,12 +360,12 @@ export namespace GLDraw {
normalArray[offset + 5] = z;

// colors
colorArray[offset] = color.r;
colorArray[offset + 3] = color.r;
colorArray[offset + 1] = color.g;
colorArray[offset + 4] = color.g;
colorArray[offset + 2] = color.b;
colorArray[offset + 5] = color.b;
colorArray[offset] = (color as Color).r;
colorArray[offset + 3] = (color as Color).r;
colorArray[offset + 1] = (color as Color).g;
colorArray[offset + 4] = (color as Color).g;
colorArray[offset + 2] = (color as Color).b;
colorArray[offset + 5] = (color as Color).b;

// faces
// 0 - 2 - 1
Expand Down Expand Up @@ -445,20 +450,20 @@ export namespace GLDraw {
vertexArray[v3offset + 2] = z3 + cap.z;
vertexArray[v4offset + 2] = z4 + cap.z;

colorArray[v1offset] = color.r;
colorArray[v2offset] = color.r;
colorArray[v3offset] = color.r;
colorArray[v4offset] = color.r;
colorArray[v1offset] = (color as Color).r;
colorArray[v2offset] = (color as Color).r;
colorArray[v3offset] = (color as Color).r;
colorArray[v4offset] = (color as Color).r;

colorArray[v1offset + 1] = color.g;
colorArray[v2offset + 1] = color.g;
colorArray[v3offset + 1] = color.g;
colorArray[v4offset + 1] = color.g;
colorArray[v1offset + 1] = (color as Color).g;
colorArray[v2offset + 1] = (color as Color).g;
colorArray[v3offset + 1] = (color as Color).g;
colorArray[v4offset + 1] = (color as Color).g;

colorArray[v1offset + 2] = color.b;
colorArray[v2offset + 2] = color.b;
colorArray[v3offset + 2] = color.b;
colorArray[v4offset + 2] = color.b;
colorArray[v1offset + 2] = (color as Color).b;
colorArray[v2offset + 2] = (color as Color).b;
colorArray[v3offset + 2] = (color as Color).b;
colorArray[v4offset + 2] = (color as Color).b;

nx1 = e[0] * normals[v1].x + e[3] * normals[v1].y + e[6] * normals[v1].z;
nx2 = e[0] * normals[v2].x + e[3] * normals[v2].y + e[6] * normals[v2].z;
Expand Down Expand Up @@ -569,24 +574,24 @@ export namespace GLDraw {

/** Create a cone
* @memberof GLDraw
* @param {geometry}
* @param {Geometry}
* geo
* @param {Point}
* from
* @param {Point}
* to
* @param {float}
* @param {number}
* radius
* @param {Color}
* color
* */
export function drawCone (geo: Geometry, from, to, radius: number, color?) {
export function drawCone (geo: Geometry, from: any, to: any, radius: number, color?: Color) {
if (!from || !to)
return;

//TODO: check if from and to do not contain x,y,z and if so generate a center based on the passed selections
// TODO: check if from and to do not contain x,y,z and if so generate a center based on the passed selections

color = color || { r: 0, g: 0, b: 0 };
color = color || ({ r: 0, g: 0, b: 0 } as Color);

let ndir = new Vector3(to.x-from.x, to.y-from.y, to.z-from.z);
var e = getRotationMatrix(ndir.x, ndir.y, ndir.z);
Expand Down Expand Up @@ -679,13 +684,18 @@ export namespace GLDraw {
geoGroup.faceidx += 6 * n;
};

interface MyObject {
vertices: any[];
verticesRows: any[][];
normals: any[];
}

// Sphere component sphereVertexCache
class SphereVertexCache {
private cache = new Map<number, Map<number, any>>(); //sphereQuality then radius
constructor() {}

getVerticesForRadius(radius, sphereQuality) {
getVerticesForRadius(radius: number, sphereQuality: any) {
sphereQuality = sphereQuality || 2;

if (!this.cache.has(sphereQuality)) {
Expand All @@ -695,7 +705,7 @@ export namespace GLDraw {
if (radiusCache.has(radius))
return radiusCache.get(radius);

var obj = {
var obj: MyObject = {
vertices: [],
verticesRows: [],
normals: []
Expand Down Expand Up @@ -753,17 +763,18 @@ export namespace GLDraw {

/** Create a sphere.
* @memberof GLDraw
* @param {geometry}
* @param {Geometry}
* geo
* @param {Point}
* pos
* @param {float}
* @param {number}
* radius
* @param {Color}
* color
* @param {number} quality of sphere (default 2, higher increases number of triangles)
* @param {number}
* sphereQuality - Quality of sphere (default 2, higher increases number of triangles)
*/
export function drawSphere(geo:Geometry, pos, radius, color, sphereQuality?) {
export function drawSphere(geo:Geometry, pos: any, radius: number, color: Colored, sphereQuality?: number) {

var vobj = sphereVertexCache.getVerticesForRadius(radius, sphereQuality);

Expand All @@ -787,9 +798,9 @@ export namespace GLDraw {
vertexArray[offset + 1] = (v.y + pos.y);
vertexArray[offset + 2] = (v.z + pos.z);

colorArray[offset] = color.r;
colorArray[offset + 1] = color.g;
colorArray[offset + 2] = color.b;
colorArray[offset] = (color as Colored).r;
colorArray[offset + 1] = (color as Colored).g;
colorArray[offset + 2] = (color as Colored).b;

}

Expand Down
43 changes: 29 additions & 14 deletions src/GLModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,31 @@ import { get, deepCopy, extend, getExtent, getAtomProperty, makeFunction, getPro
import { Gradient } from "./Gradient";
import { Parsers } from "./parsers";
import { NetCDFReader } from "netcdfjs"
import { inflate } from "pako"
import { inflate, InflateFunctionOptions, Data } from "pako"
import { AtomSelectionSpec, AtomSpec } from "./specs";
import { GLViewer } from "GLViewer";
import { ArrowSpec } from "GLShape";
import { ParserOptionsSpec } from "./parsers/ParserOptionsSpec";
import { LabelSpec } from "Label";
import { assignBonds } from "./parsers/utils/assignBonds";

function inflateString(str: string | ArrayBuffer): (string | ArrayBuffer) {
let data: Data;

if (typeof str === 'string') {
const encoder = new TextEncoder();
data = encoder.encode(str);
} else {
data = new Uint8Array(str);
}

const inflatedData = inflate(data, {
to: 'string'
} as InflateFunctionOptions & { to: 'string' });

return inflatedData;
}

/**
* GLModel represents a group of related atoms
* @class
Expand Down Expand Up @@ -2646,7 +2663,7 @@ export class GLModel {
viewer.render();
*/

public setCoordinates(str:string|ArrayBuffer, format:string) {
public setCoordinates(str: string | ArrayBuffer, format:string) {
format = format || "";
if (!str)
return []; // leave an empty model
Expand All @@ -2655,9 +2672,7 @@ export class GLModel {
// unzip gzipped files
format = format.replace(/\.gz$/, '');
try {
str = inflate(str, {
to: 'string'
});
str = inflateString(str)
} catch (err) {
console.log(err);
}
Expand Down Expand Up @@ -2745,15 +2760,15 @@ export class GLModel {
return values;
};

static parseMolData(data?, format: string="", options?) {
static parseMolData(data?: string | ArrayBuffer, format: string = "", options?: ParserOptionsSpec) {
if (!data)
return []; //leave an empty model

if (/\.gz$/.test(format)) {
//unzip gzipped files
format = format.replace(/\.gz$/, '');
try {
data = inflate(data, { to: 'string' });
data = inflateString(data);
} catch (err) {
console.log(err);
}
Expand All @@ -2767,17 +2782,17 @@ export class GLModel {
// try to guess correct format from data contents
if (data instanceof Uint8Array) {
format = "mmtf"; //currently only supported binary format?
} else if (data.match(/^@<TRIPOS>MOLECULE/gm)) {
} else if ((data as string).match(/^@<TRIPOS>MOLECULE/gm)) {
format = "mol2";
} else if (data.match(/^data_/gm) && data.match(/^loop_/gm)) {
} else if ((data as string).match(/^data_/gm) && (data as string).match(/^loop_/gm)) {
format = "cif";
} else if (data.match(/^HETATM/gm) || data.match(/^ATOM/gm)) {
} else if ((data as string).match(/^HETATM/gm) || (data as string).match(/^ATOM/gm)) {
format = "pdb";
} else if (data.match(/ITEM: TIMESTEP/gm)) {
} else if ((data as string).match(/ITEM: TIMESTEP/gm)) {
format = "lammpstrj";
} else if (data.match(/^.*\n.*\n.\s*(\d+)\s+(\d+)/gm)) {
} else if ((data as string).match(/^.*\n.*\n.\s*(\d+)\s+(\d+)/gm)) {
format = "sdf"; // could look at line 3
} else if (data.match(/^%VERSION\s+VERSION_STAMP/gm)) {
} else if ((data as string).match(/^%VERSION\s+VERSION_STAMP/gm)) {
format = "prmtop";
} else {
format = "xyz";
Expand All @@ -2786,7 +2801,7 @@ export class GLModel {
}
}
var parse = Parsers[format];
var parsedAtoms = parse(data, options);
var parsedAtoms = parse((data as string), options);

return parsedAtoms;
};
Expand Down
8 changes: 4 additions & 4 deletions src/GLShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Sphere, Cylinder, Triangle } from "./WebGL/shapes";
import { Vector3, XYZ } from "./WebGL/math";
import { clamp } from "./WebGL/math";
import { DoubleSide } from "./WebGL";
import { Color, CC, ColorSpec } from "./colors";
import { Color, CC, ColorSpec, Colored } from "./colors";
import { MarchingCube } from "./ProteinSurface4";
import { VolumeData } from "./VolumeData";
import { MeshDoubleLambertMaterial, MeshLambertMaterial, Object3D, Coloring, Mesh, LineBasicMaterial, Line, LineStyle } from "./WebGL";
Expand Down Expand Up @@ -748,9 +748,9 @@ export class GLShape {
sphereSpec.color = CC.color(sphereSpec.color);

this.intersectionShape.sphere.push(new Sphere(sphereSpec.center, sphereSpec.radius));

GLDraw.drawSphere(this.geo, sphereSpec.center,
sphereSpec.radius, sphereSpec.color, sphereSpec.quality);
GLDraw.drawSphere(this.geo, sphereSpec.center,
sphereSpec.radius, sphereSpec.color as Colored, sphereSpec.quality);

this.components.push({
centroid: new Vector3(sphereSpec.center.x,
Expand Down
Loading

0 comments on commit 657c2a2

Please sign in to comment.