Ejemplo n.º 1
0
        public static Line FromPoints(Vector p1, Vector p2)
        {
            var left  = p1.X < p2.X ? p1 : p2;
            var right = p1.X < p2.X ? p2 : p1;
            var dx    = right.X - left.X;
            var dy    = right.Y - left.Y;

            if (dx.EqualOrClose(0, 0.00001))
            {
                return(new Line(p1.X));//vertical line
            }
            var slope = dy / dx;

            if (PreciseDouble.IsInfinity(slope))
            {
                return(new Line(p1.X));//vertical line
            }
            var b = left.Y + ((left.X * -1) * slope);

            if (slope.EqualOrClose(0, 0.00001))
            {
                slope = 0;//horizontal line
            }
            return(new Line(slope, b));
        }
Ejemplo n.º 2
0
 public Line(PreciseDouble a, PreciseDouble b)
 {
     _IsVertical = false;
     _X          = 0;
     _A          = a;
     _B          = b;
 }
Ejemplo n.º 3
0
 public static Measure FromNormalizedValue(PreciseDouble value, UnitOfMeasure displayUnit)
 {
     return(new Measure()
     {
         Unit = displayUnit, normalizedValue = value
     });
 }
Ejemplo n.º 4
0
 public Line(PreciseDouble x)
 {
     _IsVertical = true;
     _X          = x;
     _A          = 0;
     _B          = 0;
 }
Ejemplo n.º 5
0
        private List <FretPosition> CalculateFretsForString(SIString str)
        {
            var frets = new List <FretPosition>();

            if (!CompensateFretPositions)
            {
                for (int i = MinimumFret; i <= MaximumFret; i++)
                {
                    frets.Add(CalculateFretPosition(str, i));
                }
            }
            else
            {
                var positions = FretCompensationCalculator.CalculateFretsCompensatedPositions(
                    str.PhysicalProperties, str.StringLength,
                    str.Tuning.FinalPitch, FretsTemperament,
                    str.ActionAtFirstFret, str.ActionAtTwelfthFret, Measure.Mm(1.1938), str.TotalNumberOfFrets);

                for (int i = 0; i < positions.Length; i++)
                {
                    PreciseDouble fretPosRatio = (str.StringLength - positions[i]).NormalizedValue / str.StringLength.NormalizedValue;
                    var           fretPos      = str.LayoutLine.P2 + (str.LayoutLine.Direction * -1) * (str.StringLength * fretPosRatio);
                    frets.Add(new FretPosition()
                    {
                        FretIndex = i - str.StartingFret, Position = fretPos, StringIndex = str.Index, PositionRatio = fretPosRatio
                    });
                }
            }
            return(frets);
        }
Ejemplo n.º 6
0
 public Vector GetPointForX(PreciseDouble x)
 {
     if (IsVertical)
     {
         return(Vector.Empty);
     }
     return(new Vector(x, B + (x * A)));
 }
Ejemplo n.º 7
0
 public static bool EqualOrClose(Vector v1, Vector v2, PreciseDouble tolerence)
 {
     if (v1.IsEmpty || v2.IsEmpty)
     {
         return(v1.IsEmpty == v2.IsEmpty);
     }
     return(v1.X.EqualOrClose(v2.X, tolerence) && v1.Y.EqualOrClose(v2.Y, tolerence));
 }
Ejemplo n.º 8
0
 public static PreciseDouble ConvertTo(PreciseDouble value, UnitOfMeasure from, UnitOfMeasure to)
 {
     if (from == to)
     {
         return(value);
     }
     return((value * from.ConversionFactor) / to.ConversionFactor);
 }
Ejemplo n.º 9
0
 public static PreciseDouble ToRadians(PreciseDouble degrees)
 {
     if (PreciseDouble.IsNaN(degrees))
     {
         return(PreciseDouble.NaN);
     }
     return(Math.PI * degrees / 180.0d);
 }
Ejemplo n.º 10
0
 public static PreciseDouble ToDegrees(PreciseDouble radians)
 {
     if (PreciseDouble.IsNaN(radians))
     {
         return(PreciseDouble.NaN);
     }
     return((radians * 180.0d) / Math.PI);
 }
Ejemplo n.º 11
0
 private UnitOfMeasure(string name, string symbol, string abv, PreciseDouble factor)
 {
     Name              = name;
     Abbreviation      = abv;
     Symbol            = symbol;
     _ConversionFactor = factor;
     IsMetric          = ((factor.DoubleValue * 100d) % 1d) == 0;
 }
Ejemplo n.º 12
0
        private SvgLayoutExporter(SILayout layout, LayoutExportConfig options)
            : base(layout, options)
        {
            Layers = new Dictionary<string, SvgGroup>();

            var svgDPI = (PreciseDouble)96;
            PixelToCm = (PreciseDouble)2.54d / svgDPI;
            PointToCm = PixelToCm / 0.75;
        }
Ejemplo n.º 13
0
 public static PreciseDouble NormalizeDegrees(PreciseDouble degrees)
 {
     degrees = degrees % 360d;
     if (degrees < 0d)
     {
         degrees += 360d;
     }
     return(degrees);
 }
Ejemplo n.º 14
0
 public PreciseDouble this[UnitOfMeasure unit]
 {
     get
     {
         return(normalizedValue / unit.ConversionFactor);
     }
     set
     {
         normalizedValue = value * unit.ConversionFactor;
     }
 }
Ejemplo n.º 15
0
 public Measure(PreciseDouble value, UnitOfMeasure unit)
 {
     if (unit != null)
     {
         normalizedValue = value * unit.ConversionFactor;
     }
     else
     {
         normalizedValue = value;
     }
     _Unit = unit;
 }
Ejemplo n.º 16
0
        public static PreciseDouble GetRelativeFretPosition(StringTuning tuning, int fret, Temperament temperament)
        {
            if (fret == 0)
            {
                return(1d);
            }

            var           pitchAtFret = FretCompensationCalculator.GetPitchAtFret(tuning.FinalPitch, fret, temperament);
            PreciseDouble fretRatio   = NoteConverter.CentsToIntonationRatio(pitchAtFret.Cents - tuning.FinalPitch.Cents);

            return((PreciseDouble)1d / fretRatio);
        }
Ejemplo n.º 17
0
        public static PreciseDouble GetAdjustedFretRatio(PitchValue tuning, int fret, Temperament temperament)
        {
            if (fret == 0)
            {
                return(0d);
            }

            var           pitchAtFret = GetPitchAtFret(tuning, fret, temperament);
            PreciseDouble fretRatio   = NoteConverter.CentsToIntonationRatio(pitchAtFret.Cents - tuning.Cents);

            return(fretRatio);
        }
Ejemplo n.º 18
0
 public Vector GetPointForY(PreciseDouble y)
 {
     if (IsVertical)
     {
         return(new Vector(X, y));
     }
     else if (IsHorizontal)
     {
         return(Vector.Empty);
     }
     return(new Vector((y - B) / A, y));
 }
Ejemplo n.º 19
0
        public void ReadXml(XmlReader reader)
        {
            reader.MoveToContent();
            var value = double.Parse(reader.GetAttribute("Value"));

            _Unit = UnitOfMeasure.GetUnitByName(reader.GetAttribute("Unit"));
            if (_Unit == null)
            {
                normalizedValue = value;
            }
            else
            {
                normalizedValue = _Unit.ConversionFactor * value;
            }
        }
Ejemplo n.º 20
0
        public Vector GetPerpendicularPoint(Vector pos, PreciseDouble dist)
        {
            for (int i = 0; i < Points.Count - 1; i++)
            {
                var p1         = Points[i].ToVector();
                var p2         = Points[i + 1].ToVector();
                var ptRelation = GetLocationRelativeToSegment(p1, p2, pos);

                if (ptRelation != PointRelation.Invalid)
                {
                    var segLine = Line.FromPoints(p1, p2);
                    var perp    = segLine.GetPerpendicular(pos);
                    return(pos + (perp.Vector * dist));
                }
            }

            return(Vector.Empty);
        }
Ejemplo n.º 21
0
 public Vector(float x, float y)
 {
     this.x = x;
     this.y = y;
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Clamps the angle between 0-360
 /// </summary>
 public void Normalize()
 {
     _Degrees = NormalizeDegrees(_Degrees);
 }
Ejemplo n.º 23
0
 private Measure(SerializationInfo info, StreamingContext context)
 {
     _Unit           = UnitOfMeasure.GetUnitByName(info.GetString("Unit"));
     normalizedValue = info.GetDouble("Value") * _Unit.ConversionFactor;
 }
Ejemplo n.º 24
0
 public static Measure Feets(PreciseDouble value)
 {
     return(new Measure(value, UnitOfMeasure.Feets));
 }
Ejemplo n.º 25
0
 public static Angle FromDegrees(PreciseDouble degrees)
 {
     return(new Angle {
         Degrees = degrees
     });
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Calculates the compensated fret positions (takes into account the pitch offset from pressing the string along the fretboard).
        /// </summary>
        /// <param name="properties">The string's physical properties.</param>
        /// <param name="stringLength">The real string length (not the scale length).</param>
        /// <param name="openTuning">The tuning of the open string (not fretted).</param>
        /// <param name="temperament">The temperament to use.</param>
        /// <param name="actionAtFirstFret">Action at the first fret, measured above the fret.</param>
        /// <param name="actionAtTwelfthFret">Action at the twelfth fret, measured above the fret.</param>
        /// <param name="fretHeight">Fret height. Set to zero for fretless.</param>
        /// <param name="numberOfFrets">Number of fret to calculate.</param>
        /// <returns>Returns an array of fret positions (distances from the bridge) starting at fret 0 (nut)</returns>
        public static Measure[] CalculateFretsCompensatedPositions(StringProperties properties, Measure stringLength, PitchValue openTuning, Temperament temperament,
                                                                   Measure actionAtFirstFret, Measure actionAtTwelfthFret, Measure fretHeight, int numberOfFrets)
        {
            var fretPositions      = new PreciseDouble[numberOfFrets + 1]; //+1 to include nut and improve readability (e.g. fretPositions[1] = fret #1 instead of fretPositions[0])
            var frettedLengths     = new PreciseDouble[numberOfFrets + 1]; //+1 to include nut and improve readability (e.g. frettedLengths[1] = fret #1 instead of frettedLengths[0])
            var frettedTensions    = new PreciseDouble[numberOfFrets + 1]; //+1 to include nut and improve readability (e.g. frettedTensions[1] = fret #1 instead of frettedTensions[0])
            var finalFretPositions = new Measure[numberOfFrets + 1];
            var fretHeightIn       = fretHeight[UnitOfMeasure.Inches];
            //Calculate the fret positions
            PreciseDouble flatLength = stringLength[UnitOfMeasure.Inches];

            for (int i = 1; i <= numberOfFrets; i++)//i=1 to skip the nut
            {
                var fretRatio = (PreciseDouble)Math.Pow(2, i / 12d);
                //fretRatio = GetAdjustedFretRatio(openTuning, i, temperament);
                fretPositions[i] = flatLength - (flatLength / fretRatio);
            }

            var gaugeOffset = (properties.StringDiameter / 2d)[UnitOfMeasure.Inches];
            var fret1Action = new Vector(fretPositions[1],
                                         actionAtFirstFret[UnitOfMeasure.Inches] +
                                         fretHeight[UnitOfMeasure.Inches]);

            var fret12Action = new Vector(fretPositions[12],
                                          actionAtTwelfthFret[UnitOfMeasure.Inches] +
                                          fretHeight[UnitOfMeasure.Inches]);

            var actionLineEq   = Line.FromPoints(fret1Action, fret12Action);
            var nutPos         = actionLineEq.GetPointForX(0);
            var saddlePos      = actionLineEq.GetPointForX(flatLength);
            var saddleToNutDir = (nutPos - saddlePos).Normalized;

            frettedLengths[0]     = (nutPos - saddlePos).Length;//actual string length
            finalFretPositions[0] = Measure.Zero;

            PreciseDouble L = frettedLengths[0];                                                                // stringLength[UnitOfMeasure.Inches];
            PreciseDouble f = openTuning.Frequency;                                                             //Frequency, Hz.
            PreciseDouble E = properties.ModulusOfElasticity;                                                   //Modulus of Elasticity, core wire, GPa

            E *= 145038d;                                                                                       //GPa to PSI

            PreciseDouble A   = properties.CoreWireArea != 0 ? properties.CoreWireArea : properties.StringArea; //Area, core wire, in²
            PreciseDouble mul = properties.UnitWeight;                                                          //String mass per unit length, lbs./ inch
            PreciseDouble g   = 386.089;                                                                        //Gravity, 386.089 in./ sec²

            //Calculate the open string tension: T = (mul * (2* L* f )^2) / g
            double T = (mul * Math.Pow((2 * L * f), 2)) / g;

            //Calculate "unstressed" string length: Lor = L / ((T / (E * A)) + 1)
            double Lor = L / ((T / (E * A)) + 1);

            for (int i = 1; i <= numberOfFrets; i++)//i=1 to skip the nut
            {
                var fretFreq    = GetPitchAtFret(openTuning, i, temperament).Frequency;
                var fretTopPos  = new Vector(fretPositions[i], fretHeightIn);
                var prevFretPos = new Vector(fretPositions[i - 1], fretHeightIn);

                if (i == 1)
                {
                    prevFretPos.Y = nutPos.Y;
                }

                var fingerPressPos = new Vector();
                fingerPressPos.X = prevFretPos.X;// (fretTopPos.X + prevFretPos.X) / 2d;
                if (i == 1)
                {
                    fingerPressPos.X = (fretTopPos.X + prevFretPos.X) / 2d;
                }
                fingerPressPos.Y = fretHeightIn;
                //fingerPressPos.Y = PreciseDouble.Max(fretHeightIn - gaugeOffset, gaugeOffset);

                //var fretCenterPos = new Vector(fretPositions[i - 1], i == 1 ? nutPos.Y : fretHeight[UnitOfMeasure.Inches]);

                //Calculate the fretted string length : Ls(n) = The sum of the distances between the top of the fret from the nut and saddle
                //var Lsn = frettedLengths[i] =
                //    (saddlePos - fretTopPos).Length +
                //    (fretTopPos - fingerPressPos).Length +
                //    (fingerPressPos - nutPos).Length;

                var Lsn = frettedLengths[i] =
                    (saddlePos - fretTopPos).Length +
                    (fretTopPos - nutPos).Length;

                //var Lsn = frettedLengths[i] = (nutPos - fretCenterPos).Length + (fretCenterPos - fretTopPos).Length + (fretTopPos - saddlePos).Length;

                //Calculate the fretted string tension : Ts(n) = ((Lsn - Lor) / Lor) * E * A
                var Tsn = frettedTensions[i] = ((Lsn - Lor) / Lor) * E * A;
                //Calculate fret compensated position: Lfret(n) = SQRT((g * Tsn )/ mul ) / (2 * fn )

                var Lfretn = MathP.Sqrt((g * Tsn) / mul) / (2d * fretFreq);

                //var opp = saddlePos.Y - fretHeightIn;
                //var theta = MathP.Asin(opp / Lfretn);
                //var fretDist = MathP.Cos(theta) * Lfretn;
                //var pos2D = saddlePos + (saddleToNutDir * Lfretn);
                //var testPos = flatLength - fretDist;

                var finalPos = flatLength - Lfretn;
                fretPositions[i]      = finalPos;
                finalFretPositions[i] = Measure.Inches(finalPos);
            }


            return(finalFretPositions);
        }
Ejemplo n.º 27
0
 public PointM(PreciseDouble x, PreciseDouble y, UnitOfMeasure unit)
 {
     this.x = new Measure(x, unit);
     this.y = new Measure(y, unit);
 }
Ejemplo n.º 28
0
 public static Measure Mm(PreciseDouble value)
 {
     return(new Measure(value, UnitOfMeasure.Mm));
 }
Ejemplo n.º 29
0
 public static Angle FromRadians(PreciseDouble radians)
 {
     return(new Angle {
         Radians = radians
     });
 }
Ejemplo n.º 30
0
 public static Measure Inches(PreciseDouble value)
 {
     return(new Measure(value, UnitOfMeasure.Inches));
 }