private static Vector3D Halfway(Vector3D v1, Vector3D v2) { Vector3D v1_ = Sterographic.PlaneToSphereSafe(v1); Vector3D v2_ = Sterographic.PlaneToSphereSafe(v2); Vector3D result = (v1_ + v2_) / 2; result.Normalize(); return(Sterographic.SphereToPlane(result)); }
public static Vector3D[] CalcViaProjections(Vector3D p1, Vector3D p2, Vector3D p3, int divisions, Geometry g) { if (g == Geometry.Euclidean) { throw new System.NotImplementedException(); } Vector3D h1 = new Vector3D(), h2 = new Vector3D(), h3 = new Vector3D(); if (g == Geometry.Hyperbolic) { h1 = Sterographic.PlaneToHyperboloid(p1); h2 = Sterographic.PlaneToHyperboloid(p2); h3 = Sterographic.PlaneToHyperboloid(p3); } else if (g == Geometry.Spherical) { h1 = Sterographic.PlaneToSphereSafe(p1); h2 = Sterographic.PlaneToSphereSafe(p2); h3 = Sterographic.PlaneToSphereSafe(p3); } List <Vector3D> temp = new List <Vector3D>(); Segment seg1 = Segment.Line(h1, h2); Segment seg2 = Segment.Line(h3, h2); Vector3D[] s1 = seg1.Subdivide(divisions); Vector3D[] s2 = seg2.Subdivide(divisions); for (int i = 0; i < divisions; i++) { Segment seg = Segment.Line(s1[i], s2[i]); temp.AddRange(seg.Subdivide(divisions - i)); } temp.Add(h2); List <Vector3D> result = new List <Vector3D>(); foreach (Vector3D v in temp) { Vector3D copy = v; if (g == Geometry.Hyperbolic) { Sterographic.NormalizeToHyperboloid(ref copy); result.Add(Sterographic.HyperboloidToPlane(copy)); } else if (g == Geometry.Spherical) { copy.Normalize(); result.Add(Sterographic.SphereToPlane(copy)); } } return(result.ToArray()); }
public static Vector3D Centroid(Geometry g, Vector3D[] conformalVerts) { if (g == Geometry.Euclidean) { Vector3D result = new Vector3D(); foreach (Vector3D v in conformalVerts) { result += v; } return(result / conformalVerts.Length); } Vector3D[] verts = conformalVerts.Select(v => { switch (g) { case Geometry.Spherical: return(Sterographic.PlaneToSphereSafe(v)); case Geometry.Hyperbolic: return(Sterographic.PlaneToHyperboloid(v)); } throw new System.ArgumentException(); }).ToArray(); // https://math.stackexchange.com/a/2173370/300001 Vector3D sum = new Vector3D(); for (int i = 0; i < verts.Length; i++) { sum += verts[i]; } Vector3D centroid = sum / Math.Sqrt(DotInGeometry(g, sum, sum)); NormalizeInGeometry(g, ref centroid); switch (g) { case Geometry.Spherical: return(Sterographic.SphereToPlane(centroid)); case Geometry.Hyperbolic: return(Sterographic.HyperboloidToPlane(centroid)); } throw new System.ArgumentException(); }
private static void ProjectAndSave(List <Circle3D> circlesOnUnitSphere) { List <Circle3D> projected = new List <Circle3D>(); foreach (Circle3D c in circlesOnUnitSphere) { Vector3D[] pp = c.RepresentativePoints.Select(p => Sterographic.SphereToPlane(p)).ToArray(); Circle3D cProj = new Circle3D(pp[0], pp[1], pp[2]); if (Infinity.IsInfinite(cProj.Radius)) { continue; } cProj.Color = c.Color; projected.Add(cProj); } SaveToBmp(projected); }
public static void CatenoidBasedSurface() { RLD_outputs outputs; SurfaceInternal(out outputs); double scale = m_params.Scale; // Map a point for a given k/m from the hemihypersphere to the complex plane. // You can also pass in -1 for k to get a point on the equator of the hemihypersphere. double mInc = Math.PI * 2 / m_params.M; Func <RLD_outputs, int, int, Vector3D> onPlane = (o, k, m) => { double theta = k == -1 ? 0 : outputs.x_i[k]; theta += Math.PI / 2; return (Sterographic.SphereToPlane( SphericalCoords.SphericalToCartesian( new Vector3D(1, theta, m * mInc) ) )); }; // Setup texture coords on fundamental triangle. // We'll use a fundamental triangle in the southern hemisphere, // with stereographically projected coords at (0,0), (1,0), and CCW on the unit circle depending on M. Polygon p = new Polygon(); p.Segments.Add(Segment.Line(new Vector3D(), new Vector3D(1, 0))); p.Segments.Add(Segment.Arc(new Vector3D(1, 0), onPlane(outputs, 1, 1), onPlane(outputs, -1, 1))); p.Segments.Add(Segment.Line(onPlane(outputs, -1, 1), new Vector3D())); int levels = 9; TextureHelper.SetLevels(levels); Vector3D[] coords = TextureHelper.TextureCoords(p, Geometry.Spherical, doGeodesicDome: true); int[] elementIndices = TextureHelper.TextureElements(1, levels); // Setup a nearTree for the catenoid locations (on the plane). NearTree nearTree = new NearTree(Metric.Spherical); for (int k = 1; k < outputs.x_i.Length; k++) { for (int m = 0; m <= 1; m++) { Vector3D loc = onPlane(outputs, k, m); nearTree.InsertObject(new NearTreeObject() { ID = k, Location = loc }); } } // Given a point on the plane, find the nearest catenoid center and calculate the height of the surface based on that. // This also calculates the locking of the point. Func <Vector3D, Tuple <double, Vector3D, Vector3D> > heightAndLocking = coord => { NearTreeObject closest; if (!nearTree.FindNearestNeighbor(out closest, coord, double.MaxValue)) { throw new System.Exception(); } Vector3D locked = new Vector3D(); if (p.Segments[0].IsPointOn(coord) || p.Segments[2].IsPointOn(coord)) { locked = new Vector3D(1, 1, 0, 0); } //if( p.Segments[1].IsPointOn( v ) ) // Not working right for some reason, but line below will work. if (Tolerance.Equal(coord.Abs(), 1)) { locked = new Vector3D(1, 1, 1, 0); } Vector3D vSphere = Sterographic.PlaneToSphere(coord); Vector3D cSphere = Sterographic.PlaneToSphere(closest.Location); double dist = vSphere.AngleTo(cSphere); int k = (int)closest.ID; double waist = outputs.t_i[k]; double rld_height = outputs.phi_i[k]; double h = waist * 3.5 * 2; // height where catenoid will meet rld_height. double factor = scale * rld_height * 2 / h; // Artifical scaling so we can see things. dist /= factor; double z = double.NaN; if (dist >= waist) { z = waist * DonHatch.acosh(dist / waist); } else if (dist >= 0.7 * waist) { z = 0; // Move the coord to the thinnest waist circle. Mobius m = new Mobius(); m.Hyperbolic(Geometry.Spherical, coord.ToComplex(), waist / dist); coord = m.Apply(coord); } if (dist < waist * 20) { locked = new Vector3D(1, 1, 1, 1); } return(new Tuple <double, Vector3D, Vector3D>(z * factor, locked, coord)); }; // Calculate all the coordinates. Vector3D[] locks = new Vector3D[coords.Length]; for (int i = 0; i < coords.Length; i++) { Vector3D coord = coords[i]; var hl = heightAndLocking(coord); locks[i] = hl.Item2; coord = hl.Item3; coords[i] = Normal(Sterographic.PlaneToSphere(coord), (double)hl.Item1); } // Relax it. Relax(coords, elementIndices, locks); Mesh mesh = new Mesh(); Sphere s = new Sphere(); for (int i = 0; i < elementIndices.Length; i += 3) { Vector3D a = coords[elementIndices[i]]; Vector3D b = coords[elementIndices[i + 1]]; Vector3D c = coords[elementIndices[i + 2]]; if (a.DNE || b.DNE || c.DNE) { continue; } for (int m = 0; m <= 0; m++) { mesh.Triangles.Add(new Mesh.Triangle(a, b, c)); mesh.Triangles.Add(new Mesh.Triangle( s.ReflectPoint(a), s.ReflectPoint(b), s.ReflectPoint(c))); a.RotateXY(mInc); b.RotateXY(mInc); c.RotateXY(mInc); } } PovRay.WriteMesh(mesh, "RLD.pov"); }