📄 geometrytools.java
字号:
/* * $RCSfile$ * $Author: egonw $ * $Date: 2007-08-28 11:23:21 +0200 (Tue, 28 Aug 2007) $ * $Revision: 8737 $ * * Copyright (C) 1997-2007 The Chemistry Development Kit (CDK) project * * Contact: cdk-devel@lists.sourceforge.net * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 * of the License, or (at your option) any later version. * All we ask is that proper credit is given for our work, which includes * - but is not limited to - adding the above copyright notice to the beginning * of your source code files, and to any copyright notice that you may distribute * with programs based on this work. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * */package org.openscience.cdk.geometry;import org.openscience.cdk.interfaces.*;import org.openscience.cdk.tools.LoggingTool;import org.openscience.cdk.tools.manipulator.ChemModelManipulator;import javax.vecmath.Point2d;import javax.vecmath.Vector2d;import java.awt.*;import java.util.HashMap;import java.util.Iterator;import java.util.List;/** * A set of static utility classes for geometric calculations and operations. * This class is extensively used, for example, by JChemPaint to edit molecule. * All methods in this class use an external set of coordinates (e. g. from the rendererModel). If you want to change the coordinates in the atoms, use GeometryToolsInternalCoordinates. * * @author Christopn Steinbeck * @author seb * @author Stefan Kuhn * @author Egon Willighagen * @author Ludovic Petain * @author Christian Hoppe * * @cdk.module standard */public class GeometryTools { private static LoggingTool logger = new LoggingTool(GeometryTools.class); /** * Adds an automatically calculated offset to the coordinates of all atoms * such that all coordinates are positive and the smallest x or y coordinate * is exactly zero, using an external set of coordinates. * See comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap renderingCoordinates) for details on coordinate sets * *@param atomCon AtomContainer for which all the atoms are translated to * positive coordinates *@param renderingCoordinates The set of coordinates to use coming from RendererModel2D */ public static void translateAllPositive(IAtomContainer atomCon,HashMap renderingCoordinates) { double minX = Double.MAX_VALUE; double minY = Double.MAX_VALUE; java.util.Iterator atoms = atomCon.atoms(); while (atoms.hasNext()) { IAtom atom = (IAtom)atoms.next(); if (renderingCoordinates.get(atom) == null && atom.getPoint2d()!=null) { renderingCoordinates.put(atom,new Point2d(atom.getPoint2d().x,atom.getPoint2d().y)); } if (renderingCoordinates.get(atom) != null) { if (((Point2d)renderingCoordinates.get(atom)).x < minX) { minX = ((Point2d)renderingCoordinates.get(atom)).x; } if (((Point2d)renderingCoordinates.get(atom)).y < minY) { minY = ((Point2d)renderingCoordinates.get(atom)).y; } } } logger.debug("Translating: minx=" + minX + ", minY=" + minY); translate2D(atomCon, minX * -1, minY * -1, renderingCoordinates); } /** * Translates the given molecule by the given Vector, using an external set of coordinates. * See comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap renderingCoordinates) for details on coordinate sets * *@param atomCon The molecule to be translated *@param transX translation in x direction *@param transY translation in y direction *@param renderingCoordinates The set of coordinates to use coming from RendererModel2D */ public static void translate2D(IAtomContainer atomCon, double transX, double transY,HashMap renderingCoordinates) { translate2D(atomCon, new Vector2d(transX, transY), renderingCoordinates); } /** * Multiplies all the coordinates of the atoms of the given molecule with the * scalefactor, using an external set of coordinates.. * See comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap renderingCoordinates) for details on coordinate sets * *@param atomCon The molecule to be scaled *@param scaleFactor Description of the Parameter *@param renderingCoordinates The set of coordinates to use coming from RendererModel2D */ public static void scaleMolecule(IAtomContainer atomCon, double scaleFactor, HashMap renderingCoordinates) { for (int i = 0; i < atomCon.getAtomCount(); i++) { if(renderingCoordinates.get(atomCon.getAtom(i))!=null){ ((Point2d)renderingCoordinates.get(atomCon.getAtom(i))).x *= scaleFactor; ((Point2d)renderingCoordinates.get(atomCon.getAtom(i))).y *= scaleFactor; } } } /** * Scales a molecule such that it fills a given percentage of a given * dimension, using an external set of coordinates * See comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap renderingCoordinates) for details on coordinate sets * *@param atomCon The molecule to be scaled *@param areaDim The dimension to be filled *@param fillFactor The percentage of the dimension to be filled *@param renderingCoordinates The set of coordinates to use coming from RendererModel2D */ public static void scaleMolecule(IAtomContainer atomCon, Dimension areaDim, double fillFactor, HashMap renderingCoordinates) { Dimension molDim = get2DDimension(atomCon, renderingCoordinates); double widthFactor = (double) areaDim.width / (double) molDim.width; double heightFactor = (double) areaDim.height / (double) molDim.height; double scaleFactor = Math.min(widthFactor, heightFactor) * fillFactor; scaleMolecule(atomCon, scaleFactor, renderingCoordinates); } /** * Returns the java.awt.Dimension of a molecule * See comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap renderingCoordinates) for details on coordinate sets * *@param atomCon of which the dimension should be returned *@return The java.awt.Dimension of this molecule */ public static Dimension get2DDimension(IAtomContainer atomCon, HashMap renderingCoordinates) { double[] minmax = getMinMax(atomCon, renderingCoordinates); double maxX = minmax[2]; double maxY = minmax[3]; double minX = minmax[0]; double minY = minmax[1]; return new Dimension((int) (maxX - minX + 1), (int) (maxY - minY + 1)); } /** * Returns the java.awt.Dimension of a MoleculeSet * See comment for center(IMoleculeSet setOfMolecules, Dimension areaDim, HashMap renderingCoordinates) for details on coordinate sets * *@param setOfMolecules Of which the dimension should be returned *@return The java.awt.Dimension of this MoleculeSet */ public static Dimension get2DDimension(IMoleculeSet setOfMolecules, HashMap renderingCoordinates) { double[] minmax = getMinMax(setOfMolecules, renderingCoordinates); double maxX = minmax[2]; double maxY = minmax[3]; double minX = minmax[0]; double minY = minmax[1]; return new Dimension((int) (maxX - minX + 1), (int) (maxY - minY + 1)); } /** * Returns the minimum and maximum X and Y coordinates of the atoms in the * AtomContainer. The output is returned as: <pre> * minmax[0] = minX; * minmax[1] = minY; * minmax[2] = maxX; * minmax[3] = maxY; * </pre> * See comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap renderingCoordinates) for details on coordinate sets * *@param container Description of the Parameter *@return An four int array as defined above. */ public static double[] getMinMax(IAtomContainer container, HashMap renderingCoordinates) { double maxX = Double.MIN_VALUE; double maxY = Double.MIN_VALUE; double minX = Double.MAX_VALUE; double minY = Double.MAX_VALUE; for (int i = 0; i < container.getAtomCount(); i++) { IAtom atom = container.getAtom(i); if (renderingCoordinates.get(atom) != null) { if (((Point2d)renderingCoordinates.get(atom)).x > maxX) { maxX = ((Point2d)renderingCoordinates.get(atom)).x; } if (((Point2d)renderingCoordinates.get(atom)).x < minX) { minX = ((Point2d)renderingCoordinates.get(atom)).x; } if (((Point2d)renderingCoordinates.get(atom)).y > maxY) { maxY = ((Point2d)renderingCoordinates.get(atom)).y; } if (((Point2d)renderingCoordinates.get(atom)).y < minY) { minY = ((Point2d)renderingCoordinates.get(atom)).y; } } } double[] minmax = new double[4]; minmax[0] = minX; minmax[1] = minY; minmax[2] = maxX; minmax[3] = maxY; return minmax; } /** * Returns the minimum and maximum X and Y coordinates of the molecules in the * MoleculeSet. The output is returned as: <pre> * minmax[0] = minX; * minmax[1] = minY; * minmax[2] = maxX; * minmax[3] = maxY; * </pre> * See comment for center(IMoleculeSet setOfMolecules, Dimension areaDim, HashMap renderingCoordinates) for details on coordinate sets * *@return An four int array as defined above. */ public static double[] getMinMax(IMoleculeSet setOfMolecules, HashMap renderingCoordinates) { double maxX = Double.MIN_VALUE; double maxY = Double.MIN_VALUE; double minX = Double.MAX_VALUE; double minY = Double.MAX_VALUE; for(int j = 0; j < setOfMolecules.getAtomContainerCount() ; j++){ IAtomContainer container = setOfMolecules.getAtomContainer(j); for (int i = 0; i < container.getAtomCount(); i++) { IAtom atom = container.getAtom(i); if (renderingCoordinates.get(atom) != null) { if (((Point2d)renderingCoordinates.get(atom)).x > maxX) { maxX = ((Point2d)renderingCoordinates.get(atom)).x; } if (((Point2d)renderingCoordinates.get(atom)).x < minX) { minX = ((Point2d)renderingCoordinates.get(atom)).x; } if (((Point2d)renderingCoordinates.get(atom)).y > maxY) { maxY = ((Point2d)renderingCoordinates.get(atom)).y; } if (((Point2d)renderingCoordinates.get(atom)).y < minY) { minY = ((Point2d)renderingCoordinates.get(atom)).y; } } } } double[] minmax = new double[4]; minmax[0] = minX;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -