OSDN Git Service

132
[psychlops/silverlight.git] / test4 / PsychlopsMain.cs
index e7b6754..c61acea 100644 (file)
-/*using Psychlops;\r
-\r
-namespace PsychlopsSilverlight4test\r
-{\r
+\r
+/*\r
+//Two types of plaid motion\r
+//E. H. Adelson and J. A. Movshon (1982).\r
+//Phenomenal coherence of moving visual patterns. Nature 300, 523-525\r
+\r
+///+ Prefix\r
+//// Include Psychlops Package\r
+using Psychlops;\r
 \r
-       public class PsychlopsMain\r
+       namespace PsychlopsSilverlightApp\r
        {\r
 \r
-               Psychlops.Canvas cnvs;\r
-               int i;\r
-               double x, y, z, t, p, temp, xx, yy;\r
-\r
-               //Set Target Initial Value\r
-               double TargetEcce = 100.0, TargetSize = 5.0, TargetNumber = 5, Rotate = 0.0;\r
-\r
-               //Set Background Initial Value\r
-               double DotNumber = 50, thetaSpeed = 3.0, Axis = 0.0, BGRadii = 150, BGSize = 5.0;\r
-\r
-               public void psychlops_main()\r
+               public class PsychlopsMain\r
                {\r
-                       cnvs = new Canvas(500, 500);\r
-\r
-                       //Declare background dots and target\r
-                       Rectangle[] BGDot = new Rectangle[2048];\r
-                       for(int i=0; i<2048; i++){\r
-                               BGDot[i]=new Rectangle();\r
-                       }\r
-                       Rectangle[] Target= new Rectangle[10];\r
-                       for(int i=0; i<10; i++){\r
-                               Target[i]=new Rectangle();\r
-                       }\r
-\r
-\r
-                       //Declare Matrix to keep back ground dots' coordinate value\r
-                       double[] DotX=new double[2048];\r
-                       double[] DotY = new double[2048];\r
-\r
+                       ///- Prefix\r
 \r
-                       //Set Independent variables to manipulate\r
-\r
-\r
-                       //Initialize positions of background dots\r
-                       for(int i=0; i<2048; i++){\r
-                               t=2.0*Math.PI*Math.random(1.0);\r
-                               p=2.0*Math.PI*Math.random(1.0);\r
-                               DotX[i]=t;\r
-                               DotY[i]=p;\r
-\r
-                               x=BGRadii*Math.cos(t)*Math.cos(p);\r
-                               y=BGRadii*Math.sin(t)*Math.cos(p);\r
-                               BGDot[i].set(BGSize, BGSize);\r
-                               BGDot[i].centering().shift(x,y);\r
+                       ///+ Global\r
+                       // Struct for component paremeters\r
+                       struct component\r
+                       {\r
+                               public double contrast;\r
+                               public double orientation;\r
+                               public double lambda;\r
+                               public double tf;\r
                        }\r
+                       ///- Global\r
 \r
-                       //Initialize positions of targets\r
-                       for(int i=0; i<10; i++)Target[i].set(TargetSize, TargetSize);\r
-\r
-                       double COS, SIN;\r
-                       DotNumber = 200;\r
-                       //Main stimulus loop\r
-                       while(true){\r
-                               //Clear the main window\r
-                               cnvs.clear(Color.green);\r
-\r
-\r
-                               temp=Axis/360*2*Math.PI;\r
-                               COS=Math.cos(temp);\r
-                               SIN=Math.sin(temp);\r
-\r
-                               //Calculate positions of background dots and set them\r
-                               for(int i=0; i<DotNumber; i++){\r
-                                       //Calculate in polar coordinate\r
-                                       t=DotX[i]+2.0*Math.PI*thetaSpeed/360.0;\r
-                                       p=DotY[i];\r
-\r
-                                       //Save current position\r
-                                       DotX[i]=t;\r
-\r
-                                       //Convert to Decartes coordinate\r
-                                       x=BGRadii*Math.cos(t)*Math.cos(p);\r
-                                       y=BGRadii*Math.sin(t)*Math.cos(p);\r
-                                       z=BGRadii*Math.sin(p);\r
-                                       xx=x;\r
-                                       yy=SIN*y+COS*z;\r
-\r
-                                       //Set Dotsize and Draw them\r
-                                       BGDot[i].set(BGSize, BGSize);\r
-                                       BGDot[i].centering().shift(xx,yy);\r
-                                       BGDot[i].draw( Color.blue );\r
+                       ///+ Stimulus drawing function\r
+                       //// A function for stimulus drawing (main body)\r
+                       void drawgratingmovie(Image[] img, component c1, component c2, double contrast, int start, int maxframe, double bg_lum, double alpha)\r
+                       {\r
+                               double _xp, _xp2, col1, col2;\r
+                               double contrast1, contrast2;\r
+                               double imageheight, imagewidth;\r
+\r
+                               contrast1 = contrast*c1.contrast/(c1.contrast+c2.contrast);\r
+                               contrast2 = contrast*c2.contrast/(c1.contrast+c2.contrast);\r
+\r
+                               imageheight = img[0].getHeight();\r
+                               imagewidth = img[0].getWidth();\r
+\r
+                               for(int frame=start; frame<start+maxframe+1; frame++){\r
+                                       //Display.progressbar(frame, maxframe+1);\r
+                                       //img[frame].convert(Image::RGBA);\r
+                                       for(int i=0; i<imagewidth; i++){\r
+                                               for(int j=0; j<imageheight; j++){\r
+                                                       _xp = Math.sin(c1.orientation)*(i-imagewidth*0.5) + Math.cos(c1.orientation) * (j-imageheight *0.5);\r
+                                                       _xp2 = Math.sin(c2.orientation)*(i-imagewidth*0.5) + Math.cos(c2.orientation) * (j-imageheight *0.5);\r
+                                                       col1 = bg_lum*contrast1*Math.sin(2*Math.PI*_xp/c1.lambda + 2*Math.PI*c1.tf*frame/(double)refresh);\r
+                                                       col2 = bg_lum*contrast2*Math.sin(2*Math.PI*_xp2/c2.lambda + 2*Math.PI*c2.tf*frame/(double)refresh);\r
+                                                       img[frame].pix(i,j,new Color(col1+col2+bg_lum));\r
+                                                       img[frame].alpha(i,j,alpha);\r
+                                               }\r
+                                       }\r
+                                       //img[frame].cache();\r
                                }\r
+                       }\r
 \r
-                               //Calculate positions of targets and set them\r
-                               temp=Rotate*2*Math.PI/360.0;\r
-                               for (int i = 0; i < 5; i++)\r
+                       ///+ Stimulus drawing function\r
+                       //// A function for stimulus drawing (main body)\r
+                       void drawplaid() {\r
+                               Canvas display = new Canvas(Canvas.window);\r
+                               ///+ Preperation\r
+                               //// Declare and initialize local variables\r
+                               int rect_size = 150;\r
+                               double rect_lum  = 0.5;\r
+                               double bg_lum    = 0.2;\r
+                               double duration = 400;\r
+\r
+                               double[] lambda = new double[2], contrast = new double[2], tf = new double[2];\r
+                               double center_orientation = 0.0;\r
+                               Image[,] movie = StaticFunctions.NewArray<Image>(2, 120);\r
+                               Image[,] component_movie = StaticFunctions.NewArray<Image>(4, 120);\r
+                               Image envelope = new Image(), envelope_small = new Image();\r
+\r
+                               for(int i=0; i<120; i++)\r
                                {\r
-                                       t = 2.0 * Math.PI/TargetNumber;\r
-                                       x = TargetEcce*Math.cos(i*t+temp);\r
-                                       y = TargetEcce*Math.sin(i*t+temp);\r
-                                       Target[i].centering().shift(x,y);\r
-                                       Target[i].fill = Color.yellow;\r
-                                       Target[i].draw();\r
+                                       movie[0, i].set(rect_size, rect_size);\r
+                                       movie[1, i].set(rect_size, rect_size);\r
+                                       component_movie[0, i].set(rect_size/2, rect_size/2);\r
+                                       component_movie[1, i].set(rect_size/2, rect_size/2);\r
+                                       component_movie[2, i].set(rect_size/2, rect_size/2);\r
+                                       component_movie[3, i].set(rect_size/2, rect_size/2);\r
                                }\r
-\r
-                               //Reflect drawing at the next frame;\r
-                               cnvs.flip();\r
+                               envelope.set(rect_size, rect_size);\r
+                               envelope_small.set(rect_size/2, rect_size/2);\r
+\r
+                               Letters let1 = new Letters(), let2 = new Letters(), let3 = new Letters();\r
+                               let1.str = "Component1";\r
+                               let2.str = "Component2";\r
+                               let3.str = "Superposition";\r
+                               let1.centering().shift(-rect_size*1.25, -rect_size);\r
+                               //let1.cache();\r
+                               let2.centering().shift(rect_size*1.25, -rect_size);\r
+                               //let2.cache();\r
+                               let3.centering().shift(0.0, rect_size*0.75);\r
+                               //let3.cache();\r
+\r
+                               component c1, c2, c0;\r
+                               c0 = new component{ 0.0,0.0,1.0,1.0 }; //dummy for component movie\r
+                               ///+ type-I\r
+                               // Prepare Type-I plaid movie\r
+                               center_orientation = 0.0;\r
+                               orientation_offset = Math.PI/6;\r
+                               contrast[0] = 1.0, contrast[1] = 1.0;\r
+                               lambda[0] = 30.0, lambda[1] = 30.0;\r
+                               tf[0] = 1.0; tf[1] = 1.0;\r
+\r
+                               c1 = {contrast[0], center_orientation-orientation_offset, lambda[0], tf[0]};\r
+                               c2 = {contrast[1], center_orientation+orientation_offset, lambda[1], tf[1]};\r
+\r
+                       drawgratingmovie(movie[0],c1, c2, 0.5, 0, refresh_int, bg_lum, 1.0);\r
+                       drawgratingmovie(component_movie[0],c0, c2, 0.5, 0, refresh_int, bg_lum, 1.0);\r
+                       drawgratingmovie(component_movie[1],c1, c0, 0.5, 0, refresh_int, bg_lum, 1.0);\r
+                       ///- type-I\r
+\r
+                       ///+ type-II\r
+                       // Prepare Type-II plaid movie\r
+                       center_orientation = -0.0, orientation_offset = Math.PI/12;\r
+                       contrast[0] = 1.0, contrast[1] = 1.0;\r
+                       lambda[0] = 30.0, lambda[1] = 30.0;\r
+                       tf[0] = 1.0; tf[1] = 4.0;\r
+\r
+                       c1 = {contrast[0], center_orientation-orientation_offset, lambda[0], tf[0]};\r
+                       c2 = {contrast[1], center_orientation+orientation_offset, lambda[1], tf[1]};\r
+\r
+                       drawgratingmovie(movie[1],c1, c2, 0.5, 0, refresh_int, bg_lum, 1.0);\r
+                       drawgratingmovie(component_movie[2],c0, c2, 0.5, 0, refresh_int, bg_lum, 1.0);\r
+                       drawgratingmovie(component_movie[3],c1, c0, 0.5, 0, refresh_int, bg_lum, 1.0);\r
+                       ///- type-II\r
+\r
+                       ///+ gaussian\r
+                       //draw Gaussian envelopes\r
+                       envelope.clear(new Color(bg_lum)); //clear offscreen image\r
+                       double _x, _y;\r
+                       for(int i=0; i<rect_size; i++){\r
+                       _x=i-0.5*rect_size;\r
+                       for(int j=0; j<rect_size; j++){\r
+                       _y=j-0.5*rect_size;\r
+                       envelope.alpha(i,j,1.0-Math.exp(-((_x*_x+_y*_y)/ (2.0*pow(rect_size/6.0, 2.0) ))));\r
+                       }\r
                        }\r
-               }\r
-       }\r
-\r
-}\r
-*/\r
 \r
-/*using Psychlops;\r
+                       envelope_small.clear(new Color(bg_lum)); //clear offscreen image\r
+                       for(int i=0; i<rect_size*0.5; i++){\r
+                       _x=i-0.25*rect_size;\r
+                       for(int j=0; j<rect_size*0.5; j++){\r
+                       _y=j-0.25*rect_size;\r
+                       envelope_small.alpha(i,j,1.0-Math.exp(-((_x*_x+_y*_y)/ (2.0*pow(rect_size/12.0, 2.0) ))));\r
+                       }\r
+                       }\r
+                       envelope.cache(); //send offscreen image from main memory to GPU\r
+                       envelope_small.cache(); //send offscreen image from main memory to GPU\r
+                       ///- gaussian\r
+\r
+                       ///+ user interface\r
+                       // Draw user interface\r
+                       Widgets::SelectBox stimulus_type;\r
+                       stimulus_type.area.set(120, 20);\r
+                       stimulus_type.append(L"TypeI");\r
+                       stimulus_type.append(L"TypeII");\r
+                       stimulus_type.centering().shift(-120.0,250.0);\r
+\r
+                       Psychlops::Widgets::SelectBox stimulus_type2;\r
+                       stimulus_type2.area.set(120, 20);\r
+                       stimulus_type2.append(L"Continuous");\r
+                       stimulus_type2.append(L"Periodic");\r
+                       stimulus_type2.centering().shift(120.0,250.0);\r
+\r
+                       Psychlops::Widgets::Slider duration_slider;\r
+                       duration_slider.area.set(100,20);\r
+                       duration_slider.centering().shift(120,280.0);\r
+                       Interval rng = new Interval();\r
+                       duration_slider.link(duration, 50<=rng<=950, 50.0, 50.0);\r
+                       ///- user interface\r
+                       ///- Preperation\r
+\r
+                       ///+ Main loop\r
+                       int frame = 0;\r
+                       int period = refresh_int;\r
+                       int movienum;\r
+                       while(!Keyboard.esc.pushed()) {\r
+                       Display.clear(new Color(bg_lum));\r
+                       movienum = stimulus_type.getSelected(); //if "Type-I" is selected, 0, else frame refresh per sec\r
+\r
+                       in;\r
+                       else period = refresh_int;\r
+\r
+                       if(frame>0){\r
+                       movie[movienum, frame].centering().draw();\r
+                       envelope.centering().draw();\r
+                       component_movie[movienum*2, frame].centering().shift(-rect_size*0.5, -rect_size).draw();\r
+                       envelope_small.centering().shift(-rect_size*0.5, -rect_size).draw();\r
+                       component_movie[movienum*2+1, frame].centering().shift(rect_size*0.5, -rect_size).draw();\r
+                       envelope_small.centering().shift(rect_size*0.5, -rect_size).draw();\r
+                       }\r
 \r
-namespace PsychlopsSilverlight4test\r
-{\r
-       public class PsychlopsMain\r
-       {\r
-               public void psychlops_main()\r
-               {\r
-                       var figure_type = Psychlops.Widgets.Browser.Element.byID("FIGURE");\r
-                       var size_x = Psychlops.Widgets.Browser.Element.byID("SIZE_X");\r
-                       var size_y = Psychlops.Widgets.Browser.Element.byID("SIZE_Y");\r
-                       var shift_x = Psychlops.Widgets.Browser.Element.byID("SHIFT_X");\r
-                       var shift_y = Psychlops.Widgets.Browser.Element.byID("SHIFT_Y");\r
-                       var color_r = Psychlops.Widgets.Browser.Element.byID("COLOR_R");\r
-                       var color_g = Psychlops.Widgets.Browser.Element.byID("COLOR_G");\r
-                       var color_b = Psychlops.Widgets.Browser.Element.byID("COLOR_B");\r
-\r
-                       Canvas window = new Canvas(300, 300);\r
-\r
-                       var figure_r = new Rectangle();\r
-                       var figure_e = new Ellipse();\r
-                       Shape figure = figure_r;\r
-                       \r
-                       while (!Keyboard.esc.pushed())\r
-                       {\r
-                               if (System.String.Compare(figure_type, "Rectangle") == 0)\r
-                               {\r
-                                       figure_r.set(size_x, size_y);\r
-                                       figure = figure_r;\r
-                               }\r
-                               else\r
-                               {\r
-                                       figure_e.set(size_x, size_y);\r
-                                       figure = figure_e;\r
-                               }\r
-                               figure.fill = new Color(color_r, color_g, color_b);\r
+                       ///+ draw user interface\r
+                       let1.draw(0.75);\r
+                       let2.draw(0.75);\r
+                       let3.draw(0.75);\r
+                       stimulus_type.draw();\r
+                       stimulus_type2.draw();\r
+                       if(stimulus_type2.getSelected())duration_slider.draw();\r
+                       ///- draw user interface\r
+                       Display.flip();\r
+                       if(frame++ >= period) frame -= refresh_int;\r
+                       }\r
+                       ///- Main loop\r
 \r
-                               window.clear(Color.black);\r
-                               figure.centering().shift(shift_x, shift_y).draw();\r
-                               window.flip();\r
                        }\r
+                       ///- Stimulus drawing function\r
+\r
+               ///+ Main function for demo circumstances\r
+               public void psychlops_main() {\r
+                       ///+ Demo circumstances\r
+                       //// Spells for run demonstration circumstances\r
+                       Procedure p = new Procedure();\r
+                       //p.setDesign(Procedure::DEMO); //Designate that this is a demo.\r
+                       p.setProcedure(drawplaid);  //The argument name is a name of drawing function.\r
+                       p.run();\r
+                       ///- Demo circumstances\r
                }\r
        }\r
-\r
 }\r
-\r
 */\r
 \r
 \r
 \r
 using Psychlops;\r
-\r
-namespace Psychlops\r
+//Position Bias Program\r
+namespace PsychlopsSilverlightApp\r
 {\r
 \r
-       public class RandomDots\r
-       {\r
-               Rectangle dot;\r
-               public Point[] cood;\r
-               public RandomDots()\r
-               {\r
-                       cood = new Point[250];\r
-                       dot = new Rectangle(5, 5);\r
-               }\r
-               public void draw()\r
-               {\r
-                       dot.fill = Color.white;\r
-                       foreach (Point p in cood)\r
+    public class PsychlopsMain\r
+    {\r
+        Canvas cnvs;\r
+        Image img, img2, img3;\r
+        int isize = 40;        \r
+        int frames;\r
+        Psychlops.Widgets.Slider tfreq;\r
+        Psychlops.Widgets.Slider contrast;\r
+        Psychlops.Widgets.Slider lambda;\r
+               \r
+\r
+               \r
+        public void psychlops_main()\r
+        {\r
+            cnvs = new Canvas(300, 600);\r
+            Interval rng = new Interval();\r
+            tfreq = new Psychlops.Widgets.Slider("Temporal Frequency(Hz)", -5 <= rng <= 5, 3.0);\r
+            contrast = new Psychlops.Widgets.Slider("Contrast", 0.0 <= rng <= 1.0, 0.25);\r
+            lambda = new Psychlops.Widgets.Slider("Wave Length", 10.0 <= rng <= 120.0, 30);\r
+\r
+            img = new Image(isize * 2, isize * 2);\r
+            img2 = new Image(isize * 2, isize * 2);\r
+            img3 = new Image(isize * 2, isize * 2);\r
+\r
+                       var gabor1 = StaticFunctions.NewArray<Figures.ShaderGabor>(100);\r
+                       foreach(var g in gabor1)\r
                        {\r
-                               dot.centering(p).draw();\r
+                               g.setSigma(isize / 8).centering().shift(Math.random(300) - 150, Math.random(600) - 300);\r
+                               g.orientation = Math.random(2*Math.PI);\r
                        }\r
-               }\r
-       }\r
-\r
-}\r
 \r
-\r
-\r
-namespace PsychlopsSilverlight4test\r
-{\r
-\r
-       public class PsychlopsMain\r
-       {\r
-               Canvas cnvs;\r
-               Image img;\r
-               Ellipse fixation;\r
-               Shape shape;\r
-               Color col;\r
-               int isize = 100;\r
-               double tfreq = 1;\r
-               int frames;\r
-               RandomDots dots;\r
-               Group g;\r
-\r
-               public void psychlops_main()\r
-               {\r
-                       cnvs = new Canvas(500, 500);\r
-                       g = new Group();\r
-                       img = new Image(isize * 2, isize * 2);\r
-                       Figures.drawGrating(ref img, 200, 200, 20, 1, 2, frames * 2.0 * Math.PI / tfreq / 60);\r
-                       g.append(img);\r
-                       g.rotation = 50;\r
-                       fixation = new Ellipse(10, 10);\r
-                       fixation.fill = Color.red;\r
-                       //var poly = new Rectangle(100, 100);\r
-                       var poly = new Letters("日本語");\r
-                       //var poly = new Polygon(); poly.append(0, 100); poly.append(-100, 0); poly.append(0, -100); poly.append(100, 0);\r
-                       //var poly = new Ellipse(100, 100);\r
-                       //var poly = new Line(0,0,100, 0);\r
-                       poly.fill = Color.red;\r
-                       poly.stroke = new Stroke { color = Color.yellow, thick = 1 };\r
-                       shape = poly;\r
-\r
-                       var rng = new Interval();\r
-                       var slider = new Psychlops.Widgets.Slider("tesrt", 0 <= rng <= 5);\r
-\r
-                       dots = new RandomDots();\r
-\r
-\r
-                       while (true)\r
+            while (true)\r
                        {\r
-                               frames++;\r
-\r
-\r
-                               cnvs.clear(new Color(Mouse.left.pressed() ? 0.75 : 0.5));\r
-                               col.set(Math.random(1.0));\r
+                cnvs.clear(new Color(0.5));\r
 \r
-                               /*if (frames % 2 == 0)\r
-                               {\r
-                                       fixation.centering().shift(100, 100);\r
-                                       fixation.draw(new Stroke(Color.blue, 3));\r
-                               }*/\r
-\r
-                               fixation.centering();\r
-                               fixation.draw(Color.red);\r
+                Figures.drawGabor(ref img, isize / 8, 1/lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * tfreq / 60);\r
+                               Figures.drawGabor(ref img2, isize / 8, 1 / lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * -tfreq / 60);\r
+                               Figures.drawGabor(ref img3, isize / 8, 1 / lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * tfreq / 60);\r
 \r
-                               //Figures.drawGabor(ref img, 20, 100, 1, 0, frames * 2.0 * Math.PI / tfreq / 60);\r
-                               //Figures.drawGaussian(ref img, 20, 1);\r
-                               //Figures.drawGrating(ref img, 200, 200, 20, 1, 2, frames * 2.0 * Math.PI / tfreq / 60);\r
-                               img.centering(Mouse.position);\r
-                               img.draw();\r
-                               //g.centering(Mouse.position).draw();\r
-                               //g.rotation += 1;\r
 \r
-                               shape.centering(Mouse.position).shift(100, 0);\r
-                               shape.draw();\r
-                               if (Keyboard.spc.pressed()) cnvs.var(Mouse.x, 100, 100);\r
+                //img.centering().shift(0, -isize * 1.5).draw();\r
+                //img2.centering().draw();\r
+                //img3.centering().shift(0, isize * 1.5).draw();\r
 \r
-                               cnvs.var(frames, 0, 20);\r
-                               slider.value = frames / 100.0;\r
-\r
-                               /*if (frames % 2 != 0)\r
-                               {\r
-                                       fixation.shift(100, 100);\r
-                                       fixation.draw(new Stroke(Color.green, 3));\r
-                               }*/\r
 \r
-                               for (int i = 0; i < dots.cood.Length; i++)\r
+                               foreach (var g in gabor1)\r
                                {\r
-                                       dots.cood[i].set(Math.random(500), Math.random(500));\r
+                                       g.wavelength = lambda;\r
+                                       g.phase = (double)frames * 2.0 * Math.PI * tfreq / 60;\r
+                                       g.contrast = contrast;\r
+                                       g.draw();\r
                                }\r
-                               dots.draw();\r
+                \r
+                if (!Mouse.left.pressed()) frames++;\r
 \r
-                               cnvs.flip();\r
-                       }\r
-               }\r
-       }\r
+                cnvs.flip();\r
+            }\r
+        }\r
+    }\r
 }\r
-\r