Statistics
| Revision:

root / branches / 3D_Animation_prepto_osgvp_2_2_0 / libraries / libAnimation3D / src / main / java / com / iver / cit / gvsig / animation / interpolator / Interpolator3DSpherical.java @ 27018

History | View | Annotate | Download (4.95 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.util.List;
23

    
24
import org.gvsig.osgvp.core.osg.Matrix;
25
import org.gvsig.osgvp.core.osg.Quat;
26
import org.gvsig.osgvp.core.osg.Vec3;
27
import org.gvsig.osgvp.viewer.Camera;
28

    
29
import com.iver.ai2.gvsig3d.camera.ProjectCamera;
30
import com.iver.cit.gvsig.animation.keyFrame.KeyFrame3DFlat;
31
import com.iver.cit.gvsig.animation.keyframe.IKeyFrame;
32
import com.iver.cit.gvsig.animation.keyframe.interpolator.IInterpolator;
33
import com.iver.cit.gvsig.animation.keyframe.interpolator.IInterpolatorTimeFuntion;
34
import com.iver.utiles.XMLEntity;
35

    
36

    
37
/**
38
 * @author ?ngel Fraile Gri??n  e-mail: angel.fraile@iver.es
39
 * @since 1.1
40
 * 
41
 */
42
public class Interpolator3DSpherical implements IInterpolator {
43

    
44
        private IInterpolatorTimeFuntion function;
45
        private String description = "Interpolaci?n basada en encuadres";
46
        private String name = "Interpolator3DSpherical";
47
        
48
        
49
        /**
50
         *         Calculating the new positions in the movement animation
51
         */
52
        
53
        public IKeyFrame interpolate(List<IKeyFrame> kfList, int index, double time) {
54
                KeyFrame3DFlat KF = new KeyFrame3DFlat();
55

    
56
                if (kfList == null)
57
                        return null;
58

    
59
                                
60
                /*Interpolaci?n por quaterniones.*/
61
                
62
                double newTime = this.getFuntion().interpolate(time);
63
                
64
                switch (kfList.size()) {
65
                // this case when there are only has 2 keyframes
66
                case 2:
67
                        // getting the keyframes
68
                        KeyFrame3DFlat kf1 = (KeyFrame3DFlat) kfList.get(0);
69
                        KeyFrame3DFlat kf2 = (KeyFrame3DFlat) kfList.get(1);
70

    
71
                        if (index == 1) {
72
                                KeyFrame3DFlat kaux = kf1;
73
                                kf1 = kf2;
74
                                kf2 = kaux;
75
                        }
76

    
77
                        if ((kf1 == null) ||(kf2 == null))
78
                                return null;
79
                        
80
                        ProjectCamera vp1 = (ProjectCamera) kf1.getAnimatedObject();
81
                        ProjectCamera vp2 = (ProjectCamera) kf2.getAnimatedObject();
82

    
83
                        Camera cam1 = vp1.getCamera();
84
                        Camera cam2 = vp2.getCamera();
85
                        double time1 = kf1.getTime();
86
                        double time2 = kf2.getTime();        
87
                        
88
                        newTime = (newTime-time1)/(time2-time1);//normalize time 0..1
89
                        
90
                        Quat from = cam1.getViewMatrix().getRotate();
91
                        Quat to = cam2.getViewMatrix().getRotate();
92

    
93
                        Vec3 trans1 = cam1.getViewMatrix().getTrans();
94
                        Vec3 trans2 = cam2.getViewMatrix().getTrans();
95

    
96
                        Vec3 new_trans = vec3LinearInterpolate(trans1, trans2, newTime);
97
                        Matrix m1 = Matrix.translate(new_trans);
98
                        Quat q = sphericalInterpolate(from, to, newTime);
99
                        Matrix m = Matrix.rotate(q);
100
                        Matrix m2 = m.prod(m1);
101
                        
102
                        Camera c = new Camera();
103
                        c.setViewMatrix(m2);
104
                
105
                        ProjectCamera pe = new ProjectCamera();
106
                        pe.setCamera(c);
107
                        KF.setName("temporal_keyframe");
108
                        KF.setAnimatedObject(pe);
109
                        break;
110

    
111
                }
112
                
113
                
114
                return KF;
115
        }
116
        
117
        /**
118
         * Spherical interpolation based in quaternions
119
         * @param from: initial position 
120
         * @param to: final position
121
         * @param time: total time
122
         * @return the actual position of the earth movement 
123
         */
124
        private Quat sphericalInterpolate(Quat from, Quat to, double time) {
125

    
126
                Quat fromQuat = new Quat();
127

    
128
                return fromQuat.slerp(time, from, to);
129

    
130
        }
131

    
132
//        private double doubleInterpolate(double from, double to, double time) {
133
//                if (from == to)
134
//                        return from;
135
//                return from + (to - from) * time;
136
//        }
137

    
138
        private Vec3 vec3LinearInterpolate(Vec3 from, Vec3 to, double time) {
139
                if (from.equals(to))
140
                        return new Vec3(from);
141
                return from.sum(to.sub(from).escalarProduct(time));
142
        }
143
        
144
        public IInterpolatorTimeFuntion getFuntion() {
145
                return this.function;
146
        }
147

    
148
        public void setFuntion(IInterpolatorTimeFuntion function) {
149
                this.function = function;
150
                
151
        }
152

    
153
        public String getClassName() {
154
                return this.getClass().getName();
155
        }
156

    
157
        public String getDescription() {
158
                return this.description;
159
        }
160

    
161
        public String getName() {
162
                return this.name;
163
        }
164

    
165
        /**
166
         * IPersistence methods.
167
         */
168
        
169
        public XMLEntity getXMLEntity() {
170
                XMLEntity xml = new XMLEntity();        
171
                xml.putProperty("className", this.getClassName());
172
                xml.putProperty("description", this.description);
173
                return xml;
174
        }
175
        
176
        public void setXMLEntity(XMLEntity xml) {
177
                if (xml.contains("description"))
178
                        this.description = xml.getStringProperty("description");
179
                
180
        }
181
}