Example #1
0
        protected float computeBevel(TerrainModifierData data, int x, int y)
        {
            int   deltaX;
            int   deltaY;
            int   xMax;
            int   yMax;
            float factor;

            if (data.bevel == "taper")
            {
                if (data.shape == "ellipse")
                {
                    deltaX  = x - data.x0;
                    deltaY  = y - data.y0;
                    xMax    = data.dx;
                    yMax    = data.dy;
                    factor  = ((deltaX * deltaX) + (deltaY * deltaY));
                    factor /= ((xMax * xMax) + (yMax * yMax));
                }
                else
                {
                    // pyramid
                    xMax   = data.dx / 2 + data.dx % 2;
                    yMax   = data.dy / 2 + data.dy % 2;
                    deltaX = Math.Abs(data.x0 + xMax - x);
                    deltaY = Math.Abs(data.y0 + yMax - y);
                    factor = Math.Max(((float)(deltaY) / yMax), ((float)(deltaX) / xMax));
                }
            }
            else
            {
                factor = 0.0f;
            }
            return(factor);
        }
Example #2
0
        protected void applyModification(ITerrainChannel map, TerrainModifierData data)
        {
            bool[,] mask;
            int xMax;
            int yMax;
            int xMid;
            int yMid;

            if (data.shape == "ellipse")
            {
                mask = this.ellipticalMask(data.dx, data.dy);
                xMax = mask.GetLength(0);
                yMax = mask.GetLength(1);
                xMid = xMax / 2 + xMax % 2;
                yMid = yMax / 2 + yMax % 2;
            }
            else
            {
                mask = this.rectangularMask(data.dx, data.dy);
                xMax = mask.GetLength(0);
                yMax = mask.GetLength(1);
                xMid = 0;
                yMid = 0;
            }
//            m_log.DebugFormat("Apply {0} mask {1}x{2} @ {3},{4}", data.shape, xMax, yMax, xMid, yMid);

            float[,] buffer = new float[map.Width, map.Height];
            for (int x = data.x0; x < xMax; ++x)
            {
                for (int y = data.y0; y < yMax; ++y)
                {
                    buffer[x, y] = map[x, y];
                }
            }

            int yDim = yMax;

            while (--yDim >= 0)
            {
                int yPos = data.y0 + yDim - yMid;
                if ((yPos >= 0) && (yPos < map.Height))
                {
                    int xDim = xMax;
                    while (--xDim >= 0)
                    {
                        int xPos = data.x0 + xDim - xMid;

                        if ((xPos >= 0) && (xPos < map.Width) && (mask[xDim, yDim]))
                        {
                            double endElevation = this.operate(buffer, data, xPos, yPos);
                            map[xPos, yPos] = (float)endElevation;
                        }
                    }
                }
            }
        }
Example #3
0
 protected String parseParameters(string[] args, out TerrainModifierData data)
 {
     string val;
     string arg;
     string result;
     data = new TerrainModifierData();
     data.shape = String.Empty;
     data.bevel = String.Empty;
     data.dx = 0;
     data.dy = 0;
     if (args.Length < 4)
     {
         result = "Usage: " + GetUsage();
     }
     else
     {
         result = this.parseFloat(args[3], out data.elevation);
     }
     if (result == String.Empty)
     {
         int index = 3;
         while(++index < args.Length && result == String.Empty)
         {
             arg = args[index];
             // check for shape
             if (arg.StartsWith("-rec=") || arg.StartsWith("-ell="))
             {
                 if (data.shape != String.Empty)
                 {
                     result = "Only 1 '-rec' or '-ell' parameter is permitted.";
                 }
                 else
                 {
                     data.shape = arg.StartsWith("-ell=") ? "ellipse" : "rectangle";
                     val = arg.Substring(arg.IndexOf("=") + 1);
                     string[] coords = val.Split(new char[] {','});
                     if ((coords.Length < 3) || (coords.Length > 4))
                     {
                         result = String.Format("Bad format for shape parameter {0}", arg);
                     }
                     else
                     {
                         result = this.parseInt(coords[0], out data.x0);
                         if (result == String.Empty)
                         {
                             result = this.parseInt(coords[1], out data.y0);
                         }
                         if (result == String.Empty)
                         {
                             result = this.parseInt(coords[2], out data.dx);
                         }
                         if (result == String.Empty)
                         {
                             if (coords.Length == 4)
                             {
                                 result = this.parseInt(coords[3], out data.dy);
                             }
                             else
                             {
                                 data.dy = data.dx;
                             }
                         }
                         if (result == String.Empty)
                         {
                             if ((data.dx <= 0) || (data.dy <= 0))
                             {
                                 result = "Shape sizes must be positive integers";
                             }
                         }
                         else
                         {
                             result = String.Format("Bad value in shape parameters {0}", arg);
                         }
                     }
                 }
             }
             else if (arg.StartsWith("-taper="))
             {
                 if (data.bevel != String.Empty)
                 {
                     result = "Only 1 '-taper' parameter is permitted.";
                 }
                 else
                 {
                     data.bevel = "taper";
                     val = arg.Substring(arg.IndexOf("=") + 1);
                     result = this.parseFloat(val, out data.bevelevation);
                     if (result != String.Empty)
                     {
                         result = String.Format("Bad format for taper parameter {0}", arg);
                     }
                 }
             }
             else
             {
                 result = String.Format("Unrecognized parameter {0}", arg);
             }
         }
     }
     return result;
 }
Example #4
0
 public abstract double operate(double[,] map, TerrainModifierData data, int x, int y);
Example #5
0
 protected double computeBevel(TerrainModifierData data, int x, int y)
 {
     int deltaX;
     int deltaY;
     int xMax;
     int yMax;
     double factor;
     if (data.bevel == "taper")
     {
         if (data.shape == "ellipse")
         {
             deltaX = x - data.x0;
             deltaY = y - data.y0;
             xMax = data.dx;
             yMax = data.dy;
             factor = (double)((deltaX * deltaX) + (deltaY * deltaY));
             factor /= ((xMax * xMax) + (yMax * yMax));
         }
         else
         {
             // pyramid
             xMax = data.dx / 2 + data.dx % 2;
             yMax = data.dy / 2 + data.dy % 2;
             deltaX = Math.Abs(data.x0 + xMax - x);
             deltaY = Math.Abs(data.y0 + yMax - y);
             factor = Math.Max(((double)(deltaY) / yMax), ((double)(deltaX) / xMax));
         }
     }
     else
     {
         factor = 0.0;
     }
     return factor;
 }
Example #6
0
        protected void applyModification(ITerrainChannel map, TerrainModifierData data)
        {
            bool[,] mask;
            int xMax;
            int yMax;
            int xMid;
            int yMid;
            if (data.shape == "ellipse")
            {
                mask = this.ellipticalMask(data.dx, data.dy);
                xMax = mask.GetLength(0);
                yMax = mask.GetLength(1);
                xMid = xMax / 2 + xMax % 2;
                yMid = yMax / 2 + yMax % 2;
            }
            else
            {
                mask = this.rectangularMask(data.dx, data.dy);
                xMax = mask.GetLength(0);
                yMax = mask.GetLength(1);
                xMid = 0;
                yMid = 0;
            }
//            m_log.DebugFormat("Apply {0} mask {1}x{2} @ {3},{4}", data.shape, xMax, yMax, xMid, yMid);
            double[,] buffer = map.GetDoubles();
            int yDim = yMax;
            while(--yDim >= 0)
            {
                int yPos = data.y0 + yDim - yMid;
                if ((yPos >= 0) && (yPos < map.Height))
                {
                    int xDim = xMax;
                    while(--xDim >= 0)
                    {
                        int xPos = data.x0 + xDim - xMid;
                        if ((xPos >= 0) && (xPos < map.Width) && (mask[xDim, yDim]))
                        {
                            double endElevation = this.operate(buffer, data, xPos, yPos);
                            map[xPos, yPos] = endElevation;
                        }
                    }
                }
            }
        }
Example #7
0
 public override double operate(double[,] map, TerrainModifierData data, int x, int y)
 {
     double factor = this.computeBevel(data, x, y);
     double result = data.elevation - (data.elevation - data.bevelevation) * factor;
     return result;
 }
Example #8
0
        protected String parseParameters(string[] args, out TerrainModifierData data)
        {
            string val;
            string arg;
            string result;

            data       = new TerrainModifierData();
            data.shape = String.Empty;
            data.bevel = String.Empty;
            data.dx    = 0;
            data.dy    = 0;
            if (args.Length < 4)
            {
                result = "Usage: " + GetUsage();
            }
            else
            {
                result = this.parseFloat(args[3], out data.elevation);
            }
            if (result == String.Empty)
            {
                int index = 3;
                while (++index < args.Length && result == String.Empty)
                {
                    arg = args[index];
                    // check for shape
                    if (arg.StartsWith("-rec=") || arg.StartsWith("-ell="))
                    {
                        if (data.shape != String.Empty)
                        {
                            result = "Only 1 '-rec' or '-ell' parameter is permitted.";
                        }
                        else
                        {
                            data.shape = arg.StartsWith("-ell=") ? "ellipse" : "rectangle";
                            val        = arg.Substring(arg.IndexOf("=") + 1);
                            string[] coords = val.Split(new char[] { ',' });
                            if ((coords.Length < 3) || (coords.Length > 4))
                            {
                                result = String.Format("Bad format for shape parameter {0}", arg);
                            }
                            else
                            {
                                result = this.parseInt(coords[0], out data.x0);
                                if (result == String.Empty)
                                {
                                    result = this.parseInt(coords[1], out data.y0);
                                }
                                if (result == String.Empty)
                                {
                                    result = this.parseInt(coords[2], out data.dx);
                                }
                                if (result == String.Empty)
                                {
                                    if (coords.Length == 4)
                                    {
                                        result = this.parseInt(coords[3], out data.dy);
                                    }
                                    else
                                    {
                                        data.dy = data.dx;
                                    }
                                }
                                if (result == String.Empty)
                                {
                                    if ((data.dx <= 0) || (data.dy <= 0))
                                    {
                                        result = "Shape sizes must be positive integers";
                                    }
                                }
                                else
                                {
                                    result = String.Format("Bad value in shape parameters {0}", arg);
                                }
                            }
                        }
                    }
                    else if (arg.StartsWith("-taper="))
                    {
                        if (data.bevel != String.Empty)
                        {
                            result = "Only 1 '-taper' parameter is permitted.";
                        }
                        else
                        {
                            data.bevel = "taper";
                            val        = arg.Substring(arg.IndexOf("=") + 1);
                            result     = this.parseFloat(val, out data.bevelevation);
                            if (result != String.Empty)
                            {
                                result = String.Format("Bad format for taper parameter {0}", arg);
                            }
                        }
                    }
                    else
                    {
                        result = String.Format("Unrecognized parameter {0}", arg);
                    }
                }
            }
            return(result);
        }
Example #9
0
 public abstract float operate(float[,] map, TerrainModifierData data, int x, int y);
Example #10
0
 public override double operate(double[,] map, TerrainModifierData data, int x, int y)
 {
     double factor = this.computeBevel(data, x, y);
     double noise = TerrainUtil.PerlinNoise2D((double)x / map.GetLength(0), (double)y / map.GetLength(1), 8, 1.0);
     return map[x, y] + (data.elevation - (data.elevation - data.bevelevation) * factor) * (noise - .5);
 }
Example #11
0
 public override double operate(double[,] map, TerrainModifierData data, int x, int y)
 {
     double[] scale = new double[3];
     scale[0] = data.elevation;
     scale[1] = ((1.0 - scale[0]) * data.bevelevation) / 8.0;
     scale[2] = ((1.0 - scale[0]) * (1.0 - data.bevelevation)) / 16.0;
     int xMax = map.GetLength(0);
     int yMax = map.GetLength(1);
     double result;
     if ((x == 0) || (y == 0) || (x == (xMax - 1)) || (y == (yMax - 1)))
     {
         result = map[x, y];
     }
     else
     {
         result = 0.0;
         for(int yPos = (y - 2); yPos < (y + 3); yPos++)
         {
             int yVal = (yPos <= 0) ? 0 : ((yPos < yMax) ? yPos : yMax - 1);
             for(int xPos = (x - 2); xPos < (x + 3); xPos++)
             {
                 int xVal = (xPos <= 0) ? 0 : ((xPos < xMax) ? xPos : xMax - 1);
                 int dist = Math.Max(Math.Abs(x - xVal), Math.Abs(y - yVal));
                 result += map[xVal, yVal] * scale[dist];
             }
         }
     }
     return result;
 }
 public abstract double operate(double[,] map, TerrainModifierData data, int x, int y);