// Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto.  Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#include "UsdPCH.h"

#include <OgnTestGatherRandomRotationsDatabase.h>

namespace omni
{
namespace graph
{

namespace test
{

class OgnTestGatherRandomRotations
{
public:
    
    static bool compute(OgnTestGatherRandomRotationsDatabase& db)
    {
        
        static const NameToken orientationNameToken = db.stringToToken("_worldOrientation");
        static const NameToken translateNameToken = db.stringToToken("_worldPosition");

        // bucketIds is coming from code gen and is set to be a uint64_t. 
        BucketId bucketId{ db.inputs.bucketIds() };
        if (bucketId == carb::flatcache::kInvalidBucketId)
            return false;

        size_t count = 0;

        pxr::GfVec4f* orientationArray;
        orientationArray = (pxr::GfVec4f *) db.abi_context().iContext->getBucketArray(db.abi_context(), bucketId, orientationNameToken, count);
        if (!orientationArray)        
            return false;
                
        pxr::GfVec3d* translationArray;
        translationArray  = (pxr::GfVec3d *) db.abi_context().iContext->getBucketArray(db.abi_context(), bucketId, translateNameToken, count);
        if (!translationArray)
            return false;
        

        float xx = 0, yy = 0, zz = 0;
        const float spacing = 0.7f;

        for (uint32_t i = 0; i != count; i++)
        {
            xx += spacing;
            if (xx > spacing * 30)
            {
                xx = 0;
                yy += spacing;
                if (yy > spacing * 30)
                {
                    yy = 0;
                    zz += spacing;
                }
            }

            translationArray[i][0] = xx;
            translationArray[i][1] = yy;
            translationArray[i][2] = zz;
                        
            float r0 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
            float r1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
            float r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
            float r3 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
            float len = sqrt(r0*r0 + r1 * r1 + r2 * r2 + r3 * r3);
            r0 /= len;
            r1 /= len;
            r2 /= len;
            r3 /= len;

            orientationArray[i][0] = r0;
            orientationArray[i][1] = r1;
            orientationArray[i][2] = r2;
            orientationArray[i][3] = r3;

        }
        return true;
        
    }
    
};

REGISTER_OGN_NODE()

}
}
}
