Example #1
0
 /// <summary>
 /// Overrides <see cref="CADability.GeoObject.ISurfaceImpl.SameGeometry (BoundingRect, ISurface, BoundingRect, double, out ModOp2D)"/>
 /// </summary>
 /// <param name="thisBounds"></param>
 /// <param name="other"></param>
 /// <param name="otherBounds"></param>
 /// <param name="precision"></param>
 /// <param name="firstToSecond"></param>
 /// <returns></returns>
 public override bool SameGeometry(BoundingRect thisBounds, ISurface other, BoundingRect otherBounds, double precision, out ModOp2D firstToSecond)
 {
     firstToSecond = ModOp2D.Null;
     if (other is PlaneSurface)
     {
         PlaneSurface second = other as PlaneSurface;
         GeoPoint     p      = PointAt(thisBounds.GetCenter());
         if (Math.Abs(second.Plane.Distance(p)) > precision)
         {
             return(false);
         }
         p = second.PointAt(otherBounds.GetCenter());
         if (Math.Abs(Plane.Distance(p)) > precision)
         {
             return(false);
         }
         if (Precision.SameDirection(Normal, second.Normal, false))
         {
             GeoPoint2D[] src = new GeoPoint2D[] { GeoPoint2D.Origin, new GeoPoint2D(1, 0), new GeoPoint2D(0, 1) };
             GeoPoint2D[] dst = new GeoPoint2D[3];
             for (int i = 0; i < 3; ++i)
             {
                 dst[i] = second.PositionOf(PointAt(src[i]));
             }
             firstToSecond = ModOp2D.Fit(src, dst, true);
             return(true);
         }
         else
         {
             return(false);
         }
     }
     return(base.SameGeometry(thisBounds, other, otherBounds, precision, out firstToSecond));
 }
        /// <summary>
        /// Overrides <see cref="CADability.GeoObject.ISurfaceImpl.HitTest (BoundingCube, out GeoPoint2D)"/>
        /// </summary>
        /// <param name="bc"></param>
        /// <param name="uv"></param>
        /// <returns></returns>
        public override bool HitTest(BoundingCube bc, out GeoPoint2D uv)
        {
            Plane        p  = new Plane(GeoPoint.Origin, direction);
            PlaneSurface ps = new PlaneSurface(p);
            ICurve2D     c  = basisCurve.GetProjectedCurve(p);

            GeoPoint[]   points   = bc.Points;
            GeoPoint2D[] points2D = new GeoPoint2D[8];
            for (int i = 0; i < 8; ++i)
            {
                points2D[i] = ps.PositionOf(points[i]);
            }
            // does c hit the polygon?
            int[,] l = bc.LineNumbers;
            for (int k = 0; k < 12; ++k)
            {
                int      i  = l[k, 0];
                int      j  = l[k, 1];
                ICurve2D c2 = new Line2D(points2D[i], points2D[j]);
                if (c2.Length > 0)
                {
                    GeoPoint2DWithParameter[] list = c.Intersect(c2);
                    for (int m = 0; m < list.Length; ++m)
                    {
                        GeoPoint2D d0 = list[m].p;
                        double     d1 = (points2D[i] - d0).Length;
                        double     d2 = (points2D[j] - d0).Length;
                        double     d3 = (points2D[i] - points2D[j]).Length;
                        if (Math.Abs(d1 + d2 - d3) < Precision.eps)
                        {
                            if (d3 < Precision.eps)
                            {
                                throw new Exception();
                            }
                            GeoPoint gp = points[i] + (d1 / d3) * (points[j] - points[i]);
                            uv = PositionOf(gp);
                            return(true);
                        }
                    }
                }
            }
            // is c in the polygon?
            GeoPoint e   = ps.PointAt(c.EndPoint);
            bool     res = (bc.Interferes(e, direction, bc.Size * 1e-8, false));

            if (res)
            {   // nur berechnen, wenn auch gültig
                uv = PositionOf(e);
            }
            else
            {
                uv = GeoPoint2D.Origin;
            }
            return(res);
        }
Example #3
0
        /// <summary>
        /// Overrides <see cref="CADability.GeoObject.ISurfaceImpl.GetPlaneIntersection (PlaneSurface, double, double, double, double, double)"/>
        /// </summary>
        /// <param name="pl"></param>
        /// <param name="umin"></param>
        /// <param name="umax"></param>
        /// <param name="vmin"></param>
        /// <param name="vmax"></param>
        /// <param name="precision"></param>
        /// <returns></returns>
        public override IDualSurfaceCurve[] GetPlaneIntersection(PlaneSurface pl, double umin, double umax, double vmin, double vmax, double precision)
        {
            PlaneSurface plm = pl.GetModified(toUnitPlane) as PlaneSurface;
            GeoVector    dir = GeoVector.ZAxis ^ plm.Normal.Normalized; // Richtung der Linie

            dir = toUnitPlane * (Normal.Normalized ^ pl.Normal.Normalized);
            GeoVector2D dir2d = new GeoVector2D(dir.x, dir.y);

            if (dir.Length > 0.0)
            {
                GeoPoint2D[] ip = plm.GetLineIntersection(GeoPoint.Origin, new GeoVector(dir.y, -dir.x, dir.z)); // dir.z müsste ohnehin 0 sein
                if (ip.Length > 0)
                {
                    GeoPoint   org = plm.PointAt(ip[0]);
                    GeoPoint2D org2d = new GeoPoint2D(org.x, org.y);
                    double     lmin, lmax;
                    if (Math.Abs(dir.x) > Math.Abs(dir.y))
                    {   // eher waagrecht
                        lmin = (umin - org.x) / dir.x;
                        lmax = (umax - org.x) / dir.x;
                    }
                    else
                    {
                        lmin = (vmin - org.y) / dir.y;
                        lmax = (vmax - org.y) / dir.y;
                    }
                    ICurve2D curve2d1 = new Line2D(org2d + lmin * dir2d, org2d + lmax * dir2d);
                    Line     line     = Line.Construct();
                    line.SetTwoPoints(PointAt(curve2d1.StartPoint), PointAt(curve2d1.EndPoint));
                    ICurve2D curve2d2 = new Line2D(pl.PositionOf(line.StartPoint), pl.PositionOf(line.EndPoint));
                    return(new DualSurfaceCurve[] { new DualSurfaceCurve(line, this, curve2d1, pl, curve2d2) });
                }
            }
            return(new DualSurfaceCurve[] { });
            //return base.GetPlaneIntersection(pl, umin, umax, vmin, vmax);
        }
        public override IDualSurfaceCurve[] GetPlaneIntersection(PlaneSurface pl, double umin, double umax, double vmin, double vmax, double precision)
        {
            if (Precision.IsPerpendicular(pl.Normal, zAxis, false))
            {
                // two lines along the cylinder axis
                Plane               lower = new Plane(location, zAxis);
                GeoPoint2D          sp2d  = lower.Project(pl.Location);
                GeoVector2D         dir2d = lower.Project(pl.Normal).ToLeft();
                GeoPoint2D[]        ips   = Geometry.IntersectLC(sp2d, dir2d, GeoPoint2D.Origin, xAxis.Length);
                IDualSurfaceCurve[] res   = new IDualSurfaceCurve[ips.Length];
                for (int i = 0; i < ips.Length; i++)
                {
                    GeoPoint p   = lower.ToGlobal(ips[i]);
                    Line     l3d = Line.TwoPoints(p, p + zAxis);
                    res[i] = new DualSurfaceCurve(l3d, this, new Line2D(this.PositionOf(l3d.StartPoint), this.PositionOf(l3d.EndPoint)), pl, new Line2D(pl.PositionOf(l3d.StartPoint), pl.PositionOf(l3d.EndPoint)));
                }
                return(res);
            }
            else
            {
                // an ellipse
                GeoPoint2D[] cnts = pl.GetLineIntersection(location, zAxis);
                if (cnts.Length == 1)
                {   // there must be exactly one intersection
                    GeoPoint  cnt       = pl.PointAt(cnts[0]);
                    GeoVector minorAxis = pl.Normal ^ zAxis;
                    minorAxis.Length = xAxis.Length;
                    GeoVector majorAxis = minorAxis ^ pl.Normal;
                    Polynom   impl      = GetImplicitPolynomial();
                    Polynom   toSolve   = impl.Substitute(new Polynom(majorAxis.x, "u", cnt.x, ""), new Polynom(majorAxis.y, "u", cnt.y, ""), new Polynom(majorAxis.z, "u", cnt.z, ""));
                    double[]  roots     = toSolve.Roots();
                    // there must be two roots
                    majorAxis = roots[0] * majorAxis;

                    Ellipse elli = Ellipse.Construct();
                    elli.SetEllipseCenterAxis(cnt, majorAxis, minorAxis);

                    GeoPoint2D[] fpnts = new GeoPoint2D[5];
                    for (int i = 0; i < 5; i++)
                    {
                        fpnts[i] = PositionOf(elli.PointAt(i / 6.0));
                    }
                    Ellipse2D e2d = Ellipse2D.FromFivePoints(fpnts); // there should be a better way to calculate the 2d ellipse, but the following is wrong:
                    // Ellipse2D e2d = new Ellipse2D(GeoPoint2D.Origin, PositionOf(cnt + majorAxis).ToVector(), PositionOf(cnt + minorAxis).ToVector());
                    // and principal axis doesn't yield the correct result either
                    // Geometry.PrincipalAxis(PositionOf(cnt + majorAxis).ToVector(), PositionOf(cnt + minorAxis).ToVector(), out GeoVector2D maj, out GeoVector2D min);
                    return(new IDualSurfaceCurve[] { new DualSurfaceCurve(elli, this, e2d, pl, pl.GetProjectedCurve(elli, 0.0)) });
                }
            }
            return(new IDualSurfaceCurve[0]);
        }