├── .DS_Store ├── src ├── .DS_Store ├── MeshGraphLib │ ├── .DS_Store │ ├── Algorithms │ │ ├── CatmullClark.cs │ │ ├── Match │ │ │ ├── Selection │ │ │ │ ├── AngleSelection.cs │ │ │ │ ├── IMatchSelection.cs │ │ │ │ └── EdgeLengthSelection.cs │ │ │ └── BFSMatching.cs │ │ ├── Walk │ │ │ ├── Interfaces │ │ │ │ └── IWalk.cs │ │ │ └── WalkBFS.cs │ │ ├── LaplacianSmooth.cs │ │ └── Quadrangulation.cs │ ├── Core │ │ ├── Face.cs │ │ ├── Edge.cs │ │ ├── XYZ.cs │ │ ├── Helper │ │ │ ├── SpatialHelper.cs │ │ │ └── ConversionHelper.cs │ │ └── Graph.cs │ └── MeshGraphLib.csproj ├── MeshQuadrangulation │ ├── .DS_Store │ ├── Properties │ │ └── launchSettings.json │ ├── MeshQuadrangulationInfo.cs │ ├── MeshQuadrangulation.csproj │ ├── MeshQuadrangulation.sln │ └── Component │ │ ├── LaplacianSmooth.cs │ │ └── QuadrangulateMesh.cs └── MeshQuadrangulation.sln ├── example └── example.gh ├── img ├── planar_mesh.png ├── planar_mesh_crop.png └── quadrangulation2.gif ├── LICENSE ├── README.md └── .gitignore /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelhi/mesh-quadrangulation-gh/HEAD/.DS_Store -------------------------------------------------------------------------------- /src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelhi/mesh-quadrangulation-gh/HEAD/src/.DS_Store -------------------------------------------------------------------------------- /example/example.gh: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelhi/mesh-quadrangulation-gh/HEAD/example/example.gh -------------------------------------------------------------------------------- /img/planar_mesh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelhi/mesh-quadrangulation-gh/HEAD/img/planar_mesh.png -------------------------------------------------------------------------------- /img/planar_mesh_crop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelhi/mesh-quadrangulation-gh/HEAD/img/planar_mesh_crop.png -------------------------------------------------------------------------------- /img/quadrangulation2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelhi/mesh-quadrangulation-gh/HEAD/img/quadrangulation2.gif -------------------------------------------------------------------------------- /src/MeshGraphLib/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelhi/mesh-quadrangulation-gh/HEAD/src/MeshGraphLib/.DS_Store -------------------------------------------------------------------------------- /src/MeshQuadrangulation/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joelhi/mesh-quadrangulation-gh/HEAD/src/MeshQuadrangulation/.DS_Store -------------------------------------------------------------------------------- /src/MeshGraphLib/Algorithms/CatmullClark.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace MeshGraphLib.Algorithms 3 | { 4 | public class CatmullClark 5 | { 6 | public CatmullClark() 7 | { 8 | } 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Algorithms/Match/Selection/AngleSelection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace MeshGraphLib.Algorithms.Match.Selection 3 | { 4 | public class AngleSelection 5 | { 6 | public AngleSelection() 7 | { 8 | } 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /src/MeshQuadrangulation/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "MeshQuadrangulation": { 4 | "commandName": "Executable", 5 | "executablePath": "C:\\Program Files\\Rhino 7\\System\\Rhino.exe", 6 | "commandLineArgs": "" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Algorithms/Walk/Interfaces/IWalk.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using MeshGraphLib.Core; 4 | 5 | namespace MeshGraphLib.Algorithms.Walk.Interfaces 6 | { 7 | public interface IWalk 8 | { 9 | List Walk(IEnumerable start_indices); 10 | } 11 | } 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Algorithms/Match/Selection/IMatchSelection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using MeshGraphLib.Core; 4 | 5 | namespace MeshGraphLib.Algorithms.Match.Selection 6 | { 7 | public interface IMatchSelection 8 | { 9 | iEdge PickMatching(IEnumerable edges, GraphXYZ graph, out int[] remaining_nodes); 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Core/Face.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace MeshGraphLib.Core 3 | { 4 | 5 | 6 | public struct iFace 7 | { 8 | public int A; 9 | public int B; 10 | public int C; 11 | public int D; 12 | 13 | public iFace(int A, int B, int C) 14 | { 15 | this.A = A; 16 | this.B = B; 17 | this.C = C; 18 | this.D = C; 19 | } 20 | 21 | public iFace(int A, int B, int C, int D) 22 | { 23 | this.A = A; 24 | this.B = B; 25 | this.C = C; 26 | this.D = D; 27 | } 28 | 29 | public bool IsTriangle => (this.D == this.C); 30 | 31 | public static iFace Unset => new iFace(-1, -1, -1); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Core/Edge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace MeshGraphLib.Core 3 | { 4 | public struct iEdge 5 | { 6 | public int id_a; 7 | public int id_b; 8 | 9 | public iEdge() 10 | { 11 | id_a = id_b = -1; 12 | } 13 | 14 | public iEdge(int id_a, int id_b) 15 | { 16 | this.id_a = id_a; 17 | this.id_b = id_b; 18 | } 19 | } 20 | 21 | public struct EdgeXYZ 22 | { 23 | XYZ xyz_a; 24 | XYZ xyz_b; 25 | 26 | public XYZ A => this.xyz_a; 27 | 28 | public XYZ B => this.xyz_b; 29 | 30 | public EdgeXYZ() 31 | { 32 | xyz_a = xyz_b = new XYZ(); 33 | } 34 | 35 | public EdgeXYZ(XYZ xyz_a, XYZ xyz_b) 36 | { 37 | this.xyz_a = xyz_a; 38 | this.xyz_b = xyz_b; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/MeshGraphLib/MeshGraphLib.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | true 9 | 11.0 10 | 11 | 12 | true 13 | 11.0 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/MeshQuadrangulation/MeshQuadrangulationInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Drawing; 3 | using Grasshopper; 4 | using Grasshopper.Kernel; 5 | 6 | using System; 7 | 8 | using MeshGraphLib; 9 | 10 | 11 | namespace MeshQuadrangulation 12 | { 13 | public class MeshQuadrangulationInfo : GH_AssemblyInfo 14 | { 15 | public override string Name => "MeshQuadrangulation"; 16 | 17 | //Return a 24x24 pixel bitmap to represent this GHA library. 18 | public override Bitmap Icon => null; 19 | 20 | //Return a short string describing the purpose of this GHA library. 21 | public override string Description => ""; 22 | 23 | public override Guid Id => new Guid("629d027d-b877-4761-a666-31df20689368"); 24 | 25 | //Return a string identifying you or your company. 26 | public override string AuthorName => ""; 27 | 28 | //Return a string representing your preferred contact details. 29 | public override string AuthorContact => ""; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Joel Hilmersson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/MeshQuadrangulation/MeshQuadrangulation.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net48 5 | 1.0 6 | MeshQuadrangulation 7 | Description of MeshQuadrangulation 8 | .gha 9 | 10 | 11 | 12 | true 13 | 11.0 14 | 15 | 16 | true 17 | 11.0 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/MeshQuadrangulation/MeshQuadrangulation.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 25.0.1705.5 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MeshQuadrangulation", "MeshQuadrangulation.csproj", "{4C15150A-2D5A-4BD2-9166-E9EE054AF2EC}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {4C15150A-2D5A-4BD2-9166-E9EE054AF2EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {4C15150A-2D5A-4BD2-9166-E9EE054AF2EC}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {4C15150A-2D5A-4BD2-9166-E9EE054AF2EC}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {4C15150A-2D5A-4BD2-9166-E9EE054AF2EC}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {1EF89413-C55E-47D4-8F20-EDC0C406F1DA} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Algorithms/Walk/WalkBFS.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Collections.Generic; 4 | using MeshGraphLib.Core; 5 | using MeshGraphLib.Algorithms.Walk.Interfaces; 6 | 7 | namespace MeshGraphLib.Algorithms.Walk 8 | { 9 | public class WalkBFS : IWalk 10 | { 11 | private GraphXYZ graph; 12 | 13 | public WalkBFS(GraphXYZ graph) 14 | { 15 | this.graph = graph; 16 | } 17 | 18 | public List Walk(IEnumerable sources) 19 | { 20 | Queue to_search = new Queue(); 21 | HashSet visited_nodes = new HashSet(sources); 22 | List edges = new List(); 23 | 24 | foreach (int id in sources) { to_search.Enqueue(id); } 25 | 26 | while (to_search.Count > 0) 27 | { 28 | int current = to_search.Dequeue(); 29 | visited_nodes.Add(current); 30 | 31 | foreach (int id in this.graph.GetConnectedNodes(current)) 32 | { 33 | if (visited_nodes.Contains(id)) { continue; } 34 | 35 | visited_nodes.Add(id); 36 | edges.Add(new iEdge(current, id)); 37 | to_search.Enqueue(id); 38 | } 39 | } 40 | 41 | return edges; 42 | 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Algorithms/Match/Selection/EdgeLengthSelection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Grasshopper.Kernel.Geometry.Delaunay; 5 | using Grasshopper.Kernel.Graphs; 6 | using MeshGraphLib.Core; 7 | using MeshGraphLib.Core.Helper; 8 | 9 | namespace MeshGraphLib.Algorithms.Match.Selection 10 | { 11 | public class EdgeLengthSelection : IMatchSelection 12 | { 13 | public EdgeLengthSelection() 14 | { 15 | } 16 | 17 | public iEdge PickMatching(IEnumerable edges, GraphXYZ graph, out int[] remaining_nodes) 18 | { 19 | double minDistance = double.MaxValue; 20 | iEdge selected_edge = new iEdge(-1, -1); 21 | 22 | foreach (iEdge e in edges) 23 | { 24 | double len = graph.GetNode(e.id_a).DistanceTo(graph.GetNode(e.id_b)); 25 | 26 | if (len < minDistance) 27 | { 28 | selected_edge = e; 29 | minDistance = len; 30 | } 31 | } 32 | 33 | HashSet remaining = graph.GetConnectedNodes(selected_edge.id_b); 34 | 35 | foreach (var e in edges) { remaining.Add(e.id_b); } 36 | 37 | remaining.Remove(selected_edge.id_a); 38 | remaining.Remove(selected_edge.id_b); 39 | remaining_nodes = remaining.ToArray(); 40 | 41 | return selected_edge; 42 | } 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Core/XYZ.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using MeshGraphLib.Core.Helper; 3 | 4 | namespace MeshGraphLib.Core 5 | { 6 | public struct XYZ 7 | { 8 | public double x; 9 | 10 | public double y; 11 | 12 | public double z; 13 | 14 | public XYZ() 15 | { 16 | this.x = this.y = this.z = 0; 17 | } 18 | 19 | public XYZ(double x, double y) 20 | { 21 | this.x = x; 22 | this.y = y; 23 | this.z = 0; 24 | } 25 | 26 | public XYZ(double x, double y, double z) 27 | { 28 | this.x = x; 29 | this.y = y; 30 | this.z = z; 31 | } 32 | 33 | public XYZ INVALID => new XYZ(double.NaN, double.NaN, double.NaN); 34 | 35 | public bool IsValid => !(double.IsNaN(this.x) || double.IsNaN(this.y) || double.IsNaN(this.z)); 36 | 37 | public int SpatialHash => Spatial.ComputeSpatialHash(this.x, this.y, this.z); 38 | 39 | public static XYZ operator +(XYZ a, XYZ b) => new XYZ(a.x + b.x, a.y + b.y, a.z + b.z); 40 | 41 | public static XYZ operator -(XYZ a, XYZ b) => new XYZ(a.x - b.x, a.y - b.y, a.z - b.z); 42 | 43 | public static double operator *(XYZ a, XYZ b) => a.x * b.x + a.y * b.y + a.z * b.z; 44 | 45 | public static XYZ operator *(XYZ xyz, double val) => new XYZ(xyz.x * val, xyz.y * val, xyz.z * val); 46 | 47 | public static XYZ operator /(XYZ xyz, double val) => new XYZ(xyz.x / val, xyz.y / val, xyz.z / val); 48 | 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /src/MeshQuadrangulation.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 25.0.1705.5 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MeshQuadrangulation", "MeshQuadrangulation\MeshQuadrangulation.csproj", "{4A86B317-4F52-4727-B783-01E3DA6FA5BD}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MeshGraphLib", "MeshGraphLib\MeshGraphLib.csproj", "{34008592-5693-4494-90E2-F9362F088201}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {4A86B317-4F52-4727-B783-01E3DA6FA5BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {4A86B317-4F52-4727-B783-01E3DA6FA5BD}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {4A86B317-4F52-4727-B783-01E3DA6FA5BD}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {4A86B317-4F52-4727-B783-01E3DA6FA5BD}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {34008592-5693-4494-90E2-F9362F088201}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {34008592-5693-4494-90E2-F9362F088201}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {34008592-5693-4494-90E2-F9362F088201}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {34008592-5693-4494-90E2-F9362F088201}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {1A4F1911-BD91-41FC-B1E8-13E5424DFEA5} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Algorithms/LaplacianSmooth.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using MeshGraphLib.Core; 4 | namespace MeshGraphLib.Algorithms 5 | { 6 | public class LaplacianSmooth 7 | { 8 | private HashSet fixed_v; 9 | private GraphXYZ vertex_graph; 10 | public LaplacianSmooth(GraphXYZ vertex_graph, IEnumerable fixed_points) 11 | { 12 | this.vertex_graph = vertex_graph; 13 | fixed_v = new HashSet(); 14 | 15 | foreach (XYZ p in fixed_points) { fixed_v.Add(vertex_graph.NodeIndex(p)); } 16 | } 17 | 18 | public GraphXYZ Smooth(int iterations) 19 | { 20 | XYZ[] smooth_nodes = new XYZ[vertex_graph.NodeCount]; 21 | 22 | XYZ[] nodes = vertex_graph.GetNodes(); 23 | 24 | for (int iter = 0; iter < iterations; iter++) 25 | { 26 | for (int i = 0; i < vertex_graph.NodeCount; i++) 27 | { 28 | if (fixed_v.Contains(i)) 29 | { 30 | smooth_nodes[i] = nodes[i]; 31 | continue; 32 | } 33 | 34 | XYZ avg = new XYZ(0, 0, 0); 35 | foreach (int id in vertex_graph.GetConnectedNodes(i)){ avg += nodes[id]; } 36 | 37 | smooth_nodes[i] = avg / vertex_graph.GetConnectedNodes(i).Count; 38 | } 39 | 40 | Array.Copy(smooth_nodes, nodes, smooth_nodes.Length); 41 | } 42 | 43 | 44 | GraphXYZ smooth_graph = new GraphXYZ(); 45 | smooth_graph.TryAddNodes(smooth_nodes); 46 | 47 | foreach (iEdge e in vertex_graph.GetEdgesConnectivity()) { smooth_graph.TryAddEdge(e.id_a, e.id_b); } 48 | 49 | return smooth_graph; 50 | } 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Algorithms/Match/BFSMatching.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using MeshGraphLib.Core; 5 | using MeshGraphLib.Algorithms.Match.Selection; 6 | 7 | namespace MeshGraphLib.Algorithms.Match 8 | { 9 | public class BFSMatching 10 | { 11 | private GraphXYZ graph; 12 | 13 | private IMatchSelection selection_criteria; 14 | 15 | public BFSMatching(GraphXYZ graph, IMatchSelection selection_criteria) 16 | { 17 | this.graph = graph; 18 | this.selection_criteria = selection_criteria; 19 | } 20 | 21 | public List ComputeMatchings(IEnumerable sources, out List singles) 22 | { 23 | Queue to_search = new Queue(); 24 | HashSet visited_nodes = new HashSet(); 25 | List edges = new List(); 26 | singles = new List(); 27 | 28 | foreach (int id in sources) { to_search.Enqueue(id); } 29 | 30 | while (to_search.Count > 0) 31 | { 32 | int current = to_search.Dequeue(); 33 | if (!visited_nodes.Add(current)) { continue; } 34 | 35 | List node_edges = new List(); 36 | 37 | foreach (int id in this.graph.GetConnectedNodes(current)) 38 | { 39 | if (visited_nodes.Contains(id)) { continue; } 40 | 41 | node_edges.Add(new iEdge(current, id)); 42 | } 43 | 44 | if (node_edges.Count == 0) 45 | { 46 | singles.Add(current); 47 | continue; 48 | } 49 | 50 | iEdge selected = selection_criteria.PickMatching(node_edges, graph, out int[] remaining); 51 | 52 | visited_nodes.Add(selected.id_b); 53 | edges.Add(selected); 54 | 55 | for (int i = 0; i < remaining.Length; i++) { to_search.Enqueue(remaining[i]); } 56 | } 57 | 58 | 59 | return edges; 60 | } 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Core/Helper/SpatialHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | namespace MeshGraphLib.Core.Helper 4 | { 5 | 6 | public static class Spatial 7 | { 8 | 9 | public static void SetGlobalTol(double val) 10 | { 11 | global_spatial_tolerance = val; 12 | } 13 | 14 | public static double GetGlobalTol() => global_spatial_tolerance; 15 | 16 | private static double global_spatial_tolerance = 1e-7; 17 | 18 | public static int ComputeSpatialHash(double x, double y, double z) 19 | { 20 | double multiplier = 1 / global_spatial_tolerance; 21 | int s_hash = 23; 22 | 23 | s_hash = s_hash * 37 + (int)(x * multiplier); 24 | s_hash = s_hash * 37 + (int)(y * multiplier); 25 | s_hash = s_hash * 37 + (int)(z * multiplier); 26 | 27 | return s_hash; 28 | } 29 | 30 | public static List GetUniqueNodes(IEnumerable edges) 31 | { 32 | HashSet hashes = new HashSet(); 33 | List unique_nodes = new List(); 34 | 35 | foreach (EdgeXYZ edge in edges) 36 | { 37 | if (hashes.Add(edge.A.SpatialHash)) { unique_nodes.Add(edge.A); } 38 | 39 | if (hashes.Add(edge.B.SpatialHash)) { unique_nodes.Add(edge.B); } 40 | } 41 | 42 | return unique_nodes; 43 | } 44 | 45 | public static int ComputeIndexHash(int id_a, int id_b) 46 | { 47 | if (id_a >= id_b) 48 | { 49 | return (23 * 37 + id_a) * 37 + id_b; 50 | } 51 | else 52 | { 53 | return (23 * 37 + id_b) * 37 + id_a; 54 | } 55 | } 56 | 57 | public static int ComputeIndexHash(int[] ids) 58 | { 59 | Array.Sort(ids); 60 | 61 | int hash = 23; 62 | for (int i = 0; i < ids.Length; i++) 63 | { 64 | hash = hash * 37 + ids[i]; 65 | } 66 | 67 | return hash; 68 | } 69 | 70 | public static double Length(this XYZ xyz) => Math.Sqrt(Math.Pow(xyz.x, 2) + Math.Pow(xyz.y, 2) + Math.Pow(xyz.z, 2)); 71 | 72 | public static double Length(this EdgeXYZ edge) => DistanceTo(edge.A, edge.B); 73 | 74 | public static double DistanceTo(this XYZ a, XYZ b) => (b - a).Length(); 75 | } 76 | 77 | } -------------------------------------------------------------------------------- /src/MeshQuadrangulation/Component/LaplacianSmooth.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | using Grasshopper; 6 | using Grasshopper.Kernel; 7 | using Rhino.Geometry; 8 | 9 | using MeshGraphLib.Core.Helper; 10 | using MeshGraphLib.Core; 11 | using MeshGraphLib.Algorithms.Walk; 12 | using MeshGraphLib.Algorithms.Walk.Interfaces; 13 | using MeshGraphLib.Algorithms.Match; 14 | using MeshGraphLib.Algorithms.Match.Selection; 15 | using MeshGraphLib.Algorithms; 16 | 17 | namespace MeshQuadrangulation 18 | { 19 | public class LaplacianSmoothComponent : GH_Component 20 | { 21 | 22 | public LaplacianSmoothComponent() 23 | : base("Laplacian Smooth", "m_lpsmth", 24 | "Smooth mesh using a laplacian", 25 | "Mesh", "Quadrangulation") 26 | { 27 | } 28 | 29 | public override Guid ComponentGuid => new Guid("3824e604-a528-4cee-aea9-7be1fa3db6fd"); 30 | 31 | protected override void RegisterInputParams(GH_InputParamManager pManager) 32 | { 33 | pManager.AddMeshParameter("Mesh","M","Mesh to smooth",GH_ParamAccess.item); 34 | pManager.AddPointParameter("Fixed Vertices","fix","Vertices to fix during the smoothing process",GH_ParamAccess.list); 35 | pManager.AddIntegerParameter("Iterations","iter","Number of smoothing iterations",GH_ParamAccess.item, 1); 36 | 37 | pManager[1].Optional = true; 38 | } 39 | 40 | protected override void RegisterOutputParams(GH_OutputParamManager pManager) 41 | { 42 | pManager.AddMeshParameter("Mesh","M","Smooth mesh",GH_ParamAccess.item); 43 | } 44 | 45 | protected override void SolveInstance(IGH_DataAccess DA) 46 | { 47 | Mesh m = new Mesh(); 48 | List fix = new List(); 49 | int iter = 1; 50 | 51 | DA.GetData(0, ref m); 52 | DA.GetDataList(1, fix); 53 | DA.GetData(2, ref iter); 54 | 55 | GraphXYZ v_graph = m.ToVertexGraph(); 56 | 57 | LaplacianSmooth smoother = new LaplacianSmooth(v_graph, fix.Select(p => p.ToXYZ())); 58 | 59 | GraphXYZ smooth_graph = smoother.Smooth(iter); 60 | 61 | Mesh smooth_m = new Mesh(); 62 | 63 | smooth_m.Vertices.AddVertices(smooth_graph.GetNodes().ToRhino()); 64 | smooth_m.Faces.AddFaces(m.Faces); 65 | 66 | smooth_m.Normals.ComputeNormals(); 67 | 68 | DA.SetData(0, smooth_m); 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mesh Quadrangulation 2 | 3 | ### Overview 4 | 5 | > **Warning** 6 | > 7 | > This repo is still very much work in progress. 8 | 9 | A set of grasshopper components to quadrangulate tri-meshes by merging faces using a graph-matching approach. 10 | 11 | This stems from the need to get quad based meshes for FE-analysis. Many good tools exist to do smooth adaptable triangulations in gh but few 12 | offer "sofistik" style FE-suitable meshes. This can be done through the process of *Triangulation -> Quadrangulation -> Smoothing*. 13 | 14 | The process works as follows. Given a quality triangulated mesh (in this case provided using the remeshing in [this toolkit](https://github.com/joelhi/g3-gh)), the quadrangulation works by constructing a graph for the face connectivity, and walking this graph according to a *breadth-first* or *depth-first* search, finding a set of [**matchings**](https://en.wikipedia.org/wiki/Matching_(graph_theory)), pairs of triangles that can be merged into quads. 15 | 16 | An example of the process is shown below. 17 | 18 | ![Example](img/quadrangulation2.gif) 19 | 20 | To generate a quadrangulated FE mesh, create a triangulation, and do a quadrangulation, followed by a smooth. Make sure to fix the nodes used for supports or edges in the smoothing process. An example file can be found in the *example* folder which shows the following geometry. 21 | 22 | ![Example_planar](img/planar_mesh_crop.png) 23 | 24 | Left is the triangular mesh, middle the quadrangulated and right the smooth quad-mesh. The red points are pinned during the process. 25 | 26 | This is quite work in progress still, and may be extended to feature more graph based mesh processing algorithms in the future; beyond what's needed for quadrangulation. 27 | 28 | ### Contents 29 | 30 | The repo has two projects. 31 | 32 | - **MeshGraphLib** 33 | - **MeshQuadrangulationGH** 34 | 35 | The first one features a graph data structure and the processing algorithms, along with some conversion helpers to and from Rhino geometry. 36 | 37 | The second is the gh-plugin, which for now only has two component: *Quadrangulate Meshes* and *Laplacian Smooth* 38 | 39 | 40 | ### Todo 41 | 42 | - [x] Base graph structure 43 | - [x] Quadrangulation algorithm 44 | - [x] Laplacian smoothing (with option to fix points) 45 | - [ ] Handle loops (faces) in graph structure 46 | - [ ] Catmull-Clark algorithm (with option to fix points) for smoothing 47 | - [ ] Handle non-convex matching cases. 48 | - [ ] Expose explicit steps in process as gh components 49 | - [ ] Implement [Blossom algorithm](https://en.wikipedia.org/wiki/Blossom_algorithm) for computing matchings. 50 | - [ ] Grasshopper Icons 51 | - [ ] Make algorithms etc. more modular 52 | -------------------------------------------------------------------------------- /src/MeshQuadrangulation/Component/QuadrangulateMesh.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | using Grasshopper; 6 | using Grasshopper.Kernel; 7 | using Rhino.Geometry; 8 | 9 | using MeshGraphLib.Core.Helper; 10 | using MeshGraphLib.Core; 11 | using MeshGraphLib.Algorithms.Walk; 12 | using MeshGraphLib.Algorithms.Walk.Interfaces; 13 | using MeshGraphLib.Algorithms.Match; 14 | using MeshGraphLib.Algorithms.Match.Selection; 15 | using MeshGraphLib.Algorithms; 16 | 17 | namespace MeshQuadrangulation 18 | { 19 | public class QuadrangulateMesh : GH_Component 20 | { 21 | /// 22 | /// Each implementation of GH_Component must provide a public 23 | /// constructor without any arguments. 24 | /// Category represents the Tab in which the component will appear, 25 | /// Subcategory the panel. If you use non-existing tab or panel names, 26 | /// new tabs/panels will automatically be created. 27 | /// 28 | public QuadrangulateMesh() 29 | : base("Mesh Quadrangulation", "m_quadr", 30 | "Quadrangulate a tri mesh using a matching algorithm", 31 | "Mesh", "Quadrangulation") 32 | { 33 | } 34 | 35 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) 36 | { 37 | pManager.AddMeshParameter("Mesh","M","Mesh to quadrangulate",GH_ParamAccess.item); 38 | pManager.AddIntegerParameter("Sources","S","Sources for the search.",GH_ParamAccess.list); 39 | 40 | pManager[1].Optional = true; 41 | } 42 | 43 | protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) 44 | { 45 | pManager.AddMeshParameter("Mesh","M","Quadrangulated Mesh",GH_ParamAccess.item); 46 | } 47 | 48 | protected override void SolveInstance(IGH_DataAccess DA) 49 | { 50 | Mesh m = new Mesh(); 51 | List sources = new List(); 52 | 53 | DA.GetData(0, ref m); 54 | if(!DA.GetDataList(1, sources)){ sources.Add(0);} 55 | 56 | GraphXYZ f_graph = m.ToFaceGraph(); 57 | iFace[] faces = m.ToFaces(); 58 | 59 | Quadrangulation q = new Quadrangulation(f_graph, faces, new EdgeLengthSelection()); 60 | 61 | iFace[] q_faces = q.Quadrangulate(sources); 62 | 63 | Mesh q_m = new Mesh(); 64 | 65 | q_m.Vertices.AddVertices(m.Vertices); 66 | q_m.Faces.AddFaces(q_faces.ToRhino()); 67 | 68 | q_m.Normals.ComputeNormals(); 69 | 70 | DA.SetData(0, q_m); 71 | 72 | } 73 | 74 | protected override System.Drawing.Bitmap Icon => null; 75 | 76 | public override Guid ComponentGuid => new Guid("3823e604-a322-4cee-aea9-7ee1fa3bb6fd"); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Algorithms/Quadrangulation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using MeshGraphLib.Core; 4 | using MeshGraphLib.Algorithms.Match; 5 | using MeshGraphLib.Algorithms.Match.Selection; 6 | using Rhino.Geometry; 7 | 8 | namespace MeshGraphLib.Algorithms 9 | { 10 | public class Quadrangulation 11 | { 12 | private BFSMatching matching; 13 | 14 | private iFace[] faces; 15 | 16 | public Quadrangulation(GraphXYZ face_graph, iFace[] faces, IMatchSelection selection_criteria) 17 | { 18 | this.matching = new BFSMatching(face_graph, selection_criteria); 19 | this.faces = faces; 20 | } 21 | 22 | public iFace[] Quadrangulate(IEnumerable sources) 23 | { 24 | var matchings = matching.ComputeMatchings(sources, out List singles); 25 | return MergeAllFaces(this.faces, matchings, singles); 26 | } 27 | 28 | public iFace[] MergeAllFaces(iFace[] faces, List matchings, List singles) 29 | { 30 | iFace[] merged = new iFace[matchings.Count + singles.Count]; 31 | 32 | for (int i = 0; i < matchings.Count; i++) 33 | { 34 | merged[i] = MergeTrianglesToQuads(faces[matchings[i].id_a], faces[matchings[i].id_b]); 35 | } 36 | 37 | for (int i = 0; i < singles.Count; i++) 38 | { 39 | merged[matchings.Count + i] = faces[singles[i]]; 40 | } 41 | 42 | return merged; 43 | } 44 | 45 | public iFace MergeTrianglesToQuads(iFace A, iFace B) 46 | { 47 | 48 | iFace final = iFace.Unset; 49 | 50 | if (A.IsTriangle && B.IsTriangle) 51 | { 52 | int[] a_arr = new int[3] { A.A, A.B, A.C }; 53 | int[] b_arr = new int[3] { B.A, B.B, B.C }; 54 | 55 | List shared = new List(); 56 | HashSet shared_index_A = new HashSet(); 57 | HashSet sharedIndexB = new HashSet(); 58 | 59 | for (int i = 0; i < 3; i++) 60 | { 61 | int index = -1; 62 | 63 | for (int j = 0; j < 3; j++) 64 | { 65 | if (b_arr[j] == a_arr[i]) { index = j; } 66 | } 67 | 68 | if (index >= 0) 69 | { 70 | shared_index_A.Add(i); sharedIndexB.Add(index); shared.Add(a_arr[i]); 71 | } 72 | } 73 | 74 | int not_shared = -1; 75 | for (int i = 0; i < 3; i++) 76 | { 77 | if (!sharedIndexB.Contains(i)) { not_shared = b_arr[i]; } 78 | } 79 | 80 | if (shared_index_A.Contains(0) && shared_index_A.Contains(1)) 81 | { 82 | final.A = a_arr[0]; final.B = not_shared; final.C = a_arr[1]; final.D = a_arr[2]; 83 | } 84 | else if (shared_index_A.Contains(0)) 85 | { 86 | final.A = a_arr[0]; final.B = a_arr[1]; final.C = a_arr[2]; final.D = not_shared; 87 | } 88 | else 89 | { 90 | final.A = a_arr[0]; final.B = a_arr[1]; final.C = not_shared; final.D = a_arr[2]; 91 | } 92 | 93 | } 94 | else { throw new Exception("This can only be done for triangles."); } 95 | 96 | return final; 97 | } 98 | } 99 | } -------------------------------------------------------------------------------- /src/MeshGraphLib/Core/Graph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Rhino.Geometry; 4 | 5 | namespace MeshGraphLib.Core 6 | { 7 | public class GraphXYZ 8 | { 9 | private List nodes_xyz { get; set; } 10 | 11 | private Dictionary nodes_map { get; set; } 12 | 13 | private List> nodes_conn { get; set; } 14 | 15 | public bool IsDirected { get; private set; } 16 | 17 | public GraphXYZ(bool directed = false) 18 | { 19 | nodes_xyz = new List(); 20 | nodes_map = new Dictionary(); 21 | nodes_conn = new List>(); 22 | 23 | this.IsDirected = directed; 24 | } 25 | 26 | public bool HasNode(XYZ node) => nodes_map.ContainsKey(node.SpatialHash); 27 | 28 | public int NodeIndex(XYZ node) => nodes_map[node.SpatialHash]; 29 | 30 | public bool HasEdge(EdgeXYZ edge) => HasEdge(edge.A, edge.B); 31 | 32 | public bool HasEdge(XYZ node_a, XYZ node_b) => HasEdge(NodeIndex(node_a), NodeIndex(node_b)); 33 | 34 | public bool HasEdge(int id_a, int id_b) 35 | { 36 | if (id_a >= nodes_xyz.Count || id_b >= nodes_xyz.Count) { return false; } 37 | 38 | if (nodes_conn[id_a].Contains(id_b)) { return true; } 39 | 40 | if (IsDirected) { return false; } 41 | 42 | return nodes_conn[id_b].Contains(id_a); 43 | } 44 | 45 | public HashSet GetConnectedNodes(int id) => nodes_conn[id]; 46 | 47 | public int NodeCount => nodes_xyz.Count; 48 | 49 | public List TryAddNodes(IEnumerable nodes) 50 | { 51 | List failed = new List(); 52 | 53 | foreach (XYZ node in nodes) 54 | { 55 | if (!TryAddNode(node)) 56 | { 57 | failed.Add(node); 58 | } 59 | } 60 | 61 | return failed; 62 | } 63 | 64 | public bool TryAddNode(XYZ node) 65 | { 66 | if (HasNode(node)) 67 | { 68 | return false; 69 | } 70 | 71 | nodes_map.Add(node.SpatialHash, nodes_xyz.Count); 72 | nodes_xyz.Add(node); 73 | nodes_conn.Add(new HashSet()); 74 | 75 | return true; 76 | } 77 | 78 | public bool TryAddEdge(EdgeXYZ edge, bool add_nodes = false) => TryAddEdge(edge.A, edge.B, add_nodes); 79 | 80 | public bool TryAddEdge(XYZ node_a, XYZ node_b, bool add_nodes = false) 81 | { 82 | if (add_nodes) 83 | { 84 | TryAddNode(node_a); 85 | TryAddNode(node_b); 86 | } 87 | 88 | return TryAddEdge(NodeIndex(node_a), NodeIndex(node_b), add_nodes); 89 | } 90 | 91 | public bool TryAddEdge(int id_a, int id_b, bool add_nodes = false) 92 | { 93 | if (HasEdge(id_a, id_b)) { return false; } 94 | 95 | if (id_a >= nodes_xyz.Count || id_b >= nodes_xyz.Count) { return false; } 96 | 97 | nodes_conn[id_a].Add(id_b); 98 | 99 | if (IsDirected) { return true; } 100 | 101 | nodes_conn[id_b].Add(id_a); 102 | 103 | return true; 104 | } 105 | 106 | public EdgeXYZ[] GetEdges() 107 | { 108 | List edges = new List(); 109 | 110 | HashSet visited_nodes = new HashSet(); 111 | 112 | for (int i = 0; i < nodes_xyz.Count; i++) 113 | { 114 | visited_nodes.Add(i); 115 | 116 | foreach (int id in nodes_conn[i]) 117 | { 118 | if (visited_nodes.Contains(id)) { continue; } 119 | 120 | edges.Add(new EdgeXYZ(nodes_xyz[i], nodes_xyz[id])); 121 | } 122 | } 123 | 124 | return edges.ToArray(); 125 | } 126 | 127 | public iEdge[] GetEdgesConnectivity() 128 | { 129 | List edges = new List(); 130 | 131 | HashSet visited_nodes = new HashSet(); 132 | 133 | for (int i = 0; i < nodes_xyz.Count; i++) 134 | { 135 | visited_nodes.Add(i); 136 | 137 | foreach (int id in nodes_conn[i]) 138 | { 139 | if (visited_nodes.Contains(id)) { continue; } 140 | 141 | edges.Add(new iEdge(i, id)); 142 | } 143 | } 144 | 145 | return edges.ToArray(); 146 | } 147 | 148 | public XYZ[] GetNodes() => nodes_xyz.ToArray(); 149 | 150 | public XYZ GetNode(int id) => nodes_xyz[id]; 151 | 152 | public bool TryGetNode(int id, out XYZ node) 153 | { 154 | if (id >= nodes_xyz.Count) 155 | { 156 | node = new XYZ(0, 0, 0); 157 | return false; 158 | } 159 | 160 | node = nodes_xyz[id]; 161 | 162 | return true; 163 | } 164 | } 165 | } 166 | 167 | -------------------------------------------------------------------------------- /src/MeshGraphLib/Core/Helper/ConversionHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Rhino.Geometry; 4 | using System.Linq; 5 | using System.ComponentModel; 6 | using System.Runtime.CompilerServices; 7 | 8 | namespace MeshGraphLib.Core.Helper 9 | { 10 | public static class Conversions 11 | { 12 | 13 | public static GraphXYZ ToVertexGraph(this Mesh mesh) 14 | { 15 | 16 | // Mesh cleanup 17 | mesh.Compact(); 18 | mesh.Vertices.CombineIdentical(true, true); 19 | mesh.Vertices.CullUnused(); 20 | 21 | GraphXYZ graph = new GraphXYZ(); 22 | 23 | graph.TryAddNodes(mesh.Vertices.ToPoint3dArray().ToXYZ()); 24 | 25 | for (int i = 0; i < mesh.Vertices.Count; i++) 26 | { 27 | int[] connected = mesh.Vertices.GetConnectedVertices(i); 28 | 29 | for (int j = 0; j < connected.Length; j++) { graph.TryAddEdge(i, connected[j]); } 30 | } 31 | 32 | return graph; 33 | } 34 | 35 | 36 | 37 | public static GraphXYZ ToFaceGraph(this Mesh mesh) 38 | { 39 | // Mesh cleanup 40 | mesh.Compact(); 41 | mesh.Vertices.CombineIdentical(true, true); 42 | mesh.Vertices.CullUnused(); 43 | 44 | GraphXYZ graph = new GraphXYZ(); 45 | 46 | // Add face center nodess to graph 47 | graph.TryAddNodes( 48 | mesh.Faces.Select( 49 | face => GetFaceCenter(face, mesh))); 50 | 51 | if (mesh.Faces.Count != graph.NodeCount) 52 | { 53 | throw new Exception("Falied to add all faces to graph. Please double check tolerance."); 54 | } 55 | 56 | // Add edges for adjacent faces. 57 | for (int i = 0; i < mesh.Faces.Count; i++) 58 | { 59 | int[] adjacent_faces = mesh.Faces.AdjacentFaces(i); 60 | 61 | for (int j = 0; j < adjacent_faces.Length; j++) 62 | { 63 | graph.TryAddEdge(i, adjacent_faces[j]); 64 | } 65 | } 66 | 67 | return graph; 68 | } 69 | 70 | 71 | private static XYZ GetFaceCenter(MeshFace face, Mesh mesh) 72 | { 73 | if (face.IsTriangle) 74 | { 75 | return (mesh.Vertices[face[0]] + mesh.Vertices[face[1]] + mesh.Vertices[face[2]]).ToXYZ() / 3; 76 | } 77 | 78 | return (mesh.Vertices[face[0]] + mesh.Vertices[face[1]] + mesh.Vertices[face[2]] + mesh.Vertices[face[3]]).ToXYZ() / 3; 79 | } 80 | 81 | public static iFace[] ToFaces(this Mesh mesh) 82 | { 83 | MeshFace[] rh_faces = mesh.Faces.ToArray(); 84 | 85 | iFace[] faces = new iFace[rh_faces.Length]; 86 | 87 | unsafe 88 | { 89 | int size = faces.Length * sizeof(MeshFace); 90 | 91 | fixed (void* f_ptr = &faces[0]) 92 | { 93 | fixed (void* r_ptr = &rh_faces[0]) 94 | { 95 | Buffer.MemoryCopy(r_ptr, f_ptr, size, size); 96 | } 97 | } 98 | } 99 | 100 | return faces; 101 | } 102 | 103 | public static MeshFace[] ToRhino(this iFace[] faces) 104 | { 105 | 106 | MeshFace[] rh_faces = new MeshFace[faces.Length]; 107 | 108 | unsafe 109 | { 110 | int size = faces.Length * sizeof(MeshFace); 111 | 112 | fixed (void* f_ptr = &faces[0]) 113 | { 114 | fixed (void* r_ptr = &rh_faces[0]) 115 | { 116 | Buffer.MemoryCopy(f_ptr, r_ptr, size, size); 117 | } 118 | } 119 | } 120 | 121 | return rh_faces; 122 | } 123 | 124 | public static Point3d ToRhino(this XYZ node) => new Point3d(node.x, node.y, node.z); 125 | 126 | public static Point3d[] ToRhino(this XYZ[] nodes) 127 | { 128 | Point3d[] rh_pts = new Point3d[nodes.Length]; 129 | 130 | unsafe 131 | { 132 | int size = nodes.Length * sizeof(XYZ); 133 | 134 | fixed (void* nd_ptr = &nodes[0]) 135 | { 136 | fixed (void* pt_ptr = &rh_pts[0]) 137 | { 138 | Buffer.MemoryCopy(nd_ptr, pt_ptr, size, size); 139 | } 140 | } 141 | } 142 | 143 | return rh_pts; 144 | } 145 | 146 | public static XYZ ToXYZ(this Point3d pt) => new XYZ(pt.X, pt.Y, pt.Z); 147 | 148 | public static XYZ ToXYZ(this Point3f pt) => new XYZ(pt.X, pt.Y, pt.Z); 149 | 150 | public static XYZ[] ToXYZ(this Point3d[] rh_pts) 151 | { 152 | XYZ[] nodes = new XYZ[rh_pts.Length]; 153 | 154 | unsafe 155 | { 156 | int size = nodes.Length * sizeof(XYZ); 157 | 158 | fixed (void* nd_ptr = &nodes[0]) 159 | { 160 | fixed (void* pt_ptr = &rh_pts[0]) 161 | { 162 | Buffer.MemoryCopy(pt_ptr, nd_ptr, size, size); 163 | } 164 | } 165 | } 166 | 167 | return nodes; 168 | } 169 | 170 | public static EdgeXYZ[] ToEdgeXYZ(this Line[] rh_lines) 171 | { 172 | EdgeXYZ[] edges = new EdgeXYZ[rh_lines.Length]; 173 | 174 | unsafe 175 | { 176 | int size = edges.Length * sizeof(EdgeXYZ); 177 | 178 | fixed (void* nd_ptr = &edges[0]) 179 | { 180 | fixed (void* pt_ptr = &rh_lines[0]) 181 | { 182 | Buffer.MemoryCopy(pt_ptr, nd_ptr, size, size); 183 | } 184 | } 185 | } 186 | 187 | return edges; 188 | } 189 | 190 | public static Line[] ToRhino(this EdgeXYZ[] edges) 191 | { 192 | Line[] rh_lns = new Line[edges.Length]; 193 | 194 | unsafe 195 | { 196 | int size = edges.Length * sizeof(Line); 197 | 198 | fixed (void* e_ptr = &edges[0]) 199 | { 200 | fixed (void* ln_ptr = &rh_lns[0]) 201 | { 202 | Buffer.MemoryCopy(e_ptr, ln_ptr, size, size); 203 | } 204 | } 205 | } 206 | 207 | return rh_lns; 208 | } 209 | } 210 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | .vscode/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.tlog 95 | *.vspscc 96 | *.vssscc 97 | .builds 98 | *.pidb 99 | *.svclog 100 | *.scc 101 | 102 | # Chutzpah Test files 103 | _Chutzpah* 104 | 105 | # Visual C++ cache files 106 | ipch/ 107 | *.aps 108 | *.ncb 109 | *.opendb 110 | *.opensdf 111 | *.sdf 112 | *.cachefile 113 | *.VC.db 114 | *.VC.VC.opendb 115 | 116 | # Visual Studio profiler 117 | *.psess 118 | *.vsp 119 | *.vspx 120 | *.sap 121 | 122 | # Visual Studio Trace Files 123 | *.e2e 124 | 125 | # TFS 2012 Local Workspace 126 | $tf/ 127 | 128 | # Guidance Automation Toolkit 129 | *.gpState 130 | 131 | # ReSharper is a .NET coding add-in 132 | _ReSharper*/ 133 | *.[Rr]e[Ss]harper 134 | *.DotSettings.user 135 | 136 | # TeamCity is a build add-in 137 | _TeamCity* 138 | 139 | # DotCover is a Code Coverage Tool 140 | *.dotCover 141 | 142 | # AxoCover is a Code Coverage Tool 143 | .axoCover/* 144 | !.axoCover/settings.json 145 | 146 | # Coverlet is a free, cross platform Code Coverage Tool 147 | coverage*.json 148 | coverage*.xml 149 | coverage*.info 150 | 151 | # Visual Studio code coverage results 152 | *.coverage 153 | *.coveragexml 154 | 155 | # NCrunch 156 | _NCrunch_* 157 | .*crunch*.local.xml 158 | nCrunchTemp_* 159 | 160 | # MightyMoose 161 | *.mm.* 162 | AutoTest.Net/ 163 | 164 | # Web workbench (sass) 165 | .sass-cache/ 166 | 167 | # Installshield output folder 168 | [Ee]xpress/ 169 | 170 | # DocProject is a documentation generator add-in 171 | DocProject/buildhelp/ 172 | DocProject/Help/*.HxT 173 | DocProject/Help/*.HxC 174 | DocProject/Help/*.hhc 175 | DocProject/Help/*.hhk 176 | DocProject/Help/*.hhp 177 | DocProject/Help/Html2 178 | DocProject/Help/html 179 | 180 | # Click-Once directory 181 | publish/ 182 | 183 | # Publish Web Output 184 | *.[Pp]ublish.xml 185 | *.azurePubxml 186 | # Note: Comment the next line if you want to checkin your web deploy settings, 187 | # but database connection strings (with potential passwords) will be unencrypted 188 | *.pubxml 189 | *.publishproj 190 | 191 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 192 | # checkin your Azure Web App publish settings, but sensitive information contained 193 | # in these scripts will be unencrypted 194 | PublishScripts/ 195 | 196 | # NuGet Packages 197 | *.nupkg 198 | # NuGet Symbol Packages 199 | *.snupkg 200 | # The packages folder can be ignored because of Package Restore 201 | **/[Pp]ackages/* 202 | # except build/, which is used as an MSBuild target. 203 | !**/[Pp]ackages/build/ 204 | # Uncomment if necessary however generally it will be regenerated when needed 205 | #!**/[Pp]ackages/repositories.config 206 | # NuGet v3's project.json files produces more ignorable files 207 | *.nuget.props 208 | *.nuget.targets 209 | 210 | # Microsoft Azure Build Output 211 | csx/ 212 | *.build.csdef 213 | 214 | # Microsoft Azure Emulator 215 | ecf/ 216 | rcf/ 217 | 218 | # Windows Store app package directories and files 219 | AppPackages/ 220 | BundleArtifacts/ 221 | Package.StoreAssociation.xml 222 | _pkginfo.txt 223 | *.appx 224 | *.appxbundle 225 | *.appxupload 226 | 227 | # Visual Studio cache files 228 | # files ending in .cache can be ignored 229 | *.[Cc]ache 230 | # but keep track of directories ending in .cache 231 | !?*.[Cc]ache/ 232 | 233 | # Others 234 | ClientBin/ 235 | ~$* 236 | *~ 237 | *.dbmdl 238 | *.dbproj.schemaview 239 | *.jfm 240 | *.pfx 241 | *.publishsettings 242 | orleans.codegen.cs 243 | 244 | # Including strong name files can present a security risk 245 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 246 | #*.snk 247 | 248 | # Since there are multiple workflows, uncomment next line to ignore bower_components 249 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 250 | #bower_components/ 251 | 252 | # RIA/Silverlight projects 253 | Generated_Code/ 254 | 255 | # Backup & report files from converting an old project file 256 | # to a newer Visual Studio version. Backup files are not needed, 257 | # because we have git ;-) 258 | _UpgradeReport_Files/ 259 | Backup*/ 260 | UpgradeLog*.XML 261 | UpgradeLog*.htm 262 | ServiceFabricBackup/ 263 | *.rptproj.bak 264 | 265 | # SQL Server files 266 | *.mdf 267 | *.ldf 268 | *.ndf 269 | 270 | # Business Intelligence projects 271 | *.rdl.data 272 | *.bim.layout 273 | *.bim_*.settings 274 | *.rptproj.rsuser 275 | *- [Bb]ackup.rdl 276 | *- [Bb]ackup ([0-9]).rdl 277 | *- [Bb]ackup ([0-9][0-9]).rdl 278 | 279 | # Microsoft Fakes 280 | FakesAssemblies/ 281 | 282 | # GhostDoc plugin setting file 283 | *.GhostDoc.xml 284 | 285 | # Node.js Tools for Visual Studio 286 | .ntvs_analysis.dat 287 | node_modules/ 288 | 289 | # Visual Studio 6 build log 290 | *.plg 291 | 292 | # Visual Studio 6 workspace options file 293 | *.opt 294 | 295 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 296 | *.vbw 297 | 298 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 299 | *.vbp 300 | 301 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 302 | *.dsw 303 | *.dsp 304 | 305 | # Visual Studio 6 technical files 306 | *.ncb 307 | *.aps 308 | 309 | # Visual Studio LightSwitch build output 310 | **/*.HTMLClient/GeneratedArtifacts 311 | **/*.DesktopClient/GeneratedArtifacts 312 | **/*.DesktopClient/ModelManifest.xml 313 | **/*.Server/GeneratedArtifacts 314 | **/*.Server/ModelManifest.xml 315 | _Pvt_Extensions 316 | 317 | # Paket dependency manager 318 | .paket/paket.exe 319 | paket-files/ 320 | 321 | # FAKE - F# Make 322 | .fake/ 323 | 324 | # CodeRush personal settings 325 | .cr/personal 326 | 327 | # Python Tools for Visual Studio (PTVS) 328 | __pycache__/ 329 | *.pyc 330 | 331 | # Cake - Uncomment if you are using it 332 | # tools/** 333 | # !tools/packages.config 334 | 335 | # Tabs Studio 336 | *.tss 337 | 338 | # Telerik's JustMock configuration file 339 | *.jmconfig 340 | 341 | # BizTalk build output 342 | *.btp.cs 343 | *.btm.cs 344 | *.odx.cs 345 | *.xsd.cs 346 | 347 | # OpenCover UI analysis results 348 | OpenCover/ 349 | 350 | # Azure Stream Analytics local run output 351 | ASALocalRun/ 352 | 353 | # MSBuild Binary and Structured Log 354 | *.binlog 355 | 356 | # NVidia Nsight GPU debugger configuration file 357 | *.nvuser 358 | 359 | # MFractors (Xamarin productivity tool) working folder 360 | .mfractor/ 361 | 362 | # Local History for Visual Studio 363 | .localhistory/ 364 | 365 | # Visual Studio History (VSHistory) files 366 | .vshistory/ 367 | 368 | # BeatPulse healthcheck temp database 369 | healthchecksdb 370 | 371 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 372 | MigrationBackup/ 373 | 374 | # Ionide (cross platform F# VS Code tools) working folder 375 | .ionide/ 376 | 377 | # Fody - auto-generated XML schema 378 | FodyWeavers.xsd 379 | 380 | # VS Code files for those working on multiple tools 381 | .vscode/* 382 | !.vscode/settings.json 383 | !.vscode/tasks.json 384 | !.vscode/launch.json 385 | !.vscode/extensions.json 386 | *.code-workspace 387 | 388 | # Local History for Visual Studio Code 389 | .history/ 390 | 391 | # Windows Installer files from build outputs 392 | *.cab 393 | *.msi 394 | *.msix 395 | *.msm 396 | *.msp 397 | 398 | # JetBrains Rider 399 | *.sln.iml 400 | --------------------------------------------------------------------------------