Skip to content

Commit 951d30d

Browse files
committed
When generating coords, sort large => small and lay out left to right.
1 parent 1650542 commit 951d30d

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

tool/sdg/src/main/java/org/openscience/cdk/layout/StructureDiagramGenerator.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,20 @@
108108
*/
109109
public class StructureDiagramGenerator {
110110

111-
static final double DEFAULT_BOND_LENGTH = 1.5;
112-
private static final Vector2d DEFAULT_BOND_VECTOR = new Vector2d(0, 1);
113-
private static final IdentityTemplateLibrary DEFAULT_TEMPLATE_LIBRARY = IdentityTemplateLibrary.loadFromResource("custom-templates.smi")
111+
static final double DEFAULT_BOND_LENGTH = 1.5;
112+
private static final Vector2d DEFAULT_BOND_VECTOR = new Vector2d(0, 1);
113+
private static final IdentityTemplateLibrary DEFAULT_TEMPLATE_LIBRARY = IdentityTemplateLibrary.loadFromResource("custom-templates.smi")
114114
.add(IdentityTemplateLibrary.loadFromResource("chebi-ring-templates.smi"));
115-
private static final double RAD_30 = Math.toRadians(-30);
115+
private static final double RAD_30 = Math.toRadians(-30);
116116
private static final ILoggingTool logger = LoggingToolFactory.createLoggingTool(StructureDiagramGenerator.class);
117117

118+
public static final Comparator<IAtomContainer> LARGEST_FIRST_COMPARATOR = new Comparator<IAtomContainer>() {
119+
@Override
120+
public int compare(IAtomContainer o1, IAtomContainer o2) {
121+
return Integer.compare(o2.getBondCount(), o1.getBondCount());
122+
}
123+
};
124+
118125
private IAtomContainer molecule;
119126
private IRingSet sssr;
120127
private final double bondLength = DEFAULT_BOND_LENGTH;
@@ -637,7 +644,12 @@ private void generateCoordinates(Vector2d firstBondVector, boolean isConnected,
637644
final IAtomContainerSet frags = ConnectivityChecker.partitionIntoMolecules(molecule);
638645
if (frags.getAtomContainerCount() > 1) {
639646
IAtomContainer rollback = molecule;
640-
generateFragmentCoordinates(molecule, toList(frags));
647+
648+
// large => small (e.g. salt will appear on the right)
649+
List<IAtomContainer> fragList = toList(frags);
650+
Collections.sort(fragList, LARGEST_FIRST_COMPARATOR);
651+
generateFragmentCoordinates(molecule, fragList);
652+
641653
// don't call set molecule as it wipes x,y coordinates!
642654
// this looks like a self assignment but actually the fragment
643655
// method changes this.molecule

0 commit comments

Comments
 (0)