Communicator.Util

 Util

Type aliases

Action

ActionLike

ActionLike: LazyLike<ActionResult>

ActionResult

ActionResult: Promise<void> | void

LazyLike

LazyLike: Lazy<T> | function

Functions

TypeAssert

  • TypeAssert<T>(_x: T): void
  • This purpose of this function is guarantee a value is of a given type.

    This is essentially a compile time console.assert(x instanceof T).

    This function is useful when terminating a type-narrowing if-else chain in that this function makes the code more robust to future type changes.


    Warning:

    T=never doesn't appear to work properly, hence the existence of TypeAssertNever.


    Example:

    declare const x: Foo | Bar;
    if (x instanceof Foo) {
        console.log("foo");
    }
    else {
        // This becomes a compiler error if, for example, `x`'s type gets changed to add `Baz`:
        //    x: Foo | Bar | Baz
        TypeAssert<Bar>(x);
        console.log("bar");
    }

    Type parameters

    • T

    Parameters

    • _x: T

    Returns void

TypeAssertNever

  • TypeAssertNever(_x: never): never
  • See TypeAssert for details.

    This is useful for making the compiler enforce fully-covered switch statements.

    This function unconditionally throws InternalLogicError.


    Example:

     switch (x) {
         case Enum.A:
             break;
         case Enum.B:
             break;
         default:
             TypeAssertNever(x); // compiler complains if missing other enum cases
     }

    Parameters

    • _x: never

    Returns never

closestPointFromPointToSegment

  • Finds the closest point on line segment p0-p1 to the given point. The closest point will always lie on or between the line segment endpoints.

    Parameters

    • p0: Point3

      First point of the line segment

    • p1: Point3

      Second point of the line segment

    • point: Point3

      Point at which to find closest line segment point.

    Returns Point3

closestPointScalarFromPointToSegment

  • Finds the scalar for the closest point on line segment p0-p1 to the given point. The returned scalar will always be in the range [0, 1], where 0 indicates p0 is closest, and 1 indicates p1 is closest.

    Parameters

    • p0: Point3

      First point of the line segment

    • p1: Point3

      Second point of the line segment

    • point: Point3

      Point at which to find closest line segment scalar

    Returns number

computeAngleBetweenVector

  • Computes the smallest angle between two vectors in degrees.

    Parameters

    • vector1: Point3

      The first vector.

    • vector2: Point3

      The second vector.

    Returns Degrees

    The angle between vectors in degrees.

computeOffaxisRotation

  • Computes the rotation matrix defined by rotating around a vector.

    Parameters

    • axisVector: Point3

      The vector to rotate around.

    • degrees: Degrees

      The amount to rotate.

    • Optional out_rotationMatrix: Matrix

      The out parameter for the rotation matrix.

    Returns Matrix

    The out parameter rotation matrix.

computePointToLineDistance

  • Computes the shortest distance between a point and a line segment.

    Parameters

    • point: Point3
    • lineBegin: Point3

      The start point of the line segment to compute against.

    • lineEnd: Point3

      The end point of the line segment to compute against.

    • out_closestPointOnLine: Point3

      The out parameter for a closest point on the line segment to the point.

    Returns number

    The distance from the point and the closest point on the line.

copyMap

  • copyMap<K, V>(input: Map<K, V>): Map<K, V>
  • Performs a shallow copy of a Map.

    Type parameters

    • K

    • V

    Parameters

    • input: Map<K, V>

      The Map to be copied

    Returns Map<K, V>

copySet

  • copySet<T>(input: Set<T>): Set<T>
  • Performs a shallow copy of a Set.

    Type parameters

    • T

    Parameters

    • input: Set<T>

      The Map to be copied

    Returns Set<T>

createCylinderMeshDataFromArc

  • createCylinderMeshDataFromArc(arc: number[], axisDirection: Point3, numSegments: number, scale: number): MeshData
  • Creates a cylinder that is deformed by an arc. An example of the resulting geometry can be observed in the Handles implementation.

    Parameters

    • arc: number[]

      an array of numbers describing points on an arc that will be used to deform the cylinder

    • axisDirection: Point3

      cylinder axis.

    • numSegments: number

      the number of segments to use when constructing the cylinder. A higher number will give a smoother appearance but consume more memory.

    • scale: number

      a scaling factor to apply to the geometry.

    Returns MeshData

degreesToRadians

  • Converts degrees to radians.

    Parameters

    • degrees: Degrees

      The degrees to convert.

    Returns Radians

    The converted radians.

distanceLineLine

  • Returns the distance between two line segments.

    Parameters

    • line1Begin: Point3

      The start of the first line segment.

    • line1End: Point3

      The end of the first line segment.

    • line2Begin: Point3

      The start of the second line segment.

    • line2End: Point3

      The end of the second line segment.

    • out_closestPointLine1: Point3

      Out parameter for the closest point of line1 to line2.

    • out_closestPointLine2: Point3

      Out parameter for the closest point of line2 to line1.

    Returns number

    The distance between the two input line segments.

filterInPlace

  • filterInPlace<T>(xs: T[], pred: function): void
  • This function takes an array of type , and a predicate function to test each element of the array. This function does not create a new array.

    Type parameters

    • T

    Parameters

    • xs: T[]

      Array to filter.

    • pred: function

      If this function returns true when testing an item, the item will be kept in the array, otherwise the item will be removed.

        • (x: T): boolean
        • Parameters

          • x: T

          Returns boolean

    Returns void

formatWithUnit

  • formatWithUnit(value: number, unit: number): string
  • Returns the formatted string of a value and its units. Unit scaling is based on unit === 1 being for millimeters.

    Parameters

    • value: number

      The value to format (without units).

    • unit: number

      The unit scale to be applied to value.

    Returns string

generateArcPoints

  • generateArcPoints(axis: Point3, angle: number, center: Point3, startOffset: Point3, segmentCount: number): Point3[]
  • Returns an array of evenly-distributed points that lie on an arc.

    Parameters

    • axis: Point3

      The normal of the plane containing the arc (in other words, the axis of rotation).

    • angle: number

      The angle swept by the arc (may be negative).

    • center: Point3

      The center point of the arc.

    • startOffset: Point3

      The starting point of the arc, expressed as an offset relative to the center.

    • segmentCount: number

      The number of line segments to be generated.

    Returns Point3[]

    An array containing segmentCount + 1 points.

generateConeCylinderMeshData

  • generateConeCylinderMeshData(cylinderRadius: number, numSegments: number, stemHeight: number, coneBaseRadius: number, capHeight: number, taperHeight: number): MeshData
  • Creates a cylinder with an attached cone. An example of the resulting geometry can be observed in the default Axis Triad or Handles implementation.

    Parameters

    • cylinderRadius: number

      the radius of the cylinder portion of the geometry.

    • numSegments: number

      the number of segments used to create the cylinder and cone portions. Increasing this number will result in a smoother appearance but consume more memory.

    • stemHeight: number

      the height of the cone portion.

    • coneBaseRadius: number

      the radius of the cone portion

    • capHeight: number

      the height of the cylinder cap

    • taperHeight: number

      the height of the taper.

    Returns MeshData

generatePointsOnCircle

  • generatePointsOnCircle(out_points: Point3[], center: Point3, radius: number, numPoints: number, axisVector: Point3): void
  • Generates tessellated points suitable for mesh creation for a given circle.

    Parameters

    • out_points: Point3[]

      The out parameter for the generated points.

    • center: Point3

      The center of the circle.

    • radius: number

      The radius of the circle.

    • numPoints: number

      The number of points to use for the tesesselated circle.

    • axisVector: Point3

      The axis to orient the circle against.

    Returns void

generateSphereMeshData

  • Creates a basic sphere with normals.

    Returns MeshData

getLongUnitString

  • getLongUnitString(unit: UnitElement[]): string
  • Parameters

    • unit: UnitElement[]

    Returns string

intersect3d2Planes

  • Computes the intersection of two planes.

    Parameters

    • plane1: Plane

      The first plane.

    • pointOnPlane1: Point3

      A point on the first plane.

    • plane2: Plane

      The second plane.

    • pointOnPlane2: Point3

      A point on the second plane.

    • out_lineBegin: Point3

      Out parameter for resulting intersection line (if any).

    • out_lineEnd: Point3

      Out parameter for resulting intersection line (if any).

    Returns 0 | 1 | 2

    0 if the planes are disjoint. 1 if the planes coincide. 2 if the planes intersect in a line.

intersectionPlaneLine

  • Computes the intersection of a line segment and a plane.

    Parameters

    • lineBegin: Point3

      The start point of the line segment to intersect.

    • lineEnd: Point3

      The end point of the line segment to intersect.

    • planePoint1: Point3

      A point on the plane to intersect.

    • planePoint2: Point3

      A point on the plane to intersect.

    • planePoint3: Point3

      A point on the plane to intersect.

    • out_intersectionPoint: Point3

      The out parameter for the point of intersection if one exists.

    Returns boolean

    True if the line segment and plane intersect. False otherwise.

    See also: intersectionPlaneLine2.

intersectionPlaneLine2

  • Computes the intersection of a line segment and a plane.

    Parameters

    • lineBegin: Point3

      The start point of the line segment to intersect.

    • lineEnd: Point3

      The end point of the line segment to intersect.

    • plane: Plane

      The plane to intersect.

    • out_intersectionPoint: Point3

      The out parameter for the point of intersection if one exists.

    Returns boolean

    True if the line segment and plane intersect. False otherwise.

isPointInRect2d

  • isPointInRect2d(point: Point2, rectPos: Point2, rectSize: Point2, tolerance?: number): boolean
  • Returns whether the 2-dimensional point point lies within the given rectangle.

    Parameters

    • point: Point2

      The point to test

    • rectPos: Point2

      The lower-left corner of the rectangle

    • rectSize: Point2

      The width and height of the rectangle

    • Optional tolerance: number

      The maximum distance along the x- or y-axis that point is allowed to be outside the rectangle

    Returns boolean

isPointOnLineSegment

  • Determine if the point is both on the line formed by p0-p1, and within the p0-p1 line-segment endpoints

    Parameters

    • p0: Point3

      First point of the line segment

    • p1: Point3

      Second point of the line segment

    • point: Point3

      Point that possibly lies on the line segment.

    • epsilon: number

      Epsilon value used with point-on-line distance calculation.

    Returns boolean

isPointOnLineSegment2d

  • isPointOnLineSegment2d(point: Point2, p1: Point2, p2: Point2, tolerance: number): boolean
  • Returns whether the 2-dimensional point point lies on the line segment p1p2.

    Parameters

    • point: Point2

      The point to test

    • p1: Point2

      The first endpoint of the line segment

    • p2: Point2

      The second endpoint of the line segment

    • tolerance: number

      If the perpendicular distance of point from the line segment is less than or equal to tolerance, the function will return true.

    Returns boolean

lineLineIntersect

oneVectorCross

  • Returns the cross product of a vector against its least significant axis.

    Parameters

    • vector: Point3

      The input vector to cross.

    • Optional out_crossVector: Point3

      The out parameter for the cross product.

    Returns Point3

radiansToDegrees

  • Converts radians to degrees.

    Parameters

    Returns Degrees

    The converted degrees.

setSubtraction

  • setSubtraction<T>(setA: Set<T>, setB: Set<T>): Set<T>
  • Returns a new set consisting of all elements in setA not found in setB.

    Type parameters

    • T

    Parameters

    • setA: Set<T>

      The starting set to start subtracting from.

    • setB: Set<T>

      The set used to reject values from setA.

    Returns Set<T>

    The resulting set.

setToArray

  • setToArray<T>(set: Set<T>): T[]
  • Turns a Set into an Array.

    Type parameters

    • T

    Parameters

    • set: Set<T>

      The set to convert.

    Returns T[]

    The resulting array.

sleep

  • Returns a promise that resolves after the provided number of milliseconds

    Parameters

    • duration: Milliseconds

      number of milliseconds until the returned promise can resolve

    Returns Promise<void>

toSet

  • toSet<T>(xs: T[]): Set<T>
  • Creates a Set from the provided array.

    Type parameters

    • T

    Parameters

    • xs: T[]

      Array to create a Set from

    Returns Set<T>

waitForAll

  • waitForAll(promises: Promise<void>[]): Promise<void>
  • This function behaves like writing [[Promise.all(promises).then(no_op)]] but does not incur the overhead of creating a .then promise. Using this function can provide a performance boost when doing large amounts of batch processing with groups of promises.

    Parameters

    • promises: Promise<void>[]

      array of promises.

    Returns Promise<void>