svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.operation / src / main / java / org / gvsig / fmap / geom / operation / DrawInts.java @ 40559
History | View | Annotate | Download (5.88 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | package org.gvsig.fmap.geom.operation; |
25 | |||
26 | import java.awt.Graphics2D; |
||
27 | import java.awt.geom.AffineTransform; |
||
28 | import java.awt.geom.PathIterator; |
||
29 | |||
30 | import org.gvsig.fmap.geom.Geometry; |
||
31 | import org.gvsig.fmap.geom.GeometryLocator; |
||
32 | import org.gvsig.fmap.geom.GeometryManager; |
||
33 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
34 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
35 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
36 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
37 | import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport; |
||
38 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
||
39 | import org.gvsig.tools.task.Cancellable; |
||
40 | import org.slf4j.Logger; |
||
41 | import org.slf4j.LoggerFactory; |
||
42 | |||
43 | public class DrawInts extends GeometryOperation { |
||
44 | private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
45 | public static final String NAME = "drawInts"; |
||
46 | public static int CODE = Integer.MIN_VALUE; |
||
47 | |||
48 | final static private Logger logger = LoggerFactory.getLogger(DrawInts.class); |
||
49 | |||
50 | public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException{ |
||
51 | DrawOperationContext doc=(DrawOperationContext)ctx; |
||
52 | ViewPort viewPort = doc.getViewPort(); |
||
53 | ISymbol symbol = doc.getSymbol(); |
||
54 | Graphics2D g=doc.getGraphics();
|
||
55 | Cancellable cancel=doc.getCancellable(); |
||
56 | // make the symbol to resize itself with the current rendering context
|
||
57 | try {
|
||
58 | if (doc.hasDPI()){
|
||
59 | double previousSize = ((CartographicSupport)symbol).
|
||
60 | toCartographicSize(viewPort, doc.getDPI(), geom); |
||
61 | // draw it as normally
|
||
62 | Geometry decimatedShape = transformToInts(geom, viewPort.getAffineTransform()); |
||
63 | |||
64 | //the AffineTransform has to be null because the transformToInts method
|
||
65 | //reprojects the geometry
|
||
66 | symbol.draw(g, null, decimatedShape, doc.getFeature(), cancel);
|
||
67 | |||
68 | // restore previous size
|
||
69 | ((CartographicSupport)symbol).setCartographicSize(previousSize, geom); |
||
70 | }else{
|
||
71 | Geometry decimatedShape = transformToInts(geom, viewPort.getAffineTransform()); |
||
72 | symbol.draw(g, viewPort.getAffineTransform(), decimatedShape, |
||
73 | doc.getFeature(), cancel); |
||
74 | } |
||
75 | } catch (CreateGeometryException e) {
|
||
76 | e.printStackTrace(); |
||
77 | throw new GeometryOperationException(e); |
||
78 | } |
||
79 | |||
80 | return null; |
||
81 | } |
||
82 | |||
83 | public int getOperationIndex() { |
||
84 | return CODE;
|
||
85 | } |
||
86 | |||
87 | private Geometry transformToInts(Geometry gp, AffineTransform at) throws CreateGeometryException { |
||
88 | GeneralPathX newGp = new GeneralPathX();
|
||
89 | double[] theData = new double[6]; |
||
90 | double[] aux = new double[6]; |
||
91 | |||
92 | // newGp.reset();
|
||
93 | PathIterator theIterator;
|
||
94 | int theType;
|
||
95 | int numParts = 0; |
||
96 | |||
97 | java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
|
||
98 | java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
|
||
99 | boolean bFirst = true; |
||
100 | int xInt, yInt, antX = -1, antY = -1; |
||
101 | |||
102 | |||
103 | theIterator = gp.getPathIterator(null); //, flatness); |
||
104 | int numSegmentsAdded = 0; |
||
105 | while (!theIterator.isDone()) {
|
||
106 | theType = theIterator.currentSegment(theData); |
||
107 | |||
108 | switch (theType) {
|
||
109 | case PathIterator.SEG_MOVETO: |
||
110 | numParts++; |
||
111 | ptSrc.setLocation(theData[0], theData[1]); |
||
112 | at.transform(ptSrc, ptDst); |
||
113 | antX = (int) ptDst.getX();
|
||
114 | antY = (int) ptDst.getY();
|
||
115 | newGp.moveTo(antX, antY); |
||
116 | numSegmentsAdded++; |
||
117 | bFirst = true;
|
||
118 | break;
|
||
119 | |||
120 | case PathIterator.SEG_LINETO: |
||
121 | ptSrc.setLocation(theData[0], theData[1]); |
||
122 | at.transform(ptSrc, ptDst); |
||
123 | xInt = (int) ptDst.getX();
|
||
124 | yInt = (int) ptDst.getY();
|
||
125 | if ((bFirst) || ((xInt != antX) || (yInt != antY)))
|
||
126 | { |
||
127 | newGp.lineTo(xInt, yInt); |
||
128 | antX = xInt; |
||
129 | antY = yInt; |
||
130 | bFirst = false;
|
||
131 | numSegmentsAdded++; |
||
132 | } |
||
133 | break;
|
||
134 | |||
135 | case PathIterator.SEG_QUADTO: |
||
136 | at.transform(theData,0,aux,0,2); |
||
137 | newGp.quadTo(aux[0], aux[1], aux[2], aux[3]); |
||
138 | numSegmentsAdded++; |
||
139 | break;
|
||
140 | |||
141 | case PathIterator.SEG_CUBICTO: |
||
142 | at.transform(theData,0,aux,0,3); |
||
143 | newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]); |
||
144 | numSegmentsAdded++; |
||
145 | break;
|
||
146 | |||
147 | case PathIterator.SEG_CLOSE: |
||
148 | if (numSegmentsAdded < 3) { |
||
149 | newGp.lineTo(antX, antY); |
||
150 | } |
||
151 | newGp.closePath(); |
||
152 | |||
153 | break;
|
||
154 | } //end switch
|
||
155 | |||
156 | theIterator.next(); |
||
157 | } //end while loop
|
||
158 | |||
159 | Geometry geom = null;
|
||
160 | switch (gp.getType()) {
|
||
161 | case Geometry.TYPES.POINT:
|
||
162 | geom = geomManager.createPoint(ptDst.getX(), ptDst.getY(), SUBTYPES.GEOM2D); |
||
163 | break;
|
||
164 | case Geometry.TYPES.CURVE:
|
||
165 | case Geometry.TYPES.ARC:
|
||
166 | geom = geomManager.createCurve(newGp, SUBTYPES.GEOM2D); |
||
167 | break;
|
||
168 | |||
169 | case Geometry.TYPES.SURFACE:
|
||
170 | case Geometry.TYPES.CIRCLE:
|
||
171 | case Geometry.TYPES.ELLIPSE:
|
||
172 | |||
173 | geom = geomManager.createSurface(newGp, SUBTYPES.GEOM2D); |
||
174 | break;
|
||
175 | } |
||
176 | return geom;
|
||
177 | } |
||
178 | |||
179 | public static void register() { |
||
180 | |||
181 | GeometryManager gm = GeometryLocator.getGeometryManager(); |
||
182 | CODE = gm.getGeometryOperationCode(NAME); |
||
183 | geomManager.registerGeometryOperation(NAME, new DrawInts());
|
||
184 | } |
||
185 | |||
186 | |||
187 | } |