source: ether_megapoli/trunk/service/implementation/gov2/noaa/pmel/sgt/LinearTransform.java @ 192

Last change on this file since 192 was 192, checked in by vmipsl, 13 years ago

Servlet _ Contour en cours _ package gov2

File size: 5.7 KB
Line 
1/*
2 * $Id: LinearTransform.java,v 1.10 2003/08/22 23:02:32 dwd Exp $
3 *
4 * This software is provided by NOAA for full, free and open release.  It is
5 * understood by the recipient/user that NOAA assumes no liability for any
6 * errors contained in the code.  Although this software is released without
7 * conditions or restrictions in its use, it is expected that appropriate
8 * credit be given to its author and to the National Oceanic and Atmospheric
9 * Administration should the software be included by the recipient as an
10 * element in other product development.
11 */
12
13package  gov.noaa.pmel.sgt;
14
15import gov.noaa.pmel.util.Range2D;
16import gov.noaa.pmel.util.GeoDate;
17import gov.noaa.pmel.util.TimeRange;
18import gov.noaa.pmel.util.SoTValue;
19import gov.noaa.pmel.util.SoTRange;
20
21/**
22 * Performs a linear transformation on cartesian axes. If the
23 * transformtion is for space the equation is phys = a*user + b
24 * and if time is phys = at*time + bt.
25 *
26 * @author Donald Denbo
27 * @version $Revision: 1.10 $, $Date: 2003/08/22 23:02:32 $
28 * @since 1.0
29 */
30public class LinearTransform extends AxisTransform implements Cloneable {
31  double at_;
32  double bt_;
33  double a_;
34  double b_;
35  /**
36   * Default constructor. Creates a transform with arguments
37   * <code>Transform(0.0, 1.0, 0.0, 1.0)</code>.
38   **/
39  public LinearTransform() {
40    super();
41  }
42  /**
43   * <code>LinearTransform</code> constructor.
44   * This constructor is used to define
45   * transforms that use double user values.
46   *
47   * @param p1 minimum value, physical coordinates
48   * @param p2 maximum value, physical coordinates
49   * @param u1 minimum value, user coordinates
50   * @param u2 maximum value, user coordinates
51   **/
52  public LinearTransform(double p1,double p2,double u1,double u2) {
53    super(p1, p2, u1, u2);
54  }
55  /**
56   * <code>LinearTransform</code> constructor.  This constructor is used to define
57   * transforms that use double user values.
58   *
59   * @param pr physical coordinate range
60   * @param ur user coordinate range
61   * @see Range2D
62   **/
63  public LinearTransform(Range2D pr,Range2D ur) {
64    super(pr, ur);
65  }
66  /**
67   * <code>LinearTransform</code> constructor.  This constructor is used to define
68   * transforms that use <code>GeoDate</code> user values.
69   *
70   * @param p1 minimum value, physical coordinates
71   * @param p2 maximum value, physical coordinates
72   * @param t1 minimum time
73   * @param t2 maximum time
74   * @see GeoDate
75   **/
76  public LinearTransform(double p1,double p2,GeoDate t1,GeoDate t2) {
77    super(p1, p2, t1, t2);
78  }
79  /**
80   * <code>LinearTransform</code> constructor.  This constructor is used to define
81   * transforms that use <code>GeoDate</code> user values.
82   *
83   * @param pr physical coordinates range
84   * @param tr time range
85   * @see Range2D
86   * @see TimeRange
87   * @see GeoDate
88   **/
89  public LinearTransform(Range2D pr,TimeRange tr) {
90    super(pr, tr);
91  }
92  /**
93   * <code>LinearTransform</code> constructor.  This constructor is used to define
94   * transforms that use <code>SoTRange</code> user values.
95   *
96   * @since 2.0
97   * @param pr physical coordinates range
98   * @param str space or time range
99   * @see SoTRange
100   * @see Range2D
101   **/
102  public LinearTransform(Range2D pr, SoTRange str) {
103    super(pr, str);
104  }
105  /**
106   * Transform from user to physical coordinates.
107   *
108   * @param u user value
109   * @return physical value
110   */
111  public double getTransP(double u) {
112    return a_*u + b_;
113  }
114  /**
115   * Create a copy of the <code>LinearTransform</code>.
116   *
117   * @return the copy
118   */
119  public AxisTransform copy() {
120    LinearTransform newTransform;
121    try {
122      newTransform = (LinearTransform)clone();
123    } catch (CloneNotSupportedException e) {
124      newTransform = new LinearTransform();
125    }
126    return (AxisTransform)newTransform;
127  }
128  //
129  /**
130   * Transform from time to physical coordinates.
131   *
132   * @param t time
133   * @return user value
134   */
135  public double getTransP(GeoDate t) {
136    return (double)(at_*t.getTime() + bt_);
137  }
138  public double getTransP(SoTValue v) {
139    if(v.isTime()) {
140      long t = v.getLongTime();
141      return (double)at_*t + bt_;
142    } else {
143      double u = ((SoTValue.Double)v).getValue();
144      return a_*u + b_;
145    }
146  }
147  /**
148   * Transform from <code>long</code> representation of time
149   * to physical coordinates.
150   *
151   * @since 3.0
152   */
153  public double getTransP(long t) {
154    return (double)at_*t + bt_;
155  }
156  /**
157   * Transform from physical to user coordinates.
158   *
159   * @param p physical value
160   * @return user value
161   */
162  public double getTransU(double p) {
163    return (p - b_)/a_;
164  }
165  /**
166   * Transform from physical coordinates to time.
167   *
168   * @param p physical value
169   * @return time value
170   */
171  public GeoDate getTimeTransU(double p) {
172    return new GeoDate((long)((p - bt_)/at_));
173  }
174  /**
175   * Transform from physical coordinates to <code>long</code>
176   * representation of time.
177   *
178   * @since 3.0
179   * @param p physical value
180   * @return milliseconds since 1970-01-01
181   */
182  public long getLongTimeTransU(double p) {
183    return (long)((p - bt_)/at_);
184  }
185
186  public SoTValue getSoTTransU(double p) {
187    if(!space_) {
188      return new SoTValue.Time((long)((p - bt_)/at_));
189    } else {
190      return new SoTValue.Double((p - b_)/a_);
191    }
192  }
193
194  //
195  void computeTransform() {
196    if(space_) {
197      double denom;
198      denom = u1_ - u2_;
199      if(denom == 0) {
200        a_ = 1.0f;
201        b_ = 0.0f;
202      } else {
203        a_ = (p1_ - p2_)/denom;
204        b_ = p1_ - a_*u1_;
205      }
206    } else {
207      double denom;
208      denom = t1_ - t2_;
209      if(denom == 0) {
210        at_ = 1.0;
211        bt_ = 0.0;
212      } else {
213        at_ = (p1_ - p2_)/denom;
214        bt_ = p1_ - at_*t1_;
215      }
216    }
217  }
218
219  public String toString() {
220    return "LinearTransform: " + a_ + ", " + b_ + "; " + at_ + ", " + bt_;
221  }
222}
Note: See TracBrowser for help on using the repository browser.