Esempio n. 1
0
IntersectType clipPoly3D(const Points3D& points,
	const Imath::Plane3<typename points_adaptor<Points3D>::scalar>& plane,
	std::list<ClippedPoly<typename points_adaptor<Points3D>::scalar> >& result,
	detail::ClippingContext<typename points_adaptor<Points3D>::scalar>* ctxt)
{
	typedef points_adaptor<Points3D> 			Adaptor;
	typedef typename Adaptor::scalar			T;
	typedef Imath::Vec2<T> 						vec2_type;
	typedef Imath::Vec3<T> 						vec3_type;
	typedef typename Adaptor::const_elem_ref	vec3_const_ref;
	typedef ClippedPoly<T>						polyclip_type;
	Adaptor a(points);

	vec3_type polyNorm;
	calc_normal(points, polyNorm);
	vec3_const_ref p0 = a[0];

	// project poly and cutting plane into 2D
	std::vector<vec2_type> points2D;
	Line2<T> plane2D;

	{
		Imath::Line3<T> planesInt;
		Imath::Plane3<T> polyPlane(p0, polyNorm);
		if(intersect(plane, polyPlane, planesInt))
		{
			Projection_2D<T> proj(polyNorm, &plane.normal);
			proj.project(planesInt, plane2D);
			proj.project(points, points2D);
		}
		else
		{
			// poly and cutting plane are parallel, so poly is either all out or all in
			if((ctxt)? ctxt->isPointInside(a.index(0)) : isInside(plane, p0))
			{
				result.push_back(polyclip_type());
				result.back().makeInside(points);
				return INTERSECT_INSIDE;
			}
			else
				return INTERSECT_OUTSIDE;
		}
	}

	// do the clip in 2D.
	if(a.is_indexed())
	{
		points_replacer<Points3D, std::vector<vec2_type> > points2D_(points, points2D, false);
		return clipPoly2D(points2D_, plane2D, result, ctxt);
	}
	else
		return clipPoly2D(points2D, plane2D, result, ctxt);
}
Esempio n. 2
0
void Polyhedron::add(const QVector<Vec3f> &verts, const Vec3f &normal)
{
	if(verts.size() < 3) return;

	SPolygon p;

	for(int i=0; i<verts.size(); i++)
	{
		p.addUniqueVert(verts[i]);
	}

	if(p.vertices.size() < 3)
	{
		return;
	}

	//Check normal direction
	Plane polyPlane(p.vertices[0], p.vertices[1], p.vertices[2], SPolygon::CCW);

	//Might need to reverse the vertex order
	if(polyPlane.normal.dot(normal) < 0.0f) p.reverseOrder();

	polygons.append(p);
}