<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "http://www.web3d.org/specifications/x3d-3.0.dtd">
<X3D profile='Immersive' version='3.0 xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation =' http://www.web3d.org/specifications/x3d-3.0.xsd '>
<head>
<meta name='titlecontent='AnimatedViewpointRecorderPrototype.x3d'/>
<meta name='descriptioncontent='Record camera position and orientation as user navigates, then filter values and produce output, both into the console output window and as a replayable node group. Future work: further filtering.'/>
<meta name='creatorcontent='Don Brutzman, Ken Curtin, Duane Davis, Christos Kalogrias'/>
<meta name='createdcontent='24 October 2003'/>
<meta name='modifiedcontent='14 January 2014'/>
<meta name='referencecontent='AnimatedViewpointRecorderExample.x3d'/>
<meta name='referencecontent='AnimatedViewpointRecorderSample.x3d'/>
<meta name='referencecontent=' http://www.realism.com/Web3D/Examples#WhereAmI '/>
<meta name='referencecontent=' http://www.realism.com/vrml/Example/WhereAmI/WhereAmI_Proto.wrl '/>
<meta name='subjectcontent='recording animated viewpoint tour'/>
<meta name='identifiercontent=' https://savage.nps.edu/Savage/Tools/Authoring/AnimatedViewpointRecorderPrototype.x3d '/>
<meta name='generatorcontent='X3D-Edit 3.2, https://savage.nps.edu/X3D-Edit'/>
<meta name='licensecontent=' ../../license.html'/>
</head>
<!--

Index for ProtoDeclare definition : AnimatedViewpointRecorder

Index for DEF nodes : NewViewpointGroup, RecordingScript, RouteHolder, WhereSensor

Index for Viewpoint image : Viewpoint_1
-->
<Scene>
<!-- ==================== -->
<ProtoDeclare name='AnimatedViewpointRecorderappinfo='AnimatedViewpointRecorder captures view position and orientation tour to create a guided tour animation. The recording output goes to the browser console where the .x3d (or .x3dv) output can be cut/pasted for further use.'>
<ProtoInterface>
<field name='starttype='SFBoolaccessType='inputOnly'
 appinfo='Set start=true to commence recording viewpoint position/orientation.'/>

<field name='stoptype='SFBoolaccessType='inputOnly'
 appinfo='Set stop=true to finish recording viewpoint position/orientation. Resulting VRML is added to scene resulting X3D and VRML is output to console.'/>

<field name='samplingIntervaltype='SFTimevalue='0.1accessType='initializeOnly'
 appinfo='default 0.1 seconds'/>

<field name='outputX3Dtype='SFBoolvalue='trueaccessType='initializeOnly'
 appinfo='whether to output .x3d syntax on browser console'/>

<field name='outputClassicVRMLtype='SFBoolvalue='falseaccessType='initializeOnly'
 appinfo='whether to output .x3d syntax on browser console'/>

<field name='filterDeadTimetype='SFBoolvalue='falseaccessType='initializeOnly'
 appinfo='TODO not yet implemented'/>
</ProtoInterface>
<ProtoBody>
<Group>
<Group DEF='NewViewpointGroup'/>
<!-- it's a big old world out there! -->
<!-- ROUTE information for WhereSensor node:  [from RecordingScript.recordingInProgress to enabled ] [from position_changed to RecordingScript.set_position ] [from orientation_changed to RecordingScript.set_orientation ] -->
<ProximitySensor DEF='WhereSensorsize='1000000000 1000000000 1000000000'/>
<!-- ROUTE information for RecordingScript node:  [from WhereSensor.position_changed to set_position ] [from WhereSensor.orientation_changed to set_orientation ] [from recordingInProgress to WhereSensor.enabled ] -->
<Script DEF='RecordingScriptdirectOutput='true'>
<field name='starttype='SFBoolaccessType='inputOnly'/>
<field name='stoptype='SFBoolaccessType='inputOnly'/>
<field name='samplingIntervaltype='SFTimeaccessType='initializeOnly'
 appinfo='seconds'/>

<field name='outputX3Dtype='SFBoolaccessType='initializeOnly'
 appinfo='whether to output .x3d syntax on browser console'/>

<field name='outputClassicVRMLtype='SFBoolaccessType='initializeOnly'
 appinfo='whether to output .x3d syntax on browser console'/>

<field name='recordingInProgresstype='SFBoolaccessType='outputOnly'
 appinfo='persistent state variable'/>

<field name='set_positiontype='SFVec3faccessType='inputOnly'/>
<field name='set_orientationtype='SFRotationaccessType='inputOnly'/>
<field name='positionArraytype='MFVec3faccessType='initializeOnly'/>
<field name='positionTimeArraytype='MFTimeaccessType='initializeOnly'/>
<field name='orientationArraytype='MFRotationaccessType='initializeOnly'/>
<field name='orientationTimeArraytype='MFTimeaccessType='initializeOnly'/>
<field name='filterDeadTimetype='SFBoolaccessType='initializeOnly'
 appinfo='not yet implemented'/>

<field name='newViewpointGrouptype='SFNodeaccessType='initializeOnly'>
<Group USE='NewViewpointGroup'/>
</field>
<field name='numberOfToursCreatedtype='SFInt32value='0accessType='initializeOnly'
 appinfo='persistent holding variable'/>

<field name='precedingPositiontype='SFVec3fvalue='0 0 0accessType='initializeOnly'
 appinfo='persistent holding variable'/>

<field name='precedingOrientationtype='SFRotationvalue='0 1 0 0accessType='initializeOnly'
 appinfo='persistent holding variable'/>

<field name='precedingPositionSampleTimetype='SFTimevalue='0accessType='initializeOnly'
 appinfo='persistent holding variable'/>

<field name='precedingOrientationSampleTimetype='SFTimevalue='0accessType='initializeOnly'
 appinfo='persistent holding variable'/>

<field name='rtype='SFFloatvalue='1accessType='initializeOnly'
 appinfo='normalization factor'/>

<field name='positionEventsReceivedtype='SFBoolvalue='falseaccessType='initializeOnly'
 appinfo='track output of ProximitySensor'/>

<field name='orientationEventsReceivedtype='SFBoolvalue='falseaccessType='initializeOnly'
 appinfo='track output of ProximitySensor'/>

<IS>
<connect nodeField='startprotoField='start'/>
<connect nodeField='stopprotoField='stop'/>
<connect nodeField='samplingIntervalprotoField='samplingInterval'/>
<connect nodeField='outputX3DprotoField='outputX3D'/>
<connect nodeField='outputClassicVRMLprotoField='outputClassicVRML'/>
<connect nodeField='filterDeadTimeprotoField='filterDeadTime'/>
</IS>
<![CDATA[
            
ecmascript:

function initialize()
{
   positionArray        = new MFVec3f ();
   orientationArray     = new MFRotation ();
   positionTimeArray    = new MFTime ();
   orientationTimeArray = new MFTime ();

      positionEventsReceived = false;
   orientationEventsReceived = false;
}

function roundoff (value, digits)
{
	resolution = 1;
	for (i = 1; i <= digits; i++ )
	{
		resolution *= 10;
	}
	return Math.round (value*resolution) / resolution; // round to resolution
}
function filterPositions()
{
    // TODO
}

function filterOrientations()
{
    // TODO
}

function set_position (eventValue, timestamp)
{
// Browser.print ('position=' + eventValue);
   // we are counting on an initialization eventValue being sent by ProximitySensor
   positionEventsReceived = true;
   if ( positionArray.length == 0 )
   {
   	positionArray[0]     = eventValue; // initialize
   	positionTimeArray[0] = timestamp;  // initialize
   }
   precedingPositionSampleTime = positionTimeArray[ positionArray.length - 1 ];

   // seconds duration since last valid sample
   if ( (timestamp - precedingPositionSampleTime) > samplingInterval )
   {
	// append values to each array
	positionArray[positionArray.length] = eventValue;
	positionTimeArray[positionTimeArray.length] = timestamp;
   }
   precedingPosition = eventValue;
}

function set_orientation (eventValue, timestamp)
{
   // we are counting on an initialization eventValue being sent by ProximitySensor
   orientationEventsReceived = true;
   if ( orientationArray.length == 0 )
   {
     r = Math.sqrt (eventValue.x*eventValue.x + eventValue.y*eventValue.y + eventValue.z*eventValue.z);
//   Browser.print ('orientation=' + eventValue.toString() + ', r=' + r + '\n'); // trace
     if (r != 0)
     {
        eventValue.x = eventValue.x / r;
        eventValue.y = eventValue.y / r;
        eventValue.z = eventValue.z / r;
     }
   	 orientationArray[0]     = eventValue; // initialize
   	 orientationTimeArray[0] = timestamp;  // initialize
   }
   precedingOrientationSampleTime = orientationTimeArray[ orientationTimeArray.length - 1 ];

   // append sample values to each array
   if ( (timestamp - precedingOrientationSampleTime) > samplingInterval )
   {
     orientationTimeArray[orientationTimeArray.length] = timestamp;
	 // normalize SFRotation axis if needed
     r = Math.sqrt (eventValue.x*eventValue.x + eventValue.y*eventValue.y + eventValue.z*eventValue.z);
//    Browser.print ('orientation=' + eventValue.toString() + ', r=' + r + '\n'); // trace
     if (r != 0)
     {
        eventValue.x = eventValue.x / r;
        eventValue.y = eventValue.y / r;
        eventValue.z = eventValue.z / r;
        // auto append to array, no need to allocate
        orientationArray[orientationArray.length] = eventValue;
     }
     else // illegal zero-magnitude axis returned by browser, so just use previous rotation
     {
        // auto append to array, no need to allocate
        orientationArray[orientationArray.length] = precedingOrientation;
     }
   }
   precedingOrientation = eventValue;
}

function start (eventValue, timestamp)
{
	if (eventValue == false) return; // only accept start if eventValue == true
	if (recordingInProgress == true) return; // ignore repeated starts while already running
	recordingInProgress  = true;
    // arrays need to be reinitialized from previous run
	initialize();

    Browser.print ('    <!-- start recording ' + numberOfToursCreated + ' -->\n\n');
}

function stop (eventValue, timestamp)
{
	if (eventValue == false) return; // only accept stop  if eventValue == true
	if (recordingInProgress == false)
	{
	   Browser.print ('    <!-- stopped recording without first starting. -->\n\n');
       return;
	}

    // ensure legal array lengths in case some events were never sent due to not moving
    if (positionEventsReceived == false)
    {
       Browser.print ('<!-- warning:  no position values received! no action taken. -->\n\n');
       return;
    }
    if (orientationEventsReceived == false)
    {
       Browser.print ('<!-- warning:  no orientation values received! no action taken. -->\n');
       return;
    }
	recordingInProgress = false;

	// preceding last values were at last sampleInterval (either set_position or set_orientation)
	// add one more to each array since they are not sent values by sensor when not changing
       positionArray[   positionArray.length] = precedingPosition;
    orientationArray[orientationArray.length] = precedingOrientation;
       positionTimeArray[   positionTimeArray.length] = timestamp;
    orientationTimeArray[orientationTimeArray.length] = timestamp;

    if (positionArray.length != positionTimeArray.length)
    {
       Browser.print ('<!-- internal error:  positionArray.length (' + positionArray.length + ') != positionTimeArray.length (' + positionTimeArray.length + ') -->\n');
    }
    if (orientationArray.length != orientationTimeArray.length)
    {
       Browser.print ('<!-- internal error:  orientationArray.length (' + orientationArray.length + ') != orientationTimeArray.length (' + orientationTimeArray.length + ') -->\n');
    }

   filterPositions();
   filterOrientations();

   // iff events are sent simultaneously, could use either array with start/stop times synchronized
   // however that might be a bad assumption...  so reset start times to match
   if (positionTimeArray[0] > orientationTimeArray[0])    positionTimeArray[0] = orientationTimeArray[0];
   if (positionTimeArray[0] < orientationTimeArray[0]) orientationTimeArray[0] = positionTimeArray[0];

   startTime = positionTimeArray[0];
   stopTime  = positionTimeArray[positionTimeArray.length-1];
   interval  = stopTime - startTime;

   x3dString =
   '    <!-- ********** start recorded Animated Tour ' + numberOfToursCreated + ' using .x3d syntax ********** -->\n' +
   '    <Group>\n' +
   '      <Viewpoint DEF=\"AnimatedViewpointRecorderViewpoint' + numberOfToursCreated + '\" description=\"Animated Tour ' + numberOfToursCreated + '\"\n' +
   '         position=\"'    + positionArray[0].x + ' '    + positionArray[0].y + ' '    + positionArray[0].z + '\" \n' +
   '         orientation=\"' + orientationArray[0].x + ' ' + orientationArray[0].y + ' ' + orientationArray[0].z + ' ' + orientationArray[0].angle + '\"/>\n' +
   '      <!-- samplingInterval=' + samplingInterval + ' seconds, default TimeSensor loop=true -->\n' +
   '      <TimeSensor DEF=\"AnimatedViewpointRecorderTimer' + numberOfToursCreated + '\" cycleInterval=\"' + interval + '\"\n' +
   '        enabled=\"true\" loop=\"true\"/>\n' +
   '      <PositionInterpolator DEF=\"AnimatedViewpointRecorderPosition' + numberOfToursCreated + '\" key=\"\n' ;
   for (counter = 0; counter < positionTimeArray.length; counter++)
   {
    x3dString = x3dString +  roundoff(((positionTimeArray[counter] - positionTimeArray[0]) / interval),5) + ' \n';
   }
   x3dString = x3dString + '\"      keyValue=\"\n';
   for (counter = 0; counter < positionArray.length; counter++)
   {
      x3dString = x3dString +   positionArray[counter].x + ' ' + positionArray[counter].y + ' ' + positionArray[counter].z + ', \n';
   }
   x3dString = x3dString + '         \"/>\n' +
   '      <OrientationInterpolator DEF=\"AnimatedViewpointRecorderOrientation' + numberOfToursCreated + '\" key=\"\n';
   for (counter = 0; counter < orientationTimeArray.length; counter++)
   {
    x3dString = x3dString +   roundoff(((orientationTimeArray[counter] - orientationTimeArray[0]) / interval),5) + ' \n';
   }
   x3dString = x3dString +   '\"      keyValue=\"\n';
   for (counter = 0; counter < orientationArray.length; counter++)
   {
      var r = Math.sqrt(orientationArray[counter].x*orientationArray[counter].x + orientationArray[counter].y*orientationArray[counter].y + orientationArray[counter].z*orientationArray[counter].z); // normalize
      if (r == 0) r = 1; // avoid divide by zero
      x3dString = x3dString + (orientationArray[counter].x / r) + ' ' + (orientationArray[counter].y / r) + ' ' + (orientationArray[counter].z / r) + ' ' + orientationArray[counter].angle + ', \n';
   }
   x3dString = x3dString + '         \"/>\n' +
   '      <Group>\n' +
   '        <ROUTE fromField=\"bindTime\" fromNode=\"AnimatedViewpointRecorderViewpoint' + numberOfToursCreated + '\"\n' +
   '          toField=\"startTime\" toNode=\"AnimatedViewpointRecorderTimer' + numberOfToursCreated + '\"/>\n' +
   '        <ROUTE fromField=\"fraction_changed\" fromNode=\"AnimatedViewpointRecorderTimer' + numberOfToursCreated + '\"\n' +
   '          toField=\"set_fraction\" toNode=\"AnimatedViewpointRecorderPosition' + numberOfToursCreated + '\"/>\n' +
   '        <ROUTE fromField=\"fraction_changed\" fromNode=\"AnimatedViewpointRecorderTimer' + numberOfToursCreated + '\"\n' +
   '          toField=\"set_fraction\" toNode=\"AnimatedViewpointRecorderOrientation' + numberOfToursCreated + '\"/>\n' +
   '        <ROUTE fromField=\"value_changed\" fromNode=\"AnimatedViewpointRecorderPosition' + numberOfToursCreated + '\"\n' +
   '          toField=\"position\" toNode=\"AnimatedViewpointRecorderViewpoint' + numberOfToursCreated + '\"/>\n' +
   '        <ROUTE fromField=\"value_changed\"\n' +
   '          fromNode=\"AnimatedViewpointRecorderOrientation' + numberOfToursCreated + '\" toField=\"orientation\" toNode=\"AnimatedViewpointRecorderViewpoint' + numberOfToursCreated + '\"/>\n' +
   '      </Group>\n' +
   '    </Group>\n';

   if (outputX3D) Browser.print (x3dString);

   vrmlString =
      '# ********** start recorded Animated Tour ' + numberOfToursCreated + ' using .x3dv syntax ********** \n' +
      'Group {\n' +
      '  children [\n' +
      '      DEF AnimatedViewpointRecorderViewpoint' + numberOfToursCreated + ' Viewpoint {\n' +
      '        description \"Animated Tour ' + numberOfToursCreated + '\"\n' +
      '        orientation ' + orientationArray[0].x + ' ' + orientationArray[0].y + ' ' + orientationArray[0].z + ' ' + orientationArray[0].angle + '\n' +
      '        position ' + positionArray[0].x + ' '    + positionArray[0].y + ' '    + positionArray[0].z + '\n' +
      '      }\n' +
      '      DEF AnimatedViewpointRecorderTimer' + numberOfToursCreated + ' TimeSensor {\n' +
      '        cycleInterval ' + interval +  '\n' +
      '        loop TRUE\n' +
      '      }\n' +
      '      DEF AnimatedViewpointRecorderPosition' + numberOfToursCreated + ' PositionInterpolator {\n' +
      '        key [\n';
   for (counter = 0; counter < positionTimeArray.length; counter++)
   {
      vrmlString = vrmlString + roundoff(((positionTimeArray[counter] - positionTimeArray[0]) / interval),5) + ' \n';
   }
   vrmlString = vrmlString + '         ]\n' +
      '        keyValue [\n';
   for (counter = 0; counter < positionArray.length; counter++)
   {
      vrmlString = vrmlString +   positionArray[counter].x + ' ' + positionArray[counter].y + ' ' + positionArray[counter].z + ', \n';
   }
   vrmlString = vrmlString + '         ]\n' +
      '      }\n' +
      '      DEF AnimatedViewpointRecorderOrientation' + numberOfToursCreated + ' OrientationInterpolator {\n' +
      '        key [\n';
   for (counter = 0; counter < orientationTimeArray.length; counter++)
   {
    vrmlString = vrmlString +   roundoff(((orientationTimeArray[counter] - orientationTimeArray[0]) / interval),5) + ' \n';
   }
   vrmlString = vrmlString + '         ]\n' +
      '        keyValue [\n';
   for (counter = 0; counter < orientationArray.length; counter++)
   {
      vrmlString = vrmlString + orientationArray[counter].x + ' ' + orientationArray[counter].y + ' ' + orientationArray[counter].z + ' ' + orientationArray[counter].angle + ', \n';
   }
   vrmlString = vrmlString + '         ]\n' +
      '      }\n' +
      '      Group {\n' +
      '         ROUTE AnimatedViewpointRecorderViewpoint' + numberOfToursCreated + '.bindTime TO AnimatedViewpointRecorderTimer' + numberOfToursCreated + '.startTime\n' +
      '         ROUTE AnimatedViewpointRecorderTimer' + numberOfToursCreated + '.fraction_changed TO AnimatedViewpointRecorderPosition' + numberOfToursCreated + '.set_fraction\n' +
      '         ROUTE AnimatedViewpointRecorderTimer' + numberOfToursCreated + '.fraction_changed TO AnimatedViewpointRecorderOrientation' + numberOfToursCreated + '.set_fraction\n' +
      '         ROUTE AnimatedViewpointRecorderPosition' + numberOfToursCreated + '.value_changed TO AnimatedViewpointRecorderViewpoint' + numberOfToursCreated + '.position\n' +
      '         ROUTE AnimatedViewpointRecorderOrientation' + numberOfToursCreated + '.value_changed TO AnimatedViewpointRecorderViewpoint' + numberOfToursCreated + '.orientation\n' +
      '      }\n' +
      '   ]\n' +
      '}\n';

   Browser.print ('\n');
   if (outputClassicVRML) Browser.print (vrmlString);

   numberOfToursCreated++;
   // TODO
   // newNode = new SFNode(vrmlString);
   // newViewpointGroup.children[numberOfToursCreated] = newNode;
}

          
]]>
</Script>
<Group DEF='RouteHolder'>
<ROUTE fromNode='WhereSensorfromField='position_changedtoNode='RecordingScripttoField='set_position'/>
<ROUTE fromNode='WhereSensorfromField='orientation_changedtoNode='RecordingScripttoField='set_orientation'/>
<ROUTE fromNode='RecordingScriptfromField='recordingInProgresstoNode='WhereSensortoField='enabled'/>
</Group>
</Group>
</ProtoBody>
</ProtoDeclare>
<!-- ==================== -->
<Background groundColor='0.2 0.4 0.2skyColor='0.2 0.2 0.4'/>
<Viewpoint description='Animated Viewpoint Recorderposition='0 0 14'/>
<Anchor description='AnimatedViewpointRecorder Example'
  url=' "AnimatedViewpointRecorderExample.x3d" "https://savage.nps.edu/Savage/Tools/Authoring/AnimatedViewpointRecorderExample.x3d" "AnimatedViewpointRecorderExample.wrl" "https://savage.nps.edu/Savage/Tools/Authoring/AnimatedViewpointRecorderExample.wrl" '>
<Shape>
<Text string='"AnimatedViewpointRecorderPrototype" "is a prototype definition file" "" "Click this text to see" "AnimatedViewpointRecorderExample"'>
<FontStyle justify='"MIDDLE" "MIDDLE"size='1.2'/>
</Text>
<Appearance>
<Material diffuseColor='0.6 0.8 0.4'/>
</Appearance>
</Shape>
</Anchor>
</Scene>
</X3D>
<!--

Index for ProtoDeclare definition : AnimatedViewpointRecorder

Index for DEF nodes : NewViewpointGroup, RecordingScript, RouteHolder, WhereSensor

Index for Viewpoint image : Viewpoint_1
-->

<!-- Color key: <X3dNode DEF='idName' field='value'/> matches <XmlElement DEF='idName' attribute='value'/>
(Light blue background: behavior node) (Grey background: inserted documentation) (Magenta background: X3D Extensibility)
    <Prototype name='ProtoName'> <field name='fieldName'/> </Prototype> -->

<!-- Additional help information about X3D scenes: X3D Resources, X3D Scene Authoring Hints and X3D Tooltips -->