|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace g3 |
| 7 | +{ |
| 8 | + // ported from WildMagic 5 |
| 9 | + // https://www.geometrictools.com/Downloads/Downloads.html |
| 10 | + |
| 11 | + public class DistPoint3Cylinder3 |
| 12 | + { |
| 13 | + Vector3d point; |
| 14 | + public Vector3d Point |
| 15 | + { |
| 16 | + get { return point; } |
| 17 | + set { point = value; DistanceSquared = -1.0; } |
| 18 | + } |
| 19 | + |
| 20 | + Cylinder3d cylinder; |
| 21 | + public Cylinder3d Cylinder |
| 22 | + { |
| 23 | + get { return cylinder; } |
| 24 | + set { cylinder = value; DistanceSquared = -1.0; } |
| 25 | + } |
| 26 | + |
| 27 | + public double DistanceSquared = -1.0; |
| 28 | + |
| 29 | + public double SignedDistance = 0.0f; |
| 30 | + public bool IsInside { get { return SignedDistance < 0; } } |
| 31 | + |
| 32 | + public Vector3d CylinderClosest; |
| 33 | + |
| 34 | + |
| 35 | + public DistPoint3Cylinder3(Vector3d PointIn, Cylinder3d CylinderIn ) |
| 36 | + { |
| 37 | + point = PointIn; cylinder = CylinderIn; |
| 38 | + } |
| 39 | + |
| 40 | + public DistPoint3Cylinder3 Compute() |
| 41 | + { |
| 42 | + GetSquared(); |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + public double Get() |
| 47 | + { |
| 48 | + return Math.Sqrt(GetSquared()); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public double GetSquared() |
| 53 | + { |
| 54 | + if (DistanceSquared >= 0) |
| 55 | + return DistanceSquared; |
| 56 | + |
| 57 | + |
| 58 | + if (cylinder.Height >= double.MaxValue) |
| 59 | + return get_squared_infinite(); |
| 60 | + |
| 61 | + |
| 62 | + // Convert the point to the cylinder coordinate system. In this system, |
| 63 | + // the point believes (0,0,0) is the cylinder axis origin and (0,0,1) is |
| 64 | + // the cylinder axis direction. |
| 65 | + Vector3d basis0 = cylinder.Axis.Direction; |
| 66 | + Vector3d basis1 = Vector3d.Zero, basis2 = Vector3d.Zero; |
| 67 | + Vector3d.ComputeOrthogonalComplement(1, basis0, ref basis1, ref basis2); |
| 68 | + double height = Cylinder.Height / 2.0; |
| 69 | + |
| 70 | + Vector3d delta = point - cylinder.Axis.Origin; |
| 71 | + Vector3d P = new Vector3d(basis1.Dot(delta), basis2.Dot(delta), basis0.Dot(delta)); |
| 72 | + |
| 73 | + double result_distance = 0; // signed! |
| 74 | + Vector3d result_closest = Vector3d.Zero; |
| 75 | + |
| 76 | + double sqrRadius = cylinder.Radius * cylinder.Radius; |
| 77 | + double sqrDistance = P[0] * P[0] + P[1] * P[1]; |
| 78 | + |
| 79 | + // The point is outside the infinite cylinder, or on the cylinder wall. |
| 80 | + double distance = Math.Sqrt(sqrDistance); |
| 81 | + double inf_distance = distance - Cylinder.Radius; |
| 82 | + double temp = Cylinder.Radius / distance; |
| 83 | + Vector3d inf_closest = new Vector3d(temp * P.x, temp * P.y, P.z); |
| 84 | + bool bOutside = (sqrDistance >= sqrRadius); |
| 85 | + |
| 86 | + result_closest = inf_closest; |
| 87 | + result_distance = inf_distance; |
| 88 | + |
| 89 | + if ( inf_closest.z >= height ) { |
| 90 | + result_closest = (bOutside) ? inf_closest : P; |
| 91 | + result_closest.z = height; |
| 92 | + result_distance = result_closest.Distance(P); // TODO: only compute sqr here |
| 93 | + bOutside = true; |
| 94 | + } else if ( inf_closest.z <= -height ) { |
| 95 | + result_closest = (bOutside) ? inf_closest : P; |
| 96 | + result_closest.z = -height; |
| 97 | + result_distance = result_closest.Distance(P); // TODO: only compute sqr here |
| 98 | + bOutside = true; |
| 99 | + } else if ( bOutside == false ) { |
| 100 | + if (inf_closest.z > 0 && Math.Abs(inf_closest.z - height) < Math.Abs(inf_distance)) { |
| 101 | + result_closest = P; |
| 102 | + result_closest.z = height; |
| 103 | + result_distance = result_closest.Distance(P); // TODO: only compute sqr here |
| 104 | + } else if ( inf_closest.z < 0 && Math.Abs(inf_closest.z - -height) < Math.Abs(inf_distance) ) { |
| 105 | + result_closest = P; |
| 106 | + result_closest.z = -height; |
| 107 | + result_distance = result_closest.Distance(P); // TODO: only compute sqr here |
| 108 | + } |
| 109 | + } |
| 110 | + SignedDistance = (bOutside) ? Math.Abs(result_distance) : -Math.Abs(result_distance); |
| 111 | + |
| 112 | + // Convert the closest point from the cylinder coordinate system to the |
| 113 | + // original coordinate system. |
| 114 | + CylinderClosest = cylinder.Axis.Origin + |
| 115 | + result_closest.x * basis1 + |
| 116 | + result_closest.y * basis2 + |
| 117 | + result_closest.z * basis0; |
| 118 | + |
| 119 | + DistanceSquared = result_distance * result_distance; |
| 120 | + |
| 121 | + return DistanceSquared; |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + public double get_squared_infinite() |
| 127 | + { |
| 128 | + // Convert the point to the cylinder coordinate system. In this system, |
| 129 | + // the point believes (0,0,0) is the cylinder axis origin and (0,0,1) is |
| 130 | + // the cylinder axis direction. |
| 131 | + Vector3d basis0 = cylinder.Axis.Direction; |
| 132 | + Vector3d basis1 = Vector3d.Zero, basis2 = Vector3d.Zero; |
| 133 | + Vector3d.ComputeOrthogonalComplement(1, basis0, ref basis1, ref basis2); |
| 134 | + |
| 135 | + Vector3d delta = point - cylinder.Axis.Origin; |
| 136 | + Vector3d P = new Vector3d(basis1.Dot(delta), basis2.Dot(delta), basis0.Dot(delta)); |
| 137 | + |
| 138 | + double result_distance = 0; // signed! |
| 139 | + Vector3d result_closest = Vector3d.Zero; |
| 140 | + |
| 141 | + double sqrRadius = cylinder.Radius * cylinder.Radius; |
| 142 | + double sqrDistance = P[0] * P[0] + P[1] * P[1]; |
| 143 | + |
| 144 | + // The point is outside the cylinder or on the cylinder wall. |
| 145 | + double distance = Math.Sqrt(sqrDistance); |
| 146 | + result_distance = distance - Cylinder.Radius; |
| 147 | + double temp = Cylinder.Radius / distance; |
| 148 | + result_closest = new Vector3d(temp * P.x, temp * P.y, P.z); |
| 149 | + |
| 150 | + |
| 151 | + // Convert the closest point from the cylinder coordinate system to the |
| 152 | + // original coordinate system. |
| 153 | + CylinderClosest = cylinder.Axis.Origin + |
| 154 | + result_closest.x * basis1 + |
| 155 | + result_closest.y * basis2 + |
| 156 | + result_closest.z * basis0; |
| 157 | + SignedDistance = result_distance; |
| 158 | + DistanceSquared = result_distance * result_distance; |
| 159 | + return DistanceSquared; |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | +} |
0 commit comments