|
| 1 | +package math; |
| 2 | + |
| 3 | +/** |
| 4 | + * A collection of common analytical geometry. |
| 5 | + * |
| 6 | + * @author Simon |
| 7 | + * @version 0.2, 30 May 2016 |
| 8 | + * |
| 9 | + */ |
| 10 | +public class GeometryUtil { |
| 11 | + |
| 12 | + /** |
| 13 | + * Calculates the centroid (center of mass) of a triangle defined by three |
| 14 | + * points. |
| 15 | + * |
| 16 | + * The centroid is computed as the average of the x, y, and z coordinates of |
| 17 | + * the three vertices. |
| 18 | + * |
| 19 | + * @param a The first vertex of the triangle. |
| 20 | + * @param b The second vertex of the triangle. |
| 21 | + * @param c The third vertex of the triangle. |
| 22 | + * @return The centroid of the triangle as a new Vector3f. |
| 23 | + */ |
| 24 | + public static Vector3f calculateCentroid(Vector3f a, Vector3f b, |
| 25 | + Vector3f c) { |
| 26 | + float x = (a.x + b.x + c.x) / 3f; |
| 27 | + float y = (a.y + b.y + c.y) / 3f; |
| 28 | + float z = (a.z + b.z + c.z) / 3f; |
| 29 | + return new Vector3f(x, y, z); |
| 30 | + } |
| 31 | + |
| 32 | + // public static Vector2f calculateIntersection(Ray2D ray, Bounds2D bounds) |
| 33 | + // { |
| 34 | + // return null; |
| 35 | + // } |
| 36 | + |
| 37 | + // public static Vector2f calculateIntersection(Ray2D ray, Bounds2D bounds) |
| 38 | + // { |
| 39 | + // float a = bounds.getWidth(); |
| 40 | + // float b = bounds.getHeight(); |
| 41 | + // float x0 = ray.origin.x; |
| 42 | + // float y0 = ray.origin.y; |
| 43 | + // float rx = bounds.getCenterX(); |
| 44 | + // float ry = bounds.getCenterY(); |
| 45 | + // float vx = ray.direction.x; |
| 46 | + // float vy = ray.direction.y; |
| 47 | + // float t = (a * (x0 - rx) + b * (y0 - ry)) / (a * vx + b * vy); |
| 48 | + // |
| 49 | + // return null; |
| 50 | + // } |
| 51 | + |
| 52 | + /** |
| 53 | + * Calculates the center of the polygon specified by the four points a, b, c |
| 54 | + * and d. |
| 55 | + * |
| 56 | + * @param a the first point of the polygon |
| 57 | + * @param b the second point of the polygon |
| 58 | + * @param c the third point of the polygon |
| 59 | + * @param d the fourth point of the polygon |
| 60 | + * @return the center point of the polygon as {@link Vector2f} |
| 61 | + */ |
| 62 | + public static Vector2f calculateCenter(Vector2f a, Vector2f b, Vector2f c, |
| 63 | + Vector2f d) { |
| 64 | + // Vector2f center = a.add(b).add(c).add(d).mult(0.25f); |
| 65 | + // return center; |
| 66 | + // Implemented to avoid to many object creations |
| 67 | + float x = (a.x + b.x + c.x + d.x) * 0.25f; |
| 68 | + float y = (a.y + b.y + c.y + d.y) * 0.25f; |
| 69 | + return new Vector2f(x, y); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Calculates the center of the polygon specified by the four points a, b, c |
| 74 | + * and d. |
| 75 | + * |
| 76 | + * @param a the first point of the polygon |
| 77 | + * @param b the second point of the polygon |
| 78 | + * @param c the third point of the polygon |
| 79 | + * @param d the fourth point of the polygon |
| 80 | + * @return the center point of the polygon as {@link Vector3f} |
| 81 | + */ |
| 82 | + public static Vector3f calculateCenter(Vector3f a, Vector3f b, Vector3f c, |
| 83 | + Vector3f d) { |
| 84 | + // Vector2f center = a.add(b).add(c).add(d).mult(0.25f); |
| 85 | + // return center; |
| 86 | + // Implemented to avoid to many object creations |
| 87 | + float x = (a.x + b.x + c.x + d.x) * 0.25f; |
| 88 | + float y = (a.y + b.y + c.y + d.y) * 0.25f; |
| 89 | + float z = (a.z + b.z + c.z + d.z) * 0.25f; |
| 90 | + return new Vector3f(x, y, z); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Calculates the intersection of a ray and circle. |
| 95 | + * |
| 96 | + * @param r the ray to test |
| 97 | + * @param circle the circle to test |
| 98 | + * @return the nearest intersection to the ray origin in positive direction |
| 99 | + * of the ray, or null if there is no intersection |
| 100 | + */ |
| 101 | + public static Vector2f calculateIntersection(Ray2D ray, Circle2D circle) { |
| 102 | + return calculateIntersection(ray, circle.getCenter(), |
| 103 | + circle.getRadius()); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Calculates the intersection of a ray and circle. |
| 108 | + * |
| 109 | + * @param ray the ray to test |
| 110 | + * @param center the center of the circle to test |
| 111 | + * @param radius the radius of the circle to test |
| 112 | + * @return the nearest intersection to the ray origin in positive direction |
| 113 | + * of the ray, or null if there is no intersection |
| 114 | + */ |
| 115 | + public static Vector2f calculateIntersection(Ray2D ray, Vector2f center, |
| 116 | + float radius) { |
| 117 | + // Taken from: |
| 118 | + // https://www.uninformativ.de/bin/RaytracingSchnitttests-76a577a-CC-BY.pdf |
| 119 | + // Which point on the ray is the most nearest to the circle |
| 120 | + float radius2 = radius * radius; |
| 121 | + float alpha = -ray.direction.dot(ray.origin.subtract(center)); |
| 122 | + Vector2f q = ray.getPoint(alpha); |
| 123 | + |
| 124 | + // Distance to the circle center |
| 125 | + q.subtractLocal(center); |
| 126 | + float distToCenter2 = q.lengthSquared(); |
| 127 | + |
| 128 | + if (distToCenter2 > radius2) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + // Using pythagorean theorem to get intersections |
| 133 | + float x = Mathf.sqrt(radius2 - distToCenter2); |
| 134 | + |
| 135 | + // Which of the intersections is nearer to the ray origin in positive |
| 136 | + // direction |
| 137 | + float dist = 0.0f; |
| 138 | + if (alpha >= x) { |
| 139 | + dist = alpha - x; |
| 140 | + } else if (alpha + x > 0) { |
| 141 | + dist = alpha + x; |
| 142 | + } else { |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + // The final intersection |
| 147 | + q = ray.getPoint(dist); |
| 148 | + return q; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Calculates the point on a circle. |
| 153 | + * |
| 154 | + * @param center the center of the circle |
| 155 | + * @param radius the radius of the circle |
| 156 | + * @param angle the angle of the point to return in radians |
| 157 | + * @param cw true to rotate clockwise, false to rotate counterclockwise |
| 158 | + * (ccw) |
| 159 | + * @return the newly created point |
| 160 | + */ |
| 161 | + public static Vector2f pointOnCircle(Vector2f center, float radius, |
| 162 | + float angle, boolean cw) { |
| 163 | + angle = cw ? angle : -angle; |
| 164 | + float x = (float) (center.x + radius * Math.cos(angle)); |
| 165 | + float y = (float) (center.y + radius * Math.sin(angle)); |
| 166 | + return new Vector2f(x, y); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Calculates the distribution point of a line segment. |
| 171 | + * |
| 172 | + * @param start the start point of the line segment |
| 173 | + * @param end the end point of the line segment |
| 174 | + * @param lambda the part ratio |
| 175 | + * @return the the distribution point |
| 176 | + */ |
| 177 | + public Vector2f getDistributionPoint(Vector2f start, Vector2f end, |
| 178 | + float lambda) { |
| 179 | + float scalar = 1f / (1f + lambda); |
| 180 | + return start.add(end.mult(lambda)).mult(scalar); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Calculates the main emphasis of a triangle. |
| 185 | + * |
| 186 | + * @param a the first point of the triangle |
| 187 | + * @param b the second point of the triangle |
| 188 | + * @param c the third point of the triangle |
| 189 | + * @return the main emphasis of the triangle described by a,b,c |
| 190 | + */ |
| 191 | + public static Vector2f getMainEmphasis(Vector2f a, Vector2f b, Vector2f c) { |
| 192 | + Vector2f m = a.add(b).add(c).mult(Mathf.ONE_THIRD); |
| 193 | + return m; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Returns the midpoint of the line segment. |
| 198 | + * |
| 199 | + * @param start the start point of the line segment |
| 200 | + * @param end the end point of the line segment |
| 201 | + * @return the midpoint of the line segment |
| 202 | + */ |
| 203 | + public static Vector2f getMidpoint(Vector2f start, Vector2f end) { |
| 204 | + // M = 1/2 * (A + B) |
| 205 | + Vector2f m = start.add(end).mult(0.5f); |
| 206 | + return m; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Returns the midpoint of the line segment. |
| 211 | + * |
| 212 | + * @param start the start point of the line segment |
| 213 | + * @param end the end point of the line segment |
| 214 | + * @return the midpoint of the line segment |
| 215 | + */ |
| 216 | + public static Vector3f getMidpoint(Vector3f start, Vector3f end) { |
| 217 | + // M = 1/2 * (A + B) |
| 218 | + Vector3f m = start.add(end).mult(0.5f); |
| 219 | + return m; |
| 220 | + } |
| 221 | + |
| 222 | + public static double angleBetweenVectors(Vector3f v1, Vector3f v2) { |
| 223 | + double dotProduct = v1.dot(v2); |
| 224 | + double magnitude1 = v1.length(); |
| 225 | + double magnitude2 = v2.length(); |
| 226 | + |
| 227 | + if (magnitude1 == 0 || magnitude2 == 0) { |
| 228 | + throw new IllegalArgumentException( |
| 229 | + "Vectors cannot have zero length"); |
| 230 | + } |
| 231 | + |
| 232 | + double cosTheta = dotProduct / (magnitude1 * magnitude2); |
| 233 | + double angle = Math.acos(cosTheta); |
| 234 | + |
| 235 | + return angle; |
| 236 | + } |
| 237 | + |
| 238 | +} |
0 commit comments