01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
05: * (C) 2005, Institut de Recherche pour le Développement
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation;
10: * version 2.1 of the License.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: */
17: package org.geotools.referencing.operation.matrix;
18:
19: // OpenGIS dependencies
20: import org.opengis.referencing.operation.Matrix;
21:
22: /**
23: * Static utility methods for creating matrix. This factory selects one of the {@link Matrix1},
24: * {@link Matrix2}, {@link Matrix3}, {@link Matrix4} or {@link GeneralMatrix} implementation
25: * according the desired matrix size. Note that if the matrix size is know at compile time,
26: * it may be more efficient to invoke directly the constructor of the appropriate class instead.
27: *
28: * @since 2.2
29: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/referencing/operation/matrix/MatrixFactory.java $
30: * @version $Id: MatrixFactory.java 22710 2006-11-12 18:04:54Z desruisseaux $
31: * @author Martin Desruisseaux
32: */
33: public final class MatrixFactory {
34: /**
35: * Do not allows instantiation of this class.
36: */
37: private MatrixFactory() {
38: }
39:
40: /**
41: * Creates a square identity matrix of size {@code size} × {@code size}.
42: */
43: public static XMatrix create(final int size) {
44: switch (size) {
45: case 1:
46: return new Matrix1();
47: case 2:
48: return new Matrix2();
49: case 3:
50: return new Matrix3();
51: case 4:
52: return new Matrix4();
53: default:
54: return new GeneralMatrix(size);
55: }
56: }
57:
58: /**
59: * Creates a matrix of size {@code numRow} × {@code numCol}.
60: * Elements on the diagonal <var>j==i</var> are set to 1.
61: */
62: public static XMatrix create(final int numRow, final int numCol) {
63: if (numRow == numCol) {
64: return create(numRow);
65: } else {
66: return new GeneralMatrix(numRow, numCol);
67: }
68: }
69:
70: /**
71: * Creates a new matrix which is a copy of the specified matrix.
72: */
73: public static XMatrix create(final Matrix matrix) {
74: final int size = matrix.getNumRow();
75: if (size == matrix.getNumCol()) {
76: switch (size) {
77: case 1:
78: return new Matrix1(matrix);
79: case 2:
80: return new Matrix2(matrix);
81: case 3:
82: return new Matrix3(matrix);
83: case 4:
84: return new Matrix4(matrix);
85: }
86: }
87: return new GeneralMatrix(matrix);
88: }
89: }
|