private byte[] GetVertexBytes(MeshVertexList vertices, out Point3d min, out Point3d max) { min = new Point3d(Double.PositiveInfinity, Double.PositiveInfinity, Double.PositiveInfinity); max = new Point3d(Double.NegativeInfinity, Double.NegativeInfinity, Double.NegativeInfinity); List <float> floats = new List <float>(vertices.Count * 3); foreach (Point3d vertex in vertices) { floats.AddRange(new float[] { (float)vertex.X, (float)vertex.Y, (float)vertex.Z }); min.X = Math.Min(min.X, vertex.X); max.X = Math.Max(max.X, vertex.X); min.Y = Math.Min(min.Y, vertex.Y); max.Y = Math.Max(max.Y, vertex.Y); min.Z = Math.Min(min.Z, vertex.Z); max.Z = Math.Max(max.Z, vertex.Z); } IEnumerable <byte> bytesEnumerable = floats.SelectMany(value => BitConverter.GetBytes(value)); return(bytesEnumerable.ToArray()); }
public Interpolate(Mesh mesh) { points = mesh.Vertices; InitAll(); MinMax(); //OneDim(); BiLinear(); }
public Interpolate(Mesh mesh, Vector3d _unitVector) { points = mesh.Vertices; InitAll(); MinMax(); OneDim(_unitVector); }
public void GetValleyFacePairBasicInfo(CMesh cm) { Mesh m = cm.mesh; List <Tuple <double, double> > face_heignt_pairs = new List <Tuple <double, double> >(); List <double> edge_length_between_face_pairs = new List <double>(); m.FaceNormals.ComputeFaceNormals(); MeshVertexList vert = m.Vertices; for (int e_ind = 0; e_ind < cm.valley_edges.Count; e_ind++) { // Register indices IndexPair edge_ind = cm.valley_edges[e_ind]; int u = edge_ind.I; int v = edge_ind.J; IndexPair face_ind = cm.valley_face_pairs[e_ind]; int P = face_ind.I; int Q = face_ind.J; MeshFace face_P = m.Faces[P]; MeshFace face_Q = m.Faces[Q]; int p = 0; int q = 0; for (int i = 0; i < 3; i++) { if (!edge_ind.Contains(face_P[i])) { p = face_P[i]; } if (!edge_ind.Contains(face_Q[i])) { q = face_Q[i]; } } /// Compute h_P & cot_Pu Vector3d vec_up = vert[p] - vert[u]; Vector3d vec_uv = vert[v] - vert[u]; double sin_Pu = (Vector3d.CrossProduct(vec_up, vec_uv) / (vec_up.Length * vec_uv.Length)).Length; double len_up = (vec_up - vec_uv).Length; double h_P = len_up * sin_Pu; /// Compute h_Q & cot_Qu Vector3d vec_uq = vert[q] - vert[u]; double sin_Qu = (Vector3d.CrossProduct(vec_uq, vec_uv) / (vec_uq.Length * vec_uv.Length)).Length; double len_uq = (vec_uq - vec_uv).Length; double h_Q = len_uq * sin_Qu; // Compute len_uv double len_uv = vec_uv.Length; // Set Tuple<h_P, h_Q> Tuple <double, double> face_height_pair = new Tuple <double, double>(h_P, h_Q); face_heignt_pairs.Add(face_height_pair); edge_length_between_face_pairs.Add(len_uv); } cm.valley_face_height_pairs = face_heignt_pairs; cm.length_of_valley_diagonal_edges = edge_length_between_face_pairs; }
private int GetVertexBuffer(MeshVertexList vertices, out Point3d min, out Point3d max, out int length) { byte[] bytes = GetVertexBytes(vertices, out min, out max); length = bytes.Length; glTFLoader.Schema.Buffer buffer = new glTFLoader.Schema.Buffer() { Uri = Constants.TextBufferHeader + Convert.ToBase64String(bytes), ByteLength = length, }; return(dummy.Buffers.AddAndReturnIndex(buffer)); }
public static void ParseRhinoVertices(MeshVertexList vertices, out double[] coord, out long[] nodeTags) { coord = new double[vertices.Count * 3]; nodeTags = new long[vertices.Count]; Point3d p; for (int i = 0; i < vertices.Count; i++) { p = vertices[i]; nodeTags[i] = i; coord[i * 3] = p.X; coord[i * 3 + 1] = p.Y; coord[i * 3 + 1] = p.Z; } }
private int GetVertexAccessor(MeshVertexList vertices) { int?vertexBufferViewIdx = GetVertexBufferView(vertices, out Point3d min, out Point3d max, out int countVertices); glTFLoader.Schema.Accessor vertexAccessor = new glTFLoader.Schema.Accessor() { BufferView = vertexBufferViewIdx, ComponentType = glTFLoader.Schema.Accessor.ComponentTypeEnum.FLOAT, Count = countVertices, Min = min.ToFloatArray(), Max = max.ToFloatArray(), Type = glTFLoader.Schema.Accessor.TypeEnum.VEC3, ByteOffset = 0, }; return(dummy.Accessors.AddAndReturnIndex(vertexAccessor)); }
public static Topology ToTopologic(this Mesh mesh) { if (mesh == null) { return(null); } MeshVertexList ghMeshVertices = mesh.Vertices; int ghMeshVertexCount = ghMeshVertices.Count; MeshFaceList ghMeshFaces = mesh.Faces; int ghMeshFaceCount = ghMeshFaces.Count; List <Vertex> vertices = new List <Vertex>(); for (int i = 0; i < ghMeshVertexCount; ++i) { Vertex vertex = ghMeshVertices[i].ToTopologic(); vertices.Add(vertex); } List <IList <int> > indices2D = new List <IList <int> >(); for (int i = 0; i < ghMeshFaceCount; ++i) { MeshFace ghMeshFace = ghMeshFaces[i]; List <int> indices1D = new List <int>(); indices1D.Add(ghMeshFace.A); indices1D.Add(ghMeshFace.B); indices1D.Add(ghMeshFace.C); if (ghMeshFace.IsQuad) { indices1D.Add(ghMeshFace.D); } indices1D.Add(ghMeshFace.A); indices2D.Add(indices1D); } IList <Topology> topologies = Topology.ByVerticesIndices(vertices, indices2D); Cluster cluster = Cluster.ByTopologies(topologies); Topology topology = cluster.SelfMerge(); return(topology); }
public override List <IfcBuildingElement> ToBuildingElementIfc(IfcStore model) { using (var transaction = model.BeginTransaction("Create Mesh Element")) { MeshFaceList faces = Mesh.Faces; MeshVertexList vertices = Mesh.Vertices; List <IfcCartesianPoint> ifcVertices = IfcTools.VerticesToIfcCartesianPoints(model, vertices); IfcFaceBasedSurfaceModel faceBasedSurfaceModel = IfcTools.CreateIfcFaceBasedSurfaceModel(model, faces, ifcVertices); var shape = IfcTools.CreateIfcShapeRepresentation(model, "Mesh"); shape.Items.Add(faceBasedSurfaceModel); var ifcRelAssociatesMaterial = IfcTools.CreateIfcRelAssociatesMaterial(model, Material.Name, Material.Grade); var buildingElements = IfcTools.CreateBuildingElements(model, ElementType, Name, shape, InsertPlanes, ifcRelAssociatesMaterial); transaction.Commit(); return(buildingElements); } }
/// <summary> /// Find the closest vertex to a given target point /// </summary> /// <param name="vertices"></param> /// <param name="target"></param> /// <param name="minDist"></param> /// <returns></returns> public static int ClosestVertex(this MeshVertexList vertices, Point3d target, out double minDist) { minDist = double.MaxValue; int iMin = -1; for (int i = 0; i < vertices.Count; i++) { Point3f v = vertices[i]; double dX = target.X - v.X; double dY = target.Y - v.Y; double dZ = target.Z - v.Z; double distSqd = dX * dX + dY * dY + dZ * dZ; if (iMin < 0 || distSqd < minDist) { iMin = i; minDist = distSqd; } } return(iMin); }
private Topology ByMesh(Mesh ghMesh) { MeshVertexList ghMeshVertices = ghMesh.Vertices; int ghMeshVertexCount = ghMeshVertices.Count; MeshFaceList ghMeshFaces = ghMesh.Faces; int ghMeshFaceCount = ghMeshFaces.Count; List <global::Topologic.Vertex> vertices = new List <global::Topologic.Vertex>(); for (int i = 0; i < ghMeshVertexCount; ++i) { Point3f ghPoint = ghMeshVertices[i]; Vertex vertex = ByPoint(ghPoint); vertices.Add(vertex); } List <List <int> > indices2D = new List <List <int> >(); for (int i = 0; i < ghMeshFaceCount; ++i) { MeshFace ghMeshFace = ghMeshFaces[i]; List <int> indices1D = new List <int>(); indices1D.Add(ghMeshFace.A); indices1D.Add(ghMeshFace.B); indices1D.Add(ghMeshFace.C); if (ghMeshFace.IsQuad) { indices1D.Add(ghMeshFace.D); } indices1D.Add(ghMeshFace.A); indices2D.Add(indices1D); } List <Topology> topologies = Topology.ByVerticesIndices(vertices, indices2D); Cluster cluster = Cluster.ByTopologies(topologies); Topology topology = cluster.SelfMerge(); return(topology); }
private int?GetVertexBufferView(MeshVertexList vertices, out Point3d min, out Point3d max, out int countVertices) { if (options.UseDracoCompression) { min = currentGeometryInfo.VerticesMin; max = currentGeometryInfo.VerticesMax; countVertices = currentGeometryInfo.VerticesCount; return(null); } int buffer = 0; int byteLength = 0; int byteOffset = 0; if (binary) { byte[] bytes = GetVertexBytes(vertices, out min, out max); buffer = 0; byteLength = bytes.Length; byteOffset = binaryBuffer.Count; binaryBuffer.AddRange(bytes); } else { buffer = GetVertexBuffer(vertices, out min, out max, out byteLength); } glTFLoader.Schema.BufferView vertexBufferView = new glTFLoader.Schema.BufferView() { Buffer = buffer, ByteOffset = byteOffset, ByteLength = byteLength, Target = glTFLoader.Schema.BufferView.TargetEnum.ARRAY_BUFFER, }; countVertices = vertices.Count; return(dummy.BufferViews.AddAndReturnIndex(vertexBufferView)); }
public Interpolate(Mesh mesh, Plane _plane) { points = mesh.Vertices; InitAll(); MinMax(); }
public override List <IfcReinforcingElement> ToReinforcingElementIfc(IfcStore model) { using (var transaction = model.BeginTransaction("Create Mesh Element")) { var rebars = new List <IfcReinforcingElement>(); switch (SpacingType) { case RebarSpacingType.NormalSpacing: { MeshFaceList faces = OriginRebarShape.RebarMesh.Faces; MeshVertexList vertices = OriginRebarShape.RebarMesh.Vertices; List <IfcCartesianPoint> ifcVertices = IfcTools.VerticesToIfcCartesianPoints(model, vertices); IfcFaceBasedSurfaceModel faceBasedSurfaceModel = IfcTools.CreateIfcFaceBasedSurfaceModel(model, faces, ifcVertices); var shape = IfcTools.CreateIfcShapeRepresentation(model, "Mesh"); shape.Items.Add(faceBasedSurfaceModel); var ifcRelAssociatesMaterial = IfcTools.CreateIfcRelAssociatesMaterial(model, Material.Name, Material.Grade); foreach (var insertPlane in RebarInsertPlanes) { var rebar = model.Instances.New <IfcReinforcingBar>(); rebar.Name = "Rebar"; rebar.NominalDiameter = Diameter; rebar.BarLength = (int)Math.Round(OriginRebarShape.RebarCurve.GetLength()); rebar.SteelGrade = Material.Grade; IfcTools.ApplyRepresentationAndPlacement(model, rebar, shape, insertPlane); ifcRelAssociatesMaterial.RelatedObjects.Add(rebar); rebars.Add(rebar); } break; } case RebarSpacingType.CustomSpacing: for (int i = 0; i < RebarGroupMesh.Count; i++) { MeshFaceList faces = RebarGroupMesh[i].Faces; MeshVertexList vertices = RebarGroupMesh[i].Vertices; List <IfcCartesianPoint> ifcVertices = IfcTools.VerticesToIfcCartesianPoints(model, vertices); IfcFaceBasedSurfaceModel faceBasedSurfaceModel = IfcTools.CreateIfcFaceBasedSurfaceModel(model, faces, ifcVertices); var shape = IfcTools.CreateIfcShapeRepresentation(model, "Mesh"); shape.Items.Add(faceBasedSurfaceModel); var ifcRelAssociatesMaterial = IfcTools.CreateIfcRelAssociatesMaterial(model, Material.Name, Material.Grade); var rebar = model.Instances.New <IfcReinforcingBar>(); rebar.Name = "Rebar"; rebar.NominalDiameter = Diameter; rebar.BarLength = (int)Math.Round(RebarGroupCurves[i].GetLength()); rebar.SteelGrade = Material.Grade; IfcTools.ApplyRepresentationAndPlacement(model, rebar, shape, Plane.WorldXY); ifcRelAssociatesMaterial.RelatedObjects.Add(rebar); rebars.Add(rebar); } break; default: throw new ArgumentException("Spacing type not recognized"); } transaction.Commit(); return(rebars); } }
public int StoreSerialized(Mesh mesh, string name) { MeshFaceList faces = mesh.Faces; MeshVertexList vertices = mesh.Vertices; MeshVertexNormalList normals = mesh.Normals; MeshTextureCoordinateList texCoords = mesh.TextureCoordinates; faces.ConvertQuadsToTriangles(); int vertexCount = vertices.Count; int triangleCount = faces.TriangleCount; Log("MeshStore[" + meshCount + "]: adding mesh with " + vertexCount + " vertices, " + triangleCount + " triangles" + (name != null && name.Length > 0 ? (" (\"" + name + "\")") : "")); meshDict.Add((ulong)output.Position); Serialize(output, MTS_FILEFORMAT_HEADER); Serialize(output, MTS_FILEFORMAT_VERSION_V4); Stream zStream = new ZlibStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, true); uint flags = MTS_SINGLE_PRECISION; if (texCoords.Count > 0) { flags |= MTS_HAS_TEXCOORDS; } if (normals.Count > 0) { flags |= MTS_HAS_NORMALS; } Serialize(zStream, flags); Serialize(zStream, (name == null) ? "" : name); Serialize(zStream, (ulong)vertexCount); Serialize(zStream, (ulong)triangleCount); for (int i = 0; i < vertexCount; ++i) { Point3f p = vertices[i]; Serialize(zStream, p.X); Serialize(zStream, p.Y); Serialize(zStream, p.Z); } if (normals.Count > 0) { for (int i = 0; i < vertexCount; ++i) { Vector3f n = normals[i]; Serialize(zStream, n.X); Serialize(zStream, n.Y); Serialize(zStream, n.Z); } } if (texCoords.Count > 0) { for (int i = 0; i < vertexCount; ++i) { Point2f uv = texCoords[i]; Serialize(zStream, uv.X); Serialize(zStream, uv.Y); } } for (int i = 0; i < triangleCount; ++i) { MeshFace face = faces[i]; if (!face.IsTriangle) { throw new Exception("Internal error: expected a triangle face!"); } Serialize(zStream, face.A); Serialize(zStream, face.B); Serialize(zStream, face.C); } zStream.Close(); return(meshCount++); }
public String StoreOBJ(Mesh mesh, string name) { MeshFaceList faces = mesh.Faces; MeshVertexList vertices = mesh.Vertices; MeshVertexNormalList normals = mesh.Normals; MeshTextureCoordinateList texCoords = mesh.TextureCoordinates; faces.ConvertQuadsToTriangles(); if (name == null || name.Length == 0) { name = "mesh" + meshCount.ToString(); } int vertexCount = vertices.Count; int triangleCount = faces.TriangleCount; Log("MeshStore[" + meshCount + "]: saving OBJ mesh with " + vertexCount + " vertices, " + triangleCount + " triangles" + "\"" + name + "\")"); //meshDict.Add((ulong)output.Position); //Serialize(output, MTS_FILEFORMAT_HEADER); //Serialize(output, MTS_FILEFORMAT_VERSION_V4); String filename = name + ".obj"; FileStream output = new FileStream(Path.Combine(basePath, filename), FileMode.Create); StreamWriter stream = new StreamWriter(output); stream.WriteLine("# Object " + name); stream.WriteLine("# Generated by the Mitsuba Rhino plugin."); stream.WriteLine("# Vertices: " + vertexCount.ToString()); stream.WriteLine("# Faces: " + triangleCount.ToString()); for (int i = 0; i < vertexCount; ++i) { Point3f p = vertices[i]; stream.WriteLine("v " + p.X.ToString() + " " + p.Y.ToString() + " " + p.Z.ToString()); } if (normals.Count > 0) { for (int i = 0; i < vertexCount; ++i) { Vector3f n = normals[i]; stream.WriteLine("vn " + n.X.ToString() + " " + n.Y.ToString() + " " + n.Z.ToString()); } } if (texCoords.Count > 0) { for (int i = 0; i < vertexCount; ++i) { Point2f uv = texCoords[i]; stream.WriteLine("vt " + uv.X.ToString() + " " + uv.Y.ToString()); } } for (int i = 0; i < triangleCount; ++i) { MeshFace face = faces[i]; if (!face.IsTriangle) { throw new Exception("Internal error: expected a triangle face!"); } String a = (face.A + 1).ToString(); String b = (face.B + 1).ToString(); String c = (face.C + 1).ToString(); if (texCoords.Count > 0) { stream.WriteLine("f " + a + "/" + a + "/" + a + " " + b + "/" + b + "/" + b + " " + c + "/" + c + "/" + c); } else { stream.WriteLine("f " + a + "//" + a + " " + b + "//" + b + " " + c + "//" + c); } } stream.Close(); meshCount++; return(filename); }
public Mesh() { Halfedges = new MeshHalfedgeList(this); Vertices = new MeshVertexList(this); Faces = new MeshFaceList(this); }
public static Mesh ConvertBrepToMesh(Brep brep, List <Curve> curves, List <Point3d> points, double meshSize, List <Parameters.GsaMember1d> mem1ds = null, List <Parameters.GsaNode> nodes = null) { Brep in_brep = brep.DuplicateBrep(); in_brep.Faces.ShrinkFaces(); // set up unroller Unroller unroller = new Unroller(in_brep); List <Curve> memcrvs = new List <Curve>(); if (mem1ds != null) { memcrvs = mem1ds.ConvertAll(x => (Curve)x.PolyCurve); unroller.AddFollowingGeometry(memcrvs); } List <Point3d> nodepts = new List <Point3d>(); if (nodes != null) { nodepts = nodes.ConvertAll(x => x.Point); unroller.AddFollowingGeometry(nodepts); } unroller.AddFollowingGeometry(points); unroller.AddFollowingGeometry(curves); unroller.RelativeTolerance = 10 ^ 32; //unroller.AbsoluteTolerance = 1000; // create list of flattened geometry Point3d[] inclPts; Curve[] inclCrvs; TextDot[] unused; // perform unroll Brep[] flattened = unroller.PerformUnroll(out inclCrvs, out inclPts, out unused); // create 2d member from flattened geometry Parameters.GsaMember2d mem = new Parameters.GsaMember2d(flattened[0], inclCrvs.ToList(), inclPts.ToList()); mem.Member.MeshSize = meshSize; // add to temp list for input in assemble function List <Parameters.GsaMember2d> mem2ds = new List <Parameters.GsaMember2d>(); mem2ds.Add(mem); if (mem1ds != null) { for (int i = 0; i < mem1ds.Count; i++) { Parameters.GsaMember1d mem1d = new Parameters.GsaMember1d(inclCrvs[i]); mem1d.Member.MeshSize = mem1ds[i].Member.MeshSize; mem1ds[i] = mem1d; } } if (nodes != null) { for (int i = 0; i < nodes.Count; i++) { nodes[i].Point = inclPts[i]; } } // assemble temp model Model model = Util.Gsa.ToGSA.Assemble.AssembleModel(null, mem2ds, mem1ds, nodes); // call the meshing algorithm model.CreateElementsFromMembers(); // extract elements from model Tuple <List <Parameters.GsaElement1dGoo>, List <Parameters.GsaElement2dGoo>, List <Parameters.GsaElement3dGoo> > elementTuple = Util.Gsa.FromGSA.GetElements(model.Elements(), model.Nodes(), model.Sections(), model.Prop2Ds()); List <Parameters.GsaElement2dGoo> elem2dgoo = elementTuple.Item2; Mesh mesh = elem2dgoo[0].Value.Mesh; Surface flat = flattened[0].Surfaces[0]; Surface orig = in_brep.Surfaces[0]; MeshVertexList vertices = mesh.Vertices; for (int i = 0; i < vertices.Count; i++) { flat.ClosestPoint(vertices.Point3dAt(i), out double u, out double v); Point3d mapVertex = orig.PointAt(u, v); vertices.SetVertex(i, mapVertex); } mesh.Faces.ConvertNonPlanarQuadsToTriangles(GhSA.Units.Tolerance, Rhino.RhinoMath.DefaultAngleTolerance, 0); return(mesh); }
public static List <IfcCartesianPoint> VerticesToIfcCartesianPoints(IfcStore model, MeshVertexList vertices) { List <IfcCartesianPoint> ifcCartesianPoints = new List <IfcCartesianPoint>(); foreach (var vertex in vertices) { IfcCartesianPoint currentVertex = model.Instances.New <IfcCartesianPoint>(); currentVertex.SetXYZ(vertex.X, vertex.Y, vertex.Z); ifcCartesianPoints.Add(currentVertex); } return(ifcCartesianPoints); }
public static List <Mesh> DistanceAnalysis(List <Mesh> Analysis, int _SocialDistanceRadius, int Codensity) { if (_SocialDistanceRadius < 1) { _SocialDistanceRadius = 1; } if (Codensity < 1) { Codensity = 1; } Plane plane = Plane.WorldXY; List <Circle> test = new List <Circle>(); foreach (Mesh swMesh in Analysis) { List <LineCurve> boundaryList = new List <LineCurve>(); List <Point3d> boundaryPoints = new List <Point3d>(); Polyline[] meshboundary = swMesh.GetOutlines(plane); MeshVertexList vertexList = swMesh.Vertices; foreach (Polyline meshPline in meshboundary) { PolylineCurve boundary = new PolylineCurve(meshPline); Line[] boundarysegments = meshPline.GetSegments(); boundaryList.AddRange(from Line segment in boundarysegments select new LineCurve(segment)); Point3d[] points; if (boundary.GetLength(1.0) <= 100) { boundary.DivideByCount(4, false, out points); boundaryPoints.AddRange(from Point3d point in points select point); } else { boundary.DivideByLength(100.0, false, out points); boundaryPoints.AddRange(from Point3d point in points select point); } } PointCloud pointCloud = new PointCloud(boundaryPoints); int width = 2; int testRadii = (_SocialDistanceRadius * Codensity) + 100; for (int vertex = 0; vertex < vertexList.Count; vertex++) { Point3d v = vertexList.Point3dAt(vertex); int cP = pointCloud.ClosestPoint(v); Point3d point = pointCloud[cP].Location; int rvalue = 255; int intersectionCount = 0; while (intersectionCount <= 3 && width < testRadii) { Curve walkwidth = (new Circle(point, width)).ToNurbsCurve(); for (int i = 0; i < boundaryList.Count; i++) { if (Curve.PlanarCurveCollision(walkwidth, boundaryList[i], plane, 0.1)) { intersectionCount++; } } width++; } rvalue = intersectionCount >= 3 && ((width / _SocialDistanceRadius) / Codensity) * 255 < 255 ? ((width / _SocialDistanceRadius) / Codensity) * 255 : intersectionCount > 2 && ((width / _SocialDistanceRadius) / Codensity) * 255 >= 255 ? 255 : 255; if (width < testRadii) { } else { rvalue = 0; } swMesh.VertexColors.SetColor(vertex, System.Drawing.Color.FromArgb(rvalue, 70, 100)); } } return(Analysis); }