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);
        }
예제 #2
0
        public void FitRect()
        {
            //     y
            //     ^
            //     |
            // 0 ---- 3
            // |   c--|-----> x
            // 1 ---- 2
            //
            // build local coordinate
            Vector3 vx = this.cornerPoints3d[2] - this.cornerPoints3d[1];

            vx.Normalize();
            Vector3         vy = Vector3.Cross(this.Normal, vx);
            CoordinateFrame localcoordinate = new CoordinateFrame(vx, vy, this.center);

            List <Vector2> corner2d = new List <Vector2>();

            foreach (Vector3 p in this.cornerPoints3d)
            {
                corner2d.Add(localcoordinate.WorldToLocal(p));
            }

            Line2 axisx = new Line2(Vector2.zero, new Vector2(1, 0));

            Line2 l01 = new Line2(corner2d[0], corner2d[1]);
            Line2 l23 = new Line2(corner2d[2], corner2d[3]);

            Line2 l12 = new Line2(corner2d[1], corner2d[2]);
            Line2 l03 = new Line2(corner2d[0], corner2d[3]);

            Vector2 mean01 = axisx.Intersection(l01);
            Vector2 mean23 = axisx.Intersection(l23);

            Vector2 new0 = l03.ProjToLine(mean01);
            Vector2 new1 = l12.ProjToLine(mean01);
            Vector2 new2 = l12.ProjToLine(mean23);
            Vector2 new3 = l03.ProjToLine(mean23);

            this.cornerPoints3d.Clear();
            this.cornerPoints3d.Add(localcoordinate.LocalToWorld(new0));
            this.cornerPoints3d.Add(localcoordinate.LocalToWorld(new1));
            this.cornerPoints3d.Add(localcoordinate.LocalToWorld(new2));
            this.cornerPoints3d.Add(localcoordinate.LocalToWorld(new3));

            this.ComputeCenter();
            this.GetSideLength();
        }
        public void Project3dToBelongPlane()
        {
            // prepare 2d coordinate
            MyVector3 circle_norm = this.belongPlane.Normal();
            MyVector3 X           = new MyVector3(1, 0, 0);
            MyVector3 Y           = new MyVector3(0, 1, 0);
            MyVector3 Z           = new MyVector3(0, 0, 1);
            MyVector3 rotAxis     = Z.Cross(circle_norm).Normalize();
            double    angle_cos   = Z.Dot(circle_norm);

            if (angle_cos > 1)
            {
                angle_cos = 1;
            }
            if (angle_cos < -1)
            {
                angle_cos = -1;
            }
            double rotAngle = Math.Acos(angle_cos);

            MyMatrix4d      Mat   = MyMatrix4d.RotationMatrix(rotAxis, rotAngle);
            MyVector3       X_new = (Mat * new MyVector4(X)).XYZ().Normalize();
            MyVector3       Y_new = (Mat * new MyVector4(Y)).XYZ().Normalize();
            CoordinateFrame frame = new CoordinateFrame(this.belongPlane.planeCenter, X_new, Y_new, circle_norm);

            // projection and denoise(leave out far away points)
            MyVector3 tmpc = frame.GetPointLocalCoord(this.center);

            this.center2d = new MyVector2(tmpc.x, tmpc.y);
            for (int i = 0; i < this.circleSlice3d.Count; i++)
            {
                MyVector3 vert = this.circleSlice3d[i];
                MyVector3 tmp  = frame.GetPointLocalCoord(vert);
                MyVector2 tmp2 = new MyVector2(tmp.x, tmp.y);
                double    dist = Math.Abs((tmp2 - this.center2d).Length() - this.radius);
                if (dist > 0.5 * this.radius && dist < 0.75 * this.radius)
                {
                    this.circleSlice3d.RemoveAt(i);
                    i--;
                }
                else
                {
                    this.circleSlice2d.Add(tmp2);
                }
            }
        }
        public MyPlane(MyVector3 point, MyVector3 normal)
        {
            double d = -point.Dot(normal);

            planeEquation = new Plane((float)normal.x, (float)normal.y, (float)normal.z, (float)d);
            planeCenter   = point;
            planeColor    = Color.FromArgb(150, 255, 255, 0);

            // add three plane points
            MyVector3 x = new MyVector3(normal.y, -normal.x, 0);

            if (x.x == 0 && x.y == 0)
            {
                x = new MyVector3(1, 0, 0);
            }
            MyVector3       y     = normal.Cross(x).Normalize();
            CoordinateFrame frame = new CoordinateFrame(point, x, y, normal);
            double          s     = 0.8;
            MyVector3       v1    = frame.GetPointLocalCoord(new MyVector3(s, s, 0));
            MyVector3       v2    = frame.GetPointLocalCoord(new MyVector3(-s, s, 0));
            MyVector3       v3    = frame.GetPointLocalCoord(new MyVector3(-s, -s, 0));
            MyVector3       v4    = frame.GetPointLocalCoord(new MyVector3(s, -s, 0));

            planeVertices.Add(v1);
            planeVertices.Add(v2);
            planeVertices.Add(v3);
            planeVertices.Add(v4);

            planePoints.Add(v1);
            planePoints.Add(v2);
            planePoints.Add(v3);
            planePoints.Add(v4);

            this.ComputeVertices();
            this.ComputeBoundQuad();
            this.ComputeCenter();
        }
예제 #5
0
        public void BuildLocalFrame()
        {
            MyVector3 X         = new MyVector3(1, 0, 0);
            MyVector3 Y         = new MyVector3(0, 1, 0);
            MyVector3 Z         = new MyVector3(0, 0, 1);
            MyVector3 rotAxis   = Z.Cross(normal).Normalize();
            double    angle_cos = Z.Dot(normal);

            if (angle_cos < -1)
            {
                angle_cos = -1;
            }
            if (angle_cos > 1)
            {
                angle_cos = 1;
            }
            double rotAngle = Math.Acos(angle_cos);

            MyMatrix4d Mat   = MyMatrix4d.RotationMatrix(rotAxis, rotAngle);
            MyVector3  X_new = (Mat * new MyVector4(X)).XYZ().Normalize();
            MyVector3  Y_new = (Mat * new MyVector4(Y)).XYZ().Normalize();

            frame = new CoordinateFrame(center, X_new, Y_new, normal);
        }