package Basic.StudentProjects;
/*
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.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.*;
// Javadoc metadata annotations follow, see below for X3DJSAIL Java source code.
/**
*
A proto which simulates x-y plane projectile motion.
Related links: ProjectileInterpolatorPrototype.java source, ProjectileInterpolatorPrototype 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 Ozan APAYDIN
*/
public class ProjectileInterpolatorPrototype
{
/** Default constructor to create this object. */
public ProjectileInterpolatorPrototype ()
{
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("ProjectileInterpolatorPrototype.x3d"))
.addMeta(new meta().setName(meta.NAME_DESCRIPTION).setContent("A proto which simulates x-y plane projectile motion."))
.addMeta(new meta().setName(meta.NAME_CREATOR ).setContent("Ozan APAYDIN"))
.addMeta(new meta().setName(meta.NAME_CREATED ).setContent("10 December 2001"))
.addMeta(new meta().setName(meta.NAME_MODIFIED ).setContent("20 October 2019"))
.addMeta(new meta().setName(meta.NAME_IDENTIFIER ).setContent("https://www.web3d.org/x3d/content/examples/Basic/StudentProjects/ProjectileInterpolatorPrototype.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("ProjectileInterpolatorPrototype.x3d"))
.addChild(new ProtoDeclare("ProjectileInterpolator").setName("ProjectileInterpolator").setAppinfo("This prototype is a Projectile Motion Interpolator. It takes Inputs : Initial Velocity and Angle then calculates trajectory of the projectile on x-y plane according to given dt time and B_m. It outputs value_changed values(SFVec3f).")
.setProtoInterface(new ProtoInterface()
.addField(new field().setName("Vi").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY).setAppinfo("Initial Velocity value of the object."))
.addField(new field().setName("theta").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY).setAppinfo("Launch Angle. The angle between horizantal and launch direction"))
.addField(new field().setName("B_m").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY).setValue(0.00004).setAppinfo("Proportional to drag force = B2/m"))
.addField(new field().setName("dt").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY).setValue(0.1).setAppinfo("Time step."))
.addField(new field().setName("fraction").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY).setAppinfo("SFFloat Values ranging [0..1]."))
.addField(new field().setName("set_fraction").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INPUTONLY).setAppinfo("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_theta").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INPUTONLY).setAppinfo("Sets theta to the value of eventIn."))
.addField(new field().setName("set_Vi").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INPUTONLY).setAppinfo("Sets Vi to the value of eventIn."))
.addField(new field().setName("value_changed").setType(field.TYPE_SFVEC3F).setAccessType(field.ACCESSTYPE_OUTPUTONLY).setAppinfo("The interpolator function eventOut results as Vector3Float.")))
.setProtoBody(new ProtoBody()
.addChild(new Script("ProjectileMotionTrackerScript").setSourceCode("""
ecmascript:
var x;
var y;
var Vx;
var Vy;
var B_m;
var dt;
var blocksize;
var Vi;
var theta;
var key;
var keyValue;
var previousFraction;
var previousFractionIndex;
var blockSize;
var outputArray;
function tracePrint (outputString)
{
var traceEnabled = true;
if (traceEnabled) Browser.println ('[WaypointInterpolator]' + outputString);
}
function alwaysPrint (outputString)
{
Browser.println ('[WaypointInterpolator]' + outputString);
}
function initialize() {
key = new Array();
keyValue = new MFVec3f();
x = new Array();
y = new Array();
calculateTrajectory();
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 = 3; //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 SFVec3f();
outputArray = keyValue[0];
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)');
outputArray = keyValue[(keyValue.length -1)];
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])');
// update outputArray - need to interpolate next
outputArray = keyValue[previousFractionIndex];
}
else {
delta = key[previousFractionIndex + 1] - key[previousFractionIndex];
differ = fraction - key[previousFractionIndex];
percentDiffer = differ / delta;
valueDelta = new SFVec3f();
for(index = 0; index < blockSize; index++) {
valueDelta[index] = keyValue[(previousFractionIndex + 1)][index] - keyValue[previousFractionIndex][index];
outputArray[index] = keyValue[previousFractionIndex][index] + valueDelta[index] * percentDiffer;
Browser.println ('valueDelta' + valueDelta[index]);
Browser.println ('perDiffer' + percentDiffer);
}
}
value_changed = outputArray;
previousFraction = fraction;
tracePrint ('value_changed=' + value_changed);
}
function set_Vi(initialVelocity, timeStamp) {
Vi = initialVelocity;
initialize(timeStamp);
}
function set_theta(angle, timeStamp) {
theta = angle;
initialize(timeStamp);
}
function calculateTrajectory() {
x[0] = 0;
y[0] = 0;
var timeKeys = new Array();
timeKeys[0] = 0.0;
//convert degree to radian
angle = Math.PI * theta / 180;
Vx = Vi * Math.cos(angle);
Vy = Vi * Math.sin(angle);
var i = 0;
do {
i = i + 1;
timeKeys[i] = timeKeys[i - 1] + dt;
Browser.println ('timeKeys' + timeKeys[i]);
x[i] = x[i - 1] + Vx * dt;
y[i] = y[i - 1] + Vy * dt;
f = B_m * Math.sqrt(Vx * Vx + Vy * Vy) * Math.exp(-y[i] / 0.0001);
Vy = Vy - 9.8 * dt - f * Vy * dt;
Vx = Vx - f * Vx * dt;
Browser.println ('Vy' + Vy);
}while(y[i] > 0);
Browser.println ('Im here' + x.length);
//interpolate to find landing point
var a = -y[i] / y[i-1];
x[i] = (x[i] + a * x[i-1]) / (1+a);
y[i] = 0;
//copy x, y values to keyValues
copyToKeyValues();
//finding keys
for(j = 0; j < timeKeys.length; j++) {
key[j] = timeKeys[j] / timeKeys[timeKeys.length - 1];
Browser.println (' ' + key[j]);
}
}
function copyToKeyValues() {
for(i = 0; i < x.length; i++) {
Browser.println ('x' + x[i]);
keyValue[i][0] = x[i];
keyValue[i][1] = y[i];
keyValue[i][2] = 0;
Browser.println ('keyValue' + i + ' ' + keyValue[i][0]);
}
}
""")
.addField(new field().setName("Vi").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY))
.addField(new field().setName("theta").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY))
.addField(new field().setName("B_m").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY))
.addField(new field().setName("dt").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY))
.addField(new field().setName("fraction").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY).setAppinfo("In range [01]"))
.addField(new field().setName("set_fraction").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INPUTONLY))
.addField(new field().setName("set_theta").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INPUTONLY))
.addField(new field().setName("set_Vi").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INPUTONLY))
.addField(new field().setName("value_changed").setType(field.TYPE_SFVEC3F).setAccessType(field.ACCESSTYPE_OUTPUTONLY))
.setIS(new IS()
.addConnect(new connect().setNodeField("Vi").setProtoField("Vi"))
.addConnect(new connect().setNodeField("theta").setProtoField("theta"))
.addConnect(new connect().setNodeField("B_m").setProtoField("B_m"))
.addConnect(new connect().setNodeField("dt").setProtoField("dt"))
.addConnect(new connect().setNodeField("fraction").setProtoField("fraction"))
.addConnect(new connect().setNodeField("set_fraction").setProtoField("set_fraction"))
.addConnect(new connect().setNodeField("set_theta").setProtoField("set_theta"))
.addConnect(new connect().setNodeField("set_Vi").setProtoField("set_Vi"))
.addConnect(new connect().setNodeField("value_changed").setProtoField("value_changed"))))))
.addComments(" ==================== ")
.addChild(new Anchor().setDescription("ProjectileInterpolatorArena").setParameter(new String[] {"target=_blank"}).setUrl(new String[] {"ProjectileInterpolatorArena.wrl","https://www.web3d.org/x3d/content/examples/Basic/StudentProjects/ProjectileInterpolatorArena.wrl","ProjectileInterpolatorArena.x3d","https://www.web3d.org/x3d/content/examples/Basic/StudentProjects/ProjectileInterpolatorArena.x3d"})
.addChild(new Shape()
.setGeometry(new Text().setString(new String[] {"ProjectileInterpolatorPrototype","defines a prototype","","Click on this text to see","ProjectileInterpolatorArena"," 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)))))
.addChild(new PositionInterpolator()));
}
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 ProjectileInterpolatorPrototype 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 ProjectileInterpolatorPrototype().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.StudentProjects.ProjectileInterpolatorPrototype\" 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.StudentProjects.ProjectileInterpolatorPrototype self-validation test results: ");
if (!validationResults.equals("success"))
System.out.println();
System.out.println(validationResults.trim());
}
}
}