#VRML V2.0 utf8
# A data plotting template, a component in a 3D data plotting application 
# Kurt A. Fisher fisherka@csolutions.net  beginner level VRML coder
# 12/2003

EXTERNPROTO W_SPHERE [ 
	exposedField SFColor	s_objColor	# obj color
] "lib_Wire_Sphere_proto.wrl"  # An external wire-frame sphere object imported to this template

# General prototype for plotting objects 
PROTO SO [ 
	exposedField MFString	objName		[ "Object", "Alternate Name" ]	# object
	exposedField SFString	objClass	"Type"			# type of object for EIA sort select
	exposedField SFString	subClass	"SubType"		# subtype of object for EIA sort select
	exposedField SFInt32	PlotType	1			# Plot type 0=sphere;1=point;2=translucent sphere;3=wiresphere;4=cube;5=arrow;6=nothing
	exposedField SFInt32	BandDecorate	1			# 0=yes add decoration;1=no decoration
	exposedField SFVec3f	objPosition	0.0 0.0 0.0		# position
	exposedField SFVec3f 	objSize		0.001 0.001 0.001	# size - radii in x,y,z planes - not diameters 
	exposedField SFRotation	posAngle	0.0 1.0 0.0 0.00	# position angle ( z y x rot );  normal plotted space rotations are around y-axis; note rotation form is not normal "x y z rot" syntax   
	exposedField SFColor	objColor	0.0 0.0 0.0		# obj color
	exposedField SFInt32	lblType		4			# label type 0=none; 1=1st strg; 2=2nd strg; 3=3rd strg; 4=all
	exposedField SFFloat	fontSize	0.1			# font size
	exposedField SFColor	lblColor	0.0 0.0 0.0		# label color 
#									# Label family, style (bold,italics,etc.) and justify cannot be changed 
#									# See "DEF tmp_lblFontStyleText" for hardcoding of these characteristics  
	exposedField SFInt32	BarDecorate	1			# 0=yes add decoration;1=no decoration
	exposedField SFVec3f	barHeight	.005 0.25 .005		#  dimensions of optional uncertainty/vector bar 
									#   x,z as diameter of bar;  y is height ( auto modified to 2 * height  )	
	exposedField SFRotation	barAngle	0.0 0.0 1.0 1.571	# direction of optional uncertainty/vector bar in x-y(3d) plane around z axis     
#									#     rotation is select x,y,z planes plus rotation in radians 
] {

# ==========================================
#  Start plot point  
DEF Sphere_Object Transform {
#                           Key position of object, translated in x,y,z
		translation IS objPosition
		rotation IS posAngle 
		children [

#  Start shape of Sphere_Object
#       Start sphere of a SO 

#  To keep objSize type uniform as 3f vector,  use Transform to resize the spherical object 
		Transform {
# 			Key size of object, stretched in x,y,z
			scale IS objSize
			children [

#  START SHAPE OF PLOTTED OBJECT
Switch { 
	whichChoice IS PlotType
	choice [ 

#  PLOT SHAPE WITH SPHERICAL OBJECT
	# version 0: shape sphere 
		Group { 
			children [ 
#  Start Anchor for referral for more object information 
				DEF SO_Anchor_Sphere Anchor {
#				              Displays refered page in new browser instance
#  Pop-up display disabled		parameter [ "target='_blank'" ]
#					parameter [ "target='_blank'" ]
					children [
						Shape {				
							appearance Appearance {
								material Material { 
#									diffuseColor IS objColor
									emissiveColor IS objColor
									shininess .08
								}
							}
							geometry Sphere {  } # end geometry
						} # end shape
						] # end children
					} # End Anchor	
			] # children
		} # end Group


#  PLOT SHAPE WITH POINT OBJECT
	# version 1: shape point 
		Group { 
			children [ 
#  Start Anchor for referral for more object information 
				DEF SO_Anchor_Point Anchor {
#				              Displays refered page in new browser instance
#  Pop-up display disabled 		parameter [ "target='_blank'" ]
#					parameter [ "target='_blank'" ]
					children [
						Shape {				
							appearance Appearance {
								material Material { 
									emissiveColor IS objColor
									shininess .08
								}
							}
							geometry PointSet { 
								coord Coordinate { 
									point [ 
									   0 0 0 
									] # end point 
								} # end coordinate 
							 } # end geometry
						} # end shape
						] # end children
					} # End Anchor	
			] # children
		} # end Group

#  PLOT SHAPE WITH TRANSLUCENT SPHERICAL OBJECT
	# version 2: shape translucent sphere 
		Group { 
			children [ 
#  Start Anchor for referral for more object information 
				DEF SO_Anchor_TranslucentSphere Anchor {
#				              Displays refered page in new browser instance
#  Pop-up display disabled 		parameter [ "target='_blank'" ]
#					parameter [ "target='_blank'" ]
					children [
						Shape {				
							appearance Appearance {
								material Material { 
#									diffuseColor IS objColor
									emissiveColor IS objColor
									shininess .08
									transparency 0.7
								}
							}
							geometry Sphere {  } # end geometry
						} # end shape
						] # end children
					} # End Anchor	
			] # children
		} # end Group


#  PLOT SHAPE WITH WIRE FRAME SPHERE OBJECT
	# version 3: shape sphere wire frame  
		Group { 
			children [ 
#  Start Anchor for referral for more object information 
				DEF SO_Anchor_WireSphere Anchor {
#				              Displays refered page in new browser instance
#  Pop-up display disabled 		parameter [ "target='_blank'" ]
#					parameter [ "target='_blank'" ]
					children [

# Import abstracted wire-frame sphere object to this template
W_SPHERE {  s_objColor IS objColor } 

						] # end children
					} # End Anchor	
			] # children
		} # end Group


#  PLOT SHAPE WITH CUBE OBJECT
	# version 4: shape cube 
		Group { 
			children [ 
#  Start Anchor for referral for more object information 
				DEF SO_Anchor_Cube Anchor {
#				              Displays refered page in new browser instance
#  Pop-up display disabled 		parameter [ "target='_blank'" ]

					children [
						Shape {				
							appearance Appearance {
								material Material { 
#									diffuseColor IS objColor
									emissiveColor IS objColor
									shininess .08
								}
							}
							geometry Box {  } # end geometry
						} # end shape
						] # end children
					} # end anchor 
			] # children
		} # end Group

#  PLOT SHAPE WITH ARROW OBJECT 
#  - Usage note - use posAngle to position the arrow in 3D space 
#  standard x-axis rotation 0.0	0.0	1.0	-1.547  
	# version 5: arrow 
		Group { 
			children [ 
#  Start Anchor for referral for more object information 
				DEF SO_Anchor_Arrow Anchor {
#				Displays refered page in new browser instance
#  Pop-up display disabled 		parameter [ "target='_blank'" ]
#					parameter [ "target='_blank'" ]
					children [
# Begin Arrow Shape 

#  Position the arrow in a standard x-axis rotation 0.0 0.0 1.0 -1.547  
#  Center the arrow in its frame 
Transform { 
	rotation 0.0 0.0 1.0 -1.547  	# rotate arrow into standard x-position
				#  posAngle will now implement in normal space with single rotation 
	translation -0.1 0.0 0.0  	# centering translation 
	children [ 
# Arrow Trunk
		Shape { 
			appearance Appearance {
				material Material { 
#									diffuseColor IS objColor
									emissiveColor IS objColor
					shininess .08
					} # end Material 
			} # end Appearance
			geometry Cylinder { 
				radius 0.1  
				height 1.75
			} # end geometry
		} # end Shape Arrow Trunk 
#  Arrow Head 
		Transform { 
			translation 0.0 0.85 0.0 
			children [ 
				Shape { 
					appearance Appearance {
						material Material { 
#									diffuseColor IS objColor
									emissiveColor IS objColor
							shininess .08
						} # end Material 
					} # end Appearance
					geometry Cone { 
						bottomRadius 0.3 
						height 0.5  
					} # end geometry
				} # end Shape Arrow Head 
		
		] # children Transform Arrow Head
		} # end Transform Arrow Head  

	] # children Transform Arrow Shape to center Arrow 
} # end Transform Arrow Shape to center Arrow 

						] # end children
					} # end anchor 
			] # children
		} # end Group
# END ARROW SHAPE 

#  START NULL OPTION GROUP - NO OBJECT
	# version 6: Null group  
		Group { 
			children [ 
			] # children
		} # end Group

	] # end choice
} # end Switch 

#  START OPTIONAL BANDS AROUND OBJECT 
Switch { 
	whichChoice IS BandDecorate 
	choice [ 

#  START SHAPE OF BAND DECORATOR
	# version 0: shape sphere 
		Group { 
			children [ 

#  Start shape statement -  band around sphere in z-x plane
				Shape {				
					appearance Appearance {
						material Material { 
#  grey shiny band 
#							diffuseColor 0.3 0.3 0.3 
							emissiveColor 0.3 0.3 0.3 
							shininess .16
						}  # end Material
					}  # end Appearance 
					geometry Cylinder {
						height 0.1
					} # end geometry 
				} #  End shape 
# End band around sphere in z-x

#  Start shape statement - band around sphere in z-y plane
				Transform {
					rotation	0.0 0.0 1.0 1.571
#  around in z-y plane
					children [
						Shape {				
						appearance Appearance {
							material Material { 
#  grey shiny band 
#							diffuseColor 0.3 0.3 0.3 
							emissiveColor 0.3 0.3 0.3 
							shininess .16
							} # end Material
						} # end Appearance 
						geometry Cylinder {
							height 0.1
						} # end Geometry 
					} # end Shape 
					] # end Children 
				} # end Transform 
# End band around sphere in z-y plane

#  Start shape statement - band around sphere in x-y plane
				Transform {
					rotation	1.0 0.0 0.0 1.571
#  around in z-y plane
					children [
						Shape {				
						appearance Appearance {
							material Material { 
#  grey shiny band 
#							diffuseColor 0.3 0.3 0.3 
							emissiveColor 0.3 0.3 0.3 
							shininess .16
							} # end Material 
						}  # end Appearance 
						geometry Cylinder {
							height 0.1
						} # end Geometry 
					} # end Shape
					] # end children 
				} # end Transform 
# End shape statement - band around sphere in x-y plane
#  END OPTIONAL BANDS AROUND OBJECT 

			] # children
		} # end Group

#  START NULL OPTION GROUP - NO DECORATOR BANDS
	# version 1: Null group  
		Group { 
			children [ 
			] # children
		} # end Group

	] # end choice
} # end Switch 

     ] # end children of Sphere_Object
     }  # end Transform of Sphere_Object
#  END PLOTTED SHAPE 

# START OPTION VECTOR/UNCERTAINTY BAR
Switch { 
	whichChoice IS BarDecorate
	choice [ 

#  START BASIC SHAPE OF SPHERICAL OBJECT
	# version 0: shape bar 
		Group { 
			children [ 

#  START OPTIONAL VECTOR BAR 
#  Start shape statement - vector point for uncertainty and direction 
				DEF objVectorBar Transform {
# rotation has four floats 1-3 are x,y,z axis and fourth is rotation 
					rotation IS barAngle  
                              scale 0.005 1.0 0.005
# - around in z-y plane
					children [
						Shape {				
						appearance Appearance {
							material Material { 
#								diffuseColor 0.0 0.0 0.0 
								emissiveColor 0.0 0.0 0.0 

#  black vector bar 
#								shininess .08
							} # end Material 
						} # end Appearance 
						geometry Cylinder {
							height .5         #  plots as 1/2 * height centered on origin
							radius 1
						} # end geometry 
					} # end Shape
					] # end children 
				} # end Transform 
#  END OPTIONAL VECTOR BAR 

		] # children
		} # end Group

#  START NULL OPTION GROUP - NO VECTOR BAR 
	# version 1: Null group 
		Group { 
			children [ 
			] # children
		} # end Group

	] # end choice
} # end Switch 
#  END OPTIONAL VECTOR BAR 


#  START LABELS FOR OBJECT 
		DEF lblObject Transform {
				translation	0.1 0.0 0.0
#  Label size is scaled in initialization script 
#  Since labels are auto-billboarding to face viewer,  there is no need to orient the labels with rotation. 
				children [
# Begin billboarding text label
					Billboard {
						axisOfRotation 0 0 0
# Auto-point the labels to the viewer 
						children [ 
# Begin billboarding shape
							Shape {
							appearance Appearance { 
								material Material { 
#								diffuseColor IS lblColor
								emissiveColor IS lblColor
								} # end Material 
							}  # end Appearance 
						    	geometry DEF lblName Text {
#     Label string is set in initialization script 
								string IS objName    
								fontStyle DEF lblNameStyle FontStyle { 
									family "SANS"
									style "BOLD"
									justify "BEGIN"
#									size 1.0 
#									size IS fontSize
								}  # End of fontStyle 
							}  # End of geometry Text
						}  # End of billboarding shape
						]  # End of billboarding children 
					}  # End of billboarding text label
#  	
			] # end children of Transform of lblObject 
		}  # end Transform of lblObject 
# END LABEL FOR OBJECT 

# ADD SENOR (TOUCH) FOR OBJECT 
		DEF sense_Sphere_Object TouchSensor {  }
# END SENOR (TOUCH) FOR OBJECT 

		]  # end children of Transform of Sphere_Object 
	}  # end of Transform of Sphere_Object 
#  END PLOTTED OBJECT,  DECORATORS AND LABEL 


# BEGIN PASS DATA FROM EXPOSED FIELDS IN PROTOTYPE TO OBJECT INSTANCE 
DEF tmp_lblName Text { string IS objName }						# to set label types on initialization
DEF tmp_lblType Switch { whichChoice IS lblType }					# to set label types on initialization
DEF tmp_objSize Transform { scale IS objSize } 						# to set label types on initialization
DEF tmp_lblFontStyleTextPass Text {  maxExtent IS fontSize }				# pass label size on initialization; 
                                                                                       	# unique syntax to handle that Text size is not exposed field 

# Syntax - disabled on CosmoPlayer compatability adjustments 
# DEF tmp_lblFontStyleText Text {							# to set label size on initialization
#	fontStyle DEF tmp_lblFontStyle FontStyle {					# syntax note - unable to set family, style, justify with script
#                          family "SANS" 
#                          style "BOLD"
#                          justify "BEGIN"
#		  size 0.0  } # end fontStyle 
#             }  # end Text	                                                              

DEF tmp_objVectorBar Transform { scale IS barHeight }			# to set uncertainty bar size on initialization
# END PASS DATA FROM PROTOTYPE TO OBJECT INSTANCE 

#  BEGIN BEHAVIORS OF OBJECT
# Initialization script 
DEF get_Initialize Script {
#   Pass object variables as input variables for these functions  
field SFNode LabelNode USE tmp_lblName                                  # label array string
field SFNode LabelType USE tmp_lblType                                  # labeling type
field SFNode LabelScale USE tmp_objSize                                 # object size for label spacing 
field SFNode LabelFontSizePass USE tmp_lblFontStyleTextPass 		# to pass font size stored in maxExtent exposed field 
# Syntax - disabled on CosmoPlayer compatability adjustments 
# field SFNode LabelFontStyle USE tmp_lblFontStyle                      # font size for label spacing 
field SFNode VectorBarSize USE tmp_objVectorBar                     	# radius of uncertainty bar - s/b diameter 

directOutput TRUE
  url [ 
	"javascript:
	 function initialize (  ) {

//  Note all arrays received from VRML are 0 based, while ECMA javascript arrays are 1 based

//  Syntax -  initialize variable for CosmoPlayer compatability - Cosmo wants array hard-initialized 
string_changed = new MFString() ;

//           Set label string 
//                          Case 0 - If no labels, label is blank    
		if ( LabelType.whichChoice == 0 ) { string_changed = [ '', '' ] ; }
//                          Case 1 - Use first string as label     
		if ( LabelType.whichChoice == 1 ) { string_changed[0] = LabelNode.string[0] ; }
//                          Case 2 - Use second string as label     
		if ( LabelType.whichChoice == 2 ) { string_changed[0] = LabelNode.string[1] ; }
//                          Case 3 - Use third string as label          
		if ( LabelType.whichChoice == 3 ) { string_changed[0] = LabelNode.string[2] ; }
//                          Case 4 - Use all strings in array as label     
		if ( LabelType.whichChoice == 4 ) { string_changed = LabelNode.string ; }

//           Assertion - check for undefined empty strings 
		if ( string_changed[0] == 'undefined' )  { string_changed[0] = '' ; }
		if ( string_changed[1] == 'undefined' )  { string_changed[1] = '' ; }
		if ( string_changed[2] == 'undefined' )  { string_changed[2] = '' ; }

//  Syntax -  initialize variable CosmoPlayer compatability 
		anchor_url_changed = new MFString(); 
// Syntax note - = new SFString is not recognized within Javascript 
		anchor_desc_changed = new String(); 
		array_desc_changed = new Array();

//           Set anchor url - based on first label name 
		anchor_url_changed[0] = 'refer_'+LabelNode.string[0] + '.htm'  ;
		anchor_url_changed[1] = 'refer_Not_implemented.htm'  ;

//  Syntax -	CosmoPlayer incompatability - CosmoPlayer will not implement cascading checks of not located links.
//		Implement a second copy of the refer - not implemented file as refer_.htm to pick up no label items 

//           Set anchor description - use all information 
//		anchor_desc_changed = LabelNode.string; 
		array_desc_changed = LabelNode.string;
		anchor_desc_changed = array_desc_changed.toString(); 

//           Determine and set the label spacing from object 
//           Creates a 3FVector for use in translating the label in the x-axis
//             Pass fontsize - a non-exposedfield of SFFloat type

// 			Syntax - Disable - For CosmoPlayer compatability - FontStyle.size is non-exposed object
//			LabelFontStyle.size = LabelFontSizePass.maxExtent ;

//   			Syntax - Replacement code CosmoPlayer compatability - FontStyle.size is non-exposed object
//           Creates a 3FVector for use in translating the label in the x-axis
		var flt_FontSize_changed = LabelFontSizePass.maxExtent; 
		SFVec3f_FontSize_changed = new SFVec3f(); 
		SFVec3f_FontSize_changed[0] = flt_FontSize_changed;
		SFVec3f_FontSize_changed[1] = flt_FontSize_changed;
		SFVec3f_FontSize_changed[2] = flt_FontSize_changed;

//             Set the label spacing 

//   Syntax - Replacement code for CosmoPlayer compatability - FontStyle.size is non-exposed object
//                Label spacing is fontsize plus object size where object size in x axis is equal or greater than font size 
                           if (   LabelScale.scale[0]  >= flt_FontSize_changed ) { label_spacing[0] = LabelScale.scale[0] + ( 0.25 * flt_FontSize_changed ) ; }
//                Label spacing is object size where object size in x axis is less than font size
                           if (   LabelScale.scale[0]  < flt_FontSize_changed ) { label_spacing[0] = LabelScale.scale[0] + ( 0.25 * flt_FontSize_changed ) ; }

//	    Set y and z translation values 
	             label_spacing[1]  = 0 ;
   	             label_spacing[2]  = 0 ;

//           Determine and set the uncertainty bar vector size -  should be twice value  
 		adjusted_bar_size[0] = VectorBarSize.scale[0] ;
 		adjusted_bar_size[1] = VectorBarSize.scale[1] * 2.0 ;
 		adjusted_bar_size[2] = VectorBarSize.scale[2] ;
	  }"
      ] # end url 
#  Make events for function output 
eventOut MFString string_changed			# label string
eventOut SFVec3f label_spacing				# label spacing
# Syntax - disabled to convert to Cosmo compability 
# eventOut SFNode label_fontstyle_out			# label fontstyle 
eventOut SFVec3f SFVec3f_FontSize_changed		# new fontsize rescalar 
eventOut MFString anchor_url_changed			# anchor url
eventOut SFString anchor_desc_changed			# anchor label
eventOut SFVec3f adjusted_bar_size			# adjusted height of uncertainty bar
 } # end Script 
# Route function results back to object 
ROUTE get_Initialize.anchor_url_changed TO SO_Anchor_Sphere.set_url				# begin set anchors and urls
ROUTE get_Initialize.anchor_desc_changed TO SO_Anchor_Sphere.set_description
ROUTE get_Initialize.anchor_url_changed TO SO_Anchor_Point.set_url
ROUTE get_Initialize.anchor_desc_changed TO SO_Anchor_Point.set_description
ROUTE get_Initialize.anchor_url_changed TO SO_Anchor_TranslucentSphere.set_url
ROUTE get_Initialize.anchor_desc_changed TO SO_Anchor_TranslucentSphere.set_description
ROUTE get_Initialize.anchor_url_changed TO SO_Anchor_WireSphere.set_url
ROUTE get_Initialize.anchor_desc_changed TO SO_Anchor_WireSphere.set_description
ROUTE get_Initialize.anchor_url_changed TO SO_Anchor_Cube.set_url
ROUTE get_Initialize.anchor_desc_changed TO SO_Anchor_Cube.set_description
ROUTE get_Initialize.anchor_url_changed TO SO_Anchor_Arrow.set_url
ROUTE get_Initialize.anchor_desc_changed TO SO_Anchor_Arrow.set_description			# end set anchors and urls
ROUTE get_Initialize.string_changed TO lblName.set_string					# set the label string 
ROUTE get_Initialize.label_spacing TO lblObject.set_translation					# sets label lead spacing 
ROUTE get_Initialize.SFVec3f_FontSize_changed TO lblObject.set_scale				# sets sets fontsize
ROUTE get_Initialize.adjusted_bar_size TO objVectorBar.scale					# sets the height of the vector/uncertainty bar
# End routers

# Popup label in browser status bar 
DEF display_Message Script { 
	field SFNode LabelNode USE tmp_lblName
	eventIn SFBool set_boolean
	url [
		"javascript:
		function set_boolean (bolValue) { 

	//  Syntax - for Cosmo compability
//  Convert string array to string 
browser_desc_changed = new String();
array_desc_changed = new Array();

array_desc_changed = LabelNode.string;
browser_desc_changed = array_desc_changed.toString(); 

//           Set popup label
if( bolValue == true ) { Browser.setDescription(browser_desc_changed); }
	//  Syntax - for Blaxxun compatibility - remove if ... else statement 
if( bolValue == false ) { Browser.setDescription(''); }

	}" 
	] # end url 
#            Output events for browser router are implied 
}  # end Script
# Router for popup label in browser 
ROUTE sense_Sphere_Object.isOver TO display_Message.set_boolean 
# End routers 

#  END BEHAVIORS OF OBJECT

}  # END OF SPHERICAL PROTOTYPE 


