Exemplo n.º 1
0
        protected override void OnCreated()
        {
            base.OnCreated();
            Navigating = true;

            TheNurbs = new NurbsSurface();

            _BsplineSurface = CreateBsplineSurface();

            BsplineSurface.AsNurb = _BsplineSurface;
            TheNurbs       = BsplineSurface;
            _Sphere.Center = new xyz(4, 0, 0);
            _Sphere.Radius = 4;

            Sphere.AsNurb      = _Sphere;
            _Torus.InnerRadius = 1;
            _Torus.OuterRadius = 5;
            Torus.AsNurb       = _Torus;

            _Cone.HalfAngle = System.Math.PI / 6;
            _Cone.Radius    = 4;
            _Cone.Height    = 5;
            Cone.AsNurb     = _Cone;

            _Cylinder.Radius = 4;
            _Cylinder.Height = 5;
            Cylinder.AsNurb  = _Cylinder;


            CurrentForm.label1.Text = "BsplineSurface";
            Marked = new System.Drawing.Point(2, 2);
        }
Exemplo n.º 2
0
        BSplineSurface CreateBsplineSurface()
        {
            BSplineSurface Result = new BSplineSurface();

            Result.ControlPoints = CtrlPoints;
            Result.SetDefaultKnots();

            return(Result);
        }
Exemplo n.º 3
0
    public void CreateScalp(Vector3[,] grid, int NI, int NJ)
    {
        Debug.Log("Creating Scalp");
        surface = GameObject.Find("ScalpSurface");
        GameObject head = GameObject.Find("Head");
        surface.AddComponent<MeshFilter>();
        surface.AddComponent<MeshCollider>();
        mySurface = new BSplineSurface();
        mySurface.setControlGrid(NI-1, NJ-1);
        mySurface.Init();
        mySurface.InitRandomGrid();
        mySurface.InitGrid(grid);
        mySurface.Calculate();
        resultGrid = mySurface.outputGrid;
        controlGrid = mySurface.controlGrid;
        generateMesh();
        updateMesh();
        MeshRenderer renderer = surface.AddComponent<MeshRenderer>();
        renderer.material = GameObject.Find("Tracker").GetComponent<MeshRenderer>().material;
        surface.transform.parent = GameObject.Find("Head").transform;

        GameObject underSurface = GameObject.Instantiate(surface);
        Destroy(underSurface.GetComponent<ScalpGenerator>());
        underSurface.transform.position = surface.transform.position;
        underSurface.transform.rotation = surface.transform.rotation;
        underSurface.transform.parent = surface.transform.parent;
        MeshFilter filter = underSurface.GetComponent<MeshFilter>();
        if (filter != null)
        {
            Mesh mesh = filter.mesh;

            Vector3[] normals = mesh.normals;
            for (int i = 0; i < normals.Length; i++)
                normals[i] = -normals[i];
            mesh.normals = normals;

            for (int m = 0; m < mesh.subMeshCount; m++)
            {
                int[] triangles = mesh.GetTriangles(m);
                for (int i = 0; i < triangles.Length; i += 3)
                {
                    int temp = triangles[i + 0];
                    triangles[i + 0] = triangles[i + 1];
                    triangles[i + 1] = temp;
                }
                mesh.SetTriangles(triangles, m);
            }
        }
        //drawn = true;
    }
Exemplo n.º 4
0
 // Use this for initialization
 void Start()
 {
     mySurface = new BSplineSurface();
     mySurface.NI = 16;
     mySurface.NJ = 12;
     mySurface.RESOLUTIONI = 32;
     mySurface.RESOLUTIONJ = 24;
     mySurface.Init();
     mySurface.InitRandomGrid();
     mySurface.Calculate();
     resultGrid = mySurface.outputGrid;
     controlGrid = mySurface.controlGrid;
     generateMesh();
     updateMesh();
 }
Exemplo n.º 5
0
 // Use this for initialization
 void Start()
 {
     mySurface             = new BSplineSurface();
     mySurface.NI          = 16;
     mySurface.NJ          = 12;
     mySurface.RESOLUTIONI = 32;
     mySurface.RESOLUTIONJ = 24;
     mySurface.Init();
     mySurface.InitRandomGrid();
     mySurface.Calculate();
     resultGrid  = mySurface.outputGrid;
     controlGrid = mySurface.controlGrid;
     generateMesh();
     updateMesh();
 }
Exemplo n.º 6
0
 public void CreateScalp(Vector3[,] grid, int NI, int NJ)
 {
     Debug.Log("Creating Scalp");
     surface = GameObject.Find("ScalpSurface");
     surface.AddComponent<MeshFilter>();
     surface.AddComponent<MeshCollider>();
     mySurface = new BSplineSurface();
     mySurface.setControlGrid(NI, NJ);
     mySurface.Init();
     //mySurface.InitRandomGrid();
     mySurface.InitGrid(grid);
     mySurface.Calculate();
     resultGrid = mySurface.outputGrid;
     controlGrid = mySurface.controlGrid;
     generateMesh();
     updateMesh();
     drawn = true;
 }
Exemplo n.º 7
0
 // Use this for initialization
 void Start()
 {
     mcol = GetComponent<MeshCollider>();
     surface = new BSplineSurface();
 }
Exemplo n.º 8
0
        public void ParameterAt()
        {
            double[][][]   ptGrid = null;
            double[]       r, a;
            double         u, v, e;
            BSplineSurface surface = null;
            int            uDegree, vDegree;
            KnotSet        uKnot, vKnot;

            uDegree = 2;
            vDegree = 3;
            uKnot   = KnotSet.CreateToUniform(uDegree, 3);
            vKnot   = KnotSet.CreateToUniform(vDegree, 5);

            ptGrid = new double[][][]
            {
                new double[][] {
                    new double[] { 0.0, 0.0, 1.0 },
                    new double[] { 0.0, 1.0, 2.0 },
                    new double[] { 0.0, 2.0, 0.0 },
                    new double[] { 0.0, 3.0, 1.0 },
                    new double[] { 0.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 1.0, 0.0, 2.0 },
                    new double[] { 1.0, 1.0, 2.0 },
                    new double[] { 1.0, 2.0, 1.0 },
                    new double[] { 1.0, 3.0, 1.0 },
                    new double[] { 1.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 2.0, 0.0, 2.0 },
                    new double[] { 2.0, 1.0, 1.0 },
                    new double[] { 2.0, 2.0, 1.0 },
                    new double[] { 2.0, 3.0, 1.0 },
                    new double[] { 2.0, 4.0, 2.0 },
                },
            };
            u       = 0.3;
            v       = 0.4;
            e       = 0.0001;
            surface = new BSplineSurface(ptGrid, uKnot, vKnot, uDegree, vDegree);
            r       = surface.ParameterAt(u, v);
            a       = new double[] { 0.6, 1.696, 1.16312 };

            Assert.IsTrue(r.SequenceEqual(a, e));


            uDegree = 2;
            vDegree = 3;
            uKnot   = KnotSet.CreateToUniform(uDegree, 3);
            vKnot   = KnotSet.CreateToUniform(vDegree, 5);

            ptGrid = new double[][][]
            {
                new double[][] {
                    new double[] { 0.0, 0.0, 1.0 },
                    new double[] { 0.0, 1.0, 2.0 },
                    new double[] { 0.0, 2.0, 0.0 },
                    new double[] { 0.0, 3.0, 1.0 },
                    new double[] { 0.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 1.0, 0.0, 2.0 },
                    new double[] { 1.0, 1.0, 2.0 },
                    new double[] { 1.0, 2.0, 1.0 },
                    new double[] { 1.0, 3.0, 1.0 },
                    new double[] { 1.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 2.0, 0.0, 2.0 },
                    new double[] { 2.0, 1.0, 1.0 },
                    new double[] { 2.0, 2.0, 1.0 },
                    new double[] { 2.0, 3.0, 1.0 },
                    new double[] { 2.0, 4.0, 2.0 },
                },
            };
            u       = 1;
            v       = 0;
            e       = 0.0001;
            surface = new BSplineSurface(ptGrid, uKnot, vKnot, uDegree, vDegree);
            r       = surface.ParameterAt(u, v);
            a       = new double[] { 2, 0, 2 };

            Assert.IsTrue(r.SequenceEqual(a, e));


            uDegree = 2;
            vDegree = 3;
            uKnot   = KnotSet.CreateToUniform(uDegree, 3);
            vKnot   = KnotSet.CreateToUniform(vDegree, 5);

            ptGrid = new double[][][]
            {
                new double[][] {
                    new double[] { 0.0, 0.0, 1.0 },
                    new double[] { 0.0, 1.0, 2.0 },
                    new double[] { 0.0, 2.0, 0.0 },
                    new double[] { 0.0, 3.0, 1.0 },
                    new double[] { 0.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 1.0, 0.0, 2.0 },
                    new double[] { 1.0, 1.0, 2.0 },
                    new double[] { 1.0, 2.0, 1.0 },
                    new double[] { 1.0, 3.0, 1.0 },
                    new double[] { 1.0, 4.0, 1.0 },
                },
                new double[][] {
                    new double[] { 2.0, 0.0, 2.0 },
                    new double[] { 2.0, 1.0, 1.0 },
                    new double[] { 2.0, 2.0, 1.0 },
                    new double[] { 2.0, 3.0, 1.0 },
                    new double[] { 2.0, 4.0, 2.0 },
                },
            };
            u       = 1;
            v       = 1;
            e       = 0.0001;
            surface = new BSplineSurface(ptGrid, uKnot, vKnot, uDegree, vDegree);
            r       = surface.ParameterAt(u, v);
            a       = new double[] { 2, 4, 2 };

            Assert.IsTrue(r.SequenceEqual(a, e));
        }
        /// <summary>
        /// Create a BSplineFace from the TrimCurve data.
        /// http://help.solidworks.com/2015/English/api/sldworksapi/SOLIDWORKS.Interop.sldworks~SOLIDWORKS.Interop.sldworks.IFace2~GetTrimCurves2.html
        /// </summary>
        /// <param name="swFace"></param>
        /// <returns></returns>
        public static BSplineFace Create(IFace2 swFace )
        {
            var start = 0;

            var packedData = swFace.GetTrimCurves2(WantCubic: true, WantNRational: false).CastArray<double>();
            var reader = new GetTrimCurves2DataReader(packedData);

            // Packed Double 1
            // An integer pair containing number of loops and total number of SP curves (trim curves).
            // The length of any edge list generated immediately after a call to IFace2::GetTrimCurves2
            // will be equal to the number of SP curves
            var packedDouble1 = reader.ReadDouble().DoubleToInteger();
            int numLoops = packedDouble1.Item1;
            int numSPCurves = packedDouble1.Item2;

            // PackeDouble 2
            // Series of integer pairs containing the number of SP curves in each loop.
            // The first integer in each pair represents the number of curves in the odd loops;
            // the second represents the even. The total number of integer pairs is half the 
            // number of loops, rounded up
            var curvesPerLoopLookup = reader.ReadIntegers(numLoops).ToList();

            var i = 0;

            // PackedDouble 3[]  ( Array of doubles )
            // For each SP curve, a set of two integer pairs.
            // The first contains the order of the curve and
            // a Boolean indicating if it is periodic.If the curve is periodic,
            // it is clamped (that is, knots of multiplicity = order exists at each end of the curve).
            // The second contains the dimension of the curve and the number of control points in it. 
            // If the dimension is 2, then the curve is non - rational; if the dimension is 3,
            // then the curve is rational.
            var spCurveInfos = reader
                .ReadBufferedIntegers(bufferSize: 4, numberOfBuffers: numSPCurves)
                .Do(b =>
                {
                    LogViewer.Log($"Get TrimCurves2 'PackedDouble 3' buffer {i++}");
                    LogViewer.Log(string.Join(" ",b));
                })
                .Select(b => new {order = b[0], isPeriodic = b[1]== 1, dimension=b[2], isRational=b[2]==3, numCtrlPoints = b[3]})
                .ToList();

            var spCurveInfos2 = spCurveInfos
                .Select
                (info =>
                {
                    var knots = reader.Read(info.order + info.numCtrlPoints).ToList();
                    return new {info.order,info.isPeriodic, info.dimension, info.isRational, info.numCtrlPoints, knots};
                })
                .ToList();

            var trimCurves = spCurveInfos2
                .Select
                (info =>
                {
                    var ctrlPoints = reader
                        .Read(info.numCtrlPoints*info.dimension)
                        .Buffer(info.dimension, info.dimension)
                        .Select(ToRationalVector3)
                        .ToList();

                    return new BSpline2D
                        ( controlPoints: ctrlPoints.ToArray()
                        , knotVectorU: info.knots.ToArray()
                        , order: info.order
                        , isClosed: info.isPeriodic
                        , isRational: info.dimension==3);

                })
                .ToArray();


            var bLoops = curvesPerLoopLookup
                .Scan(new {start = 0, step = 0}, (acc, count) => new {start = acc.start + acc.step, step = count})
                .Skip(1)
                .Select(o => trimCurves.ToArraySegment(o.start, o.step).ToArray())
                .ToArray();


            fixLoops(bLoops);

            // packed double 4
            var surfaceDimension = reader.ReadDouble().DoubleToInteger().Item1;

            // packed double 5
            var uvOrder = reader.ReadDouble().DoubleToInteger().Map((u, v) => new {u, v});

            // packed double 6
            var uvNumCtrlPoints = reader.ReadDouble().DoubleToInteger().Map((u, v) => new {u, v});

            // packed double 7
            var uvIsPeriodic = reader.ReadDouble().DoubleToInteger().Map((u, v) => new {u, v});

            // surfaceKnotValuesU
            var uKnots = reader.Read(uvOrder.u + uvNumCtrlPoints.u).ToArray();
            
            // surfaceKnotValuesV
            var vKnots = reader.Read(uvOrder.v + uvNumCtrlPoints.v).ToArray();

            // surfaceCtrlPoinCoords
            var surfaceCtrlPoints = reader.Read(surfaceDimension*uvNumCtrlPoints.u*uvNumCtrlPoints.v)
                .Buffer(surfaceDimension, surfaceDimension)
                .Select(ToRationalVector4WithWeighRescaling)
                .ToList();

            // packed doubles 8 
            // TODO handle the case for multiple surfaces
            var indexFlags = reader.ReadDouble().DoubleToInteger().Map((nSurface, index) => new {nSurface, index});

            var ctrlPointsArray = surfaceCtrlPoints.Reshape(uvNumCtrlPoints.u, uvNumCtrlPoints.v);

            var bSurface = new BSplineSurface(ctrlPointsArray,uvOrder.u, uvOrder.v,uKnots, vKnots, surfaceDimension, uvIsPeriodic.u ==1, uvIsPeriodic.v==1);

            return new BSplineFace(bSurface, bLoops);

        }
 public BSplineFace(BSplineSurface surface, IReadOnlyList<IReadOnlyList<BSpline2D>> trimLoops)
 {
     Surface = surface;
     TrimLoops = trimLoops;
 }
Exemplo n.º 11
0
 // Use this for initialization
 void Start()
 {
     mcol    = GetComponent <MeshCollider>();
     surface = new BSplineSurface();
 }
Exemplo n.º 12
0
        public override void OnPaint()
        {
            switch (ActiveSurface)
            {
            case SurfaceKind.ArrayExtruder:
                xyArray A = new xyArray();
                A.data = new xy[] { new xy(0, 0), new xy(0, 4), new xy(4, 4), new xy(4, 0), new xy(0, 0) };
                ArrayExtruder AE = new ArrayExtruder();
                AE.Array  = A;
                AE.Height = 5;
                AE.Paint(this);
                break;

            case SurfaceKind.BezierSurface:
                Lights[0].Position = DefaultLightPos;
                BezierSurface Bezs = new BezierSurface();
                Bezs.ControlPoints = new xyz[, ] {
                    { new xyz(0, 0, 0), new xyz(2, 0, 0), new xyz(4, 0, 0), new xyz(6, 0, 0) },
                    { new xyz(0, 3, 1), new xyz(2, 3, 3), new xyz(4, 3, 0), new xyz(6, 3, 0) },
                    { new xyz(0, 6, 1), new xyz(2, 6, 3), new xyz(4, 6, 0), new xyz(6, 6, 0) },
                    { new xyz(0, 9, 0), new xyz(2, 9, 0), new xyz(4, 9, 0), new xyz(6, 9, 0) },
                };


                Bezs.Paint(this);
                break;

            case SurfaceKind.BsplineSurface:
                BSplineSurface BS = new BSplineSurface();

                Lights[0].Position = DefaultLightPos;
                BS.ControlPoints   = new xyz[, ] {
                    { new xyz(0, 0, 0), new xyz(0, 3, 0), new xyz(0, 6, 0), new xyz(0, 9, 0), },
                    { new xyz(2, 0, 1), new xyz(2, 3, 3), new xyz(2, 6, 3), new xyz(2, 9, 0), },
                    { new xyz(4, 0, 1), new xyz(4, 3, 3), new xyz(4, 6, 3), new xyz(4, 9, 0), },
                    { new xyz(6, 0, 0), new xyz(6, 3, 0), new xyz(6, 6, 0), new xyz(6, 9, 0) },
                };

                BS.UDegree = 3;
                BS.VDegree = 2;
                BS.SetDefaultKnots();



                BS.Paint(this);
                break;

            case SurfaceKind.Cone:
                Lights[0].Position = new xyzwf(5, 0, (float)2.5, 1);
                Cone Cone = new Cone(new xyz(0, 0, 0), 5, 2, System.Math.PI / 4);
                Cone.Paint(this);
                break;

            case SurfaceKind.Curve2DRotator:
                Lights[0].Position = DefaultLightPos;
                Curve2dRotator C2d = new Curve2dRotator();
                C2d.Curve     = new Bezier(new xy(1, 0), new xy(2, 3), new xy(4, 3), new xy(6, 0));;
                C2d.FromAngle = 0;
                C2d.ToAngle   = Math.PI / 2;

                C2d.Paint(this);
                break;

            case SurfaceKind.Curve3dRotator:
                Curve3dRotator C3d = new Curve3dRotator();
                C3d.Curve = new Bezier3D(new xyz(1, 2, 0), new xyz(2, 1, 3), new xyz(4, 2, 3), new xyz(5, 2, 0));
                C3d.Paint(this);
                break;

            case SurfaceKind.CurveExtruder:
                Lights[0].Position = new xyzwf(3, 6, 3, 1);
                CurveExtruder CE = new CurveExtruder();
                CE.Curve     = new Bezier(new xy(1, 0), new xy(2, 3), new xy(4, 3), new xy(6, 0));
                CE.UpPlane   = new Plane(new xyz(0, 0, 4), new xyz(0, 0.3, 1));
                CE.DownPlane = new Plane(new xyz(0, 0, 0), new xyz(0, -0.3, 1));
                CE.Height    = -5;
                CE.Paint(this);
                break;

            case SurfaceKind.CustomSurface:
                Lights[0].Position = new xyzwf(8, 1, 2, 1);
                CustomSurface.Paint(this);
                break;

            case SurfaceKind.Cylinder:
                Lights[0].Position = new xyzwf(8, 1, 2, 1);
                Cylinder Cylinder = new Cylinder(3, 5);
                drawSurface(Cylinder);
                break;

            case SurfaceKind.NurbsSurface:
                NurbsSurface NS = new NurbsSurface();
                Lights[0].Position = DefaultLightPos;
                NS.ControlPoints   = new xyz[, ] {
                    { new xyz(0, 0, 0), new xyz(0, 3, 0), new xyz(0, 6, 0), new xyz(0, 9, 0), },
                    { new xyz(2, 0, 1), new xyz(2, 3, 3), new xyz(2, 6, 3), new xyz(2, 9, 0), },
                    { new xyz(4, 0, 1), new xyz(4, 3, 3), new xyz(4, 6, 3), new xyz(4, 9, 0), },
                    { new xyz(6, 0, 0), new xyz(6, 3, 0), new xyz(6, 6, 0), new xyz(6, 9, 0) },
                };

                NS.UDegree = 3;
                NS.VDegree = 2;
                NS.Weights = new double[, ] {
                    { 1, 1, 1, 1 },
                    { 1, 3, 3, 1 },
                    { 1, 3, 3, 1 },
                    { 1, 1, 1, 1 }
                };
                NS.SetDefaultKnots();
                NS.Paint(this);
                break;

            case SurfaceKind.OffsetSurface:
                Lights[0].Position = DefaultLightPos;
                BSplineSurface _BS = new BSplineSurface();
                _BS.ControlPoints = new xyz[, ] {
                    { new xyz(0, 0, 0), new xyz(0, 3, 0), new xyz(0, 6, 0), new xyz(0, 9, 0), },
                    { new xyz(2, 0, 1), new xyz(2, 3, 3), new xyz(2, 6, 3), new xyz(2, 9, 0), },
                    { new xyz(4, 0, 1), new xyz(4, 3, 3), new xyz(4, 6, 3), new xyz(4, 9, 0), },
                    { new xyz(6, 0, 0), new xyz(6, 3, 0), new xyz(6, 6, 0), new xyz(6, 9, 0) },
                };

                _BS.UDegree = 3;
                _BS.VDegree = 2;
                _BS.SetDefaultKnots();

                OffsetSurface OS = new OffsetSurface();
                OS.BasisSurface = _BS;
                OS.Distance     = 0.5;
                OS.Paint(this);


                break;

            case SurfaceKind.PlaneSurface:
                Lights[0].Position = new xyzwf(2, 2, 4, 1);
                PlaneSurface PS = new PlaneSurface(new xyz(0, 0, 0), new xyz(4, 2, 2), new xyz(1, 5, 7));
                PS.Width  = 4;
                PS.Length = 5;
                PS.Paint(this);
                break;

            case SurfaceKind.SmoothPlane:
                Lights[0].Position = new xyzwf(2, 2, 4, 1);
                SmoothPlane SP = new SmoothPlane(new xyz(0, 0, 0), new xyz(0, 5, 0), new xyz(5, 5, 0), new xyz(5, 0, 0), new xyz(-1, -1, 1), new xyz(-1, 1, 1), new xyz(1, 1, 1), new xyz(1, 1, 1));
                SP.Width  = 5;
                SP.Length = 6;
                SP.Paint(this);
                break;

            case SurfaceKind.Sphere:
                Lights[0].Position = DefaultLightPos;
                Sphere Sphere = new Sphere(new xyz(2, 1, 1), 4);
                drawSurface(Sphere);

                break;

            case SurfaceKind.Torus:
                Lights[0].Position = DefaultLightPos;
                Torus Torus = new Torus(1, 4);
                drawSurface(Torus);
                break;

            default:
                break;
            }
            base.OnPaint();
        }