BRDFLafortuneFitting.Program.LoadBRDF C# (CSharp) Method

LoadBRDF() protected static method

Loads a MERL BRDF file
protected static LoadBRDF ( FileInfo _BRDFFile ) : double[][]
_BRDFFile System.IO.FileInfo
return double[][]
        protected static double[][] LoadBRDF( FileInfo _BRDFFile )
        {
            double[][]	Result = null;
            try
            {
                using ( FileStream S = _BRDFFile.OpenRead() )
                    using ( BinaryReader Reader = new BinaryReader( S ) )
                    {
                        // Check coefficients count is the expected value
                        int	DimX = Reader.ReadInt32();
                        int	DimY = Reader.ReadInt32();
                        int	DimZ = Reader.ReadInt32();
                        int	CoeffsCount = DimX*DimY*DimZ;
                        if ( CoeffsCount != BRDF_SAMPLING_RES_THETA_H*BRDF_SAMPLING_RES_THETA_D*BRDF_SAMPLING_RES_PHI_D/2 )
                            throw new Exception( "The amount of coefficients stored in the file is not the expected value (i.e. " + CoeffsCount + "! (is it a BRDF file?)" );

                        // Allocate the R,G,B arrays
                        Result = new double[3][];
                        Result[0] = new double[CoeffsCount];
                        Result[1] = new double[CoeffsCount];
                        Result[2] = new double[CoeffsCount];

                        // Read content
                        int[]		NegativeValuesCount = new int[3] { 0, 0, 0 };
                        double[]	MinValues = new double[3] { double.MaxValue, double.MaxValue, double.MaxValue };
                        for ( int ComponentIndex=0; ComponentIndex < 3; ComponentIndex++ )
                        {
                            double	Factor = 1.0;
                            if ( ComponentIndex == 0 )
                                Factor = BRDF_SCALE_RED;
                            else if ( ComponentIndex == 1 )
                                Factor = BRDF_SCALE_GREEN;
                            else
                                Factor = BRDF_SCALE_BLUE;

                            double[]	ComponentArray = Result[ComponentIndex];
                            for ( int CoeffIndex=0; CoeffIndex < CoeffsCount; CoeffIndex++ )
                            {
                                double	Value = Factor * Reader.ReadDouble();
                                ComponentArray[CoeffIndex] = Value;
                                if ( Value < 0.0 )
                                    NegativeValuesCount[ComponentIndex]++;
                                MinValues[ComponentIndex] = Math.Min( MinValues[ComponentIndex], Value );
                            }
                        }
                    }
            }
            catch ( Exception _e )
            {	// Forward...
                throw new Exception( "Failed to load source BRDF file: " + _e.Message );
            }

            return Result;
        }