/*
	Ninja Library
	
	COPYRIGHT (C) SEGA ENTERSRISES,LTD.
*/

#ifndef _NINJA_DEF2_H_
#define _NINJA_DEF2_H_


/*--------------------------------------*/
/*      System Context                  */
/*--------------------------------------*/
enum	{
	NJD_SYSTEM_CTX,
	NJD_MODIFIER0_CTX,
	NJD_MODIFIER1_CTX,
	NJD_2PARAM_CTX,
	NJD_DEBUG_CTX,
	NJD_NINDOWS_CTX,
	NJD_CONTEXT_MAX
};

/*--------------------------------------*/
/*      Math                            */
/*--------------------------------------*/
enum {
	IP00, IP01, IP02,
	IP10, IP11, IP12,
	IP20, IP21, IP22,
	IP30, IP31, IP32
};

/************************/
/*	 Texture Defines	*/
/************************/
#define NJD_GLOBAXINDEX_FIRST_VAL 		(0xFFFFFFFF)

#define NJD_PVRHEADERSIZE				(0)
#define NJD_PVR_MM_DUMMYBYTE			(4)

#define NJD_PIXELFORMAT_ARGB1555		(0x00000000)
#define NJD_PIXELFORMAT_RGB565			(0x08000000)
#define NJD_PIXELFORMAT_ARGB4444		(0x10000000)
#define NJD_PIXELFORMAT_YUV422			(0x18000000)
#define NJD_PIXELFORMAT_BUMP			(0x20000000)
#define NJD_PIXELFORMAT_PALETTIZED_4BPP	(0x28000000)
#define NJD_PIXELFORMAT_PALETTIZED_8BPP	(0x30000000)

#define NJD_SURFACEFLAGS_MIPMAPED		BIT_0
#define NJD_SURFACEFLAGS_CHROMAKEYFIX	BIT_1
#define NJD_SURFACEFLAGS_TWIDDLED		BIT_2
#define NJD_SURFACEFLAGS_VQ				BIT_3
#define NJD_SURFACEFLAGS_STRIDE			BIT_4
#define NJD_SURFACEFLAGS_PALETTIZED		BIT_5

#define NJD_GLOBALINDEX_TAG				'XIBG'
#define NJD_PVRTEXTURE_TAG				'TRVP'

#define NJD_TEXMEMFLAG_CACHE			BIT_0
#define NJD_TEXMEMFLAG_TEXMEM			BIT_1
#define NJD_TEXMEMFLAG_FRAME			BIT_2	

enum	{
	NJD_LOAD_PVR	,
	NJD_LOAD_BMP	,
	NJD_LOAD_TGA	,
	NJD_FILE_EXT_NUM
};

#define NJM_TWIDDLEDADDR(i)	((i     & 1)		|\
							((i>>1)	& 1)<< 2	|\
							((i>>2)	& 1)<< 4	|\
							((i>>3)	& 1)<< 6	|\
							((i>>4)	& 1)<< 8	|\
							((i>>5)	& 1)<< 10	|\
							((i>>6)	& 1)<< 12	|\
							((i>>7)	& 1)<< 14)	

#define NJD_TEX_ERR_OTHER		(-1)
#define NJD_TEX_ERR_FILEOPEN	(-2)
#define NJD_TEX_ERR_EXTND		(-3)
#define NJD_TEX_ERR_HEADER		(-4)
#define NJD_TEX_ERR_FILELOAD	(-5)
#define NJD_TEX_ERR_SURFACE		(-6)
#define NJD_TEX_ERR_MAINMEMORY	(-7)
#define NJD_TEX_ERR_TEXMEMLOAD	(-8)
#define NJD_TEX_ERR_GLOBALINDEX	(-9)		


/*--------------------------------------*/
/*      Graphic2D define                */
/*--------------------------------------*/
#define NJD_2D_DRAW_MASK			0x0000000f  /* Mask for Drawing Attribute   */

#define NJD_2D_DRAW_WIRE			0		/* Wire							  */
#define NJD_2D_DRAW_POLYGON			1		/* Polygon						  */
#define NJD_2D_DRAW_ATTR_1_NUM		2		/* the total number of the attr.  */

#define NJD_2D_DRAW_COL				0		/* Non Texture					  */
#define NJD_2D_DRAW_TEX				1		/* Texture						  */
#define NJD_2D_DRAW_ATTR_2_NUM		2		/* the total number of the attr.  */

#define NJD_2D_DRAW_CONCENTRIC		0		/* Concentric Circle		      */
#define NJD_2D_DRAW_INSCRIBE		1		/* Inscribed Circle				  */
#define NJD_2D_DRAW_ATTR_3_NUM		2		/* total 2D Draw funcions		  */

#define NJD_2D_DRAW_LINE_FUNC_NUM		3
#define NJD_2D_DRAW_LINE_FUNC_1			(NJD_2D_DRAW_LINE_FUNC_NUM * 1)
#define NJD_2D_DRAW_TOTAL_LINE_FUNC		6			/* total 2D Draw funcions		  */

#define NJD_2D_DRAW_COL_LINE_N								\
			( NJD_DRAW_NORMAL + NJD_2D_DRAW_LINE_FUNC_1 * NJD_2D_DRAW_COL)
#define NJD_2D_DRAW_COL_LINE_C								\
			( NJD_DRAW_CONNECTED + NJD_2D_DRAW_LINE_FUNC_1 * NJD_2D_DRAW_COL)
#define NJD_2D_DRAW_COL_LINE_F								\
			( NJD_DRAW_FAN + NJD_2D_DRAW_LINE_FUNC_1 * NJD_2D_DRAW_COL)
#define NJD_2D_DRAW_TEX_LINE_N								\
			(NJD_DRAW_NORMAL + NJD_2D_DRAW_LINE_FUNC_1 * NJD_2D_DRAW_TEX)
#define NJD_2D_DRAW_TEX_LINE_C								\
			(NJD_DRAW_CONNECTED + NJD_2D_DRAW_LINE_FUNC_1 * NJD_2D_DRAW_TEX)
#define NJD_2D_DRAW_TEX_LINE_F								\
			(NJD_DRAW_FAN + NJD_2D_DRAW_LINE_FUNC_1 * NJD_2D_DRAW_TEX)

#define NJD_2D_DRAW_POLY_FUNC_NUM		2
#define NJD_2D_DRAW_POLY_FUNC_1			(NJD_2D_DRAW_POLY_FUNC_NUM * 1)			
#define NJD_2D_DRAW_TOTAL_POLY_FUNC		4

#define NJD_2D_DRAW_COL_POLYGON									\
			( NJD_2D_DRAW_COL									\
			+ NJD_2D_DRAW_POLY_FUNC_1 * NJD_2D_DRAW_POLYGON)
#define NJD_2D_DRAW_TEX_POLYGON									\
			( NJD_2D_DRAW_TEX									\
			+ NJD_2D_DRAW_POLY_FUNC_1 * NJD_2D_DRAW_POLYGON)
#define NJD_2D_DRAW_COL_POLY_LINE								\
			( NJD_2D_DRAW_COL									\
			+ NJD_2D_DRAW_POLY_FUNC_1 * NJD_2D_DRAW_WIRE)
#define NJD_2D_DRAW_TEX_POLY_LINE								\
			( NJD_2D_DRAW_TEX									\
			+ NJD_2D_DRAW_POLY_FUNC_1 * NJD_2D_DRAW_WIRE)

#define NJD_2D_DRAW_TRI_FUNC_NUM		3
#define NJD_2D_DRAW_TRI_FUNC_1			(NJD_2D_DRAW_TRI_FUNC_NUM * 1)			
#define NJD_2D_DRAW_TRI_FUNC_2			(NJD_2D_DRAW_TRI_FUNC_NUM * 2)	
#define NJD_2D_DRAW_TOTAL_TRI_FUNC		12			/* total 2D Draw funcions		  */

#define NJD_2D_DRAW_COL_TRIANGLE_N							\
			( NJD_DRAW_NORMAL								\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_POLYGON	\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_COL)
#define NJD_2D_DRAW_COL_TRIANGLE_C							\
			( NJD_DRAW_CONNECTED							\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_POLYGON	\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_COL)
#define NJD_2D_DRAW_COL_TRIANGLE_F							\
			( NJD_DRAW_FAN									\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_POLYGON	\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_COL)
#define NJD_2D_DRAW_TEX_TRIANGLE_N							\
			( NJD_DRAW_NORMAL								\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_POLYGON	\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_TEX)
#define NJD_2D_DRAW_TEX_TRIANGLE_C							\
			( NJD_DRAW_CONNECTED							\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_POLYGON	\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_TEX)
#define NJD_2D_DRAW_TEX_TRIANGLE_F							\
			( NJD_DRAW_FAN									\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_POLYGON	\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_TEX)
#define NJD_2D_DRAW_COL_TRI_LINE_N							\
			( NJD_DRAW_NORMAL								\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_WIRE		\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_COL)
#define NJD_2D_DRAW_COL_TRI_LINE_C							\
			( NJD_DRAW_CONNECTED							\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_WIRE		\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_COL)
#define NJD_2D_DRAW_COL_TRI_LINE_F							\
			( NJD_DRAW_FAN									\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_WIRE		\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_COL)
#define NJD_2D_DRAW_TEX_TRI_LINE_N							\
			( NJD_DRAW_NORMAL								\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_WIRE		\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_TEX)
#define NJD_2D_DRAW_TEX_TRI_LINE_C							\
			( NJD_DRAW_CONNECTED							\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_WIRE		\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_TEX)
#define NJD_2D_DRAW_TEX_TRI_LINE_F							\
			( NJD_DRAW_FAN									\
			+ NJD_2D_DRAW_TRI_FUNC_1 * NJD_2D_DRAW_WIRE		\
			+ NJD_2D_DRAW_TRI_FUNC_2 * NJD_2D_DRAW_TEX)

#define NJD_2D_DRAW_CIRCLE_FUNC_NUM		3
#define NJD_2D_DRAW_CIRCLE_FUNC_1		(NJD_2D_DRAW_CIRCLE_FUNC_NUM * 1)			
#define NJD_2D_DRAW_TOTAL_CIRCLE_FUNC	6

#define NJD_2D_DRAW_COL_CIRCLE_N							\
			( NJD_DRAW_NORMAL								\
			+ NJD_2D_DRAW_CIRCLE_FUNC_1 * NJD_2D_DRAW_COL )
#define NJD_2D_DRAW_COL_CIRCLE_C							\
			( NJD_DRAW_CONNECTED							\
			+ NJD_2D_DRAW_CIRCLE_FUNC_1 * NJD_2D_DRAW_COL )
#define NJD_2D_DRAW_COL_CIRCLE_F							\
			( NJD_DRAW_FAN									\
			+ NJD_2D_DRAW_CIRCLE_FUNC_1 * NJD_2D_DRAW_COL )
#define NJD_2D_DRAW_TEX_CIRCLE_N							\
			( NJD_DRAW_NORMAL								\
			+ NJD_2D_DRAW_CIRCLE_FUNC_1 * NJD_2D_DRAW_TEX )
#define NJD_2D_DRAW_TEX_CIRCLE_C							\
			( NJD_DRAW_CONNECTED							\
			+ NJD_2D_DRAW_CIRCLE_FUNC_1 * NJD_2D_DRAW_TEX )
#define NJD_2D_DRAW_TEX_CIRCLE_F							\
			( NJD_DRAW_FAN									\
			+ NJD_2D_DRAW_CIRCLE_FUNC_1 * NJD_2D_DRAW_TEX )

/*--------------------------------------*/
/*      Graphic3D define                */
/*--------------------------------------*/
#define NJD_3D_LINE_WIDTH 0.525f

/*--------------------------------------*/
/*      Collision define                */
/*--------------------------------------*/
#define NJD_COL_DELTA (0.00005f)
#define njCheckEdge0(V0,V1) \
                    /* 側面の各辺とのチェック */\
                    /* cp1 から辺に落とした点 */\
                    vert_v.x = box->v[sides[i][V0]].x - box->v[sides[i][V1]].x;\
                    vert_v.y = box->v[sides[i][V0]].y - box->v[sides[i][V1]].y;\
                    vert_v.z = box->v[sides[i][V0]].z - box->v[sides[i][V1]].z;\
                    t = ((vert_v.x)*((cp1.x)-(box->v[sides[i][V0]].x))\
                        +(vert_v.y)*((cp1.y)-(box->v[sides[i][V0]].y))\
                        +(vert_v.z)*((cp1.z)-(box->v[sides[i][V0]].z)))/(vert_v.x*vert_v.x+vert_v.y*vert_v.y+vert_v.z*vert_v.z);\
		            cp3.x = (vert_v.x)*t+(box->v[sides[i][V0]].x);\
		            cp3.y = (vert_v.y)*t+(box->v[sides[i][V0]].y);\
		            cp3.z = (vert_v.z)*t+(box->v[sides[i][V0]].z);\
                    /* cp2 から辺に落とした点 */\
                    t = ((vert_v.x)*((cp2.x)-(box->v[sides[i][V0]].x))\
                        +(vert_v.y)*((cp2.y)-(box->v[sides[i][V0]].y))\
                        +(vert_v.z)*((cp2.z)-(box->v[sides[i][V0]].z)))/(vert_v.x*vert_v.x+vert_v.y*vert_v.y+vert_v.z*vert_v.z);\
		            cp4.x = (vert_v.x)*t+(box->v[sides[i][V0]].x);\
		            cp4.y = (vert_v.y)*t+(box->v[sides[i][V0]].y);\
		            cp4.z = (vert_v.z)*t+(box->v[sides[i][V0]].z);\
                    /* cp1,cp2が辺をまたいでいるかをチェック */\
                    cp1cp3_v.x = cp1.x - cp3.x;\
                    cp1cp3_v.y = cp1.y - cp3.y;\
                    cp1cp3_v.z = cp1.z - cp3.z;\
                    cp2cp4_v.x = cp2.x - cp4.x;\
                    cp2cp4_v.y = cp2.y - cp4.y;\
                    cp2cp4_v.z = cp2.z - cp4.z;\
                    dot = (cp1cp3_v.x)*(cp2cp4_v.x) + (cp1cp3_v.y)*(cp2cp4_v.y) + (cp1cp3_v.z)*(cp2cp4_v.z);

#define njCheckEdge1(V0,V1) \
                    if(dot < 0){/* 辺をまたぐ */\
                        Float p1, p2;\
                        NJS_POINT3 cross;/* 直線cp1-cp2と辺の交点*/\
                        cross.x = (vert_v.x*cp1.x - cp1cp3_v.x*box->v[sides[i][V0]].x)/(vert_v.x - cp1cp3_v.x);\
                        cross.y = (vert_v.x*cp1.y - cp1cp3_v.y*box->v[sides[i][V0]].y)/(vert_v.y - cp1cp3_v.y);\
                        cross.z = (vert_v.x*cp1.x - cp1cp3_v.z*box->v[sides[i][V0]].z)/(vert_v.z - cp1cp3_v.z);\
                        /* 交点crossが辺上に在るかをチェック */\
                        if(box->v[sides[i][V0]].x > box->v[sides[i][V1]].x){\
                            p1 = box->v[sides[i][V0]].x;\
                            p2 = box->v[sides[i][V1]].x;\
                        }\
                        else{\
                            p1 = box->v[sides[i][V1]].x;\
                            p2 = box->v[sides[i][V0]].x;\
                        }\
                        if((p1 >= cross.x)&&(p2 <= cross.x)){\
                            if(box->v[sides[i][V0]].y > box->v[sides[i][V1]].y){\
                                p1 = box->v[sides[i][V0]].y;\
                                p2 = box->v[sides[i][V1]].y;\
                            }\
                            else{\
                                p1 = box->v[sides[i][V1]].y;\
                                p2 = box->v[sides[i][V0]].y;\
                            }\
                            if((p1 >= cross.y)&&(p2 <= cross.y)){\
                                if(box->v[sides[i][V0]].z > box->v[sides[i][V1]].z){\
                                    p1 = box->v[sides[i][V0]].z;\
                                    p2 = box->v[sides[i][V1]].z;\
                                }\
                                else{\
                                    p1 = box->v[sides[i][V1]].z;\
                                    p2 = box->v[sides[i][V0]].z;\
                                }\
                                if((p1 >= cross.z)&&(p2 <= cross.z))\
                                    return 1;\
                            }\
                        }\
                    }
#define njCheckEdge2(V0,V1) \
                    else{/* 辺をまたがない */\
                        Float r_pow;/* 球と側面とが交わって出来る円の半径（２乗）*/\
                        Float cp1cp3_dist_pow, cp2cp4_dist_pow;/* cp1,cp2それぞれから辺までの距離（２乗）*/\
                        Float min_dist_pow;\
                        r_pow = r*r; /* (capsule->r*capsule->r) - dist*dist; */\
                        cp1cp3_dist_pow = (cp1.x - cp3.x)*(cp1.x - cp3.x)+(cp1.y - cp3.y)*(cp1.y - cp3.y)+(cp1.z - cp3.z)*(cp1.z - cp3.z);\
                        cp2cp4_dist_pow = (cp2.x - cp4.x)*(cp2.x - cp4.x)+(cp2.y - cp4.y)*(cp2.y - cp4.y)+(cp2.z - cp4.z)*(cp2.z - cp4.z);\
                        min_dist_pow = (cp1cp3_dist_pow < cp2cp4_dist_pow) ? cp1cp3_dist_pow : cp2cp4_dist_pow;\
                        if(min_dist_pow < r_pow){\
                            if(cp1cp3_dist_pow < cp2cp4_dist_pow){\
                                Float p1, p2;\
                                /* cp3が辺上に在るかをチェック */\
                                if(box->v[sides[i][V0]].x > box->v[sides[i][V1]].x){\
                                    p1 = box->v[sides[i][V0]].x;\
                                    p2 = box->v[sides[i][V1]].x;\
                                }\
                                else{\
                                    p1 = box->v[sides[i][V1]].x;\
                                    p2 = box->v[sides[i][V0]].x;\
                                }\
                                if((p1 >= cp3.x)&&(p2 <= cp3.x)){\
                                    if(box->v[sides[i][V0]].y > box->v[sides[i][V1]].y){\
                                        p1 = box->v[sides[i][V0]].y;\
                                        p2 = box->v[sides[i][V1]].y;\
                                    }\
                                    else{\
                                        p1 = box->v[sides[i][V1]].y;\
                                        p2 = box->v[sides[i][V0]].y;\
                                    }\
                                    if((p1 >= cp3.y)&&(p2 <= cp3.y)){\
                                        if(box->v[sides[i][V0]].z > box->v[sides[i][V1]].z){\
                                            p1 = box->v[sides[i][V0]].z;\
                                            p2 = box->v[sides[i][V1]].z;\
                                        }\
                                        else{\
                                            p1 = box->v[sides[i][V1]].z;\
                                            p2 = box->v[sides[i][V0]].z;\
                                        }\
                                        if((p1 >= cp3.z)&&(p2 <= cp3.z))\
                                            return 1;\
                                    }\
                                }\
                            }
#define njCheckEdge3(V0,V1) \
                            if(cp2cp4_dist_pow < cp1cp3_dist_pow){\
                                Float p1, p2;\
                                /* cp4が辺上に在るかをチェック */\
                                if(box->v[sides[i][V0]].x > box->v[sides[i][V1]].x){\
                                    p1 = box->v[sides[i][V0]].x;\
                                    p2 = box->v[sides[i][V1]].x;\
                                }\
                                else{\
                                    p1 = box->v[sides[i][V1]].x;\
                                    p2 = box->v[sides[i][V0]].x;\
                                }\
                                if((p1 >= cp4.x)&&(p2 <= cp4.x)){\
                                    if(box->v[sides[i][V0]].y > box->v[sides[i][V1]].y){\
                                        p1 = box->v[sides[i][V0]].y;\
                                        p2 = box->v[sides[i][V1]].y;\
                                    }\
                                    else{\
                                        p1 = box->v[sides[i][V1]].y;\
                                        p2 = box->v[sides[i][V0]].y;\
                                    }\
                                    if((p1 >= cp4.y)&&(p2 <= cp4.y)){\
                                        if(box->v[sides[i][V0]].z > box->v[sides[i][V1]].z){\
                                            p1 = box->v[sides[i][V0]].z;\
                                            p2 = box->v[sides[i][V1]].z;\
                                        }\
                                        else{\
                                            p1 = box->v[sides[i][V1]].z;\
                                            p2 = box->v[sides[i][V0]].z;\
                                        }\
                                        if((p1 >= cp4.z)&&(p2 <= cp4.z))\
                                            return 1;\
                                    }\
                                }\
                            }
#define njCheckEdge4(V0,V1) \
                            if(cp2cp4_dist_pow == cp1cp3_dist_pow){\
                                Float p1, p2;\
                                /* cp3が辺上に在るかをチェック */\
                                if(box->v[sides[i][V0]].x > box->v[sides[i][V1]].x){\
                                    p1 = box->v[sides[i][V0]].x;\
                                    p2 = box->v[sides[i][V1]].x;\
                                }\
                                else{\
                                    p1 = box->v[sides[i][V1]].x;\
                                    p2 = box->v[sides[i][V0]].x;\
                                }\
                                if((p1 >= cp3.x)&&(p2 <= cp3.x)){\
                                    if(box->v[sides[i][V0]].y > box->v[sides[i][V1]].y){\
                                        p1 = box->v[sides[i][V0]].y;\
                                        p2 = box->v[sides[i][V1]].y;\
                                    }\
                                    else{\
                                        p1 = box->v[sides[i][V1]].y;\
                                        p2 = box->v[sides[i][V0]].y;\
                                    }\
                                    if((p1 >= cp3.y)&&(p2 <= cp3.y)){\
                                        if(box->v[sides[i][V0]].z > box->v[sides[i][V1]].z){\
                                            p1 = box->v[sides[i][V0]].z;\
                                            p2 = box->v[sides[i][V1]].z;\
                                        }\
                                        else{\
                                            p1 = box->v[sides[i][V1]].z;\
                                            p2 = box->v[sides[i][V0]].z;\
                                        }\
                                        if((p1 >= cp3.z)&&(p2 <= cp3.z))\
                                            return 1;\
                                    }\
                                }
#define njCheckEdge5(V0,V1) \
                                /* cp4が辺上に在るかをチェック */\
                                if(box->v[sides[i][V0]].x > box->v[sides[i][V1]].x){\
                                    p1 = box->v[sides[i][V0]].x;\
                                    p2 = box->v[sides[i][V1]].x;\
                                }\
                                else{\
                                    p1 = box->v[sides[i][V1]].x;\
                                    p2 = box->v[sides[i][V0]].x;\
                                }\
                                if((p1 >= cp4.x)&&(p2 <= cp4.x)){\
                                    if(box->v[sides[i][V0]].y > box->v[sides[i][V1]].y){\
                                        p1 = box->v[sides[i][V0]].y;\
                                        p2 = box->v[sides[i][V1]].y;\
                                    }\
                                    else{\
                                        p1 = box->v[sides[i][V1]].y;\
                                        p2 = box->v[sides[i][V0]].y;\
                                    }\
                                    if((p1 >= cp4.y)&&(p2 <= cp4.y)){\
                                        if(box->v[sides[i][V0]].z > box->v[sides[i][V1]].z){\
                                            p1 = box->v[sides[i][V0]].z;\
                                            p2 = box->v[sides[i][V1]].z;\
                                        }\
                                        else{\
                                            p1 = box->v[sides[i][V1]].z;\
                                            p2 = box->v[sides[i][V0]].z;\
                                        }\
                                        if((p1 >= cp4.z)&&(p2 <= cp4.z))\
                                            return 1;\
                                    }\
                                }\
                                /* 辺が直線cp3-cp4に含まれるかをチェック */\
                                if(cp3.x > cp4.x){\
                                    p1 = cp3.x;\
                                    p2 = cp4.x;\
                                }\
                                else{\
                                    p1 = cp4.x;\
                                    p2 = cp3.x;\
                                }\
                                if((p1 >= box->v[sides[i][V0]].x)&&(p2 <= box->v[sides[i][V0]].x)){\
                                    if(cp3.y > cp4.y){\
                                        p1 = cp3.y;\
                                        p2 = cp4.y;\
                                    }\
                                    else{\
                                        p1 = cp4.y;\
                                        p2 = cp3.y;\
                                    }\
                                    if((p1 >= box->v[sides[i][V0]].y)&&(p2 <= box->v[sides[i][V0]].y)){\
                                        if(cp3.z > cp4.z){\
                                            p1 = cp3.z;\
                                            p2 = cp4.z;\
                                        }\
                                        else{\
                                            p1 = cp4.z;\
                                            p2 = cp3.z;\
                                        }\
                                        if((p1 >= box->v[sides[i][V0]].z)&&(p2 <= box->v[sides[i][V0]].z))\
                                            return 1;\
                                    }\
                                }\
                            }\
                        }\
                    }\


#define njCheckEdge(V0,V1) \
		njCheckEdge0(V0,V1) \
		njCheckEdge1(V0,V1) \
		njCheckEdge2(V0,V1) \
		njCheckEdge3(V0,V1) \
		njCheckEdge4(V0,V1) \
		njCheckEdge5(V0,V1) 

#endif /* _NINJA_DEF_H_ */
