Click here to Skip to main content
15,895,779 members
Articles / Artificial Intelligence

AForge.NET open source framework

Rate me:
Please Sign up or sign in to vote.
4.97/5 (150 votes)
16 May 2007GPL311 min read 831.5K   48.3K   346  
The article describes an open source C# framework for researchers in the areas of Computer Vision and Artificial Intelligence - image processing, neural networks, genetic algorithms, etc.
// AForge Image Processing Library
//
// Copyright � Andrew Kirillov, 2005-2007
// andrew.kirillov@gmail.com
//

namespace AForge.Imaging.Textures
{
	using System;

	/// <summary>
	/// Marble texture
	/// </summary>
    /// 
    /// <remarks></remarks>
    /// 
	public class MarbleTexture : ITextureGenerator
	{
        // Perlin noise function used for texture generation
        private AForge.Math.PerlinNoise noise = new AForge.Math.PerlinNoise( 1.0 / 32, 1.0, 0.65, 2 );

        // randmom number generator
        private Random rand = new Random( (int) DateTime.Now.Ticks );
		private int		r;

		private double	xPeriod = 5.0;
		private double	yPeriod = 10.0;

        /// <summary>
        /// XPeriod value
        /// </summary>
        /// 
        /// <remarks>Default value is 5. Minimum value is 2.</remarks>
        /// 
		public double XPeriod
		{
			get { return xPeriod; }
			set { xPeriod = Math.Max( 2.0, value); }
		}

        /// <summary>
        /// YPeriod value
        /// </summary>
        /// 
        /// <remarks>Default value is 10. Minimum value is 2.</remarks>
        /// 
        public double YPeriod
		{
			get { return yPeriod; }
			set { yPeriod = Math.Max( 2.0, value ); }
		}

        /// <summary>
        /// Initializes a new instance of the <see cref="MarbleTexture"/> class
        /// </summary>
        /// 
		public MarbleTexture( )
        {
            Reset( );
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MarbleTexture"/> class
        /// </summary>
        /// 
        /// <param name="xPeriod">XPeriod value</param>
        /// <param name="yPeriod">YPeriod value</param>
        /// 
		public MarbleTexture( double xPeriod, double yPeriod )
		{
			this.xPeriod = xPeriod;
			this.yPeriod = yPeriod;
			Reset( );
		}

        /// <summary>
        /// Generate texture
        /// </summary>
        /// 
        /// <param name="width">Texture's width</param>
        /// <param name="height">Texture's height</param>
        /// 
        /// <returns>Two dimensional array of intensities</returns>
        /// 
        /// <remarks>Generates new texture with specified dimension.</remarks>
        /// 
        public float[,] Generate( int width, int height )
		{
			float[,]	texture = new float[height, width];
			double		xFact = xPeriod / width;
			double		yFact = yPeriod / height;

			for ( int y = 0; y < height; y++ )
			{
				for ( int x = 0; x < width; x++ )
				{
					texture[y, x] = 
						Math.Min( 1.0f, (float)
							Math.Abs( Math.Sin( 
								( x * xFact + y * yFact + noise.Function2D( x + r, y + r ) ) * Math.PI
							) )
						);

				}
			}
			return texture;
		}
        
        /// <summary>
        /// Reset generator
        /// </summary>
        /// 
        /// <remarks>Regenerates internal random numbers.</remarks>
        /// 
        public void Reset( )
		{
			r = rand.Next( 5000 );
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer IBM
United Kingdom United Kingdom
Started software development at about 15 years old and it seems like now it lasts most part of my life. Fortunately did not spend too much time with Z80 and BK0010 and switched to 8086 and further. Similar with programming languages – luckily managed to get away from BASIC and Pascal to things like Assembler, C, C++ and then C#. Apart from daily programming for food, do it also for hobby, where mostly enjoy areas like Computer Vision, Robotics and AI. This led to some open source stuff like AForge.NET, Computer Vision Sandbox, cam2web, ANNT, etc.

Comments and Discussions