Exemplo n.º 1
0
        public void RecalcPrimaryDependant(Canguro.View.GraphicView activeView, PointMagnet primaryPoint, LineMagnet[] globalAxes)
        {
            if (primaryPoint != null)
            {
                // Move area to lay on the primaryPoint and to set its direction any canonic
                // plane (X=x, Y=y or Z=z) which is the most paralell to the screen plane
                position = primaryPoint.Position;

                // Get screen plane normal
                Vector3 s0 = screenNormal[0], s1 = screenNormal[1], sNormal;
                activeView.Unproject(ref s0);
                activeView.Unproject(ref s1);
                sNormal = s0 - s1;

                // Assign the area normal to the most paralell canonical plane
                // (giving priority to the Z plane)
                int   maxCosIndex = 2;
                float cosX, cosY, cosZ;
                cosX = Vector3.Dot(sNormal, globalAxes[0].Direction);
                cosY = Vector3.Dot(sNormal, globalAxes[1].Direction);
                cosZ = Vector3.Dot(sNormal, globalAxes[2].Direction);

                if (Math.Abs(cosZ) < minZPlaneAngle)
                {
                    maxCosIndex = (cosX >= cosY) ? ((cosX > cosZ) ? 0 : 2) : ((cosY > cosZ) ? 1 : 2);
                }

                normal = globalAxes[maxCosIndex].Direction;
            }
            else
            {
                position = Vector3.Empty;
                normal   = globalAxes[2].Direction;
            }
        }
Exemplo n.º 2
0
        public override float Snap(Canguro.View.GraphicView activeView, Point mousePoint)
        {
            // Get ray from mouse position
            Vector3 rayP1 = new Vector3(mousePoint.X, mousePoint.Y, 1f);
            Vector3 rayP2 = new Vector3(mousePoint.X, mousePoint.Y, 0f);

            activeView.Unproject(ref rayP1);
            activeView.Unproject(ref rayP2);
            Vector3 ray = rayP2 - rayP1;

            ray.Normalize();

            // Check best plane angle
            Vector3 normalTmp = normal;
            float   cosAngle  = Math.Abs(Vector3.Dot(ray, normal));

            if (cosAngle < 0.03f)
            {
                float xCosAngle = Math.Abs(Vector3.Dot(ray, CommonAxes.GlobalAxes[0]));
                float yCosAngle = Math.Abs(Vector3.Dot(ray, CommonAxes.GlobalAxes[1]));
                float zCosAngle = Math.Abs(Vector3.Dot(ray, CommonAxes.GlobalAxes[2]));

                if (xCosAngle >= yCosAngle)
                {
                    if (xCosAngle > zCosAngle)
                    {
                        normalTmp = CommonAxes.GlobalAxes[0];   // YZ Plane
                    }
                    else
                    {
                        normalTmp = CommonAxes.GlobalAxes[2];   // XY Plane
                    }
                }
                else if (yCosAngle > zCosAngle)
                {
                    normalTmp = CommonAxes.GlobalAxes[1];   // XZ Plane
                }
                else
                {
                    normalTmp = CommonAxes.GlobalAxes[2];   // XY Plane
                }
            }

            float r = Vector3.Dot(position - rayP1, normalTmp) / Vector3.Dot(ray, normalTmp);

            snapPosition = rayP1 + Vector3.Scale(ray, r);

            return(lastSnapFitness = 0f);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Method to return the four bounding planes given two points in screen coordinates
        /// (as in two mouse clicks). A plane can be defined by a point on it and its normal,
        /// which is what is beaing returned for each of the four planes as defined by selectWindow.
        ///
        /// For this, 8 points are calculated, two for each corner, for the front and back plane.
        /// This defines a box. The points are to be defined as pointing towards the screen (i.e. a1 lies
        /// in the middle of the volume and a2 is in the front plane), regardless of the coordinate system
        /// used (left or right handed), and the second point will be at the screen plane.
        /// </summary>
        /// <param name="activeView">The view that's used for calculating the bounding planes</param>
        /// <param name="fx1">Clicked first point, X screen coordinate</param>
        /// <param name="fy1">Clicked first point, Y screen coordinate</param>
        /// <param name="fx2">Clicked second point, X screen coordinate</param>
        /// <param name="fy2">Clicked second point, Y screen coordinate</param>
        /// <param name="nAB">Returns the calculated normal for the plane AB</param>
        /// <param name="sAB">Returns a point on the calculated plane AB</param>
        /// <param name="nBC">Returns the calculated normal for the plane BC</param>
        /// <param name="sBC">Returns a point on the calculated plane BC</param>
        /// <param name="nCD">Returns the calculated normal for the plane CD</param>
        /// <param name="sCD">Returns a point on the calculated plane CD</param>
        /// <param name="nDA">Returns the calculated normal for the plane DA</param>
        /// <param name="sDA">Returns a point on the calculated plane DA</param>
        private void getPlanes(Canguro.View.GraphicView activeView, int fx1, int fy1, int fx2, int fy2,
                               out Vector3 nAB, out Vector3 sAB, out Vector3 nBC, out Vector3 sBC,
                               out Vector3 nCD, out Vector3 sCD, out Vector3 nDA, out Vector3 sDA,
                               ref Vector3[] corners1, ref Vector3[] corners2)
        {
            // First calculate the 8 points (2 for each corner)
            Vector3 a1 = new Vector3(fx1, fy1, 0.5f);
            Vector3 a2 = new Vector3(fx1, fy1, 1);
            Vector3 b1 = new Vector3(fx2, fy1, 0.5f);
            Vector3 b2 = new Vector3(fx2, fy1, 1);
            Vector3 c1 = new Vector3(fx2, fy2, 0.5f);
            Vector3 c2 = new Vector3(fx2, fy2, 1);
            Vector3 d1 = new Vector3(fx1, fy2, 0.5f);
            Vector3 d2 = new Vector3(fx1, fy2, 1);

            activeView.Unproject(ref a1);
            activeView.Unproject(ref a2);
            activeView.Unproject(ref b1);
            activeView.Unproject(ref b2);
            activeView.Unproject(ref c1);
            activeView.Unproject(ref c2);
            activeView.Unproject(ref d1);
            activeView.Unproject(ref d2);

            if (corners1 != null && corners2 != null && corners1.Length == 4 && corners2.Length == 4)
            {
                corners1[0] = a1;
                corners1[1] = b1;
                corners1[2] = c1;
                corners1[3] = d1;

                corners2[0] = a2;
                corners2[1] = b2;
                corners2[2] = c2;
                corners2[3] = d2;
            }

            // Now calculate the normals
            // If points P and Q are consecutive and in CCW order, then
            // normal = (q2 - p1) x (q1 - p1)
            nAB = Vector3.Normalize(Vector3.Cross((b2 - a1), (b1 - a1)));
            nBC = Vector3.Normalize(Vector3.Cross((c2 - b1), (c1 - b1)));
            nCD = Vector3.Normalize(Vector3.Cross((d2 - c1), (d1 - c1)));
            nDA = Vector3.Normalize(Vector3.Cross((a2 - d1), (a1 - d1)));

            // Now assign the points on the planes.
            // Points calculated at the corners are used here
            sAB = a1;
            sBC = c1;
            sCD = c1;
            sDA = a1;
        }
Exemplo n.º 4
0
        public Vector3 GetViewableObjectsCentroid(Canguro.View.GraphicView activeView)
        {
            // Set window equal to viewport to select everything visible
            int fx1 = activeView.Viewport.X;
            int fy1 = activeView.Viewport.Y;
            int fx2 = activeView.Viewport.X + activeView.Viewport.Width;
            int fy2 = activeView.Viewport.Y + activeView.Viewport.Height;

            List <Canguro.Model.Item> crossingItems = new List <Canguro.Model.Item>();

            SelectWindow(activeView, fx1, fy1, fx2, fy2, crossingItems, Canguro.Controller.SelectionFilter.Joints | Canguro.Controller.SelectionFilter.Lines);

            int     centroidPts = 0;
            Vector3 centroid    = Vector3.Empty;

            foreach (Canguro.Model.Item item in crossingItems)
            {
                if (item is Canguro.Model.Joint)
                {
                    centroidPts++;
                    centroid += ((Canguro.Model.Joint)item).Position;
                }
                else if (item is Canguro.Model.LineElement)
                {
                    centroidPts++;
                    centroid += ((Canguro.Model.LineElement)item).I.Position;
                    centroidPts++;
                    centroid += ((Canguro.Model.LineElement)item).J.Position;
                }
            }

            if (centroidPts > 0)
            {
                return(centroid * (1.0f / centroidPts));
            }
            else
            {
                //if nothing is in view, then return the center of the viewing frustum
                Vector3 screenCenter = new Vector3(activeView.Viewport.X + activeView.Viewport.Width / 2, activeView.Viewport.Y + activeView.Viewport.Height / 2, 0.5f);
                activeView.Unproject(ref screenCenter);
                return(screenCenter);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Executes the command.
        /// Copies the selected Items in a series around a given rotation axis.
        /// </summary>
        /// <param name="services">CommandServices object to interact with the system</param>
        public override void Run(Canguro.Controller.CommandServices services)
        {
            Dictionary <Joint, Joint> joints     = new Dictionary <Joint, Joint>();
            Dictionary <Joint, Joint> jSelection = new Dictionary <Joint, Joint>();
            List <LineElement>        lines      = new List <LineElement>();
            List <LineElement>        lSelection = new List <LineElement>();
            List <AreaElement>        areas      = new List <AreaElement>();
            List <AreaElement>        aSelection = new List <AreaElement>();
            ItemList <Joint>          jList      = services.Model.JointList;
            ItemList <LineElement>    lList      = services.Model.LineList;
            ItemList <AreaElement>    aList      = services.Model.AreaList;
            Joint       nJoint;
            LineElement nLine;
            AreaElement nArea;

            List <Item> selection = services.GetSelection();

            if (selection.Count == 0)
            {
                return;
            }

            foreach (Item item in selection)
            {
                if (item is Joint)
                {
                    jSelection.Add((Joint)item, null);
                    joints.Add((Joint)item, null);
                }
                else if (item is LineElement)
                {
                    LineElement l = (LineElement)item;
                    lSelection.Add(l);
                    lines.Add(l);
                    if (!jSelection.ContainsKey(l.I))
                    {
                        jSelection.Add(l.I, null);
                        joints.Add(l.I, null);
                    }
                    if (!jSelection.ContainsKey(l.J))
                    {
                        jSelection.Add(l.J, null);
                        joints.Add(l.J, null);
                    }
                }
                else if (item is AreaElement)
                {
                    AreaElement a = (AreaElement)item;
                    aSelection.Add(a);
                    areas.Add(a);
                    if (!jSelection.ContainsKey(a.J1))
                    {
                        jSelection.Add(a.J1, null);
                        joints.Add(a.J1, null);
                    }
                    if (!jSelection.ContainsKey(a.J2))
                    {
                        jSelection.Add(a.J2, null);
                        joints.Add(a.J2, null);
                    }
                    if (!jSelection.ContainsKey(a.J3))
                    {
                        jSelection.Add(a.J3, null);
                        joints.Add(a.J3, null);
                    }
                    if (a.J4 != null && !jSelection.ContainsKey(a.J4))
                    {
                        jSelection.Add(a.J4, null);
                        joints.Add(a.J4, null);
                    }
                }
            }

            Microsoft.DirectX.Vector3 v, v2;
            uint n = (uint)services.GetSingle(Culture.Get("getArrayRepetition"));

            float dAngle = float.Parse(services.GetString(Culture.Get("getPolarArrayAngle")));

            dAngle *= (float)Math.PI / 180.0F;
            float angle = 0.0F;

            Controller.Snap.Magnet m = services.GetPoint(Culture.Get("getPolarRotationCenter"));
            if (m == null)
            {
                return;
            }
            v = m.SnapPosition;

            services.TrackingService = LineTrackingService.Instance;
            services.TrackingService.SetPoint(m.SnapPositionInt);

            m = services.GetPoint(Culture.Get("getPolarRotationCenter"));
            if (m == null)
            {
                return;
            }
            v2 = m.SnapPosition;
            if (v2.Equals(v))
            {
                Canguro.View.GraphicView view = Canguro.View.GraphicViewManager.Instance.ActiveView;
                Vector3 v1Tmp = new Vector3(0, 0, 0);
                Vector3 v2Tmp = new Vector3(0, 0, 1);
                view.Unproject(ref v1Tmp);
                view.Unproject(ref v2Tmp);
                v2 = v2 + v1Tmp - v2Tmp;
            }
            services.TrackingService = null;

            List <Joint>       newJoints = new List <Joint>();
            List <LineElement> newLines  = new List <LineElement>();
            List <AreaElement> newAreas  = new List <AreaElement>();


            for (int i = 1; i <= n; i++)
            {
                angle += dAngle;

                Matrix trans1 = new Matrix();
                trans1.Translate(-v);
                Matrix rot = new Matrix();
                rot.RotateAxis(v2 - v, angle);
                Matrix trans2 = new Matrix();
                trans2.Translate(v);
                rot = trans1 * rot * trans2;

                foreach (Joint j in joints.Keys)
                {
                    Vector3 pos = new Vector3(j.X, j.Y, j.Z);
                    pos.TransformCoordinate(rot);

                    jList.Add(nJoint = new Joint(pos.X, pos.Y, pos.Z));
                    nJoint.Masses    = j.Masses;
                    nJoint.DoF       = j.DoF;
                    jSelection[j]    = nJoint;
                    newJoints.Add(nJoint);
                }
                foreach (LineElement l in lines)
                {
                    lList.Add(nLine = new LineElement(l, jSelection[l.I], jSelection[l.J]));
                    newLines.Add(nLine);
                }
                foreach (AreaElement a in areas)
                {
                    aList.Add(nArea = new AreaElement(a, jSelection[a.J1], jSelection[a.J2], jSelection[a.J3], (a.J4 != null) ? jSelection[a.J4] : null));
                    newAreas.Add(nArea);
                }
            }
            JoinCmd.Join(services.Model, newJoints, newLines, newAreas);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Executes the command.
        /// Moves the selected Item's Joints around a given point and with a given angle.
        /// </summary>
        /// <param name="services">CommandServices object to interact with the system</param>
        public override void Run(Canguro.Controller.CommandServices services)
        {
            Dictionary <Joint, Joint> joints = new Dictionary <Joint, Joint>();
            ItemList <Joint>          jList  = services.Model.JointList;
            ItemList <LineElement>    lList  = services.Model.LineList;

            List <Item> selection = services.GetSelection();

            if (selection.Count == 0)
            {
                return;
            }

            foreach (Item item in selection)
            {
                if (item is Joint)
                {
                    joints.Add((Joint)item, null);
                }
                else if (item is LineElement)
                {
                    LineElement l = (LineElement)item;
                    if (!joints.ContainsKey(l.I))
                    {
                        joints.Add(l.I, null);
                    }
                    if (!joints.ContainsKey(l.J))
                    {
                        joints.Add(l.J, null);
                    }
                }
            }

            Microsoft.DirectX.Vector3 v, v2;

            float angle = float.Parse(services.GetString(Culture.Get("getRotationAngle")));

            angle *= (float)Math.PI / 180.0F;
            Controller.Snap.Magnet m = services.GetPoint(Culture.Get("getRotationCenter"));
            if (m == null)
            {
                return;
            }

            v = m.SnapPosition;

            services.TrackingService = LineTrackingService.Instance;
            services.TrackingService.SetPoint(m.SnapPositionInt);

            m = services.GetPoint(Culture.Get("getPolarRotationCenter"));
            if (m == null)
            {
                return;
            }
            v2 = m.SnapPosition;
            if (v2.Equals(v))
            {
                Canguro.View.GraphicView view = Canguro.View.GraphicViewManager.Instance.ActiveView;
                Vector3 v1Tmp = new Vector3(0, 0, 0);
                Vector3 v2Tmp = new Vector3(0, 0, 1);
                view.Unproject(ref v1Tmp);
                view.Unproject(ref v2Tmp);
                v2 = v2 + v1Tmp - v2Tmp;
            }
            services.TrackingService = null;

            Matrix trans1 = new Matrix();

            trans1.Translate(-v);
            Matrix rot = new Matrix();

            rot.RotateAxis(v2 - v, angle);
            Matrix trans2 = new Matrix();

            trans2.Translate(v);

            rot = trans1 * rot * trans2;

            foreach (Joint j in joints.Keys)
            {
                Vector3 pos = new Vector3(j.X, j.Y, j.Z);

                pos.TransformCoordinate(rot);

                j.X = pos.X;
                j.Y = pos.Y;
                j.Z = pos.Z;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Executes the command.
        /// Makes a copy of the selected items with inverted positions with respect to a mirror plane, defined by 3 points.
        /// If the points are colinear, the 3rd point is taken to be perpendicular to the view.
        /// </summary>
        /// <param name="services">CommandServices object to interact with the system</param>
        public override void Run(Canguro.Controller.CommandServices services)
        {
            Dictionary <uint, Joint> joints = new Dictionary <uint, Joint>();
            List <LineElement>       lines  = new List <LineElement>();
            List <AreaElement>       areas  = new List <AreaElement>();

            services.GetSelection(joints, lines, areas);

            Dictionary <uint, Joint> jSelection = new Dictionary <uint, Joint>();

            Microsoft.DirectX.Vector3[] pivots = new Microsoft.DirectX.Vector3[3];

            // Get 3 Points
            Magnet m = services.GetPoint(Culture.Get("selectPlainPoints"));

            pivots[0] = m.SnapPosition;

            m         = services.GetPoint(Culture.Get("selectPlainPoints"));
            pivots[1] = m.SnapPosition;

            m         = services.GetPoint(Culture.Get("selectPlainPoints"));
            pivots[2] = m.SnapPosition;
            Vector3 v1 = pivots[0] - pivots[1];
            Vector3 v2 = pivots[1] - pivots[2];

            if (Vector3.Cross(v1, v2).LengthSq() < 0.0001)     // If Colinear, take perpendicular to the active view.
            {
                Canguro.View.GraphicView view = Canguro.View.GraphicViewManager.Instance.ActiveView;
                v1 = new Vector3(0, 0, 0);
                v2 = new Vector3(0, 0, 1);
                view.Unproject(ref v1);
                view.Unproject(ref v2);
                pivots[2] = pivots[2] + v1 - v2;
            }

            ItemList <Joint>       jList = services.Model.JointList;
            ItemList <LineElement> lList = services.Model.LineList;
            ItemList <AreaElement> aList = services.Model.AreaList;

            Joint              nJoint;
            LineElement        nLine;
            AreaElement        nArea;
            List <Joint>       newJoints = new List <Joint>();
            List <LineElement> newLines  = new List <LineElement>();
            List <AreaElement> newAreas  = new List <AreaElement>();

            foreach (uint jid in joints.Keys)
            {
                Joint   j          = jList[jid];
                Vector3 currentPos = new Vector3(j.X, j.Y, j.Z);
                Vector3 newPos     = Mirror(currentPos, pivots);
                jList.Add(nJoint = new Joint(newPos.X, newPos.Y, newPos.Z));
                nJoint.Masses    = j.Masses;
                nJoint.DoF       = j.DoF;
                jSelection.Add(jid, nJoint);
                newJoints.Add(nJoint);
            }
            foreach (LineElement l in lines)
            {
                lList.Add(nLine = new LineElement(l, jSelection[l.I.Id], jSelection[l.J.Id]));
                newLines.Add(nLine);
            }
            foreach (AreaElement a in areas)
            {
                aList.Add(nArea = new AreaElement(a, jSelection[a.J1.Id], jSelection[a.J2.Id], jSelection[a.J3.Id], (a.J4 != null) ? jSelection[a.J4.Id] : null));
                newAreas.Add(nArea);
            }
            JoinCmd.Join(services.Model, newJoints, newLines, newAreas);
        }