//Matrix rotation public static PointF3D Rotate(PointF3D rotation, PointF3D vertex) { float[,] rotationX = { { 1, 0, 0 }, { 0, (float)Math.Cos(rotation.x), (float)-Math.Sin(rotation.x) }, { 0, (float)Math.Sin(rotation.x), (float)Math.Cos(rotation.x) } }; float[,] rotationY = { { (float)Math.Cos(rotation.y), 0, (float)Math.Sin(rotation.y) }, { 0, 1, 0 }, { (float)-Math.Sin(rotation.y), 0, (float)Math.Cos(rotation.y) } }; float[,] rotationZ = { { (float)Math.Cos(rotation.z), (float)-Math.Sin(rotation.z), 0 }, { (float)Math.Sin(rotation.z), (float)Math.Cos(rotation.z), 0 }, { 0, 0, 1 } }; PointF3D rotated = Multiply3D(rotationY, vertex); rotated = Multiply3D(rotationX, rotated); rotated = Multiply3D(rotationZ, rotated); return(rotated); }
public static Model GetModel(string path, PointF3D translationToMid, PointF3D rotation, float scale) { StreamReader fileReader = new StreamReader(path); List <PointF3D[]> vertexLines = new List <PointF3D[]>(); List <PolyLine[]> lineLines = new List <PolyLine[]>(); List <PolyLine[]> faceLines = new List <PolyLine[]>(); if (fileReader != null) { while (!fileReader.EndOfStream) { var line = fileReader.ReadLine(); if (line.StartsWith("v ")) { vertexLines.Add(GetVertexes(fileReader, line, translationToMid, rotation, scale)); } else if (line.StartsWith("l ")) { lineLines.Add(GetLines(fileReader, line)); } else if (line.StartsWith("f ")) { faceLines.Add(GetFaces(fileReader, line)); } } } PointF3D[] vertexes; PolyLine[] polyLines; PolyLine[] faces; if (vertexLines.Count == 0) { vertexes = new PointF3D[0]; } else { vertexes = vertexLines[0]; } if (faceLines.Count == 0) { faces = new PolyLine[0]; } else { faces = faceLines[0]; } if (lineLines.Count == 0) { polyLines = new PolyLine[0]; } else { polyLines = lineLines[0]; } return(new Model(vertexes, polyLines, faces)); }
public static PointF3D[] GetVertexes(StreamReader fileReader, string current, PointF3D translationToMid, PointF3D rotation, float scale) { List <PointF3D> returnList = new List <PointF3D>(); var line = current; do { var point = line.Replace('.', ',').Split(' '); PointF3D vertex = null; try { if (point[1] != "") { vertex = new PointF3D((float)Convert.ToDouble(point[1]), (float)Convert.ToDouble(point[2]), (float)Convert.ToDouble(point[3])); } else { vertex = new PointF3D((float)Convert.ToDouble(point[2]), (float)Convert.ToDouble(point[3]), (float)Convert.ToDouble(point[4])); } } catch { continue; } //Translate it vertex += translationToMid; //Scale it vertex *= scale; //Rotate it vertex = Matrix.Rotate(rotation, vertex); returnList.Add(vertex); line = fileReader.ReadLine(); } while (line.StartsWith("v ")); return(returnList.ToArray()); }
public void ScaleBy(PointF3D scaleBy) { //Edit translater scaler += scaleBy; //Calculate new points CalcNewPoints(); }
public void Rotation(PointF3D rotation) { //Edit rotation this.rotation = rotation; //Calculate new points CalcNewPoints(); }
private void Setup() { calc2DPoints = new PointF[vertexes.Length]; rotation = new PointF3D(); translator = new PointF3D(); translator2D = new PointF(); CalcNewPoints(); }
//Constructor - With rotation public Model(PointF3D[] vertexes, PointF3D rotation) { Setup(); this.vertexes = vertexes; this.rotation = rotation; calc2DPoints = new PointF[vertexes.Length]; CalcNewPoints(); }
public void Translation(PointF3D translation) { //Edit translater this.translator = translation; //Calculate new points CalcNewPoints(); }
public void Scaler(float scaler) { //Edit translater this.scaler = new PointF3D(scaler, scaler, scaler); //Calculate new points CalcNewPoints(); }
public void TranslateBy(PointF3D translateBy) { //Edit translater translator += translateBy; //Calculate new points CalcNewPoints(); }
public void Scaler(PointF3D scaler) { //Edit translater this.scaler = scaler; //Calculate new points CalcNewPoints(); }
public void RotateBy(PointF3D rotateBy) { //Edit rotater rotation += rotateBy; //Calculate new points CalcNewPoints(); }
public static float[,] ToMatrix(PointF3D p) { float[,] m = new float[3, 1]; m[0, 0] = p.x; m[1, 0] = p.y; m[2, 0] = p.z; return(m); }
public static PointF3D ToPoint3D(float[,] m) { var p = new PointF3D(); p.x = m[0, 0]; p.y = m[1, 0]; p.z = m[2, 0]; return(p); }
private void Setup() { rotation = new PointF3D(); translator = new PointF3D(); scaler = new PointF3D(1, 1, 1); translator2D = new PointF(); faces = new PolyLine[0]; polyLines = new PolyLine[0]; }
public void TranslateBy(PointF3D translateBy) { //Edit translater translator.x += translateBy.x; translator.y += translateBy.y; translator.z += translateBy.z; //Calculate new points CalcNewPoints(); }
//Matrix projektion public static PointF Project(PointF3D vertex) { float[,] projectionM = { { 1, 0, 0 }, { 0, 1, 0 } }; return(Matrix.Multiply2D(projectionM, vertex)); }
public void RotateBy(PointF3D rotateBy) { //Edit rotater rotation.x += rotateBy.x; rotation.y += rotateBy.y; rotation.z += rotateBy.z; //Calculate new points CalcNewPoints(); }
public PointF CalcNewPoint(PointF3D vertex) { //Scale the vertex var Vertex = vertex * scaler; //Translate it Vertex = Vertex + translator; //Rotate it Vertex = Matrix.Rotate(rotation, Vertex); //Project it var point = Matrix.Project(Vertex); //Translate it return(new PointF(point.X + translator2D.X, point.Y + translator2D.Y)); }
private void CalcNewPoints() { //For each vertex in model for (int i = 0; i < vertexes.Length; i++) { //Translate the vertex var Vertex = new PointF3D(vertexes[i].x + translator.x, vertexes[i].y + translator.y, vertexes[i].z + translator.z); //Rotate it Vertex = Matrix.Rotate(rotation, Vertex); //Project it var point = Matrix.Project(Vertex); //Add it to calc2DPoints calc2DPoints[i] = point; } }
//Matrix projektion public static PointF Project(PointF3D vertex) { float[,] cameraM = { { 1, 0, 0 }, { 0, 1, 0 } }; float[,] rotationM = { { 1, 0, 0 }, { 0, 1, 0 } }; float zee = 1f; float[,] projectionM = { { zee, 0, 0 }, { 0, zee, 0 } }; return(Matrix.Multiply2D(projectionM, vertex)); }
public static PointF Multiply2D(float[,] a, PointF3D b) { return(ToPoint(Multiply(a, ToMatrix(b)))); }
public static void Log(PointF3D p) { Log(ToMatrix(p)); }
//Constructor - With rotation public Model(PointF3D[] vertexes, PointF3D rotation) { this.vertexes = vertexes; this.rotation = rotation; Setup(); }