Skip to content

Commit

Permalink
Dashed bonds for bond order < 1 3dmol#639
Browse files Browse the repository at this point in the history
  • Loading branch information
dxdc authored Feb 1, 2023
1 parent be34986 commit af9334b
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions src/GLModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,30 @@ export class GLModel {
geoGroup.faceidx += 6;
};

static drawDashedStickImposter(geo: Geometry, from: XYZ, to: XYZ, radius: number, color: Color, dashLength: number, gapLength: number) {
dashLength = dashLength || 0.1;
gapLength = gapLength || 0.25;

var cylinderLength = Math.sqrt(Math.pow((from.x - to.x), 2) + Math.pow((from.y - to.y), 2) + Math.pow((from.z - to.z), 2));
var numDashes = Math.floor(cylinderLength / (gapLength + dashLength)) + 1;
var numGaps = numDashes - 1;

gapLength = (cylinderLength - (numDashes * dashLength)) / numGaps;

var new_from = new $3Dmol.Vector3(from.x, from.y, from.z);
var new_to = new $3Dmol.Vector3(to.x, to.y, to.z);

var gapVector = new $3Dmol.Vector3((to.x - from.x) / (cylinderLength / gapLength), (to.y - from.y) / (cylinderLength / gapLength), (to.z - from.z) / (cylinderLength / gapLength));
var dashVector = new $3Dmol.Vector3((to.x - from.x) / (cylinderLength / dashLength), (to.y - from.y) / (cylinderLength / dashLength), (to.z - from.z) / (cylinderLength / dashLength));

for (var place = 0; place < numDashes + numGaps; place++) {
new_to = new $3Dmol.Vector3(new_from.x + dashVector.x, new_from.y + dashVector.y, new_from.z + dashVector.z);
// this.intersectionShape.cylinder.push(new $3Dmol.Cylinder(new_from, new_to, radius));
GLModel.drawStickImposter(geo, new_from, new_to, radius, color);
new_from = new $3Dmol.Vector3(new_to.x + gapVector.x, new_to.y + gapVector.y, new_to.z + gapVector.z);
}
};

// draws cylinders and small spheres (at bond radius)
private drawBondSticks(atom: AtomSpec, atoms: AtomSpec[], geo: Geometry) {
if (!atom.style.stick)
Expand All @@ -728,10 +752,11 @@ export class GLModel {
if (!atom.capDrawn && atom.bonds.length < 4)
fromCap = 2;

var drawCyl = GLDraw.drawCylinder; //mesh cylinder
if (geo.imposter)
drawCyl = GLModel.drawStickImposter;

var drawCyl = function (bondOrder) {
if (geo.imposter) return (bondOrder < 1) ? GLModel.drawDashedStickImposter : GLModel.drawStickImposter;
return GLDraw.drawDashedCylinder;
}
};

for (i = 0; i < atom.bonds.length; i++) {
var j = atom.bonds[i]; // our neighbor
Expand Down Expand Up @@ -777,10 +802,10 @@ export class GLModel {
if (C1 != C2) {
mp = new Vector3().addVectors(p1, p2)
.multiplyScalar(0.5);
drawCyl(geo, p1, mp, bondR, C1, fromCap, 0);
drawCyl(geo, mp, p2, bondR, C2, 0, toCap);
drawCyl(atom.bondOrder[i])(geo, p1, mp, bondR, C1, fromCap, 0);
drawCyl(atom.bondOrder[i])(geo, mp, p2, bondR, C2, 0, toCap);
} else {
drawCyl(geo, p1, p2, bondR, C1, fromCap, toCap);
drawCyl(atom.bondOrder[i])(geo, p1, p2, bondR, C1, fromCap, toCap);
}


Expand Down Expand Up @@ -840,13 +865,13 @@ export class GLModel {
.multiplyScalar(0.5);
mp2 = new Vector3().addVectors(p1b, p2b)
.multiplyScalar(0.5);
drawCyl(geo, p1a, mp, r, C1, mfromCap, 0);
drawCyl(geo, mp, p2a, r, C2, 0, mtoCap);
drawCyl(geo, p1b, mp2, r, C1, mfromCap, 0);
drawCyl(geo, mp2, p2b, r, C2, 0, mtoCap);
drawCyl(atom.bondOrder[i])(geo, p1a, mp, r, C1, mfromCap, 0);
drawCyl(atom.bondOrder[i])(geo, mp, p2a, r, C2, 0, mtoCap);
drawCyl(atom.bondOrder[i])(geo, p1b, mp2, r, C1, mfromCap, 0);
drawCyl(atom.bondOrder[i])(geo, mp2, p2b, r, C2, 0, mtoCap);
} else {
drawCyl(geo, p1a, p2a, r, C1, mfromCap, mtoCap);
drawCyl(geo, p1b, p2b, r, C1, mfromCap, mtoCap);
drawCyl(atom.bondOrder[i])(geo, p1a, p2a, r, C1, mfromCap, mtoCap);
drawCyl(atom.bondOrder[i])(geo, p1b, p2b, r, C1, mfromCap, mtoCap);
}

atomneedsi = atom.clickable || atom.hoverable;
Expand Down Expand Up @@ -894,16 +919,16 @@ export class GLModel {
.multiplyScalar(0.5);
mp3 = new Vector3().addVectors(p1, p2)
.multiplyScalar(0.5);
drawCyl(geo, p1a, mp, r, C1, mfromCap, 0);
drawCyl(geo, mp, p2a, r, C2, 0, mtoCap);
drawCyl(geo, p1, mp3, r, C1, fromCap, 0);
drawCyl(geo, mp3, p2, r, C2, 0, toCap);
drawCyl(geo, p1b, mp2, r, C1, mfromCap, 0);
drawCyl(geo, mp2, p2b, r, C2, 0, mtoCap);
drawCyl(atom.bondOrder[i])(geo, p1a, mp, r, C1, mfromCap, 0);
drawCyl(atom.bondOrder[i])(geo, mp, p2a, r, C2, 0, mtoCap);
drawCyl(atom.bondOrder[i])(geo, p1, mp3, r, C1, fromCap, 0);
drawCyl(atom.bondOrder[i])(geo, mp3, p2, r, C2, 0, toCap);
drawCyl(atom.bondOrder[i])(geo, p1b, mp2, r, C1, mfromCap, 0);
drawCyl(atom.bondOrder[i])(geo, mp2, p2b, r, C2, 0, mtoCap);
} else {
drawCyl(geo, p1a, p2a, r, C1, mfromCap, mtoCap);
drawCyl(geo, p1, p2, r, C1, fromCap, toCap);
drawCyl(geo, p1b, p2b, r, C1, mfromCap, mtoCap);
drawCyl(atom.bondOrder[i])(geo, p1a, p2a, r, C1, mfromCap, mtoCap);
drawCyl(atom.bondOrder[i])(geo, p1, p2, r, C1, fromCap, toCap);
drawCyl(atom.bondOrder[i])(geo, p1b, p2b, r, C1, mfromCap, mtoCap);
}

atomneedsi = atom.clickable || atom.hoverable;
Expand Down Expand Up @@ -2895,4 +2920,4 @@ export interface BondStyle {
color1?: ColorSpec;
/** */
color2?: ColorSpec;
}
}

0 comments on commit af9334b

Please sign in to comment.