@itwin/measure-tools-react
Version:
Frontend framework and tools for measurements
70 lines • 3.34 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { DrawingViewState, SheetViewState, SpatialViewState } from "@itwin/core-frontend";
import { WellKnownViewType } from "./MeasurementEnums.js";
import { MeasurementViewTarget } from "./MeasurementViewTarget.js";
/**
* Helper methods for working with views.
*/
export class ViewHelper {
/** Checks if the viewport is classified as a XSection view. */
static isCrossSectionView(vp) {
return MeasurementViewTarget.classifyViewport(vp) === WellKnownViewType.XSection;
}
/** Checks if the viewport is classified as a Profile view */
static isProfileView(vp) {
return MeasurementViewTarget.classifyViewport(vp) === WellKnownViewType.Profile;
}
/** Checks if the viewport is a sheet view. */
static isSheetView(vp) {
return vp.view instanceof SheetViewState;
}
/** Checks if the viewport is any section view */
static isSection(vp) {
return ViewHelper.isCrossSectionView(vp) || ViewHelper.isProfileView(vp) || ViewHelper.isSheetView(vp);
}
/** Checks if the viewport is a 3D view (spatial) */
static is3DView(vp) {
return vp.view instanceof SpatialViewState;
}
/** Checks if the viewport is a 2D view (drawing) */
static is2DView(vp) {
return vp.view instanceof DrawingViewState;
}
/** Given a ray in view coordinates, returns the intersection that is closest to the view rect.
* Each pair of corners of the ViewRect defines an infinite plane in 2D
* * Note: using Ray3d/Point3d for convenience only
*/
static closestIntersectionWithViewPlanes(rect, viewRay) {
let xScale;
let yScale;
if (0.0 !== viewRay.direction.x) {
const scale0 = (0.0 - viewRay.origin.x) / viewRay.direction.x;
const scale1 = (rect.width - viewRay.origin.x) / viewRay.direction.x;
if (scale0 > scale1 && scale0 >= 0.0)
xScale = scale0;
else if (scale1 > scale0 && scale1 >= 0.0)
xScale = scale1;
}
if (0.0 !== viewRay.direction.y) {
const scale0 = (0.0 - viewRay.origin.y) / viewRay.direction.y;
const scale1 = (rect.height - viewRay.origin.y) / viewRay.direction.y;
if (scale0 > scale1 && scale0 >= 0.0)
yScale = scale0;
else if (scale1 > scale0 && scale1 >= 0.0)
yScale = scale1;
}
const xPoint = undefined === xScale ? undefined : viewRay.origin.plusScaled(viewRay.direction, xScale);
const yPoint = undefined === yScale ? undefined : viewRay.origin.plusScaled(viewRay.direction, yScale);
if (!xPoint && !yPoint)
return undefined;
const xDiff = xPoint ? Math.abs(0.5 * rect.height - xPoint.y) : Number.MAX_VALUE;
const yDiff = yPoint ? Math.abs(0.5 * rect.width - yPoint.x) : Number.MAX_VALUE;
if (xDiff < yDiff)
return xPoint;
return yPoint;
}
}
//# sourceMappingURL=ViewHelper.js.map