示例#1
0
        private Object3d ConvertFrom(List <csgjs_polygon> lstply)
        {
            Object3d obj = new Object3d();

            for (int i = 0; i < lstply.Count; i++)
            {
                csgjs_polygon poly = lstply[i];

                for (int j = 2; j < poly.vertices.Count; j++)
                {
                    Polygon ply = new Polygon(); // create a new polygon
                    ply.m_points = new Point3d[3];
                    obj.m_lstpolys.Add(ply);     //add it to the list

                    Point3d p0 = new Point3d();
                    Point3d p1 = new Point3d();
                    Point3d p2 = new Point3d();

                    p0.Set(poly.vertices[0].pos.x, poly.vertices[0].pos.y, poly.vertices[0].pos.z);
                    p1.Set(poly.vertices[j - 1].pos.x, poly.vertices[j - 1].pos.y, poly.vertices[j - 1].pos.z);
                    p2.Set(poly.vertices[j].pos.x, poly.vertices[j].pos.y, poly.vertices[j].pos.z);

                    ply.m_points[0] = p0;
                    ply.m_points[1] = p1;
                    ply.m_points[2] = p2;

                    obj.m_lstpoints.Add(p0);
                    obj.m_lstpoints.Add(p1);
                    obj.m_lstpoints.Add(p2);
                }
            }
            obj.Update();
            return(obj);
        }
 public csgjs_polygon clone() 
 {
     csgjs_polygon c = new csgjs_polygon();
     foreach (csgjs_vertex v in vertices) 
     {
         c.vertices.Add(v.clone());
     }
     c.plane = plane.clone();
     return c;
 }
示例#3
0
        public csgjs_polygon clone()
        {
            csgjs_polygon c = new csgjs_polygon();

            foreach (csgjs_vertex v in vertices)
            {
                c.vertices.Add(v.clone());
            }
            c.plane = plane.clone();
            return(c);
        }
示例#4
0
        private List <csgjs_polygon> ConvertTo(Object3d obj)
        {
            List <csgjs_polygon> polys = new List <csgjs_polygon>();

            foreach (Polygon p in obj.m_lstpolys)
            {
                List <csgjs_vertex> list = new List <csgjs_vertex>();
                list.Add(new csgjs_vertex(p.m_points[0]));
                list.Add(new csgjs_vertex(p.m_points[1]));
                list.Add(new csgjs_vertex(p.m_points[2]));
                csgjs_polygon newpoly = new csgjs_polygon(list); // create  from a list of vertexes to initialize plane
                polys.Add(newpoly);
            }
            return(polys);
        }
示例#5
0
        public static csgjs_model csgjs_modelFromPolygons(List <csgjs_polygon> polygons)
        {
            csgjs_model model = new csgjs_model();
            int         p     = 0;

            for (int i = 0; i < polygons.Count; i++)
            {
                csgjs_polygon poly = polygons[i];

                for (int j = 2; j < poly.vertices.Count; j++)
                {
                    model.vertices.Add(poly.vertices[0]);
                    model.indices.Add(p++);
                    model.vertices.Add(poly.vertices[j - 1]);
                    model.indices.Add(p++);
                    model.vertices.Add(poly.vertices[j]);
                    model.indices.Add(p++);
                }
            }
            return(model);
        }
示例#6
0
 private List<csgjs_polygon> ConvertTo(Object3d obj) 
 {
     List<csgjs_polygon> polys = new List<csgjs_polygon>();
     foreach (Polygon p in obj.m_lstpolys) 
     {
         List<csgjs_vertex> list = new List<csgjs_vertex>();
         list.Add(new csgjs_vertex(p.m_points[0]));
         list.Add(new csgjs_vertex(p.m_points[1]));
         list.Add(new csgjs_vertex(p.m_points[2]));
         csgjs_polygon newpoly = new csgjs_polygon(list); // create  from a list of vertexes to initialize plane
         polys.Add(newpoly);
     }
     return polys;
 }
        // Split `polygon` by this plane if needed, then put the polygon or polygon
        // fragments in the appropriate lists. Coplanar polygons go into either
        // `coplanarFront` or `coplanarBack` depending on their orientation with
        // respect to this plane. Polygons in front or in back of this plane go into
        // either `front` or `back`.
        public void splitPolygon(csgjs_polygon polygon, List <csgjs_polygon> coplanarFront, List <csgjs_polygon> coplanarBack, ref List <csgjs_polygon> front, ref List <csgjs_polygon> back)
        {
            // Classify each point as well as the entire polygon into one of the above
            // four classes.
            int        polygonType = 0;
            List <int> types       = new List <int>();

            for (int i = 0; i < polygon.vertices.Count; i++)
            {
                float t    = csgjs_vector.dot(normal, polygon.vertices[i].pos) - w;
                int   type = (t < -CSG.csgjs_EPSILON) ? BACK : ((t > CSG.csgjs_EPSILON) ? FRONT : COPLANAR);
                polygonType |= type;
                types.Add(type);
            }

            // Put the polygon in the correct list, splitting it when necessary.
            switch (polygonType)
            {
            case COPLANAR:
            {
                if (csgjs_vector.dot(normal, polygon.plane.normal) > 0)
                {
                    coplanarFront.Add(polygon);
                }
                else
                {
                    coplanarBack.Add(polygon);
                }
                break;
            }

            case FRONT:
            {
                front.Add(polygon);
                break;
            }

            case BACK:
            {
                back.Add(polygon);
                break;
            }

            case SPANNING:
            {
                List <csgjs_vertex> f = new List <csgjs_vertex>();
                List <csgjs_vertex> b = new List <csgjs_vertex>();
                for (int i = 0; i < polygon.vertices.Count; i++)
                {
                    int          j = (i + 1) % polygon.vertices.Count;
                    int          ti = types[i], tj = types[j];
                    csgjs_vertex vi = polygon.vertices[i], vj = polygon.vertices[j];
                    if (ti != BACK)
                    {
                        f.Add(vi);
                    }
                    if (ti != FRONT)
                    {
                        b.Add(vi);
                    }
                    if ((ti | tj) == SPANNING)
                    {
                        float        t = (w - csgjs_vector.dot(normal, vi.pos)) / csgjs_vector.dot(normal, vj.pos - vi.pos);
                        csgjs_vertex v = csgjs_vertex.interpolate(vi, vj, t);
                        f.Add(v);
                        b.Add(v);
                    }
                }
                if (f.Count >= 3)
                {
                    front.Add(new csgjs_polygon(f));
                }

                if (b.Count >= 3)
                {
                    back.Add(new csgjs_polygon(b));
                }
                break;
            }
            }
        }
        // Split `polygon` by this plane if needed, then put the polygon or polygon
        // fragments in the appropriate lists. Coplanar polygons go into either
        // `coplanarFront` or `coplanarBack` depending on their orientation with
        // respect to this plane. Polygons in front or in back of this plane go into
        // either `front` or `back`.
        public void splitPolygon(csgjs_polygon  polygon, List<csgjs_polygon> coplanarFront, List<csgjs_polygon> coplanarBack,ref List<csgjs_polygon> front,ref List<csgjs_polygon>  back) 
        {
	        // Classify each point as well as the entire polygon into one of the above
	        // four classes.
	        int polygonType = 0;
            List<int> types = new List<int>();

	        for (int i = 0; i < polygon.vertices.Count; i++) 
	        {
                float t = csgjs_vector.dot(normal, polygon.vertices[i].pos) - w;
                int type = (t < -CSG.csgjs_EPSILON) ? BACK : ((t > CSG.csgjs_EPSILON) ? FRONT : COPLANAR);
		        polygonType |= type;
		        types.Add(type);
	        }

	        // Put the polygon in the correct list, splitting it when necessary.
	        switch (polygonType) 
	        {
	        case COPLANAR:
		        {
                    if (csgjs_vector.dot(normal, polygon.plane.normal) > 0)
				        coplanarFront.Add(polygon);
			        else 
				        coplanarBack.Add(polygon);
			        break;
		        }
	        case FRONT:
		        {
			        front.Add(polygon);
			        break;
		        }
	        case BACK:
		        {
			        back.Add(polygon);
			        break;
		        }
	        case SPANNING:
		        {
			        List<csgjs_vertex> f = new List<csgjs_vertex>();
                    List<csgjs_vertex> b = new List<csgjs_vertex>();
			        for (int i = 0; i < polygon.vertices.Count; i++) 
			        {
				        int j = (i + 1) % polygon.vertices.Count;
				        int ti = types[i], tj = types[j];
				        csgjs_vertex vi = polygon.vertices[i], vj = polygon.vertices[j];
				        if (ti != BACK) 
                            f.Add(vi);
				        if (ti != FRONT) 
                            b.Add(vi);
				        if ((ti | tj) == SPANNING) 
				        {
                            float t = (w - csgjs_vector.dot(normal, vi.pos)) / csgjs_vector.dot(normal, vj.pos - vi.pos);
                            csgjs_vertex v = csgjs_vertex.interpolate(vi, vj, t);
					        f.Add(v);
					        b.Add(v);
				        }
			        }
			        if (f.Count >= 3) 
                        front.Add(new csgjs_polygon(f));

			        if (b.Count >= 3) 
                        back.Add(new csgjs_polygon(b));
			        break;
		        }
	        }
        }