/* CMD: Rotation & Distance Generator         

   Co-Authors  : Scott Wheeler 
                 Joe Dox

   Date	       : November 22nd 1994

   Description : 1) Calculates the rotation of an object nessasary to simulate 
                    that object traveling at a user definable speed.  
                 
                 2) If desired it will then create a series of motion paths for 
                    that object.
*/

libadd = addlib("LWModelerARexx.port",0)
signal on error
signal on syntax

call addlib "rexxsupport.library", 0, -30, 0
MATHLIB="rexxmathlib.library"
IF POS(MATHLIB , SHOW('L')) = 0 THEN
  IF ~ADDLIB(MATHLIB , 0 , -30 , 0) THEN DO
	call notify(1,"!Can't find "MATHLIB)
	exit
	END

					/* Priming variables */
procname           = 'Rotation & Distance Generator v1.0 © 1994 GVASoft'
procname2          = 'Rotation & Distance Generator'
FramesInAnim       = 30
SpeedOfVehicle     = 60.0
RadiusOfWheel      = 30.0
TireCircumference  = 0.0
InchesPerSecond    = 0.0
DistanceTraveled   = 0.0
NumberOfRotations  = 0.0
MovementAxis       = 0
RotationField      = 0
AxisOffset         = 0
MeasurementSystem  = 0
CombinedMotionSave = 0
VehicleMotionSave  = 0
TireMotionSave     = 0 

call req_begin procname

id_MeasurementSystem  = req_addcontrol('Desired Measurement System','ch','Metric English') 
id_TireMotionSave     = req_addcontrol('Save Seperate Tire Motion?','b',0) 
id_VehicleMotionSave  = req_addcontrol('Save Seperate Vehicle Motion?','b',0) 
id_CombinedMotionSave = req_addcontrol('Save Combined Motion?','b',0) 
call req_setval id_MeasurementSystem, 1
call req_setval id_TireMotionSave, 1 
call req_setval id_VehicleMotionSave, 1 
call CheckCANCEL()

MeasurementSystem  = req_getval(id_MeasurementSystem)
CombinedMotionSave = req_getval(id_CombinedMotionSave)
VehicleMotionSave  = req_getval(id_VehicleMotionSave)
TireMotionSave     = req_getval(id_TireMotionSave)
call req_end

call req_begin procname2
					/* Designing window 1 */
if MeasurementSystem = 1 then do	/* Setup Metric controls */
 Distance = 'meters'
 id_radius = req_addcontrol("Radius of Tire (m)", 'n', 0)
 id_speed = req_addcontrol('Speed of Vehicle (kph)','n',0)
 id_frames = req_addcontrol('Frames In Animation','n',0)
 end
else do					/* Setup English controls */
 Distance = 'feet'
 id_radius = req_addcontrol("Radius of Tire (inches)", 'n', 0)
 id_speed = req_addcontrol('Speed of Vehicle (mph)','n',0)
 id_frames = req_addcontrol('Frames In Animation','n',0)
end 


					/* Setting Values from Imput Gadgets */

Box = BOUNDINGBOX()
Parse Var Box np x1 x2 y1 y2 z1 z2
RadiusOfWheel = MAX( abs(x1), abs(x2), abs(y1), abs(y2), abs(z1), abs(z2) )
if MeasurementSystem = 2 then RadiusOfWheel = RadiusOfWheel * 39.37  
if ( RadiusOfWheel = 0 ) then RadiusOfWheel = 1
call req_setval id_radius, RadiusOfWheel, RadiusOfWheel
call req_setval id_speed, SpeedOfVehicle, SpeedOfVehicle
call req_setval id_frames, FramesInAnim, FramesInAnim
call CheckCANCEL()

					/* Getting Values back from Gadgets */
RadiusOfTire      = req_getval(id_radius)
SpeedOfVehicle    = req_getval(id_speed)
FramesInAnim      = req_getval(id_frames)


					/* Doing the Math */
if MeasurementSystem = 1 then do
 SpeedType="kph"
 TireCircumference = RadiusOfTire*(2*3.141592654)
 MetersPerSecond   = (SpeedOfVehicle/3600)*1000.0
 DistanceTraveled  = MetersPerSecond*(FramesInAnim/30.0)
 NumberOfRotations = (DistanceTraveled/TireCircumference)*360.0
 end
else do
 SpeedType="mph"
 TireCircumference = RadiusOfTire*(2*3.141592654) 
 InchesPerSecond   = ((SpeedOfVehicle/3600)*5280)*12.0
 DistanceTraveled  = InchesPerSecond*(FramesInAnim/30.0)
 NumberOfRotations = (DistanceTraveled/TireCircumference)*360.0
 DistanceTraveled  = DistanceTraveled/12.0
end


					/* Cleaning up variables for output */
FramesInAnim 	  = TRUNC(FramesInAnim)
DistanceTraveled  = TRUNC(DistanceTraveled,2)
NumberOfRotations = TRUNC(NumberOfRotations,2)

					/* Closing first window */
call req_end				

if ( MeasurementSystem = 1 ) then do			
 ok=Notify(1,'@Your vehicle will travel 'DistanceTraveled' 'Distance' ('TRUNC((DistanceTraveled*39.37)/12,2)' feet) in 'FramesInAnim' frames.','@The wheels should rotate 'NumberOfRotations'°.')
 end
else do
 ok=Notify(1,'@Your vehicle will travel 'DistanceTraveled' 'Distance' ('TRUNC((DistanceTraveled*12)/39.37,2)' meters) in 'FramesInAnim' frames.','@The wheels should rotate 'NumberOfRotations'°.')
end
					/* Saving Combined Motion File */
if (CombinedMotionSave = 1)  then do
 call req_begin 'Define Combined Movement Axis and Rotation Field'

 id_axis = req_addcontrol('What axis will wheel travel on: ','ch','X Y Z')
 id_rotation = req_addcontrol('What is the field of rotation: ','ch','H P B')
 id_offset = req_addcontrol('Starting position offset (m): ','n',0)

 call req_setval id_axis, 3
 call req_setval id_rotation, 2
 call req_setval id_offset, 0
 call CheckCANCEL()

 MovementAxis  = req_getval(id_axis)
 RotationField = req_getval(id_rotation)
 AxisOffset    = TRUNC(req_getval(id_offset),2)

 call req_end
 ReqName    = "Save Combined Motion File"
 MotionType = "ComdinedMotion"
 call SaveMotion()
end

					/* Saving Vehicle Motion File */
if (VehicleMotionSave = 1) then do
 call req_begin 'Define Vehicle Movement Axis'

 id_axis = req_addcontrol('What axis will wheel travel on: ','ch','X Y Z')
 id_offset = req_addcontrol('Starting position offset (m): ','n',0)

 call req_setval id_axis, 3
 call req_setval id_offset, 0
 call CheckCANCEL()

 MovementAxis      = req_getval(id_axis)
 RotationField     = 1
 NumRots           = NumberOfRotations		
 NumberOfRotations = 0.0
 AxisOffset        = TRUNC(req_getval(id_offset),2)

 call req_end
 ReqName           = "Save Vehicle Motion File"
 MotionType        = "VehicleMotion"
 call SaveMotion()
end

					/* Saving Tire Motion File */
if (TireMotionSave = 1) then do
 call req_begin 'Define Tire Rotation Field'
 
 id_rotation = req_addcontrol('What is the field of rotation: ','ch','H P B')
 call req_setval id_rotation, 2
 call CheckCANCEL()

 NumberOfRotations = NumRots
 RotationField     = req_getval(id_rotation)
 MovementAxis      = 1
 AxisOffset        = 0.0
 DistanceTraveled  = 0.0

 call req_end
 ReqName    = "Save Tire Motion File"
 MotionType = "WheelMotion"
 call SaveMotion()
end


EXIT					/* End of Line */


SaveMotion:
 fname=GETFILENAME(ReqName,'motions',MotionType'.'TRUNC(SpeedOfVehicle,0)SpeedType)
 if ( fname = '(none)' ) then exit
 if ( open(outfile, fname, 'W')) then do
  call Header()
  call VehicleMovement()
  call Rotation()
  call Footer()
 end
return


Header:
  call writeln outfile, 'LWMO'
  call writeln outfile, '1'
  call writeln outfile, '9'
  call writeln outfile, '2'
return


VehicleMovement:
 if ( MovementAxis = 1 ) then call writeln outfile, AxisOffset' 0.0 0.0 0.0 0.0 0.0 1.0 1.0 1.0'
 if ( MovementAxis = 2 ) then call writeln outfile, '0.0 'AxisOffset' 0.0 0.0 0.0 0.0 1.0 1.0 1.0'
 if ( MovementAxis = 3 ) then call writeln outfile, '0.0 0.0 'AxisOffset' 0.0 0.0 0.0 1.0 1.0 1.0'
 call writeln outfile, '0 0 0.0 0.0 0.0'
 if ( MovementAxis = 1 ) then call writech outfile, DistanceTraveled+AxisOffset' 0.0 0.0 '
 if ( MovementAxis = 2 ) then call writech outfile, '0.0 'DistanceTraveled+AxisOffset' 0.0 '
 if ( MovementAxis = 3 ) then call writech outfile, '0.0 0.0 'DistanceTraveled+AxisOffset' '
return


Rotation:
  if ( RotationField = 1 ) then call writech outfile, NumberOfRotations' 0.0 0.0 '
  if ( RotationField = 2 ) then call writech outfile, '0.0 'NumberOfRotations' 0.0 '
  if ( RotationField = 3 ) then call writech outfile, '0.0 0.0 'NumberOfRotations' '
return


Footer:
  call writeln outfile, '1.0 1.0 1.0'
  call writeln outfile, FramesInAnim' 0 0.0 0.0 0.0'
  call close outfile 
Return

CheckCANCEL:				/* Check for cancel out */
 if (~req_post()) then do		
    call req_end 
    ok=Notify(1,'@'procname2,'','A Product of GVASoft International Ltd.','All Rights Reserved')
    exit
 end
return