public static V3D Interpolate(V3D a, V3D b, double t) { return(new V3D( a.X * (1 - t) + b.X * t, a.Y * (1 - t) + b.Y * t, a.Z * (1 - t) + b.Z * t)); }
public ScnMemCell(ScnNodeLexer.Node node, List <string> param_list, V3D rotation) { Point = new V3D(); EventCollection = new List <ScnEvent>(); SpeedListNormal = new Dictionary <double, Dictionary <double, string> >(); SpeedListShunt = new Dictionary <double, string>(); int block = 0; Angle = rotation.Y; Name = Tools.ChangeParamsToText(node.Name, param_list); foreach (var value in node.Values) { try { switch (block++) { case 0: Point.X = value.GetType() != typeof(double) ? double.Parse(Tools.ChangeParamsToText((string)value, param_list), FP) : (double)value; break; case 1: Point.Y = value.GetType() != typeof(double) ? double.Parse(Tools.ChangeParamsToText((string)value, param_list), FP) : (double)value; break; case 2: Point.Z = value.GetType() != typeof(double) ? double.Parse(Tools.ChangeParamsToText((string)value, param_list), FP) : (double)value; break; case 3: Type = value != null?Tools.GetEventTypeFromString((string)value) : EventTypes.Unknown; break; } } catch (Exception x) { log.Error(x.Message + "\r\n" + x.StackTrace + "\r\n"); } } }
/* draw a quad */ static void draw_quad(BITMAP b, VTX v1, VTX v2, VTX v3, VTX v4, int mode) { int col; /* four vertices */ V3D vtx1 = new V3D(0, 0, 0, 0, 0, 0); V3D vtx2 = new V3D(0, 0, 0, 32 << 16, 0, 0); V3D vtx3 = new V3D(0, 0, 0, 32 << 16, 32 << 16, 0); V3D vtx4 = new V3D(0, 0, 0, 0, 32 << 16, 0); vtx1.x = v1.x; vtx1.y = v1.y; vtx1.z = v1.z; vtx2.x = v2.x; vtx2.y = v2.y; vtx2.z = v2.z; vtx3.x = v3.x; vtx3.y = v3.y; vtx3.z = v3.z; vtx4.x = v4.x; vtx4.y = v4.y; vtx4.z = v4.z; /* cull backfaces */ if ((mode != POLYTYPE_ATEX_MASK) && (mode != POLYTYPE_PTEX_MASK) && (mode != POLYTYPE_ATEX_MASK_LIT) && (mode != POLYTYPE_PTEX_MASK_LIT) && (polygon_z_normal(ref vtx1, ref vtx2, ref vtx3) < 0)) { return; } /* set up the vertex color, differently for each rendering mode */ switch (mode) { case POLYTYPE_FLAT: col = MID(128, 255 - fixtoi(v1.z + v2.z) / 16, 255); vtx1.c = vtx2.c = vtx3.c = vtx4.c = palette_color[col]; break; case POLYTYPE_GCOL: vtx1.c = palette_color[0xD0]; vtx2.c = palette_color[0x80]; vtx3.c = palette_color[0xB0]; vtx4.c = palette_color[0xFF]; break; case POLYTYPE_GRGB: vtx1.c = 0x000000; vtx2.c = 0x7F0000; vtx3.c = 0xFF0000; vtx4.c = 0x7F0000; break; case POLYTYPE_ATEX_LIT: case POLYTYPE_PTEX_LIT: case POLYTYPE_ATEX_MASK_LIT: case POLYTYPE_PTEX_MASK_LIT: vtx1.c = MID(0, 255 - fixtoi(v1.z) / 4, 255); vtx2.c = MID(0, 255 - fixtoi(v2.z) / 4, 255); vtx3.c = MID(0, 255 - fixtoi(v3.z) / 4, 255); vtx4.c = MID(0, 255 - fixtoi(v4.z) / 4, 255); break; } /* draw the quad */ quad3d(b, mode, texture, ref vtx1, ref vtx2, ref vtx3, ref vtx4); }
public double DistanceTo(V3D p) { var a = this.Length; var b = (p - A).Length; var c = (p - B).Length; var s = 0.5 * (a + b + c); return 2 * Math.Sqrt(s * (s - a) * (s - b) * (s - c)) / a; }
/// <summary> /// Returns true if the distance between 2 points is less than treshold /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <returns></returns> private bool IsLinked(V3D p1, V3D p2) { if (p1 == null || p2 == null) { return(false); } return((p1 - p2).Length < LinkDistance); }
public double DistanceTo(V3D p) { var a = this.Length; var b = (p - A).Length; var c = (p - B).Length; var s = 0.5 * (a + b + c); return(2 * Math.Sqrt(s * (s - a) * (s - b) * (s - c)) / a); }
/// <summary> /// Splits the curve at given position (t : 0..1). /// </summary> /// <param name="t">A number from 0 to 1.</param> /// <returns>Two curves.</returns> /// <remarks> /// (De Casteljau's algorithm, see: http://caffeineowl.com/graphics/2d/vectorial/bezierintro.html) /// </remarks> private BezierCurveLength3DCubic[] SplitAt(double t) { V3D a = V3D.Interpolate(A, B, t); V3D b = V3D.Interpolate(B, C, t); V3D c = V3D.Interpolate(C, D, t); V3D m = V3D.Interpolate(a, b, t); V3D n = V3D.Interpolate(b, c, t); V3D p = P(t); return(new[] { new BezierCurveLength3DCubic(A, a, m, p), new BezierCurveLength3DCubic(p, n, c, D) }); }
/// <summary> /// Splits the curve at given position (t : 0..1) /// (De Casteljau's algorithm, see: http://caffeineowl.com/graphics/2d/vectorial/bezierintro.html) /// </summary> /// <param name="t"></param> /// <returns></returns> public Bezier3[] SplitAt(double t) { V3D a = V3D.Interpolate(A, B, t); V3D b = V3D.Interpolate(B, C, t); V3D c = V3D.Interpolate(C, D, t); V3D m = V3D.Interpolate(a, b, t); V3D n = V3D.Interpolate(b, c, t); V3D p = P(t); return(new[] { new Bezier3(A, a, m, p), new Bezier3(p, n, c, D) }); }
public void BezierCurveLength3DCubicTest() { // Cubic Bézier curve length var test = new V3D[] { new V3D(-21298.4, 0.2, 2627.51), new V3D(-11.3359, 0.0, 0.0), new V3D(11.2637, 0.0, -1.28198), new V3D(-21332.3, 0.2, 2629.43) }; var testCurve = new BezierCurveLength3DCubic(test[0], test[0] + test[1], test[2] + test[3], test[3]); double length = testCurve.Length; double interpolatedLength = testCurve.InterpolatedLength; Assert.AreEqual(true, Math.Abs(33.972527859609976 - length) < 1.0E-10); Assert.AreEqual(true, Math.Abs(length - interpolatedLength) < 0.01); }
/// <summary> /// Finds object by its name or event name /// </summary> /// <param name="name"></param> internal void FindName(string name) { foreach (var spline in Splines) { var track = spline.Track; if (track.Name == name || track.Event0 == name || track.Event1 == name || track.Event2 == name || track.Eventall0 == name || track.Eventall1 == name || track.Eventall2 == name) { Position = -V3D.Interpolate(track.Point1, track.Point2, 0.5d); if (Scale < 1f) { Scale = 1f; } Selection.Current = spline; return; } } Selection.Clear(); }
public static double LengthByQuadratic3D(List <GPoint> points) { if (points.Count < 2) { return(0.0); } int degree = 2; List <GPoint> paddedPoints = PadPoints(points, degree); double length = 0.0; for (int i = 0; i < paddedPoints.Count - degree; i += degree) { var a = new V3D(paddedPoints[i].X, paddedPoints[i].Y); var b = new V3D(paddedPoints[i + 1].X, paddedPoints[i + 1].Y); var c = new V3D(paddedPoints[i + 2].X, paddedPoints[i + 2].Y); BezierCurveLength3DQuadratic lenCalc = new BezierCurveLength3DQuadratic(a, b, c); length += lenCalc.Length; } return(length); }
public V3D Cross(V3D a) { return new V3D(Y * a.Z - Z * a.Y, Z * a.X - X * a.Z, X * a.Y - Y * a.X); }
public static V3D Interpolate(V3D a, V3D b, double t) { return new V3D( a.X * (1 - t) + b.X * t, a.Y * (1 - t) + b.Y * t, a.Z * (1 - t) + b.Z * t); }
/// <summary> /// Track created from lexer node /// </summary> /// <param name="path"></param> /// <param name="buffer"></param> /// <param name="node"></param> public ScnTrack(string path, ScnNodeLexer.Node node) { int i = 0, block = 0; string xname = null; List <string> extras = new List <string>(); foreach (var value in node.Values) { switch (block) { case 0: // common properties switch (i++) { case 0: TrackType = GetLCString(value); break; case 1: TrackLength = (double)value; break; case 2: TrackWidth = (double)value; break; case 3: Friction = (double)value; break; case 4: SoundDist = (double)value; break; case 5: Quality = (int)(double)value; break; case 6: DamageFlag = (int)(double)value; break; case 7: Environment = GetLCString(value); break; case 8: Visibility = GetLCString(value); block = Visibility == "vis" ? 1 : 2; i = 0; break; } break; case 1: // fields for visible tracks switch (i++) { case 0: Tex1 = GetString(value); break; case 1: TexLength = (double?)value; break; case 2: Tex2 = GetString(value); break; case 3: TexHeight = (double?)value; break; case 4: TexWidth = (double?)value; break; case 5: TexSlope = (double?)value; block++; i = 0; break; } break; case 2: // track vectors and parameters switch (i++) { case 0: Point1 = new V3D { X = (double)value }; break; case 1: Point1.Y = (double)value; break; case 2: Point1.Z = (double)value; break; case 3: Roll1 = (double)value; break; case 4: CVec1 = new V3D { X = (double)value }; break; case 5: CVec1.Y = (double)value; break; case 6: CVec1.Z = (double)value; break; case 7: CVec2 = new V3D { X = (double)value }; break; case 8: CVec2.Y = (double)value; break; case 9: CVec2.Z = (double)value; break; case 10: Point2 = new V3D { X = (double)value }; break; case 11: Point2.Y = (double)value; break; case 12: Point2.Z = (double)value; break; case 13: Roll2 = (double)value; break; case 14: Radius1 = (double)value; block = (TrackType == "switch" || TrackType == "cross") ? 3 : 4; i = 0; break; } break; case 3: // switch vectors and parameters switch (i++) { case 0: Point3 = new V3D { X = (double)value }; break; case 1: Point3.Y = (double)value; break; case 2: Point3.Z = (double)value; break; case 3: Roll3 = (double)value; break; case 4: CVec3 = new V3D { X = (double)value }; break; case 5: CVec3.Y = (double)value; break; case 6: CVec3.Z = (double)value; break; case 7: CVec4 = new V3D { X = (double)value }; break; case 8: CVec4.Y = (double)value; break; case 9: CVec4.Z = (double)value; break; case 10: Point4 = new V3D { X = (double)value }; break; case 11: Point4.Y = (double)value; break; case 12: Point4.Z = (double)value; break; case 13: Roll4 = (double)value; break; case 14: Radius2 = (double)value; block++; i = 0; break; } break; case 4: // extras if (i++ % 2 == 0) { xname = GetLCString(value); } else { switch (xname) { case "velocity": Velocity = (double)value; break; case "event0": Event0 = (string)value; break; case "event1": Event1 = (string)value; break; case "event2": Event2 = (string)value; break; case "eventall0": Eventall0 = (string)value; break; case "eventall1": Eventall1 = (string)value; break; case "eventall2": Eventall2 = (string)value; break; case "isolated": Isolated = (string)value; break; default: extras.Add(xname); extras.Add(GetString(value)); break; } } break; } } if (extras.Count > 0) { Extras = String.Join(" ", extras); } ScnType = "track"; SourcePath = path; Name = node.Name; SourceIndex = node.SourceIndex; SourceLength = node.SourceLength; }
public V3D Cross(V3D a) { return(new V3D(Y * a.Z - Z * a.Y, Z * a.X - X * a.Z, X * a.Y - Y * a.X)); }
/// <summary> /// Returns scenery vector objects in proximity of given point /// </summary> /// <param name="x0"></param> /// <param name="distance"></param> /// <returns></returns> public ScnVectorObjects <T> FindInProximity(V3D p, double distance = 10) { return((ScnVectorObjects <T>) this.Where(v => v.DistanceTo(p) < distance).ToList()); }
public double Dot(V3D a) { return X * a.X + Y * a.Y + Z * a.Z; }
public Bezier2(V3D a, V3D b, V3D c) { A = a; B = b; C = c; }
public static ScnMemCellCollection Parse(ref string text, List <string> param_list, V3D rotation) { var cells = new ScnMemCellCollection(); try { var lexer = new ScnNodeLexer(text, "memcell"); if (lexer.Nodes != null) { foreach (var node in lexer.Nodes) { var m = new ScnMemCell(node, param_list, rotation); cells.Add(m.Name, m); log.Trace("memcell, type {0}, name {1} params {2}", m.Type, m.Name, param_list); } } } catch (Exception x) { log.Error(x.Message + "\r\n" + x.StackTrace + "\r\n"); //System.Windows.Forms.MessageBox.Show(x.Message + "\r\n\r\n" + x.StackTrace); } return(cells); }
public ScnBezier(V3D p1, V3D c1, V3D c2, V3D p2) : base(p1, p1 + c1, c2 + p2, p2) { }
/// <summary> /// Creates a cubic Bézier curve. /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> /// <param name="d"></param> public BezierCurveLength3DCubic(V3D a, V3D b, V3D c, V3D d) { A = a; B = b; C = c; D = d; }
/// <summary> /// Creates a quadratic Bézier curve. /// </summary> /// <param name="a">Start point.</param> /// <param name="b">Control point.</param> /// <param name="c">End point.</param> public BezierCurveLength3DQuadratic(V3D a, V3D b, V3D c) { P0 = a; P1 = b; P2 = c; }
public double Dot(V3D a) { return(X * a.X + Y * a.Y + Z * a.Z); }
public Bezier3(V3D a, V3D b, V3D c, V3D d) { A = a; B = b; C = c; D = d; }
public double DistanceTo(V3D p) { throw new NotImplementedException(); }
/// <summary> /// Creates new project file /// </summary> /// <param name="path">File system path</param> /// <param name="role">Project role</param> internal ProjectFile(string path, Roles role = Roles.Any, string par = "") { log = NLog.LogManager.GetCurrentClassLogger(); Role = role; Path = path.Replace('/', '\\'); Type = GetFileType(Path); char[] delim = { ' ', '\t', ';', '\r', '\n' }; FileParams = par.Split(delim).ToList(); if (Role == Roles.Timetable) { Type = Types.Timetable; } if (Type == Types.SceneryMain) { Role = Roles.Main; } var sceneryIndex = path.IndexOf(Properties.Settings.Default.SceneryDirectory, StringComparison.InvariantCultureIgnoreCase); var sceneryLength = _SceneryDirectoryName.Length; Directory = System.IO.Path.GetDirectoryName(Path); FileName = System.IO.Path.GetFileName(Path); if (sceneryIndex >= 0 && (Role == Roles.Include || Role == Roles.Timetable || Type == Types.SceneryMain || Type == Types.SceneryPart)) { BaseDirectory = Path.Substring(0, sceneryIndex).Trim(new[] { '\\' }); SceneryDirectory = BaseDirectory + "\\" + Path.Substring(sceneryIndex, sceneryLength); RelativeDirectory = Directory.Replace(SceneryDirectory, ""); } //if (MainFiles != null && MainFiles.Exists(i => i.Path == Path)) return; if (Type == Types.SceneryMain && Role == Roles.Main) { GetScenery(this); } //else { // if (MainFiles == null) MainFiles = new List<ProjectFile>(); // MainFiles.Add(this); //} var input = !Role.HasFlag(Roles.Description) ? new ScnSyntax.Comment().Replace(Text, "") : Text; Rotoation = new V3D(); var m = new ScnSyntax.Rotate().Matches(input); if (m.Count > 0) { var s = Tools.ChangeParamsToText(m[0].Value, FileParams); var l = s.Split(delim).ToList(); for (int i = 0; i < l.Count; i++) { double d; if (double.TryParse(l[i], System.Globalization.NumberStyles.Number, FP, out d)) { switch (i) { case 0: Rotoation.X = d; break; case 1: Rotoation.Y = d; break; case 2: Rotoation.Z = d; break; } } } } }