#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <math.h>
#include <d3dx9math.h>

/*
#include <iostream>
#include <glut.h>
#include <ctype.h>
#include <math.h>

*/


double InnerProduct(double *, double *, int);

main ()
{


OBB obbA;   // OBB A （すでに回転などをしているとします）
OBB obbB;   // OBB B (こちらも)

// OBBの方向ベクトルeb2を分離軸と設定（すでに標準化されているとします）
D3DXVECTOR3 L = ObbB.GetDirect(1);

// OBB Aの軸を取得（軸の長さに定数倍します）
D3DXVECTOR3 ea1 = ObbA.GetDirect(0) * ObbA.GetLen(0);
D3DXVECTOR3 ea2 = ObbA.GetDirect(1) * ObbA.GetLen(1);

// rAおよびrBを算出
FLOAT ra = fabs(D3DXVec3Dot(&L, &ea1))  + fabs(D3DXVec3Dot( &L, &ea2 ));
FLOAT rb = ObbB.GetLen(0);   // これはeb2です

// 中心点間の距離を算出
FLOAT Interval = fabs(D3DXVec3Dot( &(obbA.GetPos()-ObbB.GetPos()), &L));

// 衝突判定
if( Interval < ra + rb )
   return true;   // 衝突

return fasle;   // 衝突していない



//float a = [1 2 3];
//float b = [4 5 6];
//float c = dot([1 2 3],[4 5 6])

double inner_product;
double vec1[] = {1.0, 2.0, 3.0};
double vec2[] = {2.0, 1.0, 3.0};
//double c = dot(vec1,vec2);

    inner_product = InnerProduct(vec1, vec2, 3);
    printf("内積: %.2f\n", inner_product);




/*
function CollisionJudge(P1[],P2[]
{
	
衝突判定
	if(Judge == true)
	{
		//衝突した
		return ture;
	}
	else
	{
		//衝突してない
		return false;
	}
}
*/

/**
 * ベクトルの内積を計算する
 * @param[in] vec1 ベクトル1
 * @param[in] vec2 ベクトル2
 * @param[in] n ベクトルの次元数
 * @return vec1 と vec2 の内積
 */

}




double InnerProduct(double *vec1, double *vec2, int n) {
    int i;
    double s = 0.0;

    for ( i = 0; i < n; i++ ) {
        s += vec1[i] * vec2[i];
    }

    return s;
}


/*
int TestOBBOBB(OBB *a, OBB *b)
{
    const float EPSILON = 1.175494e-37;

    float R[3][3], AbsR[3][3];
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            R[i][j] = D3DXVec3Dot(&a->u[i], &b->u[j]);
            AbsR[i][j] = fabsf(R[i][j]) + EPSILON;
        }
    }
        
    D3DXVECTOR3 t = b->c - a->c;
    t = D3DXVECTOR3(D3DXVec3Dot(&t, &a->u[0]), D3DXVec3Dot(&t, &a->u[1]), D3DXVec3Dot(&t, &a->u[2]));
        
    //軸L=A0, L=A1, L=A2判定
    float ra, rb;

    for(int i = 0; i < 3; i++)
    {
        ra = a->e[i];
        rb = b->e[0] * AbsR[i][0] + b->e[1] * AbsR[i][1] + b->e[2] * AbsR[i][2];
        if(fabsf(t[i]) > ra + rb)return 0;
    }
    //軸L=B0, L=B1, L=B2判定
    for(int i = 0; i < 3; i++)
    {
        ra = a->e[0] * AbsR[0][i] + a->e[1] * AbsR[1][i] + a->e[2] * AbsR[2][i];
        rb = b->e[i];
        if(fabsf(t[0] * R[0][i] + t[1] * R[1][i] + t[2] * R[2][i]) > ra + rb)return 0;
    }

    //軸L=A0 X B0判定
    ra = a->e[1] * AbsR[2][0] + a->e[2] * AbsR[1][0];
    rb = b->e[1] * AbsR[0][2] + b->e[2] * AbsR[0][1];
    if(fabsf(t[2] * R[1][0] - t[1] * R[2][0]) > ra + rb)return 0;

    //軸L=A0 X B1判定
    ra = a->e[1] * AbsR[2][1] + a->e[2] * AbsR[1][1];
    rb = b->e[0] * AbsR[0][2] + b->e[2] * AbsR[0][0];
    if(fabsf(t[2] * R[1][1] - t[1] * R[2][1]) > ra + rb)return 0;

    //軸L=A0 X B2判定
    ra = a->e[1] * AbsR[2][2] + a->e[2] * AbsR[1][2];
    rb = b->e[0] * AbsR[0][1] + b->e[1] * AbsR[0][0];
    if(fabsf(t[2] * R[1][2] - t[1] * R[2][2]) > ra + rb)return 0;

    //軸L=A1 X B0判定
    ra = a->e[0] * AbsR[2][0] + a->e[2] * AbsR[0][0];
    rb = b->e[1] * AbsR[1][2] + b->e[2] * AbsR[1][1];
    if(fabsf(t[0] * R[2][0] - t[2] * R[0][0]) > ra + rb)return 0;

    //軸L=A1 X B1判定
    ra = a->e[0] * AbsR[2][1] + a->e[2] * AbsR[0][1];
    rb = b->e[0] * AbsR[1][2] + b->e[2] * AbsR[1][0];
    if(fabsf(t[0] * R[2][1] - t[2] * R[0][1]) > ra + rb)return 0;

    //軸L=A1 X B2判定
    ra = a->e[0] * AbsR[2][2] + a->e[2] * AbsR[0][2];
    rb = b->e[0] * AbsR[1][1] + b->e[1] * AbsR[1][0];
    if(fabsf(t[0] * R[2][2] - t[2] * R[0][2]) > ra + rb)return 0;

    //軸L=A2 X B0判定
    ra = a->e[0] * AbsR[1][0] + a->e[1] * AbsR[0][0];
    rb = b->e[1] * AbsR[2][2] + b->e[2] * AbsR[2][1];
    if(fabsf(t[1] * R[0][0] - t[0] * R[1][0]) > ra + rb)return 0;

    //軸L=A2 X B1判定
    ra = a->e[0] * AbsR[1][1] + a->e[1] * AbsR[0][1];
    rb = b->e[0] * AbsR[2][2] + b->e[2] * AbsR[2][0];
    if(fabsf(t[1] * R[0][1] - t[0] * R[1][1]) > ra + rb)return 0;

    //軸L=A2 X B2判定
    ra = a->e[0] * AbsR[1][2] + a->e[1] * AbsR[0][2];
    rb = b->e[0] * AbsR[2][1] + b->e[1] * AbsR[2][0];
    if(fabsf(t[1] * R[0][2] - t[0] * R[1][2]) > ra + rb)return 0;

    return 1;
}
*/


/*
typedef struct D3DXVECTOR3 : public D3DVECTOR {
public:
    D3DXVECTOR3() {};
    D3DXVECTOR3( CONST FLOAT * );
    D3DXVECTOR3( CONST D3DVECTOR& );
    D3DXVECTOR3( FLOAT x, FLOAT y, FLOAT z );

    // casting
    operator FLOAT* ();
    operator CONST FLOAT* () const;

    // assignment operators
    D3DXVECTOR3& operator += ( CONST D3DXVECTOR3& );
    D3DXVECTOR3& operator -= ( CONST D3DXVECTOR3& );
    D3DXVECTOR3& operator *= ( FLOAT );
    D3DXVECTOR3& operator /= ( FLOAT );

    // unary operators
    D3DXVECTOR3 operator + () const;
    D3DXVECTOR3 operator - () const;

    // binary operators
    D3DXVECTOR3 operator + ( CONST D3DXVECTOR3& ) const;
    D3DXVECTOR3 operator - ( CONST D3DXVECTOR3& ) const;
    D3DXVECTOR3 operator * ( FLOAT ) const;
    D3DXVECTOR3 operator / ( FLOAT ) const;

    friend D3DXVECTOR3 operator * ( FLOAT, CONST struct D3DXVECTOR3& );

    BOOL operator == ( CONST D3DXVECTOR3& ) const;
    BOOL operator != ( CONST D3DXVECTOR3& ) const;

} D3DXVECTOR3, *LPD3DXVECTOR3;
*/
