码迷,mamicode.com
首页 > 其他好文 > 详细

Unreal Math: FVector(2)

时间:2019-08-03 21:10:26      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:整合   equal   other   speed   define   com   ike   float   direction   


旋转

计算旋转到目标向量朝向的欧拉角(四元数),不含Roll。

/**
 * Return the FRotator orientation corresponding to the direction in which the vector points.
 * Sets Yaw and Pitch to the proper numbers, and sets Roll to zero because the roll can‘t be determined from a vector.
 *
 * @return FRotator from the Vector‘s direction, without any roll.
 * @see ToOrientationQuat()
 */
CORE_API FRotator ToOrientationRotator() const;

/**
 * Return the Quaternion orientation corresponding to the direction in which the vector points.
 * Similar to the FRotator version, returns a result without roll such that it preserves the up vector.
 *
 * @note If you don‘t care about preserving the up vector and just want the most direct rotation, you can use the faster
 * ‘FQuat::FindBetweenVectors(FVector::ForwardVector, YourVector)‘ or ‘FQuat::FindBetweenNormals(...)‘ if you know the vector is of unit length.
 *
 * @return Quaternion from the Vector‘s direction, without any roll.
 * @see ToOrientationRotator(), FQuat::FindBetweenVectors()
 */
CORE_API FQuat ToOrientationQuat() const;

/**
 * Return the FRotator orientation corresponding to the direction in which the vector points.
 * Sets Yaw and Pitch to the proper numbers, and sets Roll to zero because the roll can‘t be determined from a vector.
 * @note Identical to ‘ToOrientationRotator()‘ and preserved for legacy reasons.
 * @return FRotator from the Vector‘s direction.
 * @see ToOrientationRotator(), ToOrientationQuat()
 */
CORE_API FRotator Rotation() const;
/** When this vector contains Euler angles (degrees), ensure that angles are between +/-180 */
CORE_API void UnwindEuler();

当vector表示欧拉角时候,将欧拉角的范围转换为在+/-180

球坐标

/** 
 * Converts a Cartesian unit vector into spherical coordinates on the unit sphere.
 * @return Output Theta will be in the range [0, PI], and output Phi will be in the range [-PI, PI]. 
 */
FVector2D UnitCartesianToSpherical() const;
{
	checkSlow(IsUnit());
	const float Theta = FMath::Acos(Z / Size()); (0,PI)
	const float Phi = FMath::Atan2(Y, X);(-PI,PI,x轴正向为0)
	return FVector2D(Theta, Phi);
}

计算笛卡尔坐标系中的单位球的向量对应的球坐标下的theta和phi的值。

方位角

float HeadingAngle() const;

在x-y平面上面的投影的方位角。

正交化

/**
 * Create an orthonormal basis from a basis with at least two orthogonal vectors.
 * It may change the directions of the X and Y axes to make the basis orthogonal,
 * but it won‘t change the direction of the Z axis.
 * All axes will be normalized.
 *
 * @param XAxis The input basis‘ XAxis, and upon return the orthonormal basis‘ XAxis.
 * @param YAxis The input basis‘ YAxis, and upon return the orthonormal basis‘ YAxis.
 * @param ZAxis The input basis‘ ZAxis, and upon return the orthonormal basis‘ ZAxis.
 */
static CORE_API void CreateOrthonormalBasis(FVector& XAxis,FVector& YAxis,FVector& ZAxis);

构造一组正交基,采用施密特正交化方法。

点面相关

/**
 * Compare two points and see if they‘re the same, using a threshold.
 *
 * @param P First vector.
 * @param Q Second vector.
 * @return Whether points are the same within a threshold. Uses fast distance approximation (linear per-component distance).
 */
static bool PointsAreSame(const FVector &P, const FVector &Q);

/**
 * Compare two points and see if they‘re within specified distance.
 *
 * @param Point1 First vector.
 * @param Point2 Second vector.
 * @param Dist Specified distance.
 * @return Whether two points are within the specified distance. Uses fast distance approximation (linear per-component distance).
 */
static bool PointsAreNear(const FVector &Point1, const FVector &Point2, float Dist);

点到平面距离,包含正负号,通过向量投影即可计算,见上一篇镜像和反射。

/**
 * Calculate the signed distance (in the direction of the normal) between a point and a plane.
 *
 * @param Point The Point we are checking.
 * @param PlaneBase The Base Point in the plane.
 * @param PlaneNormal The Normal of the plane (assumed to be unit length).
 * @return Signed distance between point and plane.
 */
static float PointPlaneDist(const FVector &Point, const FVector &PlaneBase, const FVector &PlaneNormal);

点在平面上面的投影,OD已知,DE可以用距离乘以单位向量得到,OE=OD-DE

技术图片
inline FVector FVector::PointPlaneProject(const FVector& Point, const FPlane& Plane)
{
	//Find the distance of X from the plane
	//Add the distance back along the normal from the point
	return Point - Plane.PlaneDot(Point) * Plane;
}

/**
 * Calculate the projection of a point on the plane defined by counter-clockwise (CCW) points A,B,C.
 *
 * @param Point The point to project onto the plane
 * @param A 1st of three points in CCW order defining the plane 
 * @param B 2nd of three points in CCW order defining the plane
 * @param C 3rd of three points in CCW order defining the plane
 * @return Projection of Point onto plane ABC
 */
static FVector PointPlaneProject(const FVector& Point, const FVector& A, const FVector& B, const FVector& C);

/**
* Calculate the projection of a point on the plane defined by PlaneBase and PlaneNormal.
*
* @param Point The point to project onto the plane
* @param PlaneBase Point on the plane
* @param PlaneNorm Normal of the plane (assumed to be unit length).
* @return Projection of Point onto plane
*/
static FVector PointPlaneProject(const FVector& Point, const FVector& PlaneBase, const FVector& PlaneNormal);

向量投影到平面,一样的原理

inline FVector FVector::VectorPlaneProject(const FVector& V, const FVector& PlaneNormal)
{
	return V - V.ProjectOnToNormal(PlaneNormal);
}

空间关系

向量平行,通过判断cos值和THRESH_NORMALS_ARE_PARALLEL (0.999845f)的大小,可能同向也可能反向。

/**
 * See if two normal vectors are nearly parallel, meaning the angle between them is close to 0 degrees.
 *
 * @param  Normal1 First normalized vector.
 * @param  Normal1 Second normalized vector.
 * @param  ParallelCosineThreshold Normals are parallel if absolute value of dot product (cosine of angle between them) is greater than or equal to this. For example: cos(1.0 degrees).
 * @return true if vectors are nearly parallel, false otherwise.
 */
static bool Parallel(const FVector& Normal1, const FVector& Normal2, float ParallelCosineThreshold = THRESH_NORMALS_ARE_PARALLEL);

向量同向,和上面的类似

/**
 * See if two normal vectors are coincident (nearly parallel and point in the same direction).
 * 
 * @param  Normal1 First normalized vector.
 * @param  Normal2 Second normalized vector.
 * @param  ParallelCosineThreshold Normals are coincident if dot product (cosine of angle between them) is greater than or equal to this. For example: cos(1.0 degrees).
 * @return true if vectors are coincident (nearly parallel and point in the same direction), false otherwise.
 */
static bool Coincident(const FVector& Normal1, const FVector& Normal2, float ParallelCosineThreshold = THRESH_NORMALS_ARE_PARALLEL);

向量正交,判断cos值和THRESH_NORMALS_ARE_ORTHOGONAL (0.017455f)的大小。

/**
 * See if two normal vectors are nearly orthogonal (perpendicular), meaning the angle between them is close to 90 degrees.
 * 
 * @param  Normal1 First normalized vector.
 * @param  Normal2 Second normalized vector.
 * @param  OrthogonalCosineThreshold Normals are orthogonal if absolute value of dot product (cosine of angle between them) is less than or equal to this. For example: cos(89.0 degrees).
 * @return true if vectors are orthogonal (perpendicular), false otherwise.
 */
static bool Orthogonal(const FVector& Normal1, const FVector& Normal2, float OrthogonalCosineThreshold = THRESH_NORMALS_ARE_ORTHOGONAL);

平面共面,算法是

  • 若法向量不平行,则平面不共面;
  • 若法向量平行,计算平面1的点到平面2的距离,距离在阈值内的可以认定为共面;
/**
 * See if two planes are coplanar. They are coplanar if the normals are nearly parallel and the planes include the same set of points.
 *
 * @param Base1 The base point in the first plane.
 * @param Normal1 The normal of the first plane.
 * @param Base2 The base point in the second plane.
 * @param Normal2 The normal of the second plane.
 * @param ParallelCosineThreshold Normals are parallel if absolute value of dot product is greater than or equal to this.
 * @return true if the planes are coplanar, false otherwise.
 */
static bool Coplanar(const FVector& Base1, const FVector& Normal1, const FVector& Base2, const FVector& Normal2, float ParallelCosineThreshold = THRESH_NORMALS_ARE_PARALLEL);
{
	if      (!FVector::Parallel(Normal1,Normal2,ParallelCosineThreshold)) return false;
	else if (FVector::PointPlaneDist (Base2,Base1,Normal1) > THRESH_POINT_ON_PLANE) return false;
	else return true;
}

向量三重积

/**
 * Triple product of three vectors: X dot (Y cross Z).
 *
 * @param X The first vector.
 * @param Y The second vector.
 * @param Z The third vector.
 * @return The triple product: X dot (Y cross Z).
 */
static float Triple(const FVector& X, const FVector& Y, const FVector& Z);

贝塞尔曲线

三次方贝塞尔曲线

技术图片
/**
 * Generates a list of sample points on a Bezier curve defined by 2 points.
 *
 * @param ControlPoints	Array of 4 FVectors (vert1, controlpoint1, controlpoint2, vert2).
 * @param NumPoints Number of samples.
 * @param OutPoints Receives the output samples.
 * @return The path length.
 */
static CORE_API float EvaluateBezier(const FVector* ControlPoints, int32 NumPoints, TArray<FVector>& OutPoints);

点到AABB的距离

平面一共九个区域,AABB内部为0,外部根据不同的区域最近距离很容易看出来,最后整合即可。

技术图片
/** 
 * Util to calculate distance from a point to a bounding box 
 *
 * @param Mins 3D Point defining the lower values of the axis of the bound box
 * @param Max 3D Point defining the lower values of the axis of the bound box
 * @param Point 3D position of interest
 * @return the distance from the Point to the bounding box.
 */
FORCEINLINE float ComputeSquaredDistanceFromBoxToPoint(const FVector& Mins, const FVector& Maxs, const FVector& Point)
{
	// Accumulates the distance as we iterate axis
	float DistSquared = 0.f;

	// Check each axis for min/max and add the distance accordingly
	// NOTE: Loop manually unrolled for > 2x speed up
	if (Point.X < Mins.X)
	{
		DistSquared += FMath::Square(Point.X - Mins.X);
	}
	else if (Point.X > Maxs.X)
	{
		DistSquared += FMath::Square(Point.X - Maxs.X);
	}
	
	if (Point.Y < Mins.Y)
	{
		DistSquared += FMath::Square(Point.Y - Mins.Y);
	}
	else if (Point.Y > Maxs.Y)
	{
		DistSquared += FMath::Square(Point.Y - Maxs.Y);
	}
	
	if (Point.Z < Mins.Z)
	{
		DistSquared += FMath::Square(Point.Z - Mins.Z);
	}
	else if (Point.Z > Maxs.Z)
	{
		DistSquared += FMath::Square(Point.Z - Maxs.Z);
	}
	
	return DistSquared;
}

Unreal Math: FVector(2)

标签:整合   equal   other   speed   define   com   ike   float   direction   

原文地址:https://www.cnblogs.com/haisong1991/p/11296111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!