Statistics
| Revision:

root / trunk / libraries / libAnimation2D / src / main / java / com / iver / cit / gvsig / animation / interpolator / Interpolator2D.java @ 25868

History | View | Annotate | Download (4.36 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*/
19

    
20
package com.iver.cit.gvsig.animation.interpolator;
21

    
22
import java.awt.geom.Rectangle2D;
23
import java.util.List;
24

    
25
import com.iver.cit.gvsig.animation.keyFrame.KeyFrame2D;
26
import com.iver.cit.gvsig.animation.keyframe.IKeyFrame;
27
import com.iver.cit.gvsig.animation.keyframe.interpolator.IInterpolator;
28
import com.iver.cit.gvsig.animation.keyframe.interpolator.IInterpolatorTimeFuntion;
29
import com.iver.cit.gvsig.project.ProjectExtent;
30
import com.iver.utiles.XMLEntity;
31

    
32
/**
33
 * @author ?ngel Fraile Gri??n  e-mail: angel.fraile@iver.es
34
 * @since 1.1
35
 * 
36
 */
37

    
38
public class Interpolator2D implements IInterpolator {
39

    
40
        private String description = "Interpolaci?n basada en encuadres";
41
        private String name = "Interpolator2D";
42
        
43
        private IInterpolatorTimeFuntion funtion; 
44

    
45
        /**
46
         *         Calculating the new positions in the movement animation
47
         */
48
        public IKeyFrame interpolate(List<IKeyFrame> kfList, int index, double time) {
49
                KeyFrame2D KF = new KeyFrame2D();
50

    
51
                if (kfList == null)
52
                        return null;
53

    
54

    
55
                switch (kfList.size()) {
56
                // this case when there are only has 2 keyframes
57
                case 2:
58
                        // getting the keyframes
59
                        KeyFrame2D kf1 = (KeyFrame2D) kfList.get(0);
60
                        KeyFrame2D kf2 = (KeyFrame2D) kfList.get(1);
61

    
62
                        if (index == 1) {
63
                                KeyFrame2D kaux = kf1;
64
                                kf1 = kf2;
65
                                kf2 = kaux;
66
                        }
67

    
68
                        ProjectExtent vp1 = (ProjectExtent) kf1.getAnimatedObject();
69
                        ProjectExtent vp2 = (ProjectExtent) kf2.getAnimatedObject();
70

    
71

    
72
                        double min1 = vp1.getExtent().getMinX();
73
                        double time1 = kf1.getTime();
74
                        double min2 = vp2.getExtent().getMinX();
75
                        double time2 = kf2.getTime();
76
                        double left = linearInterpolate(min1, min2, time1, time2, time);
77
                        min1 = vp1.getExtent().getMinY();
78
                        min2 = vp2.getExtent().getMinY();
79
                        double top = linearInterpolate(min1, min2, time1, time2, time);
80
                        min1 = vp1.getExtent().getWidth();
81
                        min2 = vp2.getExtent().getWidth();
82
                        double right = linearInterpolate(min1, min2, time1, time2, time);
83
                        min1 = vp1.getExtent().getHeight();
84
                        min2 = vp2.getExtent().getHeight();
85
                        double bottom = linearInterpolate(min1, min2, time1, time2, time);
86

    
87
                        Rectangle2D newExtent = new Rectangle2D.Double(left, top, right,
88
                                        bottom);
89
                        ProjectExtent pe = new ProjectExtent();
90
                        pe.setExtent(newExtent);
91
                        KF.setAnimatedObject(pe);
92
                        break;
93
                }
94

    
95
                return KF;
96
        }
97

    
98
        /**
99
         * Return a value interpolate 
100
         * 
101
         * @param minX: initial x value in the 2D extent
102
         * @param minX2: final x value in the 2D extent
103
         * @param timePos: initial time value
104
         * @param timePos2: final time value
105
         * @param time: total time
106
         * @return
107
         */
108
        private double linearInterpolate(double minX, double minX2, double timePos,
109
                        double timePos2, double time) {
110
                // P1 + (P2-P1)*((t-t1)/(t2-t1))
111
                return (minX + (minX2 - minX)
112
                                * ((time - timePos) / (timePos2 - timePos)));
113
        }
114

    
115
        public IInterpolatorTimeFuntion getFuntion() {
116
                return this.funtion;
117
        }
118

    
119
        public void setFuntion(IInterpolatorTimeFuntion funtion) {
120
                this.funtion = funtion;
121
        }
122

    
123
        public String getClassName() {
124
                return this.getClass().getName();
125
        }
126

    
127
        public String getDescription() {
128
                return this.description;
129
        }
130

    
131
        public String getName() {
132
                return this.name;
133
        }
134

    
135
        public XMLEntity getXMLEntity() {
136
                XMLEntity xml = new XMLEntity();        
137
                xml.putProperty("className", this.getClassName());
138
                xml.putProperty("description", this.description);
139
                return xml;
140
        }
141
        
142
        public void setXMLEntity(XMLEntity xml) {
143
                if (xml.contains("description"))
144
                        this.description = xml.getStringProperty("description");
145
                
146
        }
147
}