001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005
006package armyc2.c5isr.renderer.utilities;
007
008//import android.graphics.Point;
009import android.graphics.PointF;
010//import Point2D;
011//import Point2D;
012import armyc2.c5isr.graphics2d.*;
013import armyc2.c5isr.graphics2d.Point2D;
014
015/**
016 *
017*
018 */
019public class PointConversion implements IPointConversion {
020    int _pixelWidth = 0;
021    int _PixelHeight = 0;
022    double _geoTop = 0;
023    double _geoLeft = 0;
024    double _geoBottom = 0;
025    double _geoRight = 0;
026
027    //pixels to geo
028    //double _geoMultiplierX = 0;
029    //double _geoMultiplierY = 0;
030    //geo to pixels
031    double _pixelMultiplierX = 0;
032    double _pixelMultiplierY = 0;
033    boolean _normalize = true;
034    public void set_normalize(boolean value)
035    {
036        _normalize=value;
037    }
038
039    public PointConversion(int pixelWidth, int pixelHeight,
040                            double geoTop, double geoLeft,
041                            double geoBottom, double geoRight)
042    {/*
043            _pixelWidth = pixelWidth;
044            _PixelHeight = pixelHeight;
045            _geoTop = geoTop;
046            _geoLeft = geoLeft;
047            _geoBottom = geoBottom;
048            _geoRight = geoRight;*/
049
050            UpdateExtents(pixelWidth, pixelHeight, geoTop, geoLeft, geoBottom, geoRight);
051    }
052
053    public void UpdateExtents(int pixelWidth, int pixelHeight,
054                            double geoTop, double geoLeft,
055                            double geoBottom, double geoRight)
056    {
057            _pixelWidth = pixelWidth;
058            _PixelHeight = pixelHeight;
059            _geoTop = geoTop;
060            _geoLeft = geoLeft;
061            _geoBottom = geoBottom;
062            _geoRight = geoRight;
063
064            //_geoMultiplierX = ((double)_pixelWidth) / (_geoRight - _geoLeft) ;
065            //_geoMultiplierY = ((double)_PixelHeight) / (_geoTop - _geoBottom) ;
066
067            _pixelMultiplierX = (_geoRight - _geoLeft) / ((double)_pixelWidth) ;
068            _pixelMultiplierY = (_geoTop - _geoBottom) / ((double)_PixelHeight) ;   
069            
070            if(_geoRight-_geoLeft < -180)
071            {
072                _pixelMultiplierX = (_geoRight - _geoLeft + 360) / ((double)_pixelWidth) ;                
073            }
074            if(_geoRight-_geoLeft > 180)
075            {
076                _pixelMultiplierX = (360 - (_geoRight - _geoLeft) ) / ((double)_pixelWidth) ;                
077            }
078            if(_geoTop < _geoBottom)            
079                _pixelMultiplierY = -Math.abs(_pixelMultiplierY);
080            else
081                _pixelMultiplierY = Math.abs(_pixelMultiplierY);
082            
083//            if(_geoRight < _geoLeft)            
084//                _pixelMultiplierX = -Math.abs(_pixelMultiplierX);
085//            else
086//                _pixelMultiplierX = Math.abs(_pixelMultiplierX);
087            //end section
088    }
089
090    @Override
091    public PointF PixelsToGeo(PointF pixel)
092    {
093        PointF coords = new PointF();
094
095        coords.x = (float)(pixel.x * _pixelMultiplierX + _geoLeft); //xMultiplier;
096        coords.y = (float)(_geoTop - (pixel.y * _pixelMultiplierY));
097        
098        //diagnostic 12-18-12
099        if(coords.x > 180)
100            coords.x -= 360;
101        if(coords.x < -180)
102            coords.x += 360;
103        //end section
104        
105        return coords;
106    }
107
108    @Override
109    public PointF GeoToPixels(PointF coord)
110    {
111        PointF pixel = new PointF();
112
113        //double xMultiplier = _pixelMultiplierX;//(_geoRight - _geoLeft) / ((double)_pixelWidth) ;
114        //double yMultiplier = _pixelMultiplierY;//(_geoTop - _geoBottom) / ((double)_PixelHeight) ;
115        double temp;
116
117        //temp = ((coord.x  - _geoLeft) / _pixelMultiplierX);//xMultiplier);
118        double calcValue=coord.x  - _geoLeft;
119        if(_normalize)
120        {
121            if(calcValue<-180)
122                calcValue+=360;
123            else if(calcValue>180)
124                calcValue-=360;
125        }
126        temp = (calcValue / _pixelMultiplierX);//xMultiplier);
127
128        pixel.x = (float)temp;
129
130        temp = ((_geoTop - coord.y) / _pixelMultiplierY);//yMultiplier);
131        pixel.y = (float)temp;
132                
133        return pixel;
134    }
135    
136//      @Override
137//      public Point2D PixelsToGeo(Point pixel) {
138//              Point2D coords = new Point2D.Double();
139//      
140//      double x = (float)(pixel.x * _pixelMultiplierX + _geoLeft); //xMultiplier;
141//        double y = (float)(_geoTop - (pixel.y * _pixelMultiplierY));
142//        
143//        //diagnostic 12-18-12
144//        if(x > 180)
145//            x -= 360;
146//        if(x < -180)
147//            x += 360;
148//        //end section
149//        
150//        coords.setLocation(x, y);
151//        
152//        return coords;
153//      }
154
155//      @Override
156//      public Point GeoToPixels(Point2D coord) {
157//              Point pixel = new Point();
158//              int x = 0;
159//              int y = 0;
160//        double temp;
161//
162//        temp = ((coord.getX()  - _geoLeft) / _pixelMultiplierX);//xMultiplier);
163//
164//        x = (int)temp;
165//
166//        temp = ((_geoTop - coord.getY()) / _pixelMultiplierY);//yMultiplier);
167//        y = (int)temp;
168//                
169//        pixel.x = x;
170//        pixel.y = y;
171//        return pixel;
172//      }
173
174        @Override
175        public Point2D PixelsToGeo(Point2D pixel) {
176                
177                Point2D coords = new Point2D.Double();
178        
179        double x = (float)(pixel.getX() * _pixelMultiplierX + _geoLeft); //xMultiplier;
180        double y = (float)(_geoTop - (pixel.getY() * _pixelMultiplierY));
181        
182        //diagnostic 12-18-12
183        if(x > 180)
184            x -= 360;
185        if(x < -180)
186            x += 360;
187        //end section
188        
189        coords.setLocation(x, y);
190        
191        return coords;
192        }
193
194        //@Override
195        public Point2D GeoToPixels(Point2D coord) {
196                
197                Point2D pixel = new Point2D.Double();
198                double x = 0;
199                double y = 0;
200        double temp;
201
202        //temp = ((coord.getX()  - _geoLeft) / _pixelMultiplierX);//xMultiplier);
203        double calcValue=coord.getX()  - _geoLeft;
204        if(_normalize)
205        {
206            if(calcValue<-180)
207                calcValue+=360;
208            else if(calcValue>180)
209                calcValue-=360;
210        }
211        temp = (calcValue / _pixelMultiplierX);//xMultiplier);
212
213        x = temp;
214
215        temp = ((_geoTop - coord.getY()) / _pixelMultiplierY);//yMultiplier);
216        y = temp;
217                
218        pixel.setLocation(x,y);
219        return pixel;
220        }
221
222
223    public int getPixelWidth()
224    {
225        return _pixelWidth;
226    }
227
228    public int getPixelHeight()
229    {
230        return _PixelHeight;
231    }
232
233    public double getUpperLat()
234    {
235        return _geoTop;
236    }
237
238    public double getLowerLat()
239    {
240        return _geoBottom;
241    }
242
243    public double getLeftLon()
244    {
245        return _geoLeft;
246    }
247
248    public double getRightLon()
249    {
250        return _geoRight;
251    }
252
253
254
255
256
257}