private static void SetSeq([NotNull] ref string res, ref IntSequence seq) { if (res == string.Empty) { res = seq.GetSeq(); } else { res += ", " + seq.GetSeq(); } }
public static void test_points() { string filename = "c:\\scratch\\bunny_solid.obj"; DMesh3 mesh = StandardMeshReader.ReadMesh(filename); PointSplatsGenerator pointgen = new PointSplatsGenerator() { PointIndices = IntSequence.Range(mesh.VertexCount), PointF = mesh.GetVertex, NormalF = (vid) => { return((Vector3d)mesh.GetVertexNormal(vid)); }, Radius = mesh.CachedBounds.DiagonalLength * 0.01 }; DMesh3 pointMesh = pointgen.Generate().MakeDMesh(); StandardMeshWriter.WriteMesh("c:\\scratch\\POINTS.obj", pointMesh, WriteOptions.Defaults); }
/// <summary> /// Список чисел в строку, с групперовкой последовательных номеров /// ints = 1, 2, 3, 4, 5, 7, 8, 10, 15, 16, 100, 101, 102, 103, 105, 106, 107, 109 /// res = "1-8, 10, 15, 16, 100-107, 109" /// </summary> public static string IntsToStringSequence(int[] ints) { var uniqints = ints.Distinct(); string res = string.Empty; IntSequence seq = new IntSequence(uniqints.First()); foreach (var n in uniqints.Skip(1)) { if (!seq.AddInt(n)) { SetSeq(ref res, ref seq); seq = new IntSequence(n); } } if (!seq.IsNull()) { SetSeq(ref res, ref seq); } return res; }
static void Main(string[] args) { { IEnumerable <int> sequence = new IntSequence().Take(0); if (sequence.Count() == 0) { Console.WriteLine("Nums 0 count PASS"); } else { Console.WriteLine("Nums 0 count FAILED"); } } { IEnumerable <int> sequence = new IntSequence().Take(5); if (sequence.Count() == 5) { Console.WriteLine("Nums fixed count PASS"); } else { Console.WriteLine("Nums fixed count FAILED"); } } { IEnumerable <int> sequence = new IntSequence(5).Take(5).ToArray(); if (sequence.Last() == 9 && sequence.Count() == 5) { Console.WriteLine("Nums start point PASS"); } else { Console.WriteLine("Nums start point FAILED"); } } }
/// <summary> /// Список чисел в строку, с групперовкой последовательных номеров /// int's = 1, 2, 3, 4, 5, 7, 8, 10, 15, 16, 100, 101, 102, 103, 105, 106, 107, 109 /// res = "1-8, 10, 15, 16, 100-107, 109" /// </summary> public static string IntsToStringSequence(int[] ints) { var uniqints = ints.Distinct().ToList(); var res = string.Empty; var seq = new IntSequence(uniqints.First()); foreach (var n in uniqints.Skip(1)) { if (!seq.AddInt(n)) { SetSeq(ref res, ref seq); seq = new IntSequence(n); } } if (!seq.IsNull()) { SetSeq(ref res, ref seq); } return(res); }
/// <summary> /// Calculates the mapping matrix between fromElements and toElements. The mapping method /// is decided from the combination of methodDescription, fromElements.ElementType and /// toElements.ElementType. /// The valid values for methodDescription is obtained through use of the /// GetAvailableMethods method. /// </summary> /// /// <remarks> /// UpdateMappingMatrix is called during initialisation. UpdateMappingMatrix must be called prior /// to Mapvalues. /// </remarks> /// /// <param name="methodIdentifier">String identification of mapping method</param> /// <param name="fromElements">The IElementset to map from.</param> /// <param name="toElements">The IElementset to map to</param> /// /// <returns> /// The method has no return value. /// </returns> private void UpdateMappingMatrix(ref IIdentifiable methodIdentifier, ref IElementSet fromElements, ref IElementSet toElements) { try { ElementSetChecker.CheckElementSet(fromElements); ElementSetChecker.CheckElementSet(toElements); _method = SpatialAdaptedOutputFactory.GetMethod(methodIdentifier); _numberOfToRows = toElements.ElementCount; _numberOfFromColumns = fromElements.ElementCount; _mappingMatrix = new DoubleSparseMatrix(_numberOfToRows, _numberOfFromColumns); if (fromElements.ElementType == ElementType.Point && toElements.ElementType == ElementType.Point) { #region try { for (int i = 0; i < _numberOfToRows; i++) { XYPoint toPoint = CreateXYPoint(toElements, i); for (int j = 0; j < _numberOfFromColumns; j++) { XYPoint fromPoint = CreateXYPoint(fromElements, j); _mappingMatrix[i, j] = XYGeometryTools.CalculatePointToPointDistance(toPoint, fromPoint); } } if (_method == ElementMapperMethod.Nearest) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } else if (_method == ElementMapperMethod.Inverse) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } if (minDist == 0) { int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } else { double denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = 1 / _mappingMatrix[i, j]; denominator = denominator + _mappingMatrix[i, j]; } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } } else { throw new Exception("methodDescription unknown for point point mapping"); } } catch (Exception e) { throw new Exception("Point to point mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.Point && toElements.ElementType == ElementType.PolyLine) { #region try { for (int i = 0; i < _numberOfToRows; i++) { XYPolyline toPolyLine = CreateXYPolyline(toElements, i); for (int j = 0; j < _numberOfFromColumns; j++) { XYPoint fromPoint = CreateXYPoint(fromElements, j); _mappingMatrix[i, j] = XYGeometryTools.CalculatePolylineToPointDistance(toPolyLine, fromPoint); } } if (_method == ElementMapperMethod.Nearest) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } else if (_method == ElementMapperMethod.Inverse) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } if (minDist == 0) { int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } else { double denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = 1 / _mappingMatrix[i, j]; denominator = denominator + _mappingMatrix[i, j]; } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } } else { throw new Exception("methodDescription unknown for point to polyline mapping"); } } catch (Exception e) { throw new Exception("Point to polyline mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.Point && toElements.ElementType == ElementType.Polygon) { #region try { for (int i = 0; i < _numberOfToRows; i++) { XYPolygon polygon = CreateXYPolygon(toElements, i); int count = 0; XYPoint point; for (int n = 0; n < _numberOfFromColumns; n++) { point = CreateXYPoint(fromElements, n); if (XYGeometryTools.IsPointInPolygon(point, polygon)) { if (_method == ElementMapperMethod.Mean) { count = count + 1; } else if (_method == ElementMapperMethod.Sum) { count = 1; } else { throw new Exception( "methodDescription unknown for point to polygon mapping"); } } } for (int n = 0; n < _numberOfFromColumns; n++) { point = CreateXYPoint(fromElements, n); if (XYGeometryTools.IsPointInPolygon(point, polygon)) { _mappingMatrix[i, n] = 1.0 / count; } } } } catch (Exception e) { throw new Exception("Point to polygon mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.PolyLine && toElements.ElementType == ElementType.Point) { #region try { for (int i = 0; i < _numberOfToRows; i++) { XYPoint toPoint = CreateXYPoint(toElements, i); for (int j = 0; j < _numberOfFromColumns; j++) { XYPolyline fromPolyLine = CreateXYPolyline(fromElements, j); _mappingMatrix[i, j] = XYGeometryTools.CalculatePolylineToPointDistance(fromPolyLine, toPoint); } } if (_method == ElementMapperMethod.Nearest) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } else if (_method == ElementMapperMethod.Inverse) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } if (minDist == 0) { int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } else { double denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = 1 / _mappingMatrix[i, j]; denominator = denominator + _mappingMatrix[i, j]; } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } } else { throw new Exception("methodDescription unknown for polyline to point mapping"); } } catch (Exception e) // Catch for all of the Point to Polyline part { throw new Exception("Polyline to point mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.PolyLine && toElements.ElementType == ElementType.Polygon) { #region try { // For each polygon in target for (int i = 0; i < _numberOfToRows; i++) { XYPolygon polygon = CreateXYPolygon(toElements, i); if (_method == ElementMapperMethod.WeightedMean) { double totalLineLengthInPolygon = 0; for (int n = 0; n < _numberOfFromColumns; n++) { XYPolyline polyline = CreateXYPolyline(fromElements, n); _mappingMatrix[i, n] = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon( polyline, polygon); totalLineLengthInPolygon += _mappingMatrix[i, n]; } if (totalLineLengthInPolygon > 0) { for (int n = 0; n < _numberOfFromColumns; n++) { _mappingMatrix[i, n] = _mappingMatrix[i, n] / totalLineLengthInPolygon; } } } else if (_method == ElementMapperMethod.WeightedSum) { // For each line segment in PolyLine for (int n = 0; n < _numberOfFromColumns; n++) { XYPolyline polyline = CreateXYPolyline(fromElements, n); _mappingMatrix[i, n] = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon( polyline, polygon) / polyline.GetLength(); } } else { throw new Exception( "methodDescription unknown for polyline to polygon mapping"); } } } catch (Exception e) { throw new Exception("Polyline to polygon mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.Polygon && toElements.ElementType == ElementType.Point) { #region try { if (_method != ElementMapperMethod.Value) { throw new Exception("methodDescription unknown for polygon to point mapping"); } // Only create search tree if number of cols/rows is larger than say 10/10. bool useSearchTree = _numberOfFromColumns > 10 && _numberOfToRows > 10; XYElementSearchTree <int> fromSearchTree = null; ICollection <int> fromCandidateElmts = null; if (useSearchTree) { fromSearchTree = XYElementSearchTree <int> .BuildSearchTree(fromElements); } else { fromCandidateElmts = new IntSequence(0, _numberOfFromColumns - 1); } for (int n = 0; n < _numberOfToRows; n++) { XYPoint point = CreateXYPoint(toElements, n); if (useSearchTree) { XYExtent toExtent = XYExtentUtil.GetExtent(point, XYGeometryTools.EPSILON); fromCandidateElmts = fromSearchTree.FindElements(toExtent); } int count = 0; // Check first for strict inclusion foreach (int i in fromCandidateElmts) { XYPolygon polygon = CreateXYPolygon(fromElements, i); if (XYGeometryTools.IsPointInPolygon(point, polygon)) { _mappingMatrix[n, i] = 1.0; count++; } } if (count == 0) { // Not strictly inside any polygon, check also edges foreach (int i in fromCandidateElmts) { XYPolygon polygon = CreateXYPolygon(fromElements, i); if (XYGeometryTools.IsPointInPolygonOrOnEdge(point, polygon)) { _mappingMatrix[n, i] = 1.0; count++; } } } if (count > 1) { // In case of more than one hit, use average foreach (int i in fromCandidateElmts) { if (_mappingMatrix[n, i] != 0.0) { _mappingMatrix[n, i] = 1.0 / count; } } } } } catch (Exception e) { throw new Exception("Polygon to point mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.Polygon && toElements.ElementType == ElementType.PolyLine) // Polygon to PolyLine { #region try { for (int i = 0; i < _numberOfToRows; i++) { XYPolyline polyline = CreateXYPolyline(toElements, i); if (_method == ElementMapperMethod.WeightedMean) { for (int n = 0; n < _numberOfFromColumns; n++) { XYPolygon polygon = CreateXYPolygon(fromElements, n); _mappingMatrix[i, n] = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon( polyline, polygon) / polyline.GetLength(); } double sum = 0; for (int n = 0; n < _numberOfFromColumns; n++) { sum += _mappingMatrix[i, n]; } for (int n = 0; n < _numberOfFromColumns; n++) { _mappingMatrix[i, n] = _mappingMatrix[i, n] / sum; } } else if (_method == ElementMapperMethod.WeightedSum) { for (int n = 0; n < _numberOfFromColumns; n++) { XYPolygon polygon = CreateXYPolygon(fromElements, n); _mappingMatrix[i, n] = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon( polyline, polygon) / polyline.GetLength(); } } else { throw new Exception( "methodDescription unknown for polygon to polyline mapping"); } } } catch (Exception e) // catch for all of Polygon to PolyLine { throw new Exception("Polygon to polyline mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.Polygon && toElements.ElementType == ElementType.Polygon) // Polygon to Polygon { #region try { // Only create search tree if number of cols/rows is larger than say 100/10. bool useSearchTree = _numberOfFromColumns > 10 && _numberOfToRows > 10; XYElementSearchTree <int> fromSearchTree = null; ICollection <int> fromCandidateElmts = null; if (useSearchTree) { fromSearchTree = XYElementSearchTree <int> .BuildSearchTree(fromElements); } else { fromCandidateElmts = new IntSequence(0, _numberOfFromColumns - 1); } for (int i = 0; i < _numberOfToRows; i++) { XYPolygon toPolygon = CreateXYPolygon(toElements, i); if (useSearchTree) { XYExtent toExtent = XYExtentUtil.GetExtent(toPolygon); fromCandidateElmts = fromSearchTree.FindElements(toExtent); } foreach (int j in fromCandidateElmts) { XYPolygon fromPolygon = CreateXYPolygon(fromElements, j); _mappingMatrix[i, j] = XYGeometryTools.CalculateSharedArea( toPolygon, fromPolygon); if (_method == ElementMapperMethod.Distribute) { _mappingMatrix[i, j] /= fromPolygon.GetArea(); } } if (_method == ElementMapperMethod.WeightedMean) { double denominator = 0; foreach (int j in fromCandidateElmts) { denominator = denominator + _mappingMatrix[i, j]; } foreach (int j in fromCandidateElmts) { if (denominator != 0) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } else if (_method == ElementMapperMethod.WeightedSum) { foreach (int j in fromCandidateElmts) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / toPolygon.GetArea(); } } else if (_method != ElementMapperMethod.Distribute) { throw new Exception( "methodDescription unknown for polygon to polygon mapping"); } } } catch (Exception e) // catch for all of Polygon to Polygon { throw new Exception("Polygon to polygon mapping failed", e); } #endregion } else // if the fromElementType, toElementType combination is no implemented { throw new Exception( "Mapping of specified ElementTypes not included in ElementMapper"); } } catch (Exception e) { throw new Exception("UpdateMappingMatrix failed to update mapping matrix", e); } }
private static void SetSeq(ref string res, ref IntSequence seq) { if (res == string.Empty) { res = seq.GetSeq(); } else { res += ", " + seq.GetSeq(); } }