From a18f323a3bf447b6f2ce12d0d1a4f39a07814e93 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 12 Aug 2010 00:12:58 +0900 Subject: [PATCH] 123 --- dev4/psychlops/core/graphic/canvas.cs | 308 ++++++++++++++++++++++++++++------ dev4/psychlops/core/graphic/module.cs | 35 +++- test4/MainPage.xaml.cs | 1 + test4/PsychlopsMain.cs | 38 +++-- 4 files changed, 314 insertions(+), 68 deletions(-) diff --git a/dev4/psychlops/core/graphic/canvas.cs b/dev4/psychlops/core/graphic/canvas.cs index 5a1a7c9..fdc3987 100644 --- a/dev4/psychlops/core/graphic/canvas.cs +++ b/dev4/psychlops/core/graphic/canvas.cs @@ -34,19 +34,22 @@ namespace Psychlops internal int lettersStackN; internal Image[] imageStack; internal int imageStackN; + internal Group[] groupStack; + internal int groupStackN; public StackableDrawable() { // stack = new System.Collections.Generic.Queue(); - stack = new Internal.PrimitiveFigure[40000]; - lineStack = new Line[10000]; - rectStack = new Rectangle[10000]; - ellipseStack = new Ellipse[10000]; + stack = new Internal.PrimitiveFigure[10000]; + lineStack = new Line[2000]; + rectStack = new Rectangle[2000]; + ellipseStack = new Ellipse[2000]; polygonStack = new Polygon[1000]; lettersStack = new Letters[1000]; - imageStack = new Image[1000]; - for (int i = 0; i < 10000; i++) + imageStack = new Image[100]; + groupStack = new Group[100]; + for (int i = 0; i < 2000; i++) { lineStack[i] = new Line(0,0,0,0); rectStack[i] = new Rectangle(); @@ -56,7 +59,11 @@ namespace Psychlops { polygonStack[i] = new Polygon(); lettersStack[i] = new Letters(); - imageStack[i] = new Image(1,1); + } + for (int i = 0; i < 100; i++) + { + imageStack[i] = new Image(1, 1); + groupStack[i] = new Group(); } } @@ -139,11 +146,12 @@ namespace Psychlops internal SolidColorBrush[] brushPool; internal int brushPoolN; - internal System.Windows.UIElement[] UIElementPool; + internal System.Windows.Controls.Canvas[] UIElementPool; internal int UIElementPoolN; internal System.Windows.Shapes.Line[] linePool; internal int linePoolN; + internal System.Windows.Shapes.Rectangle[] dummyRectPool; internal System.Windows.Shapes.Rectangle[] rectPool; internal int rectPoolN; internal System.Windows.Shapes.Ellipse[] ellipsePool; @@ -154,6 +162,8 @@ namespace Psychlops internal int lettersPoolN; internal System.Windows.Controls.Image[] imagePool; internal int imagePoolN; + internal System.Windows.Controls.Canvas[] groupPool; + internal int groupPoolN; #region initializer @@ -171,6 +181,7 @@ namespace Psychlops System.Windows.Controls.UserControl panel; Rectangle back_panel; double width_, height_; + Clock before; public Canvas(int wid, int hei) { @@ -188,6 +199,8 @@ namespace Psychlops protected bool AsyncInitBool; protected void initialize(int wid, int hei) { + before = new Clock(); + before.update(); AsyncInitBool = false; width_ = wid; height_ = hei; @@ -207,7 +220,7 @@ namespace Psychlops protected void initialize__(int wid, int hei) { api_canvas.Width = wid; - api_canvas.Height = hei; + api_canvas.Height = hei+20; api_canvas.MouseMove += Mouse.Canvas_MousePos; api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown; api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp; @@ -221,18 +234,21 @@ namespace Psychlops htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px"); htmlHost.SetStyleAttribute("margin", "2em auto auto auto"); - UIElementPool = new System.Windows.UIElement[10000]; - pointPool = new System.Windows.Point[10000]; - brushPool = new SolidColorBrush[10000]; - linePool = new System.Windows.Shapes.Line[10000]; - rectPool = new System.Windows.Shapes.Rectangle[10000]; - ellipsePool = new System.Windows.Shapes.Ellipse[10000]; - for (int i = 0; i < 10000; i++) + UIElementPool = new System.Windows.Controls.Canvas[2000]; + pointPool = new System.Windows.Point[2000]; + brushPool = new SolidColorBrush[2000]; + linePool = new System.Windows.Shapes.Line[2000]; + rectPool = new System.Windows.Shapes.Rectangle[2000]; + dummyRectPool = new System.Windows.Shapes.Rectangle[2000]; + ellipsePool = new System.Windows.Shapes.Ellipse[2000]; + for (int i = 0; i < 2000; i++) { + UIElementPool[i] = new System.Windows.Controls.Canvas(); pointPool[i] = new System.Windows.Point(); brushPool[i] = new SolidColorBrush(); linePool[i] = new System.Windows.Shapes.Line(); rectPool[i] = new System.Windows.Shapes.Rectangle(); + dummyRectPool[i] = new System.Windows.Shapes.Rectangle(); ellipsePool[i] = new System.Windows.Shapes.Ellipse(); } polygonPool = new System.Windows.Shapes.Polygon[1000]; @@ -243,9 +259,11 @@ namespace Psychlops lettersPool[i] = new System.Windows.Controls.TextBlock(); } imagePool = new System.Windows.Controls.Image[100]; + groupPool = new System.Windows.Controls.Canvas[100]; for (int i = 0; i < 100; i++) { imagePool[i] = new System.Windows.Controls.Image(); + groupPool[i] = new System.Windows.Controls.Canvas(); } masterPool = new System.Windows.Controls.Canvas(); @@ -253,12 +271,11 @@ namespace Psychlops api_canvas.Children.Add(masterPool); //api_canvas.Children.Remove(Internal.Main.widgetStack); - /* - for (int i = 0; i < 10000; i++) + for (int i = 0; i < 2000; i++) { masterPool.Children.Add(UIElementPool[i]); + UIElementPool[i].Children.Add(dummyRectPool[i]); } - * */ AsyncInitBool = true; @@ -305,13 +322,9 @@ namespace Psychlops int nextIntervalFrame = 1, chacked = 0; public void flip(int n) { - flip(); - } - public void flip() - { lock (this) { - nextIntervalFrame = 1; + nextIntervalFrame = n; chacked = 1; } //pointStackN = 0; @@ -322,7 +335,9 @@ namespace Psychlops ellipseStackN = 0; lettersStackN = 0; imageStackN = 0; + groupStackN = 0; + /* pointPoolN = 0; brushPoolN = 0; linePoolN = 0; @@ -331,10 +346,167 @@ namespace Psychlops polygonPoolN = 0; lettersPoolN = 0; imagePoolN = 0; + groupPoolN = 0; + * */ + //executeFlip(); Internal.Main.canvas_flag.WaitOne(); } + public void flip() + { + flip(1); + } + + + #region version modifyNative2 + public void executeFlip() + { + Clock after = new Clock(); + after.update(); + AppState.statusBar = ((after - before).at_msec().ToString()) + " msec"; + + Line lineS; + Rectangle rectS; + Ellipse ellipseS; + Polygon polygonS; + Letters lettersS; + Image imageS; + Group groupS; + System.Windows.Shapes.Line lineP; + System.Windows.Shapes.Rectangle rectP; + System.Windows.Shapes.Ellipse ellipseP; + System.Windows.Shapes.Polygon polygonP; + System.Windows.Controls.TextBlock lettersP; + System.Windows.Controls.Image imageP; + System.Windows.Controls.Canvas groupP; + + lock (this) + { + nextIntervalFrame--; + } + + + + var en = masterPool.Children.GetEnumerator(); + bool full = en.MoveNext(); + var cnv = en.Current as System.Windows.Controls.Canvas; + if (nextIntervalFrame <= 0) + { + if (chacked > 0) + { + if (stackN > 0) + { + for (int i = 0; i < stackN - 2; i++) + { + if (full == false) + { + break; + } + else + { + if( null != (rectS = stack[i] as Rectangle) ) + { + if (null != (rectP = cnv.Children[0] as System.Windows.Shapes.Rectangle)) + { + rectS.modifyNative(rectP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (lineS = stack[i] as Line)) + { + if (null != (lineP = cnv.Children[0] as System.Windows.Shapes.Line)) + { + lineS.modifyNative(lineP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (ellipseS = stack[i] as Ellipse)) + { + if (null != (ellipseP = cnv.Children[0] as System.Windows.Shapes.Ellipse)) + { + ellipseS.modifyNative(ellipseP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (polygonS = stack[i] as Polygon)) + { + if (null != (polygonP = cnv.Children[0] as System.Windows.Shapes.Polygon)) + { + polygonS.modifyNative(polygonP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (lettersS = stack[i] as Letters)) + { + if (null != (lettersP = cnv.Children[0] as System.Windows.Controls.TextBlock)) + { + lettersS.modifyNative(lettersP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (imageS = stack[i] as Image)) + { + if (null != (imageP = cnv.Children[0] as System.Windows.Controls.Image)) + { + imageS.modifyNative(imageP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (groupS = stack[i] as Group)) + { + if (null != (groupP = cnv.Children[0] as System.Windows.Controls.Canvas)) + { + groupS.modifyNative(groupP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + full = en.MoveNext(); + cnv = en.Current as System.Windows.Controls.Canvas; + } + } + stackN = 0; + } + lock (this) + { + chacked = 0; + } + Psychlops.Internal.Main.canvas_flag.Set(); + } + } + System.Threading.Thread.Sleep(0); + } + #endregion + #region version modifyNative + /* public void executeFlip() { Line lineS; @@ -343,12 +515,14 @@ namespace Psychlops Polygon polygonS; Letters lettersS; Image imageS; + Group groupS; System.Windows.Shapes.Line lineP; System.Windows.Shapes.Rectangle rectP; System.Windows.Shapes.Ellipse ellipseP; System.Windows.Shapes.Polygon polygonP; System.Windows.Controls.TextBlock lettersP; System.Windows.Controls.Image imageP; + System.Windows.Controls.Canvas groupP; lock (this) { @@ -392,6 +566,10 @@ namespace Psychlops { ellipseS.modifyNative(ellipseP, this); } + else + { + masterPool.Children.Add(stack[i].poolNative(this)); + } } else if (null != (polygonS = stack[i] as Polygon)) { @@ -414,6 +592,13 @@ namespace Psychlops imageS.modifyNative(imageP, this); } } + else if (null != (groupS = stack[i] as Group)) + { + if (null != (groupP = en.Current as System.Windows.Controls.Canvas)) + { + groupS.modifyNative(groupP, this); + } + } full = en.MoveNext(); } } @@ -428,10 +613,11 @@ namespace Psychlops } System.Threading.Thread.Sleep(0); } + * */ #endregion - /* #region version poolNative 2 + /* public void executeFlip() { @@ -471,8 +657,8 @@ namespace Psychlops } System.Threading.Thread.Sleep(0); } - #endregion */ + #endregion #region Properties @@ -936,7 +1122,7 @@ namespace Psychlops System.Windows.Controls.Canvas.SetTop(tmp, datum.y); tmp.Visibility = Visibility.Visible; d.imagePoolN++; - return this; + return tmp; } public void modifyNative(System.Windows.Controls.Image tmp, Canvas d) { @@ -958,59 +1144,75 @@ namespace Psychlops internal void create__() { cnvs = new System.Windows.Controls.Canvas(); + trans = new System.Windows.Media.TransformGroup(); + transF = new System.Windows.Media.TransformCollection(); + rotateF = new System.Windows.Media.RotateTransform(); + scaleF = new System.Windows.Media.ScaleTransform(); + translateF = new System.Windows.Media.TranslateTransform(); + transF.Add(rotateF); + transF.Add(scaleF); + transF.Add(translateF); + trans.Children = transF; + cnvs.RenderTransform = trans; AsyncBool = true; } - delegate void AppendFunc1(System.Func func); - public void append__(Internal.PrimitiveFigure fig) + public Group clone() { - cnvs.Children.Add(fig.toNative()); + return (Group)MemberwiseClone(); } - public Group clone() + delegate void AppendFunc1(Internal.PrimitiveFigure func); + void append__(Internal.PrimitiveFigure fig) { - return (Group)MemberwiseClone(); + fig.centering(0, 0); + UIElement e = fig.toNative(); + cnvs.Children.Add(e); + System.Windows.Controls.Canvas.SetLeft(e, fig.datum.x); + System.Windows.Controls.Canvas.SetTop(e, fig.datum.y); } + delegate void SimpleProcedure(); + void getRotation__() { rotation_ = rotateF.Angle; } + void setRotation__() { rotateF.Angle = rotation_; } + //void getTranslation__() { rotation_ = rotateF.Angle; } + void setTranslation__() { translateF.X = datum.x; translateF.Y = datum.y; } + void setScaling__() { scaleF.ScaleX = scaling_.x; scaleF.ScaleY = scaling_.y; } public static implicit operator System.Windows.Controls.Canvas(Group d) { - var tmp = new System.Windows.Controls.Canvas(); -/* foreach (var item in list) - { - tmp.Children.Add(d.); - } - System.Windows.Controls.Canvas.SetLeft(tmp, d.datum.x); - System.Windows.Controls.Canvas.SetTop(tmp, d.datum.y); -*/ return tmp; + var tmp = d.cnvs;//new System.Windows.Controls.Canvas(); + System.Windows.Controls.Canvas.SetLeft(d.cnvs, d.datum.x); + System.Windows.Controls.Canvas.SetTop(d.cnvs, d.datum.y); + return tmp; } public UIElement toNative() { return this; } public void copyToStack(Templates.StackableDrawable d) { -/* var tmp = d.imageStack[d.imageStackN]; + var tmp = d.groupStack[d.groupStackN]; tmp.datum = datum; - tmp.buffer = buffer; - tmp.self_rect = self_rect; + tmp.cnvs = cnvs; d.stack[d.stackN] = tmp; - d.imageStackN++; + d.groupStackN++; d.stackN++; -*/ } + } public UIElement poolNative(Canvas d) { -/* var tmp = d.imagePool[d.imagePoolN]; - tmp.Source = buffer; + //d.groupPool[d.groupPoolN] = cnvs; + //var tmp = d.groupPool[d.groupPoolN]; + var tmp = cnvs; System.Windows.Controls.Canvas.SetLeft(tmp, datum.x); System.Windows.Controls.Canvas.SetTop(tmp, datum.y); tmp.Visibility = Visibility.Visible; - d.imagePoolN++; -*/ return this; + //d.groupPoolN++; + return tmp; } public void modifyNative(System.Windows.Controls.Canvas tmp, Canvas d) { -/* tmp.Source = buffer; - System.Windows.Controls.Canvas.SetLeft(tmp, datum.x); - System.Windows.Controls.Canvas.SetTop(tmp, datum.y); + d.groupPool[d.groupPoolN] = cnvs; + System.Windows.Controls.Canvas.SetLeft(cnvs, datum.x); + System.Windows.Controls.Canvas.SetTop(cnvs, datum.y); tmp.Visibility = Visibility.Visible; -*/ } + } } diff --git a/dev4/psychlops/core/graphic/module.cs b/dev4/psychlops/core/graphic/module.cs index b899cae..db366c1 100644 --- a/dev4/psychlops/core/graphic/module.cs +++ b/dev4/psychlops/core/graphic/module.cs @@ -147,23 +147,50 @@ namespace Psychlops { System.Collections.Generic.List
list; System.Windows.Controls.Canvas cnvs; - AppendFunc1 append_; + System.Windows.Media.TransformGroup trans; + System.Windows.Media.TransformCollection transF; + System.Windows.Media.RotateTransform rotateF; + SimpleProcedure setRotation_; + System.Windows.Media.ScaleTransform scaleF; + SimpleProcedure setScaling_; + System.Windows.Media.TranslateTransform translateF; + bool AsyncBool; - public double rotation; - Point axis, scaling; + double rotation_; + public double rotation + { + get { return rotation_; } + set { rotation_ = value; rotateF.Dispatcher.BeginInvoke(setRotation_); } + } + public Point axis + { + get; + set; + } + Point scaling_; + public Point scaling + { + get { return scaling_; } + set { scaling_ = value; scaleF.Dispatcher.BeginInvoke(setScaling_); } + } + + AppendFunc1 append_; public Group() { + setRotation_ = new SimpleProcedure(setRotation__); + setScaling_ = new SimpleProcedure(setScaling__); append_ = new AppendFunc1(append__); list = new System.Collections.Generic.List
(); AsyncBool = false; initialize__(); while (!AsyncBool) { } } + public Group append(Internal.PrimitiveFigure fig) { list.Add(fig); - append_(fig); + cnvs.Dispatcher.BeginInvoke(append_, fig); return this; } diff --git a/test4/MainPage.xaml.cs b/test4/MainPage.xaml.cs index a53999f..685c460 100644 --- a/test4/MainPage.xaml.cs +++ b/test4/MainPage.xaml.cs @@ -23,6 +23,7 @@ namespace PsychlopsSilverlight4test System.Windows.Application.Current.Host.Settings.MaxFrameRate = 60; System.Windows.Application.Current.Host.Settings.EnableFrameRateCounter = true; System.Windows.Application.Current.Host.Settings.EnableCacheVisualization = false; + //System.Windows.Application.Current.Host.Settings.EnableGPUAcceleration = false; // System.Windows.Media.CompositionTarget.Rendering += new System.EventHandler(CompositionTarget_Rendering); //main_routine = new System.ComponentModel.BackgroundWorker(); diff --git a/test4/PsychlopsMain.cs b/test4/PsychlopsMain.cs index d33eb14..e7b6754 100644 --- a/test4/PsychlopsMain.cs +++ b/test4/PsychlopsMain.cs @@ -1,4 +1,4 @@ -using Psychlops; +/*using Psychlops; namespace PsychlopsSilverlight4test { @@ -108,9 +108,9 @@ namespace PsychlopsSilverlight4test } } +*/ -/* -using Psychlops; +/*using Psychlops; namespace PsychlopsSilverlight4test { @@ -159,7 +159,7 @@ namespace PsychlopsSilverlight4test */ -/* + using Psychlops; namespace Psychlops @@ -195,19 +195,24 @@ namespace PsychlopsSilverlight4test { Canvas cnvs; Image img; - Rectangle fixation; + Ellipse fixation; Shape shape; Color col; int isize = 100; double tfreq = 1; int frames; RandomDots dots; + Group g; public void psychlops_main() { cnvs = new Canvas(500, 500); + g = new Group(); img = new Image(isize * 2, isize * 2); - fixation = new Rectangle(10, 10); + Figures.drawGrating(ref img, 200, 200, 20, 1, 2, frames * 2.0 * Math.PI / tfreq / 60); + g.append(img); + g.rotation = 50; + fixation = new Ellipse(10, 10); fixation.fill = Color.red; //var poly = new Rectangle(100, 100); var poly = new Letters("日本語"); @@ -228,17 +233,26 @@ namespace PsychlopsSilverlight4test { frames++; + cnvs.clear(new Color(Mouse.left.pressed() ? 0.75 : 0.5)); col.set(Math.random(1.0)); + /*if (frames % 2 == 0) + { + fixation.centering().shift(100, 100); + fixation.draw(new Stroke(Color.blue, 3)); + }*/ + fixation.centering(); fixation.draw(Color.red); //Figures.drawGabor(ref img, 20, 100, 1, 0, frames * 2.0 * Math.PI / tfreq / 60); //Figures.drawGaussian(ref img, 20, 1); - Figures.drawGrating(ref img, 200, 200, 20, 1, 2, frames * 2.0 * Math.PI / tfreq / 60); + //Figures.drawGrating(ref img, 200, 200, 20, 1, 2, frames * 2.0 * Math.PI / tfreq / 60); img.centering(Mouse.position); img.draw(); + //g.centering(Mouse.position).draw(); + //g.rotation += 1; shape.centering(Mouse.position).shift(100, 0); shape.draw(); @@ -247,8 +261,11 @@ namespace PsychlopsSilverlight4test cnvs.var(frames, 0, 20); slider.value = frames / 100.0; - fixation.shift(100, 100); - fixation.draw(new Stroke(Color.blue, 3)); + /*if (frames % 2 != 0) + { + fixation.shift(100, 100); + fixation.draw(new Stroke(Color.green, 3)); + }*/ for (int i = 0; i < dots.cood.Length; i++) { @@ -260,6 +277,5 @@ namespace PsychlopsSilverlight4test } } } - } -*/ + -- 2.11.0