private void firstCircle(Vector3 v1, Vector3 v2, SceneBrep scene, Matrix4 m) { Vector3 dif = (v2 - v1) * size; Vector3 rdif = (v2 - v1) * (1 - size); last_circle = createCircleAround(v1, v2); float s = radius / curve.current_count; last_circle = Transforms.scale(last_circle, s, s, s); connect_indices = new int[cdetail]; Vector3 center = getCenter(last_circle); for (int i = 0; i < cdetail; i++) { connect_indices[i] = scene.AddVertex(Vector3.TransformPosition(last_circle[i] + v1, m)); scene.SetNormal(connect_indices[i], Vector3.TransformVector(last_circle[i] - center, m).Normalized()); float r = (last_circle[i].X + v1.X + 1.1f) / 2.5f; float g = (last_circle[i].Y + v1.Y + 1.1f) / 2.5f; float b = (last_circle[i].Z + v1.Z + 1.1f) / 2.5f; scene.SetColor(connect_indices[i], new Vector3(r, g, b)); } for (int i = 0; i < cdetail; i++) { last_circle[i] += v1 + rdif; } }
private int lastCircle(Vector3 v1, Vector3 v2, SceneBrep scene, Matrix4 m) { _ = createCircleAround(v1, v2); Vector3 center = getCenter(last_circle); Vector3 dif = (v2 - v1) * size; _ = (v2 - v1) * (1 - size); int[] v = new int[cdetail]; for (int i = 0; i < cdetail; i++) { v[i] = scene.AddVertex(Vector3.TransformPosition(last_circle[i] + dif, m)); scene.SetNormal(v[i], Vector3.TransformVector(last_circle[i] - center, m).Normalized()); float r = (last_circle[i].X + 1.1f) / 2.5f; float g = (last_circle[i].Y + 1.1f) / 2.5f; float b = (last_circle[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]); return(cdetail * 2); }
private int drawRose(SceneBrep scene, Matrix4 m, string param) { int number_of_points = segments; double period = Math.PI * d; if ((n % 2 == 0) || (d % 2 == 0)) { period *= 2; } double s = 0.0; double ds = 1.0 / number_of_points; double dtheta = period / number_of_points; List <int> points = new List <int>(); double k = (double)n / d; for (int i = 0; i < number_of_points; i++) { double theta = i * dtheta; double r = scale * Math.Cos(k * theta); float x = (float)(r * Math.Cos(theta * ratioX)); float y = (float)(r * Math.Sin(theta * ratioY)); float z = (float)(r * Math.Cos(theta * i * ratioZ)); if (noRatioZ) { z = (float)(r * Math.Cos(theta)); } if (noRatioX) { x = (float)(r * Math.Cos(theta)); } if (noRatioY) { y = (float)(r * Math.Sin(theta)); } Vector3 A = new Vector3(x, y, z); int v = scene.AddVertex(Vector3.TransformPosition(A, m)); points.Add(v); scene.SetTxtCoord(v, new Vector2((float)s, (float)s)); System.Drawing.Color c = Raster.Draw.ColorRamp(0.5 * (s + 1.0)); scene.SetColor(v, new Vector3(c.R / 255.0f, c.G / 255.0f, c.B / 255.0f)); s += ds; } for (int i = 0; i < number_of_points - 1; i++) { scene.AddLine(points[i], points[i + 1]); } scene.AddLine(points[0], points[points.Count - 1]); return(number_of_points); }
/// <summary> /// Construct a new Brep solid (preferebaly closed = regular one). /// </summary> /// <param name="scene">B-rep scene to be modified</param> /// <param name="m">Transform matrix (object-space to world-space)</param> /// <param name="param">Shape parameters if needed</param> /// <returns>Number of generated faces (0 in case of failure)</returns> public int AddMesh(SceneBrep scene, Matrix4 m, string param) { // {{ TODO: put your Mesh-construction code here parseParams(param); // If there will be large number of new vertices, reserve space for them to save time. scene.Reserve(segments + 1); double t = 0.0; double dt = maxT / segments; double s = 0.0; // for both texture coordinate & color ramp double ds = 1.0 / segments; int vPrev = 0; Vector3 A; for (int i = 0; i <= segments; i++) { // New vertex's coordinates. A.X = (float)(radius * Math.Cos(kx * t + dx)); A.Y = (float)(radius * Math.Cos(ky * t + dy)); A.Z = (float)(radius * Math.Cos(kz * t + dz)); // New vertex. int v = scene.AddVertex(Vector3.TransformPosition(A, m)); // Vertex attributes. scene.SetTxtCoord(v, new Vector2((float)s, (float)s)); System.Drawing.Color c = Raster.Draw.ColorRamp(0.5 * (s + 1.0)); scene.SetColor(v, new Vector3(c.R / 255.0f, c.G / 255.0f, c.B / 255.0f)); // New line? if (i > 0) { scene.AddLine(vPrev, v); } // Next vertex. t += dt; s += ds; vPrev = v; } // Thick line (for rendering). scene.LineWidth = 3.0f; return(segments); // }} }
private int drawOctahedron(SceneBrep scene, Matrix4 m, string param, float sc, Vector3 color, double s, double theta) { int one, two, three, four, five, six; float localScale = (float)sc; Vector4 row1 = new Vector4((float)Math.Cos(theta), (float)-Math.Sin(theta), 0, 0); Vector4 row2 = new Vector4((float)Math.Sin(theta), (float)Math.Cos(theta), 0, 0); Vector4 row3 = new Vector4(0, 0, 1, 0); Vector4 row4 = new Vector4(0, 0, 0, 1); m = new Matrix4(row1, row2, row3, row4); Vector3 A = new Vector3(localScale, 0, 0); one = scene.AddVertex(Vector3.TransformPosition(A, m)); A = new Vector3(0, localScale, 0); two = scene.AddVertex(Vector3.TransformPosition(A, m)); A = new Vector3(-localScale, 0, 0); three = scene.AddVertex(Vector3.TransformPosition(A, m)); A = new Vector3(0, -localScale, 0); four = scene.AddVertex(Vector3.TransformPosition(A, m)); A = new Vector3(0, 0, localScale); five = scene.AddVertex(Vector3.TransformPosition(A, m)); A = new Vector3(0, 0, -localScale); six = scene.AddVertex(Vector3.TransformPosition(A, m)); scene.SetTxtCoord(one, new Vector2((float)s, (float)s)); scene.SetTxtCoord(two, new Vector2((float)s, (float)s)); scene.SetTxtCoord(three, new Vector2((float)s, (float)s)); scene.SetTxtCoord(four, new Vector2((float)s, (float)s)); scene.SetTxtCoord(five, new Vector2((float)s, (float)s)); scene.SetTxtCoord(six, new Vector2((float)s, (float)s)); scene.SetColor(one, color); scene.SetColor(two, color); scene.SetColor(three, color); scene.SetColor(four, color); scene.SetColor(five, color); scene.SetColor(six, color); scene.AddLine(one, two); scene.AddLine(two, three); scene.AddLine(three, four); scene.AddLine(four, one); scene.AddLine(five, one); scene.AddLine(five, two); scene.AddLine(five, three); scene.AddLine(five, four); scene.AddLine(six, one); scene.AddLine(six, two); scene.AddLine(six, three); scene.AddLine(six, four); return(6); }
private int drawSpiral(SceneBrep scene, Matrix4 m, string param) { int number_of_points = segments; double period = Math.PI * scale; double dtheta = period / number_of_points; List <int> points = new List <int>(); double ds = 1.0 / (double)number_of_points; double s = 0.0; for (int i = 0; i < number_of_points; i++) { double theta = i * dtheta; float x = (float)(s * s * Math.Cos(theta * ratioX)); float y = (float)(s * s * Math.Sin(theta * ratioY)); float z = (float)(s * s); Vector3 A = new Vector3(x, y, z); int v = scene.AddVertex(Vector3.TransformPosition(A, m)); scene.SetTxtCoord(v, new Vector2((float)s, (float)s)); System.Drawing.Color c = Raster.Draw.ColorRamp(0.5 * (s + 1.0)); scene.SetColor(v, new Vector3(c.R / 255.0f, c.G / 255.0f, c.B / 255.0f)); points.Add(v); s += ds; } for (int i = 0; i < number_of_points - 1; i++) { scene.AddLine(points[i], points[i + 1]); } //scene.AddLine(points[0], points[points.Count - 1]); return(number_of_points); }
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); }
public int ReadBrep(string fileName, SceneBrep scene) { Debug.Assert(scene != null); this.scene = scene; scene.Reset(); if (fileName == null || fileName.Length == 0) { throw new IOException("Invalid file name"); } if (fileName.EndsWith(".gz")) { headerReader = new StreamReader(new GZipStream(new FileStream(fileName, FileMode.Open), CompressionMode.Decompress)); } else { var fs = new FileStream(fileName, FileMode.Open); headerReader = new StreamReader(fs); } // prepare buffers for data filling if (!ParseHeader()) { return(-1); } // read vertices var vertexReader = GetReader("vertex"); var element = vertexReader.ReadElement(); List <Vector2> txtCoords = new List <Vector2>(256); int[] f = new int[3]; int v0 = scene.Vertices; int lastVertex = v0 - 1; while (element != null) { lastVertex = scene.AddVertex(Vector3.TransformPosition(element.GetVertex(), matrix)); if (DoNormals) { Vector3.TransformNormal(element.GetNormal(), matrix); scene.SetNormal(lastVertex, element.GetNormal()); } if (DoTxtCoords) { scene.SetTxtCoord(lastVertex, element.GetTextureCoordinate()); } if (DoColors) { scene.SetColor(lastVertex, element.GetVertexColor()); } element = vertexReader.ReadElement(); } // read triangles var faceReader = GetReader("face"); element = faceReader.ReadElement(); while (element != null) { int A, B, C; element.GetTriangleVertices(out A, out B, out C); scene.AddTriangle(A, B, C); element = faceReader.ReadElement(); } headerReader.Close(); vertexReader.Close(); faceReader.Close(); return(scene.Triangles); }
/// <summary> /// Construct a new Brep solid (preferebaly closed = regular one). /// </summary> /// <param name="scene">B-rep scene to be modified</param> /// <param name="m">Transform matrix (object-space to world-space)</param> /// <param name="param">Shape parameters if needed</param> /// <returns>Number of generated faces (0 in case of failure)</returns> public int AddMesh(SceneBrep scene, Matrix4 m, string param) { parseParams(param); scene.LineWidth = lineWidth; // create shape // create vertices in 3D local space Vector3[] shapeVerticesLocal = new Vector3[] { new Vector3(0, 1, depth), new Vector3(0.866025f, 0.5f, depth), new Vector3(0.866025f, -0.5f, depth), new Vector3(0, -1, depth), new Vector3(-0.866025f, -0.5f, depth), new Vector3(-0.866025f, 0.5f, depth), new Vector3(0, 0, depth), new Vector3(0, 1, -depth), new Vector3(0.866025f, 0.5f, -depth), new Vector3(0.866025f, -0.5f, -depth), new Vector3(0, -1, -depth), new Vector3(-0.866025f, -0.5f, -depth), new Vector3(-0.866025f, 0.5f, -depth), new Vector3(0, 0, -depth), }; // push vertices to scene and get theirs indexes int[] shapeVerticesIndex = new int[shapeVerticesLocal.Length]; for (int i = 0; i < shapeVerticesLocal.Length; i++) { int vertexIndex = scene.AddVertex(Vector3.TransformPosition(shapeVerticesLocal[i], m)); shapeVerticesIndex[i] = vertexIndex; scene.SetColor(vertexIndex, new Vector3(0, 0, 0)); } Vector3[] edgeColors = new Vector3[] { new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 0) }; // { vertex, vertex, color } List <int[, ]> shapeEdges = new List <int[, ]>(); shapeEdges.Add(new int[2, 3] { { 0, 1, 0 }, { 8, 9, 0 } }); shapeEdges.Add(new int[2, 3] { { 1, 2, 1 }, { 9, 13, 1 } }); shapeEdges.Add(new int[2, 3] { { 2, 6, 1 }, { 10, 9, 1 } }); shapeEdges.Add(new int[2, 3] { { 2, 3, 0 }, { 10, 11, 0 } }); shapeEdges.Add(new int[2, 3] { { 3, 4, 1 }, { 11, 13, 1 } }); shapeEdges.Add(new int[2, 3] { { 4, 6, 1 }, { 12, 11, 1 } }); shapeEdges.Add(new int[2, 3] { { 4, 5, 0 }, { 12, 7, 0 } }); shapeEdges.Add(new int[2, 3] { { 5, 0, 1 }, { 7, 13, 1 } }); shapeEdges.Add(new int[2, 3] { { 0, 6, 1 }, { 8, 7, 1 } }); shapeEdges.Add(new int[2, 3] { { 6, 0, 2 }, { 9, 13, 2 } }); shapeEdges.Add(new int[2, 3] { { 6, 2, 2 }, { 11, 13, 2 } }); shapeEdges.Add(new int[2, 3] { { 6, 4, 2 }, { 7, 13, 2 } }); shapeEdges.Add(new int[2, 3] { { 0, 1, 3 }, { 7, 8, 3 } }); shapeEdges.Add(new int[2, 3] { { 1, 2, 3 }, { 8, 9, 3 } }); shapeEdges.Add(new int[2, 3] { { 2, 3, 3 }, { 9, 10, 3 } }); shapeEdges.Add(new int[2, 3] { { 3, 4, 3 }, { 10, 11, 3 } }); shapeEdges.Add(new int[2, 3] { { 4, 5, 3 }, { 11, 12, 3 } }); shapeEdges.Add(new int[2, 3] { { 5, 0, 3 }, { 12, 7, 3 } }); // push edges to scene foreach (int[,] group in shapeEdges) { scene.AddLine(shapeVerticesIndex[group[0, 0]], shapeVerticesIndex[group[0, 1]]); scene.AddLine(shapeVerticesIndex[group[1, 0]], shapeVerticesIndex[group[1, 1]]); } //push faces to scene foreach (int[,] group in shapeEdges) { for (int i = 0; i < splits; i++) { Vector3 beginDiff = shapeVerticesLocal[group[0, 0]] - shapeVerticesLocal[group[0, 1]]; beginDiff /= splits; Vector3 begin = shapeVerticesLocal[group[0, 0]] - beginDiff * i; Vector3 endDiff = shapeVerticesLocal[group[1, 0]] - shapeVerticesLocal[group[1, 1]]; endDiff /= splits; Vector3 end = shapeVerticesLocal[group[1, 0]] - endDiff * i; int beginIndex = scene.AddVertex(Vector3.TransformPosition(begin, m)); scene.SetColor(beginIndex, edgeColors[group[0, 2]]); int endIndex = scene.AddVertex(Vector3.TransformPosition(end, m)); scene.SetColor(endIndex, edgeColors[group[1, 2]]); scene.AddLine(beginIndex, endIndex); } } return(1); // some magic number }