/// <summary> /// http://www.cs.ubc.ca/~rbridson/docs/bridson-siggraph2007-curlnoise.pdf /// </summary> /// <param name="point"></param> /// <returns></returns> public static Vec2d CurlAt(Vec2d point) { return CurlAt(point.X, point.Y); }
//C++ TO C# CONVERTER WARNING: 'const' methods are not available in C#: //ORIGINAL LINE: virtual double FuncGrad(const Vector & x, Vector & grad) const public override double FuncGrad(Vector x, ref Vector grad) { // from 2d: int lpi; int gpi; Vec <3> n, vgrad; Point <3> pp1; Vec2d g1 = new Vec2d(); Vec2d vdir = new Vec2d(); double badness; double hbad; double hderiv; vgrad = 0; badness = 0; ld.meshthis.GetNormalVector(ld.surfi, ld.sp1, ld.gi1, n); pp1 = ld.sp1 + x(0) * ld.t1.functorMethod + x(1) * ld.t2.functorMethod; // meshthis -> ProjectPoint (surfi, pp1); // meshthis -> GetNormalVector (surfi, pp1, n); //C++ TO C# CONVERTER NOTE: This static local variable declaration (not allowed in C#) has been moved just prior to the method: // static Array<Point2d> pts2d; FuncGrad_pts2d.SetSize(mesh.GetNP()); grad = 0; for (int j = 1; j <= ld.locelements.Size(); j++) { lpi = ld.locrots.Get(j); Element2d bel = mesh[ld.locelements.Get(j)]; gpi = bel.PNum(lpi); for (int k = 1; k <= bel.GetNP(); k++) { PointIndex pi = bel.PNum(k); FuncGrad_pts2d.Elem(pi) = new Point2d(ld.t1.functorMethod * (new mesh.Point(pi) - ld.sp1), ld.t2.functorMethod * (new mesh.Point(pi) - ld.sp1)); } FuncGrad_pts2d.Elem(gpi) = new Point2d(x(0), x(1)); for (int k = 1; k <= 2; k++) { if (k == 1) { vdir = new Vec2d(1, 0); } else { vdir = new Vec2d(0, 1); } hbad = bel.CalcJacobianBadnessDirDeriv(FuncGrad_pts2d, lpi, vdir, ref hderiv); grad(k - 1) += hderiv; if (k == 1) { badness += hbad; } } } /* * vgrad.Add (-(vgrad * n), n); * * grad.Elem(1) = vgrad * t1; * grad.Elem(2) = vgrad * t2; */ return(badness); }
public HUDElement() { position = Vec2d.Zero; }
/// <summary> /// Returns a vector composed of offset noise values. /// </summary> /// <param name="point"></param> /// <returns></returns> public static Vec2d VectorAt(Vec2d point) { return VectorAt(point.X, point.Y); }
/// <summary> /// /// </summary> /// <param name="point"></param> /// <param name="amount"></param> public static void IncrementAt(this GridField2d <Vec2d> field, GridPoint2d point, Vec2d amount) { FieldUtil.IncrementAt(field, point.Corners, point.Weights, amount); }
/// <summary> /// Returns the index of the nearest point in the grid. /// Assumes the point is within the bounds of the grid. /// </summary> /// <param name="point"></param> /// <returns></returns> public int IndexAtUnchecked(Vec2d point) { (int i, int j) = IndicesAt(point); return(GridUtil.FlattenIndices(i, j, _nx)); }
//-------------------------------------------------------------------------------------------------- public bool ComputeParameters(out Parameters parameters) { if (FirstPoint.IsEqual(SecondPoint, 0.01)) { parameters = new(); return(false); } double scale = 1.0; // Dimension line direction var dimDir = new Vec2d(_FirstPoint, _SecondPoint).ToDir(); var dimMid = _FirstPoint.Lerped(_SecondPoint, 0.5); var dimWidth = _FirstPoint.Distance(_SecondPoint); scale = Math.Min(scale, dimWidth / (DrawingRenderHelper.GetArrowSize().Length * 3)); if (_AutoText) { Text = dimWidth.ToInvariantString("F1"); } var rotation = dimDir.Angle(Dir2d.DX); bool dimRightToLeft = false; if (rotation < -Maths.HalfPI) { rotation += Maths.PI; dimRightToLeft = true; } else if (rotation > Maths.HalfPI) { rotation -= Maths.PI; dimRightToLeft = true; } // Extension line var extDir = new Dir2d(dimDir.Y, -dimDir.X); if (dimDir.Angle(new Vec2d(dimMid, Position).ToDir()) > 0) { extDir.Reverse(); } var dimToPos = new gp_Lin2d(_FirstPoint, dimDir).Distance(Position); var extVector = extDir.ToVec(dimToPos + _ExtensionOverlength); var dimOffset = extDir.ToVec(dimToPos); // Text var textOrigin = _FirstPoint.Lerped(_SecondPoint, 0.5) .Translated(dimOffset); var textWidth = 1.0; var textHeight = 1.0; var textScale = scale; if (!Text.IsNullOrWhiteSpace()) { var fontStyle = DrawingRenderHelper.GetDefaultFontStyle(); var textSize = DrawingRenderHelper.MeasureText(Text, fontStyle); textScale = Math.Min(1.0, dimWidth * 0.90 / textSize.X); scale = Math.Min(scale, textScale); // Exact positioning on above dim line var textOffset = dimDir.Angle(extDir) > 0 ? 1.0 : -1.0; if (dimRightToLeft) { textOffset *= -1; } textOrigin.Translate(extDir.ToVec(textOffset)); // Center textOrigin.Translate(dimDir.ToVec((dimRightToLeft ? 1.0 : -1.0) * textSize.X / 2 * textScale)); textWidth = textSize.X * textScale; textHeight = textSize.Y * textScale; } parameters = new Parameters() { ExtensionVector = extVector, DimensionDirection = dimDir, DimensionOffset = dimOffset, DimensionRotation = rotation, TextOrigin = textOrigin, TextWidth = textWidth, TextHeight = textHeight, Scale = scale, TextScale = textScale }; return(true); }
/// <summary> /// /// </summary> /// <param name="scale"></param> /// <param name="orientation"></param> public Transform2d(Vec2d scale, Orient2d orientation) { Scale = scale; Rotation = orientation.Rotation; Translation = orientation.Translation; }
// Twice the signed area of the triangle (p0, p1, p2) private static double area2(Vec2d p0, Vec2d p1, Vec2d p2) { return(p0.X * (p1.Y - p2.Y) + p1.X * (p2.Y - p0.Y) + p2.X * (p0.Y - p1.Y)); }
public static Vec2[] GetConvexHull(Vec2[] pts) { return(Vec2d.ToVec2Array(GetConvexHull(Vec2d.FromVec2Array(pts)))); }
private static bool lessThan(Vec2d p1, Vec2d p2) { return(p1.X < p2.X || p1.X == p2.X && p1.Y < p2.Y); }
/* * /// <summary> * /// Sets this field to the values of another. * /// </summary> * /// <param name="other"></param> * /// <param name="parallel"></param> * public void Sample(IField3d<T> other, bool parallel = false) * { * Action<Tuple<int, int>> body = range => * { * (int i, int j, int k) = IndicesAt(range.Item1); * * for (int index = range.Item1; index < range.Item2; index++, i++) * { * if (i == CountX) { j++; i = 0; } * if (j == CountY) { k++; j = 0; } * _values[index] = other.ValueAt(CoordinateAt(i, j, k)); * } * }; * * if (parallel) * Parallel.ForEach(Partitioner.Create(0, Count), body); * else * body(Tuple.Create(0, Count)); * } * * * /// <summary> * /// Sets this field to the values of another. * /// </summary> * /// <typeparam name="U"></typeparam> * /// <param name="other"></param> * /// <param name="converter"></param> * /// <param name="parallel"></param> * public void Sample<U>(IField3d<U> other, Func<U, T> converter, bool parallel = false) * { * Action<Tuple<int, int>> body = range => * { * (int i, int j, int k) = IndicesAt(range.Item1); * * for (int index = range.Item1; index < range.Item2; index++, i++) * { * if (i == CountX) { j++; i = 0; } * if (j == CountY) { k++; j = 0; } * _values[index] = converter(other.ValueAt(CoordinateAt(i, j, k))); * } * }; * * if (parallel) * Parallel.ForEach(Partitioner.Create(0, Count), body); * else * body(Tuple.Create(0, Count)); * } */ #region Explicit interface implementations /// <summary> /// /// </summary> /// <param name="point"></param> /// <returns></returns> T IField2d <T> .ValueAt(Vec2d point) { return(ValueAt(new Vec3d(point.X, point.Y, 0.0))); }
/// <summary> /// /// </summary> /// <param name="field"></param> /// <param name="point"></param> /// <param name="stepSize"></param> /// <param name="mode"></param> /// <returns></returns> public static IEnumerable <Vec2d> IntegrateFrom(this IField2d <Vec2d> field, Vec2d point, double stepSize, IntegrationMode mode = IntegrationMode.Euler) { return(SimulationUtil.IntegrateFrom(field, point, stepSize, mode)); }
public Vec2d getRandomUniformDensityCircleNode() { Random rnd = this.getRnd(); Vec2d retVec = new Vec2d(((2*rnd.NextDouble())-1)*this.radius2dForAngle3d(this.maxangle), ((2*rnd.NextDouble())-1)*this.radius2dForAngle3d(this.maxangle)); // is the created 2d vector within the circle boundaries? if (retVec.length() > this.radius2dForAngle3d(this.maxangle)) { //wrong size? make new one and return it. return this.getRandomUniformDensityCircleNode(); } return retVec; }
public ExplosionController(Particle e, Vec2d spawnForce) : base(e) { movementDelta = spawnForce; }
public static extern void ml_EM_predict( IntPtr model, IntPtr sample, IntPtr probs, out Vec2d ret);
public OffsetCalculator() { Value = Vec2d.Zero; }
/// <summary> /// /// </summary> /// <param name="point"></param> /// <param name="value"></param> public static void SetAt(this GridField2d <Vec2d> field, GridPoint2d point, Vec2d value) { FieldUtil.SetAt(field, point.Corners, point.Weights, value); }
public void Change(Vec2d off) { Value = new Vec2d(off); }
/// <summary> /// Returns the index of the nearest point in the grid. /// </summary> /// <param name="point"></param> /// <returns></returns> public int IndexAt(Vec2d point) { (int i, int j) = IndicesAt(point); return(GridUtil.FlattenIndices(WrapX(i), WrapY(j), _nx)); }
/// <summary> /// /// </summary> /// <param name="point"></param> /// <returns></returns> Vec3d IDifferentiableField2d <Vec3d> .GradientAt(Vec2d point) { return(GradientAt(point)); }
//-------------------------------------------------------------------------------------------------- public bool ComputeParameters(out Parameters parameters) { if (FirstPoint.IsEqual(SecondPoint, 0.01)) { parameters = new(); return(false); } var arrowSize = DrawingRenderHelper.GetArrowSize(); double scale = 1.0; // Dimension line var radius = _CenterPoint.Distance(Position); // Extension line var extDir1 = new Vec2d(_CenterPoint, _FirstPoint).ToDir(); var angle1 = -extDir1.Angle(Dir2d.DX); var extVector1 = extDir1.ToVec(radius - _CenterPoint.Distance(_FirstPoint) + _ExtensionOverlength); var extDir2 = new Vec2d(_CenterPoint, _SecondPoint).ToDir(); var angle2 = -extDir2.Angle(Dir2d.DX); var extVector2 = extDir2.ToVec(radius - _CenterPoint.Distance(_SecondPoint) + _ExtensionOverlength); var angle = (angle1 - angle2).Abs(); if (_AutoText) { Text = (angle1 - angle2).Abs().ToDeg().ToInvariantString("F0") + "°"; } Geom2d_Circle circle = new(new Ax2d(_CenterPoint, Dir2d.DX), radius); var dimWidth = circle.Circ2d().Length() / circle.Period() * angle; scale = Math.Min(scale, dimWidth / (arrowSize.Length * 3)); // Text Pnt2d textOrigin = new(); Vec2d textTangent = new(); circle.D1(angle1.Lerp(angle2, 0.5), ref textOrigin, ref textTangent); if (textTangent.X < 0.0) { textTangent.Reverse(); } var textDirection = textTangent.ToDir(); var textNormal = new Vec2d(_CenterPoint, textOrigin).ToDir(); var textWidth = 1.0; var textHeight = 1.0; var textRotation = textDirection.Angle(Dir2d.DX); var textScale = scale; if (!Text.IsNullOrWhiteSpace()) { var fontStyle = DrawingRenderHelper.GetDefaultFontStyle(); var textSize = DrawingRenderHelper.MeasureText(Text, fontStyle); textScale = Math.Min(1.0, dimWidth * 0.90 / textSize.X); scale = Math.Min(scale, textScale); var textOffset = textNormal.Y < 0 ? -1.0 : 1.0; // Exact positioning on above dim line textOrigin.Translate(textNormal.ToVec(textOffset)); textOrigin.Translate(textDirection.ToVec(-textSize.X / 2 * textScale)); textWidth = textSize.X * textScale; textHeight = textSize.Y * textScale; } // Arrows bool reverse = angle1 < angle2; double arrowAngle = circle.Period() / circle.Circ2d().Length() * arrowSize.Length * scale * (reverse ? 0.5 : -0.5); angle1 += arrowAngle; angle2 -= arrowAngle; Pnt2d arrowP1 = new(), arrowP2 = new(); Vec2d arrowT1 = new(), arrowT2 = new(); circle.D1(angle1, ref arrowP1, ref arrowT1); circle.D1(angle2, ref arrowP2, ref arrowT2); if (reverse) { arrowT1.Reverse(); } else { arrowT2.Reverse(); } arrowP1.Translate(arrowT1.ToDir().ToVec(arrowSize.Length * scale * 0.5)); arrowP2.Translate(arrowT2.ToDir().ToVec(arrowSize.Length * scale * 0.5)); // Move arrows slightly towards center, to compensate that the tangent is taken from the middle of the arrow double arrowOffset = _CenterPoint.Distance(circle.Value(angle1)) - _CenterPoint.Distance(arrowP1); arrowP1.Translate(extDir1.ToVec(arrowOffset)); arrowP2.Translate(extDir2.ToVec(arrowOffset)); parameters = new () { FirstExtensionVector = extVector1, FirstArrowPoint = arrowP1, FirstArrowTangent = arrowT1.ToDir(), SecondExtensionVector = extVector2, SecondArrowPoint = arrowP2, SecondArrowTangent = arrowT2.ToDir(), Radius = radius, StartAngle = angle1 + arrowAngle, EndAngle = angle2 - arrowAngle, TextOrigin = textOrigin, TextRotation = textRotation, TextWidth = textWidth, TextHeight = textHeight, Scale = scale, TextScale = textScale }; return(true); } //-------------------------------------------------------------------------------------------------- #endregion }
private void DrawLine(float cx, float cy, float len, double rot, char cH, char cV, char cTN, char cTP) { var wxs = new Vec2d(cx + len / 2, cy); wxs.RotateAround(new Vec2d(cx, cy), Math.PI * rot / 180); var wxe = new Vec2d(cx - len / 2, cy); wxe.RotateAround(new Vec2d(cx, cy), Math.PI * rot / 180); if ((int)Math.Round(rot % 180) == 0) { var sx = cx - len / 2; var ex = cx + len / 2; var y = cy; var done = false; for (var x = sx + 0.5f; x < ex; x += 0.5f) { SetMap(x, y, cH); done = true; } if (!done) { SetMap(sx, y, cH); } } else if ((int)Math.Round(rot % 180) == 90) { var x = cx; var sy = cy - len / 2; var ey = cy + len / 2; var done = false; for (var y = sy + 0.5f; y < ey; y += 0.5f) { SetMap(x, y, cV); done = true; } if (!done) { SetMap(x, sy, cV); } } else if ((int)Math.Round(rot % 180) < 90) { var sx = (float)wxs.X; var sy = (float)wxs.Y; var ex = (float)(wxe.X); var ey = (float)(wxe.Y); var dx = (ex - sx) / 32f; var dy = (ey - sy) / 32f; int ly = -99999; int lx = -99999; for (int i = 0; i <= 32; i++) { int ry = (int)Math.Round((sy + i * dy) * 2); int rx = (int)Math.Round((sx + i * dx) * 2); if (ry != ly && rx != lx) { SetMap(sx + i * dx, sy + i * dy, cTN); ly = ry; lx = rx; } } SetMap(sx, sy, cTN); SetMap(ex, ey, cTN); } else { var sx = (float)wxs.X; var sy = (float)wxs.Y; var ex = (float)(wxe.X); var ey = (float)(wxe.Y); var dx = (ex - sx) / 32f; var dy = (ey - sy) / 32f; int ly = -99999; int lx = -99999; for (int i = 0; i <= 32; i++) { int ry = (int)Math.Round((sy + i * dy) * 2); int rx = (int)Math.Round((sx + i * dx) * 2); if (ry != ly && rx != lx) { SetMap(sx + i * dx, sy + i * dy, cTP); ly = ry; lx = rx; } } SetMap(sx, sy, cTP); SetMap(ex, ey, cTP); } }
/// <summary> /// /// </summary> /// <param name="point"></param> /// <returns></returns> public static double ValueAt(Vec2d point) { return ValueAt(point.X, point.Y); }
/// <summary> /// /// </summary> /// <param name="point"></param> private void ToNoiseSpace(ref Vec2d point) { point.X = (point.X + OffsetX) * _txInv; point.Y = (point.Y + OffsetY) * _tyInv; }
/// <summary> /// Returns the gradient of noise values. /// </summary> /// <param name="point"></param> /// <returns></returns> public static Vec2d GradientAt(Vec2d point) { return GradientAt(point.X, point.Y); }
/// <summary> /// /// </summary> /// <param name="point"></param> /// <returns></returns> public double ValueAt(Vec2d point) { return(PerlinNoise.ValueAt(point.X * _txInv, point.Y * _tyInv)); }
public override void Initialize(EntityProperties properties, JsonObject attributes) { base.Initialize(properties, attributes); skintree = entity.WatchedAttributes.GetTreeAttribute("skinConfig"); if (skintree == null) { entity.WatchedAttributes["skinConfig"] = skintree = new TreeAttribute(); } entity.WatchedAttributes.RegisterModifiedListener("skinConfig", onSkinConfigChanged); entity.WatchedAttributes.RegisterModifiedListener("voicetype", onVoiceConfigChanged); entity.WatchedAttributes.RegisterModifiedListener("voicepitch", onVoiceConfigChanged); AvailableSkinParts = properties.Attributes["skinnableParts"].AsObject <SkinnablePart[]>(); foreach (var val in AvailableSkinParts) { string partCode = val.Code; val.VariantsByCode = new Dictionary <string, SkinnablePartVariant>(); AvailableSkinPartsByCode[val.Code] = val; if (val.Type == EnumSkinnableType.Texture && entity.Api.Side == EnumAppSide.Client) { ICoreClientAPI capi = entity.Api as ICoreClientAPI; LoadedTexture texture = new LoadedTexture(capi); foreach (var variant in val.Variants) { AssetLocation textureLoc; if (val.TextureTemplate != null) { textureLoc = val.TextureTemplate.Clone(); textureLoc.Path = textureLoc.Path.Replace("{code}", variant.Code); } else { textureLoc = variant.Texture; } IAsset asset = capi.Assets.TryGet(textureLoc.Clone().WithPathAppendixOnce(".png").WithPathPrefixOnce("textures/"), true); int r = 0, g = 0, b = 0; float c = 0; BitmapRef bmp = asset.ToBitmap(capi); for (int i = 0; i < 8; i++) { Vec2d vec = GameMath.R2Sequence2D(i); Color col2 = bmp.GetPixelRel((float)vec.X, (float)vec.Y); if (col2.A > 0.5) { r += col2.R; g += col2.G; b += col2.B; c++; } } bmp.Dispose(); c = Math.Max(1, c); variant.Color = ColorUtil.ColorFromRgba((int)(r / c), (int)(g / c), (int)(b / c), 255); val.VariantsByCode[variant.Code] = variant; } } else { foreach (var variant in val.Variants) { val.VariantsByCode[variant.Code] = variant; } } } if (entity.Api.Side == EnumAppSide.Server && AppliedSkinParts.Count == 0) { foreach (var val in AvailableSkinParts) { string partCode = val.Code; string variantCode = val.Variants[entity.World.Rand.Next(val.Variants.Length)].Code; selectSkinPart(partCode, variantCode, false); } } onVoiceConfigChanged(); }
/// <summary> /// /// </summary> /// <param name="point"></param> /// <returns></returns> public Vec2d VectorAt(Vec2d point) { ToNoiseSpace(ref point); return(PerlinNoise.VectorAt(point.X, point.Y)); }
public static extern void core_Mat_push_back_Vec2d(IntPtr self, Vec2d v);
/// <summary> /// /// </summary> /// <param name="point"></param> /// <returns></returns> Vec2d IField2d <Vec2d> .ValueAt(Vec2d point) { return(VectorAt(point)); }
public Vec2d getRingNode(double circleRadius, double nodeOrientation) { //Console.WriteLine("Radius: "+circleRadius+ " maxangle: "+this.maxangle); Vec2d retvec = new Vec2d(circleRadius*Math.Cos(nodeOrientation), circleRadius*Math.Sin(nodeOrientation)); return retvec; }
public CoinController(CoinEntity e, Vec2d spawnForce) : base(e) { movementDelta = spawnForce; }
public void Transform(ref Vec2d pointToTransform) { Transform(ref pointToTransform.X, ref pointToTransform.Y); }
/// <summary> /// /// </summary> /// <param name="scale"></param> /// <param name="rotation"></param> /// <param name="translation"></param> public Transform2d(Vec2d scale, OrthoBasis2d rotation, Vec2d translation) { Scale = scale; Rotation = rotation; Translation = translation; }