Example #1
0
 private void Attemptation_Click(object sender, EventArgs e)
 {
     if (!AttemptationStatus)
     {
         AttemptationStatus = true;
         Attemptation.Text  = "END";
         CommentBox.Visible = true;
         CommentBox.Enabled = true;
         Comment.Enabled    = true;
         Comment.Visible    = true;
         Dual.Add(Dual[NumbersOfGame].Control.Practice());
         AttemptationIndexArray[NumbersOfGame + 1] = NumbersOfGame;
         NumbersOfGame++;
         Invalidate();
     }
     else if (AttemptationStatus)
     {
         Recover.Visible    = true;
         Recover.Enabled    = true;
         AttemptationStatus = false;
         Attemptation.Text  = "Attempt";
         Dual.Add(Dual[AttemptationIndexArray[NumbersOfGame]]);
         NumbersOfGame++;
         Invalidate();
         //Dual[NumbersOfGame].Paint.Drawboard();
         //Dual[NumbersOfGame].Paint.Drawchess(Dual[NumbersOfGame].Black);
         //Dual[NumbersOfGame].Paint.Drawchess(Dual[NumbersOfGame].White);
     }
 }
        static void Main(string[] args)
        {
            var S   = 100.0;
            var K   = 110.0;
            var r   = 0.07;
            var vol = 0.2;
            var T   = 0.25;

            var S_Dual   = new Dual(S, 1);
            var K_Dual   = new Dual(K, 0);
            var r_Dual   = new Dual(r, 0);
            var vol_Dual = new Dual(vol, 0);
            var T_Dual   = new Dual(T, 0);

            //Func<double, double> bs = (s) => BlackScholes.VanillaCall(s, K, vol, T, r);
            using (var _ = new StopwatchEx("BlackSholes"))
            {
                foreach (var __ in Enumerable.Range(0, 1))
                {
                    var p_Dual = BlackScholesDual.VanillaCall(S_Dual, K_Dual, vol_Dual, T_Dual, r_Dual);
                    var p      = BlackScholes.VanillaCall(S, K, vol, T, r);
                    var delta  = BlackScholes.Delta(S, K, vol, T, r);
                    //var delta_fd = FiniteDifference(bs, S, 1e-13);

                    Console.WriteLine(p_Dual);
                    Console.WriteLine("Price ={0}, Delta = {1}", p, delta);
                    //Console.WriteLine("Delta(FD) = {0}", delta_fd);
                }
            }
        }
Example #3
0
        public static void BasicTests()
        {
            Assert.Equal(1, new Dual(1).Magnitude);
            Assert.Equal(3, (new Dual(1) + new Dual(2)).Magnitude);
            Assert.Equal(new Dual(1), new Dual(1));
            Assert.NotEqual(new Dual(2), new Dual(1));
            var x = new Dual(2);

            Assert.Equal(4, (x + x).Magnitude);
            Assert.Equal(0, (x + x).Derivative);
            Assert.Equal(0, (x - x).Magnitude);
            Assert.Equal(0, (-x + x).Magnitude);
            Assert.Equal(6, (x * 3).Magnitude);
            Assert.Equal(6, (3 * x).Magnitude);
            Assert.Equal(Math.Sin(x.Magnitude), x.Sin().Magnitude);
            Assert.Equal(Math.Cos(x.Magnitude), x.Cos().Magnitude);
            Assert.Equal(4, (x ^ 2).Magnitude);
            Assert.Equal(4, (x * x).Magnitude);
            Assert.Equal(x, x);
            Assert.Equal(0, x.CompareTo(x));
            //Assert.Equal(default, x - x);
            Assert.Equal(Math.Cos(3), Calculus.DerivativeAt(3, (Dual x) => x.Cos()).Magnitude);
            Assert.Equal(-Math.Sin(3), Calculus.DerivativeAt(3, (Dual x) => x.Cos()).Derivative);
            Assert.Equal(Math.Sin(3), Calculus.DerivativeAt(3, (Dual x) => x.Sin()).Magnitude);
            Assert.Equal(Math.Cos(3), Calculus.DerivativeAt(3, (Dual x) => x.Sin()).Derivative);
        }
Example #4
0
        public void PlayBack(Game GameToPlayBack)
        {
            PlayBackStatus = true;
            PlaybackGame   = GameToPlayBack;
            Dual.Add(GameToPlayBack.Control.Practice());
            Dual[NumbersOfGame].Paint.Drawboard();
            NumbersOfGame++;
            for (int i = 0; i < Dual.Count; i++)
            {
                if (Dual[i].Equals(GameToPlayBack))
                {
                    AttemptationIndexArray[NumbersOfGame] = i;
                }
            }
            //AttemptationIndexArray[NumbersOfGame]
            AttemptationStatus   = true;
            Recover.Visible      = false;
            Recover.Enabled      = false;
            Attemptation.Visible = false;

            //ReStart.Visible = false;
            //ReStart.Enabled = false;
            Next.Visible       = true;
            Next.Enabled       = true;
            Previous.Visible   = true;
            Previous.Enabled   = true;
            CommentBox.Visible = true;
            Comment.Visible    = true;
            Comment.Enabled    = true;
            CommentBox.Enabled = true;
            CommentBox.Text    = GameToPlayBack.Comments[GameToPlayBack.InitialStep];
        }
Example #5
0
 public static Dual <Offer <SL, SR>, Select <TL, TR> > Offer <SL, SR, TL, TR>(Dual <SL, TL> contL, Dual <SR, TR> contR)
     where SL : SessionType
     where SR : SessionType
     where TL : SessionType
     where TR : SessionType
 {
     throw new NotImplementedException();
 }
Example #6
0
        // (Mutual) Recursions ====

        public static DualEnv <Env <S0, S1>, Env <T0, T1> > Arrange <S0, S1, T0, T1>(Dual <S0, T0> p0, Dual <S1, T1> p1)
            where S0 : SessionType
            where S1 : SessionType
            where T0 : SessionType
            where T1 : SessionType
        {
            throw new NotImplementedException();
        }
Example #7
0
 static void Main(string[] args)
 {
     var dual = new Dual();
     ITest test = dual;
     test.Test();
     ITest2 test2 = dual;
     test2.Test();
     Console.ReadLine();
 }
Example #8
0
        public void LineLine()
        {
            // a*e01 + b*e01 + c*e02 + d*e23 + e*e31 + f*e12
            var l1 = new Line(1f, 0f, 0f, 3f, 2f, 1f);
            var l2 = new Line(0f, 1f, 0f, 4f, 1f, -2f);

            Dual l1l2 = l1 ^ l2;

            Assert.AreEqual(6f, l1l2.e0123);
        }
Example #9
0
        public void PointPlane()
        {
            // x*e_032 + y*e_013 + z*e_021 + e_123
            var p1 = new Point(-2f, 1f, 4f);
            // d*e_0 + a*e_1 + b*e_2 + c*e_3
            var p2 = new Plane(1f, 2f, 3f, 4f);

            Dual p1p2 = p1 ^ p2;

            Assert.AreEqual(-16f, p1p2.e0123);
        }
Example #10
0
        public void TestDual()
        {
            var a = new Dual<Scalar>(new Scalar(1), new Scalar(2));
            var b = a.Pow(new Dual<Scalar>(new Scalar(2), new Scalar(2)));

            Assert.AreEqual(1, b.A.Value);
            Assert.AreEqual(4, b.B.Value);

            var c = a.Log();
            Assert.True(c.A.Value == 0);
            Assert.True(c.B.Value == 2);
        }
Example #11
0
        public async Task sendnotif(ActionContext ac)
        {
            long[] key   = ac.Query[nameof(key)];
            short  notif = 0;

            if (ac.GET)
            {
                ac.GivePane(200, m =>
                {
                    if (key == null)
                    {
                        m.CALLOUT("请先选择目标订单");
                    }
                    else
                    {
                        m.FORM_();
                        m.RADIOS(nameof(notif), notif, NOTIFS, label: "通知内容", required: true);
                        m._FORM();
                    }
                });
            }
            else
            {
                var f = await ac.ReadAsync <Form>();

                notif = f[nameof(notif)];
                List <Dual <long, string> > rows = new List <Dual <long, string> >(16);
                using (var dc = ac.NewDbContext())
                {
                    dc.Sql("SELECT id, wx FROM orders WHERE id")._IN_(key);
                    if (dc.Query())
                    {
                        while (dc.Next())
                        {
                            long   id;
                            string wx;
                            dc.Let(out id).Let(out wx);
                            rows.Add(new Dual <long, string>(id, wx));
                        }
                    }
                }

                for (int i = 0; i < rows.Count; i++)
                {
                    Dual <long, string> row = rows[i];
                    await WeiXinUtility.PostSendAsync(row.B, "【商家通知】" + NOTIFS[notif] + "(订单编号:" + row.A + ")");
                }

                ac.GivePane(200);
            }
        }
Example #12
0
        public void IdealLineLine()
        {
            // a*e01 + b*e01 + c*e02 + d*e23 + e*e31 + f*e12
            var l1 = new Line(1f, 0f, 0f, 3f, 2f, 1f);
            // a*e01 + b*e02 + c*e03
            var l2 = new IdealLine(-4f, -5f, -6f);

            Dual l1l2 = l1 ^ l2;

            Assert.AreEqual(-28f, l1l2.e0123);
            Assert.AreEqual(0f, l1l2.Scalar);

            Dual l2l1 = l2 ^ l1;

            Assert.AreEqual(-28f, l1l2.e0123);
            Assert.AreEqual(0f, l2l1.Scalar);
        }
Example #13
0
        /// <summary>
        /// Creates the cell geometry as polySurfaces
        /// </summary>
        /// <param name="randomCol">If true each group of faces will be with a random colour</param>
        /// <returns>The guids for each cell</returns>
        public IList <Guid> SaveAsCells(bool randomCol = false, bool asMesh = false)
        {
            var    doc       = Rhino.RhinoDoc.ActiveDoc;
            var    foamCells = new List <Guid>();
            string primal    = SerializeJson();
            string dual      = Dual?.SerializeJson() ?? "";
            bool   foamSaved = false;

            foreach (var cell in Cells)
            {
                Color color = Color.FromArgb(rand.Next(50, 200), rand.Next(50, 200), rand.Next(50, 200));


                var attributes = new Rhino.DocObjects.ObjectAttributes
                {
                    ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject,
                    Name        = cell.Id.ToString()
                };
                if (randomCol)
                {
                    attributes.ObjectColor = color;
                }

                GeometryBase cellGeo;

                if (!asMesh)
                {
                    cellGeo = cell.CreateBrepMatched();
                }
                else
                {
                    cellGeo = cell.CreateCellMesh();
                }
                if (!foamSaved && !cell.Exterior)
                {
                    cellGeo.UserDictionary.Set("Primal", primal);
                    cellGeo.UserDictionary.Set("Dual", dual);
                    foamSaved = true;
                }
                foamCells.Add(doc.Objects.Add(cellGeo, attributes));
            }
            doc.Groups.Add(Id.ToString(), foamCells);

            return(foamCells);
        }
Example #14
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (!AttemptationStatus)
     {
         NumbersOfGame++;
         Condition = true;
         Dual.Add(new Game(Vertex, SizePerLine, this));
         Dual[NumbersOfGame].Paint.Drawboard();
         Invalidate();
     }
     else
     {
         Dual.Add(Dual[AttemptationIndexArray[NumbersOfGame]].Control.Practice());
         AttemptationIndexArray[NumbersOfGame + 1] = AttemptationIndexArray[NumbersOfGame];
         NumbersOfGame++;
         Dual[AttemptationIndexArray[NumbersOfGame]].Paint.Drawchess(Dual[NumbersOfGame].Black);
         Dual[AttemptationIndexArray[NumbersOfGame]].Paint.Drawchess(Dual[NumbersOfGame].White);
         Invalidate();
     }
 }//restart
Example #15
0
        public Dictionary <string, object> SerializeDict()
        {
            //TODO see what happens for nulls
            Dictionary <string, object> props = new Dictionary <string, object>
            {
                { "Id", Id },
                { "Vertices", Vertices.Select(x => x.Id).ToList() },
                { "Edges", Edges.Select(x => x.Id).ToList() },
                { "Faces", Faces.Select(x => x.Id).ToList() },
                // neighbors is not used and not serialized
                { "Centroid", PVToDict(Centroid) },
                // FMesh is not serialized - it will be recomputed
                { "Dual", Dual.Select(x => x.Id).ToList() },
                { "Exterior", Exterior }
            };



            return(props);
        }
Example #16
0
 private void DualPlayer_MouseClick(object sender, MouseEventArgs e)
 {
     if (!AttemptationStatus)
     {
         if (Condition)
         {
             int ocassion = Dual[NumbersOfGame].Control.Put(e.Location);//判断是否继续游戏
             if (ocassion == 1)
             {
                 NumbersOfGame++;
                 Dual.Add(new Game(Vertex, SizePerLine, this));
             }
             if (ocassion == -1)
             {
                 Condition = false;
                 //MessageBox.Show({"The Score if "+Score[0].ToString()+" : "+Score[1].ToString(), FinalScore);
             }
         }
     }
     if (AttemptationStatus)
     {
         if (Condition)
         {
             int ocassion = Dual[NumbersOfGame].Control.Put(e.Location);
             Dual[NumbersOfGame].Paint.DrawchessWithNumber(Dual[NumbersOfGame]);
             if (ocassion == 1)
             {
                 NumbersOfGame++;
                 Dual[NumbersOfGame].Paint.Drawboard();
                 Dual[NumbersOfGame].Paint.Drawchess(Dual[NumbersOfGame].Black);
                 Dual[NumbersOfGame].Paint.Drawchess(Dual[NumbersOfGame].White);
                 Dual.Add(Dual[AttemptationIndexArray[NumbersOfGame]].Control.Practice());
             }
             if (ocassion == -1)
             {
                 Condition = false;
             }
         }
     }
 }
        public void TestCase()
        {
            var a = new Scalar(0.5);
            var b = new Complex<Scalar>(new Scalar(0.5), new Scalar(0.0));
            var c = new Dual<Scalar>(new Scalar(0.5), new Scalar(0.0));

            var aSin = a.Sin();
            var bSin = b.Sin();
            var cSin = c.Sin();
            Assert.True(aSin.Value == bSin.A.Value);
            Assert.True(aSin.Value == cSin.A.Value);

            var aCos = a.Cos();
            var bCos = b.Cos();
            var cCos = c.Cos();
            Assert.True(aCos.Value == bCos.A.Value);
            Assert.True(aCos.Value == cCos.A.Value);

            var aSinh = a.Sinh();
            var bSinh = b.Sinh();
            var cSinh = c.Sinh();
            Assert.True(aSinh.Value == bSinh.A.Value);
            Assert.True(aSinh.Value == cSinh.A.Value);

            var aCosh = a.Cosh();
            var bCosh = b.Cosh();
            var cCosh = c.Cosh();
            Assert.True(aCosh.Value == bCosh.A.Value);
            Assert.True(aCosh.Value == cCosh.A.Value);

            var aExp = a.Exp();
            var bExp = b.Exp();
            var cExp = c.Exp();
            Assert.True(aExp.Value == bExp.A.Value);
            Assert.True(aExp.Value == cExp.A.Value);
        }
Example #18
0
 public static Session <S, S> ForkThread <S, T>(this Dual <S, T> p, Action <Session <T, T> > func)
     where S : SessionType
     where T : SessionType
 {
     throw new NotImplementedException();
 }
Example #19
0
 public static Dual VanillaCall(Dual S, Dual K, Dual vol, Dual T, Dual r)
 {
     return(S * Normsdist(D1(S, K, vol, T, r)) - K * MathDual.Exp(-1.0 * r * T) * Normsdist(D2(S, K, vol, T, r)));
 }
Example #20
0
 private static Dual D1(Dual S, Dual K, Dual vol, Dual T, Dual r)
 {
     return((MathDual.Log(S / K) + (r + vol * vol / 2) * T) / (vol * MathDual.Sqrt(T)));
 }
Example #21
0
 private static Dual D2(Dual S, Dual K, Dual vol, Dual T, Dual r)
 {
     return(D1(S, K, vol, T, r) - vol * MathDual.Sqrt(T));
 }
Example #22
0
 private static Dual Normsdist(Dual z)
 {
     return(1 / (1.0 + MathDual.Exp(-1.7 * z)));
 }
Example #23
0
        /// <summary>
        /// Saves the foam as a group of color-coded lines in the Rhino document
        /// For each line a user dictionary is saved with the following entries:
        /// EdgeId, StartVertexId, EndVertexId
        /// The first line contains a JSON serialization for the whole foam +
        /// If available the dual will also be saved
        /// </summary>
        public IList <Guid> SaveAsEdges()
        {
            // first determine if form or force
            // if foam has naked edges then it is form else it is force

            var doc = Rhino.RhinoDoc.ActiveDoc;

            doc.Views.RedrawEnabled = false;
            var form = Edges.Where(e => e.Id > 0).Any(x => x.Faces.Count < 2);

            var positiveEdges = Edges.Where(x => x.Id > 0);
            var edgeUserDict  = new Dictionary <int, Dictionary <string, int> >();

            bool foamSaved = false;

            var guids = new List <Guid>();

            string primal = SerializeJson();
            string dual   = Dual?.SerializeJson() ?? "";


            // pre-make the data for the user dictionary for each edge
            foreach (var edge in positiveEdges)
            {
                var edgeDict = new Dictionary <string, int>();
                edgeDict.Add("Id", edge.Id);
                edgeDict.Add("V0", edge.Vertices[0].Id);
                edgeDict.Add("V1", edge.Vertices[1].Id);
                edgeUserDict.Add(edge.Id, edgeDict);
            }

            // handling different behaviors for form and force
            if (form)
            {
                var intEdges     = new List <PFEdge>();
                var halfExtEdges = new List <PFEdge>();
                var fullExtEdges = new List <PFEdge>();

                foreach (var edge in positiveEdges)
                {
                    if (edge.Id > 0)
                    {
                        if (edge.Vertices[0].External && edge.Vertices[1].External)
                        {
                            fullExtEdges.Add(edge);
                        }
                        else if (edge.Vertices[0].External ^ edge.Vertices[1].External)
                        {
                            halfExtEdges.Add(edge);
                        }
                        else
                        {
                            intEdges.Add(edge);
                        }
                    }
                }

                var areas   = intEdges.Select(x => x.Dual?.Area ?? 0.0);
                var maxArea = areas.Count() > 0 ? areas.Max() : 1.0;
                var minArea = areas.Count() > 0 ? areas.Min() : 0.0;
                // create all layers
                var intLineLayer = new Rhino.DocObjects.Layer()
                {
                    Name = "_Form_InternalLines"
                };
                if (doc.Layers.All(x => x.Name != intLineLayer.Name))
                {
                    doc.Layers.Add(intLineLayer);
                }
                intLineLayer = doc.Layers.First(x => x.Name == "_Form_InternalLines");

                var extForceLayer = new Rhino.DocObjects.Layer()
                {
                    Name = "_Form_ExternalForces"
                };
                if (doc.Layers.All(x => x.Name != extForceLayer.Name))
                {
                    doc.Layers.Add(extForceLayer);
                }
                extForceLayer = doc.Layers.First(x => x.Name == "_Form_ExternalForces");

                var externalPolyLayer = new Rhino.DocObjects.Layer()
                {
                    Name = "_Form_ExternalPoly"
                };
                if (doc.Layers.All(x => x.Name != externalPolyLayer.Name))
                {
                    doc.Layers.Add(externalPolyLayer);
                }
                externalPolyLayer = doc.Layers.First(x => x.Name == "_Form_ExternalPoly");



                // handle the interior edges
                foreach (var edge in intEdges)
                {
                    var attributes = new Rhino.DocObjects.ObjectAttributes
                    {
                        ObjectColor = Util.CreateBlue(Util.ValueUnitizer(edge.Dual?.Area ?? 1.0, new List <double> {
                            minArea, maxArea
                        }, new List <double> {
                            0.0, 1.0
                        })),
                        PlotWeight = Math.Round(Util.ValueUnitizer(edge.Dual?.Area ?? 1.0, new List <double> {
                            minArea, maxArea
                        }, new List <double> {
                            0, 11
                        }) * 0.05 + 0.15),
                        ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject,
                        Name        = edge.Id.ToString(),
                        LayerIndex  = intLineLayer.LayerIndex,
                    };
                    var lineCrv = edge.CreateLine().ToNurbsCurve();

                    foreach (var keyVal in edgeUserDict[edge.Id])
                    {
                        lineCrv.UserDictionary.Set(keyVal.Key, keyVal.Value);
                    }

                    // the foam will be serialized with the first interior edge
                    // this will include the dual if present
                    if (!foamSaved)
                    {
                        lineCrv.UserDictionary.Set("Primal", primal);
                        lineCrv.UserDictionary.Set("Dual", dual);
                        foamSaved = true;
                    }

                    guids.Add(doc.Objects.AddCurve(lineCrv, attributes));
                }

                // handle applied forces - or half ext edges

                foreach (var edge in halfExtEdges)
                {
                    var attributes = new Rhino.DocObjects.ObjectAttributes
                    {
                        ObjectColor = System.Drawing.Color.FromArgb(0, 100, 0),

                        ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject,
                        Name        = edge.Id.ToString(),
                        LayerIndex  = extForceLayer.LayerIndex,
                    };
                    if (edge.Vertices[1].External)
                    {
                        attributes.ObjectDecoration = Rhino.DocObjects.ObjectDecoration.StartArrowhead;
                    }
                    else
                    {
                        attributes.ObjectDecoration = Rhino.DocObjects.ObjectDecoration.EndArrowhead;
                    }

                    var lineCrv = edge.CreateLine().ToNurbsCurve();

                    foreach (var keyVal in edgeUserDict[edge.Id])
                    {
                        lineCrv.UserDictionary.Set(keyVal.Key, keyVal.Value);
                    }
                    if (!foamSaved)
                    {
                        lineCrv.UserDictionary.Set("Primal", primal);
                        lineCrv.UserDictionary.Set("Dual", dual);
                        foamSaved = true;
                    }

                    guids.Add(doc.Objects.AddCurve(lineCrv, attributes));
                }


                // handle external polyhedron or full external edges

                foreach (var edge in fullExtEdges)
                {
                    var attributes = new Rhino.DocObjects.ObjectAttributes
                    {
                        ObjectColor = System.Drawing.Color.LightGray,

                        ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject,
                        Name        = edge.Id.ToString(),
                        LayerIndex  = externalPolyLayer.LayerIndex,
                    };
                    var lineCrv = edge.CreateLine().ToNurbsCurve();
                    foreach (var keyVal in edgeUserDict[edge.Id])
                    {
                        lineCrv.UserDictionary.Set(keyVal.Key, keyVal.Value);
                    }
                    guids.Add(doc.Objects.AddCurve(lineCrv, attributes));
                }
            }
            else
            {
                var forceLineLayer = new Rhino.DocObjects.Layer()
                {
                    Name = "_Force_Lines"
                };
                if (doc.Layers.All(x => x.Name != forceLineLayer.Name))
                {
                    doc.Layers.Add(forceLineLayer);
                }
                forceLineLayer = doc.Layers.First(x => x.Name == "_Force_Lines");


                foreach (var edge in positiveEdges)
                {
                    var attributes = new Rhino.DocObjects.ObjectAttributes
                    {
                        ObjectColor = System.Drawing.Color.DarkSlateGray,

                        ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject,
                        Name        = edge.Id.ToString(),
                        LayerIndex  = forceLineLayer.LayerIndex,
                    };

                    var lineCrv = edge.CreateLine().ToNurbsCurve();
                    foreach (var keyVal in edgeUserDict[edge.Id])
                    {
                        lineCrv.UserDictionary.Set(keyVal.Key, keyVal.Value);
                    }

                    // the foam will be serialized with the first interior edge
                    // this will include the dual if present
                    if (!foamSaved)
                    {
                        lineCrv.UserDictionary.Set("Primal", primal);
                        lineCrv.UserDictionary.Set("Dual", dual);
                        foamSaved = true;
                    }



                    guids.Add(doc.Objects.AddCurve(lineCrv, attributes));
                }
            }


            doc.Views.RedrawEnabled = true;
            doc.Groups.Add(Id.ToString(), guids);
            return(guids);
        }
Example #24
0
 public void Visit(Dual dual)
 {
     _dual = true;
 }
Example #25
0
        /// <summary>
        /// Saves the foam object as a set of trimmed surfaces (one faced breps)
        /// For each brep it saves an entry in the user dictionary with the faceId
        /// For each vertex in the brep as Point an entry in the user dictionary is saved with the PFvertex Id
        /// </summary>
        /// <returns></returns>
        public IList <Guid> SaveAsFaces(bool asMesh = false)
        {
            // first determine if form or force
            // if foam has naked edges then it is form else it is force

            var doc = Rhino.RhinoDoc.ActiveDoc;

            doc.Views.RedrawEnabled = false;
            var form = Edges.Where(e => e.Id > 0).Any(x => x.Faces.Count < 2);

            var positiveFaces = Faces.Where(x => x.Id > 0);
            //var faceUserDict = new Dictionary<int, List<int>>();

            bool foamSaved = false;

            var guids = new List <Guid>();

            string primal = SerializeJson();
            string dual   = Dual?.SerializeJson() ?? "";



            if (form)
            {
                // if the foam is a form then just save everything with the same color
                // except the exterior faces (applied forces and supports that will be printed with a different color

                var intFaces = new List <PFFace>();
                var extFaces = new List <PFFace>();

                foreach (var face in positiveFaces)
                {
                    if (face.External)
                    {
                        extFaces.Add(face);
                    }
                    else
                    {
                        intFaces.Add(face);
                    }
                }

                var intFaceLayer = new Rhino.DocObjects.Layer()
                {
                    Name = "_Form_InternalFaces"
                };
                if (doc.Layers.All(x => x.Name != intFaceLayer.Name))
                {
                    doc.Layers.Add(intFaceLayer);
                }
                intFaceLayer = doc.Layers.First(x => x.Name == "_Form_InternalFaces");

                var extFaceLayer = new Rhino.DocObjects.Layer()
                {
                    Name = "_Form_ExternalFaces"
                };
                if (doc.Layers.All(x => x.Name != extFaceLayer.Name))
                {
                    doc.Layers.Add(extFaceLayer);
                }
                extFaceLayer = doc.Layers.First(x => x.Name == "_Form_ExternalFaces");



                // go through internal faces
                foreach (var face in intFaces)
                {
                    var attributes = new Rhino.DocObjects.ObjectAttributes
                    {
                        ObjectColor = System.Drawing.Color.SlateGray,

                        ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject,
                        Name        = face.Id.ToString(),
                        LayerIndex  = intFaceLayer.LayerIndex,
                    };

                    GeometryBase faceGeo;
                    if (!asMesh)
                    {
                        faceGeo = face.CreateBrepMatched();
                    }
                    else
                    {
                        faceGeo = face.FMesh;
                    }
                    // the foam will be serialized with the first interior face
                    // this will include the dual if present
                    if (!foamSaved)
                    {
                        faceGeo.UserDictionary.Set("Primal", primal);
                        faceGeo.UserDictionary.Set("Dual", dual);
                        foamSaved = true;
                    }

                    guids.Add(doc.Objects.Add(faceGeo, attributes));
                }

                // go through the ext faces
                foreach (var face in extFaces)
                {
                    var attributes = new Rhino.DocObjects.ObjectAttributes
                    {
                        ObjectColor = System.Drawing.Color.LightSlateGray,

                        ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject,
                        Name        = face.Id.ToString(),
                        LayerIndex  = extFaceLayer.LayerIndex,
                    };

                    GeometryBase faceGeo;
                    if (!asMesh)
                    {
                        faceGeo = face.CreateBrepMatched();
                    }
                    else
                    {
                        faceGeo = face.FMesh;
                    }

                    if (!foamSaved)
                    {
                        faceGeo.UserDictionary.Set("Primal", primal);
                        faceGeo.UserDictionary.Set("Dual", dual);
                        foamSaved = true;
                    }


                    guids.Add(doc.Objects.Add(faceGeo, attributes));
                }
            }
            else // if force
            {
                var maxArea        = Faces.Select(x => x.Area).Max();
                var minArea        = Faces.Select(x => x.Area).Min();
                var forceFaceLayer = new Rhino.DocObjects.Layer()
                {
                    Name = "_Force_Faces"
                };
                if (doc.Layers.All(x => x.Name != forceFaceLayer.Name))
                {
                    doc.Layers.Add(forceFaceLayer);
                }
                forceFaceLayer = doc.Layers.First(x => x.Name == "_Force_Faces");

                foreach (var face in positiveFaces)
                {
                    var attributes = new Rhino.DocObjects.ObjectAttributes
                    {
                        ObjectColor = Util.CreateBlue(Util.ValueUnitizer(face.Area,
                                                                         new List <double> {
                            minArea, maxArea
                        }, new List <double> {
                            0.0, 1.0
                        })),

                        ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject,
                        Name        = face.Id.ToString(),
                        LayerIndex  = forceFaceLayer.LayerIndex,
                    };



                    GeometryBase faceGeo;
                    if (!asMesh)
                    {
                        faceGeo = face.CreateBrepMatched();
                    }
                    else
                    {
                        faceGeo = face.FMesh;
                    }
                    // the foam will be serialized with the first interior face
                    // this will include the dual if present
                    if (!foamSaved)
                    {
                        faceGeo.UserDictionary.Set("Primal", primal);
                        faceGeo.UserDictionary.Set("Dual", dual);
                        foamSaved = true;
                    }
                    guids.Add(doc.Objects.Add(faceGeo, attributes));
                }
            }

            doc.Views.RedrawEnabled = true;
            doc.Groups.Add(Id.ToString(), guids);


            return(guids);
        }
Example #26
0
 static Dual Func(Dual x) =>
 (x ^ 2) + 1;
Example #27
0
        public static void TestPow()
        {
            var x = new Dual(25);

            Assert.Equal(x * x, x ^ 2);
        }
Example #28
0
 // Same as Quintile, just refactored to ensure outcome is the same
 static Dual Quintile2(Dual x) =>
 (x ^ 3) * ((x ^ 2) + 2);
Example #29
0
 public static Dual <Offer <S0, S1, S2, S3>, Select <T0, T1, T2, T3> > Offer <S0, S1, S2, S3, T0, T1, T2, T3>(Dual <S0, T0> cont0, Dual <S1, T1> cont1, Dual <S2, T2> cont2, Dual <S3, T3> cont03)
     where S0 : SessionType
     where S1 : SessionType
     where S2 : SessionType
     where S3 : SessionType
     where T0 : SessionType
     where T1 : SessionType
     where T2 : SessionType
     where T3 : SessionType
 {
     throw new NotImplementedException();
 }
Example #30
0
 public void SetNormalToDual()
 {
     Normal = Dual.GetDirectionVector();
 }
Example #31
0
 static Dual Quintile(Dual x) =>
 (x ^ 5) + 2 * (x ^ 3);
Example #32
0
 public virtual void Visit(Dual dual)
 {
 }