public CameraTransform(Point3D position, Quaternion rotation) { DefaultPosition = new Point3D(position); DefaultRotation = new Quaternion(rotation); Position = new Point3D(position); Rotation = new Quaternion(rotation); }
public Point2D Get2DPoint(Point3D point) { Point3D new_point = Transform.GetCameraCoordinates(point); double x2pz2 = new_point.X * new_point.X + new_point.Z * new_point.Z; double r_scale = Math3D.InvSqrt(x2pz2) * Math.Acos(new_point.Y * Math3D.InvSqrt(x2pz2 + new_point.Y * new_point.Y)) * parameter; return new Point2D(new_point.X * r_scale, new_point.Z * r_scale); }
private void Move(Point3D axis, double length) { Position.X += length * axis.X; Position.Y += length * axis.Y; Position.Z += length * axis.Z; }
public Point3D GetCameraCoordinates(Point3D point) { // Reverse rotate the world. return Rotate(Rotation.GetConjugate(), new Point3D(point.X - Position.X, point.Y - Position.Y, point.Z - Position.Z)); }
public void Reset() { Position = new Point3D(DefaultPosition); Rotation = new Quaternion(DefaultRotation); }
private static Point3D Rotate(Quaternion rotation, Point3D point) { Quaternion result = rotation * (new Quaternion(0.0, point.X, point.Y, point.Z)) * rotation.GetConjugate(); return new Point3D(result.I, result.J, result.K); }
private static Quaternion GetRotateQuaternion(Point3D axis, double angle) { double t = angle * 0.5; double s = Math.Sin(t); return new Quaternion(Math.Cos(t), s * axis.X, s * axis.Y, s * axis.Z); }
private PointF GetViewCoordinates(Point3D point) { Point2D p = Camera.Get2DPoint(point); return new PointF((float)(x_center + p.X), (float)(y_center - p.Y)); }
private List<PointF> SliceLine(double min_length, Point3D line_start, Point3D line_end, PointF line_start_map, PointF line_end_map) { var point_list = new List<PointF>(); Action<Point3D, Point3D, PointF, PointF, int> slice = null; slice = (point_1, point_2, point_map_1, point_map_2, depth) => { if (depth > max_recursion_depth) { return; } Point3D p3d_center = new Point3D((point_1.X + point_2.X) * 0.5, (point_1.Y + point_2.Y) * 0.5, (point_1.Z + point_2.Z) * 0.5); PointF p_center = GetViewCoordinates(p3d_center); if (Utilities.GetDistance(point_map_1, p_center) > min_length) { slice(point_1, p3d_center, point_map_1, p_center, depth + 1); } point_list.Add(p_center); if (Utilities.GetDistance(p_center, point_map_2) > min_length) { slice(p3d_center, point_2, p_center, point_map_2, depth + 1); } }; point_list.Add(line_start_map); slice(line_start, line_end, line_start_map, line_end_map, 0); point_list.Add(line_end_map); return point_list; }
protected override void DoDraw(Graphics graphics) { graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.SmoothingMode = SmoothingMode.HighQuality; Action<Point3D, Point3D, Pen, string, Brush> draw_axis = (p1, p2, pen, label, brush) => { var axis = GetLines(min_line_length, p1, p2); graphics.DrawLines(pen, axis); graphics.DrawString(label, main_font, brush, axis.Last()); }; draw_axis(new Point3D(x_axis_from, 0.0, 0.0), new Point3D(x_axis_to, 0.0, 0.0), x_axis_pen, "X", x_axis_brush); draw_axis(new Point3D(0.0, y_axis_from, 0.0), new Point3D(0.0, y_axis_to, 0.0), y_axis_pen, "Y", y_axis_brush); draw_axis(new Point3D(0.0, 0.0, z_axis_from), new Point3D(0.0, 0.0, z_axis_to), z_axis_pen, "Z", z_axis_brush); Point3D yaw_axis = this.Camera.Transform.GetYawAxis(); Point3D pitch_axis = this.Camera.Transform.GetPitchAxis(); Point3D roll_axis = this.Camera.Transform.GetRollAxis(); draw_axis(new Point3D(), yaw_axis, yaw_axis_pen, "Yaw", yaw_axis_brush); draw_axis(new Point3D(), pitch_axis, pitch_axis_pen, "Pitch", pitch_axis_brush); draw_axis(new Point3D(), roll_axis, roll_axis_pen, "Roll", roll_axis_brush); Point3D ypr = new Point3D(yaw_axis.X + pitch_axis.X + roll_axis.X, yaw_axis.Y + pitch_axis.Y + roll_axis.Y, yaw_axis.Z + pitch_axis.Z + roll_axis.Z); var ypr_box = new Point3D[] { new Point3D(-yaw_axis.X - pitch_axis.X + roll_axis.X, -yaw_axis.Y - pitch_axis.Y + roll_axis.Y, -yaw_axis.Z - pitch_axis.Z + roll_axis.Z), new Point3D(-yaw_axis.X + pitch_axis.X + roll_axis.X, -yaw_axis.Y + pitch_axis.Y + roll_axis.Y, -yaw_axis.Z + pitch_axis.Z + roll_axis.Z), new Point3D(yaw_axis.X + pitch_axis.X + roll_axis.X, yaw_axis.Y + pitch_axis.Y + roll_axis.Y, yaw_axis.Z + pitch_axis.Z + roll_axis.Z), new Point3D(yaw_axis.X - pitch_axis.X + roll_axis.X, yaw_axis.Y - pitch_axis.Y + roll_axis.Y, yaw_axis.Z - pitch_axis.Z + roll_axis.Z), new Point3D(-yaw_axis.X - pitch_axis.X - roll_axis.X, -yaw_axis.Y - pitch_axis.Y - roll_axis.Y, -yaw_axis.Z - pitch_axis.Z - roll_axis.Z), new Point3D(-yaw_axis.X + pitch_axis.X - roll_axis.X, -yaw_axis.Y + pitch_axis.Y - roll_axis.Y, -yaw_axis.Z + pitch_axis.Z - roll_axis.Z), new Point3D(yaw_axis.X + pitch_axis.X - roll_axis.X, yaw_axis.Y + pitch_axis.Y - roll_axis.Y, yaw_axis.Z + pitch_axis.Z - roll_axis.Z), new Point3D(yaw_axis.X - pitch_axis.X - roll_axis.X, yaw_axis.Y - pitch_axis.Y - roll_axis.Y, yaw_axis.Z - pitch_axis.Z - roll_axis.Z) }; this.DrawPolygon(graphics, box_pen, min_line_length, ypr_box[0], ypr_box[1], ypr_box[2], ypr_box[3]); this.DrawPolygon(graphics, box_pen, min_line_length, ypr_box[4], ypr_box[5], ypr_box[6], ypr_box[7]); this.DrawLines(graphics, box_pen, min_line_length, ypr_box[0], ypr_box[4]); this.DrawLines(graphics, box_pen, min_line_length, ypr_box[1], ypr_box[5]); this.DrawLines(graphics, box_pen, min_line_length, ypr_box[2], ypr_box[6]); this.DrawLines(graphics, box_pen, min_line_length, ypr_box[3], ypr_box[7]); var box_vertexes = new Point3D[] { new Point3D(-box_length, -box_length, -box_length), new Point3D(-box_length, box_length, -box_length), new Point3D(box_length, box_length, -box_length), new Point3D(box_length, -box_length, -box_length), new Point3D(-box_length, -box_length, 0.0), new Point3D(-box_length, box_length, 0.0), new Point3D(box_length, box_length, 0.0), new Point3D(box_length, -box_length, 0.0), new Point3D(-box_length, -box_length, box_length), new Point3D(-box_length, box_length, box_length), new Point3D(box_length, box_length, box_length), new Point3D(box_length, -box_length, box_length), new Point3D(-box_length, 0.0, -box_length), new Point3D(-box_length, 0.0, box_length), new Point3D(box_length, 0.0, box_length), new Point3D(box_length, 0.0, -box_length), new Point3D(0.0, -box_length, -box_length), new Point3D(0.0, -box_length, box_length), new Point3D(0.0, box_length, box_length), new Point3D(0.0, box_length, -box_length) }; this.DrawPolygon(graphics, box_pen, min_line_length, box_vertexes[0], box_vertexes[1], box_vertexes[2], box_vertexes[3]); this.DrawPolygon(graphics, box_pen, min_line_length, box_vertexes[4], box_vertexes[5], box_vertexes[6], box_vertexes[7]); this.DrawPolygon(graphics, box_pen, min_line_length, box_vertexes[8], box_vertexes[9], box_vertexes[10], box_vertexes[11]); this.DrawPolygon(graphics, box_pen, min_line_length, box_vertexes[12], box_vertexes[13], box_vertexes[14], box_vertexes[15]); this.DrawPolygon(graphics, box_pen, min_line_length, box_vertexes[16], box_vertexes[17], box_vertexes[18], box_vertexes[19]); this.DrawLines(graphics, box_pen, min_line_length, box_vertexes[0], box_vertexes[8]); this.DrawLines(graphics, box_pen, min_line_length, box_vertexes[1], box_vertexes[9]); this.DrawLines(graphics, box_pen, min_line_length, box_vertexes[2], box_vertexes[10]); this.DrawLines(graphics, box_pen, min_line_length, box_vertexes[3], box_vertexes[11]); graphics.DrawLine(guide_line_pen, 0.0f, Size.Height * 0.5f, Size.Width, Size.Height * 0.5f); graphics.DrawLine(guide_line_pen, Size.Width * 0.5f, 0.0f, Size.Width * 0.5f, Size.Height); graphics.DrawString(string.Format("Scene Size: {0} × {1}\r\n" + "Camera Angle Of View: {2}°\r\n" + "Camera Full View Size: {3}\r\n" + "Yaw Axis: ({4}, {5}, {6})\r\n" + "Pitch Axis: ({7}, {8}, {9})\r\n" + "Roll Axis: ({10}, {11}, {12})\r\n" + "Camera Position: ({13}, {14}, {15})\r\n" + "Camera Rotation: [{16}, {17}, {18}, {19}]", this.Size.Width, this.Size.Height, this.Camera.AngleOfView / Math.PI * 180, this.Camera.FullViewSize, yaw_axis.X, yaw_axis.Y, yaw_axis.Z, pitch_axis.X, pitch_axis.Y, pitch_axis.Z, roll_axis.X, roll_axis.Y, roll_axis.Z, this.Camera.Transform.Position.X, this.Camera.Transform.Position.Y, this.Camera.Transform.Position.Z, this.Camera.Transform.Rotation.W, this.Camera.Transform.Rotation.I, this.Camera.Transform.Rotation.J, this.Camera.Transform.Rotation.K), main_font, SystemBrushes.WindowText, 10, 10); }