001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2005, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.referencing.operation.matrix;
018:
019: // J2SE dependencies
020: import java.io.Serializable;
021: import javax.vecmath.SingularMatrixException;
022:
023: // OpenGIS dependencies
024: import org.opengis.referencing.operation.Matrix;
025:
026: // Geotools dependencies
027: import org.geotools.resources.i18n.Errors;
028: import org.geotools.resources.i18n.ErrorKeys;
029:
030: /**
031: * A matrix of fixed {@value #SIZE}×{@value #SIZE} size. This trivial matrix is returned as a
032: * result of {@linkplain org.opengis.referencing.operation.MathTransform1D} derivative computation.
033: *
034: * @since 2.2
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/referencing/operation/matrix/Matrix1.java $
036: * @version $Id: Matrix1.java 24493 2007-02-17 17:28:12Z desruisseaux $
037: * @author Martin Desruisseaux
038: */
039: public class Matrix1 implements XMatrix, Serializable {
040: /** Serial number for interoperability with different versions. */
041: private static final long serialVersionUID = -4829171016106097031L;
042:
043: /** The only element in this matrix. */
044: public double m00;
045:
046: /** The matrix size, which is {@value}. */
047: public static final int SIZE = 1;
048:
049: /**
050: * Creates a new identity matrix.
051: */
052: public Matrix1() {
053: m00 = 1;
054: }
055:
056: /**
057: * Creates a new matrix initialized to the specified value.
058: */
059: public Matrix1(final double m00) {
060: this .m00 = m00;
061: }
062:
063: /**
064: * Creates a new matrix initialized to the same value than the specified one.
065: * The specified matrix size must be {@value #SIZE}×{@value #SIZE}.
066: */
067: public Matrix1(final Matrix matrix) {
068: if (matrix.getNumRow() != SIZE || matrix.getNumCol() != SIZE) {
069: throw new IllegalArgumentException(Errors
070: .format(ErrorKeys.ILLEGAL_MATRIX_SIZE));
071: }
072: m00 = matrix.getElement(0, 0);
073: }
074:
075: /**
076: * Returns the number of rows in this matrix, which is always {@value #SIZE}
077: * in this implementation.
078: */
079: public final int getNumRow() {
080: return SIZE;
081: }
082:
083: /**
084: * Returns the number of colmuns in this matrix, which is always {@value #SIZE}
085: * in this implementation.
086: */
087: public final int getNumCol() {
088: return SIZE;
089: }
090:
091: /**
092: * {@inheritDoc}
093: */
094: public final double getElement(final int row, final int col) {
095: if (row == 0 && col == 0) {
096: return m00;
097: } else {
098: throw new IndexOutOfBoundsException();
099: }
100: }
101:
102: /**
103: * {@inheritDoc}
104: */
105: public final void setElement(final int row, final int col,
106: final double value) {
107: if (row == 0 && col == 0) {
108: m00 = value;
109: } else {
110: throw new IndexOutOfBoundsException();
111: }
112: }
113:
114: /**
115: * {@inheritDoc}
116: */
117: public final void setZero() {
118: m00 = 0;
119: }
120:
121: /**
122: * {@inheritDoc}
123: */
124: public final void setIdentity() {
125: m00 = 1;
126: }
127:
128: /**
129: * {@inheritDoc}
130: */
131: public final boolean isIdentity() {
132: return m00 == 1;
133: }
134:
135: /**
136: * {@inheritDoc}
137: */
138: public final boolean isIdentity(double tolerance) {
139: return Math.abs(m00 - 1) <= Math.abs(tolerance);
140: }
141:
142: /**
143: * {@inheritDoc}
144: */
145: public final boolean isAffine() {
146: return m00 == 1;
147: }
148:
149: /**
150: * {@inheritDoc}
151: */
152: public final void negate() {
153: m00 = -m00;
154: }
155:
156: /**
157: * {@inheritDoc}
158: */
159: public final void transpose() {
160: // Nothing to do for a 1x1 matrix.
161: }
162:
163: /**
164: * Inverts this matrix in place.
165: */
166: public final void invert() {
167: if (m00 == 0) {
168: throw new SingularMatrixException();
169: }
170: m00 = 1 / m00;
171: }
172:
173: /**
174: * {@inheritDoc}
175: */
176: public final void multiply(final Matrix matrix) {
177: if (matrix.getNumRow() != SIZE || matrix.getNumCol() != SIZE) {
178: throw new IllegalArgumentException(Errors
179: .format(ErrorKeys.ILLEGAL_MATRIX_SIZE));
180: }
181: m00 *= matrix.getElement(0, 0);
182: }
183:
184: /**
185: * Returns {@code true} if the specified object is of type {@code Matrix1} and
186: * all of the data members are equal to the corresponding data members in this matrix.
187: */
188: public boolean equals(final Object object) {
189: if (object != null && object.getClass().equals(getClass())) {
190: final Matrix1 that = (Matrix1) object;
191: return Double.doubleToLongBits(this .m00) == Double
192: .doubleToLongBits(that.m00);
193: }
194: return false;
195: }
196:
197: /**
198: * Returns a hash code value based on the data values in this object.
199: */
200: public int hashCode() {
201: return (int) (Double.doubleToLongBits(m00) ^ serialVersionUID);
202: }
203:
204: /**
205: * Returns a string representation of this matrix. The returned string is implementation
206: * dependent. It is usually provided for debugging purposes only.
207: */
208: public String toString() {
209: return GeneralMatrix.toString(this );
210: }
211:
212: /**
213: * Returns a clone of this matrix.
214: */
215: public Object clone() {
216: try {
217: return super .clone();
218: } catch (CloneNotSupportedException e) {
219: // Should not happen, since we are cloneable.
220: throw new AssertionError(e);
221: }
222: }
223: }
|