/// <summary> /// Construct a GridPlacementRule from closest points using a set of reference anchors. Each polyline vertex will be associated with its closest anchor. /// </summary> /// <param name="cell">The definition of the grid cell.</param> /// <param name="gridArea">The region in which to lay out the grid.</param> /// <param name="Anchors">The reference anchors from which to calculate the associations.</param> /// <param name="name">The name.</param> /// <param name="gridCreationRule">An optional function to apply to the grid to override its default subdivision.</param> public static GridPlacementRule FromClosestPoints(GridCellDefinition cell, Polygon gridArea, IList <Vector3> Anchors, string name, Action <Grid2d> gridCreationRule = null) { var anchorIndices = new List <int>(); var anchorDisplacements = new List <Vector3>(); foreach (var v in gridArea.Vertices) { var closestAnchorIndex = Enumerable.Range(0, Anchors.Count).OrderBy(a => Anchors[a].DistanceTo(v)).First(); anchorIndices.Add(closestAnchorIndex); var closestAnchor = Anchors[closestAnchorIndex]; anchorDisplacements.Add(v - closestAnchor); } return(new GridPlacementRule(cell, gridArea, anchorIndices, anchorDisplacements, name, gridCreationRule)); }
/// <summary> /// Construct a new grid placement rule from scratch. /// </summary> /// <param name="definition">The grid cell definition.</param> /// <param name="gridArea">The region in which to lay out the grid.</param> /// <param name="anchorIndices">For each vertex, the index of the corresponding anchor.</param> /// <param name="anchorDisplacements">For each vertex, the displacement from its anchor.</param> /// <param name="name">The name.</param> /// <param name="gridCreationRule">An optional action that handles subdividing the created Grid2d.</param> public GridPlacementRule(GridCellDefinition definition, Polygon gridArea, IList <int> anchorIndices, IList <Vector3> anchorDisplacements, string name, Action <Grid2d> gridCreationRule = null) { Curve = gridArea; AnchorIndices = anchorIndices; AnchorDisplacements = anchorDisplacements; Name = name; CellDefinition = definition; if (gridCreationRule == null) { GridCreationRule = (Grid2d g) => { g.U.DivideByFixedLength(CellDefinition.CellLength, FixedDivisionMode.RemainderAtBothEnds); g.V.DivideByFixedLength(CellDefinition.CellWidth, FixedDivisionMode.RemainderAtBothEnds); }; } else { GridCreationRule = gridCreationRule; } }