~holger-seelig/titania-library/trunk

« back to all changes in this revision

Viewing changes to Components/Interpolation/SplineScalarInterpolator/SplineScalarInterpolator.x3d

  • 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
<?xml version="1.0" encoding="UTF-8"?>
 
2
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.3//EN" "http://www.web3d.org/specifications/x3d-3.3.dtd">
 
3
<X3D profile='Full' version='3.3' xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='http://www.web3d.org/specifications/x3d-3.3.xsd'>
 
4
  <head>
 
5
    <meta name='comment' content='World of Titania'/>
 
6
  </head>
 
7
  <Scene>
 
8
    <ExternProtoDeclare name='Grid' url='"Grid.x3d#Grid"'>
 
9
      <field accessType='inputOutput' type='SFVec3f' name='translation'/>
 
10
      <field accessType='inputOutput' type='SFRotation' name='rotation'/>
 
11
      <field accessType='inputOutput' type='SFVec3f' name='scale'/>
 
12
      <field accessType='inputOutput' type='MFInt32' name='dimension'/>
 
13
      <field accessType='inputOutput' type='MFInt32' name='majorLineEvery'/>
 
14
      <field accessType='inputOutput' type='MFInt32' name='majorLineOffset'/>
 
15
      <field accessType='inputOutput' type='SFVec3f' name='planeOffset'/>
 
16
      <field accessType='inputOutput' type='SFColor' name='planeColor'/>
 
17
      <field accessType='inputOutput' type='SFFloat' name='planeTransparency'/>
 
18
      <field accessType='inputOutput' type='SFColor' name='lineColor'/>
 
19
      <field accessType='inputOutput' type='SFFloat' name='lineTransparency'/>
 
20
      <field accessType='inputOutput' type='SFColor' name='majorLineColor'/>
 
21
      <field accessType='inputOutput' type='SFFloat' name='majorLineTransparency'/>
 
22
      <field accessType='inputOutput' type='SFVec3f' name='numberOffset'/>
 
23
      <field accessType='inputOutput' type='SFNode' name='numberAppearance'/>
 
24
      <field accessType='inputOutput' type='SFNode' name='numberFontStyle'/>
 
25
      <field accessType='initializeOnly' type='SFBool' name='solid'/>
 
26
    </ExternProtoDeclare>
 
27
    <ExternProtoDeclare name='LineTrail' url='"LineTrail.x3d#LineTrail"'>
 
28
      <field accessType='inputOutput' type='SFTime' name='resetTime'/>
 
29
      <field accessType='inputOutput' type='SFVec3f' name='point'/>
 
30
      <field accessType='inputOutput' type='SFInt32' name='dimension'/>
 
31
    </ExternProtoDeclare>
 
32
    <NavigationInfo
 
33
        type='"PLANE_create3000.de"'
 
34
        transitionType='"ANIMATE"'/>
 
35
    <Viewpoint
 
36
        description='Initial View'
 
37
        position='0 0 28.9553'/>
 
38
    <Group DEF='Grid'>
 
39
      <ProtoInstance name='Grid'>
 
40
        <fieldValue name='rotation' value='1 0 0 1.5707963'/>
 
41
        <fieldValue name='dimension' value='60, 0, 30'/>
 
42
        <fieldValue name='planeOffset' value='0 -0.001 0'/>
 
43
        <fieldValue name='planeColor' value='1 1 1'/>
 
44
        <fieldValue name='planeTransparency' value='0'/>
 
45
        <fieldValue name='lineColor' value='0 0 0'/>
 
46
        <fieldValue name='majorLineColor' value='0 0 0'/>
 
47
        <fieldValue name='majorLineTransparency' value='0.4'/>
 
48
        <fieldValue name='numberOffset' value='-0.1 0 0.14'/>
 
49
        <fieldValue name='numberAppearance'>
 
50
          <Appearance DEF='_1' containerField='children'>
 
51
            <Material
 
52
                diffuseColor='0 0 0'/>
 
53
          </Appearance>
 
54
        </fieldValue>
 
55
        <fieldValue name='numberFontStyle'>
 
56
          <ScreenFontStyle DEF='_2' containerField='children'
 
57
              justify='"END", "BEGIN"'/>
 
58
        </fieldValue>
 
59
      </ProtoInstance>
 
60
      <TouchSensor DEF='Touch'/>
 
61
      <Script DEF='PickingScript'>
 
62
        <field accessType='inputOnly' type='SFTime' name='set_touchTime'/>
 
63
        <field accessType='inputOnly' type='SFVec3f' name='set_hitPoint'/>
 
64
        <field accessType='inputOutput' type='SFInt32' name='pointType'/>
 
65
        <field accessType='inputOutput' type='SFBool' name='active'/>
 
66
        <field accessType='outputOnly' type='SFTime' name='doubleClickTime'/>
 
67
        <field accessType='initializeOnly' type='SFTime' name='doubleClickInterval' value='0.3'/>
 
68
        <field accessType='initializeOnly' type='SFFloat' name='pointSize' value='0.5'/>
 
69
        <field accessType='initializeOnly' type='SFNode' name='xInterpolator'>
 
70
          <ScalarInterpolator DEF='XInterpolator'
 
71
              key='0, 0.375, 0.75, 1'
 
72
              keyValue='-20, -5, 10, 20'/>
 
73
        </field>
 
74
        <field accessType='initializeOnly' type='SFNode' name='yInterpolator'>
 
75
          <SplineScalarInterpolator DEF='YInterpolator'
 
76
              key='-20, -5, 10, 20'
 
77
              keyValue='-10, 5, -5, 10'
 
78
              keyVelocity='0, 0, 0, 0'/>
 
79
        </field>
 
80
<![CDATA[ecmascript:
 
81
 
 
82
var
 
83
        firstTime     = 0,
 
84
        pointIndex    = -1,
 
85
        hitPoint      = new SFVec2f (),
 
86
        offset        = new SFVec2f (),
 
87
        velocityScale = 0.5,
 
88
        SPLINE        = 0,
 
89
        VELOCITY      = 1;
 
90
 
 
91
function initialize ()
 
92
{
 
93
        xInterpolator .key         = new MFFloat (0, 0.375, 0.75, 1);
 
94
        xInterpolator .keyValue    = new MFFloat (-20, -5, 10, 20);
 
95
        yInterpolator .keyValue    = new MFFloat (-10, 5, -5, 10);
 
96
        yInterpolator .keyVelocity = new MFFloat (0, 0, 0, 0);
 
97
}
 
98
 
 
99
function set_touchTime (value, time)
 
100
{
 
101
        if (pointType == VELOCITY)
 
102
                return;
 
103
 
 
104
        if (value - firstTime > doubleClickInterval)
 
105
        {
 
106
                firstTime = value;
 
107
                return;
 
108
        }
 
109
 
 
110
        doubleClickTime = time;
 
111
 
 
112
        if (pointType == SPLINE)
 
113
        {
 
114
                if (pointIndex < 0 || pointIndex >= xInterpolator .keyValue .length)
 
115
                        addPoint ();
 
116
 
 
117
                else
 
118
                        removePoint ();
 
119
        }
 
120
}
 
121
 
 
122
function set_hitPoint (value, time)
 
123
{
 
124
        hitPoint .x = value .x;
 
125
        hitPoint .y = value .y;
 
126
 
 
127
        if (! active)
 
128
                return;
 
129
 
 
130
        if (pointIndex < 0 || pointIndex >= xInterpolator .keyValue .length)
 
131
                return;
 
132
 
 
133
        switch (pointType)
 
134
        {
 
135
                case SPLINE:
 
136
                        xInterpolator .keyValue [pointIndex] = hitPoint .x + offset .x;
 
137
                        yInterpolator .keyValue [pointIndex] = hitPoint .y + offset .y;
 
138
                        break;
 
139
                case VELOCITY:
 
140
                        yInterpolator .keyVelocity [pointIndex] = ((hitPoint .y + offset .y) - yInterpolator .keyValue [pointIndex]) / velocityScale;
 
141
                        break;
 
142
        }
 
143
}
 
144
 
 
145
function set_active (value, time)
 
146
{
 
147
        if (! active)
 
148
                return;
 
149
 
 
150
        pointIndex = pick (hitPoint);
 
151
}
 
152
 
 
153
function pick (hitPoint)
 
154
{
 
155
        var pointIndex = -1;
 
156
 
 
157
        for (var i = 0, length = xInterpolator .keyValue .length; i < length; ++ i)
 
158
        {
 
159
                var yOffset = 0;
 
160
                
 
161
                if (pointType == VELOCITY)
 
162
                        yOffset = yInterpolator .keyVelocity [i] * velocityScale;
 
163
 
 
164
                var point = new SFVec2f (xInterpolator .keyValue [i],
 
165
                                         yInterpolator .keyValue [i] + yOffset);
 
166
 
 
167
                offset = point .subtract (hitPoint);
 
168
 
 
169
                if (offset .length () > pointSize)
 
170
                        continue;
 
171
 
 
172
                pointIndex = i;
 
173
                break;
 
174
        }
 
175
        
 
176
        return pointIndex;
 
177
}
 
178
 
 
179
function addPoint ()
 
180
{
 
181
        var pointIndex = getAddPointIndex ()
 
182
 
 
183
        xInterpolator .keyValue    .splice (pointIndex, 0, hitPoint .x);
 
184
        yInterpolator .keyValue    .splice (pointIndex, 0, hitPoint .y);
 
185
        yInterpolator .keyVelocity .splice (pointIndex, 0, 0);
 
186
        
 
187
        setXInterpolatorKey ();
 
188
}
 
189
 
 
190
function removePoint ()
 
191
{
 
192
        var pointIndex = getRemovePointIndex ()
 
193
 
 
194
        xInterpolator .keyValue    .splice (pointIndex, 1);
 
195
        yInterpolator .keyValue    .splice (pointIndex, 1);
 
196
        yInterpolator .keyVelocity .splice (pointIndex, 1);
 
197
 
 
198
        setXInterpolatorKey ();
 
199
}
 
200
 
 
201
function getAddPointIndex ()
 
202
{
 
203
        for (var i = 0, length = xInterpolator .keyValue .length; i < length; ++ i)
 
204
        {
 
205
                if (xInterpolator .keyValue [i] > hitPoint .x)
 
206
                        break;
 
207
        }
 
208
        
 
209
        return i;
 
210
}
 
211
 
 
212
function getRemovePointIndex ()
 
213
{
 
214
        var
 
215
                first = xInterpolator .keyValue [0];
 
216
                last  = xInterpolator .keyValue [xInterpolator .keyValue .length - 1];
 
217
 
 
218
        if (hitPoint .x < first)
 
219
                return 0;
 
220
 
 
221
        if (hitPoint .x > last)
 
222
                return xInterpolator .keyValue .length - 1;
 
223
 
 
224
        return pick (hitPoint);
 
225
}
 
226
 
 
227
function setXInterpolatorKey ()
 
228
{
 
229
        if (xInterpolator .keyValue .length)
 
230
        {
 
231
                var
 
232
                        first       = xInterpolator .keyValue [0];
 
233
                        last        = xInterpolator .keyValue [xInterpolator .keyValue .length - 1];
 
234
                        distance    = last - first,
 
235
                        totalLength = 0,
 
236
                        lengthSoFar = 0;
 
237
 
 
238
                for (var i = 1, length = xInterpolator .keyValue .length; i < length; ++ i)
 
239
                {
 
240
                        var point0 = new SFVec2f (xInterpolator .keyValue [i - 1],
 
241
                                                       yInterpolator .keyValue [i - 1]);
 
242
                                                       
 
243
                        var point1 = new SFVec2f (xInterpolator .keyValue [i],
 
244
                                                       yInterpolator .keyValue [i]);
 
245
                                                      
 
246
                        totalLength += point1 .subtract (point0) .length ();
 
247
                }
 
248
 
 
249
                xInterpolator .key [0] = 0;
 
250
 
 
251
                for (var i = 1, length = xInterpolator .keyValue .length; i < length; ++ i)
 
252
                {
 
253
                        var point0 = new SFVec2f (xInterpolator .keyValue [i - 1],
 
254
                                                       yInterpolator .keyValue [i - 1]);
 
255
                                                       
 
256
                        var point1 = new SFVec2f (xInterpolator .keyValue [i],
 
257
                                                       yInterpolator .keyValue [i]);
 
258
                                                      
 
259
                        lengthSoFar += point1 .subtract (point0) .length ();
 
260
                        
 
261
                        xInterpolator .key [i] = lengthSoFar / totalLength;
 
262
                }
 
263
 
 
264
                xInterpolator .key .length = length;
 
265
        }
 
266
        else
 
267
        {
 
268
                xInterpolator .key .length = 0;
 
269
        }
 
270
}
 
271
]]>
 
272
      </Script>
 
273
    </Group>
 
274
    <Group DEF='LineTrail'>
 
275
      <TimeSensor DEF='Timer'
 
276
          cycleInterval='10'
 
277
          loop='true'/>
 
278
      <ScalarInterpolator USE='XInterpolator'/>
 
279
      <SplineScalarInterpolator USE='YInterpolator'/>
 
280
      <Script DEF='TrailScript'>
 
281
        <field accessType='inputOnly' type='SFFloat' name='set_y'/>
 
282
        <field accessType='outputOnly' type='SFVec3f' name='point_changed'/>
 
283
        <field accessType='initializeOnly' type='SFNode' name='xInterpolator'>
 
284
          <ScalarInterpolator USE='XInterpolator'/>
 
285
        </field>
 
286
        <field accessType='initializeOnly' type='SFNode' name='yInterpolator'>
 
287
          <SplineScalarInterpolator USE='YInterpolator'/>
 
288
        </field>
 
289
<![CDATA[ecmascript:
 
290
 
 
291
function set_y (value, time)
 
292
{
 
293
        point_changed .x = xInterpolator .value_changed;
 
294
        point_changed .y = yInterpolator .value_changed;
 
295
}
 
296
]]>
 
297
      </Script>
 
298
      <Shape>
 
299
        <Appearance>
 
300
          <LineProperties
 
301
              linewidthScaleFactor='3'/>
 
302
          <Material
 
303
              emissiveColor='0.552941 0.270588 0.14902'/>
 
304
        </Appearance>
 
305
        <ProtoInstance name='LineTrail' DEF='Trail' containerField='geometry'>
 
306
          <fieldValue name='resetTime' value='1505552564.37677'/>
 
307
          <fieldValue name='point' value='-20 -10 0'/>
 
308
          <fieldValue name='dimension' value='1000'/>
 
309
        </ProtoInstance>
 
310
      </Shape>
 
311
    </Group>
 
312
    <Switch DEF='PointTypeSwitch'
 
313
        whichChoice='0'>
 
314
      <Transform DEF='Points'>
 
315
        <Script DEF='PointsScript'>
 
316
          <field accessType='inputOnly' type='MFFloat' name='set_x'/>
 
317
          <field accessType='inputOnly' type='MFFloat' name='set_y'/>
 
318
          <field accessType='initializeOnly' type='SFNode' name='points'>
 
319
            <Polypoint2D DEF='_3'
 
320
                point='-20 -10, -5 5, 10 -5, 20 10'/>
 
321
          </field>
 
322
          <field accessType='initializeOnly' type='SFNode' name='xInterpolator'>
 
323
            <ScalarInterpolator USE='XInterpolator'/>
 
324
          </field>
 
325
          <field accessType='initializeOnly' type='SFNode' name='yInterpolator'>
 
326
            <SplineScalarInterpolator USE='YInterpolator'/>
 
327
          </field>
 
328
<![CDATA[ecmascript:
 
329
 
 
330
function initialize ()
 
331
{
 
332
        eventsProcessed ();
 
333
}
 
334
 
 
335
function eventsProcessed ()
 
336
{
 
337
        for (var i = 0, length = xInterpolator .keyValue .length; i < length; ++ i)
 
338
        {
 
339
                points .point [i] .x = xInterpolator .keyValue [i];
 
340
                points .point [i] .y = yInterpolator .keyValue [i];
 
341
        }
 
342
 
 
343
        points .point .length = i;
 
344
}
 
345
]]>
 
346
        </Script>
 
347
        <Shape>
 
348
          <Appearance>
 
349
            <LineProperties DEF='_4'
 
350
                linewidthScaleFactor='20'/>
 
351
            <Material
 
352
                emissiveColor='1 0.75 0.5'/>
 
353
          </Appearance>
 
354
          <Polypoint2D USE='_3'/>
 
355
        </Shape>
 
356
        <Shape>
 
357
          <Appearance>
 
358
            <LineProperties DEF='_5'
 
359
                linewidthScaleFactor='8'/>
 
360
            <Material
 
361
                emissiveColor='1 0.5 0'/>
 
362
          </Appearance>
 
363
          <Polypoint2D USE='_3'/>
 
364
        </Shape>
 
365
      </Transform>
 
366
      <Transform DEF='KeyVelocity'>
 
367
        <Script DEF='KeyVelocityScript'>
 
368
          <field accessType='inputOnly' type='MFFloat' name='set_x'/>
 
369
          <field accessType='inputOnly' type='MFFloat' name='set_y'/>
 
370
          <field accessType='inputOnly' type='MFFloat' name='set_velocity'/>
 
371
          <field accessType='initializeOnly' type='SFNode' name='lines'>
 
372
            <LineSet DEF='_6'
 
373
                vertexCount='2, 2, 2, 2'>
 
374
              <Coordinate
 
375
                  point='-20 -10 0, -20 -10 0, -5 5 0, -5 5 0, 10 -5 0, 10 -5 0, 20 10 0, 20 10 0'/>
 
376
            </LineSet>
 
377
          </field>
 
378
          <field accessType='initializeOnly' type='SFNode' name='points'>
 
379
            <Polypoint2D DEF='_7'
 
380
                point='-20 -10, -5 5, 10 -5, 20 10'/>
 
381
          </field>
 
382
          <field accessType='initializeOnly' type='SFNode' name='xInterpolator'>
 
383
            <ScalarInterpolator USE='XInterpolator'/>
 
384
          </field>
 
385
          <field accessType='initializeOnly' type='SFNode' name='yInterpolator'>
 
386
            <SplineScalarInterpolator USE='YInterpolator'/>
 
387
          </field>
 
388
<![CDATA[ecmascript:
 
389
 
 
390
var velocityScale = 0.5;
 
391
 
 
392
function initialize ()
 
393
{
 
394
        eventsProcessed ();
 
395
}
 
396
 
 
397
function eventsProcessed ()
 
398
{
 
399
        for (var i = 0, length = xInterpolator .keyValue .length; i < length; ++ i)
 
400
        {
 
401
                var
 
402
                        x = xInterpolator .keyValue [i],
 
403
                        y = yInterpolator .keyValue [i],
 
404
                        v = y + yInterpolator .keyVelocity [i] * velocityScale;
 
405
 
 
406
                lines .vertexCount [i] = 2;
 
407
 
 
408
                lines .coord .point [i * 2 + 0] .x = x;
 
409
                lines .coord .point [i * 2 + 0] .y = y;
 
410
                lines .coord .point [i * 2 + 1] .x = x;
 
411
                lines .coord .point [i * 2 + 1] .y = v;
 
412
 
 
413
                points .point [i] .x = x;
 
414
                points .point [i] .y = v;
 
415
        }
 
416
 
 
417
        lines .vertexCount .length  = i;
 
418
        lines .coord .point .length = i * 2;
 
419
        points .point .length       = i;
 
420
}
 
421
]]>
 
422
        </Script>
 
423
        <Shape>
 
424
          <Appearance>
 
425
            <LineProperties
 
426
                linewidthScaleFactor='2'/>
 
427
            <Material DEF='_8'
 
428
                emissiveColor='0.117647 0.564706 1'/>
 
429
          </Appearance>
 
430
          <LineSet USE='_6'/>
 
431
        </Shape>
 
432
        <Shape>
 
433
          <Appearance>
 
434
            <LineProperties USE='_5'/>
 
435
            <Material USE='_8'/>
 
436
          </Appearance>
 
437
          <Polypoint2D USE='_3'/>
 
438
        </Shape>
 
439
        <Shape>
 
440
          <Appearance>
 
441
            <LineProperties USE='_4'/>
 
442
            <Material
 
443
                emissiveColor='0.429132 0.718372 1'/>
 
444
          </Appearance>
 
445
          <Polypoint2D USE='_7'/>
 
446
        </Shape>
 
447
        <Shape>
 
448
          <Appearance>
 
449
            <LineProperties USE='_5'/>
 
450
            <Material USE='_8'/>
 
451
          </Appearance>
 
452
          <Polypoint2D USE='_7'/>
 
453
        </Shape>
 
454
      </Transform>
 
455
    </Switch>
 
456
    <ROUTE fromNode='XInterpolator' fromField='value_changed' toNode='YInterpolator' toField='set_fraction'/>
 
457
    <ROUTE fromNode='Timer' fromField='fraction_changed' toNode='XInterpolator' toField='set_fraction'/>
 
458
    <ROUTE fromNode='YInterpolator' fromField='value_changed' toNode='TrailScript' toField='set_y'/>
 
459
    <ROUTE fromNode='TrailScript' fromField='point_changed' toNode='Trail' toField='set_point'/>
 
460
    <ROUTE fromNode='Timer' fromField='cycleTime' toNode='Trail' toField='set_resetTime'/>
 
461
    <ROUTE fromNode='XInterpolator' fromField='keyValue_changed' toNode='YInterpolator' toField='set_key'/>
 
462
    <ROUTE fromNode='XInterpolator' fromField='keyValue_changed' toNode='PointsScript' toField='set_x'/>
 
463
    <ROUTE fromNode='YInterpolator' fromField='keyValue_changed' toNode='PointsScript' toField='set_y'/>
 
464
    <ROUTE fromNode='Touch' fromField='isActive' toNode='PickingScript' toField='set_active'/>
 
465
    <ROUTE fromNode='Touch' fromField='hitPoint_changed' toNode='PickingScript' toField='set_hitPoint'/>
 
466
    <ROUTE fromNode='Touch' fromField='touchTime' toNode='PickingScript' toField='set_touchTime'/>
 
467
    <ROUTE fromNode='PickingScript' fromField='doubleClickTime' toNode='Trail' toField='set_resetTime'/>
 
468
    <ROUTE fromNode='PointTypeSwitch' fromField='whichChoice_changed' toNode='PickingScript' toField='set_pointType'/>
 
469
    <ROUTE fromNode='XInterpolator' fromField='keyValue_changed' toNode='KeyVelocityScript' toField='set_x'/>
 
470
    <ROUTE fromNode='YInterpolator' fromField='keyValue_changed' toNode='KeyVelocityScript' toField='set_y'/>
 
471
    <ROUTE fromNode='YInterpolator' fromField='keyVelocity_changed' toNode='KeyVelocityScript' toField='set_velocity'/>
 
472
  </Scene>
 
473
</X3D>