/** * @file XTPMathUtils.h * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ /** @cond */ #if !defined(__XTPMATHUTILS_H__) # define __XTPMATHUTILS_H__ /** @endcond */ # if _MSC_VER >= 1000 # pragma once # endif // _MSC_VER >= 1000 # include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" /** * Evaluates to a double Not-A-Number value */ # define XTP_NAN CXTPMathUtils::GetNaN() /** * Returns TRUE if the value provided is double Not-A-Number value */ # define XTP_ISNAN(v) CXTPMathUtils::IsNan(v) /** * @brief * This class provide some mathematical utility functions. * @details */ class _XTP_EXT_CLASS CXTPMathUtils { public: /** * @brief * Call this function to normalize angle in radian i.e. bring the value * within the range of 0 - 2PI. * @param angleRadian Angle in radians. * @return * A double value denoting the normalized radian. * @details * It is a static function. */ static double AFX_CDECL NormalizeRadian(double angleRadian); /** * @brief * Call this function to normalize angle in degree i.e. bring the value * within the range of 0 and 360. * @param angle Angle in degree. * @return * A double value denoting the normalized degree. * @details * It is a static function. */ static double AFX_CDECL NormalizeDegree(double angle); /** * @brief * Call this function to check the sign of a double value. * @param value The double value. * @return * +1 if the value is positive and -1 if the value negative. * @details * It is a static function. */ static int AFX_CDECL Sign(double value); /** * @brief * Call this function to compare two double values.It also checks * whether they are equal or almost equal. * @param value1 The first value. * @param value2 The second value. * @return * +1 if the value1 is greater than value2, -1 if vice versa. * 0 if they are equal or almost equal. * @details * It is a static function. */ static int AFX_CDECL Compare(double value1, double value2); /** * @brief * Call this function to compare two double values.It also checks * whether they are equal or almost equal based on a 3rd value. * @param value1 The first value. * @param value2 The second value. * @param epsilon The equality threshold. * @return * +1 if the value1 is greater than value2, -1 if vice versa. * 0 if they are equal or almost equal. * @details * It is a static function. */ static int AFX_CDECL Compare(double value1, double value2, double epsilon); /** * @brief * Call this function to convert radian angle to degree. * @param angleRadian The angle in radian. * @return * A double value denoting the angle in degree. * @details * It is a static function. */ static double AFX_CDECL Radian2Degree(double angleRadian); /** * @brief * Call this function to convert degree angle to radian. * @param angleDegree The angle in degree. * @return * A double value denoting the angle in radian. * @details * It is a static function. */ static double AFX_CDECL Degree2Radian(double angleDegree); /** * @brief * Rounds a real value taking into account its sign and returns * result as integer. * @param dValue A value to be rounded. * @return * A rounded value casted to integer type. */ static int AFX_CDECL Round(double dValue); /** * @brief * Obtains Not-a-Number (NaN) value for double type. * @return * Double NaN value. * @see * IsNan */ static double AFX_CDECL GetNaN(); /** * @brief * Checks if the value provided is Not-a-Number (NaN) value. * @param x A value to check. * @return * TRUE if the value provided is NaN value. * @see * GetNaN */ static BOOL AFX_CDECL IsNan(double x); /** * @brief * Computes a normal for a triangle specified. * @param x1 1st triangle X coordinate. * @param y1 1st triangle Y coordinate. * @param z1 1st triangle Z coordinate. * @param x2 2nd triangle X coordinate. * @param y2 2nd triangle Y coordinate. * @param z2 2nd triangle Z coordinate. * @param x3 3rd triangle X coordinate. * @param y3 3rd triangle Y coordinate. * @param z4 3rd triangle Z coordinate. * @param nx Result normal X value. * @param ny Result normal Y value. * @param nz Result normal Z value. * @param normalize If true the result normal vector is clamped to 0..1 range. * @param ccw Determines which side of the triangle the normal vector will * be directed from. By default triangle points are assumed to * be in close-wise order which determines the front face of * the triangle for which the normal is computed. In order * to reverse normal vector this value has to be false which means * counter clock wise vertex unwinding. * @return */ static void AFX_CDECL ComputeTriangleNormal(double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3, double z4, double& nx, double& ny, double& nz, bool normalize = true, bool ccw = false); /** * @brief * Computes 2D line segment length. * @param x1 1st point X coordinate. * @param y1 1st point Y coordinate. * @param x2 2nd point X coordinate. * @param y2 2nd point Y coordinate. * @return * The distance between points provided. */ static double AFX_CDECL GetSegmentLength(double x1, double y1, double x2, double y2); /** * @brief * Computes 3D line segment length. * @param x1 1st point X coordinate. * @param y1 1st point Y coordinate. * @param z1 1st point Z coordinate. * @param x2 2nd point X coordinate. * @param y2 2nd point Y coordinate. * @param z2 2nd point Z coordinate. * @return * The distance between points provided. */ static double AFX_CDECL GetSegmentLength(double x1, double y1, double z1, double x2, double y2, double z2); /** * @brief * Tests if the point specified hits a line segment determined * by two points. * @param x1 1st point X coordinate. * @param y1 1st point Y coordinate. * @param x2 2nd point X coordinate. * @param y2 2nd point Y coordinate. * @param xPoint X coordinate of the point being tested. * @param yPoint Y coordinate of the point being tested. * @param nLineThickness Line thickness. * @return * TRUE if the point provided hits the line segment. */ static BOOL AFX_CDECL HitTestLineSegment(double x1, double y1, double x2, double y2, double xPoint, double yPoint, int nLineThickness); /** * @brief * Computes a point where two lines detemined by four points intersect. * @param x11 Line's one 1st point X coordinate. * @param y11 Line's one 1st point Y coordinate. * @param x12 Line's one 2nd point X coordinate. * @param y12 Line's one 2nd point Y coordinate. * @param x21 Line's two 1st point X coordinate. * @param y21 Line's two 1st point Y coordinate. * @param x22 Line's two 2nd point X coordinate. * @param y22 Line's two 2nd point Y coordinate. * @param xi On successful completion takes a value of X coordinate of * line intersection position. * @param yi On successful completion takes a value of Y coordinate of * line intersection position. * @return * TRUE if lines intersect and intersection point is found. */ static BOOL AFX_CDECL GetLinesIntersection(double x11, double y11, double x12, double y12, double x21, double y21, double x22, double y22, double& xi, double& yi); /** * @brief * Computes vector cross product. * @param x1 1st vector X coordinate. * @param y1 1st vector Y coordinate. * @param z1 1st vector Z coordinate. * @param x2 2nd vector X coordinate. * @param y2 2nd vector Y coordinate. * @param z2 2nd vector Z coordinate. * @param rx Result vector X coordinate. * @param ry Result vector Y coordinate. * @param rz Result vector Z coordinate. * @see * ComputeVectorDotProduct, ComputeVectorLength * @return */ static void AFX_CDECL ComputeVectorCrossProduct(double x1, double y1, double z1, double x2, double y2, double z2, double& rx, double& ry, double& rz); /** * @brief * Computes vector cross product. * @param x1 1st vector X coordinate. * @param y1 1st vector Y coordinate. * @param z1 1st vector Z coordinate. * @param x2 2nd vector X coordinate. * @param y2 2nd vector Y coordinate. * @param z2 2nd vector Z coordinate. * @return * Vector dot product value. * @see * ComputeVectorCrossProduct, ComputeVectorLength */ static double AFX_CDECL ComputeVectorDotProduct(double x1, double y1, double z1, double x2, double y2, double z2); /** * @brief * Computes vector length. * @param x Vector X coordinate. * @param y Vector Y coordinate. * @param z Vector Z coordinate. * @return * Vector length value. * @see * ComputeVectorCrossProduct, ComputeVectorDotProduct */ static double AFX_CDECL ComputeVectorLength(double x, double y, double z); public: static const double m_dPI; /**< Pi value */ static const double m_dEPS; /**< The smallest possible double value, or epsilon. */ }; # include "Common/Base/Diagnostic/XTPEnableNoisyWarnings.h" /** @cond */ #endif //#if !defined(__XTPMATHUTILS_H__) /** @endcond */