public static MyMatrix4 getSingleRotationMatrix(double angle, char axis) { MyMatrix4 m = getOneMatrix(); switch (axis) { case 'x': m.Lattice[1, 1] = (float)Math.Cos(angle); m.Lattice[1, 2] = (float)-Math.Sin(angle); m.Lattice[2, 1] = (float)Math.Sin(angle); m.Lattice[2, 2] = (float)Math.Cos(angle); break; case 'y': m.Lattice[0, 0] = (float)Math.Cos(angle); m.Lattice[0, 2] = (float)Math.Sin(angle); m.Lattice[2, 0] = (float)-Math.Sin(angle); m.Lattice[2, 2] = (float)Math.Cos(angle); break; case 'z': m.Lattice[0, 0] = (float)Math.Cos(angle); m.Lattice[0, 1] = (float)-Math.Sin(angle); m.Lattice[1, 0] = (float)Math.Sin(angle); m.Lattice[1, 1] = (float)Math.Cos(angle); break; } return(m); }
public static MyMatrix4 getTranslationMatrix(float x, float y, float z) { MyMatrix4 m = getOneMatrix(); m.Lattice[0, 3] = x; m.Lattice[1, 3] = y; m.Lattice[2, 3] = z; return(m); }
public static MyMatrix4 getScaleMatrix(float x, float y, float z) { MyMatrix4 m = getOneMatrix(); m.Lattice[0, 0] = x; m.Lattice[1, 1] = y; m.Lattice[2, 2] = z; return(m); }
private Vector3[] rotateAroundPoint(Vector3[] to_rotate, Vector3 point, MyMatrix4 mat) { Vector3[] field = new Vector3[to_rotate.Length]; for (int i = 0; i < to_rotate.Length; i++) { field[i] = mat * (to_rotate[i] - point) + point; } return(field); }
public static MyMatrix4 getOneMatrix() { MyMatrix4 m = new MyMatrix4(); for (int i = 0; i < 4; i++) { m.Lattice[i, i] = 1; } return(m); }
public static Vector3[] rotate(Vector3[] to_rotate, double angle, char axis) { Vector3[] rotated = new Vector3[to_rotate.Length]; MyMatrix4 mat = MyMatrix4.getSingleRotationMatrix(angle, axis); for (int i = 0; i < to_rotate.Length; i++) { rotated[i] = mat * to_rotate[i]; } return(rotated); }
public static Vector3[] translate(Vector3[] to_translate, float x, float y, float z) { Vector3[] translated = new Vector3[to_translate.Length]; MyMatrix4 mat = MyMatrix4.getTranslationMatrix(x, y, z); for (int i = 0; i < to_translate.Length; i++) { translated[i] = mat * to_translate[i]; } return(translated); }
public static Vector3[] scale(Vector3[] to_scale, float x, float y, float z) { Vector3[] scaled = new Vector3[to_scale.Length]; MyMatrix4 mat = MyMatrix4.getScaleMatrix(x, y, z); for (int i = 0; i < to_scale.Length; i++) { scaled[i] = mat * to_scale[i]; } return(scaled); }
public static Vector3 rotate(Vector3 to_rotate, double angle, char axis) { MyMatrix4 mat = MyMatrix4.getSingleRotationMatrix(angle, axis); return(mat * to_rotate); }
public static Vector3 translate(Vector3 to_translate, float x, float y, float z) { MyMatrix4 mat = MyMatrix4.getTranslationMatrix(x, y, z); return(mat * to_translate); }
public static Vector3 scale(Vector3 to_scale, float x, float y, float z) { MyMatrix4 mat = MyMatrix4.getScaleMatrix(x, y, z); return(mat * to_scale); }
private Vector3 rotateAroundPoint(Vector3 to_rotate, Vector3 point, MyMatrix4 mat) { return(mat * (to_rotate - point) + point); }
private int createBentPipe(SceneBrep scene, Matrix4 m, Vector3 v1, Vector3 v2, Vector3 v3) { Vector3[] centers = new Vector3[bdetail]; Vector3 oned = (v1 - v2) * size; Vector3 twod = (v3 - v2) * size; Vector3 rtwod = (v3 - v2) * (1 - size); Vector3 rot_point = v2 + oned + twod; Vector3 test_point = oned + twod; test_point.X = Math.Abs(test_point.X); test_point.Y = Math.Abs(test_point.Y); test_point.Z = Math.Abs(test_point.Z); double angle = -(Math.PI / 2) / (bdetail - 1); char axis; if (test_point.Y < test_point.X) { axis = (test_point.Z < test_point.Y) ? 'z' : 'y'; } else { axis = (test_point.Z < test_point.X) ? 'z' : 'x'; } MyMatrix4 mat = MyMatrix4.getSingleRotationMatrix(angle, axis); float l = longestSide(last_circle[0] - v1); Vector3 sp = rotateAroundPoint(last_circle[0], rot_point, mat); float l2 = longestSide(sp - v1); if (l2 < l) { mat = MyMatrix4.getSingleRotationMatrix(-angle, axis); } Vector3[] field = new Vector3[bdetail * cdetail]; Array.Copy(last_circle, field, cdetail); centers[0] = getCenter(last_circle); Vector3[] new_circle = new Vector3[cdetail]; for (int i = 1; i < bdetail; i++) { Array.ConstrainedCopy(field, (i - 1) * cdetail, new_circle, 0, cdetail); new_circle = rotateAroundPoint(new_circle, rot_point, mat); Array.ConstrainedCopy(new_circle, 0, field, i * cdetail, cdetail); centers[i] = getCenter(new_circle); } Array.ConstrainedCopy(field, (bdetail - 1) * cdetail, last_circle, 0, cdetail); for (int i = 0; i < cdetail; i++) { last_circle[i] += rtwod - twod; } int[] v = new int[field.Length]; for (int i = 0; i < field.Length; i++) { v[i] = scene.AddVertex(Vector3.TransformPosition(field[i], m)); scene.SetNormal(v[i], Vector3.TransformVector(field[i] - centers[i / cdetail], m).Normalized()); float r = (field[i].X + 1.1f) / 2.5f; float g = (field[i].Y + 1.1f) / 2.5f; float b = (field[i].Z + 1.1f) / 2.5f; scene.SetColor(v[i], new Vector3(r, g, b)); } for (int i = 0; i < cdetail - 1; i++) { scene.AddTriangle(connect_indices[i], v[i], connect_indices[i + 1]); scene.AddTriangle(connect_indices[i + 1], v[i], v[i + 1]); } scene.AddTriangle(connect_indices[cdetail - 1], v[cdetail - 1], connect_indices[0]); scene.AddTriangle(connect_indices[0], v[cdetail - 1], v[0]); for (int i = 0; i < (bdetail - 1); i++) { for (int j = 0; j < cdetail - 1; j++) { int a = i * cdetail + j; scene.AddTriangle(v[a], v[a + cdetail], v[a + 1]); scene.AddTriangle(v[a + 1], v[a + cdetail], v[a + cdetail + 1]); } int b = i * cdetail + cdetail - 1; scene.AddTriangle(v[b], v[b + cdetail], v[b - cdetail + 1]); scene.AddTriangle(v[b - cdetail + 1], v[b + cdetail], v[b + 1]); } Array.ConstrainedCopy(v, (bdetail - 1) * cdetail, connect_indices, 0, cdetail); return(bdetail * cdetail * 2 + cdetail * 2); }