| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | |
|
| | 6 | | namespace MorphoGeometry |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Intersection class. |
| | 10 | | /// </summary> |
| | 11 | | public class Intersection |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Ray face intersection. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="ray">Ray.</param> |
| | 17 | | /// <param name="face">Face.</param> |
| | 18 | | /// <param name="reverse">Reverse the face.</param> |
| | 19 | | /// <param name="project">Project the intersection points.</param> |
| | 20 | | /// <returns>Intersection point.</returns> |
| | 21 | | public static Vector RayFaceIntersect(Ray ray, Face face, |
| | 22 | | bool reverse = false, bool project = false) |
| 0 | 23 | | { |
| | 24 | | Vector v0, v1, v2; |
| | 25 | |
|
| 0 | 26 | | if (reverse) |
| 0 | 27 | | { |
| 0 | 28 | | v1 = face.A; |
| 0 | 29 | | v0 = face.B; |
| 0 | 30 | | v2 = face.C; |
| 0 | 31 | | } |
| | 32 | | else |
| 0 | 33 | | { |
| 0 | 34 | | v0 = face.A; |
| 0 | 35 | | v1 = face.B; |
| 0 | 36 | | v2 = face.C; |
| 0 | 37 | | } |
| | 38 | |
|
| 0 | 39 | | Vector v0v1 = v1.Sub(v0); |
| 0 | 40 | | Vector v0v2 = v2.Sub(v0); |
| 0 | 41 | | Vector pvec = ray.direction.Cross(v0v2); |
| 0 | 42 | | float det = v0v1.Dot(pvec); |
| | 43 | |
|
| 0 | 44 | | if (det < 0.000001) |
| 0 | 45 | | return null; |
| | 46 | |
|
| 0 | 47 | | float invDet = (float)(1.0 / det); |
| 0 | 48 | | Vector tvec = ray.origin.Sub(v0); |
| 0 | 49 | | float u = tvec.Dot(pvec) * invDet; |
| | 50 | |
|
| 0 | 51 | | if (u < 0 || u > 1) |
| 0 | 52 | | return null; |
| | 53 | |
|
| 0 | 54 | | Vector qvec = tvec.Cross(v0v1); |
| 0 | 55 | | float v = ray.direction.Dot(qvec) * invDet; |
| | 56 | |
|
| 0 | 57 | | if (v < 0 || u + v > 1) |
| 0 | 58 | | return null; |
| | 59 | |
|
| 0 | 60 | | float distance = (v0v2.Dot(qvec) * invDet); |
| 0 | 61 | | float c = (float) Math.Sqrt(ray.direction.x * |
| 0 | 62 | | ray.direction.x + ray.direction.y * ray.direction.y + |
| 0 | 63 | | ray.direction.z * ray.direction.z); |
| 0 | 64 | | float angle = distance / c; |
| | 65 | |
|
| 0 | 66 | | Vector intersection = new Vector(ray.origin.x + |
| 0 | 67 | | ray.direction.x * angle, ray.origin.y + |
| 0 | 68 | | ray.direction.y * angle, ray.origin.z + ray.direction.z * angle); |
| | 69 | |
|
| 0 | 70 | | if (project) |
| 0 | 71 | | intersection = new Vector(ray.origin.x, ray.origin.y, ray.origin.z); |
| | 72 | |
|
| 0 | 73 | | return intersection; |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Rays facegroup intersection. |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="rays">Rays.</param> |
| | 80 | | /// <param name="facegroup">Facegroup to hit.</param> |
| | 81 | | /// <param name="RayMethod">Intersection method.</param> |
| | 82 | | /// <param name="reverse">Reverse the faces.</param> |
| | 83 | | /// <param name="project">Project the intersection points.</param> |
| | 84 | | /// <returns>Intersection points.</returns> |
| | 85 | | public static IEnumerable<Vector> RaysFaceGroupIntersect(IEnumerable<Ray> rays, |
| | 86 | | FaceGroup facegroup, |
| | 87 | | Func<Ray, Face, bool, bool, Vector> RayMethod, |
| | 88 | | bool reverse = false, |
| | 89 | | bool project = false) |
| 0 | 90 | | { |
| 0 | 91 | | var faceIntersectArr = new List<Vector>[facegroup.Faces.Count]; |
| | 92 | |
|
| 0 | 93 | | Parallel.For(0, facegroup.Faces.Count, i => |
| 0 | 94 | | { |
| 0 | 95 | | var partial = new List<Vector>(); |
| 0 | 96 | | var face = facegroup.Faces[i]; |
| 0 | 97 | | var min = face.Min(); |
| 0 | 98 | | var max = face.Max(); |
| 0 | 99 | |
|
| 0 | 100 | | foreach (var ray in rays) |
| 0 | 101 | | { |
| 0 | 102 | | if (ray.origin.x < min.x) continue; |
| 0 | 103 | | if (ray.origin.y < min.y) continue; |
| 0 | 104 | | if (ray.origin.x > max.x) continue; |
| 0 | 105 | | if (ray.origin.y > max.y) continue; |
| 0 | 106 | |
|
| 0 | 107 | | var intersection = RayMethod(ray, face, reverse, project); |
| 0 | 108 | | if (intersection != null) |
| 0 | 109 | | { |
| 0 | 110 | | partial.Add(intersection); |
| 0 | 111 | | } |
| 0 | 112 | | } |
| 0 | 113 | | faceIntersectArr[i] = partial; |
| 0 | 114 | | }); |
| 0 | 115 | | var intersections = faceIntersectArr |
| 0 | 116 | | .SelectMany(i => i) |
| 0 | 117 | | .Where(_ => _ != null); |
| | 118 | |
|
| 0 | 119 | | return intersections; |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Ray face intersection both face directions. |
| | 124 | | /// </summary> |
| | 125 | | /// <param name="ray">Ray.</param> |
| | 126 | | /// <param name="face">Face.</param> |
| | 127 | | /// <param name="reverse">Reverse the face.</param> |
| | 128 | | /// <param name="project">Project intersection points.</param> |
| | 129 | | /// <returns>Intersection point.</returns> |
| | 130 | | public static Vector RayFaceIntersectFrontBack(Ray ray, |
| | 131 | | Face face, bool reverse, bool project) |
| 0 | 132 | | { |
| 0 | 133 | | if (face.IsPointBehind(ray.origin) < 0) |
| 0 | 134 | | { |
| 0 | 135 | | return RayFaceIntersect(ray, face, true, project); |
| | 136 | | } |
| | 137 | | else |
| 0 | 138 | | { |
| 0 | 139 | | return RayFaceIntersect(ray, face, reverse, project); |
| | 140 | | } |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | /// <summary> |
| | 144 | | /// Is point inside a facegroup. |
| | 145 | | /// </summary> |
| | 146 | | /// <param name="vectors">Points to test.</param> |
| | 147 | | /// <param name="facegroup">Facegroup.</param> |
| | 148 | | /// <returns>Inside points.</returns> |
| | 149 | | public static IEnumerable<Vector> IsPointInsideFaceGroup(List<Vector> vectors, |
| | 150 | | FaceGroup facegroup) |
| 0 | 151 | | { |
| 0 | 152 | | var points = new Vector[vectors.Count()]; |
| | 153 | |
|
| 0 | 154 | | for (int i = 0; i < vectors.Count; i++) |
| 0 | 155 | | { |
| 0 | 156 | | if (IsPointInside(facegroup, vectors[i])) |
| 0 | 157 | | { |
| 0 | 158 | | points[i] = vectors[i]; |
| 0 | 159 | | } |
| 0 | 160 | | } |
| 0 | 161 | | var intersections = points |
| 0 | 162 | | .Where(_ => _ != null); |
| | 163 | |
|
| 0 | 164 | | return intersections; |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | /// <summary> |
| | 168 | | /// Is point inside a facegroup. |
| | 169 | | /// </summary> |
| | 170 | | /// <param name="facegroup">Facegroup.</param> |
| | 171 | | /// <param name="point">Point to test.</param> |
| | 172 | | /// <returns>True or false.</returns> |
| | 173 | | public static bool IsPointInside(FaceGroup facegroup, Vector point) |
| 0 | 174 | | { |
| 0 | 175 | | var triangles = facegroup.ToArray(); |
| 0 | 176 | | var P = point.ToArray(); |
| | 177 | |
|
| 0 | 178 | | foreach (var tri in triangles) |
| 0 | 179 | | foreach (var pt in tri) |
| 0 | 180 | | for (var i = 0; i < pt.Length; i++) |
| 0 | 181 | | pt[i] -= P[i]; |
| | 182 | |
|
| 0 | 183 | | var qM = new float[triangles.Length][][]; |
| 0 | 184 | | var Mnorm = new float[triangles.Length][]; |
| 0 | 185 | | for (var i = 0; i < triangles.Length; i++) |
| 0 | 186 | | { |
| 0 | 187 | | var tri = triangles[i]; |
| 0 | 188 | | qM[i] = new float[tri.Length][]; |
| 0 | 189 | | Mnorm[i] = new float[tri.Length]; |
| 0 | 190 | | for (var j = 0; j < tri.Length; j++) |
| 0 | 191 | | { |
| 0 | 192 | | var pt = tri[j]; |
| 0 | 193 | | qM[i][j] = new float[pt.Length]; |
| 0 | 194 | | for (int k = 0; k < pt.Length; k++) |
| 0 | 195 | | { |
| 0 | 196 | | qM[i][j][k] = Convert.ToSingle(Math.Pow((double)pt[k], |
| 0 | 197 | | (double)2)); |
| 0 | 198 | | } |
| 0 | 199 | | Mnorm[i][j] = Convert.ToSingle(Math |
| 0 | 200 | | .Sqrt((double)qM[i][j].Sum())); |
| 0 | 201 | | } |
| 0 | 202 | | } |
| | 203 | |
|
| 0 | 204 | | var winding_number = 0.0; |
| 0 | 205 | | for (var i = 0; i < Mnorm.Length; i++) |
| 0 | 206 | | { |
| 0 | 207 | | triangles[i].Deconstruct(out float[] A, out float[] B, out float[] C); |
| 0 | 208 | | Mnorm[i].Deconstruct(out float a, out float b, out float c); |
| | 209 | |
|
| 0 | 210 | | var j = (a * b * c); |
| 0 | 211 | | var jj = c * (Vector.FromArray(A) |
| 0 | 212 | | .Dot(Vector.FromArray(B))); |
| 0 | 213 | | var jjj = a * (Vector.FromArray(B) |
| 0 | 214 | | .Dot(Vector.FromArray(C))); |
| 0 | 215 | | var jjjj = b * (Vector.FromArray(C) |
| 0 | 216 | | .Dot(Vector.FromArray(A))); |
| | 217 | |
|
| 0 | 218 | | var tot = j + jj + jjj + jjjj; |
| | 219 | |
|
| 0 | 220 | | var det = Vector.Det3x3(new float[3][] { A, B, C }); |
| 0 | 221 | | winding_number += Math.Atan2((double)det, (double)tot - 1); |
| | 222 | |
|
| 0 | 223 | | } |
| 0 | 224 | | return winding_number >= (2.0 * Math.PI); |
| 0 | 225 | | } |
| | 226 | | } |
| | 227 | | } |