//Выполнить public static void Execute(IFigure obj, AffineMatrix m) { if (obj is Polyhedron) { foreach (var x in (obj as Polyhedron).Faces) { Execute(x, m); } Execute((obj as Polyhedron).Center, m); } else if (obj is Face) { foreach (var x in (obj as Face).Edges) { Execute(x, m); } } else if (obj is Edge) { Execute((obj as Edge).First, m); Execute((obj as Edge).Second, m); } else if (obj is My_Point) { (obj as My_Point).Copy(m * (obj as My_Point)); } }
public static void RotateOverStreight(Polyhedron ph, Point3d a, Point3d b, double phi) { double m = b.X - a.X; double n = b.Y - a.Y; double p = b.Z - a.Z; double s = Math.Sqrt(m * m + n * n); double d = Math.Sqrt(m * m + n * n + p * p); AffineMatrix m0; if (Math.Abs(s) < 1e-6) { AffineMatrix m1 = AffineMatrix.CreateTranslationMatrix(-a.X, -a.Y, -a.Z); AffineMatrix m4 = AffineMatrix.CreateZaxisRotationMatrix(phi); AffineMatrix m7 = AffineMatrix.CreateTranslationMatrix(a.X, a.Y, a.Z); m0 = m7 * m4 * m1; } else { AffineMatrix m1 = AffineMatrix.CreateTranslationMatrix(-a.X, -a.Y, -a.Z); AffineMatrix m2 = AffineMatrix.CreateZaxisRotationMatrix(n / s, m / s); AffineMatrix m3 = AffineMatrix.CreateXaxisRotationMatrix(p / d, s / d); AffineMatrix m4 = AffineMatrix.CreateZaxisRotationMatrix(phi); AffineMatrix m5 = AffineMatrix.CreateXaxisRotationMatrix(p / d, -s / d); AffineMatrix m6 = AffineMatrix.CreateZaxisRotationMatrix(n / s, -m / s); AffineMatrix m7 = AffineMatrix.CreateTranslationMatrix(a.X, a.Y, a.Z); m0 = m7 * m6 * m5 * m4 * m3 * m2 * m1; } Execute(ph, m0); }
public static AffineMatrix CreateOrtographicProjectionMatrix(char axis) { AffineMatrix result = new AffineMatrix(); switch (axis) { case 'x': case 'X': result[0, 0] = 0; break; case 'y': case 'Y': result[0, 0] = 0; result[1, 1] = 0; result[2, 2] = 0; result[1, 2] = 1; result[2, 0] = 1; break; case 'z': case 'Z': result[0, 0] = 0; result[1, 1] = 0; result[2, 2] = 0; result[1, 0] = 1; result[2, 1] = 1; break; default: throw new Exception("Wrong char in ortographic projection!"); } return(result); }
public static void Execute(IFigure obj, AffineMatrix m) { if (obj is Polyhedron) { foreach (var x in (obj as Polyhedron).Faces) { Execute(x, m); } Execute((obj as Polyhedron).Center, m); } else if (obj is Face) { foreach (var x in (obj as Face).Edges) { Execute(x, m); } } else if (obj is Edge) { Execute((obj as Edge).First, m); Execute((obj as Edge).Second, m); } else if (obj is Point3d) { (obj as Point3d).Copy(m * (obj as Point3d)); } else { throw new Exception("Unknown type"); } }
public static AffineMatrix CreateReflectionMatrix(char axis) { AffineMatrix result = new AffineMatrix(); switch (axis) { case 'x': case 'X': result[0, 0] = -1; break; case 'y': case 'Y': result[1, 1] = -1; break; case 'z': case 'Z': result[2, 2] = -1; break; default: throw new Exception("Wrong char in reflection!"); } return(result); }
//Поворот вокруг оси public static void Rotate(Polyhedron polyhedron, My_Point a, My_Point b, double anglPhi) { double m = b.X - a.X; double n = b.Y - a.Y; double p = b.Z - a.Z; double s = Math.Sqrt(m * m + n * n); double d = Math.Sqrt(m * m + n * n + p * p); AffineMatrix m0; if (Math.Abs(s) < 1e-6) { AffineMatrix m1 = AffineMatrix.TranslationMatrix(-a.X, -a.Y, -a.Z); AffineMatrix m4 = AffineMatrix.ZaxisRotationMatrix(anglPhi); AffineMatrix m7 = AffineMatrix.TranslationMatrix(a.X, a.Y, a.Z); m0 = m7 * m4 * m1; } else { AffineMatrix m1 = AffineMatrix.TranslationMatrix(-a.X, -a.Y, -a.Z); AffineMatrix m2 = AffineMatrix.ZaxisRotationMatrix(n / s, m / s); AffineMatrix m3 = AffineMatrix.XaxisRotationMatrix(p / d, s / d); AffineMatrix m4 = AffineMatrix.ZaxisRotationMatrix(anglPhi); AffineMatrix m5 = AffineMatrix.XaxisRotationMatrix(p / d, -s / d); AffineMatrix m6 = AffineMatrix.ZaxisRotationMatrix(n / s, -m / s); AffineMatrix m7 = AffineMatrix.TranslationMatrix(a.X, a.Y, a.Z); m0 = m7 * m6 * m5 * m4 * m3 * m2 * m1; } Execute(polyhedron, m0); }
public static void MakePerspectiveProjection(Polyhedron ph, double fov, double zfar, double znear) { AffineMatrix m = AffineMatrix.CreatePerspectiveProjectionMatrix(fov, zfar, znear); //AffineMatrix.Transpon(m); Execute(ph, m); }
public static void RotateOverCenter(Polyhedron ph, char axis, double phi) { Point3d p = ph.Center.Clone() as Point3d; AffineMatrix m1 = AffineMatrix.CreateTranslationMatrix(-p.X, -p.Y, -p.Z); AffineMatrix m2; switch (axis) { case 'x': case 'X': m2 = AffineMatrix.CreateXaxisRotationMatrix(phi); break; case 'y': case 'Y': m2 = AffineMatrix.CreateYaxisRotationMatrix(phi); break; case 'z': case 'Z': m2 = AffineMatrix.CreateZaxisRotationMatrix(phi); break; default: throw new Exception("Wrong char in rotation over center!"); } AffineMatrix m3 = AffineMatrix.CreateTranslationMatrix(p.X, p.Y, p.Z); AffineMatrix m = m3 * m2 * m1; Execute(ph, m); }
public static AffineMatrix CreateCentralProjectionMatrix(double a) { AffineMatrix result = new AffineMatrix(); result[0, 0] = 0; result[3, 0] = -1.0 / a; return(result); }
public static void Execute(Cam cam, AffineMatrix m) { Point3d o = m * new Point3d(cam.Pos.X, cam.Pos.Y, cam.Pos.Z); Point3d oX = m * new Point3d(cam.Pos.X + cam.View.X, cam.Pos.Y + cam.View.Y, cam.Pos.Z + cam.View.Z); Point3d oY = m * new Point3d(cam.Pos.X + cam.Hor.X, cam.Pos.Y + cam.Hor.Y, cam.Pos.Z + cam.Hor.Z); Point3d oZ = m * new Point3d(cam.Pos.X + cam.Vert.X, cam.Pos.Y + cam.Vert.Y, cam.Pos.Z + cam.Vert.Z); cam.SetCamByPoints(o, oX, oY, oZ); }
//==========================ИЗ ЛЕКЦИИ======================= public static AffineMatrix TranslationMatrix(double x, double y, double z) { AffineMatrix result = new AffineMatrix(); result[0, 3] = x; result[1, 3] = y; result[2, 3] = z; return(result); }
public static AffineMatrix CreateScaleMatrix(double mx, double my, double mz) { AffineMatrix result = new AffineMatrix(); result[0, 0] = mx; result[1, 1] = my; result[2, 2] = mz; return(result); }
public static void ScaleOverCenter(Polyhedron ph, double a) { Point3d p = ph.Center.Clone() as Point3d; AffineMatrix m1 = AffineMatrix.CreateTranslationMatrix(-p.X, -p.Y, -p.Z); AffineMatrix m2 = AffineMatrix.CreateScaleMatrix(a, a, a); AffineMatrix m3 = AffineMatrix.CreateTranslationMatrix(p.X, p.Y, p.Z); AffineMatrix m = m3 * m2 * m1; Execute(ph, m); }
public static AffineMatrix ZaxisRotationMatrix(double cos, double sin) { AffineMatrix result = new AffineMatrix(); result[0, 0] = cos; result[0, 1] = -sin; result[1, 0] = sin; result[1, 1] = cos; return(result); }
public static AffineMatrix CreateZaxisRotationMatrix(double cos_phi, double sin_phi) { AffineMatrix result = new AffineMatrix(); result[0, 0] = cos_phi; result[0, 1] = -sin_phi; result[1, 0] = sin_phi; result[1, 1] = cos_phi; return(result); }
public static void RotateCam(Cam cam, double dist, double phi) { Vector toFixedPoint = cam.Pos - (cam.View * dist); Vector RotDirection = toFixedPoint + cam.Vert; Point3d fixedPoint = new Point3d(toFixedPoint.X, toFixedPoint.Y, toFixedPoint.Z); Point3d rotDir = new Point3d(RotDirection.X, RotDirection.Y, RotDirection.Z); AffineMatrix m = AffineMatrix.CreateRotationMatrix(fixedPoint, rotDir, phi); Execute(cam, m); }
public static AffineMatrix IsometricMatrix() { AffineMatrix result = new AffineMatrix(); result[1, 0] = -Math.Sqrt(3) / 2.0; result[1, 1] = -result[1, 0]; result[2, 0] = result[2, 1] = -1.0 / 2.0; result[2, 2] = 1; result[0, 0] = 0; return(result); }
public static AffineMatrix CreateZaxisRotationMatrix(double phi) { double cos_phi = Math.Cos(phi); double sin_phi = Math.Sin(phi); AffineMatrix result = new AffineMatrix(); result[0, 0] = cos_phi; result[0, 1] = -sin_phi; result[1, 0] = sin_phi; result[1, 1] = cos_phi; return(result); }
public static void Transpon(AffineMatrix a) { for (int i = 0; i < 4; i++) { for (int j = 0; j < i; j++) { double tmp = a[i, j]; a[i, j] = a[j, i]; a[j, i] = tmp; } } }
public static AffineMatrix YaxisRotationMatrix(double phi) { double cos = Math.Cos(phi); double sin = Math.Sin(phi); AffineMatrix result = new AffineMatrix(); result[1, 1] = cos; result[3, 1] = -sin; result[1, 3] = sin; result[3, 3] = cos; return(result); }
//================================================================ public static AffineMatrix operator *(AffineMatrix a, AffineMatrix b) { AffineMatrix result = new AffineMatrix(); for (int i = 0; i < 4; ++i) { result[i, i] = 0; } for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { for (int k = 0; k < 4; ++k) { result[i, j] += a[i, k] * b[k, j]; } } } return(result); }
public static AffineMatrix CreateViewMatrix(Point3d pos, Point3d view, Point3d hor, Point3d vert) { AffineMatrix res = new AffineMatrix(); res[0, 0] = vert.X; res[0, 1] = vert.Y; res[0, 2] = vert.Z; res[1, 0] = hor.X; res[1, 1] = hor.Y; res[1, 2] = hor.Z; res[2, 0] = view.X; res[2, 1] = view.Y; res[2, 2] = view.Z; res[3, 0] = res[3, 1] = res[3, 2] = 0; res[0, 3] = -(pos.X * vert.X + pos.Y * vert.Y + pos.Z * vert.Z); res[1, 3] = -(pos.X * hor.X + pos.Y * hor.Y + pos.Z * hor.Z); res[2, 3] = -(pos.X * view.X + pos.Y * view.Y + pos.Z * view.Z); res[3, 3] = 1; return(res); }
internal static AffineMatrix CreatePerspectiveProjectionMatrix(double fov, double zfar, double znear) { double w = 1 / Math.Tan(fov / 2); AffineMatrix perspectiveProjectionMatrix = new AffineMatrix(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { perspectiveProjectionMatrix[i, j] = 0; } } perspectiveProjectionMatrix[0, 0] = w; perspectiveProjectionMatrix[1, 1] = w; perspectiveProjectionMatrix[2, 2] = zfar / (zfar - znear); perspectiveProjectionMatrix[2, 3] = 1; perspectiveProjectionMatrix[3, 2] = zfar * (-znear) / (zfar - znear); return(perspectiveProjectionMatrix); }
public static AffineMatrix CreateViewMatrix(Vector pos, Vector view, Vector hor, Vector vert) { AffineMatrix res = new AffineMatrix(); res[0, 0] = vert.X; res[0, 1] = vert.Y; res[0, 2] = vert.Z; res[1, 0] = hor.X; res[1, 1] = hor.Y; res[1, 2] = hor.Z; res[2, 0] = view.X; res[2, 1] = view.Y; res[2, 2] = view.Z; res[3, 0] = res[3, 1] = res[3, 2] = 0; res[0, 3] = -(pos * vert); res[1, 3] = -(pos * hor); res[2, 3] = -(pos * view); res[3, 3] = 1; return(res); }
public static void CentralProjection(Polyhedron ph, double c) { AffineMatrix m = AffineMatrix.CreateCentralProjectionMatrix(c); Execute(ph, m); }
public static void Reflect(Polyhedron ph, char axis) { AffineMatrix m = AffineMatrix.CreateReflectionMatrix(axis); Execute(ph, m); }
public static void IsometricProjection(Polyhedron ph) { AffineMatrix m = AffineMatrix.CreateIsometricProjectionMatrix(); Execute(ph, m); }
public static void IsometricProjection(Polyhedron polyhedron) { AffineMatrix m = AffineMatrix.IsometricMatrix(); Execute(polyhedron, m); }
public static void OrtographicProjection(Polyhedron ph, char axis) { AffineMatrix m = AffineMatrix.CreateOrtographicProjectionMatrix(axis); Execute(ph, m); }
public static void MakeView(Polyhedron ph, Cam cam) { AffineMatrix m = AffineMatrix.CreateViewMatrix(cam.Pos, cam.View, cam.Hor, cam.Vert); Execute(ph, m); }