BRDFSlices.DisplayForm.LoadBRDF C# (CSharp) Method

LoadBRDF() public static method

Loads a MERL BRDF file
public static LoadBRDF ( FileInfo _BRDFFile ) : BRDFSlices.Vector3[
_BRDFFile FileInfo
return BRDFSlices.Vector3[
        public static Vector3[,,] LoadBRDF( FileInfo _BRDFFile )
        {
            Vector3[,,]	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 Vector3[DimX,DimY,DimZ];

                        int	PitchThetaD = BRDF_SAMPLING_RES_PHI_D/2;
                        int	PitchThetaH = PitchThetaD*BRDF_SAMPLING_RES_THETA_D;

                        // Read content
                        int[]		NegativeValuesCount = new int[3] { 0, 0, 0 };
                        Vector3		MinValues = new Vector3() { x=double.MaxValue, y=double.MaxValue, z=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;

                            for ( int CoeffIndex=0; CoeffIndex < CoeffsCount; CoeffIndex++ )
                            {
                                int	Temp = CoeffIndex;
                                int	ThetaH = Temp / PitchThetaH;
                                Temp -= ThetaH * PitchThetaH;
                                int	ThetaD = Temp / PitchThetaD;
                                Temp -= ThetaD * PitchThetaD;
                                int	PhiD = Temp;

                                double	Value = Factor * Reader.ReadDouble();

            //								Result[ThetaH,ThetaD,PhiD] = V = new Vector3();
                                Result[ThetaH,ThetaD,PhiD][ComponentIndex] = 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;
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            try
            {
                // Analyze arguments
                if (args.Length != 1)
                {
                    throw new Exception("Usage: BRDFSlices \"Path to MERL BRDF\"");
                }

                FileInfo SourceBRDF = new FileInfo(args[0]);
                if (!SourceBRDF.Exists)
                {
                    throw new Exception("Source BRDF file \"" + SourceBRDF.FullName + "\" does not exist!");
                }

                // Load the BRDF
                Vector3[,,]     BRDF = DisplayForm.LoadBRDF(SourceBRDF);


                DisplayForm F = new DisplayForm(BRDF);
                Application.Run(F);
            }
            catch (Exception _e)
            {
                MessageBox.Show("An error occurred!\r\n\r\n" + _e.Message + "\r\n\r\n" + _e.StackTrace, "BRDF Fitting", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }