void main()
{
VECTOR3D plane_n; // plane normal
POINT3D  plane_p; // point on plane
PLANE3D plane;    // the plane

printf("\n3D Plane-Parametric Line Intersector\n");

// get the normal to the plane
printf("\nEnter normal to plane: nx, ny, nz?");
scanf("%f, %f, %f", &plane_n.x, &plane_n.y, &plane_n.z);

// get a point on the plane
printf("\nEnter a point on the plane: x,y,z?");
scanf("%f, %f, %f", &plane_p.x, &plane_p.y, &plane_p.z);

// create the plane from the point and normal
PLANE3D_Init(&plane, &plane_p, &plane_n, TRUE);

POINT3D p1, p2;  // our endpoints
PARMLINE3D pl1; // our line

while(1) {

printf("\nEnter coords parametric line from p1 to p2 in form: x1,y1,z1, x2,y2,z2?");
scanf("%f, %f, %f, %f, %f, %f",&p1.x, &p1.y, &p1.z, &p2.x, &p2.y, &p2.z);

// create a parametric line from p1 to p2
Init_Parm_Line3D(&p1, &p2, &pl1);

float t;    // intersection parameter
POINT3D pt; // intersection point

// compute intersection
int intersection_type = Intersect_Parm_Line3D_Plane3D(&pl1, &plane, &t, &pt);

// based on type of intersection display results
switch(intersection_type)
      {
      case PARM_LINE_NO_INTERSECT:
           {
           printf("\nThe plane and line does not intersect!");
           } break;

      case PARM_LINE_INTERSECT_IN_SEGMENT:
           {
           POINT3D pt;
           Compute_Parm_Line3D(&pl1, t, &pt);
           printf("\nThe line and plane intersects when t=%f at (%f, %f, %f)",t,pt.x, pt.y, pt.z);
           } break;

      case PARM_LINE_INTERSECT_OUT_SEGMENT:
           {
           POINT3D pt;
           Compute_Parm_Line3D(&pl1, t, &pt);
           printf("\nThe line and plane intersects when t=%f at (%f, %f, %f)",t,pt.x, pt.y, pt.z);
           printf("\nNote that the intersection occurs out of range of the line segment");
           } break;

      default: break;
 
      } // end switch





} // end while


} // end main
Example #2
0
void Init_CAM4D(CAM4D_PTR cam,
	int cam_attr,
	Vector4d *cam_pos,
	Vector4d *cam_dir,
	Vector4d *cam_target,
	float near_clip_z,
	float far_clip_z,
	float fov,
	float viewport_width,
	float viewport_height)
{
	cam->attr = cam_attr;

	cam->pos = (*cam_pos);
	cam->dir = (*cam_dir);

	cam->u = Vector4d(1, 0, 0, 1);
	cam->v = Vector4d(0, 1, 0, 1);
	cam->n = Vector4d(0, 0, 1, 1);

	if (cam_target != NULL) {
		cam->target = (*cam_target);
	} else {
		cam->target = Vector4d(0, 0, 0, 1);
	}

	cam->near_clip_z = near_clip_z;
	cam->far_clip_z = far_clip_z;

	cam->viewport_width = viewport_width;
	cam->viewport_height = viewport_height;

	cam->viewport_center_x = (viewport_width - 1) / 2;
	cam->viewport_center_y = (viewport_height - 1) / 2;

	cam->aspect_ratio = (float)viewport_width / (float)viewport_height;

	// Init matrixs to be identity matrix
	cam->mcam = Matrix4d::Identity(4, 4);
	cam->mper = Matrix4d::Identity(4, 4);
	cam->mscr = Matrix4d::Identity(4, 4);

	cam->fov = fov;

	cam->viewplane_width = 2.0;
	cam->viewplane_height = 2.0f / cam->aspect_ratio;

	float tan_fov_div2 = tan(DEG_TO_RAD(fov / 2));
	cam->view_dist = 0.5 * (cam->viewplane_width) * tan_fov_div2;

	if (fov == 90.0) {
		Vector3d pt_origin = Vector3d(0, 0, 0);
		Vector3d vn;

		// Init clip plane
		vn = Vector3d(1, 0, -1);
		PLANE3D_Init(&cam->rt_clip_plane, &pt_origin, &vn, 1);
		vn = Vector3d(-1, 0, -1);
		PLANE3D_Init(&cam->lt_clip_plane, &pt_origin, &vn, 1);
		vn = Vector3d(0, 1, -1);
		PLANE3D_Init(&cam->tp_clip_plane, &pt_origin, &vn, 1);
		vn = Vector3d(0, -1, -1);
		PLANE3D_Init(&cam->bt_clip_plane, &pt_origin, &vn, 1);
	} else {
		Vector3d pt_origin = Vector3d(0, 0, 0);
		Vector3d vn;

		// Init clip plane
		vn = Vector3d(cam->view_dist, 0, -cam->viewplane_width / 2.0);
		PLANE3D_Init(&cam->rt_clip_plane, &pt_origin, &vn, 1);
		vn = Vector3d(-cam->view_dist, 0, -cam->viewplane_width / 2.0);
		PLANE3D_Init(&cam->lt_clip_plane, &pt_origin, &vn, 1);
		vn = Vector3d(0, cam->view_dist, -cam->viewplane_width / 2.0);
		PLANE3D_Init(&cam->tp_clip_plane, &pt_origin, &vn, 1);
		vn = Vector3d(0, -cam->view_dist, -cam->viewplane_width / 2.0);
		PLANE3D_Init(&cam->bt_clip_plane, &pt_origin, &vn, 1);
	}
}
Example #3
0
void Init_CAM4DV1(CAM4DV1_PTR cam, int cam_attr, POINT4D_PTR cam_pos, VECTOR4D_PTR cam_dir
	, POINT4D_PTR cam_target, float near_clip_z, float far_clip_z, float fov, float viewport_width, float viewport_height)
{
	cam->attr = cam_attr;
	VECTOR4D_COPY(&cam->pos, cam_pos);
	VECTOR4D_COPY(&cam->dir, cam_dir);

	VECTOR4D_INITXYZ(&cam->u, 1.f, .0f, .0f);
	VECTOR4D_INITXYZ(&cam->v, 0.f, 1.f, .0f);
	VECTOR4D_INITXYZ(&cam->n, .0f, .0f, 1.f);

	if (cam_pos != NULL)
		VECTOR4D_COPY(&cam->target, cam_target);
	else
		VECTOR4D_ZERO(&cam->target);

	cam->near_clip_z = near_clip_z;
	cam->far_clip_z = far_clip_z;

	cam->viewport_width = viewport_width;
	cam->viewport_height = viewport_height;

	cam->viewport_center_x = (viewport_width - 1) / 2;
	cam->viewport_center_y = (viewport_height - 1) / 2;

	cam->aspect_ratio = (float)viewport_width / (float)viewport_height;

	MAT_IDENTITY_4X4(&cam->mcam);
	MAT_IDENTITY_4X4(&cam->mper);
	MAT_IDENTITY_4X4(&cam->mscr);

	cam->fov = fov;

	cam->viewplane_width = 2;
	cam->viewplane_height = 2 / cam->aspect_ratio;

	float tan_fov_div2 = tan(DEG_TO_RAD(fov / 2));
	//cam->view_dist = 0.5f * cam->viewplane_width * tan_fov_div2;
	cam->view_dist = 0.5f * cam->viewplane_width / tan_fov_div2;
	cam->viewport_dist = 0.5f * cam->viewport_width / tan_fov_div2;

	if (fov == 90.0)
	{
		POINT3D pt_origin;
		VECTOR3D_INITXYZ(&pt_origin, 0, 0, 0);
		VECTOR3D vn;
		//ÓҲüôÃæ
		VECTOR3D_INITXYZ(&vn, 1, 0, 11);
		PLANE3D_Init(&cam->rt_clip_plane, &pt_origin, &vn, 1);
		//×ó²Ã¼ôÃæ
		VECTOR3D_INITXYZ(&vn, -1, 0, 1);
		PLANE3D_Init(&cam->lt_clip_plane, &pt_origin, &vn, 1);
		//ÉϲüôÃæ
		VECTOR3D_INITXYZ(&vn, 0, 1, 1);
		PLANE3D_Init(&cam->tp_clip_plane, &pt_origin, &vn, 1);
		//ϲüôÃæ
		VECTOR3D_INITXYZ(&vn, 0, -1, 1);
		PLANE3D_Init(&cam->bt_clip_plane, &pt_origin, &vn, 1);
	}
	else
	{
		POINT3D pt_origin;
		VECTOR3D_INITXYZ(&pt_origin, 0, 0, 0);
		VECTOR3D vn;

		VECTOR3D_INITXYZ(&vn, cam->view_dist, 0, cam->viewplane_width / 2.0f);
		PLANE3D_Init(&cam->rt_clip_plane, &pt_origin, &vn, 1);

		VECTOR3D_INITXYZ(&vn, -cam->view_dist, 0, cam->viewplane_width / 2.0f);
		PLANE3D_Init(&cam->lt_clip_plane, &pt_origin, &vn, 1);

		VECTOR3D_INITXYZ(&vn, 0, cam->view_dist, cam->viewplane_width / 2.0f);
		PLANE3D_Init(&cam->rt_clip_plane, &pt_origin, &vn, 1);

		VECTOR3D_INITXYZ(&vn, 0, -cam->view_dist, cam->viewplane_width / 2.0f);
		PLANE3D_Init(&cam->rt_clip_plane, &pt_origin, &vn, 1);
	}
}