/**
 * SphericalLib.BinReader
 * 
 * SphericalLib is a .NET library to deal with geometry on the unit sphere.
 * It is intented to be used for complex survey footprint descriptions in
 * standalone applications as well as in web services and perhaps Yukon.
 * 
 * Written by Tamas Budavari (budavari@pha.jhu.edu)
 * See bottom of file for revision history
 * 
 * Current revision:
 *   ID:          $Id: BinReader.cs,v 1.1.1.1 2006/11/14 02:56:39 gupchup Exp $
 *   Revision:    $Revision: 1.1.1.1 $
 *   Date:        $Date: 2006/11/14 02:56:39 $
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Spherical;

namespace Spherical.IO
{
    /// <summary>
    /// [DEPRICATED] This class implements methods to read objects of SphericalLib
    /// from binary files, see also BinWriter.
    /// </summary>
    [CLSCompliant(true)]
    public class BinReader
    {
        private BinaryReader reader;

        /// <summary>
        /// Create reader from filename
        /// </summary>
        /// <param name="path"></param>
        public BinReader(string path)
        {
            FileStream input = new FileStream(path, FileMode.Open);
            reader = new BinaryReader(input);
        }

        /// <summary>
        /// Create reader from stream
        /// </summary>
        /// <param name="input"></param>
        public BinReader(Stream input)
        {
            reader = new BinaryReader(input);
        }

        /// <summary>
        /// Close reader
        /// </summary>
        public void Close()
        {
            reader.Close();
        }

        /// <summary>
        /// Read a point
        /// </summary>
        /// <returns></returns>
        public Cartesian ReadCartesian()
        {
            double x = reader.ReadDouble();
            double y = reader.ReadDouble();
            double z = reader.ReadDouble();
            Cartesian c = new Cartesian(x, y, z, false);
            return c;
        }

        /// <summary>
        /// Read a halfspace (cartesian and cos)
        /// </summary>
        /// <returns></returns>
        public Halfspace ReadHalfspace()
        {
            Cartesian c = this.ReadCartesian();
            double cos0 = reader.ReadDouble();
            Halfspace h = new Halfspace(c,cos0);
            return h;
        }

        /// <summary>
        /// Read an arc
        /// </summary>
        /// <returns></returns>
        public Arc ReadArc()
        {
            Cartesian p1 = this.ReadCartesian();
            Cartesian p2 = this.ReadCartesian();
            Halfspace c = this.ReadHalfspace();
            Arc a = new Arc(c, p1, p2);
            return a;
        }

        /// <summary>
        /// Read a patch
        /// </summary>
        /// <returns></returns>
        public Patch ReadPatch()
        {
            byte key = reader.ReadByte();
            if (key != (byte)'P')
                throw new FileLoadException
                    (String.Format("BinReader.ReadPatch(): not a patch, code '{0}'", key));
            int num = reader.ReadInt32();
            Arc[] arclist = new Arc[num];
            for (int i = 0; i < num; i++)
            {
                arclist[i] = this.ReadArc();
            }
            Patch p = new Patch(arclist);
            return p;
        }

        /// <summary>
        /// Read a convex
        /// </summary>
        /// <returns></returns>
        public Convex ReadConvex()
        {
            byte key = reader.ReadByte();
            if (key != (byte)'C') 
                throw new FileLoadException
                    (String.Format("BinReader.ReadConvex(): not a convex, code '{0}'", key));
            int num = reader.ReadInt32();
            Convex c = new Convex(num);
            for (int i = 0; i < num; i++)
                c.Add(this.ReadHalfspace());
            // check if simplified
            byte peek = reader.ReadByte();
            if (peek == 'S')
            {
                double area = reader.ReadDouble(); 
                c.SetArea(area);
                byte key2 = reader.ReadByte();
                if (key2 != (byte)'L')
                    throw new FileLoadException
                        (String.Format("BinReader.ReadConvex(): not a list of patches, code '{0}'", key2));
                int npatch = reader.ReadInt32();
                List<Patch> patches = new List<Patch>(npatch);
                for (int ip = 0; ip < npatch; ip++)
                {
                    Patch p = this.ReadPatch();
                    patches.Add(p);
                }
                c.SetPatches(patches);
            }
            else if (peek != 'N')
            {
                throw new FileLoadException
                    (String.Format("BinReader.ReadConvex(): not a S or N for simplified, code '{0}'", peek));
            }
            return c;
        }

        /// <summary>
        /// Read a region
        /// </summary>
        /// <returns></returns>
        public Region ReadRegion()
        {
            byte key = reader.ReadByte();
            if (key != (byte)'R') 
                throw new FileLoadException
                    ("BinReader.ReadRegion(): not a region");
            int num = reader.ReadInt32();
            Region r = new Region(num);
            for (int i = 0; i < num; i++)
                r.Add(this.ReadConvex());
            return r;
        }

        /// <summary>
        /// Revision from CVS
        /// </summary>
        public static string Revision
        {
            get { return "$Revision: 1.1.1.1 $"; }
        }

    }
}

#region Revision History
/* Revision History

        $Log: BinReader.cs,v $
        Revision 1.1.1.1  2006/11/14 02:56:39  gupchup
        no message

        Revision 1.4  2006/05/23 13:21:10  budavari
        New binary IO via BinaryFormatter

        Revision 1.3  2006/04/19 16:52:13  budavari
        EVerything is now CLS compliant

        Revision 1.2  2006/03/21 22:06:53  budavari
        Binary IO upgraded to full

        Revision 1.1  2006/02/28 16:36:59  budavari
        Module has IO related classes

        Revision 1.1  2006/01/24 15:56:05  budavari
        Re-structured directory layout to accommodate more projects

        Revision 1.1  2005/12/14 22:35:33  budavari
        Binary IO classes

*/
#endregion