Example #1
0
        public double NormaErrorW2(Function u, Function du, FEMContext FEMContext)
        {
            double norma = 0;

            for (int e = 0; e < FEMContext.Data.NT.GetLength(0); e++)
            {
                Point i     = FEMContext.Data.CT[FEMContext.Data.NT[e].GetVertexID(0)];
                Point j     = FEMContext.Data.CT[FEMContext.Data.NT[e].GetVertexID(1)];
                Point m     = FEMContext.Data.CT[FEMContext.Data.NT[e].GetVertexID(2)];
                Point point = new Point((i.X + j.X + m.X) / 3, (i.Y + j.Y + m.Y) / 3);
                if (TriangleOnPoint(point, i, j, m))
                {
                    double fi  = CalculateArea(point, j, m) * FEMContext.X[FEMContext.Data.NT[e].GetVertexID(0)];
                    double fj  = CalculateArea(i, point, m) * FEMContext.X[FEMContext.Data.NT[e].GetVertexID(1)];
                    double fm  = CalculateArea(i, j, point) * FEMContext.X[FEMContext.Data.NT[e].GetVertexID(2)];
                    double Uh  = (fi + fj + fm) / AreaTriangles[e];
                    double dUh = UhDerivative(e, FEMContext);

                    norma += (Math.Abs(Math.Pow(Uh - u(point.X), 2)) + Math.Abs(Math.Pow(dUh - du(point.X), 2))) * AreaTriangles[e];
                }
                else
                {
                    MessageBox.Show("Exception - (NormaErrorW2)");
                }
            }
            return(norma);
        }
Example #2
0
        private double CalculateArea(Point i, Point j, Point m)
        {
            var matrix = new Matrix(new double[3, 3] {
                { 1, i.X, i.Y }, { 1, j.X, j.Y }, { 1, m.X, m.Y }
            });
            var result = matrix.Determinant3x3();

            return(result / 2);
        }
Example #3
0
        private TriangleNet.Geometry.Point Echelle(TriangleNet.Geometry.Point centre, TriangleNet.Geometry.Point point, double f)
        {
            gVecteur v = new gVecteur(point.X - centre.X, point.Y - centre.Y, 0);

            v.Multiplier(f);
            double x = centre.X + v.X;
            double y = centre.Y + v.Y;

            return(new TriangleNet.Geometry.Point(x, y));
        }
        private Point GetIncenter(ITriangle triangle)
        {
            Point  incenter = new Point();
            Vertex A = triangle.GetVertex(0), B = triangle.GetVertex(1), C = triangle.GetVertex(2);
            double a = GetLength(B, C), b = GetLength(A, C), c = GetLength(A, B);
            double p = a + b + c;

            incenter.X = (a * A.X + b * B.X + c * C.X) / p;
            incenter.Y = (a * A.Y + b * B.Y + c * C.Y) / p;
            return(incenter);
        }
Example #5
0
 private void CalculateAreas(FEMContext FEMContext)
 {
     this.AreaTriangles = new double[FEMContext.Data.NT.GetLength(0)];
     for (int k = 0; k < FEMContext.Data.NT.GetLength(0); k++)
     {
         Point i = FEMContext.Data.CT[FEMContext.Data.NT[k].GetVertexID(0)];
         Point j = FEMContext.Data.CT[FEMContext.Data.NT[k].GetVertexID(1)];
         Point m = FEMContext.Data.CT[FEMContext.Data.NT[k].GetVertexID(2)];
         AreaTriangles[k] = CalculateArea(i, j, m);
     }
 }
Example #6
0
        private TriangleNet.Geometry.Point TransformPt(TriangleNet.Geometry.Point Pt)
        {
            double[] arr = new double[3];
            arr[0] = Pt.X;
            arr[1] = Pt.Y;
            arr[2] = 0;
            MathPoint swPt = (MathPoint)Mu.CreatePoint(arr);

            swPt = swPt.MultiplyTransform(xForm);
            arr  = swPt.ArrayData;

            return(new TriangleNet.Geometry.Point(arr[0], arr[1]));
        }
Example #7
0
 private void CalculateLength(FEMContext FEMContext)
 {
     this.LengthSegments = new double[FEMContext.Data.GT.Length][];
     for (int border = 0; border < FEMContext.Data.GT.Length; border++)
     {
         LengthSegments[border] = new double[FEMContext.Data.GT[border].GetLength(0)];
         for (int k = 0; k < FEMContext.Data.GT[border].GetLength(0); k++)
         {
             Point p1 = FEMContext.Data.CT[FEMContext.Data.GT[border][k, 0]];
             Point p2 = FEMContext.Data.CT[FEMContext.Data.GT[border][k, 1]];
             LengthSegments[border][k] = Length(p1, p2);
         }
     }
 }
Example #8
0
        private bool TriangleOnPoint(Point point, Point i, Point j, Point m)
        {
            double a = Math.Round((i.X - point.X) * (j.Y - i.Y) - (j.X - i.X) * (i.Y - point.Y), 13);

            double b = Math.Round((j.X - point.X) * (m.Y - j.Y) - (m.X - j.X) * (j.Y - point.Y), 13);

            double c = Math.Round((m.X - point.X) * (i.Y - m.Y) - (i.X - m.X) * (m.Y - point.Y), 13);

            if ((a >= 0 && b >= 0 && c >= 0) || (a <= 0 && b <= 0 && c <= 0))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #9
0
        private TriangleNet.Geometry.Point CalculateCenterOfGravity()
        {
            TriangleNet.Geometry.Point[] TVert = new TriangleNet.Geometry.Point[3];
            double[] TSide = new double[4];
            double[] Sum_TCenterXS_TCenterYS_S   = new double[3];
            TriangleNet.Geometry.Point ObjCenter = new TriangleNet.Geometry.Point();

            foreach (Triangle triangle in mesh.Triangles)
            {
                //ulozim vrcholy trojuholnika do pola
                for (int i = 0; i < 3; i++)
                {
                    TVert[i]   = new TriangleNet.Geometry.Point();
                    TVert[i].X = triangle.GetVertex(i).X;
                    TVert[i].Y = triangle.GetVertex(i).Y;
                }
                //dlzka stran do pola + obvod
                TSide[3] = 0;
                for (int i = 0; i < 3; i++)
                {
                    TSide[i]  = Math.Sqrt(Math.Pow((TVert[i].X - TVert[(i + 1) % 3].X), 2) + Math.Pow((TVert[i].Y - TVert[(i + 1) % 3].Y), 2));
                    TSide[3] += TSide[i];
                }

                //ratame obsah - heronov vzorec
                TSide[3] = TSide[3] / 2;
                double TS = 0;
                TS = Math.Sqrt(TSide[3] * (TSide[3] - TSide[0]) * (TSide[3] - TSide[1]) * (TSide[3] - TSide[2]));

                //ratame tazisko trojuholnika
                double[] TC = new double[2];
                TC[0] = TVert[0].X + 2 * ((TVert[1].X + (double)(TVert[2].X - TVert[1].X) / 2) - TVert[0].X) / 3;
                TC[1] = TVert[0].Y + 2 * ((TVert[1].Y + (double)(TVert[2].Y - TVert[1].Y) / 2) - TVert[0].Y) / 3;

                //pomocne sumy pre vyratanie taziska polygonu
                Sum_TCenterXS_TCenterYS_S[0] += TC[0] * TS;
                Sum_TCenterXS_TCenterYS_S[1] += TC[1] * TS;
                Sum_TCenterXS_TCenterYS_S[2] += TS;
            }
            //suradnice taziska polygonu
            ObjCenter.X = Sum_TCenterXS_TCenterYS_S[0] / Sum_TCenterXS_TCenterYS_S[2];
            ObjCenter.Y = Sum_TCenterXS_TCenterYS_S[1] / Sum_TCenterXS_TCenterYS_S[2];

            //procedura vypluje bod = tazisko
            return(new TriangleNet.Geometry.Point(ObjCenter.X, ObjCenter.Y));
        }
Example #10
0
        private double UhDerivative(int numberTriangle, FEMContext FEMContext)
        {
            double UhDer;
            Point  i  = FEMContext.Data.CT[FEMContext.Data.NT[numberTriangle].GetVertexID(0)];
            Point  j  = FEMContext.Data.CT[FEMContext.Data.NT[numberTriangle].GetVertexID(1)];
            Point  m  = FEMContext.Data.CT[FEMContext.Data.NT[numberTriangle].GetVertexID(2)];
            double bI = j.Y - m.Y;
            double bJ = m.Y - i.Y;
            double bM = i.Y - j.Y;

            double Ui    = FEMContext.X[FEMContext.Data.NT[numberTriangle].GetVertexID(0)];
            double Uj    = FEMContext.X[FEMContext.Data.NT[numberTriangle].GetVertexID(1)];
            double Um    = FEMContext.X[FEMContext.Data.NT[numberTriangle].GetVertexID(2)];
            double sigma = 2 * AreaTriangles[numberTriangle];


            UhDer = (Ui * bI + Uj * bJ + Um * bM) / sigma;

            return(UhDer);
        }
        private void DrawCircle(ITriangle triangle)
        {
            Point incenter = GetIncenter(triangle);

            var k = 80.0;

            System.Windows.Point center = new System.Windows.Point
            {
                X = k * incenter.X + 250.0,
                Y = k * incenter.Y + 100.0
            };

            double size = 10;
            //add circle
            Ellipse circle = new Ellipse()
            {
                Width  = size,
                Height = size,
                Stroke = Brushes.Red
            };

            circle.SetValue(Canvas.LeftProperty, center.X - circle.Width / 2);
            circle.SetValue(Canvas.TopProperty, center.Y - circle.Height / 2);
            MainCanvas.Children.Add(circle);
            //add ID
            TextBlock triangleId = new TextBlock();

            triangleId.Text       = triangle.ID.ToString();
            triangleId.Foreground = circle.Stroke;
            Viewbox viewbox = new Viewbox();

            viewbox.Visibility  = Visibility.Visible;
            viewbox.Stretch     = Stretch.Uniform;
            viewbox.Child       = triangleId;
            viewbox.Height      = circle.Height / 2;
            viewbox.Width       = circle.Width / 2;
            viewbox.MouseEnter += ViewBoxOnMouseDown;
            viewbox.SetValue(Canvas.LeftProperty, center.X - viewbox.Width / 2);
            viewbox.SetValue(Canvas.TopProperty, center.Y - viewbox.Height / 2);
            MainCanvas.Children.Add(viewbox);
        }
Example #12
0
        public double NormaW2Exact(Function u, Function du, FEMContext FEMContext)
        {
            double norma = 0;

            for (int e = 0; e < FEMContext.Data.NT.GetLength(0); e++)
            {
                Point i     = FEMContext.Data.CT[FEMContext.Data.NT[e].GetVertexID(0)];
                Point j     = FEMContext.Data.CT[FEMContext.Data.NT[e].GetVertexID(1)];
                Point m     = FEMContext.Data.CT[FEMContext.Data.NT[e].GetVertexID(2)];
                Point point = new Point((i.X + j.X + m.X) / 3, (i.Y + j.Y + m.Y) / 3);
                if (TriangleOnPoint(point, i, j, m))
                {
                    norma += (Math.Pow(u(point.X), 2) + Math.Pow(du(point.X), 2)) * AreaTriangles[e];
                }
                else
                {
                    MessageBox.Show("Exception - (NormaW2_Exact)");
                }
            }
            return(Math.Sqrt(norma));
        }
Example #13
0
        public static bool IsPointInPolygon(TriangleNet.Geometry.Point point, List <Vertex> poly)
        {
            bool inside = false;

            double x = point.X;
            double y = point.Y;

            int count = poly.Count;

            for (int i = 0, j = count - 1; i < count; i++)
            {
                if (((poly[i].Y < y && poly[j].Y >= y) || (poly[j].Y < y && poly[i].Y >= y)) &&
                    (poly[i].X <= x || poly[j].X <= x))
                {
                    inside ^= (poly[i].X + (y - poly[i].Y) / (poly[j].Y - poly[i].Y) * (poly[j].X - poly[i].X) < x);
                }

                j = i;
            }


            return(inside);
        }
Example #14
0
        public double NormaL2(FEMContext FEMContext)
        {
            double norma = 0;

            for (int e = 0; e < FEMContext.Data.NT.GetLength(0); e++)
            {
                Point i     = FEMContext.Data.CT[FEMContext.Data.NT[e].GetVertexID(0)];
                Point j     = FEMContext.Data.CT[FEMContext.Data.NT[e].GetVertexID(1)];
                Point m     = FEMContext.Data.CT[FEMContext.Data.NT[e].GetVertexID(2)];
                Point point = new Point((i.X + j.X + m.X) / 3, (i.Y + j.Y + m.Y) / 3);
                if (TriangleOnPoint(point, i, j, m))
                {
                    double fi = CalculateArea(point, j, m) * FEMContext.X[FEMContext.Data.NT[e].GetVertexID(0)];
                    double fj = CalculateArea(point, m, i) * FEMContext.X[FEMContext.Data.NT[e].GetVertexID(1)];
                    double fm = CalculateArea(point, i, j) * FEMContext.X[FEMContext.Data.NT[e].GetVertexID(2)];
                    norma += Math.Pow((fi + fj + fm) / AreaTriangles[e], 2) * AreaTriangles[e];
                }
                else
                {
                    MessageBox.Show("Exception - (NormaL2)");
                }
            }
            return(Math.Sqrt(norma));
        }
Example #15
0
        /// <summary>
        /// Return true if the given point is inside the polygon, or false if it is not.
        /// </summary>
        /// <param name="point">The point to check.</param>
        /// <param name="poly">The polygon (list of contour points).</param>
        /// <returns></returns>
        /// <remarks>
        /// WARNING: If the point is exactly on the edge of the polygon, then the function
        /// may return true or false.
        ///
        /// See http://alienryderflex.com/polygon/
        /// </remarks>
        public static bool IsPointInPolygon(TriangleNet.Geometry.Point point, SectionContour contour,
                                            List <SectionContour> otherContours)
        {
            bool inside = false;

            double x = point.X;
            double y = point.Y;

            List <Vertex> poly = contour.Points.Select(p => new Vertex(p.X, p.Y)).ToList();

            inside = IsPointInPolygon(point, poly);
            if (inside)
            {
                foreach (var item in otherContours)
                {
                    if (IsPointInPolygon(point, item.Points.Select(p => new Vertex(p.X, p.Y)).ToList()))
                    {
                        inside = false;
                        break;
                    }
                }
            }
            return(inside);
        }
Example #16
0
        protected override void Command()
        {
            Feature Fonction = MdlBase.eSelect_RecupererObjet <Feature>(1, -1);

            MdlBase.eEffacerSelection();

            Esquisse = Fonction.GetSpecificFeature2();
            xForm    = Esquisse.ModelToSketchTransform.Inverse();
            Mu       = App.Sw.GetMathUtility();

            String fact = "0.8";

            if (Interaction.InputBox("Facteur de décalage", "F :", ref fact) == DialogResult.OK)
            {
                if (!String.IsNullOrWhiteSpace(fact))
                {
                    Double r = fact.eToDouble();
                    if (r != 0)
                    {
                        FacteurDecal = r;
                    }
                }
            }

            var polygon = new Polygon();

            if (Esquisse.GetSketchRegionCount() == 0)
            {
                return;
            }

            // On ajoute les contours
            SketchRegion region = Esquisse.GetSketchRegions()[0];
            Loop2        loop   = region.GetFirstLoop();

            int i = 0;

            while (loop != null)
            {
                var ListPt = new List <TriangleNet.Geometry.Vertex>();
                foreach (SolidWorks.Interop.sldworks.Vertex v in loop.GetVertices())
                {
                    double[] pt = (double[])v.GetPoint();
                    ListPt.Add(new TriangleNet.Geometry.Vertex(pt[0], pt[1]));
                }

                polygon.Add(new Contour(ListPt, i++), !loop.IsOuter());

                loop = loop.GetNext();
            }


            foreach (SketchPoint pt in Esquisse.GetSketchPoints2())
            {
                polygon.Add(new TriangleNet.Geometry.Vertex(pt.X, pt.Y));
            }

            var contraintes = new ConstraintOptions();

            contraintes.ConformingDelaunay = true;
            contraintes.SegmentSplitting   = 0;

            //var Qualite = new QualityOptions();
            //Qualite.SteinerPoints = 0;
            Mesh mesh = (Mesh)polygon.Triangulate(contraintes);

            var voronoi = new BoundedVoronoi(mesh);

            var SM = MdlBase.SketchManager;

            SelectionnerSupportEsquisse();
            SM.InsertSketch(false);
            SM.AddToDB          = true;
            SM.DisplayWhenAdded = false;

            List <TriangleNet.Geometry.Vertex> lstVertex = new List <TriangleNet.Geometry.Vertex>();

            foreach (var v in mesh.Vertices)
            {
                lstVertex.Add(v);
            }

            foreach (var edge in mesh.Edges)
            {
                try
                {
                    TriangleNet.Geometry.Point p1 = TransformPt(lstVertex[edge.P0]);
                    TriangleNet.Geometry.Point p2 = TransformPt(lstVertex[edge.P1]);
                    SM.CreateLine(p1.X, p1.Y, 0, p2.X, p2.Y, 0);
                }
                catch (Exception errDesc)
                { this.LogMethode(new Object[] { errDesc }); }
            }

            SM.DisplayWhenAdded = true;
            SM.AddToDB          = false;
            SM.InsertSketch(true);
            MdlBase.eEffacerSelection();

            SelectionnerSupportEsquisse();
            SM.InsertSketch(false);
            SM.AddToDB          = true;
            SM.DisplayWhenAdded = false;

            foreach (var t in mesh.Triangles)
            {
                try
                {
                    TriangleNet.Geometry.Point p1 = TransformPt(lstVertex[t.GetVertexID(0)]);
                    TriangleNet.Geometry.Point p2 = TransformPt(lstVertex[t.GetVertexID(1)]);
                    TriangleNet.Geometry.Point p3 = TransformPt(lstVertex[t.GetVertexID(2)]);

                    TriangleNet.Geometry.Point centre = new TriangleNet.Geometry.Point();
                    centre.X = (p1.X + p2.X + p3.X) / 3.0;
                    centre.Y = (p1.Y + p2.Y + p3.Y) / 3.0;

                    TriangleNet.Geometry.Point pt1;
                    TriangleNet.Geometry.Point pt2;

                    pt1 = TransformPt(Echelle(centre, t.GetVertex(0), FacteurDecal));
                    pt2 = TransformPt(Echelle(centre, t.GetVertex(1), FacteurDecal));
                    SM.CreateLine(pt1.X, pt1.Y, 0, pt2.X, pt2.Y, 0);

                    pt1 = TransformPt(Echelle(centre, t.GetVertex(1), FacteurDecal));
                    pt2 = TransformPt(Echelle(centre, t.GetVertex(2), FacteurDecal));
                    SM.CreateLine(pt1.X, pt1.Y, 0, pt2.X, pt2.Y, 0);

                    pt1 = TransformPt(Echelle(centre, t.GetVertex(2), FacteurDecal));
                    pt2 = TransformPt(Echelle(centre, t.GetVertex(0), FacteurDecal));
                    SM.CreateLine(pt1.X, pt1.Y, 0, pt2.X, pt2.Y, 0);
                }
                catch (Exception errDesc)
                { this.LogMethode(new Object[] { errDesc }); }
            }

            SM.DisplayWhenAdded = true;
            SM.AddToDB          = false;
            SM.InsertSketch(true);
            MdlBase.eEffacerSelection();

            SelectionnerSupportEsquisse();
            SM.InsertSketch(false);
            SM.AddToDB          = true;
            SM.DisplayWhenAdded = false;

            foreach (var f in voronoi.Faces)
            {
                try
                {
                    TriangleNet.Geometry.Point centre = f.generator;

                    foreach (var e in f.EnumerateEdges())
                    {
                        var pt1 = TransformPt(Echelle(centre, e.Origin, FacteurDecal));
                        var pt2 = TransformPt(Echelle(centre, e.Twin.Origin, FacteurDecal));
                        SM.CreateLine(pt1.X, pt1.Y, 0, pt2.X, pt2.Y, 0);
                    }
                }
                catch (Exception errDesc)
                { this.LogMethode(new Object[] { errDesc }); }
            }

            SM.DisplayWhenAdded = true;
            SM.AddToDB          = false;
            SM.InsertSketch(true);
        }
Example #17
0
 public static MyMath.Point getPoint(this TriangleNet.Geometry.Point p)
 {
     return(new MyMath.Point((float)p.X, (float)p.Y));
 }
Example #18
0
 private double Length(Point p1, Point p2)
 {
     return(Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2)));
 }
Example #19
0
        public static TriangleNet.Geometry.Point FindPointInPolygon(SectionContour contour, List <SectionContour> otherContours,
                                                                    int limit, double eps = 2e-5)
        {
            List <Vertex> poly   = contour.Points.Select(p => new Vertex(p.X, p.Y)).ToList();
            var           bounds = new TriangleNet.Geometry.Rectangle();

            bounds.Expand(poly);

            int length = poly.Count;

            var test = new TriangleNet.Geometry.Point();

            TriangleNet.Geometry.Point a, b, c; // Current corner points.

            double bx, by;
            double dx, dy;
            double h;

            var predicates = new RobustPredicates();

            a = poly[0];
            b = poly[1];

            for (int i = 0; i < length; i++)
            {
                c = poly[(i + 2) % length];

                // Corner point.
                bx = b.X;
                by = b.Y;

                // NOTE: if we knew the contour points were in counterclockwise order, we
                // could skip concave corners and search only in one direction.

                h = predicates.CounterClockwise(a, b, c);

                if (Math.Abs(h) < eps)
                {
                    // Points are nearly co-linear. Use perpendicular direction.
                    dx = (c.Y - a.Y) / 2;
                    dy = (a.X - c.X) / 2;
                }
                else
                {
                    // Direction [midpoint(a-c) -> corner point]
                    dx = (a.X + c.X) / 2 - bx;
                    dy = (a.Y + c.Y) / 2 - by;
                }

                // Move around the contour.
                a = b;
                b = c;

                h = 1.0;

                for (int j = 0; j < limit; j++)
                {
                    // Search in direction.
                    test.X = bx + dx * h;
                    test.Y = by + dy * h;

                    if (bounds.Contains(test) && IsPointInPolygon(test, contour, otherContours))
                    {
                        return(test);
                    }

                    // Search in opposite direction (see NOTE above).
                    test.X = bx - dx * h;
                    test.Y = by - dy * h;

                    if (bounds.Contains(test) && IsPointInPolygon(test, contour, otherContours))
                    {
                        return(test);
                    }

                    h = h / 2;
                }
            }

            throw new Exception();
        }