~holger-seelig/titania-library/trunk

« back to all changes in this revision

Viewing changes to Components/Interpolation/ScalarInterpolator/ScalarInterpolator.x3dv

  • Committer: Holger Seelig
  • Date: 2017-09-18 02:55:31 UTC
  • Revision ID: holger.seelig@yahoo.de-20170918025531-n60g34kd9bfs41t1
Updated prototypes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#X3D V3.3 utf8 Titania V3.0.4
 
2
 
 
3
PROFILE Full
 
4
 
 
5
META "comment" "World of Titania"
 
6
 
 
7
EXTERNPROTO Grid [
 
8
  inputOutput    SFVec3f    translation
 
9
  inputOutput    SFRotation rotation
 
10
  inputOutput    SFVec3f    scale
 
11
  inputOutput    MFInt32    dimension
 
12
  inputOutput    MFInt32    majorLineEvery
 
13
  inputOutput    MFInt32    majorLineOffset
 
14
  inputOutput    SFVec3f    planeOffset
 
15
  inputOutput    SFColor    planeColor
 
16
  inputOutput    SFFloat    planeTransparency
 
17
  inputOutput    SFColor    lineColor
 
18
  inputOutput    SFFloat    lineTransparency
 
19
  inputOutput    SFColor    majorLineColor
 
20
  inputOutput    SFFloat    majorLineTransparency
 
21
  inputOutput    SFVec3f    numberOffset
 
22
  inputOutput    SFNode     numberAppearance
 
23
  inputOutput    SFNode     numberFontStyle
 
24
  initializeOnly SFBool     solid
 
25
]
 
26
"Grid.x3d#Grid"
 
27
 
 
28
EXTERNPROTO LineTrail [
 
29
  inputOutput SFTime  resetTime
 
30
  inputOutput SFVec3f point
 
31
  inputOutput SFInt32 dimension
 
32
]
 
33
"LineTrail.x3d#LineTrail"
 
34
 
 
35
NavigationInfo {
 
36
  type "NONE"
 
37
}
 
38
 
 
39
Viewpoint {
 
40
  description "Initial View"
 
41
  position 0 0 28.9553
 
42
}
 
43
 
 
44
DEF Grid Group {
 
45
  children [
 
46
    Grid {
 
47
      rotation 1 0 0 1.5707963
 
48
      dimension [ 60, 0, 30 ]
 
49
      planeOffset 0 -0.001 0
 
50
      planeColor 1 1 1
 
51
      planeTransparency 0
 
52
      lineColor 0 0 0
 
53
      majorLineColor 0 0 0
 
54
      majorLineTransparency 0.4
 
55
      numberOffset -0.1 0 0.14
 
56
      numberAppearance Appearance {
 
57
        material Material {
 
58
          diffuseColor 0 0 0
 
59
        }
 
60
      }
 
61
      numberFontStyle ScreenFontStyle {
 
62
        justify [ "END", "BEGIN" ]
 
63
      }
 
64
    }
 
65
    DEF Touch TouchSensor { }
 
66
    DEF PickingScript Script {
 
67
      inputOnly      SFTime  set_touchTime
 
68
      inputOnly      SFVec3f set_hitPoint
 
69
      inputOutput    SFBool  active FALSE
 
70
      outputOnly     SFTime  doubleClickTime
 
71
      initializeOnly SFTime  doubleClickInterval 0.3
 
72
      initializeOnly SFFloat pointSize 0.5
 
73
      initializeOnly SFNode  xInterpolator DEF XInterpolator ScalarInterpolator {
 
74
        key [ 0, 0.375, 0.75, 1 ]
 
75
        keyValue [ -20, -5, 10, 20 ]
 
76
      }
 
77
      initializeOnly SFNode  yInterpolator DEF XInterpolator_1 ScalarInterpolator {
 
78
        key [ -20, -5, 10, 20 ]
 
79
        keyValue [ -10, 5, -5, 10 ]
 
80
      }
 
81
 
 
82
      url "ecmascript:
 
83
 
 
84
var
 
85
        firstTime  = 0,
 
86
        pointIndex = -1,
 
87
        hitPoint   = new SFVec2f (),
 
88
        offset     = new SFVec2f ();
 
89
 
 
90
function initialize ()
 
91
{
 
92
        xInterpolator .key      = new MFFloat (0, 0.375, 0.75, 1);
 
93
        xInterpolator .keyValue = new MFFloat (-20, -5, 10, 20);
 
94
        yInterpolator .keyValue = new MFFloat (-10, 5, -5, 10);
 
95
}
 
96
 
 
97
function set_touchTime (value, time)
 
98
{
 
99
        if (value - firstTime > doubleClickInterval)
 
100
        {
 
101
                firstTime = value;
 
102
                return;
 
103
        }
 
104
 
 
105
        doubleClickTime = time;
 
106
 
 
107
        if (pointIndex < 0 || pointIndex >= xInterpolator .keyValue .length)
 
108
                addPoint ();
 
109
 
 
110
        else
 
111
                removePoint ();
 
112
}
 
113
 
 
114
function set_hitPoint (value, time)
 
115
{
 
116
        hitPoint .x = value .x;
 
117
        hitPoint .y = value .y;
 
118
 
 
119
        if (! active)
 
120
                return;
 
121
 
 
122
        if (pointIndex < 0 || pointIndex >= xInterpolator .keyValue .length)
 
123
                return;
 
124
 
 
125
        xInterpolator .keyValue [pointIndex] = hitPoint .x + offset .x;
 
126
        yInterpolator .keyValue [pointIndex] = hitPoint .y + offset .y;
 
127
}
 
128
 
 
129
function set_active (value, time)
 
130
{
 
131
        if (! active)
 
132
                return;
 
133
 
 
134
        pointIndex = pick (hitPoint);
 
135
}
 
136
 
 
137
function pick (hitPoint)
 
138
{
 
139
        var pointIndex = -1;
 
140
 
 
141
        for (var i = 0, length = xInterpolator .keyValue .length; i < length; ++ i)
 
142
        {
 
143
                var point = new SFVec2f (xInterpolator .keyValue [i],
 
144
                                         yInterpolator .keyValue [i]);
 
145
 
 
146
                offset = point .subtract (hitPoint);
 
147
 
 
148
                if (offset .length () > pointSize)
 
149
                        continue;
 
150
 
 
151
                pointIndex = i;
 
152
                break;
 
153
        }
 
154
        
 
155
        return pointIndex;
 
156
}
 
157
 
 
158
function addPoint ()
 
159
{
 
160
        var pointIndex = getAddPointIndex ()
 
161
 
 
162
        xInterpolator .keyValue .splice (pointIndex, 0, hitPoint .x);
 
163
        yInterpolator .keyValue .splice (pointIndex, 0, hitPoint .y);
 
164
        
 
165
        setXInterpolatorKey ();
 
166
}
 
167
 
 
168
function removePoint ()
 
169
{
 
170
        var pointIndex = getRemovePointIndex ()
 
171
 
 
172
        xInterpolator .keyValue .splice (pointIndex, 1);
 
173
        yInterpolator .keyValue .splice (pointIndex, 1);
 
174
        
 
175
        setXInterpolatorKey ();
 
176
}
 
177
 
 
178
function getAddPointIndex ()
 
179
{
 
180
        for (var i = 0, length = xInterpolator .keyValue .length; i < length; ++ i)
 
181
        {
 
182
                if (xInterpolator .keyValue [i] > hitPoint .x)
 
183
                        break;
 
184
        }
 
185
        
 
186
        return i;
 
187
}
 
188
 
 
189
function getRemovePointIndex ()
 
190
{
 
191
        var
 
192
                first = xInterpolator .keyValue [0];
 
193
                last  = xInterpolator .keyValue [xInterpolator .keyValue .length - 1];
 
194
 
 
195
        if (hitPoint .x < first)
 
196
                return 0;
 
197
 
 
198
        if (hitPoint .x > last)
 
199
                return xInterpolator .keyValue .length - 1;
 
200
 
 
201
        return pick (hitPoint);
 
202
}
 
203
 
 
204
function setXInterpolatorKey ()
 
205
{
 
206
        if (xInterpolator .keyValue .length)
 
207
        {
 
208
                var
 
209
                        first       = xInterpolator .keyValue [0];
 
210
                        last        = xInterpolator .keyValue [xInterpolator .keyValue .length - 1];
 
211
                        distance    = last - first,
 
212
                        totalLength = 0,
 
213
                        lengthSoFar = 0;
 
214
 
 
215
                for (var i = 1, length = xInterpolator .keyValue .length; i < length; ++ i)
 
216
                {
 
217
                        var point0 = new SFVec2f (xInterpolator .keyValue [i - 1],
 
218
                                                       yInterpolator .keyValue [i - 1]);
 
219
                                                       
 
220
                        var point1 = new SFVec2f (xInterpolator .keyValue [i],
 
221
                                                       yInterpolator .keyValue [i]);
 
222
                                                      
 
223
                        totalLength += point1 .subtract (point0) .length ();
 
224
                }
 
225
 
 
226
                xInterpolator .key [0] = 0;
 
227
 
 
228
                for (var i = 1, length = xInterpolator .keyValue .length; i < length; ++ i)
 
229
                {
 
230
                        var point0 = new SFVec2f (xInterpolator .keyValue [i - 1],
 
231
                                                       yInterpolator .keyValue [i - 1]);
 
232
                                                       
 
233
                        var point1 = new SFVec2f (xInterpolator .keyValue [i],
 
234
                                                       yInterpolator .keyValue [i]);
 
235
                                                      
 
236
                        lengthSoFar += point1 .subtract (point0) .length ();
 
237
                        
 
238
                        xInterpolator .key [i] = lengthSoFar / totalLength;
 
239
                }
 
240
 
 
241
                xInterpolator .key .length = length;
 
242
        }
 
243
        else
 
244
        {
 
245
                xInterpolator .key .length = 0;
 
246
        }
 
247
}
 
248
"
 
249
    }
 
250
  ]
 
251
}
 
252
 
 
253
DEF LineTrail Group {
 
254
  children [
 
255
    DEF Timer TimeSensor {
 
256
      cycleInterval 10
 
257
      loop TRUE
 
258
    }
 
259
    USE XInterpolator
 
260
    USE XInterpolator_1
 
261
    DEF TrailScript Script {
 
262
      inputOnly      SFFloat set_y
 
263
      outputOnly     SFVec3f point_changed
 
264
      initializeOnly SFNode  xInterpolator USE XInterpolator
 
265
      initializeOnly SFNode  yInterpolator USE XInterpolator_1
 
266
 
 
267
      url "ecmascript:
 
268
 
 
269
function set_y (value, time)
 
270
{
 
271
        point_changed .x = xInterpolator .value_changed;
 
272
        point_changed .y = yInterpolator .value_changed;
 
273
}
 
274
"
 
275
    }
 
276
    Shape {
 
277
      appearance Appearance {
 
278
        lineProperties LineProperties {
 
279
          linewidthScaleFactor 3
 
280
        }
 
281
        material Material {
 
282
          emissiveColor 0.552941 0.270588 0.14902
 
283
        }
 
284
      }
 
285
      geometry DEF Trail LineTrail {
 
286
        resetTime 1505540070.69239
 
287
        point -4.36295 4.5753 0
 
288
        dimension 1000
 
289
      }
 
290
    }
 
291
  ]
 
292
}
 
293
 
 
294
DEF Points Transform {
 
295
  children [
 
296
    DEF PointsScript Script {
 
297
      inputOnly      MFFloat set_x
 
298
      inputOnly      MFFloat set_y
 
299
      initializeOnly SFNode  points DEF _1 Polypoint2D {
 
300
        point [ -20 -10, -5 5, 10 -5, 20 10 ]
 
301
      }
 
302
      initializeOnly SFNode  xInterpolator USE XInterpolator
 
303
      initializeOnly SFNode  yInterpolator USE XInterpolator_1
 
304
 
 
305
      url "ecmascript:
 
306
 
 
307
function initialize ()
 
308
{
 
309
        eventsProcessed ();
 
310
}
 
311
 
 
312
function eventsProcessed ()
 
313
{
 
314
        for (var i = 0, length = xInterpolator .keyValue .length; i < length; ++ i)
 
315
        {
 
316
                points .point [i] .x = xInterpolator .keyValue [i];
 
317
                points .point [i] .y = yInterpolator .keyValue [i];
 
318
        }
 
319
 
 
320
        points .point .length = i;
 
321
}
 
322
"
 
323
    }
 
324
    Shape {
 
325
      appearance Appearance {
 
326
        lineProperties LineProperties {
 
327
          linewidthScaleFactor 20
 
328
        }
 
329
        material Material {
 
330
          emissiveColor 1 0.75 0.5
 
331
        }
 
332
      }
 
333
      geometry USE _1
 
334
    }
 
335
    Shape {
 
336
      appearance Appearance {
 
337
        lineProperties LineProperties {
 
338
          linewidthScaleFactor 8
 
339
        }
 
340
        material Material {
 
341
          emissiveColor 1 0.5 0
 
342
        }
 
343
      }
 
344
      geometry USE _1
 
345
    }
 
346
  ]
 
347
}
 
348
 
 
349
ROUTE XInterpolator.value_changed TO XInterpolator_1.set_fraction
 
350
ROUTE Timer.fraction_changed TO XInterpolator.set_fraction
 
351
ROUTE XInterpolator_1.value_changed TO TrailScript.set_y
 
352
ROUTE TrailScript.point_changed TO Trail.set_point
 
353
ROUTE Timer.cycleTime TO Trail.set_resetTime
 
354
ROUTE XInterpolator.keyValue_changed TO XInterpolator_1.set_key
 
355
ROUTE XInterpolator.keyValue_changed TO PointsScript.set_x
 
356
ROUTE XInterpolator_1.keyValue_changed TO PointsScript.set_y
 
357
ROUTE Touch.isActive TO PickingScript.set_active
 
358
ROUTE Touch.hitPoint_changed TO PickingScript.set_hitPoint
 
359
ROUTE Touch.touchTime TO PickingScript.set_touchTime
 
360
ROUTE PickingScript.doubleClickTime TO Trail.set_resetTime