Exemplo n.º 1
0
        private void SetExampleScene(ILPanel panel)
        {
            ILScene scene = new ILScene();

            try {
                ILLabel.DefaultFont = new System.Drawing.Font("Helvetica", 8);
                //ilPanel1.Driver = RendererTypes.GDI;

                #region upper left plot
                // prepare some data
                ILArray <float> P = 1,
                                x = ILMath.linspace <float>(-2, 2, 40),
                                y = ILMath.linspace <float>(2, -2, 40);

                ILArray <float> F = ILMath.meshgrid(x, y, P);
                // a simple RBF
                ILArray <float> Z = ILMath.exp(-(1.2f * F * F + P * P));
                // surface expects a single matrix
                Z[":;:;2"] = F; Z[":;:;1"] = P;

                // add a plot cube
                var pc = scene.Add(new ILPlotCube {
                    // shrink viewport to upper left quadrant
                    ScreenRect = new RectangleF(0.05f, 0, 0.4f, 0.5f),
                    // 3D rotation
                    TwoDMode = false,
                    Children =
                    {
                        // add surface
                        new ILSurface(Z)
                        {
                            // disable mouse hover marking
                            Fill      = { Markable = false },
                            Wireframe ={ Markable                 = false },
                            // make it shiny
                            UseLighting = true,
                            Children    = { new ILColorbar() }
                        },
                        //ILLinePlot.CreateXPlots(Z["1:10;:;0"], markers: new List<MarkerStyle>() {
                        //    MarkerStyle.None,MarkerStyle.None,MarkerStyle.None,MarkerStyle.None,MarkerStyle.Circle, MarkerStyle.Cross, MarkerStyle.Plus, MarkerStyle.TriangleDown }),
                        //new ILLegend("hi","n","ku","zs","le", "blalblalblalblalb\\color{red} hier gehts rot")
                    },
                    Rotation = Matrix4.Rotation(new Vector3(1.1f, -0.4f, -0.69f), 1.3f)
                });

                #endregion

                #region top right plot
                // create a gear shape
                var gear = new ILGear(toothCount: 30, inR: 0.5f, outR: 0.9f)
                {
                    Fill = { Markable = false, Color = Color.DarkGreen }
                };
                // group with right clipping plane
                var clipgroup = new ILGroup()
                {
                    Clipping = new ILClipParams()
                    {
                        Plane0 = new Vector4(1, 0, 0, 0)
                    },
                    Children =
                    {
                        // a camera holding the (right) clipped gear
                        new ILCamera()
                        {
                            // shrink viewport to upper top quadrant
                            ScreenRect = new RectangleF(0.5f, 0, 0.5f, 0.5f),
                            // populate interactive changes back to the global scene
                            IsGlobal = true,
                            // adds the gear to the camera
                            Children ={ gear                },
                            Position = new Vector3(0, 0, -15)
                        }
                    }
                };
                // setup the scene
                var gearGroup = scene.Add(new ILGroup {
                    clipgroup, clipgroup // <- second time: group is cloned
                });

                gearGroup.First <ILCamera>().Parent.Clipping = new ILClipParams()
                {
                    Plane0 = new Vector4(-1, 0, 0, 0)
                };
                // make the left side transparent green
                gearGroup.First <ILTriangles>().Color = Color.FromArgb(100, Color.Green);

                // synchronize both cameras; source: left side
                gearGroup.First <ILCamera>().PropertyChanged += (s, arg) => {
                    gearGroup.Find <ILCamera>().ElementAt(1).CopyFrom(s as ILCamera, false);
                };
                #endregion

                #region left bottom plot
                // start value
                int nrBalls = 10; bool addBalls = true;
                var balls = new ILPoints("balls")
                {
                    Positions = ILMath.tosingle(ILMath.randn(3, nrBalls)),
                    Colors    = ILMath.tosingle(ILMath.rand(3, nrBalls)),
                    Color     = null,
                    Markable  = false
                };
                var leftBottomCam = scene.Add(new ILCamera {
                    ScreenRect = new RectangleF(0, 0.5f, 0.5f, 0.5f),
                    Projection = Projection.Perspective,
                    Children   = { balls }
                });
                // funny label
                string harmony    = @"\color{red}H\color{blue}a\color{green}r\color{yellow}m\color{magenta}o\color{cyan}n\color{black}y\reset
";
                var    ballsLabel = scene.Add(new ILLabel(tag: "harmony")
                {
                    Text     = harmony,
                    Fringe   = { Color = Color.FromArgb(240, 240, 240) },
                    Position = new Vector3(-0.75f, -0.25f, 0)
                });
                long   oldFPS          = 1;
                PointF currentMousePos = new PointF();
                // setup the swarm. Start with a few balls, increase number
                // until framerate drops below 60 fps.
                ILArray <float> velocity = ILMath.tosingle(ILMath.randn(3, nrBalls));
                EventHandler <ILRenderEventArgs> updateBallsRenderFrame = (s, arg) => {
                    // transform viewport coords into 3d scene coords
                    Vector3 mousePos = new Vector3(currentMousePos.X * 2 - 1,
                                                   currentMousePos.Y * -2 + 1, 0);
                    // framerate dropped? -> stop adding balls
                    if (panel.FPS < oldFPS && panel.FPS < 60)
                    {
                        addBalls = false;
                    }
                    oldFPS = panel.FPS;
                    Computation.UpdateBalls(mousePos, balls, velocity, addBalls);
                    // balls buffers have been changed -> must call configure() to publish
                    balls.Configure();
                    // update balls label
                    ballsLabel.Text = harmony + "(" + balls.Positions.DataCount.ToString() + " balls)";
                };

                // saving the mouse position in MouseMove is easier for
                // transforming the coordinates into the viewport
                leftBottomCam.MouseMove += (s, arg) => {
                    // save the mouse position
                    currentMousePos = arg.LocationF;
                };
                panel.BeginRenderFrame += updateBallsRenderFrame;
                m_cleanUpExample        = () => {
                    leftBottomCam.MouseMove -= (s, arg) => {
                        // save the mouse position
                        currentMousePos = arg.LocationF;
                    };
                    panel.BeginRenderFrame -= updateBallsRenderFrame;
                };
                #endregion

                panel.Scene = scene;
            } catch (Exception exc) {
                System.Diagnostics.Trace.WriteLine("ILPanel_Load Error:");
                System.Diagnostics.Trace.WriteLine("====================");
                System.Diagnostics.Trace.WriteLine(exc.ToString());
                MessageBox.Show(exc.ToString());
            }
        }