#include <../source/TAL/objbase.h>

vhandle_t V_GLOBAL
dxfOpenWrite(char *name)
{
	vhandle_t dxf = fileOpenWrite(name);
	if(dxf == LIBV_INVALID_HANDLE)
		return LIBV_INVALID_HANDLE;
	filePrintf(dxf,"0\nSECTION\n2\nENTITIES\n");
	return dxf;
}

void V_GLOBAL
dxfLine(vhandle_t dxf,vec3_t p1,vec3_t p2)
{
#ifdef	DXF_GLOBAL_SCALE_FACTOR
	vec3_t p1s, p2s;
	vec3Scale(p1s,p1,DXF_GLOBAL_SCALE_FACTOR);
	vec3Scale(p2s,p2,DXF_GLOBAL_SCALE_FACTOR);
#else	/* !DXF_GLOBAL_SCALE_FACTOR */
#define	p1s p1
#define	p2s p2
#endif	/* !DXF_GLOBAL_SCALE_FACTOR */
	/* Line on layer 1 */
	filePrintf(dxf,"0\nLINE\n8\n1\n"
		"10\n%g\n"
		"20\n%g\n"
		"30\n%g\n"
		"11\n%g\n"
		"21\n%g\n"
		"31\n%g\n",
		p1s[0],p1s[1],p1s[2],p2s[0],p2s[1],p2s[2]);
#ifdef	DXF_GLOBAL_SCALE_FACTOR
#undef	p1s
#undef	p2s
#endif	/* DXF_GLOBAL_SCALE_FACTOR */
}

void V_GLOBAL
dxfQuad(vhandle_t dxf,vec3_t p1,vec3_t p2,vec3_t p3,vec3_t p4)
{
#ifdef	DXF_GLOBAL_SCALE_FACTOR
	vec3_t p1s, p2s, p3s, p4s;
	vec3Scale(p1s,p1,DXF_GLOBAL_SCALE_FACTOR);
	vec3Scale(p2s,p2,DXF_GLOBAL_SCALE_FACTOR);
	vec3Scale(p3s,p3,DXF_GLOBAL_SCALE_FACTOR);
	vec3Scale(p4s,p4,DXF_GLOBAL_SCALE_FACTOR);
#else	/* !DXF_GLOBAL_SCALE_FACTOR */
#define	p1s p1
#define	p2s p2
#define	p3s p3
#define	p4s p4
#endif	/* !DXF_GLOBAL_SCALE_FACTOR */
	/* Quad on layer 1 */
	filePrintf(dxf,"0\n3DFACE\n8\n1\n"
		"10\n%g\n"
		"20\n%g\n"
		"30\n%g\n"
		"11\n%g\n"
		"21\n%g\n"
		"31\n%g\n"
		"12\n%g\n"
		"22\n%g\n"
		"32\n%g\n"
		"13\n%g\n"
		"23\n%g\n"
		"33\n%g\n",
		p1s[0],p1s[1],p1s[2],p2s[0],p2s[1],p2s[2],p3s[0],p3s[1],p3s[2],p4s[0],p4s[1],p4s[2]);
#ifdef	DXF_GLOBAL_SCALE_FACTOR
#undef	p1s
#undef	p2s
#undef	p3s
#undef	p4s
#endif	/* DXF_GLOBAL_SCALE_FACTOR */
}

void V_GLOBAL
dxfTriangle(vhandle_t dxf,vec3_t p1,vec3_t p2,vec3_t p3)
{
	dxfQuad(dxf,p1,p2,p3,p3);
}

void V_GLOBAL	dxfClose(vhandle_t dxf)
{
	filePrintf(dxf,"0\nENDSEC\n0\nEOF\n");
	fileClose(dxf);
}
