// clip the segments of e by the planes of polyhedron a.
void clip_segments(const polyhedron &ph, MySegments &is, MySegments &os)
{
	const MyArrayPoly &p = ph.p;

	for (unsigned int i = 0; i < is.size(); i+=2) {
		idVec4 a = is[i  ];
		idVec4 b = is[i+1];
		idVec4 c;

		bool discard = false;

		for (unsigned int j = 0; j < p.size(); j++) {
			float da = a * p[j].plane;
			float db = b * p[j].plane;
			float rdw = 1/(da - db);

			int code = 0;

			if (da > 0)
				code = 2;

			if (db > 0)
				code |= 1;


			switch (code) {
				case 3:
					discard = true;
					break;

				case 2:
					c = -db * rdw * a + da * rdw * b;
					a = c;
					break;

				case 1:
					c = -db * rdw * a + da * rdw * b;
					b = c;
					break;

				case 0:
					break;

				default:
					common->Printf("bad clip code!\n");
					break;
			}

			if (discard)
				break;
		}

		if (! discard) {
			os.push_back(a);
			os.push_back(b);
		}
	}

}
void polyhedron_edges( polyhedron &a, MySegments &e ) {
	e.empty();
	if( a.e.size() == 0 && a.p.size() != 0 ) {
		a.compute_neighbors();
	}
	for( unsigned int i = 0; i < a.e.size(); i++ ) {
		e.push_back( a.v[a.e[i].vi[0]] );
		e.push_back( a.v[a.e[i].vi[1]] );
	}
}
void draw_segments( const viewDef_t *viewDef, const MySegments & s, idVec4 color )
{
	for(unsigned int i = 0; i < s.size(); i+=2)
	{
		viewDef->renderWorld->DebugLine( color, v4to3(s[i]), v4to3(s[i+1]));
	}
}