Canguro.Commands.View.ZoomAll.Run C# (CSharp) Method

Run() public method

Executuion method for this command
public Run ( Canguro activeView ) : void
activeView Canguro The view to zoom
return void
        public override void Run(Canguro.View.GraphicView activeView)
        {
            /// Get the joint list
            ItemList<Joint> jList = Canguro.Model.Model.Instance.JointList;

            // We need two vectors for having mininum and maximum BB corners
            Vector3 min = new Vector3(-1, -1, -1);
            Vector3 max = new Vector3(1, 1, 1);

            /// Get maximum and minimun from joint list
            foreach (Joint j in jList)
            {
                if (j != null && j.IsVisible )
                {
                    Vector3 pos = j.Position;
                    min.X = (min.X > pos.X) ? pos.X : min.X;
                    min.Y = (min.Y > pos.Y) ? pos.Y : min.Y;
                    min.Z = (min.Z > pos.Z) ? pos.Z : min.Z;
                    max.X = (max.X < pos.X) ? pos.X : max.X;
                    max.Y = (max.Y < pos.Y) ? pos.Y : max.Y;
                    max.Z = (max.Z < pos.Z) ? pos.Z : max.Z;
                }
            }

            /// Get line list
            ItemList<LineElement> lineList = Canguro.Model.Model.Instance.LineList;
            /// When all joints are invisible, we cannot compute a BB, so lets check against line joints
            foreach (LineElement l in lineList)
            {
                if (l != null && l.IsVisible)
                {
                    Vector3 pos = l.I.Position;
                    min.X = (min.X > pos.X) ? pos.X : min.X;
                    min.Y = (min.Y > pos.Y) ? pos.Y : min.Y;
                    min.Z = (min.Z > pos.Z) ? pos.Z : min.Z;
                    max.X = (max.X < pos.X) ? pos.X : max.X;
                    max.Y = (max.Y < pos.Y) ? pos.Y : max.Y;
                    max.Z = (max.Z < pos.Z) ? pos.Z : max.Z;

                    pos = l.J.Position;
                    min.X = (min.X > pos.X) ? pos.X : min.X;
                    min.Y = (min.Y > pos.Y) ? pos.Y : min.Y;
                    min.Z = (min.Z > pos.Z) ? pos.Z : min.Z;
                    max.X = (max.X < pos.X) ? pos.X : max.X;
                    max.Y = (max.Y < pos.Y) ? pos.Y : max.Y;
                    max.Z = (max.Z < pos.Z) ? pos.Z : max.Z;
                }
            }
            if (min.X < max.X)
            {
                // Diagonal de bounding box
                Vector3 diagonal = max - min;
                Vector3 center = min + Vector3.Multiply(diagonal, 0.5f);
                float modelSize = Vector3.Length(diagonal);
                float screenSize = (float)Math.Min(activeView.Viewport.Height-20, activeView.Viewport.Width-20);

                /************ Codigo genial **************
                 * Calculo de escala
                 * Calculo de traslacion
                 *****************************************/

                // Translation to bounding box center
                Vector3 centerProjected = center;
                activeView.Project(ref centerProjected);

                MouseEventArgs e = new MouseEventArgs(MouseButtons.Left, 1, (int)centerProjected.X, (int)centerProjected.Y, 0);
                activeView.ArcBallCtrl.OnBeginPan(e);

                e = new MouseEventArgs(MouseButtons.Left, 1, activeView.Viewport.Width/2, activeView.Viewport.Height/2, 0);
                activeView.ArcBallCtrl.OnMovePan(e);

                activeView.ViewMatrix = activeView.ArcBallCtrl.ViewMatrix;

                // View volume scaling

                Vector2[] maxDims = findMaxPoints(min, max, activeView);
                float sizeX = maxDims[1].X - maxDims[0].X;
                float sizeY = maxDims[1].Y - maxDims[0].Y;

                float newScale = 0.0f;

                newScale  = screenSize * activeView.ArcBallCtrl.ScalingFac / (float)Math.Max(sizeX, sizeY);

                activeView.ArcBallCtrl.ZoomAbsolute(newScale);
                activeView.ViewMatrix = activeView.ArcBallCtrl.ViewMatrix;
            }
        }