public void ComputeBoundQuad()
        {
            MyVector3 normal  = Normal();
            MyVector3 xaxis   = new MyVector3(1, 0, 0);
            MyVector3 yaxis   = new MyVector3(0, 1, 0);
            MyVector3 zaxis   = new MyVector3(0, 0, 1);
            MyVector3 rotAxis = zaxis.Cross(normal).Normalize();

            double cos = zaxis.Dot(normal);

            if (cos > 1)
            {
                cos = 1;
            }
            if (cos < -1)
            {
                cos = -1;
            }
            double          rotAngle  = Math.Acos(cos);
            MyMatrix4d      R         = MyMatrix4d.RotationMatrix(rotAxis, rotAngle);
            MyVector3       new_xaxis = (R * new MyVector4(xaxis)).XYZ().Normalize();
            MyVector3       new_yaxis = (R * new MyVector4(yaxis)).XYZ().Normalize();
            CoordinateFrame frame     = new CoordinateFrame(this.planeCenter, new_xaxis, new_yaxis, normal);

            double xmin = double.MaxValue; double ymin = double.MaxValue;
            double xmax = double.MinValue; double ymax = double.MinValue;

            foreach (MyVector3 v in this.planeVertices)
            {
                MyVector3 u = frame.GetPointLocalCoord(v);
                xmin = Math.Min(u.x, xmin);
                xmax = Math.Max(u.x, xmax);
                ymin = Math.Min(u.y, ymin);
                ymax = Math.Max(u.y, ymax);
            }

            MyVector3 v1 = new MyVector3(xmin, ymin, 0);
            MyVector3 v2 = new MyVector3(xmax, ymin, 0);
            MyVector3 v3 = new MyVector3(xmax, ymax, 0);
            MyVector3 v4 = new MyVector3(xmin, ymax, 0);

            SmartCanvas.Point3[] pts3 =
                new SmartCanvas.Point3[4] {
                new SmartCanvas.Point3(frame.GetPointSpaceCoord(v1)),
                new SmartCanvas.Point3(frame.GetPointSpaceCoord(v2)),
                new SmartCanvas.Point3(frame.GetPointSpaceCoord(v3)),
                new SmartCanvas.Point3(frame.GetPointSpaceCoord(v4))
            };
            this.boundQuad = new Quad3D(pts3);
        }
Beispiel #2
0
        public void FindBoundPolygon()
        {
            // find convexhull
            PointF[] ps = new PointF[this.slicePoints2d.Count];
            for (int i = 0; i < this.slicePoints2d.Count; i++)
            {
                PointF p = new PointF((float)this.slicePoints2d[i].x, (float)this.slicePoints2d[i].y);
                ps[i] = p;
            }
            PointF[] hull = CvInvoke.ConvexHull(ps);

            // find boundary polygon
            VectorOfPointF hull2 = new VectorOfPointF();

            hull2.Push(hull);
            VectorOfPointF poly = new VectorOfPointF();

            // when inferring # of polygon edge, the 3-rd param can be [0.0005,0.0015], than choose the best(how to define "best"??)
            CvInvoke.ApproxPolyDP(hull2, poly, 0.0003, true);
            for (int i = 0; i < poly.Size; i++)
            {
                this.cornerPoints2d.Add(new MyVector2(poly[i].X, poly[i].Y));
            }

            // unproject to 3d
            foreach (MyVector2 corner2d in this.cornerPoints2d)
            {
                MyVector3 corner3d = frame.GetPointSpaceCoord(new MyVector3(corner2d, 0.0));
                this.cornerPoints3d.Add(corner3d);
            }
        }