com.actelion.research.chem.Coordinates Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openchemlib Show documentation
Show all versions of openchemlib Show documentation
Open Source Chemistry Library
/*
* Copyright (c) 1997 - 2016
* Actelion Pharmaceuticals Ltd.
* Gewerbestrasse 16
* CH-4123 Allschwil, Switzerland
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package com.actelion.research.chem;
import com.actelion.research.chem.conf.Coordinate;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Random;
/**
* Class to encapsulate 3D coordinates
*/
public final class Coordinates implements Serializable, Comparable {
public double x,y,z;
public Coordinates() {}
public Coordinates(Coordinates c) {
this(c.x, c.y, c.z);
}
public Coordinates(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Copies x,y,z from c to this
* @param c
*/
public void set(Coordinates c) {
set(c.x, c.y, c.z);
}
public void set(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getLength() {
return dist();
}
public final double dist() {
return Math.sqrt(distSq());
}
public final double distSq() {
return x*x + y*y + z*z;
}
public final double distanceSquared(Coordinates c) {
return (c.x-x)*(c.x-x) + (c.y-y)*(c.y-y) + (c.z-z)*(c.z-z);
}
public final double distSquareTo(Coordinates c) {
return distanceSquared(c);
}
public final double distance(Coordinates c) {
return Math.sqrt(distanceSquared(c));
}
public final double dot(Coordinates c) {
return x*c.x + y*c.y + z*c.z;
}
public final Coordinates cross(Coordinates c) {
return new Coordinates(y*c.z-z*c.y, -(x*c.z-z*c.x), x*c.y-y*c.x);
}
/**
* Gets the angle formed between the 2 vectors ([0,PI])
* @param c
* @return angle in radian
*/
public final double getAngle(Coordinates c) {
double d1 = distSq();
double d2 = c.distSq();
if(d1==0 || d2==0) return 0;
double d = dot(c) / Math.sqrt(d1*d2);
if(d>=1) return 0;
if(d<=-1) return Math.PI;
return Math.acos(d);
}
/**
* Calculates the angle of the line from this location to c
* projected into the x/y plane. With Y facing upwards and X right,
* if the line points in Y direction, ten angle is 0.0 increasing
* in clockwise direction.
* @param c
* @return -PI < angle < PI
*/
public double getAngleXY(Coordinate c) {
double dx = c.x - x;
double dy = c.y - y;
if (dy == 0.0)
return (dx > 0.0) ? Math.PI/2.0 : -Math.PI/2.0;
double angle = Math.atan(dx/dy);
if (dy < 0.0)
return (dx < 0.0) ? angle - Math.PI : angle + Math.PI;
return angle;
}
public final double getDihedral(Coordinates c2, Coordinates c3, Coordinates c4) {
return getDihedral(this, c2, c3, c4);
}
public final Coordinates subC(Coordinates c) {
return new Coordinates(x-c.x, y-c.y, z-c.z);
}
public final Coordinates addC(Coordinates c) {
return new Coordinates(x+c.x, y+c.y, z+c.z);
}
public final Coordinates scaleC(double scale) {
return new Coordinates(x*scale, y*scale, z*scale);
}
public final void sub(Coordinates c) {
x-=c.x;
y-=c.y;
z-=c.z;
}
public final void add(Coordinates c) {
x+=c.x;
y+=c.y;
z+=c.z;
}
public final void scale(double scale) {
x*=scale;
y*=scale;
z*=scale;
}
public final void negate() {
x=-x;
y=-y;
z=-z;
}
public final Coordinates rotate(Coordinates normal, double theta) {
if(Math.abs(normal.x*normal.x+normal.y*normal.y+normal.z*normal.z-1)>1E-6) throw new IllegalArgumentException("normal needs to a unit vector: "+normal);
double x = normal.x;
double y = normal.y;
double z = normal.z;
double c = Math.cos(theta);
double s = Math.sin(theta);
double t = 1-c;
Coordinates opp = new Coordinates(
(t*x*x+c)*this.x + (t*x*y+s*z)*this.y + (t*x*z-s*y)*this.z,
(t*x*y-s*z)*this.x + (t*y*y+c)*this.y + (t*y*z+s*x)*this.z,
(t*x*z+s*y)*this.x + (t*z*y-s*x)*this.y + (t*z*z+c)*this.z
);
return opp;
}
public final Coordinates unitC() {
double d = dist();
if(d==0) {
System.err.println("Cannot call unitC() on a null vector");
// Thread.dumpStack();
return new Coordinates(1,0,0);
}
return new Coordinates(x/d, y/d, z/d);
}
public final void unit() {
double d = dist();
if(d==0) {
System.err.println("Cannot call unit() on a null vector. Returned (1,0,0)");
// Thread.dumpStack();
x = 1;
y = 0;
z = 0;
// throw new IllegalArgumentException("Cannot call unit() on a null vector.");
return;
}
x/=d;
y/=d;
z/=d;
}
public final boolean insideBounds(Coordinates[] bounds) {
return bounds!=null && bounds[0].x<=x && x<=bounds[1].x && bounds[0].y<=y && y<=bounds[1].y && bounds[0].z<=z && z<=bounds[1].z;
}
@Override
public final String toString() {
DecimalFormat df = new DecimalFormat("0.00");
return "[" + df.format(x) + ", " + df.format(y) + ", " + df.format(z) + "]";
}
public final String toStringSpaceDelimited() {
DecimalFormat df = new DecimalFormat("0.00");
return df.format(x) + " " + df.format(y) + " " + df.format(z);
}
@Override
public final boolean equals(Object o) {
if(o==null || !(o instanceof Coordinates)) return false;
Coordinates c = (Coordinates) o;
return Math.abs(c.x-x) + Math.abs(c.y-y) + Math.abs(c.z-z) < 1E-6;
}
public final boolean isNaN() {
return Double.isNaN(x) || Double.isNaN(y) || Double.isNaN(z);
}
public final Coordinates min(Coordinates c) {
return new Coordinates(Math.min(x, c.x), Math.min(y, c.y), Math.min(z, c.z));
}
public final Coordinates max(Coordinates c) {
return new Coordinates(Math.max(x, c.x), Math.max(y, c.y), Math.max(z, c.z));
}
public double cosAngle(Coordinates c) {
double d = dist() * c.dist();
if(d<=0) return 0;
return dot(c) / d;
}
/////////////////// UTILITITIES ///////////////////////////////////////
public static final Coordinates min(Coordinates[] c) {
Coordinates min = new Coordinates(c[0]);
for (int i = 1; i < c.length; i++) {
min.x = Math.min(c[i].x, min.x);
min.y = Math.min(c[i].y, min.y);
min.z = Math.min(c[i].z, min.z);
}
return min;
}
public static final Coordinates max(Coordinates[] c) {
Coordinates max = new Coordinates(c[0]);
for (int i = 1; i < c.length; i++) {
max.x = Math.max(c[i].x, max.x);
max.y = Math.max(c[i].y, max.y);
max.z = Math.max(c[i].z, max.z);
}
return max;
}
public static final Coordinates createBarycenter(Coordinates... coords) {
if(coords==null) throw new IllegalArgumentException("The coordinates are null");
Coordinates res = new Coordinates();
for(int i=0; i0? Math.sqrt(sum/l): 0;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy