Example #1
0
        private static CSG extrude(IVector3d dir, Polygon polygon1)
        {
            List <Polygon> newPolygons = new List <Polygon>();

            if (dir.z() < 0)
            {
                throw new ArgumentException("z < 0 currently not supported for extrude: " + dir);
            }

            newPolygons.AddRange(PolygonUtil.concaveToConvex(polygon1));
            Polygon polygon2 = polygon1.translated(dir);

            int numvertices = polygon1.vertices.Count;

            for (int i = 0; i < numvertices; i++)
            {
                int nexti = (i + 1) % numvertices;

                IVector3d bottomV1 = polygon1.vertices[i].pos;
                IVector3d topV1    = polygon2.vertices[i].pos;
                IVector3d bottomV2 = polygon1.vertices[nexti].pos;
                IVector3d topV2    = polygon2.vertices[nexti].pos;

                List <IVector3d> pPoints = new List <IVector3d> {
                    bottomV2, topV2, topV1, bottomV1
                };

                newPolygons.Add(Polygon.fromPoints(pPoints, polygon1.getStorage()));
            }

            polygon2 = polygon2.flipped();
            List <Polygon> topPolygons = PolygonUtil.concaveToConvex(polygon2);

            newPolygons.AddRange(topPolygons);

            return(CSG.fromPolygons(newPolygons));
        }
Example #2
0
        private static List <Polygon> extrude(IVector3d dir, Polygon polygon1, bool top, bool bottom)
        {
            List <Polygon> newPolygons = new List <Polygon>();


            if (bottom)
            {
                newPolygons.AddRange(PolygonUtil.concaveToConvex(polygon1));
            }

            Polygon polygon2 = polygon1.translated(dir);

            Transform rot = Transform.unity();

            IVector3d a = polygon2.getPlane().getNormal().normalized();
            IVector3d b = dir.normalized();

            IVector3d c = a.crossed(b);

            double l = c.magnitude(); // sine of angle

            if (l > 1e-9)
            {
                IVector3d axis  = c.times(1.0 / l);
                double    angle = a.angle(b);

                double sx = 0;
                double sy = 0;
                double sz = 0;

                int n = polygon2.vertices.Count;

                foreach (Vertex v in polygon2.vertices)
                {
                    sx += v.pos.x();
                    sy += v.pos.y();
                    sz += v.pos.z();
                }

                IVector3d center = Vector3d.xyz(sx / n, sy / n, sz / n);

                rot = rot.rot(center, axis, angle * Math.PI / 180.0);

                foreach (Vertex v in polygon2.vertices)
                {
                    v.pos = rot.transform(v.pos);
                }
            }

            int numvertices = polygon1.vertices.Count;

            for (int i = 0; i < numvertices; i++)
            {
                int nexti = (i + 1) % numvertices;

                IVector3d bottomV1 = polygon1.vertices[i].pos;
                IVector3d topV1    = polygon2.vertices[i].pos;
                IVector3d bottomV2 = polygon1.vertices[nexti].pos;
                IVector3d topV2    = polygon2.vertices[nexti].pos;

                List <IVector3d> pPoints = new List <IVector3d> {
                    bottomV2, topV2, topV1, bottomV1
                };

                newPolygons.Add(Polygon.fromPoints(pPoints, polygon1.getStorage()));
            }

            polygon2 = polygon2.flipped();
            List <Polygon> topPolygons = PolygonUtil.concaveToConvex(polygon2);

            if (top)
            {
                newPolygons.AddRange(topPolygons);
            }

            return(newPolygons);
        }