#include "3D_Object.h"
#include "3D_Plane.h"
#include "3D_Line.h"
#include "Space_Cal.h"
using namespace std;
#ifdef _DEBUG
#pragma comment ( lib, "cxcore200d.lib" )
#pragma comment ( lib, "cv200d.lib" )
#pragma comment ( lib, "highgui200d.lib" )
#else
#pragma comment ( lib, "cxcore200.lib" )
#pragma comment ( lib, "cv200.lib" )
#pragma comment ( lib, "highgui200.lib" )
#endif
#include "cv.h"
#include "highgui.h"
#define X0 (g_ViewPoint.x)
#define Y0 (g_ViewPoint.y)
#define Z0 (g_ViewPoint.z)
#define eye_focal_distance 1
#define PI 3.14159265358979f
#define FRAME_HEIGTH 400
#define FRAME_WIDTH 400
#define PIXEL_RATE 200
#define WINDOW_NAME "3D Model"
//1 正方体 2 长方体 3 椎体 4 面
#define OBJECT_CHOOSE 1
IplImage* frame = NULL;
_3DPoint g_ViewPoint(4,2,0);//视点
_3DObject g_Object;//观测的物体
const float weitiao = 0.2f;
int mouse_begin_x;
int mouse_begin_y;
int mouse_counter;
void InitObject();//初始化观测物体的参数
void on_mouse( int event, int x, int y, int flags, void* param );//鼠标响应函数
void Draw_3D_Object();//画3D图形
void DrawDotLine(CvPoint point1,CvPoint point2);//画虚线
void SetInvisiablePoint();//设置不可见的点
//获取视网膜平面
_3DPlane GetRetinaPlane()
{
return _3DPlane(X0,Y0,Z0,-X0*X0-Y0*Y0-Z0*Z0-eye_focal_distance*sqrt(X0*X0+Y0*Y0+Z0*Z0));
}
//获取视网膜平面原点空间坐标
_3DPoint GetOriPoint_In_RetinaPlane()
{
float Dis = GetDistance_From_TowPoint(_3DPoint(0,0,0),g_ViewPoint);
float k = (Dis+eye_focal_distance)/Dis;
return _3DPoint(X0*k,Y0*k,Z0*k);
}
//视网膜平面定义的X轴
_3DVector GetRetinaXAxis()
{
_3DVector X_Axis = Cross(g_ViewPoint,_3DVector(0,0,1));
//归一化
float m = GetDistance_From_TowPoint(_3DPoint(0,0,0),X_Axis);
X_Axis.x /= m;
X_Axis.y /= m;
X_Axis.z /= m;
return X_Axis;
}
//返回值前两个坐标有效,表示二维平面下的坐标
_3DPoint Get2DPoint(_3DPlane RetinaPlane,_3DPoint _OriPoint,_3DPoint
RetinaOriPoint,_3DVector XAxis,_3DVector YAxis)
{
_3DLine Line = GetLine_From_TwoPoint(g_ViewPoint,_OriPoint);
_3DPoint Point_In_Retina = GetPoint_From_Line_And_Plane(Line,RetinaPlane);
_3DVector temp = _3DVector(Point_In_Retina.x-RetinaOriPoint.x,
Point_In_Retina.y-RetinaOriPoint.y,
Point_In_Retina.z-RetinaOriPoint.z);
return _3DPoint(Dot(temp,XAxis),Dot(temp,YAxis),0);
}
int main(int argc,char* argv[])
{
InitObject();
frame = cvCreateImage(cvSize(FRAME_WIDTH,FRAME_HEIGTH),IPL_DEPTH_8U,3);
cvNamedWindow(WINDOW_NAME);
cvSetMouseCallback(WINDOW_NAME, on_mouse, 0 );
Draw_3D_Object();
while(1)
{
cvShowImage(WINDOW_NAME,frame);
cvWaitKey(2);
}
return 0;
}