OSDN Git Service

12
authorHOSOKAWA Kenchi <hskwk@users.sourceforge.jp>
Tue, 3 Apr 2012 15:22:53 +0000 (00:22 +0900)
committerHOSOKAWA Kenchi <hskwk@users.sourceforge.jp>
Tue, 3 Apr 2012 15:22:53 +0000 (00:22 +0900)
dev5/psychlops/core/math/matrix.cs
test5/PsychlopsMain.cs

index de4a836..4d011e5 100644 (file)
@@ -6,6 +6,11 @@ namespace Psychlops
 \r
        public abstract class Matrix\r
        {\r
+               public static Matrix gen(int row, int col)\r
+               {\r
+                       return new Internal.MatrixImplementation(row, col);\r
+               }\r
+\r
                public abstract double this[int row, int col]\r
                {\r
                        get;\r
@@ -26,6 +31,16 @@ namespace Psychlops
                        return new Internal.MatrixExpression();\r
                }\r
                */\r
+               public void each(Func<double, double> f)\r
+               {\r
+                       for (int i = 1; i <= getRows(); i++)\r
+                       {\r
+                               for (int j = 1; j <= getCols(); j++)\r
+                               {\r
+                                       this[i, j] = f(this[i, j]);\r
+                               }\r
+                       }\r
+               }\r
 \r
                public abstract int rows { get; }\r
                public abstract int cols { get; }\r
index f1d7bb8..4242429 100644 (file)
@@ -1,4 +1,143 @@
-//Psychlops Code Template\r
+// Motion with glass patterns.\r
+// Ross, J., Badcock, D. R., and Hayes, A. (2000)\r
+// Coherent global motion in the absence of coherent velocity signals.\r
+// Current Biology, 10, 679-682.\r
+///+ Prefix\r
+//// Include Psychlops Package\r
+using Psychlops;\r
+\r
+namespace PsychlopsSilverlightApp\r
+{\r
+\r
+       public class PsychlopsMain\r
+       {\r
+               Psychlops.Widgets.Slider distance, duration, stimulus_type;\r
+               ///- Prefix\r
+\r
+               public void psychlops_main()\r
+               {\r
+                       GlassPattern();\r
+               }\r
+\r
+               readonly int DOTNUM = 512;\r
+\r
+               ///+ Stimulus drawing function\r
+               //// A function for stimulus drawing (main body)\r
+               void GlassPattern() {\r
+\r
+                       ///+ Preperation\r
+                       //// Declare and initialize local variables\r
+                       Canvas cnvs = new Canvas(400,400); //Prepare drawing window\r
+                       double dotsize = 2;\r
+                       double dot_lum  = 0.5;\r
+                       double bg_lum    = 0.2;\r
+                       double fieldsize = 300;\r
+                       double[] orientation = new double[DOTNUM];\r
+                       Rectangle dots = new Rectangle(dotsize, dotsize);\r
+                       ///+ prepare dots position\r
+                       //// prepare dots position and paired orientation\r
+                       Matrix positionmat = Matrix.gen(DOTNUM,2);\r
+                       //Math.random(positionmat, -0.5*fieldsize, 0.5*fieldsize);\r
+                       positionmat.each( (v) => Math.random(-0.5*fieldsize, 0.5*fieldsize) );\r
+\r
+                       \r
+                       for(int i=0; i<DOTNUM; i++){orientation[i] = Math.atan2(positionmat[i+1,2], positionmat[i+1,1]);}\r
+                       ///- prepare dots position\r
+\r
+\r
+                       ///+ Draw Gaussian Envelope\r
+                       ////Draw offscreen Gaussian Envelope\r
+                       Image envelope = new Image(fieldsize+100, fieldsize+100);\r
+                       envelope.clear(new Color(bg_lum)); //clear offscreen image\r
+                       double _x, _y;\r
+                       for(int i=0; i<fieldsize+100; i++){\r
+                               _x=i-0.5*fieldsize-50;\r
+                               for(int j=0; j<fieldsize+100; j++){\r
+                                       _y=j-0.5*fieldsize-50;\r
+                                       envelope.alpha(i,j,1.0-Math.exp(-((_x*_x+_y*_y)/ (2.0*Math.pow(fieldsize/6.0, 2.0) ))));\r
+                               }\r
+                       }\r
+                       envelope.cache();\r
+                       ///- Draw Gaussian Envelope\r
+                       ///+ set Independent\r
+                       //// set variables and value ranges for interaction\r
+                       Interval rng = new Interval();\r
+                       stimulus_type = new Psychlops.Widgets.Slider("Stimulus Type", 0.0 <= rng <= 2.0, 1, 1);\r
+                       stimulus_type.setValue(0);\r
+                       distance = new Psychlops.Widgets.Slider("distance between pairs", 1 <= rng <= 100, 1, 1);\r
+                       distance.setValue(3.0);\r
+                       duration = new Psychlops.Widgets.Slider("Refresh", 0.0 <= rng <= 10.0, 1, 1);\r
+                       duration.setValue(10);\r
+                       ///- set Independent\r
+\r
+                       int frame = 0;\r
+                       while(!Keyboard.esc.pushed()) {\r
+                               ///+ position change\r
+                               //// Re-randomize position with a desingated interval\r
+                               if(frame > duration)\r
+                               {\r
+                                       //Math.random(positionmat, -0.5 * fieldsize, 0.5 * fieldsize);\r
+                                       positionmat.each((v) => Math.random(-0.5 * fieldsize, 0.5 * fieldsize));\r
+                                       for (int i = 0; i < DOTNUM; i++) { orientation[i] = Math.atan2(positionmat[i + 1, 2], positionmat[i + 1, 1]); }\r
+                                       frame = 0;\r
+                               }\r
+                               ///- position change\r
+                               Display.clear(new Color(bg_lum)); //Clear window\r
+\r
+                               //// Draw DOTNUM pairs of dots\r
+                               for(int i=0; i<DOTNUM; i++){\r
+                                       ///+ draw first dots\r
+\r
+                                       ///+ switch stimulus type\r
+                                       //// draw the second dots to generate selected stimulu type.\r
+                                       switch((int)stimulus_type){\r
+                                               ///+ case rotation\r
+                                               //// Rotation\r
+                                               case 0:\r
+                                               dots.centering().shift(positionmat[i+1,1],positionmat[i+1,2]); // move to center position\r
+                                               dots.shift(-0.5*distance*Math.cos(orientation[i]+0.5*Math.PI),-0.5*distance*Math.sin(orientation[i]+0.5*Math.PI));// move to the first position\r
+                                               dots.draw(new Color(dot_lum)); // draw first dot\r
+                                               dots.shift(distance*Math.cos(orientation[i]+0.5*Math.PI),distance*Math.sin(orientation[i]+0.5*Math.PI));// move to the second position\r
+                                               //cnvs.msg("Rotation", cnvs.getCenter().x, cnvs.getHeight() - 20);\r
+                                               break;\r
+                                               ///- case rotation\r
+                                               ///+ case diversion\r
+                                               ////Diversion\r
+                                               case 1:\r
+                                               dots.centering().shift(positionmat[i+1,1],positionmat[i+1,2]);// move to center position\r
+                                               dots.shift(-0.5*distance*Math.cos(orientation[i]+0.5*Math.PI),-0.5*distance*Math.sin(orientation[i]+0.5*Math.PI));// move to the first position\r
+                                               dots.draw(new Color(dot_lum));// draw first dot\r
+                                               dots.shift(distance*Math.cos(orientation[i]),distance*Math.sin(orientation[i]));// move to the second position\r
+                                               break;\r
+                                               ///- case diversion\r
+                                               ///+ case spiral\r
+                                               //// Spiral\r
+                                               case 2:\r
+                                               dots.centering().shift(positionmat[i+1,1],positionmat[i+1,2]);// move to center position\r
+                                               dots.shift(-0.5*distance*Math.cos(orientation[i]+0.5*Math.PI),-0.5*distance*Math.sin(orientation[i]+0.5*Math.PI));// move to the first position\r
+                                               dots.draw(new Color(dot_lum));// draw first dot\r
+                                               dots.shift(distance*Math.cos(orientation[i]+0.25*Math.PI),distance*Math.sin(orientation[i]+0.25*Math.PI));// move to the second position\r
+                                               break;\r
+                                               ///- case spiral\r
+                                       }\r
+                                       ///- switch stimulus type\r
+\r
+                                       dots.draw(new Color(dot_lum)); // Draw dots at a desinated position.\r
+                               }\r
+                               ///- Draw dots\r
+                               envelope.centering().draw(); //Draw offscreen Gaussian onto screen\r
+                               Display.flip(); // Flip frame buffers\r
+                               frame++;\r
+                       }\r
+               }\r
+       }\r
+}\r
+\r
+\r
+\r
+\r
+/*\r
+ * //Psychlops Code Template\r
 //    Please visit following web site to get sample codes.\r
 //    http://psychlops.sourceforge.jp/ja/?StartCode\r
 //    CodeDresser at following address is also available to view the code.\r
@@ -117,6 +256,8 @@ namespace PsychlopsSilverlightApp
 \r
 }\r
 \r
+*/\r
+\r
 /*\r
                public void psychlops_main() {\r
 \r