Example #1
0
        public static void TerrainTriangulation(Model model)
        {
            MainWindow.SetBackgroundStyleAndColor(model);

            int sideCount = 100;

            int len = sideCount * sideCount;

            Point3D[] pts = new Point3D[len];

            Random rand = new Random(3);

            for (int j = 0; j < sideCount; j++)
            {
                for (int i = 0; i < sideCount; i++)
                {
                    double x = rand.NextDouble() * sideCount;
                    double y = rand.NextDouble() * sideCount;
                    double z = 0;

                    double _x = x / 2 - 15;
                    double _y = y / 2 - 15;

                    double den = Math.Sqrt(_x * _x + _y * _y);

                    if (den != 0)
                    {
                        z = 10 * Math.Sin(Math.Sqrt(_x * _x + _y * _y)) / den;
                    }

                    int R = (int)(255 * (z + 2) / 12);
                    int B = (int)(2.55 * y);

                    Utility.LimitRange <int>(0, ref R, 255);
                    Utility.LimitRange <int>(0, ref B, 255);

                    PointRGB pt = new PointRGB(x, y, z, (byte)R, 255, (byte)B);

                    pts[i + j * sideCount] = pt;
                }
            }

            Mesh m = UtilityEx.Triangulate(pts);

            model.Entities.Add(m);

            Plane pln = new Plane(new Point3D(0, 20, 20), new Vector3D(20, -30, 10));

            PlanarEntity pe = new PlanarEntity(pln, 25);

            model.Entities.Add(pe, Color.Magenta);

            ICurve[] curves = m.Section(pln, 0);

            foreach (Entity ent in curves)
            {
                model.Entities.Add(ent);
            }
        }
Example #2
0
        private void CreateGround()
        {
            const int rows = 5;
            const int cols = 5;

            PointRGB[] vertices = new PointRGB[rows * cols];

            double surfaceOffset = -3;

            Random random           = new Random();
            double maxHeightSurface = 3;
            double minHeightSurface = -3;

            int indexArray = 0;

            for (int j = 0; j < rows; j++)
            {
                for (int i = 0; i < cols; i++)
                {
                    // values 87.5 and 50 for dividing in 4 parts the grid(350x200)
                    double x = i * 87.5 - 150;
                    double y = j * 50 - 100;

                    double z = random.NextDouble() * (maxHeightSurface - minHeightSurface) + minHeightSurface;

                    // sets saddlebrown color
                    int red   = 139;
                    int green = 69;
                    int blue  = 19;

                    if (x == -62.5 && y == 0 || x == 25 && y == 0)
                    {
                        z = -surfaceOffset;
                    }

                    if ((i % 2 == 0) && (j % 2 == 0))
                    {
                        // sets greenforest color
                        red   = 34;
                        green = 139;
                        blue  = 34;
                    }

                    vertices[indexArray++] = new PointRGB(x, y, z, (byte)red, (byte)green, (byte)blue);
                }
            }

            IndexTriangle[] triangles = new IndexTriangle[((rows - 1) * (cols - 1) * 2)];

            indexArray = 0;
            for (int j = 0; j < (rows - 1); j++)
            {
                for (int i = 0; i < (cols - 1); i++)
                {
                    triangles[indexArray++] = (new IndexTriangle(i + j * cols, i + j * cols + 1, i + (j + 1) * cols + 1));
                    triangles[indexArray++] = (new IndexTriangle(i + j * cols, i + (j + 1) * cols + 1, i + (j + 1) * cols));
                }
            }

            Mesh surface = new Mesh();

            surface.NormalAveragingMode = Mesh.normalAveragingType.Averaged;

            surface.Vertices  = vertices;
            surface.Triangles = triangles;

            // sets surface lower than the grid
            surface.Translate(0, 0, surfaceOffset);

            model1.Entities.Add(surface);

            // fits the model in the model1
            model1.ZoomFit();
        }
Example #3
0
        /// <summary>
        /// Creates a regular grid of 3dpoints from the Surface on the specified uv-interval and colors them by gaussian
        /// </summary>
        /// <param name="s">the surface to mesh</param>
        /// <param name="ROWS">the number of constant-u sections</param>
        /// <param name="COLS">the number of constant-v sections</param>
        /// <param name="uvLim">(optional)the uv limits to mesh, uvLim[0,x] = uLim, uvLim[1,x] = vLim</param>
        /// <returns>the grid of points for meshing</returns>
        public static PointRGB[] GetMeshGaussianPoints(ISurface s, int ROWS, int COLS, double[,] uvLim)
        {
            Vect2 uv = new Vect2();
            Vect3 xyz = new Vect3();
            List<Vect3> xyzs = new List<Vect3>(ROWS * COLS);
            PointRGB[] meshpts = new PointRGB[ROWS * COLS];
            double[] gauss = new double[ROWS * COLS];
            double kMax = -1e9, kMin = 1e9;
            int i = 0;
            for (int iU = 0; iU < ROWS; iU++)
            {
                uv[0] = BLAS.interpolate((double)iU / (double)(ROWS - 1), uvLim[0, 1], uvLim[0, 0]);
                //uv[0] = (double)iU / (double)(ROWS - 1);
                for (int iV = 0; iV < COLS; iV++, i++)
                {
                    uv[1] = BLAS.interpolate((double)iV / (double)(COLS - 1), uvLim[1, 1], uvLim[1, 0]);
                    //uv[1] = (double)iV / (double)(COLS - 1);
                    s.xRad(uv, ref xyz, ref gauss[i]);
                    //copy to temp array
                    xyzs.Add(new Vect3(xyz));
                    //meshpts[i].X = xyz[0];
                    //meshpts[i].Y = xyz[1];
                    //meshpts[i].Z = xyz[2];
                    //track max/min for color scale
                    kMax = Math.Max(kMax, gauss[i]);
                    kMin = Math.Min(kMin, gauss[i]);
                }
            }
            double ave, q1, q3, stddev = BLAS.StandardDeviation(gauss, out ave, out q3, out q1);
            Color c;
            for (i = 0; i < meshpts.Length; i++)
            {
                c = ColorMath.GetScaleColor(ave + 2 * stddev, ave - 2 * stddev, gauss[i]);
                meshpts[i] = new PointRGB(xyzs[i][0], xyzs[i][1], xyzs[i][2], c);
            }

            return meshpts;
        }