svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / utils / MathUtils.java @ 5217
History | View | Annotate | Download (5.48 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.georeferencing.utils; |
20 |
|
21 |
import java.awt.geom.Rectangle2D; |
22 |
|
23 |
import org.cresques.cts.IProjection; |
24 |
import org.cresques.io.GeoRasterFile; |
25 |
import org.cresques.px.Extent; |
26 |
|
27 |
import com.iver.cit.gvsig.fmap.ViewPort; |
28 |
|
29 |
public class MathUtils { |
30 |
public static double INCHESMTR = 39.73007874D; |
31 |
public static double INCHESCM = 0.3973007874D; |
32 |
public static double INCHESMM = 0.03973007874D; |
33 |
public static double MTRSINCH = 0.0254D; |
34 |
public static double MMSINCH = 25.4D; |
35 |
public static double CMSINCH = 2.54D; |
36 |
|
37 |
|
38 |
/**
|
39 |
* Recorta el n?mero de decimales a n del n?mero pasado por par?metro
|
40 |
* @param num Valor a recortar el n?mero de decimales
|
41 |
* @param n N?mero de decimales a recortar
|
42 |
* @return N?mero recortado
|
43 |
*/
|
44 |
public static double tailDecimals(double num, int n){ |
45 |
long m = (int)Math.pow(10, n); |
46 |
num *= m; |
47 |
long aux = ((long)num); |
48 |
num = (double)((double)aux / (double)m); |
49 |
return num;
|
50 |
} |
51 |
|
52 |
/**
|
53 |
* Recorta el n?mero de decimales a n del n?mero pasado por par?metro
|
54 |
* @param num Valor a recortar el n?mero de decimales
|
55 |
* @param n N?mero de decimales a recortar
|
56 |
* @return N?mero recortado
|
57 |
*/
|
58 |
public static String tailDecimals(String num, int n){ |
59 |
int index = num.indexOf("."); |
60 |
if(index != -1){ |
61 |
try{
|
62 |
num = num.substring(0, index + n);
|
63 |
}catch(IndexOutOfBoundsException e){ |
64 |
//No hacemos nada pq retornar? num
|
65 |
} |
66 |
} |
67 |
return num;
|
68 |
} |
69 |
|
70 |
/**
|
71 |
* Convierte pixels en milimetros dependiendo del los puntos por pulgada
|
72 |
* @param pixels N?mero de pixels
|
73 |
* @param ppp Puntos por pulgada
|
74 |
* @return N?mero de milimetros
|
75 |
*/
|
76 |
public static double convertPixelsToMms(int pixels, int ppp){ |
77 |
return convertPixelsToInches(pixels, ppp) * MMSINCH;
|
78 |
} |
79 |
|
80 |
/**
|
81 |
* Convierte pixels en centimetros dependiendo del los metros por pixel
|
82 |
* @param pixels N?mero de pixels
|
83 |
* @param mtsPixel Metros por pixel
|
84 |
* @return N?mero de centimetros
|
85 |
*/
|
86 |
public static double convertPixelsToCms(int pixels, int ppp){ |
87 |
return convertPixelsToInches(pixels, ppp) * CMSINCH;
|
88 |
} |
89 |
|
90 |
/**
|
91 |
* Convierte pixels en metros dependiendo del los metros por pixel
|
92 |
* @param pixels N?mero de pixels
|
93 |
* @param mtsPixel Metros por pixel
|
94 |
* @return N?mero de metros
|
95 |
*/
|
96 |
public static double convertPixelsToMts(int pixels, int ppp){ |
97 |
return convertPixelsToInches(pixels, ppp) * MTRSINCH;
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* Convierte pixels en pulgadas dependiendo del los metros por pixel
|
102 |
* @param pixels N?mero de pixels
|
103 |
* @param mtsPixel Metros por pixel
|
104 |
* @return N?mero de pulgadas
|
105 |
*/
|
106 |
public static double convertPixelsToInches(int pixels, int ppp){ |
107 |
return (double)((double)pixels / (double)ppp); |
108 |
} |
109 |
|
110 |
/**
|
111 |
* Convierte metros en pixels dependiendo de los puntos por pulgada
|
112 |
* @param mts N?mero de metros
|
113 |
* @param ppp Puntos por pulgada
|
114 |
* @return N?mero de pixeles
|
115 |
*/
|
116 |
public static int convertMtsToPixels(double mts, int ppp){ |
117 |
return (int)(mts * MathUtils.INCHESMTR * ppp); |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Convierte pulgadas en pixels dependiendo de los puntos por pulgada
|
122 |
* @param mts N?mero de pulgadas
|
123 |
* @param ppp Puntos por pulgada
|
124 |
* @return N?mero de pixeles
|
125 |
*/
|
126 |
public static int convertInchesToPixels(double inches, int ppp){ |
127 |
return (int)(inches * ppp); |
128 |
} |
129 |
|
130 |
/**
|
131 |
* Convierte centimetros en pixels dependiendo de los puntos por pulgada
|
132 |
* @param mts N?mero de centmetros
|
133 |
* @param ppp Puntos por pulgada
|
134 |
* @return N?mero de pixeles
|
135 |
*/
|
136 |
public static int convertCmsToPixels(double cms, int ppp){ |
137 |
return (int)(cms * MathUtils.INCHESCM * ppp); |
138 |
} |
139 |
|
140 |
/**
|
141 |
* Convierte milimetros en pixels dependiendo de los puntos por pulgada
|
142 |
* @param mts N?mero de milimetros
|
143 |
* @param ppp Puntos por pulgada
|
144 |
* @return N?mero de pixeles
|
145 |
*/
|
146 |
public static int convertMmsToPixels(double mms, int ppp){ |
147 |
return (int)(mms * MathUtils.INCHESMM * ppp); |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Devuelve la traspuesta de una matriz
|
152 |
* @param M Matriz
|
153 |
* @param m N?mero de filas
|
154 |
* @param n N?mero de columnas
|
155 |
* @return Matriz traspuesta
|
156 |
*/
|
157 |
public static double[][] transpose(double[][] M, int m, int n){ |
158 |
double[][] a = new double[n][m]; |
159 |
int i, j;
|
160 |
|
161 |
for (i = 0; i < n; i++) |
162 |
for (j = 0; j < m; j++) |
163 |
a[i][j] = M[j][i]; |
164 |
return a;
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* Producto de matrices (AxB)
|
169 |
* @param a Matriz A
|
170 |
* @param b Matriz B
|
171 |
* @param M
|
172 |
* @param N
|
173 |
* @param O
|
174 |
* @return
|
175 |
*/
|
176 |
public static double[][] multmatrix(double[][] a, double[][] b, int M, int N, int O){ |
177 |
double[][] c = new double[M][O]; |
178 |
double sum;
|
179 |
int i, j, k;
|
180 |
|
181 |
for (i = 0; i < M; i++) { |
182 |
for (j = 0; j < O; j++) { |
183 |
sum = 0;
|
184 |
for (k = 0; k < N; k++) |
185 |
sum += b[k][j] * a[i][k]; |
186 |
c[i][j] = sum; |
187 |
} |
188 |
} |
189 |
return (c);
|
190 |
} |
191 |
} |