using System;
using System.Collections.Generic;
using System.Text;

namespace CSGBuilder {
    class Box : Primitive {

        // A téglatest középpontja az origó, csak a méreteit kell megadni
        private double width, height, depth;

        #region Public properties
        public double Width {
            get { return width; }
            set { width = value; }
        }

        public double Height {
            get { return height; }
            set { height = value; }
        }

        public double Depth {
            get { return depth; }
            set { depth = value; }
        }
        #endregion

        public Box(double width, double height, double depth, Point3Dh ct) {
            this.width = width;
            this.height = height;
            this.depth = depth;
            if (ct == null) this.center = new Point3Dh(0.0, 0.0, 0.0, 1.0); else this.center = ct;
            this.e1 = new Point3Dh(center.X + width / 2, center.Y, center.Z, 1.0);
            this.e2 = new Point3Dh(center.X, center.Y + height / 2, center.Z, 1.0);
            this.e3 = new Point3Dh(center.X, center.Y, center.Z + depth / 2, 1.0);
            this.upperCorner = new Point3Dh(width / 2 + center.X, height / 2 + center.Y, depth / 2 + center.Z, 1.0);
            this.lowerCorner = new Point3Dh(-width / 2 + center.X, -height / 2 + center.Y, -depth / 2 + center.Z, 1.0);
            meshTesselation();   
        }

        // A kocka tesszellációja
        public void meshTesselation() {
            mesh = new BRep();

            #region Vertices
            mesh.Vertices.Add(new Point3Dh(lowerCorner.X, lowerCorner.Y, lowerCorner.Z, 1.0)); // 0.
            mesh.Vertices.Add(new Point3Dh(upperCorner.X, lowerCorner.Y, lowerCorner.Z, 1.0)); // 1.
            mesh.Vertices.Add(new Point3Dh(upperCorner.X, upperCorner.Y, lowerCorner.Z, 1.0)); // 2.
            mesh.Vertices.Add(new Point3Dh(lowerCorner.X, upperCorner.Y, lowerCorner.Z, 1.0)); // 3.
            mesh.Vertices.Add(new Point3Dh(     center.X,      center.Y, lowerCorner.Z, 1.0)); // 4.
            mesh.Vertices.Add(new Point3Dh(upperCorner.X, lowerCorner.Y, upperCorner.Z, 1.0)); // 5.
            mesh.Vertices.Add(new Point3Dh(upperCorner.X, upperCorner.Y, upperCorner.Z, 1.0)); // 6.
            mesh.Vertices.Add(new Point3Dh(upperCorner.X,      center.Y,      center.Z, 1.0)); // 7.
            mesh.Vertices.Add(new Point3Dh(lowerCorner.X, lowerCorner.Y, upperCorner.Z, 1.0)); // 8.
            mesh.Vertices.Add(new Point3Dh(lowerCorner.X, upperCorner.Y, upperCorner.Z, 1.0)); // 9.
            mesh.Vertices.Add(new Point3Dh(     center.X,      center.Y, upperCorner.Z, 1.0)); // 10.
            mesh.Vertices.Add(new Point3Dh(lowerCorner.X,      center.Y,      center.Z, 1.0)); // 11.
            mesh.Vertices.Add(new Point3Dh(     center.X, upperCorner.Y,      center.Z, 1.0)); // 12.
            mesh.Vertices.Add(new Point3Dh(     center.X, lowerCorner.Y,      center.Z, 1.0)); // 13.
            #endregion

            #region Edges
            mesh.Edges.Add(new Edge(0, 1));
            mesh.Edges.Add(new Edge(1, 2));
            mesh.Edges.Add(new Edge(2, 3));
            mesh.Edges.Add(new Edge(3, 0));
            mesh.Edges.Add(new Edge(0, 4));
            mesh.Edges.Add(new Edge(1, 4));
            mesh.Edges.Add(new Edge(2, 4));
            mesh.Edges.Add(new Edge(3, 4));

            mesh.Edges.Add(new Edge(1, 5));
            mesh.Edges.Add(new Edge(5, 6));
            mesh.Edges.Add(new Edge(6, 2));
            mesh.Edges.Add(new Edge(2, 1));
            mesh.Edges.Add(new Edge(1, 7));
            mesh.Edges.Add(new Edge(5, 7));
            mesh.Edges.Add(new Edge(6, 7));
            mesh.Edges.Add(new Edge(2, 7));

            mesh.Edges.Add(new Edge(5, 8));
            mesh.Edges.Add(new Edge(8, 9));
            mesh.Edges.Add(new Edge(9, 6));
            mesh.Edges.Add(new Edge(6, 5));
            mesh.Edges.Add(new Edge(5, 10));
            mesh.Edges.Add(new Edge(8, 10));
            mesh.Edges.Add(new Edge(9, 10));
            mesh.Edges.Add(new Edge(6, 10));

            mesh.Edges.Add(new Edge(8, 0));
            mesh.Edges.Add(new Edge(0, 3));
            mesh.Edges.Add(new Edge(3, 9));
            mesh.Edges.Add(new Edge(9, 8));
            mesh.Edges.Add(new Edge(8, 11));
            mesh.Edges.Add(new Edge(0, 11));
            mesh.Edges.Add(new Edge(3, 11));
            mesh.Edges.Add(new Edge(9, 11));

            mesh.Edges.Add(new Edge(3, 2));
            mesh.Edges.Add(new Edge(2, 6));
            mesh.Edges.Add(new Edge(6, 9));
            mesh.Edges.Add(new Edge(9, 3));
            mesh.Edges.Add(new Edge(3, 12));
            mesh.Edges.Add(new Edge(2, 12));
            mesh.Edges.Add(new Edge(6, 12));
            mesh.Edges.Add(new Edge(9, 12));

            mesh.Edges.Add(new Edge(8, 5));
            mesh.Edges.Add(new Edge(5, 1));
            mesh.Edges.Add(new Edge(1, 0));
            mesh.Edges.Add(new Edge(0, 8));
            mesh.Edges.Add(new Edge(8, 13));
            mesh.Edges.Add(new Edge(5, 13));
            mesh.Edges.Add(new Edge(1, 13));
            mesh.Edges.Add(new Edge(0, 13));
            #endregion

            #region Faces
            // 1. lap
            mesh.Faces.Add(new Face(0, 1, 4));
            mesh.Faces.Add(new Face(1, 2, 4));
            mesh.Faces.Add(new Face(2, 3, 4));
            mesh.Faces.Add(new Face(3, 0, 4));

            // 2. lap
            mesh.Faces.Add(new Face(1, 5, 7));
            mesh.Faces.Add(new Face(5, 6, 7));
            mesh.Faces.Add(new Face(6, 2, 7));
            mesh.Faces.Add(new Face(2, 1, 7));

            // 3. lap
            mesh.Faces.Add(new Face(5, 8, 10));
            mesh.Faces.Add(new Face(8, 9, 10));
            mesh.Faces.Add(new Face(9, 6, 10));
            mesh.Faces.Add(new Face(6, 5, 10));

            // 4. lap
            mesh.Faces.Add(new Face(8, 0, 11));
            mesh.Faces.Add(new Face(0, 3, 11));
            mesh.Faces.Add(new Face(3, 9, 11));
            mesh.Faces.Add(new Face(9, 8, 11));

            // 5. lap
            mesh.Faces.Add(new Face(3, 2, 12));
            mesh.Faces.Add(new Face(2, 6, 12));
            mesh.Faces.Add(new Face(6, 9, 12));
            mesh.Faces.Add(new Face(9, 3, 12));

            // 6. lap
            mesh.Faces.Add(new Face(8, 5, 13));
            mesh.Faces.Add(new Face(5, 1, 13));
            mesh.Faces.Add(new Face(1, 0, 13));
            mesh.Faces.Add(new Face(0, 8, 13));
            #endregion

            // a lapok további felosztása
            //ProcessLongFaces();
        }

    }
}
