Skip to content

Commit

Permalink
Merge pull request #716 from adithyaakrishna/revert-703-feat/more-typ…
Browse files Browse the repository at this point in the history
…eSafe

Revert "Added More Types for Parsers and WebGL and GL Files"
  • Loading branch information
dkoes authored Aug 31, 2023
2 parents 6f7cca7 + 01fbd93 commit 8cd97e0
Show file tree
Hide file tree
Showing 24 changed files with 179 additions and 202 deletions.
17 changes: 5 additions & 12 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@
"dependencies": {
"clean-jsdoc-theme": "^4.2.9",
"iobuffer": "^5.3.1",
"netcdfjs": "^3.0.0",
"netcdfjs": "^2.0.2",
"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: 38 additions & 49 deletions src/GLDraw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Vector3 } from "./WebGL/math";
import { Geometry } from "./WebGL";
import { Color, Colored } from "colors";


//define enum values
Expand All @@ -18,11 +17,6 @@ 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 @@ -130,7 +124,7 @@ export namespace GLDraw {
this.basisVectors = nvecs;
};

getVerticesForRadius(radius: any, cap: CAP, capType: any) {
getVerticesForRadius(radius, cap, capType) {
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 @@ -278,30 +272,31 @@ export namespace GLDraw {

var cylVertexCache = new CylVertexCache();


/**
* Create a cylinder
* @memberof GLDraw
* @param {Geometry}
* @param {geometry}
* geo
* @param {Point}
* from
* @param {Point}
* to
* @param {number}
* @param {float}
* 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: any, to: any, radius: number, color: Color | Color[], fromCap:CAP = 0, toCap:CAP = 0) {
export function drawCylinder(geo: Geometry, from, to, radius: number, color, fromCap:CAP = 0, toCap:CAP = 0) {
if (!from || !to)
return;

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

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

// colors
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;
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;

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

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] = color.r;
colorArray[v2offset] = color.r;
colorArray[v3offset] = color.r;
colorArray[v4offset] = color.r;

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 + 1] = color.g;
colorArray[v2offset + 1] = color.g;
colorArray[v3offset + 1] = color.g;
colorArray[v4offset + 1] = color.g;

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;
colorArray[v1offset + 2] = color.b;
colorArray[v2offset + 2] = color.b;
colorArray[v3offset + 2] = color.b;
colorArray[v4offset + 2] = 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 @@ -574,24 +569,24 @@ export namespace GLDraw {

/** Create a cone
* @memberof GLDraw
* @param {Geometry}
* @param {geometry}
* geo
* @param {Point}
* from
* @param {Point}
* to
* @param {number}
* @param {float}
* radius
* @param {Color}
* color
* */
export function drawCone (geo: Geometry, from: any, to: any, radius: number, color?: Color) {
export function drawCone (geo: Geometry, from, to, radius: number, 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 } as Color);
color = color || { r: 0, g: 0, b: 0 };

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 @@ -684,18 +679,13 @@ 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: number, sphereQuality: any) {
getVerticesForRadius(radius, sphereQuality) {
sphereQuality = sphereQuality || 2;

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

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

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

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

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

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

}

Expand Down
43 changes: 14 additions & 29 deletions src/GLModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,14 @@ import { get, deepCopy, extend, getExtent, getAtomProperty, makeFunction, getPro
import { Gradient } from "./Gradient";
import { Parsers } from "./parsers";
import { NetCDFReader } from "netcdfjs"
import { inflate, InflateFunctionOptions, Data } from "pako"
import { inflate } 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 @@ -2663,7 +2646,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 @@ -2672,7 +2655,9 @@ export class GLModel {
// unzip gzipped files
format = format.replace(/\.gz$/, '');
try {
str = inflateString(str)
str = inflate(str, {
to: 'string'
});
} catch (err) {
console.log(err);
}
Expand Down Expand Up @@ -2760,15 +2745,15 @@ export class GLModel {
return values;
};

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

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

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

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

0 comments on commit 8cd97e0

Please sign in to comment.