public static Vec Center(Vec a, Vec b, Vec c) { Matrix m = new Matrix(); m.Add(new Vec() { 1, 1, 1, 1 }); m.Add(new Vec() { a *a, a[0], a[1], 1 }); m.Add(new Vec() { b *b, b[0], b[1], 1 }); m.Add(new Vec() { c *c, c[0], c[1], 1 }); Vec d = new Vec(); d.Add((m.MinorMatrix(0, 1).Det / m.MinorMatrix(0, 0).Det) / 2); d.Add((m.MinorMatrix(0, 2).Det / m.MinorMatrix(0, 0).Det) / -2); return(d); }
public string CustomRendering() { Vec m1 = game.robot[0, firstrobot - 1]; double az = sfals > 1 ? 0 : 1 - sfals; double bz = nogood ? 1.0 : 0.0; Vec hh = bal.Add(dw.ToVec()); /* * string R = * "[ " + * "{\"Line\": {\"x1\": " + bal.x + * ",\"y1\": " + bal.y + ",\"z1\": " + bal.z + ",\"x2\": " + (hh.x) + ",\"y2\": " + * hh.y + ",\"z2\": " + hh.z + * ",\"width\": 4.0,\"r\": 1.0,\"g\": 1.0,\"b\": 1.0,\"a\": 1.0}},"; * if (HasValue(m1.x)) * R * += "{ \"Sphere\": { \"x\": " + m1.x.ToString("0.0") + * ", \"y\": " + m1.y.ToString("0.0") + * ", \"z\": " + m1.z.ToString("0.0") + * ", \"radius\": 2.0, \"r\": " + az.ToString("0.0") + ", \"g\": 0.0, \"b\": " + bz.ToString("0.0") + ", \"a\": 0.6 \n } },"; + if (HasValue(afterball.x)) + R += + + "{ \"Sphere\": { \"x\": " + afterball.x.ToString("0.0") + + ", \"y\": " + afterball.y.ToString("0.0") + + ", \"z\": " + afterball.z.ToString("0.0") + + ", \"radius\": 2.0, \"r\": " + 1.0 + ", \"g\": 1.0, \"b\": " + 1.0 + ", \"a\": 1.0 \n } },"; + if (HasValue(afterball1.x)) + R += + + + "{ \"Sphere\": { \"x\": " + afterball1.x.ToString("0.0") + + ", \"y\": " + afterball1.y.ToString("0.0") + + ", \"z\": " + afterball1.z.ToString("0.0") + + ", \"radius\": 2.0, \"r\": " + 1.0 + ", \"g\": 0.0, \"b\": " + 1.0 + ", \"a\": 1.0 \n } },"; + R+= + + + + "{ \"Sphere\": { \"x\": " + ggx.ToString("0.0") + + ", \"y\": " + 1.0 + + ", \"z\": " + -40.0 + + ", \"radius\": 2.0, \"r\": " + 1.0 + ", \"g\": 0.0, \"b\": " + 1.0 + ", \"a\": 1.0 \n } }" + + + ",{\"Text\":\""+ printout + " \"}]";*/ string R = ""; printout = ""; return(R); }
public static Vec[] RenderEntry() { int w = 256; int h = 256; int samps = 25; // # samples var tmp1 = new Vec(50, 52, 295.6); var tmp2 = new Vec(0, -0.042612, -1).Norm(); Ray cam = new Ray(ref tmp1, ref tmp2); // cam pos, dir Vec cx = new Vec(w * .5135 / h, 0, 0); Vec cy = (cx.Cross(ref cam.d)).Norm().Mul(.5135); Vec[] c = new Vec[w * h]; //#pragma omp parallel for schedule(dynamic, 1) // OpenMP // Loop over image rows for (int y = 0; y < h; y++) { //fprintf(stderr,"\rRendering (%d spp) %5.2f%%",samps*4,100.*y/(h-1)); RandomLCG rand = new RandomLCG((uint)y); // Loop cols for (ushort x = 0; x < w; x++) { // 2x2 subpixel rows for (int sy = 0; sy < 2; sy++) { int i = (h - y - 1) * w + x; // 2x2 subpixel cols for (int sx = 0; sx < 2; sx++) { Vec r = Vec_Zero; for (int s = 0; s < samps; s++) { double r1 = 2 * rand.NextNumber(); double r2 = 2 * rand.NextNumber(); double dx = r1 < 1 ? MathSqrt(r1) - 1 : 1 - MathSqrt(2 - r1); double dy = r2 < 1 ? MathSqrt(r2) - 1 : 1 - MathSqrt(2 - r2); var tmp3 = cy.Mul(((sy + .5 + dy) / 2 + y) / h - .5); Vec d = cx.Mul(((sx + .5 + dx) / 2 + x) / w - .5).Add( ref tmp3).Add(ref cam.d); var tmp4 = d.Mul(140); var tmp5 = cam.o.Add(ref tmp4); var tmp6 = d.Norm(); var tmp7 = new Ray(ref tmp5, ref tmp6); var tmp8 = radiance(ref tmp7, 0, ref rand).Mul(1.0 / samps); r = r.Add(ref tmp8); } var tmp9 = new Vec(clamp(r.x), clamp(r.y), clamp(r.z)).Mul(.25); c[i] = c[i].Add(ref tmp9); } } } } return(c); }
public void Add() { var p1 = new Vec(1, 2, 3); Assert.AreEqual(new Vec(2, 4, 6), p1.Added(new Vec(1, 2, 3))); p1.Add(new Vec(1, 2, 3)); Assert.AreEqual(new Vec(2, 4, 6), p1); }
public static Vec SigmoidN(Vec x, double mean, double sd, double scale) { Vec v = new Vec(); foreach (double d in x) { v.Add(SigmoidN(d, mean, sd, scale)); } return(v); }
public static Vec Sigmoid(Vec x) { Vec v = new Vec(); foreach (double d in v) { v.Add(Sigmoid(d)); } return(v); }
public static Vec SoftMax(Vec x) { Vec s = new Vec(); double sum = 0; for (int i = 0; i < x.Dims; i++) { sum += Math.Exp(x[i]); } for (int i = 0; i < x.Dims; i++) { s.Add(Math.Exp(x[i] / sum)); } return(s); }
public static Vec StiffMax(Vec x) { Vec s = new Vec(); double sum = 0; for (int i = 0; i < x.Dims; i++) { sum += x[i]; } for (int i = 0; i < x.Dims; i++) { s.Add(x[i] / sum); } return(s); }
public Dan DanToArenaQuarter() { Vec simplifiedVec = new Vec(x, y, 0); //ground Dan dan = new Dan(y, vec1); if (y > 10) { dan = new Dan(ar.height - y, vec3); } //De("ground " + dan); //Ceiling //De("Tetto"); /* * Dan ddp = DanToPlane(vec2, * vec3); * * dan = Dan.Min(dan, * ddp * ); * De("tetto "+ddp.distance) ;*/ //side X //De("side X"); dan = Dan.Min(dan, DanToPlane(vec6, new Vec(-1, 0, 0)) ); /*side z (goal) * dan = Dan.Min(dan, * DanToPlane(new Vec(0,0,ar.depth/2+ar.goal_depth), * new Vec(0,0,-1)) * );*/ //size z //De("side z"); Vec v = this.Sub(goalcorner); if ( (v.x >= (ag2) - ar.goal_side_radius) || (v.y > ar.goal_height + ar.goal_side_radius) || ((v.x > 0) && (v.y > 0) && (v.Len() >= ar.goal_top_radius + ar.goal_side_radius)) ) { dan = Dan.Min(dan, DanToPlane(vec4, vec5) ); } //corner //De("Corner"); if ( (x > aw2cr) && (z > ad2cr) ) { Dan cornerDan = DanToSphereInner(new Vec(aw2cr, y, ad2cr), ar.corner_radius); dan = Dan.Min(dan, cornerDan ); } // goal outer corner if (z < ad2 + ar.goal_side_radius) { //side x //De("palo"); if (x < ag2 + ar.goal_side_radius) { dan = Dan.Min(dan, DanToSphereOuter(new Vec(ag2 + ar.goal_side_radius, y, ad2 + ar.goal_side_radius ), ar.goal_side_radius) ); } // ceiling //De("Traversa tetto"); if (y < ar.goal_height + ar.goal_side_radius) { dan = Dan.Min(dan, DanToSphereOuter(new Vec(x, ar.goal_height + ar.goal_side_radius, ad2 + ar.goal_side_radius), ar.goal_side_radius) ); } //top corner side //De("Traversa angolo alto"); Vec vv = this.Sub(goalcorner); if ((vv.x > 0) && (vv.y > 0)) { goalcorner = goalcorner.Add(vv.Normalize().Mul(ar.goal_top_radius + ar.goal_side_radius)); dan = Dan.Min(dan, DanToSphereOuter(new Vec(x, y, ad2 + ar.goal_side_radius), ar.goal_side_radius) ); } } //bottom curls if (y < ar.bottom_radius) { //x //De("bottom corners: side x"); if (x > aw2 - ar.bottom_radius) { dan = Dan.Min(dan, DanToSphereInner(new Vec(aw2 - ar.bottom_radius, ar.bottom_radius, z), ar.bottom_radius) ); } //z //De("side z"); if (z > ad2 - ar.bottom_radius) { if (x >= ag2 + ar.goal_side_radius) { dan = Dan.Min(dan, DanToSphereInner(new Vec(x, ar.bottom_radius, ad2 - ar.bottom_radius), ar.bottom_radius) ); } } //goal outer corner Vec O = new Vec(ag2 + ar.goal_side_radius, ad2 + ar.goal_side_radius, 0); Vec V = new Vec(x, y, 0).Sub(O); if ((V.x > 0) && (V.y > 0)) { if (V.Len() < ar.goal_side_radius + ar.bottom_radius) { // De("goal out corn"); O = O.Add(V.Normalize()).Mul(ar.goal_side_radius + ar.bottom_radius); dan = Dan.Min(dan, DanToSphereInner(new Vec(O.x, ar.bottom_radius, O.y), ar.bottom_radius) ); } } if (x > aw2 - ar.corner_radius) { if (z > ad2 - ar.corner_radius) { Vec n = simplifiedVec.Sub(corner_o); double dist = n.Len(); if (dist > ar.corner_radius - ar.bottom_radius) { n = n.Mul(1 / dist); Vec o2 = corner_o.Add(n).Mul(ar.corner_radius - ar.bottom_radius); //De("bottom corner"); dan = Dan.Min(dan, DanToSphereInner(new Vec(o2.x, ar.bottom_radius, o2.y), ar.bottom_radius) ); } } } /* * //corner bottom * if (x > aw2 - ar.corner_radius) * if (z > ad2 - ar.corner_radius) * { * //De("corner bottom"); * * Vec N = simplifiedVec.Sub(corner_o); * double dist = N.Len(); * if (dist > ar.corner_radius - ar.bottom_radius) * { * N = N.Mul(1 / dist); * Vec o2 = corner_o.Add(N).Mul(ar.corner_radius - ar.bottom_radius); * dan = Dan.Min(dan, * DanToSphereInner(new Vec(o2.x, ar.bottom_radius, o2.y), * ar.bottom_radius * ) * ); * } * }*/ } //ceiling corners if (y > ar.height - ar.top_radius) { //side x if (x > aw2 - ar.top_radius) { //De("ceil corn X side"); dan = Dan.Min(dan, DanToSphereInner(new Vec(aw2 - ar.top_radius, ar.height - ar.top_radius, z), ar.top_radius) ); } //side z if (z > ad2 - ar.top_radius) { //De("ceil corn Z side"); dan = Dan.Min(dan, DanToSphereInner(new Vec(x, ar.height - ar.top_radius, ad2 - ar.top_radius) , ar.top_radius)); } //corner if (x > aw2 - ar.corner_radius) { if (z > ad2 - ar.corner_radius) { Vec corner_o = new Vec(aw2 - ar.corner_radius, ad2 - ar.corner_radius, 0); Vec dv = simplifiedVec.Sub(corner_o); if (dv.Len() > ar.corner_radius - ar.top_radius) { //De("ceil corner"); Vec n = dv.Normalize(); Vec o2 = corner_o.Add(n).Mul(ar.corner_radius - ar.top_radius); dan = Dan.Min(dan, DanToSphereInner(new Vec(o2.x, ar.height - ar.top_radius, o2.y), ar.top_radius)); } } } } return(dan); }
public void ValueType_Vec() { var p1 = new Vec(1, 0, 0); var p2 = new Vec(0, 1, 0); Assert.IsFalse(p1.IsEqual(p2, 0.99, 0.1)); Assert.IsTrue(p1.IsEqual(p2, 1.01, 0.1)); Assert.IsTrue(p1.IsEqual(p2, 0.99, Math.PI / 2)); Assert.IsTrue(p1.IsNormal(p2, 0.1)); Assert.IsFalse(p1.IsOpposite(p2, 0.1)); Assert.IsTrue(p1.IsOpposite(p2, Math.PI / 2)); Assert.IsFalse(p1.IsParallel(p2, 0.1)); Assert.IsTrue(p1.IsParallel(p2, Math.PI / 2)); p1 = new Vec(1, 2, 3); p2 = new Vec(4, 5, 6); Assert.AreEqual(14, p1.SquareMagnitude()); Assert.AreEqual(Math.Sqrt(14), p1.Magnitude()); p2 = p1; p2.Add(new Vec(1, 2, 3)); Assert.AreEqual(new Vec(2, 4, 6), p2); Assert.AreEqual(new Vec(2, 4, 6), p1.Added(new Vec(1, 2, 3))); p2 = new Vec(1, 2, 3); p2.Subtract(new Vec(3, 2, 1)); Assert.AreEqual(new Vec(-2, 0, 2), p2); Assert.AreEqual(new Vec(-2, 0, 2), p1.Subtracted(new Vec(3, 2, 1))); p2 = new Vec(1, 2, 3); p2.Cross(new Vec(3, 2, 1)); Assert.AreEqual(new Vec(-4, 8, -4), p2); Assert.AreEqual(new Vec(-4, 8, -4), p1.Crossed(new Vec(3, 2, 1))); Assert.AreEqual(Math.Sqrt(96), p1.CrossMagnitude(new Vec(3, 2, 1))); Assert.AreEqual(96, p1.CrossSquareMagnitude(new Vec(3, 2, 1))); p2 = new Vec(1, 2, 3); p2.CrossCross(new Vec(1, 2, 3), new Vec(4, 5, 6)); Assert.AreEqual(new Vec(-24, -6, 12), p2); Assert.AreEqual(new Vec(-24, -6, 12), p1.CrossCrossed(new Vec(1, 2, 3), new Vec(4, 5, 6))); p2 = new Vec(1, 2, 3); p2.Divide(2); Assert.AreEqual(new Vec(0.5, 1, 1.5), p2); Assert.AreEqual(new Vec(0.5, 1, 1.5), p1.Divided(2)); Assert.AreEqual(14, p1.Dot(new Vec(1, 2, 3))); Assert.AreEqual(0, p1.DotCross(new Vec(4, 5, 6), new Vec(4, 5, 6))); p2 = new Vec(1, 2, 3); p2.Multiply(2); Assert.AreEqual(new Vec(2, 4, 6), p2); Assert.AreEqual(new Vec(2, 4, 6), p1.Multiplied(2)); p2 = new Vec(1, 2, 3); p2.Scale(2); Assert.AreEqual(new Vec(2, 4, 6), p2); Assert.AreEqual(new Vec(2, 4, 6), p1.Scaled(2)); p2 = new Vec(1, 2, 3); p2.Normalize(); Assert.IsTrue(p2.IsEqual(new Vec(0.26726, 0.53452, 0.80178), 0.00001, 0.00001)); Assert.IsTrue(p1.Normalized().IsEqual(new Vec(0.26726, 0.53452, 0.80178), 0.00001, 0.00001)); p2 = new Vec(1, 2, 3); p2.Reverse(); Assert.AreEqual(new Vec(-1, -2, -3), p2); Assert.AreEqual(new Vec(-1, -2, -3), p1.Reversed()); p2.SetLinearForm(new Vec(1, 2, 3), new Vec(4, 5, 6)); Assert.AreEqual(new Vec(5, 7, 9), p2); p2.SetLinearForm(2, new Vec(1, 2, 3), new Vec(4, 5, 6)); Assert.AreEqual(new Vec(6, 9, 12), p2); p2.SetLinearForm(2, new Vec(1, 2, 3), 3, new Vec(4, 5, 6)); Assert.AreEqual(new Vec(14, 19, 24), p2); p2.SetLinearForm(2, new Vec(1, 2, 3), 3, new Vec(4, 5, 6), new Vec(7, 8, 9)); Assert.AreEqual(new Vec(21, 27, 33), p2); p2.SetLinearForm(2, new Vec(1, 2, 3), 3, new Vec(4, 5, 6), 4, new Vec(7, 8, 9)); Assert.AreEqual(new Vec(42, 51, 60), p2); p2.SetLinearForm(2, new Vec(1, 2, 3), 3, new Vec(4, 5, 6), 4, new Vec(7, 8, 9), new Vec(10, 11, 12)); Assert.AreEqual(new Vec(52, 62, 72), p2); p2 = new Vec(2, 0, 0); p2.Mirror(new Vec(0, 1, 0)); Assert.AreEqual(new Vec(-2, 0, 0), p2); Assert.AreEqual(new Vec(2, 0, 0), p2.Mirrored(new Vec(0, 1, 0))); var m2 = new Ax1(new Pnt(-1, 2, -3), new Dir(-1, 0, 0)); p2 = new Vec(2, 1, 3); Assert.AreEqual(new Vec(2, -1, -3), p2.Mirrored(m2)); p2.Mirror(m2); Assert.AreEqual(new Vec(2, -1, -3), p2); var a2 = new Ax2(new Pnt(-1, 2, -3), new Dir(-1, 0, 0)); p2 = new Vec(2, 1, 3); Assert.AreEqual("-2,1,3", p2.Mirrored(a2).ToString()); p2.Mirror(a2); Assert.AreEqual("-2,1,3", p2.ToString()); p2 = new Vec(2, 1, 3); Assert.IsTrue(new Vec(2, 3, -1).IsEqual(p2.Rotated(m2, Math.PI / 2), 0.0001, 0.0001)); p2.Rotate(m2, Math.PI / 2); Assert.IsTrue(new Vec(2, 3, -1).IsEqual(p2, 0.0001, 0.0001)); //TestContext.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0},{1},{2}", gp2.x, gp2.y, gp2.z)); Trsf t1 = new Trsf(); t1.SetRotation(Ax1.OZ, Math.PI / 2); p2 = new Vec(4, 5, 6); Assert.AreEqual("-5,4,6", p2.Transformed(t1).ToString()); p2.Transform(t1); Assert.AreEqual("-5,4,6", p2.ToString()); }
public static void Main(string[] args) { Console.WriteLine("Pruebas de rendimiento"); int count = 100; int count2 = 1000000; List <SingleTest> tests = new List <SingleTest>(); List <IVec> vectors = new List <IVec>(); for (int i = 0; i < count2; i++) { vectors.Add(new Vec(i, i, i, i)); } List <Vec> vectors2 = new List <Vec>(); for (int i = 0; i < count2; i++) { vectors2.Add(new Vec(i, i, i, i)); } // BuffVec tests.Add(new SingleTest( "Test.BuffVec", () => { BuffVec sum = new BuffVec(); for (int i = 0; i < vectors.Count; i++) { sum.Add(vectors[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.BuffVec (2)", () => { BuffVec sum = new BuffVec(); for (int i = 0; i < vectors2.Count; i++) { sum.Add(vectors2[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.BuffVec.Add_2", () => { BuffVec sum = new BuffVec(); for (int i = 0; i < vectors.Count; i++) { sum.Add_2(vectors[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.BuffVec.Add_2 (2) ", () => { BuffVec sum = new BuffVec(); for (int i = 0; i < vectors2.Count; i++) { sum.Add_2(vectors2[i]); } return(sum); }) ); // BuffVec2 // ***** 2a ***** tests.Add(new SingleTest( "Test.BuffVec2", () => { BuffVec_v2 sum = new BuffVec_v2(); for (int i = 0; i < vectors.Count; i++) { sum.Add(vectors[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.BuffVec2 (2)", () => { BuffVec_v2 sum = new BuffVec_v2(); for (int i = 0; i < vectors2.Count; i++) { sum.Add(vectors2[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.BuffVec2.Add_2", () => { BuffVec_v2 sum = new BuffVec_v2(); for (int i = 0; i < vectors.Count; i++) { sum.Add_2(vectors[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.BuffVec2.Add_2 (2)", () => { BuffVec_v2 sum = new BuffVec_v2(); for (int i = 0; i < vectors2.Count; i++) { sum.Add_2(vectors2[i]); } return(sum); }) ); // Vec tests.Add(new SingleTest( "Test.Vec", () => { Vec sum = new Vec(); for (int i = 0; i < vectors.Count; i++) { sum = sum.Add(vectors[i]); } return(sum); }) ); // ***** Mejor rendimiento ***** tests.Add(new SingleTest( "Test.Vec (2)", () => { Vec sum = new Vec(); for (int i = 0; i < vectors2.Count; i++) { sum = sum.Add(vectors2[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.Vec.Add_2", () => { Vec sum = new Vec(); for (int i = 0; i < vectors.Count; i++) { sum = sum.Add_2(vectors[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.Vec.Add_2 (2)", () => { Vec sum = new Vec(); for (int i = 0; i < vectors2.Count; i++) { sum = sum.Add_2(vectors2[i]); } return(sum); }) ); // Ultimas pruebas tests.Add(new SingleTest( "Test.BuffVec2.Add_3", () => { BuffVec_v2 sum = new BuffVec_v2(); for (int i = 0; i < vectors.Count; i++) { sum.Add_3(vectors[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.BuffVec2.Add_3 (2)", () => { BuffVec_v2 sum = new BuffVec_v2(); for (int i = 0; i < vectors2.Count; i++) { sum.Add_3(vectors2[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.BuffVec.Add_3", () => { BuffVec sum = new BuffVec(); for (int i = 0; i < vectors.Count; i++) { sum.Add_3(vectors[i]); } return(sum); }) ); tests.Add(new SingleTest( "Test.BuffVec.Add_3 (2)", () => { BuffVec sum = new BuffVec(); for (int i = 0; i < vectors2.Count; i++) { sum.Add_3(vectors2[i]); } return(sum); }) ); /*SingleTest aux = tests[9]; * tests.Clear(); * tests.Add(aux);*/ foreach (SingleTest test in tests) { Measure(test, count); } Console.WriteLine("Resultado"); tests.Sort((a, b) => a.Time.CompareTo(b.Time)); long minTime = tests[0].Time; foreach (SingleTest test in tests) { Console.WriteLine("{0,-30} {1,10:F3} : {2,10}", test.Name, test.Time / (double)minTime, test.Time); } Console.ReadKey(); foreach (object result in tests[0].Results) { Console.WriteLine("" + result); } }
private static IEnumerable <Vec> GetEndlessPath(PasswordBlueprint blueprint) { var v = new Vec(blueprint.X, blueprint.Y); yield return(v); var d1 = new Vec(blueprint.Direction[0]); var d2 = 1 < blueprint.Direction.Length ? new Vec(blueprint.Direction[1]) : (Vec?)null; if (blueprint.Shape == BlueprintShape.Straight) { while (true) { v = v.Add(d1); if (d2.HasValue) { v = v.Add(d2.Value); } yield return(v); } } if (!d2.HasValue) { throw new ArgumentException($"Expecting two direction for shape '{blueprint.Shape}'."); } var a = d1; var b = d2.Value; if (blueprint.Shape == BlueprintShape.Stairs) { while (true) { v = v.Add(a); (a, b) = (b, a); yield return(v); } } if (blueprint.Shape == BlueprintShape.L) { v = v.Add(a); yield return(v); while (true) { v = v.Add(b); yield return(v); } } var steps = 1; while (true) { for (int i = 0; i < steps; i++) { v = v.Add(a); yield return(v); } for (int i = 0; i < steps; i++) { v = v.Add(b); yield return(v); } (a, b) = (a.Multiply(-1), b.Multiply(-1)); steps++; } }