OSDN Git Service

hk
authorunknown <hskwk@.(none)>
Wed, 17 Mar 2010 23:45:21 +0000 (08:45 +0900)
committerunknown <hskwk@.(none)>
Wed, 17 Mar 2010 23:45:21 +0000 (08:45 +0900)
dev3/PsychlopsSilverlight3.csproj
dev3/psychlops/core/device/hid.cs
dev3/psychlops/core/graphic/canvas.cs
dev3/psychlops/core/graphic/font.cs [new file with mode: 0644]
dev3/psychlops/core/graphic/module.cs
dev3/psychlops/core/graphic/shape.cs
dev3/psychlops/core/math/util.cs
dev3/psychlops/extention/standard/figures.cs
test3/MainPage.xaml
test3/MainPage.xaml.cs
test3/PsychlopsMain.cs

index 0b09745..1a4e533 100644 (file)
@@ -54,6 +54,7 @@
     <Compile Include="psychlops\core\app\Class1.cs" />\r
     <Compile Include="psychlops\core\device\hid.cs" />\r
     <Compile Include="psychlops\core\graphic\canvas.cs" />\r
+    <Compile Include="psychlops\core\graphic\font.cs" />\r
     <Compile Include="psychlops\core\graphic\module.cs" />\r
     <Compile Include="psychlops\core\math\util.cs" />\r
     <Compile Include="psychlops\extention\standard\figures.cs" />\r
index ff671b5..0dce2b3 100644 (file)
@@ -48,12 +48,12 @@ namespace Psychlops
                }\r
        }\r
 \r
-       public class Mouse\r
+       public static class Mouse\r
        {\r
-               static protected Point position_;\r
+               static internal Point position_;\r
                static internal System.Windows.UIElement _prime;\r
                static public HID.Button left, right, middle;\r
-               static protected Point wheelDelta_;\r
+               static internal Point wheelDelta_;\r
 \r
                static public Point position { get { return position_; } }\r
                static public int x { get { return (int)position_.x; } }\r
index 4414a24..9098329 100644 (file)
@@ -17,20 +17,26 @@ namespace Psychlops
        {\r
                public static System.Windows.Controls.Image default_buffer_frame;\r
                public static System.Windows.Controls.UserControl default_panel;\r
+               public static System.Windows.Controls.Canvas default_api_canvas;\r
                public static WriteableBitmap default_buffer;\r
                System.Windows.Controls.Image instance;\r
-               System.Windows.Controls.UserControl panel; \r
+               System.Windows.Controls.Canvas api_canvas;\r
+               System.Windows.Controls.UserControl panel;\r
                WriteableBitmap buffer;\r
+               System.Windows.Shapes.Rectangle back_panel;\r
+               System.Windows.Media.SolidColorBrush back_panel_color;\r
 \r
                public Canvas(int wid, int hei)\r
                {\r
                        panel = default_panel;\r
+                       api_canvas = default_api_canvas;\r
                        instance = default_buffer_frame;\r
                        initialize(wid, hei);\r
                }\r
-               public Canvas(int wid, int hei, System.Windows.Controls.Image target, System.Windows.Controls.UserControl system)\r
+               public Canvas(int wid, int hei, System.Windows.Controls.Image target, System.Windows.Controls.Canvas apicnvs,  System.Windows.Controls.UserControl system)\r
                {\r
                        panel = system;\r
+                       api_canvas = apicnvs;\r
                        instance = target;\r
                        initialize(wid, hei);\r
                }\r
@@ -38,15 +44,23 @@ namespace Psychlops
                {\r
                        instance.Width = wid;\r
                        instance.Height = hei;\r
-                       buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height);\r
                        instance.Source = buffer;\r
-                       instance.MouseMove += Mouse.Canvas_MousePos;\r
-                       instance.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
-                       instance.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
-                       instance.MouseWheel += Mouse.Canvas_MouseWheel;\r
-                       Mouse._prime = instance;\r
+                       api_canvas.Width = wid;\r
+                       api_canvas.Height = hei;\r
+                       buffer = new WriteableBitmap((int)instance.Width, (int)instance.Height);\r
+                       api_canvas.MouseMove += Mouse.Canvas_MousePos;\r
+                       api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown;\r
+                       api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp;\r
+                       api_canvas.MouseWheel += Mouse.Canvas_MouseWheel;\r
+                       Mouse._prime = api_canvas;\r
                        Main.drawable = this;\r
                        default_buffer = buffer;\r
+\r
+                       back_panel = new System.Windows.Shapes.Rectangle();\r
+                       back_panel.Width = wid;\r
+                       back_panel.Height = hei;\r
+                       back_panel_color = new SolidColorBrush();\r
+                       back_panel.Fill = back_panel_color;\r
                }\r
                public Point getCenter()\r
                {\r
@@ -59,7 +73,10 @@ namespace Psychlops
                }\r
                public void clear(Color col)\r
                {\r
-                       buffer.Clear(col);\r
+                       api_canvas.Children.Clear();\r
+                       back_panel_color.Color = col;\r
+                       api_canvas.Children.Add(back_panel);\r
+                       //buffer.Clear(col);\r
                }\r
 \r
                public void pix(int x, int y, Color col)\r
@@ -93,37 +110,95 @@ namespace Psychlops
 \r
                public void line(Line drawee, Color col)\r
                {\r
-                       buffer.DrawLine((int)drawee.begin.x, (int)drawee.begin.y, (int)drawee.end.x, (int)drawee.end.y, col);\r
+                       //buffer.DrawLine((int)drawee.begin.x, (int)drawee.begin.y, (int)drawee.end.x, (int)drawee.end.y, col);\r
+                       var zapi_shape = new System.Windows.Shapes.Line();\r
+                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
+                       zapi_shape.X1 = (int)drawee.begin.x;\r
+                       zapi_shape.Y1 = (int)drawee.begin.y;\r
+                       zapi_shape.X2 = (int)drawee.end.x;\r
+                       zapi_shape.Y2 = (int)drawee.end.y;\r
+                       zapi_fill.Color = col;\r
+                       zapi_fill.Opacity = 1.0;\r
+                       zapi_shape.Stroke = zapi_fill;\r
+                       zapi_shape.StrokeThickness = 1;\r
+                       api_canvas.Children.Add(zapi_shape);\r
+                       //System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
+                       //System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
                }\r
                public void rect(Rectangle drawee, Color col)\r
                {\r
                        //buffer.DrawRectangle((int)drawee.v1.x, (int)drawee.v1.y, (int)drawee.v2.x, (int)drawee.v2.y, col);\r
-                       api_rect.Width = drawee.width;\r
-                       api_rect.Height = drawee.height;\r
-                       api_fill.Color = col;\r
-                       api_rect.Fill = api_fill;\r
-                       api_translation.X = drawee.left;\r
-                       api_translation.Y = drawee.top;\r
-                       buffer.Render(api_rect, api_translation);\r
+\r
+                       var zapi_shape = new System.Windows.Shapes.Rectangle();\r
+                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
+                       zapi_shape.Width = drawee.width;\r
+                       zapi_shape.Height = drawee.height;\r
+                       zapi_fill.Color = col;\r
+                       zapi_shape.Fill = zapi_fill;\r
+                       api_canvas.Children.Add(zapi_shape);\r
+                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
+                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
                }\r
                public void ellipse(Ellipse drawee, Color col)\r
                {\r
-                       buffer.DrawEllipse((int)(drawee.datum.x - drawee.xdiameter / 2), (int)(drawee.datum.y - drawee.ydiameter / 2), (int)(drawee.datum.x + drawee.xdiameter / 2), (int)(drawee.datum.y + drawee.ydiameter / 2), col);\r
+                       //buffer.DrawEllipse((int)(drawee.datum.x - drawee.xdiameter / 2), (int)(drawee.datum.y - drawee.ydiameter / 2), (int)(drawee.datum.x + drawee.xdiameter / 2), (int)(drawee.datum.y + drawee.ydiameter / 2), col);\r
+\r
+                       var zapi_shape = new System.Windows.Shapes.Ellipse();\r
+                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
+                       zapi_shape.Width = drawee.width;\r
+                       zapi_shape.Height = drawee.height;\r
+                       zapi_fill.Color = col;\r
+                       zapi_shape.Fill = zapi_fill;\r
+                       api_canvas.Children.Add(zapi_shape);\r
+                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.left);\r
+                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.top);\r
                }\r
                public void polygon(Polygon drawee, Color col)\r
                {\r
+                       /*\r
                        int[] ps = new int[drawee.vertices.Count];\r
-                       int i=0;\r
+                       buffer.DrawPolyline(ps, col);\r
+                       */\r
+                       var zapi_shape = new System.Windows.Shapes.Polygon();\r
+                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
+                       int i = 0;\r
                        foreach (Point p in drawee.vertices)\r
                        {\r
-                               ps[i] = (int)p.x; i++;\r
-                               ps[i] = (int)p.y; i++;\r
+                               zapi_shape.Points.Add(p);\r
                        }\r
-                       buffer.DrawPolyline(ps, col);\r
+                       zapi_fill.Color = col;\r
+                       zapi_shape.Fill = zapi_fill;\r
+                       api_canvas.Children.Add(zapi_shape);\r
+                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x);\r
+                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y);\r
+               }\r
+               public void letters(Letters drawee, Color col)\r
+               {\r
+                       //var zapi_shape = new System.Windows.Documents.Glyphs();\r
+                       var zapi_shape = new System.Windows.Controls.TextBlock();\r
+                       var zapi_fill = new System.Windows.Media.SolidColorBrush();\r
+                       zapi_shape.MaxWidth = width;\r
+                       zapi_shape.MaxHeight = height;\r
+                       zapi_shape.Text = drawee.str;\r
+                       //zapi_shape.FontFamily = ;\r
+                       zapi_shape.FontSize = drawee.font.size;\r
+                       zapi_shape.FontStyle = drawee.font.style==Font.Style.normal_style ? System.Windows.FontStyles.Normal : System.Windows.FontStyles.Italic;\r
+                       zapi_shape.FontWeight = drawee.font.weight>400 ? System.Windows.FontWeights.Bold : System.Windows.FontWeights.Normal;\r
+                       zapi_shape.TextAlignment = drawee.align==Letters.HorizontalAlign.TEXT_ALIGN_LEFT ? TextAlignment.Left : TextAlignment.Center;\r
+                       zapi_fill.Color = col;\r
+                       zapi_shape.Foreground = zapi_fill;\r
+                       api_canvas.Children.Add(zapi_shape);\r
+                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x);\r
+                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y);\r
                }\r
                public void image(Image drawee)\r
                {\r
-                       buffer.Blit(drawee.datum, drawee.buffer, drawee.self_rect, Colors.White, WriteableBitmapExtensions.BlendMode.None);\r
+                       //buffer.Blit(drawee.datum, drawee.buffer, drawee.self_rect, Colors.White, WriteableBitmapExtensions.BlendMode.None);\r
+                       var zapi_shape = new System.Windows.Controls.Image();\r
+                       zapi_shape.Source = drawee.buffer;\r
+                       api_canvas.Children.Add(zapi_shape);\r
+                       System.Windows.Controls.Canvas.SetLeft(zapi_shape, drawee.datum.x);\r
+                       System.Windows.Controls.Canvas.SetTop(zapi_shape, drawee.datum.y);\r
                }\r
 \r
 \r
@@ -133,6 +208,9 @@ namespace Psychlops
                        buffer.Invalidate();\r
                        frame++;\r
                }\r
+\r
+               public double width { get { return api_canvas.Width; } }\r
+               public double height { get { return api_canvas.Height; } }\r
        }\r
 \r
 \r
diff --git a/dev3/psychlops/core/graphic/font.cs b/dev3/psychlops/core/graphic/font.cs
new file mode 100644 (file)
index 0000000..3c03840
--- /dev/null
@@ -0,0 +1,131 @@
+using System;\r
+using System.Net;\r
+using System.Windows;\r
+using System.Windows.Controls;\r
+using System.Windows.Documents;\r
+using System.Windows.Ink;\r
+using System.Windows.Input;\r
+using System.Windows.Media;\r
+using System.Windows.Media.Animation;\r
+using System.Windows.Shapes;\r
+\r
+namespace Psychlops\r
+{\r
+       public class Font\r
+       {\r
+               public static Font default_font;\r
+\r
+               public enum Style { normal_style, italic, oblique };\r
+               public enum Weight { normal_weight=400, bold=700 };\r
+               public double size;\r
+               public int weight;\r
+               public Style style;\r
+               public string[] family;\r
+\r
+               static Font()\r
+               {\r
+                       default_font = new Font();\r
+               }\r
+               public Font()\r
+               {\r
+                       size = 24;\r
+                       weight = (int)Weight.normal_weight;\r
+                       style = Style.normal_style;\r
+                       family = new string[1];\r
+               }\r
+               public Font(double size_, int weight_, Style style_, string family_)\r
+               {\r
+                       size = size_;\r
+                       weight = weight_;\r
+                       style = style_;\r
+                       family = new string[1];\r
+                       family[0] = family_;\r
+               }\r
+               public Font(string family_, double size_, int weight_, Style style_)\r
+               {\r
+                       size = size_;\r
+                       weight = weight_;\r
+                       style = style_;\r
+                       family = new string[1];\r
+                       family[0] = family_;\r
+               }\r
+               ~Font()\r
+               {\r
+               }\r
+       }\r
+\r
+       public class Letters : Shape\r
+       {\r
+               protected string str_;\r
+               protected Font font_;\r
+               protected double width_, height_;\r
+               public Point datum;\r
+\r
+               public enum HorizontalAlign { NOT_SPECIFIED=-1, TEXT_ALIGN_LEFT=0, TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT };\r
+               public HorizontalAlign align;\r
+\r
+               public Letters()\r
+               {\r
+                       str_ = "";\r
+                       font = Font.default_font;\r
+                       align = HorizontalAlign.TEXT_ALIGN_LEFT;\r
+               }\r
+               public Letters(String init_str)\r
+               {\r
+                       str_ = init_str;\r
+                       font_ = Font.default_font;\r
+                       align = HorizontalAlign.TEXT_ALIGN_LEFT;\r
+               }\r
+               public Letters(String init_str, Font init_font)\r
+               {\r
+                       str_ = init_str;\r
+                       font_ = init_font;\r
+                       align = HorizontalAlign.TEXT_ALIGN_LEFT;\r
+               }\r
+               ~Letters()\r
+               {\r
+               }\r
+               public Font font\r
+               {\r
+                       get { return font_; }\r
+                       set { font_ = value; }\r
+               }\r
+               public Font getFont() { return font; }\r
+               public string str\r
+               {\r
+                       get { return str_; }\r
+                       set { str_ = value; }\r
+               }\r
+               public String getString() { return str; }\r
+               public Figure centering(Point p)\r
+               {\r
+                       datum = p;\r
+                       align = HorizontalAlign.TEXT_ALIGN_CENTER;\r
+                       return this;\r
+               }\r
+               public Figure shift(Point p)\r
+               {\r
+                       return this;\r
+               }\r
+               public Letters locate(Point p)\r
+               {\r
+                       datum = p;\r
+                       return this;\r
+               }\r
+               public Letters locate(double x, double y)\r
+               {\r
+                       datum.set(x,y);\r
+                       return this;\r
+               }\r
+\r
+               public void draw()\r
+               {\r
+                       Main.drawable.letters(this, Color.white);\r
+               }\r
+               public void draw(Color c)\r
+               {\r
+                       Main.drawable.letters(this, c);\r
+               }\r
+       }\r
+\r
+}\r
index b21784c..2ecc730 100644 (file)
@@ -66,6 +66,13 @@ namespace Psychlops
                        r = g = b = lum;\r
                        a = 1.0;\r
                }\r
+               public Color(double red, double green, double blue)\r
+               {\r
+                       r = red;\r
+                       g = green;\r
+                       b = blue;\r
+                       a = 1.0;\r
+               }\r
                public Color(double red, double green, double blue, double alpha)\r
                {\r
                        r = red;\r
@@ -73,6 +80,25 @@ namespace Psychlops
                        b = blue;\r
                        a = alpha;\r
                }\r
+               public void set(double lum)\r
+               {\r
+                       r = g = b = lum;\r
+                       a = 1.0;\r
+               }\r
+               public void set(double red, double green, double blue)\r
+               {\r
+                       r = red;\r
+                       g = green;\r
+                       b = blue;\r
+                       a = 1.0;\r
+               }\r
+               public void set(double red, double green, double blue, double alpha)\r
+               {\r
+                       r = red;\r
+                       g = green;\r
+                       b = blue;\r
+                       a = alpha;\r
+               }\r
                public static implicit operator System.Windows.Media.Color(Color d)\r
                {\r
                        return System.Windows.Media.Color.FromArgb((byte)(d.a * 255), (byte)(d.r * 255), (byte)(d.g * 255), (byte)(d.b * 255));\r
@@ -100,8 +126,9 @@ namespace Psychlops
                void line(Line drawee, Color col);\r
                void rect(Rectangle drawee, Color col);\r
                void ellipse(Ellipse drawee, Color col);\r
-               void image(Image drawee);\r
                void polygon(Polygon drawee, Color col);\r
+               void letters(Letters drawee, Color col);\r
+               void image(Image drawee);\r
        }\r
 \r
 \r
@@ -122,10 +149,12 @@ namespace Psychlops
                {\r
                        return target.centering(Main.drawable.getCenter());\r
                }\r
+               public static Figure centering(this Figure target, double x, double y)\r
+               {\r
+                       return target.centering(new Point(x, y));\r
+               }\r
        }\r
 \r
-\r
-\r
        public class Image : Figure\r
        {\r
                public WriteableBitmap buffer;\r
index ffefeea..9533e8e 100644 (file)
@@ -14,6 +14,7 @@ namespace Psychlops{
 \r
        public interface Shape : Figure\r
        {\r
+               void draw(Color c);\r
        }\r
        public static class ShapeExtention\r
        {\r
@@ -25,13 +26,30 @@ namespace Psychlops{
                */\r
        }\r
 \r
+       public struct Stroke\r
+       {\r
+\r
+       }\r
 \r
        public class Line : Shape\r
        {\r
                public Point begin, end;\r
 \r
+               public Line(double x1, double y1, double x2, double y2)\r
+               {\r
+                       set(x1, y1, x2, y2);\r
+               }\r
                public Line(Point v1, Point v2)\r
                {\r
+                       set(v1, v2);\r
+               }\r
+               public void set(double x1, double y1, double x2, double y2)\r
+               {\r
+                       begin.set(x1, y1);\r
+                       end.set(x2, y2);\r
+               }\r
+               public void set(Point v1, Point v2)\r
+               {\r
                        begin = v1;\r
                        end   = v2;\r
                }\r
@@ -61,6 +79,10 @@ namespace Psychlops{
                        Main.drawable.line(this, Color.white);\r
                }\r
 \r
+               public double left { get { return begin.x < end.x ? begin.x : end.x; } }\r
+               public double top { get { return begin.y < end.y ? begin.y : end.y; ; } }\r
+               public double right { get { return begin.x > end.x ? begin.x : end.x; ; } }\r
+               public double bottom { get { return begin.y > end.y ? begin.y : end.y; ; } }\r
                public double width { get { return Math.abs(begin.x - end.x); } }\r
                public double height { get { return Math.abs(begin.y - end.y); } }\r
 \r
@@ -142,6 +164,21 @@ namespace Psychlops{
                public Point datum;\r
                public double xdiameter, ydiameter;\r
 \r
+               public Ellipse()\r
+               {\r
+                       set(0,0);\r
+               }\r
+               public Ellipse(double wid, double hei)\r
+               {\r
+                       set(wid, hei);\r
+               }\r
+\r
+               public Ellipse set(double wid, double hei)\r
+               {\r
+                       xdiameter = wid;\r
+                       ydiameter = hei;\r
+                       return this;\r
+               }\r
                public Figure shift(Point p)\r
                {\r
                        datum += p;\r
@@ -161,6 +198,13 @@ namespace Psychlops{
                {\r
                        Main.drawable.ellipse(this, Color.white);\r
                }\r
+\r
+               public double left { get { return datum.x - xdiameter/2.0; } }\r
+               public double top { get { return datum.y - ydiameter / 2.0; } }\r
+               public double right { get { return datum.x + xdiameter / 2.0; } }\r
+               public double bottom { get { return datum.y + ydiameter / 2.0; } }\r
+               public double width { get { return Math.abs(xdiameter); } }\r
+               public double height { get { return Math.abs(ydiameter); } }\r
        }\r
 \r
 \r
@@ -173,6 +217,24 @@ namespace Psychlops{
                {\r
                        vertices = new System.Collections.Generic.List<Point>();\r
                }\r
+               public Polygon(double[] verts)\r
+               {\r
+                       vertices = new System.Collections.Generic.List<Point>();\r
+                       for (int i=0; i < verts.Length; i+=2)\r
+                       {\r
+                               vertices.Add(new Point(verts[i], verts[i+1]));\r
+                       }\r
+\r
+               }\r
+               public Polygon(Point[] verts)\r
+               {\r
+                       vertices = new System.Collections.Generic.List<Point>();\r
+                       foreach (Point p in verts)\r
+                       {\r
+                               vertices.Add(p);\r
+                       }\r
+\r
+               }\r
                public Polygon append(Point p) { vertices.Add(p); return this; }\r
                public Polygon append(double x, double y) { return append(new Point(x, y, 0.0)); }\r
                public Polygon append(double x, double y, double z) { return append(new Point(x, y, z)); }\r
index 3d451a8..7f498d3 100644 (file)
@@ -6,6 +6,12 @@ namespace Psychlops
        public static class Math\r
        {\r
                public static readonly double PI = 3.14159265, E = 2.718281828459045;\r
+               public static Random random_generator;\r
+               static Math()\r
+               {\r
+                       random_generator = new Random();\r
+               }\r
+\r
                public static double abs(double x)\r
                {\r
                        return System.Math.Abs(x);\r
@@ -39,6 +45,19 @@ namespace Psychlops
                        return System.Math.Sqrt(x * x + y * y);\r
                }\r
 \r
+               public static double random()\r
+               {\r
+                       return (random_generator.NextDouble());\r
+               }\r
+               public static double random(double x)\r
+               {\r
+                       return (random_generator.NextDouble()) * x;\r
+               }\r
+               public static double random(double x, double y)\r
+               {\r
+                       return (random_generator.NextDouble()) * (y-x) + x;\r
+               }\r
+\r
 \r
                public static double gaussian(double x, double sigma)\r
                {\r
index 6d512f6..dd26370 100644 (file)
@@ -1,52 +1,49 @@
-namespace Psychlops\r
+\r
+using System.Windows.Media.Imaging;\r
+namespace Psychlops\r
 {\r
 \r
     public static class Figures\r
     {\r
-        /*\r
-           public static void drawGrating(out Image img, int width , int height, double frequency, double contrast, double orientation, double phase)\r
-           {\r
-                   if(img.width != width || img.height != height) img = new Image(width, height);\r
-                   double xp, yp, r, freq = frequency*2*PI;\r
-                   for(int y=0; y<height; y++) {\r
-                           yp = y-height/2.0;\r
-                           for(int x=0; x<width; x++) {\r
-                                   xp = x-width/2.0;\r
-                                   r = sqrt(xp*xp+yp*yp);\r
-                                   img.pix_raw(x,y,\r
-                                     Color(\r
-                                           0.5+0.5 * contrast* cos(freq* (sin(orientation)*xp-cos(orientation)*yp) + phase)\r
-                                     )\r
-                                   );\r
-                           }\r
-                   }\r
-           }\r
 \r
-        public static void drawGaussian(out Image img, double sigma, double factor)\r
+               public static void drawGrating(double wavelength, double contrast, double orientation, double phase)\r
+               {\r
+                       double freq = 2 * Math.PI / wavelength;\r
+                       Canvas.default_buffer.ForEach(\r
+                               (x, y) => new Color(.5 + .5 * Math.sin(phase + (Math.sin(orientation) * x - Math.cos(orientation) * y) * freq))\r
+                       );\r
+               }\r
+\r
+               public static void drawGrating(ref Image img, int width, int height, double wavelength, double contrast, double orientation, double phase)\r
            {\r
-            const int width = (int)(sigma * 8), height = (int)(sigma * 8);\r
-            if (img.width != width || img.height != height) img = new Image(width, height);\r
-            double xp, yp, r, r2;\r
-                   for(int y=0; y<height; y++) {\r
-                           yp = y-height/2.0;\r
-                           for(int x=0; x<width; x++) {\r
-                                   xp = x-width/2.0;\r
-                                   r = sqrt(xp*xp+yp*yp);\r
-                                   r2 = -(r*r) / (2.0*sigma*sigma);\r
-                                   img.pix_raw(x,y,Color(factor*(exp(r2))));\r
-                           }\r
-                   }\r
+                       double width_half = width / 2.0, height_half = height / 2.0;\r
+            if (img==null || img.width != width || img.height != height)\r
+                img = new Image(width, height);\r
+            double freq = 2 * Math.PI / wavelength;\r
+                       img.field(\r
+                               (x, y) => new Color(.5 +.5 * Math.sin(phase + (Math.sin(orientation)*x-Math.cos(orientation)*y) * freq))\r
+                       );\r
            }\r
-         * */\r
+               public static void drawGaussian(ref Image img, double sigma, double factor)\r
+               {\r
+                       int width = (int)(sigma * 8), height = (int)(sigma * 8);\r
+                       double width_half = width / 2.0, height_half = height / 2.0;\r
+                       if (img == null || img.width != width || img.height != height)\r
+                               img = new Image(width, height);\r
+                       img.field(\r
+                               (x, y) => new Color(factor * Math.gaussian(Math.radius(x - width_half, y - height_half), width / 8.0))\r
+                       );\r
+               }\r
 \r
-        public static void drawGabor(out Image img, double sigma, double wavelength, double contrast, double orientation, double phase)\r
+               public static void drawGabor(ref Image img, double sigma, double wavelength, double contrast, double orientation, double phase)\r
            {\r
             int width = (int)(sigma * 8), height = (int)(sigma * 8);\r
-            //if (img.width != width || img.height != height)\r
+                       double width_half = width / 2.0, height_half = height / 2.0;\r
+            if (img==null || img.width != width || img.height != height)\r
                 img = new Image(width, height);\r
             double freq = 2 * Math.PI / wavelength;\r
                        img.field(\r
-                (x, y) => new Color(.5 + Math.gaussian(Math.radius(x - width, y - height), width / 8.0) * .5 * Math.sin(phase + x * freq))\r
+                               (x, y) => new Color(.5 + Math.gaussian(Math.radius(x - width_half, y - height_half), width / 8.0) * .5 * Math.sin(phase + (Math.sin(orientation)*x-Math.cos(orientation)*y) * freq))\r
                        );\r
            }\r
 \r
index 58409bd..39b6ab9 100644 (file)
@@ -2,8 +2,8 @@
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" \r
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"\r
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" \r
-    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">\r
-  <Grid x:Name="LayoutRoot">\r
+    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" x:Name="LayoutMain">\r
+  <Canvas x:Name="LayoutRoot">\r
     <Image x:Name="main_buffer" Width="640" Height="480"/>\r
-  </Grid>\r
+  </Canvas>\r
 </UserControl>\r
index b61a607..082ec87 100644 (file)
@@ -12,6 +12,7 @@ namespace PsychlopsSilverlight3test
                        InitializeComponent();\r
                        // Show fps counter\r
                        Canvas.default_buffer_frame = main_buffer;\r
+                       Canvas.default_api_canvas = LayoutRoot;\r
                        Canvas.default_panel = this;\r
                        System.Windows.Application.Current.Host.Settings.MaxFrameRate = 60;\r
                        System.Windows.Application.Current.Host.Settings.EnableFrameRateCounter = true;\r
index 829afbd..1a00e41 100644 (file)
@@ -7,6 +7,8 @@ namespace PsychlopsSilverlight3test
                Canvas cnvs;\r
                Image img;\r
                Rectangle fixation;\r
+               Shape shape;\r
+               Color col;\r
                int isize = 100;\r
                double tfreq = 1;\r
 \r
@@ -15,20 +17,28 @@ namespace PsychlopsSilverlight3test
                        cnvs = new Canvas(500, 500);\r
                        img = new Image(isize * 2, isize * 2);\r
                        fixation = new Rectangle(10, 10);\r
+                       var poly = new Letters("glkhglk");\r
+                       shape = poly;\r
                        cnvs.clear(Color.gray);\r
                }\r
 \r
                public void each_frames()\r
                {\r
-                       cnvs.clear(new Color(Mouse.left.pressed() ? 1.0 : 0.5));\r
-                       img.field(\r
-                               (x, y) => new Color(.5 + Math.gaussian(Math.radius(x - isize, y - isize), isize / 8.0) * .5 * Math.sin(cnvs.frame * 2 * Math.PI / tfreq / 60 + x / 3.0))\r
-                                       );\r
-                       img.centering(Mouse.position);\r
-                       //img.shift(1,0);\r
-                       img.draw();\r
+                       cnvs.clear(new Color(Mouse.left.pressed() ? 0.75 : 0.25));\r
+                       col.set(Math.random(1.0));\r
+\r
                        fixation.centering();\r
                        fixation.draw(Color.red);\r
+\r
+                       Figures.drawGabor(ref img, 20, 10, 1, 0, cnvs.frame * 2.0 * Math.PI / tfreq / 60);\r
+                       //Figures.drawGaussian(ref img, 20, 1);\r
+                       //Figures.drawGrating(10, 1, 2, cnvs.frame * 2.0 * Math.PI / tfreq / 60);\r
+                       img.centering(Mouse.position);\r
+                       img.draw();\r
+\r
+                       shape.centering(Mouse.position);\r
+                       shape.draw(Color.red);\r
+\r
                        cnvs.flip();\r
                }\r
        }\r