4 using System.Collections.Generic;
19 [
System.Obsolete(
"Use CameraExtensions.GetHorizontalFieldOfViewRadians(Camera camera) instead.")]
30 [
System.Obsolete(
"Use CameraExtensions.IsInFOV(Camera camera, Vector3 position) instead.")]
31 public static bool IsInFOV(Vector3 position)
46 Vector3 ptInWorld = (from == null) ? ptInFrom : from.TransformPoint(ptInFrom);
47 Vector3 ptInTo = (to == null) ? ptInWorld : to.InverseTransformPoint(ptInWorld);
61 Vector3 dirInWorld = (from == null) ? dirInFrom : from.TransformDirection(dirInFrom);
62 Vector3 dirInTo = (to == null) ? dirInWorld : to.InverseTransformDirection(dirInWorld);
76 Vector3 vecInWorld = (from == null) ? vecInFrom : from.TransformVector(vecInFrom);
77 Vector3 vecInTo = (to == null) ? vecInWorld : to.InverseTransformVector(vecInWorld);
86 Ray outputRay =
new Ray
88 origin = TransformPointFromTo(from, to, rayToConvert.origin),
89 direction = TransformDirectionFromTo(from, to, rayToConvert.direction)
105 Quaternion q =
new Quaternion();
106 q.w = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] + m[1, 1] + m[2, 2])) / 2;
107 q.x = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] - m[1, 1] - m[2, 2])) / 2;
108 q.y = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] + m[1, 1] - m[2, 2])) / 2;
109 q.z = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] - m[1, 1] + m[2, 2])) / 2;
110 q.x *= Mathf.Sign(q.x * (m[2, 1] - m[1, 2]));
111 q.y *= Mathf.Sign(q.y * (m[0, 2] - m[2, 0]));
112 q.z *= Mathf.Sign(q.z * (m[1, 0] - m[0, 1]));
121 Vector3 upwards =
new Vector3(unityMtx.m01, unityMtx.m11, unityMtx.m21);
122 Vector3 forward =
new Vector3(unityMtx.m02, unityMtx.m12, unityMtx.m22);
123 translation =
new Vector3(unityMtx.m03, unityMtx.m13, unityMtx.m23);
124 rotation = Quaternion.LookRotation(forward, upwards);
134 return new Vector3(v.x, 0.0f, v.z);
144 return new Vector3(0.0f, v.y, v.z);
154 return new Vector3(v.x, v.y, 0.0f);
166 Vector3 closestPoint = ClosestPointOnLineToPoint(point, linePointA, linePointB);
167 return (point - closestPoint).magnitude;
172 Vector3 v = linePointB - linePointA;
173 Vector3 w = point - linePointA;
175 float c1 = Vector3.Dot(w, v);
176 float c2 = Vector3.Dot(v, v);
179 Vector3 pointB = linePointA + (v * b);
186 Vector3 closestPoint = ClosestPointOnLineSegmentToPoint(point, lineStart, lineEnd);
187 return (point - closestPoint).magnitude;
192 Vector3 v = lineEnd - lineStart;
193 Vector3 w = point - lineStart;
195 float c1 = Vector3.Dot(w, v);
201 float c2 = Vector3.Dot(v, v);
209 Vector3 pointB = lineStart + (v * b);
214 public static bool TestPlanesAABB(Plane[] planes,
int planeMask, Bounds bounds, out
bool entirelyInside)
217 int entirelyInsideCount = 0;
218 Vector3 boundsCenter = bounds.center;
219 Vector3 boundsExtent = bounds.extents;
224 while (mask <= planeMask)
227 if ((uint)(planeMask & mask) != 0)
229 Plane p = planes[planeIndex];
230 Vector3 n = p.normal;
231 n.x = Mathf.Abs(n.x);
232 n.y = Mathf.Abs(n.y);
233 n.z = Mathf.Abs(n.z);
235 float distance = p.GetDistanceToPoint(boundsCenter);
236 float radius = Vector3.Dot(boundsExtent, n);
238 if (distance + radius < 0)
241 entirelyInside =
false;
245 if (distance > radius)
247 entirelyInsideCount++;
255 entirelyInside = entirelyInsideCount == planes.Length;
266 public static bool InRange(Vector2 vec, Vector2 lower, Vector2 upper)
268 return vec.x >= lower.x && vec.x <= upper.x && vec.y >= lower.y && vec.y <= upper.y;
278 public static bool InRange(Vector3 vec, Vector3 lower, Vector3 upper)
280 return vec.x >= lower.x && vec.x <= upper.x && vec.y >= lower.y && vec.y <= upper.y && vec.z >= lower.z && vec.z <= upper.z;
289 public static Matrix4x4
Add(Matrix4x4 a, Matrix4x4 b)
291 Matrix4x4 result =
new Matrix4x4();
292 result.SetColumn(0, a.GetColumn(0) + b.GetColumn(0));
293 result.SetColumn(1, a.GetColumn(1) + b.GetColumn(1));
294 result.SetColumn(2, a.GetColumn(2) + b.GetColumn(2));
295 result.SetColumn(3, a.GetColumn(3) + b.GetColumn(3));
305 public static Matrix4x4
Subtract(Matrix4x4 a, Matrix4x4 b)
307 Matrix4x4 result =
new Matrix4x4();
308 result.SetColumn(0, a.GetColumn(0) - b.GetColumn(0));
309 result.SetColumn(1, a.GetColumn(1) - b.GetColumn(1));
310 result.SetColumn(2, a.GetColumn(2) - b.GetColumn(2));
311 result.SetColumn(3, a.GetColumn(3) - b.GetColumn(3));
323 return Vector3.Cross(ray.direction, point - ray.origin).magnitude;
334 float a = Vector3.Dot(p.direction, p.direction);
335 float b = Vector3.Dot(p.direction, q.direction);
336 float c = Vector3.Dot(q.direction, q.direction);
337 Vector3 w0 = p.origin - q.origin;
338 float den = a * c - b * b;
339 float epsilon = 0.00001f;
343 return 0.5f * (p.origin + q.origin);
345 float d = Vector3.Dot(p.direction, w0);
346 float e = Vector3.Dot(q.direction, w0);
347 float sc = (b * e - c * d) / den;
348 float tc = (a * e - b * d) / den;
349 Vector3 point = 0.5f * (p.origin + sc * p.direction + q.origin + tc * q.direction);
365 public static Vector3
NearestPointToLinesRANSAC(List<Ray> rays,
int ransac_iterations,
float ransac_threshold, out
int numActualInliers)
368 Vector3 nearestPoint = NearestPointToLines(rays[0], rays[rays.Count - 1]);
369 numActualInliers = 0;
372 for (
int it = 0; it < ransac_iterations; it++)
374 Vector3 testPoint = NearestPointToLines(rays[
Random.Range(0, rays.Count)], rays[
Random.Range(0, rays.Count)]);
377 int numInliersForIteration = 0;
378 for (
int ind = 0; ind < rays.Count; ++ind)
380 if (DistanceOfPointToLine(rays[ind], testPoint) < ransac_threshold)
381 ++numInliersForIteration;
385 if (numInliersForIteration > numActualInliers)
387 numActualInliers = numInliersForIteration;
388 nearestPoint = testPoint;
394 var inlierList = rays.Where(r => DistanceOfPointToLine(r, nearestPoint) < ransac_threshold);
395 numActualInliers = inlierList.Count();
396 if (numActualInliers >= 2)
398 nearestPoint = NearestPointToLinesLeastSquares(inlierList);
422 Matrix4x4 sumOfProduct = Matrix4x4.zero;
423 Vector4 sumOfProductTimesDirection = Vector4.zero;
425 foreach (Ray r
in rays)
427 Vector4 point = r.origin;
428 Matrix4x4 directionColumnMatrix =
new Matrix4x4();
429 Vector3 rNormal = r.direction.normalized;
430 directionColumnMatrix.SetColumn(0, rNormal);
431 Matrix4x4 directionRowMatrix = directionColumnMatrix.transpose;
432 Matrix4x4 product = directionColumnMatrix * directionRowMatrix;
433 Matrix4x4 identityMinusDirectionProduct = Subtract(Matrix4x4.identity, product);
434 sumOfProduct = Add(sumOfProduct, identityMinusDirectionProduct);
435 Vector4 vectorProduct = identityMinusDirectionProduct * point;
436 sumOfProductTimesDirection += vectorProduct;
439 Matrix4x4 sumOfProductInverse = sumOfProduct.inverse;
440 Vector3 nearestPoint = sumOfProductInverse * sumOfProductTimesDirection;