#usda 1.0
(
    "Savanna, stereo patch tracker"
)

def Xform "Scene"
{
    def Xform "ObjectsRoot"
    {
        def Xform "ObjectsPlaceholder"
        {
        }

        def Xform "DefaultObjectPrim"
        {
            def FrameSampler "RandomRelativeGaussianTranslation"
            {
                rel ref_model_diameter = <..>
                float3[] translation = [(-1, -1, -1), (1, 1, 1)]
                float3 gaussian_sampling_mean = (0, 0, 0)
                float3 gaussian_sampling_stddev = (0.2, 0.2, 0.2)
            }
            def FrameSampler "RandomUniformRotationEliza"
            {
                float3[] rotation = [(-90, -180,-180), (90, 180, 180)]

                # The template choses from these range options based on object parameters.
                def TemplatingParameters "RotationRanges" {
                    float[] limited_x_range = [0, 90]
                    float[] limited_y_range = [-90, 90]
                    float[] limited_z_range = [-10, 10]
                    float3[] all = [(-90, -180, -180), (90, 180, 180)]
                }
            }

            # A PoseLocalToWorld label request will be added after centering and scale
        }

        def FrameSampler "ActivateRandomObject"
        {
            rel ref_variation_path = </Scene/ObjectsRoot/ObjectsPlaceholder>
            rel ref_copy_attribute_destination = </Scene/ObjectsRoot/ObjectsPlaceholder>
            string[] copy_attributes_names = ["model_diameter"]
        }

        def LabelRequest "VariationLR"
        {
            string[] request:names = ["ActivateRandomObjectChoice"]
        }

        def FrameSampler "RandomPoseInFOV"
        {
            rel ref_cameras = [</CameraRig/DCAML_s2wExtrinsics/DCAML>, </CameraRig/DCAMR_s2wExtrinsics/DCAMR>]
            bool inverse_z_sampling = true
            float[] z_range = [0.05, 1.5]
            float border_region = 0.18
            float3[] rotation = [(0, 0, 0), (0, 0, 0)]
        }

        
        # Tracker occluders around the perturbed object
        def Xform "Occluders"
        {
            def ClipSampler "RandomVisibility"
            {
                float probability = 0.5
            }
            
            def Xform "OccluderCube1" (
                references = @../Cube.usda@
                inherits = </Scene/OccluderBase>
            )
            {
            }
            
            def Xform "OccluderCone1" (
                references = @../Cone.usda@
                inherits = </Scene/OccluderBase>
            )
            {
            }
            
            def Xform "OccluderCapsule1" (
                references = @../Capsule.usda@
                inherits = </Scene/OccluderBase>
            )
            {
            }
            
            def Xform "OccluderSphere1" (
                references = @../Sphere.usda@
                inherits = </Scene/OccluderBase>
            )
            {
            }
        }

        # LED/Marker aggressors around the perturbed object  
        def Xform "Aggressors"
        {
            def ClipSampler "RandomVisibility"
            {
                # Disable for now for further tuning
                float probability = 0.1
            }
            
            def Xform "AggressorMarker1" (
                references = @../Marker_with_visibility.usda@
                inherits = </Scene/AggressorBase>
            )
            {
                double radius = 0.003
            }
            def Xform "AggressorMarker2" (
                references = @../Marker_with_visibility.usda@
                inherits = </Scene/AggressorBase>
            )
            {
                double radius = 0.004
            }
            def Xform "AggressorMarker3" (
                references = @../Marker_with_visibility.usda@
                inherits = </Scene/AggressorBase>
            )
            {
                double radius = 0.005
            }
            def Xform "AggressorMarker4" (
                references = @../Marker_with_visibility.usda@
                inherits = </Scene/AggressorBase>
            )
            {
                double radius = 0.006
            }
            def Xform "AggressorLED1" (
                references = @../EmissiveLED_with_visibility.usda@
                inherits = </Scene/AggressorBase>
            )
            {
                double radius = 0.003
            }
            def Xform "AggressorLED2" (
                references = @../EmissiveLED_with_visibility.usda@
                inherits = </Scene/AggressorBase>
            )
            {
                double radius = 0.004
            }
            def Xform "AggressorLED3" (
                references = @../EmissiveLED_with_visibility.usda@
                inherits = </Scene/AggressorBase>
            )
            {
                double radius = 0.005
            }
            def Xform "AggressorLED4" (
                references = @../EmissiveLED_with_visibility.usda@
                inherits = </Scene/AggressorBase>
            )
            {
                double radius = 0.006
            }
        }
    }

    def Xform "OccluderBase" ()
    {
        int shader_mask_value = 0
        float model_diameter = 0.02
        
        def FrameSampler "RandomUniformRotationEliza"
        {
            float3[] rotation = [(-90, -180,-180), (90, 180, 180)]
        }

        def FrameSampler "RandomRelativeGaussianTranslation"
        {
            # The occluder is given a larger perturb range to avoid becoming a hint to the object position.
            rel ref_model_diameter = </Scene/ObjectsRoot/ObjectsPlaceholder>
            float3[] translation = [(-0.75, -0.75, -0.75), (0.75, 0.75, 0.75)]
            float3 gaussian_sampling_mean = (0, 0, 0)
            float3 gaussian_sampling_stddev = (0.2, 0.2, 0.2)
        }

        def FrameSampler "AdaptiveScale"
        {
            # Reference to the model diameter that the Occluderground prims (occluders) must match.
            # The diameter changes based on which object was activated by RandomActiveObject sampler which copies the selected child prim model diameter to the placeholder prim.
            rel ref_model_diameter = </Scene/ObjectsRoot/ObjectsPlaceholder>
        }
    }

    def Xform "AggressorBase" ()
    {   
        int shader_mask_value = 255
        float model_diameter = 0.02
        def FrameSampler "RandomUniformRotationEliza"
        {
            float3[] rotation = [(-90, -180,-180), (90, 180, 180)]
        }

        def FrameSampler "RandomRelativeGaussianTranslation"
        {
            rel ref_model_diameter = </Scene/ObjectsRoot/ObjectsPlaceholder>
            float3[] translation = [(-1, -1, -1), (1, 1, 1)]
            float3 gaussian_sampling_mean = (0, 0, 0)
            float3 gaussian_sampling_stddev = (0.75, 0.75, 0.75)
        }
    }
}

def Xform "CameraRig"
{
    def Xform "DCAML_s2wExtrinsics"{

        matrix4d xformOp:transform = ( (-0.947094606677514, -0.31931221706845736, 0.0324270571158056, 0), 
                                       (-0.15178226562261854, 0.3565762081665299, -0.9218544091189479, 0), 
                                       (0.2827966581217504, -0.8780051912149528, -0.3861773353716709, 0), 
                                       (0.04418828493520931, -0.04659549007547479, -0.029009893294978516, 1) )
        uniform token[] xformOpOrder = ["xformOp:transform"]

        def Xform "DCAML"
        {
            int image:width = 1280
            int image:height = 1280
            custom float camera:field_of_view = 160

            def Camera "DCAML_patch"
            {
                int image:height = 128
                int image:width = 128
                float2 clippingRange = (0.001, 1000)
                string[] image:output = ["color", "mask"]

                def FrameSampler "PointAtPrim"
                {
                    float3 up_vector = (0, -1, 0)
                    bool only_outplane_rotation = true
                    bool use_previous_frame = false
                    rel ref_target_prim = </Scene/ObjectsRoot/ObjectsPlaceholder>
                }
                def FrameSampler "FocalScaling"
                {
                    float patch_diameter_factor = 1.1
                    bool use_previous_frame = false
                    rel ref_model_diameter = </Scene/ObjectsRoot/ObjectsPlaceholder>
                    rel ref_focal_target = </Scene/ObjectsRoot/ObjectsPlaceholder>
                }

                # NOTE: We don't populate image processing here since it's already provided by the second camera
                # and utilized for this one too.

                def LabelRequest "DCAML_patch_LR"
                {
                    string[] request:names = ["PoseLocalToWorld", "PatchDistance", "CameraIntrinsics"] #, "PatchDiameter"]
                }
            }
        }
    }

    def Xform "DCAMR_s2wExtrinsics"{

        matrix4d xformOp:transform = ( (-0.947094606677514, 0.31931221706845736, -0.03242705711580563, 0), 
                                (0.1517822656226186, 0.35657620816653, -0.9218544091189479, -0), 
                                (-0.2827966581217504, -0.8780051912149528, -0.3861773353716708, 0), 
                                (-0.044187930818622635, -0.0465943906416703, -0.029009409725578958, 1) )
        uniform token[] xformOpOrder = ["xformOp:transform"]

        def Xform "DCAMR"
        {
            int image:width = 1280
            int image:height = 1280
            custom float camera:field_of_view = 160

            def Camera "DCAMR_patch"
            {
                int image:height = 128
                int image:width = 128
                float2 clippingRange = (0.001, 1000)
                string[] image:output = ["color", "mask"]

                def FrameSampler "PointAtPrim"
                {
                    float3 up_vector = (0, -1, 0)
                    bool only_outplane_rotation = true
                    bool use_previous_frame = false
                    rel ref_target_prim = </Scene/ObjectsRoot/ObjectsPlaceholder>
                }

                def FrameSampler "FocalScaling"
                {
                    float patch_diameter_factor = 1.1
                    bool use_previous_frame = false
                    rel ref_model_diameter = </Scene/ObjectsRoot/ObjectsPlaceholder>
                    rel ref_focal_target = </Scene/ObjectsRoot/ObjectsPlaceholder>
                }

                def FrameSampler "ImageProcessing"
                {
                   float[] bloom = [0.0, 0.0]
                   float[] camera_noise = [0.0, 0.5]
                   float[] tint_hue = [0.0, 1.0]
                   float[] tint_strength = [0.0, 0.5]
                   float[] exposure = [-4.0, 2.0]
                   float[] gaussian_range = [0.0, 3.0]
                   float[] motion_blur_strength = [0.0, 0.1]
                   float[] motion_blur_angle = [0.0, 180.0]
                }

                def LabelRequest "DCAMR_patch_LR"
                {
                    string[] request:names = ["PoseLocalToWorld", "PatchDistance", "CameraIntrinsics"] #, "PatchDiameter"]
                }
            }
            
            def FrameSampler "RandomPose"
            {
                float3[] translation = [(-0.04, -0.004, -0.005), (0.01, 0.005, 0.005)]
                float3[] rotation = [(-0.5, -1, -0.5), (0.5, 1, 0.5)]
            }
        }
    }
}

def Xform "Lights"{
    def DomeLight "Background"
    {
        def FrameSampler "RandomRotation"
        {
            float3[] rotation = [(-90, -180,-180), (90, 180, 180)]
        }

        def FrameSampler "RandomBackground"
        {
            asset folder = @LeonaroTexturesAssets/TextureImages@
            string[] filenames = ["*.hdr", "*.exr", "*.jpg"]
        }

        float inputs:intensity = 1  # This is the default RE value for IBL intensity.
        def FrameSampler "RandomAttr_1" {
            string attr_name = "inputs:intensity"
            float[] range = [0.5, 2]
        }

        float inputs:skybox_intensity = 0  # This is the default RE value for skybox intensity.
        def FrameSampler "RandomAttr_2" {
            string attr_name = "inputs:skybox_intensity"
            float[] range = [-5.0, 5.0]
        }

        def ClipSampler "RandomVisibility"
        {
            float probability = 0.9
        }
    }

    def Xform "Lowlights" {
        def FrameSampler "RandomSwitch"
        {
        }

        def ClipSampler "RandomVisibility"
        {
            float probability = 0.2
        }

        def DiskLight "SpotLight" (
        )
        {
            float inputs:angle = 0.53

            def FrameSampler "RandomAttr_1" {
                string attr_name = "inputs:intensity"
                float[] range = [10000, 100000000]
            }

            float inputs:shaping:cone:angle = 160
            float inputs:shaping:cone:softness = 0.1
        }

        def DistantLight "DirectionalLight" (
        )
        {
            def FrameSampler "RandomAttr_1" {
                string attr_name = "inputs:intensity"
                float[] range = [1000, 10000000]
            }

            float inputs:angle = 2.0 # angular size of directional light. 0.53 is the default for USD.
        }


        def FrameSampler "RandomTranslation"
        {
            float3[] translation = [(-2.0, -2.0, -2.0), (2.0, 2.0, 2.0)]
        }

        def FrameSampler "PointAtPrim"
        {
            float3 up_vector = (0, -1, 0)
            bool only_outplane_rotation = true
            bool use_previous_frame = false
            rel ref_target_prim = </Scene/ObjectsRoot/ObjectsPlaceholder>
        }
    }
}