Class SimpleShape
- Namespace
- CADability.Shapes
- Assembly
- CADability.dll
A simply connected 2d shape. It consists of a Border outline and 0 or more holes. The holes don't overlap (disjunct) and reside totally inside the outline.
public class SimpleShape : ISerializable, IQuadTreeInsertable, IComparable<SimpleShape>, IJsonSerialize
- Inheritance
-
SimpleShape
- Implements
- Inherited Members
Constructors
SimpleShape()
Creates an uninitialized shape for the JSON deserialization; SetObjectData(IJsonReadData) fills it in.
protected SimpleShape()
SimpleShape(Border)
Constructs a simple shape by specifying the outline
public SimpleShape(Border outline)
Parameters
outlineBorderThe outline
SimpleShape(Border, params Border[])
Constructs a simple shape by specifying the outline and any number of holes.
public SimpleShape(Border outline, params Border[] holes)
Parameters
SimpleShape(SerializationInfo, StreamingContext)
Constructor required by deserialization
protected SimpleShape(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInfoSerializationInfo
contextStreamingContextStreamingContext
Properties
Area
Returns the area of this simple shape.
public double Area { get; }
Property Value
DebugList
The segments of the outline and of the holes as 3d curves in the xy plane, for inspection in a debugger or for displaying the shape in a model. Intended for debugging.
public GeoObjectList DebugList { get; }
Property Value
Empty
Returns true, if the Area of this shape is 0.0
public bool Empty { get; }
Property Value
Holes
Returns all holes of this simple shape in an array. You may modify the array since it is a clone of the holes array in this object. But you may not modify the individual holes.
public Border[] Holes { get; }
Property Value
- Border[]
NumHoles
Gets the number of holes in this simple shape.
public int NumHoles { get; }
Property Value
Outline
Gets the outline of this simple shape. Do not modify the border because this shape might become invalid. To obtain a good performance the outline of this simple shape is returned, not a clone.
public Border Outline { get; }
Property Value
Segments
All segments of this shape: those of the outline followed by those of the holes, each border counterclockwise as every closed Border is. The original curves are returned, do not modify them.
public IEnumerable<ICurve2D> Segments { get; }
Property Value
Methods
CalculateSignature()
Computes a signature of this shape that is invariant under rotation and translation and can be compared quickly (see isEqual(Signature, out bool, double)): the shape is rotated so that its longest outline segment is horizontal and cut with two horizontal and four vertical lines at fixed relative positions; the parameter intervals inside the shape on these lines and the total length of all borders make up the signature. Used by isCongruent(SimpleShape, Signature, out ModOp2D, double) to compare many shapes cheaply before verifying congruence.
public CompoundShape.Signature CalculateSignature()
Returns
- CompoundShape.Signature
The signature of this shape
CheckTopology()
Checks whether this shape is correct. I.e. no (self-) intersections of the outline or holes, all holes inside the outline
public bool CheckTopology()
Returns
Clip(ICurve2D, bool)
Clips the provided curve by this shape. Either the inner parts or the parts outside of this shape are returned.
public double[] Clip(ICurve2D toClip, bool returnInsideParts)
Parameters
toClipICurve2DCurve to be clipped
returnInsidePartsbooltrue: return inside parts, false: return outside parts
Returns
- double[]
the clipped curves
Clone()
Returns a clone (deep copy) of this simple shape
public SimpleShape Clone()
Returns
- SimpleShape
The clone
ConnectPaths(Path2D, Path2D)
Joins two closed paths into a single closed path by inserting a bridge: the closest points p1 on
outline and p2 on hole are determined, and the result runs from p2 to p1,
around the outline back to p1, back to p2 and around the hole back to p2. Both paths are cloned and flattened
first, the originals stay unchanged. The direction of each path is kept, so a hole should be passed reversed
(clockwise) for a correctly wound result.
public static Path2D ConnectPaths(Path2D outline, Path2D hole)
Parameters
Returns
- Path2D
The combined closed path
Contains(GeoPoint2D, bool)
Tests whether the provided point is contained in this simple shape.
public bool Contains(GeoPoint2D p, bool acceptOnCurve)
Parameters
pGeoPoint2DThe point to be examined
acceptOnCurvebooltrue: accept points on the border as inside points, false: return true only for points totally inside this shape
Returns
- bool
true if the point is contained, false otherwise
Expand(double)
Expands this simple shape by the given amount. note that the result is a CompoundShape which is composed of multiple simple shapes.
public CompoundShape Expand(double d)
Parameters
ddoubleAmount to expand, if negative Shrink(double) will be called
Returns
- CompoundShape
The resulting shape
GetExtent()
Returns the extent of this shape, i.e. the size of the horizontally adjusted rectangle that encloses it.
public BoundingRect GetExtent()
Returns
GetModified(ModOp2D)
Returns a modified shape of this shape. This shape remains unchanged.
public SimpleShape GetModified(ModOp2D m)
Parameters
mModOp2DModification by which this shape is modified
Returns
GetObjectData(IJsonWriteData)
Writes the outline and the holes for the JSON serialization (IJsonSerialize).
public void GetObjectData(IJsonWriteData data)
Parameters
dataIJsonWriteDataReceives the properties of this shape
GetObjectData(SerializationInfo, StreamingContext)
Writes the data of this object for the legacy binary serialization (ISerializable). Files are written in the JSON format (IJsonSerialize) now; the binary format is still read.
public void GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInforeceives the data
contextStreamingContextthe destination of the serialization
GetPosition(GeoPoint2D, double)
Determines where a point lies relative to this shape: Inside for a point inside the
outline but not inside a hole, Outside for a point outside the outline or inside a
hole, OnCurve for a point on the outline or on the border of a hole (within
prec). Unlike Contains(GeoPoint2D, bool) the three cases are distinguished.
public Border.Position GetPosition(GeoPoint2D p, double prec = 0)
Parameters
pGeoPoint2DThe point to test
precdoubleDistance within which the point counts as being on a border; 0 uses eps
Returns
GetPosition(CompoundShape, CompoundShape)
Checks the relative position of two shapes to each other. The order of the parameters is important for the result.
public static SimpleShape.Position GetPosition(CompoundShape cs1, CompoundShape cs2)
Parameters
cs1CompoundShapeFirst shape
cs2CompoundShapeSecond shape
Returns
- SimpleShape.Position
The position
GetPosition(SimpleShape, SimpleShape)
Checks the relative position of two shapes to each other. The order of the parameters is important for the result.
public static SimpleShape.Position GetPosition(SimpleShape s1, SimpleShape s2)
Parameters
s1SimpleShapeFirst shape
s2SimpleShapeSecond shape
Returns
- SimpleShape.Position
The position
GetSingleOutline()
Returns the outline and the holes as one closed path: each hole (reversed, i.e. clockwise) is joined to the outline by a pair of coincident bridge lines at the closest points, see ConnectPaths(Path2D, Path2D). Such a path describes the shape with a single contour, e.g. for exporting to formats or algorithms that do not know holes.
public Path2D GetSingleOutline()
Returns
- Path2D
A single closed path covering the outline and the holes
GetSomeInnerPoint()
Finds a point inside this shape (inside the outline, not in a hole) that is well away from the borders: the shape is clipped with a growing set of horizontal and vertical lines and the middle of the widest piece inside is taken, as long as its distance to the borders exceeds a limit that is relaxed with every round. Used e.g. as a test point to evaluate a surface on a face. Returns the origin if no such point can be found.
public GeoPoint2D GetSomeInnerPoint()
Returns
- GeoPoint2D
A point inside the shape
HitTest(ref BoundingRect)
Checks whether this shape and the provided rectangle overlap
public bool HitTest(ref BoundingRect Rect)
Parameters
RectBoundingRect
Returns
Hole(int)
Gets the hole with the specified index. The original border is returned. Do not modify this or the simple shape might become invalid. If you need to modify it, make a clone first.
public Border Hole(int Index)
Parameters
Indexint
Returns
Intersect(SimpleShape, SimpleShape)
Returns the intersection (common parts, overlapping area) of two simple shapes.
public static CompoundShape Intersect(SimpleShape Part1, SimpleShape Part2)
Parameters
Part1SimpleShapeFirst shape
Part2SimpleShapeSecond shape
Returns
- CompoundShape
The resulting shape (may be empty)
Intersect(SimpleShape, SimpleShape, double)
Returns the intersection (common area) of two simple shapes like Intersect(SimpleShape, SimpleShape),
with an explicit precision for intersecting the outlines: points closer than precision are
considered identical, which makes the operation robust for shapes that touch or share edges.
public static CompoundShape Intersect(SimpleShape Part1, SimpleShape Part2, double precision)
Parameters
Part1SimpleShapeFirst shape
Part2SimpleShapeSecond shape
precisiondoublePrecision for the intersection of the outlines
Returns
- CompoundShape
The resulting shape (may be empty)
MakeCircle(GeoPoint2D, double)
Creates a circular disk without holes, see MakeCircle(GeoPoint2D, double).
public static SimpleShape MakeCircle(GeoPoint2D center, double radius)
Parameters
centerGeoPoint2DCenter of the circle
radiusdoubleRadius of the circle
Returns
- SimpleShape
The circular shape
MakeLongHole(ICurve2D, double, double)
Creates the shape of a slot around an arbitrary curve: all points closer than halfWidth to
centerLine. The curve is approximated by lines and arcs with the given precision, a slot is
built around each piece (see MakeLongHole(ICurve2D, double); arcs with a radius not greater
than the half width and arcs sweeping more than 180 degrees are handled specially) and the slots are united. As
the pieces are connected, the union is a single simple shape, which may have a hole when the curve winds around.
public static SimpleShape MakeLongHole(ICurve2D centerLine, double halfWidth, double precision)
Parameters
centerLineICurve2DThe open center curve of the slot, a single curve or a Path2D
halfWidthdoubleHalf the width of the slot, i.e. the radius of the round ends
precisiondoubleMaximum deviation when approximating the curve by lines and arcs
Returns
- SimpleShape
The slot, or null if no piece of the curve yielded a shape
MakePaths(Plane)
Converts the outline and the holes of this shape into Path objects.
public Path[] MakePaths(Plane plane)
Parameters
planePlanePlane in 3D space where the shape is located
Returns
- Path[]
The resulting path(s)
MakePolygon(GeoPoint2D, double, int)
Creates a regular polygon without holes, see MakePolygon(GeoPoint2D, double, int).
public static SimpleShape MakePolygon(GeoPoint2D center, double radius, int numberOfSides)
Parameters
centerGeoPoint2DCenter of the polygon
radiusdoubleDistance of the vertices from the center
numberOfSidesintNumber of sides (and vertices), at least 3
Returns
- SimpleShape
The polygonal shape
Project(Plane, Plane)
The 2d-simple shape is assumed to reside in plane "fromPlane". It will be projected perpendicular onto the plane "toPlane". If the planes are perpendicular, the result will be am empty shape.
public SimpleShape Project(Plane fromPlane, Plane toPlane)
Parameters
Returns
- SimpleShape
the projected shape
Reduce(double)
Simplifies the outline and the holes in place: segments that are not lines or arcs are approximated by lines and
arcs with the given precision, adjacent segments that can be merged into one are fused, and segments shorter than
precision as well as dead ends (spikes going out and back) are removed. Holes whose area drops
below precision are dropped. Useful after boolean operations, which tend to leave tiny segments.
public void Reduce(double precision)
Parameters
precisiondoubleMaximum deviation of the approximation and minimum length of a segment to keep
SetObjectData(IJsonReadData)
Reads the outline and the holes written by GetObjectData(IJsonWriteData) for the JSON deserialization.
public void SetObjectData(IJsonReadData data)
Parameters
dataIJsonReadDataThe properties of the shape being read
Shrink(double)
Shrinks this simple shape by the given amount. note that the result is a CompoundShape which is composed of multiple simple shapes.
public CompoundShape Shrink(double d)
Parameters
ddoubleAmount to shrink, if negative Expand(double) will be called
Returns
- CompoundShape
The resulting shape (may be empty)
Split(Border)
Returns a CompoundShape consisting of several SimpleShapes that touch each other.
This SimpleShape is cut along the open Border ToSplitWith into
several subshapes.
public CompoundShape Split(Border ToSplitWith)
Parameters
ToSplitWithBorderThe curve to cut with (open Border)
Returns
- CompoundShape
Splitted shape
Subtract(SimpleShape, SimpleShape)
Returns the subtraction of Part1 minus Part2, i.e. all parts that belont to Part1
but not to Part2.
public static CompoundShape Subtract(SimpleShape Part1, SimpleShape Part2)
Parameters
Part1SimpleShapeShape to be subtracted from
Part2SimpleShapeShape that is subtracted
Returns
- CompoundShape
The resulting shape (may be empty)
ThickPolyLine(Polyline2D, double)
Creates the shape covered by a round pen of the given radius moving along the polyline: the union of a rectangle
of width 2*radius around each edge and a disk of that radius around each vertex, so the ends
and the corners are rounded. The pieces are connected, so the union is a single simple shape; should it fall
apart (e.g. for a degenerate polyline), only the first simple shape is returned.
public static SimpleShape ThickPolyLine(Polyline2D p2d, double radius)
Parameters
p2dPolyline2DThe polyline to thicken
radiusdoubleHalf the width of the resulting band
Returns
- SimpleShape
The thickened polyline as a simple shape
Unite(SimpleShape, SimpleShape)
Returns the union of two simple shapes.
public static CompoundShape Unite(SimpleShape Part1, SimpleShape Part2)
Parameters
Part1SimpleShapeFirst shape
Part2SimpleShapeSecond shape
Returns
- CompoundShape
The resulting shape (may be empty)
isCongruent(SimpleShape, Signature, out ModOp2D, double)
Tests whether this shape and other are congruent, i.e. whether a rotation and a translation
(possibly combined with a reflection) map this shape onto the other within precision. The
comparison starts with the signatures (see CalculateSignature()), computed here for every longest
segment of the outline and compared with otherSig; a candidate is then verified by actually
transforming this shape and checking that it coincides with other. Used to find identical
faces, e.g. for recognizing repeated features in a shell.
public bool isCongruent(SimpleShape other, CompoundShape.Signature otherSig, out ModOp2D thisToOther, double precision)
Parameters
otherSimpleShapeThe shape to compare with
otherSigCompoundShape.SignatureThe signature of
other, as returned by its CalculateSignature()thisToOtherModOp2DThe transformation mapping this shape onto
other, Null if not congruentprecisiondoubleMaximum distance for points to be considered identical
Returns
- bool
true if the shapes are congruent