Ejemplo n.º 1
0
        /// <summary>
        /// Spherical linear interpolator
        /// </summary>
        public static Rhino.Geometry.Vector3d Slerp(Rhino.Geometry.Vector3d v0, Rhino.Geometry.Vector3d v1, double n)
        {
            if (n <= 0.0)
            {
                return(v0);
            }

            if (v0 == v1 || n >= 1.0)
            {
                return(v1);
            }

            Rhino.Geometry.Vector3d u0 = new Rhino.Geometry.Vector3d(v0);
            Rhino.Geometry.Vector3d u1 = new Rhino.Geometry.Vector3d(v1);

            u0.Unitize();
            u1.Unitize();

            double dot = Rhino.Geometry.Vector3d.Multiply(u0, u1);

            dot = ((dot) < (-1.0) ? (-1.0) : ((dot) > (1.0) ? (1.0) : (dot)));
            double theta = Math.Acos(dot);

            if (Math.Abs(theta) < Double.Epsilon)
            {
                return(v1);
            }

            double st = Math.Sin(theta);

            return((v0 * ((Math.Sin((1.0 - n) * theta)) / st)) + (v1 * ((Math.Sin(n * theta) / st))));
        }
    public static Rhino.Commands.Result MoveGripObjects(Rhino.RhinoDoc doc)
    {
        // The following example demonstrates how to move a surface's grip objects.
        // In this sample, all grips will be moved a fixed distance of 0.5 units
        // in the normal direction of the surface at that grip location.

        Rhino.DocObjects.ObjRef objRef;
        Rhino.Commands.Result   rc = Rhino.Input.RhinoGet.GetOneObject("Select surface for control point editing", false, Rhino.DocObjects.ObjectType.Surface, out objRef);
        if (rc != Rhino.Commands.Result.Success)
        {
            return(rc);
        }

        Rhino.DocObjects.RhinoObject obj = objRef.Object();
        if (null == obj)
        {
            return(Rhino.Commands.Result.Failure);
        }

        Rhino.Geometry.Surface srf = objRef.Surface();
        if (null == srf)
        {
            return(Rhino.Commands.Result.Failure);
        }

        // Make sure the object's grips are enabled
        obj.GripsOn = true;
        doc.Views.Redraw();

        Rhino.DocObjects.GripObject[] grips = obj.GetGrips();
        for (int i = 0; i < grips.Length; i++)
        {
            Rhino.DocObjects.GripObject grip = grips[i];

            // Calculate the point on the surface closest to our test point,
            // which is the grip's 3-D location (for this example).
            double u, v;
            if (srf.ClosestPoint(grip.CurrentLocation, out u, out v))
            {
                // Compute the surface normal at a point
                Rhino.Geometry.Vector3d dir = srf.NormalAt(u, v);
                dir.Unitize();

                // Scale by our fixed distance
                dir *= 0.5;

                // Move the grip to a new location
                grip.Move(dir);
            }
        }

        // Altered grip positions on a RhinoObject are used to calculate an updated
        // object that is added to the document.
        doc.Objects.GripUpdate(obj, false);
        doc.Views.Redraw();

        return(Rhino.Commands.Result.Success);
    }
Ejemplo n.º 3
0
        private void SetLocalVectors()
        {
            var tempX = new Rhino.Geometry.Vector3d(ePos.X - sPos.X, ePos.Y - sPos.Y, ePos.Z - sPos.Z);

            tempX.Unitize();
            LocalX = tempX;


            LocalY = Rhino.Geometry.Vector3d.CrossProduct(elNormal, LocalX);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Find the Perspective viewport in the 3dm file and sets up the default view.
        /// </summary>
        public void LoadCamera()
        {
            if (App.Manager.CurrentModel == null)
            {
                return;
            }

            Camera = new ViewportInfo();
            bool cameraIsInitialized = false;

            int viewCount = App.Manager.CurrentModel.ModelFile.Views.Count;

            // find first perspective viewport projection in file
            if (viewCount > 0)
            {
                foreach (var view in App.Manager.CurrentModel.ModelFile.Views)
                {
                    if (view.Viewport.IsPerspectiveProjection)
                    {
                        cameraIsInitialized = true;
                        Camera             = view.Viewport;
                        Camera.TargetPoint = view.Viewport.TargetPoint;
                        Camera.SetScreenPort(0, (int)View.Bounds.Size.Width, 0, (int)View.Bounds.Size.Height, 1, 1000);
                        Camera.FrustumAspect = Camera.ScreenPortAspect;
                        Camera.SetFrustumNearFar(App.Manager.CurrentModel.BBox);
                        break;
                    }
                }
            }

            // If there isn't one, then cook up a viewport from scratch...
            if (!cameraIsInitialized)
            {
                Camera.SetScreenPort(0, (int)View.Bounds.Size.Width, 0, (int)View.Bounds.Size.Height, 1, 1000);
                Camera.TargetPoint = new Rhino.Geometry.Point3d(0, 0, 0);
                var plane = new Rhino.Geometry.Plane(Rhino.Geometry.Point3d.Origin, new Rhino.Geometry.Vector3d(-1, -1, -1));
                Camera.SetCameraLocation(new Rhino.Geometry.Point3d(10, 10, 10));
                var dir = new Rhino.Geometry.Vector3d(-1, -1, -1);
                dir.Unitize();
                Camera.SetCameraDirection(dir);
                Camera.SetCameraUp(plane.YAxis);
                Camera.SetFrustum(-1, 1, -1, 1, 0.1, 1000);
                Camera.FrustumAspect           = Camera.ScreenPortAspect;
                Camera.IsPerspectiveProjection = true;
                Camera.Camera35mmLensLength    = 50;
                if (App.Manager.CurrentModel != null)
                {
                    if (App.Manager.CurrentModel.AllMeshes != null)
                    {
                        Camera.DollyExtents(App.Manager.CurrentModel.AllMeshes, 1.0);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public override void DrawViewportWires(Grasshopper.Kernel.IGH_PreviewArgs args)
        {
            if (Hidden)
            {
                return;
            }
            if (listArrow != null)
            {
                args.Display.DrawLines(listArrow, System.Drawing.Color.Red);
            }
            //eigenvectors
            if (crossCyan != null)
            {
                args.Display.DrawLines(crossCyan, System.Drawing.Color.Cyan);
            }
            if (crossMagenta != null)
            {
                args.Display.DrawLines(crossMagenta, System.Drawing.Color.Magenta);
            }
            foreach (var leaf in listLeaf)
            {
                if (leaf.forceSrf != null)
                {
                    var srf = leaf.forceSrf.Duplicate() as Rhino.Geometry.NurbsSurface;
                    //srf.Transform(zScale);
                    args.Display.DrawSurface(srf, System.Drawing.Color.Aqua, 2);
                    if (leaf.tuples != null)
                    {
                        foreach (var tup in leaf.tuples)
                        {
                            var P = leaf.formSrf.PointAt(tup.u, tup.v);
                            var Q = leaf.forceSrf.PointAt(tup.u, tup.v);
                            Rhino.Geometry.Vector3d D = new Rhino.Geometry.Vector3d(Q.X, Q.Y, Q.Z);
                            D.Unitize();
                            D *= 0.3;
                            args.Display.DrawLine(new Rhino.Geometry.Line(P, P + D), System.Drawing.Color.Magenta);
                        }
                    }
                }
            }
            foreach (var leaf in listLeaf)
            {

                /*
                if (leaf.shellSrf != null)
                {
                    var srf = leaf.shellSrf.Duplicate() as Rhino.Geometry.NurbsSurface;
                    srf.Transform(zDown_eq);
                    args.Display.DrawSurface(srf, System.Drawing.Color.Brown, 3);
                }*/
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Sets the viewport's camera location, target and up vector
        /// </summary>
        public static void SetTarget(this ViewportInfo viewport, Rhino.Geometry.Point3d targetLocation, Rhino.Geometry.Point3d cameraLocation, Rhino.Geometry.Vector3d cameraUp)
        {
            Rhino.Geometry.Vector3d cameraDirection = targetLocation - cameraLocation;
            cameraDirection.Unitize();

            if (!viewport.CameraDirection.IsTiny())
            {
                Rhino.Geometry.Vector3d cameraDirection0 = -viewport.CameraZ;
                Rhino.Geometry.Vector3d cameraY          = viewport.CameraY;
                const double            tiltAngle        = 0;

                viewport.SetCameraLocation(cameraLocation);
                viewport.SetCameraDirection(cameraDirection);

                bool didSetTarget = false;
                didSetTarget = viewport.SetCameraUp(cameraUp);

                if (!didSetTarget)
                {
                    didSetTarget = viewport.SetCameraUp(cameraY);
                    cameraUp     = cameraY;
                }

                if (!didSetTarget)
                {
                    Rhino.Geometry.Vector3d rotationAxis = Rhino.Geometry.Vector3d.CrossProduct(cameraDirection0, cameraDirection);
                    double sinAngle = rotationAxis.Length;
                    double cosAngle = cameraDirection0 * cameraDirection;
                    Rhino.Geometry.Transform rot = Rhino.Geometry.Transform.Rotation(sinAngle, cosAngle, rotationAxis, Rhino.Geometry.Point3d.Origin);
                    cameraUp     = rot * cameraY;
                    didSetTarget = viewport.SetCameraUp(cameraUp);
                }

                if (didSetTarget)
                {
                    // Apply tilt angle to new camera and target location
                    if (Math.Abs(tiltAngle) > 1.0e-6)
                    {
                        Rhino.Geometry.Transform rot = Rhino.Geometry.Transform.Rotation(tiltAngle, -cameraDirection0, cameraLocation);
                        cameraUp     = rot * cameraUp;
                        didSetTarget = viewport.SetCameraUp(cameraUp);
                    }

                    if (didSetTarget)
                    {
                        viewport.TargetPoint = targetLocation;
                    }
                }
            }
        }
Ejemplo n.º 7
0
 public void set_direction(Rhino.Geometry.Point3d rec, Rhino.Geometry.Vector3d V)
 {
     V.Unitize();
     Dir = new Rhino.Geometry.Line(rec, rec + V);
 }
Ejemplo n.º 8
0
 public void set_direction(Rhino.Geometry.Point3d rec, Rhino.Geometry.Point3d dir)
 {
     Rhino.Geometry.Vector3d V = rec - dir;
     V.Unitize();
     Dir = new Rhino.Geometry.Line(rec, rec + dir);
 }
Ejemplo n.º 9
0
        private void AnimateRestoreView()
        {
            FastDrawing = true;
            var restoreViewCurrentTime = DateTime.Now;

            var    currentTime              = restoreViewCurrentTime;
            var    startTime                = RestoreViewStartTime;
            var    timeElapsed              = currentTime.Subtract(startTime);
            var    timeElapsedInMs          = timeElapsed.TotalMilliseconds;
            var    totalTimeOfAnimationInMs = RestoreViewTotalTime.TotalMilliseconds;
            double percentCompleted         = timeElapsedInMs / totalTimeOfAnimationInMs;

            if (percentCompleted > 1)
            {
                // Animation is completed. Perform one last draw.
                percentCompleted            = 1;
                IsInAnimatedRestoreView     = false;
                View.UserInteractionEnabled = true;
                CameraIsAtInitialPosition   = !ShouldStartRestoreToInitialPosition;
                AnimationTimer.Invalidate();
            }

            // Get some data from the starting view
            Rhino.Geometry.Point3d sourceTarget = RestoreViewStartViewport.TargetPoint;
            Rhino.Geometry.Point3d sourceCamera = RestoreViewStartViewport.CameraLocation;
            double sourceDistance = sourceCamera.DistanceTo(sourceTarget);

            Rhino.Geometry.Vector3d sourceUp = RestoreViewStartViewport.CameraUp;
            sourceUp.Unitize();

            // Get some data from the ending view
            Rhino.Geometry.Point3d targetTarget = RestoreViewFinishViewport.TargetPoint;
            Rhino.Geometry.Point3d targetCamera = RestoreViewFinishViewport.CameraLocation;
            double targetDistance = targetCamera.DistanceTo(targetTarget);

            Rhino.Geometry.Vector3d targetCameraDir = targetCamera - targetTarget;
            Rhino.Geometry.Vector3d targetUp        = RestoreViewFinishViewport.CameraUp;
            targetUp.Unitize();

            // Adjust the target camera location so that the starting camera to target distance
            // and the ending camera to target distance are the same.  Doing this will calculate
            // a constant rotational angular momentum when tweening the camera location.
            // Further down we independently tween the camera to target distance.
            targetCameraDir.Unitize();
            targetCameraDir *= sourceDistance;
            targetCamera     = targetCameraDir + targetTarget;

            // calculate interim viewport values
            double frameDistance = ViewportInfoExtensions.CosInterp(sourceDistance, targetDistance, percentCompleted);

            Rhino.Geometry.Point3d frameTarget = new Rhino.Geometry.Point3d();

            frameTarget.X = ViewportInfoExtensions.CosInterp(sourceTarget.X, targetTarget.X, percentCompleted);
            frameTarget.Y = ViewportInfoExtensions.CosInterp(sourceTarget.Y, targetTarget.Y, percentCompleted);
            frameTarget.Z = ViewportInfoExtensions.CosInterp(sourceTarget.Z, targetTarget.Z, percentCompleted);

            var origin = Rhino.Geometry.Point3d.Origin;

            Rhino.Geometry.Point3d  frameCamera    = origin + (ViewportInfoExtensions.Slerp((sourceCamera - origin), (targetCamera - origin), percentCompleted));
            Rhino.Geometry.Vector3d frameCameraDir = frameCamera - frameTarget;

            // adjust the camera location along the camera direction vector to preserve the target location and the camera distance
            frameCameraDir.Unitize();
            frameCameraDir *= frameDistance;
            frameCamera     = frameCameraDir + frameTarget;

            Rhino.Geometry.Vector3d frameUp = new Rhino.Geometry.Vector3d(ViewportInfoExtensions.Slerp(sourceUp, targetUp, percentCompleted));

            if (percentCompleted >= 1)
            {
                // put the last redraw at the exact end point to eliminate any rounding errors
                Camera.SetTarget(RestoreViewFinishViewport.TargetPoint, RestoreViewFinishViewport.CameraLocation, RestoreViewFinishViewport.CameraUp);
            }
            else
            {
                Camera.SetTarget(frameTarget, frameCamera, frameUp);
            }

            SetFrustum(Camera, App.Manager.CurrentModel.BBox);

            View.SetNeedsDisplay();

            if (!IsInAnimatedRestoreView)
            {
                // FastDrawing is still enabled and we just scheduled a draw of the model at the final location.
                // This entirely completes the animation. Now schedule one more redraw of the model with FastDrawing disabled
                // and this redraw will be done at exactly the same postion.  This prevents the final animation frame
                // from jumping to the final location because the final draw will take longer with FastDrawing disabled.
                PerformSelector(new Selector("RedrawDetailed"), null, 0.05);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Spherical linear interpolator
        /// </summary>
        public static Rhino.Geometry.Vector3d Slerp(Rhino.Geometry.Vector3d v0, Rhino.Geometry.Vector3d v1, double n)
        {
            if (n <= 0.0)
                return v0;

            if (v0 == v1 || n >= 1.0)
                return v1;

            Rhino.Geometry.Vector3d u0 = new Rhino.Geometry.Vector3d (v0);
            Rhino.Geometry.Vector3d u1 = new Rhino.Geometry.Vector3d (v1);

            u0.Unitize ();
            u1.Unitize ();

            double dot = Rhino.Geometry.Vector3d.Multiply (u0, u1);
            dot = ((dot) < (-1.0) ? (-1.0) : ((dot) > (1.0) ? (1.0) : (dot)));
            double theta = Math.Acos (dot);
            if (Math.Abs(theta) < Double.Epsilon)
                return v1;

            double st = Math.Sin(theta);
            return ( (v0 * ((Math.Sin ((1.0 - n) * theta)) / st)) + (v1 * ((Math.Sin (n * theta) / st))) );
        }
Ejemplo n.º 11
0
        private void SetLocalVectors()
        {
            var tempX = new Rhino.Geometry.Vector3d(ePos.X - sPos.X, ePos.Y - sPos.Y, ePos.Z - sPos.Z);
            tempX.Unitize();
            LocalX = tempX;

            LocalY = Rhino.Geometry.Vector3d.CrossProduct(elNormal, LocalX);
        }