svn-gvsig-desktop / import / ext3D / branches / ext3D_v1.1 / lib3DMap / src / com / iver / ai2 / gvsig3d / gui / FeatureFactory.java @ 15490
History | View | Annotate | Download (16.1 KB)
1 |
package com.iver.ai2.gvsig3d.gui; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.util.HashMap; |
5 |
import java.util.Iterator; |
6 |
import java.util.List; |
7 |
|
8 |
import com.iver.ai2.gvsig3d.resources.ResourcesFactory; |
9 |
import com.iver.ai2.gvsig3d.utils.Punto3D; |
10 |
import com.iver.cit.gvsig.fmap.core.v02.FLabel; |
11 |
|
12 |
import es.upv.ai2.libjosg.AutoTransform; |
13 |
import es.upv.ai2.libjosg.Group; |
14 |
import es.upv.ai2.libjosg.Node; |
15 |
import es.upv.ai2.libjosg.Optimizer; |
16 |
import es.upv.ai2.libjosg.Vec3; |
17 |
import es.upv.ai2.libjosg.Vec4; |
18 |
import es.upv.ai2.libjosg.Optimizer.OptimizationOptions; |
19 |
import es.upv.ai2.libjosg.features.PixelPoint; |
20 |
import es.upv.ai2.libjosg.features.Polygon; |
21 |
import es.upv.ai2.libjosg.features.Polyline; |
22 |
import es.upv.ai2.libjosg.features.TessellablePolygon; |
23 |
import es.upv.ai2.libjosg.features.Text; |
24 |
import es.upv.ai2.libjosg.planets.Planet; |
25 |
import es.upv.ai2.libjosg.planets.Planet.PlanetType; |
26 |
|
27 |
public class FeatureFactory extends Group { |
28 |
|
29 |
private static double radius; |
30 |
|
31 |
static Planet planet;
|
32 |
|
33 |
public FeatureFactory(double ra, Planet m_planet) { |
34 |
super();
|
35 |
radius = ra; |
36 |
// this.setNodeName("VECTOR");
|
37 |
planet = m_planet; |
38 |
init(); |
39 |
} |
40 |
|
41 |
private void init() { |
42 |
|
43 |
// this.addChild(insertTexto("Polo norte", poloNorte));
|
44 |
// this.addChild(insertTexto("MADRID", valenciaCar));
|
45 |
|
46 |
// Vec3 posIni = new Vec3(0, 0, 0);
|
47 |
// Vec3 posEnd = new Vec3(0, 0, 100000);
|
48 |
|
49 |
// Polyline line = (Polyline) VectorTest.insertLineS(posIni, posEnd,
|
50 |
// new Vec4(1.0, 1.0, 1.0, 1.0), 0);
|
51 |
// if (planet.getType() == PlanetType.PLANE_MODE)
|
52 |
// this.addChild(line);
|
53 |
|
54 |
} |
55 |
|
56 |
// public Node insertTexto(String texto, Vec3 posicion) {
|
57 |
// // A?adiendo texto
|
58 |
// Text text = new Text();
|
59 |
// // text.setNodeName(texto);
|
60 |
// text.setText(texto);
|
61 |
// text.setCharacterSize(32);
|
62 |
//
|
63 |
// text.setAxisAlignment(Text.AxisAlignment.SCREEN);
|
64 |
// text.setAlignment(Text.AlignmentType.CENTER_CENTER);
|
65 |
// text.setCharacterSizeMode(Text.CharacterSizeMode.SCREEN_COORDS);
|
66 |
// text.setPosition((float) posicion.x(), (float) posicion.y(),
|
67 |
// (float) posicion.z());
|
68 |
//
|
69 |
// if (ResourcesFactory.exitsResouce("arial.ttf"))
|
70 |
// text.setFont(ResourcesFactory.getResourcePath("arial.ttf"));
|
71 |
// else
|
72 |
// text.setFont("arial.ttf");
|
73 |
// text.setAutoRotateToScreen(true);
|
74 |
//
|
75 |
//
|
76 |
// Optimizer opt = new Optimizer();
|
77 |
// opt.optimizeOptions(text, OptimizationOptions.SPATIALIZE_GROUPS);
|
78 |
//
|
79 |
// return text;
|
80 |
// }
|
81 |
// /**
|
82 |
// * this method create a new 3D text. With a value and position. And insert
|
83 |
// a
|
84 |
// * point under 3D text
|
85 |
// *
|
86 |
// * @param texto
|
87 |
// * text that is visible in 3D
|
88 |
// * @param position
|
89 |
// * Position of 3D text in cartesian coordinates
|
90 |
// * @return New node
|
91 |
// */
|
92 |
// public void insertText(String texto, Vec3 posicion) {
|
93 |
// posicion.setZ(posicion.z() + radius);
|
94 |
// Vec3 e = planet.convertLatLongHeightToXYZ(posicion);
|
95 |
//
|
96 |
// this.addChild(insertPoint(e, new Vec4(1.0, 0, 0, 1.0)));
|
97 |
//
|
98 |
// this.addChild(insertTexto(texto, e));
|
99 |
// }
|
100 |
//
|
101 |
// /**
|
102 |
// * this method create a new 3D point. With a color and position
|
103 |
// *
|
104 |
// * @param color
|
105 |
// * Color of the point
|
106 |
// * @param position
|
107 |
// * Position of 3D point in cartesian coordinates
|
108 |
// * @return
|
109 |
// */
|
110 |
// public Node insertPoint(Vec3 position, Vec4 color) {
|
111 |
// // Creating point
|
112 |
// Point p = new Point();
|
113 |
//
|
114 |
// // Adding position and color
|
115 |
// for (int i = 0; i < 1; i++) {
|
116 |
// p.setPointSize(100);
|
117 |
// p.addPoint(position, color);
|
118 |
// }
|
119 |
//
|
120 |
// return p;
|
121 |
// }
|
122 |
|
123 |
/**
|
124 |
* Method to create a new 3D text. With a value and position. Text aligment
|
125 |
* is LEFT CENTER, and his font is "Arial.ttf"
|
126 |
*
|
127 |
* @param texto
|
128 |
* Text that is visible in 3D
|
129 |
* @param position
|
130 |
* Position of 3D text in cartesian coordinates
|
131 |
* @return New node
|
132 |
*/
|
133 |
public static Node insertTextoS(String texto, Vec3 position, Vec4 color, |
134 |
int tam, boolean inPixels) { |
135 |
|
136 |
// Creating text node
|
137 |
Text text = new Text();
|
138 |
// Set up text properties
|
139 |
text.setText(texto); |
140 |
text.setCharacterSize(tam); |
141 |
// Select that the text will be in pixels or screen coordinates
|
142 |
if (inPixels) {
|
143 |
text.setCharacterSizeMode(Text.CharacterSizeMode.SCREEN_COORDS); |
144 |
} else {
|
145 |
text.setCharacterSizeMode(Text.CharacterSizeMode.OBJECT_COORDS); |
146 |
} |
147 |
text.setColor(color); |
148 |
text.setAxisAlignment(Text.AxisAlignment.SCREEN); |
149 |
text.setAlignment(Text.AlignmentType.LEFT_BOTTOM); |
150 |
text.setPosition((float) position.x(), (float) position.y(), |
151 |
(float) position.z());
|
152 |
|
153 |
// Search font in resources directori
|
154 |
if (ResourcesFactory.exitsResouce("arial.ttf")) |
155 |
text.setFont(ResourcesFactory.getResourcePath("arial.ttf"));
|
156 |
else
|
157 |
text.setFont("arial.ttf");
|
158 |
text.setAutoRotateToScreen(true);
|
159 |
|
160 |
// Optimizer generated nodes
|
161 |
Optimizer opt = new Optimizer();
|
162 |
opt.optimize(text, OptimizationOptions.SPATIALIZE_GROUPS); |
163 |
|
164 |
// Return node
|
165 |
return text;
|
166 |
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Method to create a new 3D point. With a color, heigth and position.
|
171 |
*
|
172 |
* @param position
|
173 |
* Position of 3D text in cartesian coordinates
|
174 |
* @param color
|
175 |
* color of the 3D point
|
176 |
* @param tam
|
177 |
* the heigth of the point
|
178 |
* @return New text node
|
179 |
*/
|
180 |
public static Node insertPointS(Vec3 position, Vec4 color, int tam) { |
181 |
AutoTransform au = new AutoTransform();
|
182 |
// Creating point
|
183 |
PixelPoint p = new PixelPoint();
|
184 |
// p.setDistanceAttenuation(new Vec3(0.0,0.0,0.0000001));
|
185 |
p.setPointSize(tam); |
186 |
p.addPoint(position, color); |
187 |
|
188 |
au.addChild(p); |
189 |
// return au;
|
190 |
return p;
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* Method to create a new 3D point. With a color, heigth and position.
|
195 |
*
|
196 |
* @param hash
|
197 |
* Hash Table that contains "Puntos3D" elements
|
198 |
* @return
|
199 |
*/
|
200 |
public static Node insertPointS(HashMap hash) { |
201 |
|
202 |
Group g = new Group(); |
203 |
|
204 |
// Iterate over the keys in the map
|
205 |
Iterator it = hash.keySet().iterator();
|
206 |
while (it.hasNext()) {
|
207 |
// Get key
|
208 |
Integer key = (Integer) it.next(); |
209 |
List list = (List) hash.get(key); |
210 |
|
211 |
PixelPoint p = new PixelPoint();
|
212 |
for (Iterator iter = list.iterator(); iter.hasNext();) { |
213 |
// Getting PixelPoint
|
214 |
Punto3D element = (Punto3D) iter.next(); |
215 |
Vec4 color = element.getColor(); |
216 |
// Blending only is active if the alpha component are diferent
|
217 |
// that 1
|
218 |
if (color.w() != 1) { |
219 |
p.setEnabledBlending(true);
|
220 |
} |
221 |
// Enabling antialiasing
|
222 |
p.setEnabledSmoothing(true);
|
223 |
|
224 |
// Set up size, position and color
|
225 |
p.setPointSize(element.getZize()); |
226 |
p.addPoint(element.getPosition(), color); |
227 |
} |
228 |
// Adding points to group
|
229 |
g.addChild(p); |
230 |
} |
231 |
|
232 |
// Optimizer generated nodes
|
233 |
Optimizer opt = new Optimizer();
|
234 |
opt.optimize(g, OptimizationOptions.SPATIALIZE_GROUPS); |
235 |
|
236 |
return g;
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* Method to add 3D points to specific point type. With a color, heigth and
|
241 |
* position.
|
242 |
*
|
243 |
* @param point
|
244 |
* Node to add new 3D point
|
245 |
* @param position
|
246 |
* Position of 3D text in cartesian coordinates
|
247 |
* @param color
|
248 |
* color of the 3D point
|
249 |
* @param tam
|
250 |
* the heigth of the point
|
251 |
* @return New text node
|
252 |
*/
|
253 |
public static void addNodeToPoint(Node point, Vec3 position, Vec4 color, |
254 |
int tam) {
|
255 |
|
256 |
PixelPoint p = null;
|
257 |
// Getting point
|
258 |
if (point != null) |
259 |
p = (PixelPoint) point; |
260 |
|
261 |
// If not exits create new
|
262 |
if (p == null) |
263 |
p = new PixelPoint();
|
264 |
|
265 |
// Blending only is active if the alpha component are diferent that 1
|
266 |
if (color.w() != 1) { |
267 |
p.setEnabledBlending(true);
|
268 |
} |
269 |
|
270 |
// Set up size, position and color
|
271 |
p.setPointSize(tam); |
272 |
p.addPoint(position, color); |
273 |
|
274 |
// Optimizer generated nodes
|
275 |
Optimizer opt = new Optimizer();
|
276 |
opt.optimize(p, OptimizationOptions.SPATIALIZE_GROUPS); |
277 |
|
278 |
} |
279 |
|
280 |
/**
|
281 |
* Method to create a new 3D line. With a color, heigth and position.
|
282 |
*
|
283 |
* @param ini
|
284 |
* Initial position of line
|
285 |
* @param end
|
286 |
* End positon of line.
|
287 |
* @param color
|
288 |
* color of the 3D point
|
289 |
* @param tam
|
290 |
* the heigth of the point
|
291 |
* @return New line node
|
292 |
*/
|
293 |
|
294 |
public static Node insertLineS(Vec3 ini, Vec3 end, Vec4 color, int tam) { |
295 |
// Creating a polyline
|
296 |
Polyline p = new Polyline(ini, end);
|
297 |
|
298 |
// Set up parameter
|
299 |
p.setWidth(2f);
|
300 |
p.setPattern((short) 0xFFFF); |
301 |
p.setFactor(2);
|
302 |
p.setColor(color); |
303 |
|
304 |
// Optimize the node
|
305 |
Optimizer opt = new Optimizer();
|
306 |
opt.optimize(p); |
307 |
|
308 |
// Return the node
|
309 |
return p;
|
310 |
} |
311 |
|
312 |
/**
|
313 |
* Method to add new points to specific line type. With a color, heigth and
|
314 |
* position.
|
315 |
*
|
316 |
* @param line
|
317 |
* Node to add new point
|
318 |
* @param position
|
319 |
* Position of 3D point in cartesian coordinates
|
320 |
* @param color
|
321 |
* color of the 3D point
|
322 |
* @param tam
|
323 |
* the heigth of the point
|
324 |
*/
|
325 |
public static void addNodeToLine(Node line, Vec3 position, Vec4 color, |
326 |
int tam) {
|
327 |
// Getting polyline
|
328 |
Polyline p = (Polyline) line; |
329 |
|
330 |
// If not exits, create a new
|
331 |
if (p == null) |
332 |
p = new Polyline(position, position);
|
333 |
|
334 |
// Add a point and color
|
335 |
p.addVertex(position, color); |
336 |
|
337 |
} |
338 |
|
339 |
/**
|
340 |
* Create a new point node with a color and size. Using a position of 3D
|
341 |
* points
|
342 |
*
|
343 |
* @param position
|
344 |
* Position of 3D point
|
345 |
* @param color
|
346 |
* Color of the 3D point
|
347 |
* @param tam
|
348 |
* Size of the point (in pixels)
|
349 |
* @return
|
350 |
*/
|
351 |
public static Node insertPointS(Vec3[] position, Vec4 color, int tam) { |
352 |
// Creating point node
|
353 |
PixelPoint p = new PixelPoint();
|
354 |
|
355 |
// Set up parameters and insert position
|
356 |
for (int i = 0; i < position.length; i++) { |
357 |
p.setPointSize(tam); |
358 |
p.addPoint(position[i], color); |
359 |
} |
360 |
|
361 |
// Optimizer generated nodes
|
362 |
Optimizer opt = new Optimizer();
|
363 |
opt.optimize(p, OptimizationOptions.SPATIALIZE_GROUPS); |
364 |
|
365 |
// Return node
|
366 |
return p;
|
367 |
} |
368 |
|
369 |
/**
|
370 |
* Create a new tesselation poligon with a color using an array of 3D points
|
371 |
*
|
372 |
* @param points
|
373 |
* Array of 3D points
|
374 |
* @param color
|
375 |
* Color of the poligon
|
376 |
* @return Poligon node
|
377 |
*/
|
378 |
public static Node insertPolygon(Vec3[] points, Vec4 color) { |
379 |
// Creating a polygon
|
380 |
TessellablePolygon p = new TessellablePolygon();
|
381 |
|
382 |
// Set up parameters
|
383 |
// p.setType(Polygon.PolygonType.FILLED_POLYGON);
|
384 |
p.setWidth(2f);
|
385 |
// p.setPattern((short) 0xFFFF);
|
386 |
// p.setFactor(2);
|
387 |
p.setType(Polygon.PolygonType.FILLED_POLYGON);
|
388 |
// p.setType(Polygon.PolygonType.EMPTY_POLYGON);
|
389 |
// p.setType(Polygon.PolygonType.PATTERN_POLYGON);
|
390 |
|
391 |
// Adding points
|
392 |
for (int i = 0; i < points.length - 1; i++) { |
393 |
p.addVertex(points[i], color); |
394 |
// System.out.println("Position: " + inv[i].x() + " " + inv[i].y() +
|
395 |
// " " + inv[i].z());
|
396 |
} |
397 |
|
398 |
// Tesselation of this poligon
|
399 |
p.tesselate(); |
400 |
|
401 |
// Optimizer generated nodes
|
402 |
Optimizer opt = new Optimizer();
|
403 |
opt.optimize(p, OptimizationOptions.SPATIALIZE_GROUPS); |
404 |
|
405 |
// Return node
|
406 |
return p;
|
407 |
} |
408 |
|
409 |
/**
|
410 |
* Create a new tesselation poligon with a color using an List of 3D points
|
411 |
*
|
412 |
* @param points
|
413 |
* List of 3D points
|
414 |
* @param color
|
415 |
* Color of the poligon
|
416 |
* @return Poligon node
|
417 |
*/
|
418 |
public static Node insertPolygon(List points, Vec4 color) { |
419 |
// Creating a polygon
|
420 |
TessellablePolygon p = new TessellablePolygon();
|
421 |
|
422 |
// Set up parameters
|
423 |
// p.setType(Polygon.PolygonType.FILLED_POLYGON);
|
424 |
p.setWidth(2f);
|
425 |
// p.setPattern((short) 0xFFFF);
|
426 |
// p.setFactor(2);
|
427 |
p.setType(Polygon.PolygonType.FILLED_POLYGON);
|
428 |
// p.setType(Polygon.PolygonType.EMPTY_POLYGON);
|
429 |
// p.setType(Polygon.PolygonType.PATTERN_POLYGON);
|
430 |
|
431 |
// Blending only is active if the alpha component are diferent that 1
|
432 |
if (color.w() != 1) { |
433 |
p.setEnabledBlending(true);
|
434 |
} |
435 |
// Adding points
|
436 |
for (int i = 0; i < points.size(); i++) { |
437 |
Vec3 element = (Vec3) points.get(i); |
438 |
p.addVertex(element, color); |
439 |
} |
440 |
|
441 |
// p.setTexture("C:/modelos3d/Images/land_shallow_topo_2048.jpg");
|
442 |
|
443 |
// Tesselation of this poligon
|
444 |
p.tesselate(); |
445 |
|
446 |
// Optimizer generated nodes
|
447 |
Optimizer opt = new Optimizer();
|
448 |
opt.optimize(p, OptimizationOptions.ALL_OPTIMIZATIONS); |
449 |
|
450 |
// Return node
|
451 |
return p;
|
452 |
} |
453 |
|
454 |
/**
|
455 |
* Method to create a new 3D line. With a List of 3D points and a color
|
456 |
*
|
457 |
* @param points
|
458 |
* List of 3D points
|
459 |
* @param color
|
460 |
* Color of the line
|
461 |
* @return
|
462 |
*
|
463 |
*/
|
464 |
public static Node insertLine(List points, Vec4 color, float size) { |
465 |
Vec3 ori = null, fin = null; |
466 |
|
467 |
// Getting first points
|
468 |
if (points.size() > 1) { |
469 |
ori = (Vec3) points.get(0);
|
470 |
fin = (Vec3) points.get(1);
|
471 |
} |
472 |
|
473 |
// If the line do not have more than 2 points return null
|
474 |
if ((ori == null) || (fin == null)) |
475 |
return null; |
476 |
|
477 |
// Create new polyline
|
478 |
Polyline p = new Polyline(ori, fin);
|
479 |
p.setVertexColor(0, color);
|
480 |
p.setVertexColor(1, color);
|
481 |
|
482 |
// Set up parameters
|
483 |
p.setWidth(size); |
484 |
p.setPattern((short) 0xFFFF); |
485 |
p.setFactor(2);
|
486 |
|
487 |
// Blending only is active if the alpha component are diferent that 1
|
488 |
if (color.w() != 1) { |
489 |
p.setEnabledBlending(true);
|
490 |
} |
491 |
|
492 |
// Enabling antialiasing
|
493 |
p.setEnabledSmoothing(true);
|
494 |
|
495 |
p.setColor(color); |
496 |
|
497 |
// Adding points
|
498 |
for (int i = 2; i < points.size(); i++) { |
499 |
Vec3 element = (Vec3) points.get(i); |
500 |
p.addVertex(element, color); |
501 |
} |
502 |
|
503 |
// Optimizer generated nodes
|
504 |
Optimizer opt = new Optimizer();
|
505 |
opt.optimize(p); |
506 |
|
507 |
// Return node
|
508 |
return p;
|
509 |
|
510 |
} |
511 |
|
512 |
/**
|
513 |
* Create a new group of 3D points with a color and size. Using a List of 3D
|
514 |
* points
|
515 |
*
|
516 |
* @param points
|
517 |
* List of 3D points
|
518 |
* @param color
|
519 |
* Color of the line
|
520 |
* @param size
|
521 |
* Size of the point (in pixels)
|
522 |
* @return
|
523 |
*/
|
524 |
public static Node insertPoints(List points, Vec4 color, float size) { |
525 |
// Creating point
|
526 |
PixelPoint p = new PixelPoint();
|
527 |
|
528 |
// Set up parameters
|
529 |
p.setPointSize(size); |
530 |
|
531 |
// Adding points
|
532 |
for (int i = 0; i < points.size(); i++) { |
533 |
Vec3 element = (Vec3) points.get(i); |
534 |
p.addPoint(element, color); |
535 |
} |
536 |
|
537 |
// Optimizer generated nodes
|
538 |
Optimizer opt = new Optimizer();
|
539 |
opt.optimize(p); |
540 |
|
541 |
// Return node
|
542 |
return p;
|
543 |
|
544 |
} |
545 |
|
546 |
/**
|
547 |
* Create a new group of 3D labels with a size and heigth. Using a List of
|
548 |
* 3D points
|
549 |
*
|
550 |
* @param labels
|
551 |
* List of Flabels
|
552 |
*
|
553 |
* @param size
|
554 |
* Size of the labels
|
555 |
* @param heigth
|
556 |
* Heigth of labels
|
557 |
* @param inPixels
|
558 |
* True indicates that size is in pixel and false indicates real
|
559 |
* size;
|
560 |
* @param type
|
561 |
* @return Node group
|
562 |
*/
|
563 |
public static Node insertLabels(List labels, int size, float heigth, |
564 |
boolean inPixels, int planetType) { |
565 |
// Creating a group node
|
566 |
Group p = new Group(); |
567 |
|
568 |
// Getting total size
|
569 |
int total = labels.size();
|
570 |
|
571 |
// this is for control a max of labels because the system not suport
|
572 |
// more than 10000
|
573 |
int max = 3000; |
574 |
if (total > max)
|
575 |
total = max; |
576 |
|
577 |
if (labels != null) { |
578 |
for (int i = 0; i < total; i++) { |
579 |
FLabel f = (FLabel) labels.get(i); |
580 |
|
581 |
// Set up parameters
|
582 |
|
583 |
// Getting color
|
584 |
// Color co = f.getColor();
|
585 |
Color co = new Color(255, 255, 255, 255); |
586 |
float r, g, b, a;
|
587 |
r = ((float) co.getRed()) / 255.0f; |
588 |
g = ((float) co.getGreen()) / 255.0f; |
589 |
b = ((float) co.getBlue()) / 255.0f; |
590 |
a = ((float) co.getAlpha()) / 255.0f; |
591 |
// Set color
|
592 |
Vec4 textColor = new Vec4(r, g, b, a);
|
593 |
|
594 |
// Get label name
|
595 |
String label = f.getString();
|
596 |
// double heigth = h;
|
597 |
Vec3 pos = null;
|
598 |
if (planetType == PlanetType.PROJECTED) {
|
599 |
pos = new Vec3(f.getOrig().getX(), f.getOrig().getY(),
|
600 |
heigth); |
601 |
} else {
|
602 |
// Transform geodesical coordinates to cartesian
|
603 |
Vec3 posGeo = new Vec3(f.getOrig().getY(), f.getOrig()
|
604 |
.getX(), heigth); |
605 |
|
606 |
pos = planet.convertLatLongHeightToXYZ(posGeo); |
607 |
} |
608 |
|
609 |
// Adding new child text nodes
|
610 |
p.addChild(insertTextoS(label, pos, textColor, size, inPixels)); |
611 |
} |
612 |
} |
613 |
|
614 |
// Optimizer generated nodes
|
615 |
Optimizer opt = new Optimizer();
|
616 |
opt.optimize(p, OptimizationOptions.SPATIALIZE_GROUPS); |
617 |
|
618 |
return p;
|
619 |
|
620 |
} |
621 |
|
622 |
} |