Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libAnimation3D / src / main / java / com / iver / cit / gvsig / animation / interpolator / Interpolator3DSpherical.java @ 23595

History | View | Annotate | Download (4.54 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.Matrix;
25
import org.gvsig.osgvp.Quat;
26
import org.gvsig.osgvp.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
public class Interpolator3DSpherical implements IInterpolator {
37

    
38
        private IInterpolatorTimeFuntion function;
39
        private String description = "Interpolaci?n basada en encuadres";
40
        private String name = "Interpolator3DSpherical";
41
        
42
        public IKeyFrame interpolate(List<IKeyFrame> kfList, int index, double time) {
43
                KeyFrame3DFlat KF = new KeyFrame3DFlat();
44

    
45
                if (kfList == null)
46
                        return null;
47

    
48
                                
49
                /*Interpolaci?n por quaterniones.*/
50
                
51
                double newTime = this.getFuntion().interpolate(time);
52
                
53
                switch (kfList.size()) {
54
                // this case when there are only has 2 keyframes
55
                case 2:
56
                        // getting the keyframes
57
                        KeyFrame3DFlat kf1 = (KeyFrame3DFlat) kfList.get(0);
58
                        KeyFrame3DFlat kf2 = (KeyFrame3DFlat) kfList.get(1);
59

    
60
                        if (index == 1) {
61
                                KeyFrame3DFlat kaux = kf1;
62
                                kf1 = kf2;
63
                                kf2 = kaux;
64
                        }
65

    
66
                        if ((kf1 == null) ||(kf2 == null))
67
                                return null;
68
                        
69
                        ProjectCamera vp1 = (ProjectCamera) kf1.getAnimatedObject();
70
                        ProjectCamera vp2 = (ProjectCamera) kf2.getAnimatedObject();
71

    
72
                        Camera cam1 = vp1.getCamera();
73
                        Camera cam2 = vp2.getCamera();
74
                        double time1 = kf1.getTime();
75
                        double time2 = kf2.getTime();        
76
                        
77
                        newTime = (newTime-time1)/(time2-time1);//normalize time 0..1
78
                        
79
                        Quat from = cam1.getViewMatrix().getRotate();
80
                        Quat to = cam2.getViewMatrix().getRotate();
81

    
82
                        Vec3 trans1 = cam1.getViewMatrix().getTrans();
83
                        Vec3 trans2 = cam2.getViewMatrix().getTrans();
84

    
85
                        Vec3 new_trans = vec3LinearInterpolate(trans1, trans2, newTime);
86
                        Matrix m1 = Matrix.translate(new_trans);
87
                        Quat q = sphericalInterpolate(from, to, newTime);
88
                        Matrix m = Matrix.rotate(q);
89
                        Matrix m2 = m.prod(m1);
90
                        
91
                        Camera c = new Camera();
92
                        c.setViewMatrix(m2);
93
                
94
                        ProjectCamera pe = new ProjectCamera();
95
                        pe.setCamera(c);
96
                        KF.setName("temporal_keyframe");
97
                        KF.setAnimatedObject(pe);
98
                        break;
99

    
100
                }
101
                
102
                
103
                return KF;
104
        }
105
        
106
        private Quat sphericalInterpolate(Quat from, Quat to, double time) {
107

    
108
                Quat fromQuat = new Quat();
109

    
110
                return fromQuat.slerp(time, from, to);
111

    
112
        }
113

    
114
//        private double doubleInterpolate(double from, double to, double time) {
115
//                if (from == to)
116
//                        return from;
117
//                return from + (to - from) * time;
118
//        }
119

    
120
        private Vec3 vec3LinearInterpolate(Vec3 from, Vec3 to, double time) {
121
                if (from.equals(to))
122
                        return new Vec3(from);
123
                return from.sum(to.sub(from).escalarProduct(time));
124
        }
125
        
126
        public IInterpolatorTimeFuntion getFuntion() {
127
                return this.function;
128
        }
129

    
130
        public void setFuntion(IInterpolatorTimeFuntion function) {
131
                this.function = function;
132
                
133
        }
134

    
135
        public String getClassName() {
136
                return this.getClass().getName();
137
        }
138

    
139
        public String getDescription() {
140
                return this.description;
141
        }
142

    
143
        public String getName() {
144
                return this.name;
145
        }
146

    
147
        /*
148
         * IPersistence methods.
149
         */
150
        
151
        public XMLEntity getXMLEntity() {
152
                XMLEntity xml = new XMLEntity();        
153
                xml.putProperty("className", this.getClassName());
154
                xml.putProperty("description", this.description);
155
                return xml;
156
        }
157
        
158
        public void setXMLEntity(XMLEntity xml) {
159
                if (xml.contains("description"))
160
                        this.description = xml.getStringProperty("description");
161
                
162
        }
163
}