package Basic.development;
/*
Copyright (c) 1995-2023 held by the author(s). All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of the Web3D Consortium (https://www.web3d.org)
nor the names of its contributors may be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
import org.web3d.x3d.jsail.Core.*;
import org.web3d.x3d.jsail.fields.*;
import org.web3d.x3d.jsail.Geometry3D.*;
import org.web3d.x3d.jsail.Grouping.*;
import org.web3d.x3d.jsail.Interpolation.*;
import org.web3d.x3d.jsail.Networking.*;
import org.web3d.x3d.jsail.Scripting.*;
import org.web3d.x3d.jsail.Shape.*;
import org.web3d.x3d.jsail.Text.*;
import org.web3d.x3d.jsail.Texturing.*;
// Javadoc metadata annotations follow, see below for X3DJSAIL Java source code.
/**
*
CoordinateInterpolator2D prototype declaration, to interpolate across an array of Vector2FloatArray/MFVec2f values to produce an interpolated Vector2FloatArray - click text to see example.
Related links: CoordinateInterpolator2dPrototype.java source, CoordinateInterpolator2dPrototype catalog page, X3D Resources, X3D Scene Authoring Hints, and X3D Tooltips.
This program uses the
X3D Java Scene Access Interface Library (X3DJSAIL).
It has been produced using the
X3dToJava.xslt
stylesheet
(version control)
is used to create Java source code from an original .x3d
model.
* @author Don Brutzman, Jeff Weekley, Jane Wu
*/
public class CoordinateInterpolator2dPrototype
{
/** Default constructor to create this object. */
public CoordinateInterpolator2dPrototype ()
{
initialize();
}
/** Create and initialize the X3D model for this object. */
public final void initialize()
{
try { // catch-all
x3dModel = new X3D().setProfile(X3D.PROFILE_IMMERSIVE).setVersion(X3D.VERSION_3_0)
.setHead(new head()
.addMeta(new meta().setName(meta.NAME_TITLE ).setContent("CoordinateInterpolator2dPrototype.x3d"))
.addMeta(new meta().setName(meta.NAME_DESCRIPTION).setContent("CoordinateInterpolator2D prototype declaration, to interpolate across an array of Vector2FloatArray/MFVec2f values to produce an interpolated Vector2FloatArray - click text to see example."))
.addMeta(new meta().setName(meta.NAME_CREATOR ).setContent("Don Brutzman, Jeff Weekley, Jane Wu"))
.addMeta(new meta().setName(meta.NAME_CREATED ).setContent("28 June 2001"))
.addMeta(new meta().setName(meta.NAME_MODIFIED ).setContent("20 January 2020"))
.addMeta(new meta().setName(meta.NAME_REFERENCE ).setContent("https://www.web3d.org/technicalinfo/specifications/vrml97/part1/concepts.html#4.6.8"))
.addMeta(new meta().setName(meta.NAME_REFERENCE ).setContent("https://www.web3d.org/technicalinfo/specifications/vrml97/part1/nodesRef.html#CoordinateInterpolator"))
.addMeta(new meta().setName(meta.NAME_SUBJECT ).setContent("CoordinateInterpolator2D"))
.addMeta(new meta().setName(meta.NAME_IDENTIFIER ).setContent("https://www.web3d.org/x3d/content/examples/Basic/development/CoordinateInterpolator2dPrototype.x3d"))
.addMeta(new meta().setName(meta.NAME_GENERATOR ).setContent("X3D-Edit 3.3, https://savage.nps.edu/X3D-Edit"))
.addMeta(new meta().setName(meta.NAME_LICENSE ).setContent("../license.html")))
.setScene(new Scene()
.addChild(new WorldInfo().setTitle("CoordinateInterpolator2dPrototype.x3d"))
.addChild(new ProtoDeclare("CoordinateInterpolator2D").setName("CoordinateInterpolator2D").setAppinfo("Provide interpolation capability for Vector2FloatArray/MFVec2f values").setDocumentation("https://www.web3d.org/technicalinfo/specifications/vrml97/part1/concepts.html#4.6.8")
.setProtoInterface(new ProtoInterface()
.addField(new field().setName("set_fraction").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INPUTONLY).setAppinfo("Regular interpolator-style input, the set_fraction eventIn receives an SFFloat event and causes the interpolator function to evaluate resulting in a value_changed eventOut with the same timestamp as the set_fraction event."))
.addField(new field().setName("set_key").setType(field.TYPE_MFFLOAT).setAccessType(field.ACCESSTYPE_INPUTONLY))
.addField(new field().setName("key").setType(field.TYPE_MFFLOAT).setAccessType(field.ACCESSTYPE_INPUTOUTPUT).setAppinfo("keyValue holds the array of Vector2FloatArrays that match each animation key."))
.addField(new field().setName("key_changed").setType(field.TYPE_MFFLOAT).setAccessType(field.ACCESSTYPE_OUTPUTONLY).setAppinfo("Array sequentially increasing typically [0..1]. Must have the same number of keys as keyValues."))
.addField(new field().setName("set_keyValue").setType(field.TYPE_MFVEC2F).setAccessType(field.ACCESSTYPE_INPUTONLY).setAppinfo("Array of integer values. Must have the same number of keys as keyValues."))
.addField(new field().setName("keyValue").setType(field.TYPE_MFVEC2F).setAccessType(field.ACCESSTYPE_INPUTOUTPUT).setAppinfo("keyValue holds the array of Vector2FloatArrays that match each animation key."))
.addField(new field().setName("keyValue_changed").setType(field.TYPE_MFVEC2F).setAccessType(field.ACCESSTYPE_OUTPUTONLY).setAppinfo("Array of integer values. Must have the same number of keys as keyValues."))
.addField(new field().setName("value_changed").setType(field.TYPE_MFVEC2F).setAccessType(field.ACCESSTYPE_OUTPUTONLY).setAppinfo("Regular interpolator-style output, the interpolator function averages between respective keyValue Vector2FloatArrays resulting in a Vector2FloatArray value_changed eventOut with the same timestamp as the set_fraction event.")))
.setProtoBody(new ProtoBody()
.addChild(new Group()
.addChild(new Switch().setWhichChoice(-1)
.addChild(new ScalarInterpolator("KeyHolder")
.setIS(new IS()
.addConnect(new connect().setNodeField("key").setProtoField("key"))))
.addChild(new Shape()
.setGeometry(new IndexedFaceSet()
.setTexCoord(new TextureCoordinate("KeyValueHolder")
.setIS(new IS()
.addConnect(new connect().setNodeField("point").setProtoField("keyValue")))))
.setAppearance(new Appearance("DefaultAppearance")
.setMaterial(new Material()))))
.addChild(new Script("InterpolationScript").setDirectOutput(true).setSourceCode("""
ecmascript:
// internal global persistent variables
var previousFraction;
var previousFractionIndex;
var blockSize;
var outputArray;
function tracePrint (outputString)
{
var traceEnabled = false;
if (traceEnabled) Browser.println ('[CoordinateInterpolator2D]' + outputString);
}
function alwaysPrint (outputString)
{
Browser.println ('[CoordinateInterpolator2D]' + outputString);
}
function initialize ()
{
key = keyHolderNode.key;
keyValue = keyValueHolderNode.point;
previousFractionIndex = -1;
previousFraction = 0;
// check key array ranges [0..1] and is monotonically increasing
// check that size of keyValue array is integer multiple of size of key array
tracePrint ('key =' + key);
tracePrint ('key.length= ' + key.length);
tracePrint ('keyValue= ' + keyValue);
tracePrint ('keyValue.length=' + keyValue.length);
blockSize = keyValue.length/key.length;
tracePrint ('blockSize=' + blockSize);
if (blockSize != Math.round(blockSize))
{
alwaysPrint ('*** warning: blockSize not an integer multiple. check sizes of key and keyValue');
}
if (key[0] != 0)
{
alwaysPrint ('*** warning: key[0] != 0');
}
if (key[key.length-1] != 1)
{
alwaysPrint ('*** warning: key[' + (key.length - 1) + '] != 1, reset from' + key[key.length-1] + ' to 1');
key[key.length-1] = 1;
}
for (index = 0; index < blockSize; index++)
{
if ((key[index] < 0) || (key[index] > 1))
{
alwaysPrint ('*** warning: key[' + index + '] =' + key[index] + ', out of range [0..1]');
}
}
// instantiate default array, later computations just update it
outputArray = new MFVec2f ();
for (index = 0; index < blockSize; index++)
{
// dynamically grow outputArray to match initial block
outputArray[index] = keyValue[index];
}
tracePrint ('initial outputArray=' + outputArray);
}
function set_fraction (inputFloat, timestamp) {
fraction = inputFloat;
tracePrint ('previousFractionIndex=' + previousFractionIndex
+ ', fraction=' + fraction + ', previousFraction=' + previousFraction);
if (fraction < 0)
{
tracePrint ('*** illegal fraction' + fraction + ' set to 0');
fraction = 0;
previousFractionIndex = 0; // first
}
else if (fraction > 1)
{
alwaysPrint ('*** illegal fraction' + fraction + ' set to 1');
fraction = 1;
previousFractionIndex = blockSize - 1; // last
}
else if (previousFractionIndex == -1)
{
previousFractionIndex = 0; // first
tracePrint ('previousFractionIndex initialized for first event');
}
else if ((fraction >= previousFraction) && (fraction >= key[previousFractionIndex+1]))
{
previousFractionIndex++;
}
else if (fraction < previousFraction) // regress, or loop repeat without reaching one
{
previousFractionIndex = 0;
while ((fraction >= key[previousFractionIndex+1]) && (previousFractionIndex < blockSize))
{
previousFractionIndex++;
}
tracePrint ('reset/reincrement previousFractionIndex to' + previousFractionIndex);
}
if (fraction == 1) // use final block
{
tracePrint ('(fraction == 1)');
for (index = 0; index < blockSize; index++)
{
// update outputArray with final four keyValues
outputArray[4 - index] = keyValue[keyValue.length - index];
}
previousFractionIndex = -1; // setup for restart
tracePrint ('finished final fraction==1 block');
}
// when fraction matches index, calculate value_changed from corresponding keyValue array
else if (fraction == key[previousFractionIndex])
{
tracePrint ('(fraction == key[previousFractionIndex])');
for (index = 0; index < blockSize; index++)
{
// update outputArray - need to interpolate next
outputArray[index] = keyValue[blockSize * (previousFractionIndex) + index];
}
}
else // calculate value_changed by interpolating between adjacent keyValue arrays
{
partialFraction = fraction - key[previousFractionIndex];
deltaFraction = key[previousFractionIndex+1] - key[previousFractionIndex];
percentFraction = partialFraction / deltaFraction;
// tracePrint ('deltaFraction =' + deltaFraction);
// tracePrint ('partialFraction =' + partialFraction);
tracePrint ('percentFraction =' + percentFraction);
for (index = 0; index < blockSize; index++)
{
// no arithmetic operators provided for SFVec2f, treat element by element
nextKeyValue = keyValue[blockSize * (previousFractionIndex + 1) + index];
priorKeyValue = keyValue[blockSize * (previousFractionIndex) + index];
deltaKeyValue = new SFVec2f (
nextKeyValue[0] - priorKeyValue[0],
nextKeyValue[1] - priorKeyValue[1]);
// tracePrint ('deltaKeyValue =' + deltaKeyValue);
// update outputArray
outputArray[index][0] = keyValue[blockSize * (previousFractionIndex) + index][0]
+ percentFraction * deltaKeyValue[0];
outputArray[index][1] = keyValue[blockSize * (previousFractionIndex) + index][1]
+ percentFraction * deltaKeyValue[1];
}
}
value_changed = outputArray;
previousFraction = fraction;
tracePrint ('value_changed=' + value_changed);
}
function set_key (inputArray, timestamp) {
key = inputArray; // update key Vector2FloatArray
keyHolderNode.key = key; // update holder
initialize (timestamp); // reverify key, keyValue sizes
key_changed = key; // eventOut
}
function set_keyValue (inputArray, timestamp) {
keyValue = inputArray; // update keyValue Vector2FloatArray
keyValueHolderNode.point = keyValue; // update holder
initialize (timestamp); // reverify key, keyValue sizes
keyValue_changed = keyValue; // eventOut
}
""")
.addField(new field().setName("set_fraction").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INPUTONLY))
.addField(new field().setName("fraction").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY).setValue(0.0).setAppinfo("local variable"))
.addField(new field().setName("set_key").setType(field.TYPE_MFFLOAT).setAccessType(field.ACCESSTYPE_INPUTONLY))
.addField(new field().setName("keyHolderNode").setType(field.TYPE_SFNODE).setAccessType(field.ACCESSTYPE_INITIALIZEONLY)
.addChild(new ScalarInterpolator().setUSE("KeyHolder")))
.addField(new field().setName("key_changed").setType(field.TYPE_MFFLOAT).setAccessType(field.ACCESSTYPE_OUTPUTONLY))
.addField(new field().setName("set_keyValue").setType(field.TYPE_MFVEC2F).setAccessType(field.ACCESSTYPE_INPUTONLY))
.addField(new field().setName("keyValueHolderNode").setType(field.TYPE_SFNODE).setAccessType(field.ACCESSTYPE_INITIALIZEONLY)
.addChild(new TextureCoordinate().setUSE("KeyValueHolder")))
.addField(new field().setName("keyValue_changed").setType(field.TYPE_MFVEC2F).setAccessType(field.ACCESSTYPE_OUTPUTONLY))
.addField(new field().setName("value_changed").setType(field.TYPE_MFVEC2F).setAccessType(field.ACCESSTYPE_OUTPUTONLY))
.setIS(new IS()
.addConnect(new connect().setNodeField("set_fraction").setProtoField("set_fraction"))
.addConnect(new connect().setNodeField("set_key").setProtoField("set_key"))
.addConnect(new connect().setNodeField("key_changed").setProtoField("key_changed"))
.addConnect(new connect().setNodeField("set_keyValue").setProtoField("set_keyValue"))
.addConnect(new connect().setNodeField("keyValue_changed").setProtoField("keyValue_changed"))
.addConnect(new connect().setNodeField("value_changed").setProtoField("value_changed")))))))
.addComments(" ====================================== ")
.addComments(" Example use ")
.addChild(new Anchor().setDescription("CoordinateInterpolator2dExample").setParameter(new String[] {"target=_blank"}).setUrl(new String[] {"CoordinateInterpolator2dExample.x3d","https://savage.nps.edu/Savage/Tools/Animation/CoordinateInterpolator2dExample.x3d","CoordinateInterpolator2dExample.wrl","https://savage.nps.edu/Savage/Tools/Animation/CoordinateInterpolator2dExample.wrl"})
.addChild(new Shape()
.setGeometry(new Text().setString(new String[] {"CoordinateInterpolator2dPrototype","defines a prototype","","Click on this text to see","CoordinateInterpolator2dExample"," scene"})
.setFontStyle(new FontStyle().setJustify(FontStyle.JUSTIFY_MIDDLE_MIDDLE).setSize(0.7)))
.setAppearance(new Appearance()
.setMaterial(new Material().setDiffuseColor(1.0,1.0,0.2))))));
}
catch (Exception ex)
{
System.err.println ("*** Further hints on X3DJSAIL errors and exceptions at");
System.err.println ("*** https://www.web3d.org/specifications/java/X3DJSAIL.html");
throw (ex);
}
}
// end of initialize() method
/** The initialized model object, created within initialize() method. */
private X3D x3dModel;
/**
* Provide a
* shallow copy
* of the X3D model.
* @see X3D
* @return CoordinateInterpolator2dPrototype model
*/
public X3D getX3dModel()
{
return x3dModel;
}
/**
* Default main() method provided for test purposes, uses CommandLine to set global ConfigurationProperties for this object.
* @param args array of input parameters, provided as arguments
* @see X3D.handleArguments(args)
* @see X3D.validationReport()
* @see CommandLine
* @see CommandLine.USAGE
* @see ConfigurationProperties
*/
public static void main(String args[])
{
System.out.println("Build this X3D model, showing validation diagnostics...");
X3D thisExampleX3dModel = new CoordinateInterpolator2dPrototype().getX3dModel();
// System.out.println("X3D model construction complete.");
// next handle command line arguments
boolean hasArguments = (args != null) && (args.length > 0);
boolean validate = true; // default
boolean argumentsLoadNewModel = false;
String fileName = new String();
if (args != null)
{
for (String arg : args)
{
if (arg.toLowerCase().startsWith("-v") || arg.toLowerCase().contains("validate"))
{
validate = true; // making sure
}
if (arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_X3D) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_CLASSICVRML) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_X3DB) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_VRML97) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_EXI) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_GZIP) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_ZIP) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_HTML) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_XHTML))
{
argumentsLoadNewModel = true;
fileName = arg;
}
}
}
if (argumentsLoadNewModel)
System.out.println("WARNING: \"Basic.development.CoordinateInterpolator2dPrototype\" model invocation is attempting to load file \"" + fileName + "\" instead of simply validating itself... file loading ignored.");
else if (hasArguments) // if no arguments provided, this method produces usage warning
thisExampleX3dModel.handleArguments(args);
if (validate)
{
// System.out.println("--- TODO fix duplicated outputs ---"); // omit when duplicated outputs problem is solved/refactored
String validationResults = thisExampleX3dModel.validationReport();
// System.out.println("-----------------------------------"); // omit when duplicated outputs problem is solved/refactored
System.out.print("Basic.development.CoordinateInterpolator2dPrototype self-validation test results: ");
if (!validationResults.equals("success"))
System.out.println();
System.out.println(validationResults.trim());
}
}
}