using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DHI.OpenMI2.Sdk.Backbone;
using DHI.OpenMI2.Sdk.Spatial;
namespace MikeSheInOpenDA.Spatial
{
public class XYZGeometryTools
{
public const double EPSILON = 1e-5;
///
/// Returns the distance between the two points.
///
/// Point
/// Point
/// Point to point distance
public static double CalculatePointToPointDistance2D(IXYLayerPoint p1, IXYLayerPoint p2)
{
double dx = p1.X - p2.X;
double dy = p1.Y - p2.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
///
/// Returns the distance between the two points.
///
/// Point
/// Point
/// Point to point distance
public static double CalculatePointToPointDistance2D(IXYLayerPoint p1, ICoordinate p2)
{
double dx = p1.X - p2.X;
double dy = p1.Y - p2.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
public static bool IsPointInPolygon(double x, double y, XYPolygon polygon)
{
if (x > polygon.GetX(0) && x < polygon.GetX(1) &&
y > polygon.GetY(0) && y < polygon.GetY(2))
{
return true;
}
else
{
return false;
}
}
///
/// Finds if the given point is located in the model grid.
/// Cycles through every grid rectangle.
///
/// point to check if it's within the grid.
/// the model grid.
/// layer specific
///
public static bool IsPointInModelPlain(IXYLayerPoint pt, IDictionary modelCoordinates, bool layerIndifferent = false)
{
return modelCoordinates.Values.Any(rec => rec.PointInObject(new XYLayerPoint(pt.X, pt.Y, 0), layerIndifferent));
}
///
/// Finds if the given point is located in the model grid.
/// Cycles through every grid rectangle.
///
/// point to check if it's within the grid.
/// the model grid.
/// layer specific
///
public static bool IsPointInModelPlain(ICoordinate pt, IDictionary modelCoordinates, bool layerIndifferent = false)
{
return modelCoordinates.Values.Any(rec => rec.PointInObject(new XYLayerPoint(pt.X, pt.Y, 0), layerIndifferent));
}
///
/// Finds if the given point is located in the model grid.
/// Cycles through every grid rectangle.
///
/// point to check if it's within the grid.
/// the model grid.
/// layer specific
///
public static bool IsPointInModelPlain(ICoordinate pt, IList modelCoordinates, bool layerIndifferent = false)
{
return modelCoordinates.Any(rec => rec.PointInObject(new XYLayerPoint(pt.X, pt.Y, 0), layerIndifferent));
}
///
/// Finds if the given point is located in the model grid.
/// Cycles through every grid rectangle.
///
/// point to check if it's within the grid.
/// the model grid.
/// layer specific
///
public static bool IsPointInModelPlain(IXYLayerPoint pt, IList modelCoordinates, bool layerIndifferent = false)
{
return modelCoordinates.Any(rec => rec.PointInObject(new XYLayerPoint(pt.X, pt.Y, 0), layerIndifferent));
}
///
/// Seaches the model coordinates to find if the given point is locaded within the model. If so, return the model index.
/// NOTE the point is a Z LAYER!
///
/// a point to search for
/// Dictionary containing the model coordinates as values and the key is the model index for each coordinate.
/// Return model index if found, else return -1
public static int ModelIndexWherePointIsLocated(IXYLayerPoint pt, IDictionary modelCoordinates)
{
foreach (KeyValuePair pair in modelCoordinates)
{
if (pair.Value.PointInObject(new XYLayerPoint(pt.X, pt.Y, pt.Layer), false))
{
return pair.Key;
}
;
}
return -1;
}
///
/// Seaches the model coordinates to find if the given point is locaded within the model. If so, return the model index.
/// NOTE the point is a Z LAYER!
///
/// a point to search for
/// Dictionary containing the model coordinates as values and the key is the model index for each coordinate.
/// Return model index if found, else return -1
public static int ModelIndexWherePointIsLocated(ICoordinate pt, IDictionary modelCoordinates)
{
foreach (KeyValuePair pair in modelCoordinates)
{
if (pair.Value.PointInObject(new XYLayerPoint(pt.X, pt.Y, Convert.ToInt32(pt.Z)), false))
{
return pair.Key;
}
;
}
return -1;
}
}
}