forked from veldrid/veldrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundingSphere.cs
75 lines (63 loc) · 2.07 KB
/
BoundingSphere.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Numerics;
namespace Veldrid.Utilities
{
public struct BoundingSphere
{
public Vector3 Center;
public float Radius;
public BoundingSphere(Vector3 center, float radius)
{
Center = center;
Radius = radius;
}
public override string ToString()
{
return string.Format("Center:{0}, Radius:{1}", Center, Radius);
}
public bool Contains(Vector3 point)
{
return (Center - point).LengthSquared() <= Radius * Radius;
}
public static BoundingSphere CreateFromPoints(IList<Vector3> points)
{
Vector3 center = Vector3.Zero;
foreach (Vector3 pt in points)
{
center += pt;
}
center /= points.Count;
float maxDistanceSquared = 0f;
foreach (Vector3 pt in points)
{
float distSq = Vector3.DistanceSquared(center, pt);
if (distSq > maxDistanceSquared)
{
maxDistanceSquared = distSq;
}
}
return new BoundingSphere(center, (float)Math.Sqrt(maxDistanceSquared));
}
public static unsafe BoundingSphere CreateFromPoints(Vector3* pointPtr, int numPoints, int stride)
{
Vector3 center = Vector3.Zero;
StrideHelper<Vector3> helper = new StrideHelper<Vector3>(pointPtr, numPoints, stride);
foreach (Vector3 pos in helper)
{
center += pos;
}
center /= numPoints;
float maxDistanceSquared = 0f;
foreach (Vector3 pos in helper)
{
float distSq = Vector3.DistanceSquared(center, pos);
if (distSq > maxDistanceSquared)
{
maxDistanceSquared = distSq;
}
}
return new BoundingSphere(center, (float)Math.Sqrt(maxDistanceSquared));
}
}
}