Revision 86
trunk/libraries/libCq CMS for java.old/src/org/cresques/px/dxf/DxfCalArcs.java | ||
---|---|---|
1 |
/* |
|
2 |
* Created on 24-mar-2004 |
|
3 |
* |
|
4 |
* To change the template for this generated file go to |
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments |
|
6 |
*/ |
|
7 | 1 |
package org.cresques.px.dxf; |
8 | 2 |
|
9 | 3 |
import java.awt.geom.Point2D; |
10 | 4 |
import java.util.Vector; |
11 | 5 |
|
12 |
import org.cresques.geo.Point3D; |
|
13 |
|
|
14 |
//import com.vividsolutions.jts.geom.CoordinateList; |
|
15 |
//import com.vividsolutions.jts.geom.Coordinate; |
|
16 |
|
|
17 |
/** |
|
18 |
* Clase auxiliar para el calculo de arcos como LineStrings |
|
19 |
* |
|
20 |
* @author Jose Morell (jomora3@eresmas.com) y Luis W. Sevilla (sevilla_lui@gva.es) |
|
21 |
* @version 0.1 |
|
22 |
* |
|
23 |
* To change the template for this generated type comment go to |
|
24 |
* Window>Preferences>Java>Code Generation>Code and Comments |
|
25 |
*/ |
|
26 | 6 |
public class DxfCalArcs { |
27 |
|
|
28 | 7 |
final boolean debug = true; |
29 | 8 |
|
30 |
final double toRad = Math.PI/(double)180.0; |
|
31 |
|
|
32 |
//Coordinate coord1, coord2; |
|
33 |
//Coordinate center; |
|
34 | 9 |
Point2D coord1, coord2; |
35 | 10 |
Point2D center; |
36 | 11 |
double radio, empieza, acaba; |
37 |
double z; |
|
38 |
|
|
39 | 12 |
double bulge; |
13 |
double d, dd, aci; |
|
14 |
Point2D coordAux; |
|
40 | 15 |
|
41 |
/** |
|
42 |
* Crea un Arc a partir de dos puntos y un pandeo. Se usa en POLYLINE |
|
43 |
* @param p1 |
|
44 |
* @param p2 |
|
45 |
* @param bulge |
|
46 |
*/ |
|
47 |
//public DxfCalArcs(Coordinate p1, Coordinate p2, double bulge) { |
|
48 | 16 |
public DxfCalArcs(Point2D p1, Point2D p2, double bulge) { |
49 | 17 |
coord1 = p1; |
50 | 18 |
System.out.println("AAAAAAA: coord1 = " + coord1); |
... | ... | |
52 | 20 |
System.out.println("AAAAAAA: coord2 = " + coord2); |
53 | 21 |
this.bulge = bulge; |
54 | 22 |
System.out.println("AAAAAAA: bulge = " + bulge); |
55 |
//z = p1.z; |
|
56 |
//calculate(bulge); |
|
23 |
if (bulge < 0.0) { |
|
24 |
coord1 = p2; |
|
25 |
coord2 = p1; |
|
26 |
} |
|
57 | 27 |
calculate(); |
58 | 28 |
} |
59 | 29 |
|
60 |
/** |
|
61 |
* Calcula un arco a partir de dos puntos y un 'pandeo' |
|
62 |
* @param bulge pandeo |
|
63 |
* @return El arco (this) |
|
64 |
*/ |
|
65 |
//DxfCalArcs calculate(double bulge) { |
|
66 | 30 |
DxfCalArcs calculate() { |
67 |
|
|
68 |
Point2D coordAux = new Point2D.Double(); |
|
69 |
center = new Point2D.Double(); |
|
70 |
double d,aci,alfa,beta,landa,dd,aci2; |
|
71 |
d=aci=alfa=beta=landa=dd=aci2=0; |
|
72 |
|
|
73 |
//d = Math.sqrt((coord2.getX()-coord1.getX())*(coord2.getX()-coord1.getX()) + (coord2.getY()-coord1.getY())*(coord2.getY()-coord1.getY()) + (coord2.z-coord1.z)*(coord2.z-coord1.z)); |
|
74 | 31 |
d = Math.sqrt((coord2.getX()-coord1.getX())*(coord2.getX()-coord1.getX()) + (coord2.getY()-coord1.getY())*(coord2.getY()-coord1.getY())); |
75 | 32 |
System.out.println("AAAAAA: d = " + d); |
76 | 33 |
|
77 |
coordAux.setLocation((coord1.getX()+coord2.getX())/2.0, (coord1.getY()+coord2.getY())/2.0); |
|
78 |
//coordAux.z = (coord1.z+coord2.z)/2.0; |
|
34 |
coordAux = new Point2D.Double((coord1.getX()+coord2.getX())/2.0, (coord1.getY()+coord2.getY())/2.0); |
|
79 | 35 |
System.out.println("AAAAAA: coordAux = " + coordAux); |
80 | 36 |
|
81 |
if (bulge > 0.0) { |
|
82 |
aci = Math.atan((coord2.getX()-coord1.getX())/(coord2.getY()-coord1.getY())); |
|
83 |
System.out.println("AAAAAA: aci = " + aci); |
|
84 |
if (coord2.getY() > coord1.getY()) { |
|
85 |
aci += Math.PI; |
|
86 |
System.out.println("AAAAAA: aci = " + aci); |
|
87 |
} |
|
88 |
|
|
89 |
beta = Math.atan(bulge); |
|
90 |
alfa = beta*4.0; |
|
91 |
landa = alfa/2.0; |
|
92 |
dd = (d/2.0)/(Math.tan(landa)); |
|
93 |
System.out.println("AAAAAA: dd = " + dd); |
|
94 |
|
|
95 |
//aci2 = aci; |
|
96 |
//System.out.println("OOOOOO: aci2 = " + aci2); |
|
97 |
|
|
98 |
center.setLocation(coordAux.getX() + dd*Math.sin(aci+(Math.PI/2.0)), coordAux.getY() + dd*Math.cos(aci+(Math.PI/2.0))); |
|
99 |
//center.z = coordAux.z; |
|
100 |
|
|
101 |
if (debug) System.out.println("AAAAAA: center = " + center); |
|
102 |
|
|
103 |
// Calculo del Radio |
|
104 |
radio = (d/2.0)/(Math.sin(landa)); |
|
105 |
|
|
106 |
calculateEA(alfa); |
|
37 |
double b = Math.abs(bulge); |
|
38 |
double beta = Math.atan(b); |
|
39 |
//double beta = Math.atan(bulge); |
|
40 |
double alfa = beta*4.0; |
|
41 |
double landa = alfa/2.0; |
|
42 |
dd = (d/2.0)/(Math.tan(landa)); |
|
43 |
System.out.println("AAAAAA: dd = " + dd); |
|
44 |
radio = (d/2.0)/(Math.sin(landa)); |
|
45 |
aci = Math.atan((coord2.getX()-coord1.getX())/(coord2.getY()-coord1.getY())); |
|
46 |
System.out.println("AAAAAA: aci = " + aci); |
|
47 |
double aciDegree = aci*180.0/Math.PI; |
|
48 |
System.out.println("AAAAAA: aciDegree = " + aciDegree); |
|
49 |
if (coord2.getY() > coord1.getY()) { |
|
50 |
aci += Math.PI; |
|
51 |
System.out.println("AAAAAA: aci = " + aci); |
|
52 |
aciDegree = aci*180.0/Math.PI; |
|
53 |
System.out.println("AAAAAA: aciDegree = " + aciDegree); |
|
107 | 54 |
} |
108 |
if (bulge < 0.0) { |
|
109 |
bulge = bulge * (-1); |
|
110 |
System.out.println("OOOOOOo: bulge = " + bulge); |
|
111 |
aci = Math.atan((coord1.getX()-coord2.getX())/(coord1.getY()-coord2.getY())); |
|
112 |
System.out.println("OOOOOO: aci = " + aci); |
|
113 |
if (coord2.getY() > coord1.getY()) { |
|
114 |
aci += Math.PI; |
|
115 |
System.out.println("OOOOOO: aci = " + aci); |
|
116 |
} |
|
117 |
|
|
118 |
beta = Math.atan(bulge); |
|
119 |
alfa = beta*4.0; |
|
120 |
landa = alfa/2.0; |
|
121 |
// Cuidado con dd. Cambia de un modo mu raro. |
|
122 |
dd = (d/2.0)/(Math.tan(landa)); |
|
123 |
System.out.println("OOOOOO: dd = " + dd); |
|
124 |
|
|
125 |
//aci2 = aci; |
|
126 |
//System.out.println("OOOOOO: aci2 = " + aci2); |
|
127 |
|
|
128 |
// El centro lo calcula donde le da la gana. |
|
129 |
center.setLocation(coordAux.getX() + dd*Math.sin(aci+(Math.PI/2.0)), coordAux.getY() + dd*Math.cos(aci+(Math.PI/2.0))); |
|
130 |
|
|
131 |
if (debug) System.out.println("OOOOOO: center = " + center); |
|
132 |
|
|
133 |
// Calculo del Radio |
|
134 |
radio = (d/2.0)/(Math.sin(landa)); |
|
135 |
|
|
136 |
//calculateEAInverse(alfa); |
|
137 |
calculateEA(alfa); |
|
138 |
} |
|
55 |
center = new Point2D.Double(coordAux.getX() + dd*Math.sin(aci+(Math.PI/2.0)), coordAux.getY() + dd*Math.cos(aci+(Math.PI/2.0))); |
|
56 |
System.out.println("AAAAAAAA: center = " + center); |
|
57 |
calculateEA(alfa); |
|
139 | 58 |
return this; |
140 | 59 |
} |
141 | 60 |
|
142 |
/** |
|
143 |
* C?lculo de los angulos de comienzo y fin del arco |
|
144 |
* @param alfa |
|
145 |
*/ |
|
146 |
void calculateEA(double alfa){ |
|
61 |
void calculateEA(double alfa){ |
|
147 | 62 |
empieza = Math.atan2(coord1.getY()-center.getY(), coord1.getX()-center.getX()); |
148 | 63 |
acaba = (empieza + alfa); |
149 | 64 |
empieza = empieza*180.0/Math.PI; |
150 | 65 |
acaba = acaba*180.0/Math.PI; |
151 |
|
|
152 |
if (debug) System.out.println("empieza = " + empieza + "\nacaba = " + acaba); |
|
153 |
} |
|
66 |
System.out.println("empieza = " + empieza + "\nacaba = " + acaba); |
|
67 |
} |
|
154 | 68 |
|
155 |
void calculateEAInverse(double alfa){ |
|
156 |
empieza = Math.atan2(coord2.getY()-center.getY(), coord2.getX()-center.getX()); |
|
157 |
acaba = (empieza + alfa); |
|
158 |
empieza = empieza*180.0/Math.PI; |
|
159 |
acaba = acaba*180.0/Math.PI; |
|
160 |
|
|
161 |
if (debug) System.out.println("empieza = " + empieza + "\nacaba = " + acaba); |
|
162 |
} |
|
163 |
|
|
164 |
/** |
|
165 |
* Genera los puntos que corresponden al arco. |
|
166 |
* @param inc Incremento (en grados) |
|
167 |
* @return Lista de coordenadas |
|
168 |
*/ |
|
169 |
|
|
170 |
//public CoordinateList getPoints(double inc, Coordinate xtru) { |
|
171 |
public DxfEntityList getPoints(double inc, Point3D xtru) { |
|
172 |
//CoordinateList arc = new CoordinateList(); |
|
173 |
DxfEntityList arc = null; |
|
174 |
double angulo; |
|
175 |
|
|
176 |
int iempieza = (int) empieza + 1; // ojo aqui !! |
|
177 |
int iacaba = (int) acaba; |
|
178 |
|
|
179 |
if (empieza <= acaba) { |
|
180 |
//angulo = empieza; |
|
181 |
addNode(arc, empieza, z, "POS empieza", xtru); |
|
182 |
for (angulo = iempieza; angulo <= iacaba; angulo += inc) { |
|
183 |
addNode(arc, angulo, z, "POS ", xtru); |
|
184 |
} |
|
185 |
//angulo = acaba; |
|
186 |
addNode(arc, acaba, z, "POS acaba ", xtru); |
|
187 |
|
|
188 |
//Coordinate aux = new Coordinate(); |
|
189 |
Point2D aux = new Point2D.Double(); |
|
190 |
//aux = arc.getCoordinate(arc.size()-1); |
|
191 |
aux = (Point2D)arc.entities.get(arc.entities.size()-1); |
|
192 |
double aux1 = Math.abs(aux.getX()-coord2.getX()); |
|
193 |
double aux2 = Math.abs(aux.getY()-coord2.getY()); |
|
194 |
if (aux1<=0.000005 && aux2<=0.000005) { |
|
195 |
//arc.remove(arc.size()-1); |
|
196 |
arc.entities.remove(arc.entities.size()-1); |
|
197 |
//arc.add(coord2); |
|
198 |
arc.entities.add(coord2); |
|
199 |
} |
|
200 |
} else { |
|
201 |
//acaba = 360-(alfa-empieza); |
|
202 |
//angulo = empieza; |
|
203 |
addNode(arc, empieza, z, "NEG ", xtru); |
|
204 |
for (angulo = iempieza ; angulo <= 360; angulo += inc) { |
|
205 |
addNode(arc, angulo, z, "NEG ", xtru); |
|
206 |
} |
|
207 |
for (angulo = 1; angulo <= iacaba; angulo += inc) { |
|
208 |
addNode(arc, angulo, z, "NEG ", xtru); |
|
209 |
} |
|
210 |
angulo = acaba; |
|
211 |
addNode(arc, angulo, z, "NEG ", xtru); |
|
212 |
|
|
213 |
//Coordinate aux = new Coordinate(); |
|
214 |
Point2D aux = new Point2D.Double(); |
|
215 |
//aux = arc.getCoordinate(arc.size()-1); |
|
216 |
aux = (Point2D)arc.entities.get(arc.entities.size()-1); |
|
217 |
double aux1 = Math.abs(aux.getX()-coord2.getX()); |
|
218 |
double aux2 = Math.abs(aux.getY()-coord2.getY()); |
|
219 |
if (aux1<=0.000005 && aux2<=0.000005) { |
|
220 |
//arc.remove(arc.size()-1); |
|
221 |
arc.entities.remove(arc.entities.size()-1); |
|
222 |
//arc.remove(arc.size()-1); |
|
223 |
arc.entities.remove(arc.entities.size()-1); |
|
224 |
//arc.add(coord2); |
|
225 |
arc.entities.add(coord2); |
|
226 |
} |
|
227 |
} |
|
228 |
return arc; |
|
229 |
} |
|
230 |
|
|
231 |
//public CoordinateList getPoints(double inc, Coordinate xtru) { |
|
232 |
//public DxfEntityList getPoints(double inc) { |
|
233 | 69 |
public Vector getPoints(double inc) { |
234 |
//CoordinateList arc = new CoordinateList(); |
|
235 | 70 |
Vector arc = new Vector(); |
236 |
//DxfEntityList arc = null; |
|
237 | 71 |
double angulo; |
238 |
|
|
239 |
/*if (bulge < 0.0) { |
|
240 |
empieza = acaba; |
|
241 |
acaba = empieza; |
|
242 |
}*/ |
|
243 | 72 |
|
244 | 73 |
int iempieza = (int) empieza + 1; // ojo aqui !! |
245 | 74 |
int iacaba = (int) acaba; |
246 | 75 |
|
247 | 76 |
if (empieza <= acaba) { |
248 |
//angulo = empieza; |
|
249 |
addNode(arc, empieza, z, "POS empieza"); |
|
77 |
addNode(arc, empieza); |
|
250 | 78 |
for (angulo = iempieza; angulo <= iacaba; angulo += inc) { |
251 |
addNode(arc, angulo, z, "POS ");
|
|
79 |
addNode(arc, angulo); |
|
252 | 80 |
} |
253 |
//angulo = acaba;
|
|
81 |
addNode(arc, acaba);
|
|
254 | 82 |
|
255 |
/*if (bulge > 0.0) { |
|
256 |
addNode(arc, acaba, z, "POS acaba "); |
|
257 |
} |
|
258 |
if (bulge < 0.0){ |
|
259 |
addNode(arc, empieza, z, "POS acaba "); |
|
260 |
}*/ |
|
261 |
addNode(arc, acaba, z, "POS acaba "); |
|
83 |
Point2D aux = (Point2D)arc.get(arc.size()-1); |
|
262 | 84 |
|
263 |
//Coordinate aux = new Coordinate(); |
|
264 |
Point2D aux = new Point2D.Double(); |
|
265 |
//aux = arc.getCoordinate(arc.size()-1); |
|
266 |
aux = (Point2D)arc.get(arc.size()-1); |
|
267 |
|
|
268 | 85 |
double aux1 = Math.abs(aux.getX()-coord2.getX()); |
269 | 86 |
double aux2 = Math.abs(aux.getY()-coord2.getY()); |
270 | 87 |
if (aux1<=0.000005 && aux2<=0.000005) { |
271 | 88 |
arc.remove(arc.size()-1); |
272 | 89 |
arc.remove(arc.size()-1); |
273 |
//arc.add(coord2); |
|
274 | 90 |
arc.add(coord2); |
275 | 91 |
} |
276 | 92 |
|
277 | 93 |
} else { |
278 |
//acaba = 360-(alfa-empieza); |
|
279 |
//angulo = empieza; |
|
280 |
addNode(arc, empieza, z, "NEG "); |
|
94 |
addNode(arc, empieza); |
|
281 | 95 |
for (angulo = iempieza ; angulo <= 360; angulo += inc) { |
282 |
addNode(arc, angulo, z, "NEG ");
|
|
96 |
addNode(arc, angulo); |
|
283 | 97 |
} |
284 | 98 |
for (angulo = 1; angulo <= iacaba; angulo += inc) { |
285 |
addNode(arc, angulo, z, "NEG ");
|
|
99 |
addNode(arc, angulo); |
|
286 | 100 |
} |
101 |
addNode(arc, angulo); |
|
287 | 102 |
|
288 |
/*if (bulge > 0.0) { |
|
289 |
angulo = acaba; |
|
290 |
addNode(arc, angulo, z, "NEG "); |
|
291 |
} |
|
292 |
if (bulge < 0.0) { |
|
293 |
angulo = empieza; |
|
294 |
addNode(arc, angulo, z, "NEG "); |
|
295 |
}*/ |
|
296 |
//angulo = acaba; |
|
297 |
addNode(arc, angulo, z, "NEG "); |
|
103 |
Point2D aux = (Point2D)arc.get(arc.size()-1); |
|
298 | 104 |
|
299 |
//Coordinate aux = new Coordinate(); |
|
300 |
Point2D aux = new Point2D.Double(); |
|
301 |
//aux = arc.getCoordinate(arc.size()-1); |
|
302 |
aux = (Point2D)arc.get(arc.size()-1); |
|
303 | 105 |
double aux1 = Math.abs(aux.getX()-coord2.getX()); |
304 | 106 |
double aux2 = Math.abs(aux.getY()-coord2.getY()); |
305 | 107 |
if (aux1<=0.000005 && aux2<=0.000005) { |
... | ... | |
310 | 112 |
} |
311 | 113 |
return arc; |
312 | 114 |
} |
313 |
|
|
314 |
/** |
|
315 |
* A?ade un punto al arco |
|
316 |
* @param arc |
|
317 |
* @param angulo |
|
318 |
* @param z |
|
319 |
* @param comment |
|
320 |
*/ |
|
321 |
/*private void addNode(CoordinateList arc, double angulo, double z, String comment, Coordinate xtru) { |
|
322 |
final boolean debug = false; |
|
323 |
|
|
324 |
Coordinate coord_in = new Coordinate(); |
|
325 |
Coordinate coord_out = new Coordinate(); |
|
326 |
|
|
327 |
double yy; |
|
328 |
double xx; |
|
329 |
yy = center.getY() + radio * Math.sin(angulo*toRad); |
|
330 |
xx = center.getX() + radio * Math.cos(angulo*toRad); |
|
331 |
|
|
332 |
arc.add(new Coordinate(xx,yy,z)); |
|
333 |
if (debug) System.out.println(comment+"arc.add( " + xx + ", " + yy + " )"); |
|
334 |
}*/ |
|
335 | 115 |
|
336 |
private void addNode(DxfEntityList arc, double angulo, double z, String comment, Point3D xtru) { |
|
337 |
final boolean debug = false; |
|
338 |
|
|
339 |
Point2D coord_in = new Point2D.Double(); |
|
340 |
Point2D coord_out = new Point2D.Double(); |
|
341 |
|
|
342 |
double yy; |
|
343 |
double xx; |
|
344 |
yy = center.getY() + radio * Math.sin(angulo*toRad); |
|
345 |
xx = center.getX() + radio * Math.cos(angulo*toRad); |
|
346 |
|
|
347 |
arc.entities.add(new Point2D.Double(xx,yy)); |
|
348 |
if (debug) System.out.println(comment+"arc.add( " + xx + ", " + yy + " )"); |
|
349 |
} |
|
350 |
|
|
351 |
private void addNode(Vector arc, double angulo, double z, String comment) { |
|
352 |
final boolean debug = false; |
|
353 |
|
|
354 |
Point2D coord_in = new Point2D.Double(); |
|
355 |
Point2D coord_out = new Point2D.Double(); |
|
356 |
|
|
357 |
double yy; |
|
358 |
double xx; |
|
359 |
|
|
360 |
yy = center.getY() + radio * Math.sin(angulo*toRad); |
|
361 |
xx = center.getX() + radio * Math.cos(angulo*toRad); |
|
362 |
//System.out.println("xx = " + xx + ", yy = " + yy + ", arc = " + arc); |
|
116 |
private void addNode(Vector arc, double angulo) { |
|
117 |
double yy = center.getY() + radio * Math.sin(angulo*Math.PI/180.0); |
|
118 |
double xx = center.getX() + radio * Math.cos(angulo*Math.PI/180.0); |
|
363 | 119 |
arc.add(new Point2D.Double(xx,yy)); |
364 |
if (debug) System.out.println(comment+"arc.add( " + xx + ", " + yy + " )");
|
|
120 |
System.out.println("arc.add( " + xx + ", " + yy + " )");
|
|
365 | 121 |
} |
366 | 122 |
|
367 | 123 |
} |
Also available in: Unified diff