public static void GetSingleBondGeometry(BondLayout descriptor, double standoff) { var start = descriptor.Start; var end = descriptor.End; var sg = new StreamGeometry(); if (descriptor.StartAtomHull != null) { AdjustTerminus(ref start, end, descriptor.StartAtomHull, standoff); } if (descriptor.EndAtomHull != null) { AdjustTerminus(ref end, start, descriptor.EndAtomHull, standoff); } using (var sgc = sg.Open()) { sgc.BeginFigure(start, false, false); sgc.LineTo(end, true, false); sgc.Close(); } sg.Freeze(); descriptor.DefiningGeometry = sg; }
public static BondLayout GetBondDescriptor(AtomVisual startAtomVisual, AtomVisual endAtomVisual, double modelXamlBondLength, Globals.BondStereo parentStereo, Point startAtomPosition, Point endAtomPosition, double?parentOrderValue, Globals.BondDirection parentPlacement, Point?centroid, Point?secondaryCentroid) { if (parentStereo == Globals.BondStereo.Wedge || parentStereo == Globals.BondStereo.Hatch) { WedgeBondLayout wbd = new WedgeBondLayout() { Start = startAtomPosition, End = endAtomPosition, StartAtomVisual = startAtomVisual, EndAtomVisual = endAtomVisual }; var endAtom = endAtomVisual.ParentAtom; var otherBonds = endAtom.Bonds.Except(new[] { startAtomVisual.ParentAtom.BondBetween(endAtom) }).ToList(); Bond bond = null; if (otherBonds.Any()) { bond = otherBonds.ToArray()[0]; } bool chamferBond = (otherBonds.Any() && (endAtom.Element as Element) == Globals.PeriodicTable.C && endAtom.SymbolText == "" && bond.Order == Globals.OrderSingle); if (!chamferBond) { wbd.CappedOff = false; BondGeometry.GetWedgeBondGeometry(wbd, modelXamlBondLength); } else { var nonHPs = (from b in otherBonds select b.OtherAtom(endAtom).Position).ToList(); if (nonHPs.Any()) { wbd.CappedOff = true; BondGeometry.GetChamferedWedgeGeometry(wbd, modelXamlBondLength, nonHPs); } else { wbd.CappedOff = false; BondGeometry.GetWedgeBondGeometry(wbd, modelXamlBondLength); } } return(wbd); } //wavy bond if (parentStereo == Globals.BondStereo.Indeterminate && parentOrderValue == 1.0) { BondLayout sbd = new BondLayout { Start = startAtomPosition, End = endAtomPosition, StartAtomVisual = startAtomVisual, EndAtomVisual = endAtomVisual }; BondGeometry.GetWavyBondGeometry(sbd, modelXamlBondLength); return(sbd); } switch (parentOrderValue) { //indeterminate double case 2 when parentStereo == Globals.BondStereo.Indeterminate: DoubleBondLayout dbd = new DoubleBondLayout() { StartAtomVisual = startAtomVisual, EndAtomVisual = endAtomVisual, Start = startAtomPosition, End = endAtomPosition }; BondGeometry.GetCrossedDoubleGeometry(dbd, modelXamlBondLength); return(dbd); //partial or undefined bonds case 0: case 0.5: case 1.0: BondLayout sbd = new BondLayout { Start = startAtomPosition, End = endAtomPosition, StartAtomVisual = startAtomVisual, EndAtomVisual = endAtomVisual }; BondGeometry.GetSingleBondGeometry(sbd); return(sbd); //double bond & 1.5 bond case 1.5: case 2: DoubleBondLayout dbd2 = new DoubleBondLayout() { StartAtomVisual = startAtomVisual, EndAtomVisual = endAtomVisual, Start = startAtomPosition, End = endAtomPosition, Placement = parentPlacement, PrimaryCentroid = centroid, SecondaryCentroid = secondaryCentroid }; BondGeometry.GetDoubleBondGeometry(dbd2, modelXamlBondLength); return(dbd2); //triple and 2.5 bond case 2.5: case 3: TripleBondLayout tbd = new TripleBondLayout() { StartAtomVisual = startAtomVisual, EndAtomVisual = endAtomVisual, Start = startAtomPosition, End = endAtomPosition, Placement = parentPlacement, PrimaryCentroid = centroid, SecondaryCentroid = secondaryCentroid }; BondGeometry.GetTripleBondGeometry(tbd, modelXamlBondLength); return(tbd); default: return(null); } }
/// <summary> /// Quite ghastly routine to draw a wiggly bond /// </summary> /// <param name="descriptor">BondDescriptor which is populated</param> /// <param name="standardBondLength">Standard bond length as defined by the model</param> /// <param name="standoff"></param> public static void GetWavyBondGeometry(BondLayout descriptor, double standardBondLength, double standoff) { var sg = new StreamGeometry(); //first do the adjustment for any atom visuals if (descriptor.StartAtomHull != null) { AdjustTerminus(ref descriptor.Start, descriptor.End, descriptor.StartAtomHull, standoff); } if (descriptor.EndAtomHull != null) { AdjustTerminus(ref descriptor.End, descriptor.Start, descriptor.EndAtomHull, standoff); } //Work out the control points for a quadratic Bezier by sprouting alternately along the bond line Vector halfAWiggle; using (var sgc = sg.Open()) { var bondVector = descriptor.PrincipleVector; //come up with a number of wiggles that looks aesthetically sensible var noOfWiggles = (int)Math.Ceiling(bondVector.Length / (standardBondLength * Globals.BondOffsetPercentage * 2)); if (noOfWiggles < 3) { noOfWiggles = 3; } //now calculate a wiggle vector that is 60 degrees from the bond angle var wiggleLength = bondVector.Length / noOfWiggles; halfAWiggle = bondVector; halfAWiggle.Normalize(); halfAWiggle *= wiggleLength / 2; //work out left and right sprouting vectors var toLeft = new Matrix(); toLeft.Rotate(-60); var toRight = new Matrix(); toRight.Rotate(60); var leftVector = halfAWiggle * toLeft; var rightVector = halfAWiggle * toRight; var allpoints = new List <Point>(); //allpoints holds the control points for the bezier allpoints.Add(descriptor.Start); var lastPoint = descriptor.Start; //move along the bond vector, sprouting control points alternately for (var i = 0; i < noOfWiggles; i++) { var leftPoint = lastPoint + leftVector; allpoints.Add(leftPoint); allpoints.Add(lastPoint + halfAWiggle); var rightPoint = lastPoint + halfAWiggle + rightVector; allpoints.Add(rightPoint); lastPoint += halfAWiggle * 2; allpoints.Add(lastPoint); } allpoints.Add(descriptor.End); BezierFromPoints(sgc, allpoints); sgc.Close(); } //define the boundary descriptor.Boundary.Clear(); descriptor.Boundary.AddRange(new[] { descriptor.Start - halfAWiggle.Perpendicular(), descriptor.End - halfAWiggle.Perpendicular(), descriptor.End + halfAWiggle.Perpendicular(), descriptor.Start + halfAWiggle.Perpendicular() }); sg.Freeze(); descriptor.DefiningGeometry = sg; //local function void BezierFromPoints(StreamGeometryContext sgc, List <Point> allpoints) { sgc.BeginFigure(allpoints[0], false, false); sgc.PolyQuadraticBezierTo(allpoints.Skip(1).ToArray(), true, true); } }
public static BondLayout GetBondDescriptor(AtomVisual startAtomVisual, AtomVisual endAtomVisual, double modelXamlBondLength, Globals.BondStereo parentStereo, Point startAtomPosition, Point endAtomPosition, double?parentOrderValue, Globals.BondDirection parentPlacement, Point?centroid, Point?secondaryCentroid, double standoff) { List <Point> startAtomHull = new List <Point>(); List <Point> endAtomHull = new List <Point>(); if (startAtomVisual.ParentAtom.SymbolText != "" || startAtomVisual.ShowAllCarbons) { startAtomHull = startAtomVisual.Hull; } if (endAtomVisual.ParentAtom.SymbolText != "" || endAtomVisual.ShowAllCarbons) { endAtomHull = endAtomVisual.Hull; } if ((parentStereo == Globals.BondStereo.Wedge || parentStereo == Globals.BondStereo.Hatch) && parentOrderValue == 1) { WedgeBondLayout wbd = new WedgeBondLayout { Start = startAtomPosition, End = endAtomPosition, StartAtomHull = startAtomHull, EndAtomHull = endAtomHull }; var endAtom = endAtomVisual.ParentAtom; var otherBonds = endAtom.Bonds.Except(new[] { startAtomVisual.ParentAtom.BondBetween(endAtom) }).ToList(); Bond bond = null; bool oblique = true; if (otherBonds.Any()) { bond = otherBonds.ToArray()[0]; Vector wedgevector = wbd.End - wbd.Start; foreach (Bond b in otherBonds) { Atom otherAtom = b.OtherAtom(endAtom); Vector v = wbd.End - otherAtom.Position; double angle = System.Math.Abs(Vector.AngleBetween(wedgevector, v)); if (angle < 109.5 || angle > 130.5) { oblique = false; break; } } } bool chamferBond = otherBonds.Any() && oblique && (endAtom.Element as Element) == Globals.PeriodicTable.C && endAtom.SymbolText == "" && bond.Order == Globals.OrderSingle; if (!chamferBond) { wbd.CappedOff = false; BondGeometry.GetWedgeBondGeometry(wbd, modelXamlBondLength, standoff); } else { var nonHPs = (from b in otherBonds select b.OtherAtom(endAtom).Position).ToList(); if (nonHPs.Any()) { wbd.CappedOff = true; BondGeometry.GetChamferedWedgeGeometry(wbd, modelXamlBondLength, nonHPs, standoff); } else { wbd.CappedOff = false; BondGeometry.GetWedgeBondGeometry(wbd, modelXamlBondLength, standoff); } } return(wbd); } //wavy bond if (parentStereo == Globals.BondStereo.Indeterminate && parentOrderValue == 1.0) { BondLayout sbd = new BondLayout { Start = startAtomPosition, End = endAtomPosition, StartAtomHull = startAtomHull, EndAtomHull = endAtomHull }; BondGeometry.GetWavyBondGeometry(sbd, modelXamlBondLength, standoff); return(sbd); } switch (parentOrderValue) { //indeterminate double case 2 when parentStereo == Globals.BondStereo.Indeterminate: DoubleBondLayout dbd = new DoubleBondLayout() { StartAtomHull = startAtomHull, EndAtomHull = endAtomHull, Start = startAtomPosition, End = endAtomPosition, StartNeigbourPositions = (from Atom a in startAtomVisual.ParentAtom.NeighboursExcept(endAtomVisual.ParentAtom) select a.Position).ToList(), EndNeighbourPositions = (from Atom a in endAtomVisual.ParentAtom.NeighboursExcept(startAtomVisual.ParentAtom) select a.Position).ToList() }; BondGeometry.GetCrossedDoubleGeometry(dbd, modelXamlBondLength, standoff); return(dbd); //partial or undefined bonds case 0: case 0.5: case 1.0: BondLayout sbd = new BondLayout { Start = startAtomPosition, End = endAtomPosition, StartAtomHull = startAtomHull, EndAtomHull = endAtomHull }; BondGeometry.GetSingleBondGeometry(sbd, standoff); return(sbd); //double bond & 1.5 bond case 1.5: case 2: DoubleBondLayout dbd2 = new DoubleBondLayout() { StartAtomHull = startAtomHull, EndAtomHull = endAtomHull, Start = startAtomPosition, End = endAtomPosition, Placement = parentPlacement, PrimaryCentroid = centroid, SecondaryCentroid = secondaryCentroid, StartNeigbourPositions = (from Atom a in startAtomVisual.ParentAtom.NeighboursExcept(endAtomVisual.ParentAtom) select a.Position).ToList(), EndNeighbourPositions = (from Atom a in endAtomVisual.ParentAtom.NeighboursExcept(startAtomVisual.ParentAtom) select a.Position).ToList() }; BondGeometry.GetDoubleBondGeometry(dbd2, modelXamlBondLength, standoff); return(dbd2); //triple and 2.5 bond case 2.5: case 3: TripleBondLayout tbd = new TripleBondLayout() { StartAtomHull = startAtomHull, EndAtomHull = endAtomHull, Start = startAtomPosition, End = endAtomPosition, Placement = parentPlacement, PrimaryCentroid = centroid, SecondaryCentroid = secondaryCentroid }; BondGeometry.GetTripleBondGeometry(tbd, modelXamlBondLength, standoff); return(tbd); default: return(null); } }