/// <summary> /// Compute all 16 possible hashcodes for hashing in a 4-D unit grid. /// Retrive all items with the 16 hashodes written into the supplied /// array. Items need to be added just with the first of the 16 /// hashcodes (also computed by function HashCodeGet1of16). /// </summary> public static void Get16(V4d point, int[] hca) { var xi = (long)Floor(point.X); var yi = (long)Floor(point.Y); var zi = (long)Floor(point.Z); var wi = (long)Floor(point.W); int xh0 = (int)(xi >> 1), xh1 = xh0 - 1 + ((int)(xi & 1) << 1); int yh0 = (int)(yi >> 1), yh1 = yh0 - 1 + ((int)(yi & 1) << 1); int zh0 = (int)(zi >> 1), zh1 = zh0 - 1 + ((int)(zi & 1) << 1); int dh0 = (int)(wi >> 1), dh1 = dh0 - 1 + ((int)(wi & 1) << 1); hca[0] = Combine(xh0, yh0, zh0, dh0); hca[1] = Combine(xh1, yh0, zh0, dh0); hca[2] = Combine(xh0, yh1, zh0, dh0); hca[3] = Combine(xh1, yh1, zh0, dh0); hca[4] = Combine(xh0, yh0, zh1, dh0); hca[5] = Combine(xh1, yh0, zh1, dh0); hca[6] = Combine(xh0, yh1, zh1, dh0); hca[7] = Combine(xh1, yh1, zh1, dh0); hca[8] = Combine(xh0, yh0, zh0, dh1); hca[9] = Combine(xh1, yh0, zh0, dh1); hca[10] = Combine(xh0, yh1, zh0, dh1); hca[11] = Combine(xh1, yh1, zh0, dh1); hca[12] = Combine(xh0, yh0, zh1, dh1); hca[13] = Combine(xh1, yh0, zh1, dh1); hca[14] = Combine(xh0, yh1, zh1, dh1); hca[15] = Combine(xh1, yh1, zh1, dh1); }
/// <summary> /// Multiplies matrix with a V4d. /// </summary> public static V4d Multiply(M22d mat, V4d vec) { return(new V4d(mat.M00 * vec.X + mat.M01 * vec.Y, mat.M10 * vec.X + mat.M11 * vec.Y, vec.Z, vec.W)); }
/// <summary> /// Multiplacation of a <see cref="Scale3d"/> with a <see cref="V4d"/>. /// </summary> public static V4d Multiply(Scale3d scale, V4d vec) { return(new V4d(scale.X * vec.X, scale.Y * vec.Y, scale.Z * vec.Z, vec.W)); }
/// <summary> /// Compute the first of 16 possible hashcodes for hashing in a 4-D /// unit grid. Add items with this function, retrieve with function /// HashCode.Get16. /// </summary> public static int Get1of16(V4d point) { var xi = (long)Floor(point.X); var yi = (long)Floor(point.Y); var zi = (long)Floor(point.Z); var wi = (long)Floor(point.W); return(Combine((int)(xi >> 1), (int)(yi >> 1), (int)(zi >> 1), (int)(wi >> 1))); }
/// <summary> /// Calculates the centroid for a given set of V4ds. /// </summary> public static V4d ComputeCentroid(this V4d[] vectors) { V4d sum = V4d.Zero; for (var i = 0; i < vectors.Length; i++) { sum += vectors[i]; } return(sum / (double)vectors.Length); }
public V4d MatrixColumnIndexerUnsafe() { V4d accum = V4d.Zero; for (int i = 0; i < count; i++) { accum += matrices[i].ColUnsafe(indices[i].X); } return(accum); }
public V4d MatrixRowIndexerIf() { V4d accum = V4d.Zero; for (int i = 0; i < count; i++) { accum += matrices[i].RowIf(indices[i].X); } return(accum); }
/// <summary> /// Calculates the centroid for a given set of V4ds. /// </summary> public static V4d ComputeCentroid(this V4d[] vectors, int[] indices) { V4d sum = V4d.Zero; for (var i = 0; i < indices.Length; i++) { sum += vectors[indices[i]]; } return(sum / (double)indices.Length); }
/// <summary> /// Calculates the sum for a given set of V4ds. /// </summary> public static V4d Sum(this IEnumerable <V4d> vectors) { V4d sum = V4d.Zero; foreach (var e in vectors) { sum += e; } return(sum); }
/// <summary> /// Calculates the sum for a given set of V4ds. /// </summary> public static V4d Sum(this V4d[] vectors) { V4d sum = V4d.Zero; for (var i = 0; i < vectors.Length; i++) { sum += vectors[i]; } return(sum); }
/// <summary> /// Calculates the centroid for a given set of V4ds. /// </summary> public static V4d ComputeCentroid(this IEnumerable <V4d> vectors) { V4d sum = V4d.Zero; int count = 0; foreach (var e in vectors) { sum += e; count++; } return(sum / (double)count); }
/// <summary> /// Calculates a weighted centroid for a given array of V4ds. /// </summary> public static V4d ComputeCentroid(this V4d[] vectors, double[] weights) { V4d sum = V4d.Zero; double weightSum = 0; for (int i = 0; i < vectors.Length; i++) { sum += weights[i] * vectors[i]; weightSum += weights[i]; } return(sum / weightSum); }
/// <summary> /// Calculates a weighted centroid for vectors and weights given by indices. /// Sum(vectors[indices[i]] * weights[indices[i]]) / Sum(weights[indices[i]]. /// </summary> public static V4d ComputeCentroid(this V4d[] vectors, double[] weights, int[] indices) { V4d sum = V4d.Zero; double weightSum = 0; for (int i = 0; i < indices.Length; i++) { var w = weights[indices[i]]; sum += w * vectors[indices[i]]; weightSum += w; } return(sum / weightSum); }
public void MatrixMultiplicationTest() { using (Report.JobTimed("Matrix multiplication tests")) { var rand = new RandomSystem(); Test.Begin("Row vector with matrix"); var m = new M44d(rand.CreateUniformDoubleArray(16)); var v = new V4d(rand.CreateUniformDoubleArray(4)); Test.IsTrue(v * m == m.Transposed * v); Test.End(); } }
public static V4d Multiply(Rot2d rot, V4d vec) { double a = (double)System.Math.Cos(rot.Angle); double b = (double)System.Math.Sin(rot.Angle); return(new V4d(a * vec.X + b * vec.Y, -b * vec.X + a * vec.Y, vec.Z, vec.W)); }
/// <summary> /// Build a geometry transformation from the given parameters as specified in Transform /// http://gun.teipir.gr/VRML-amgem/spec/part1/nodesRef.html#Transform /// </summary> public static Trafo3d BuildVrmlGeometryTrafo(V3d center, V4d rotation, V3d scale, V4d scaleOrientation, V3d translation) { // create composite trafo (naming taken from vrml97 spec) M44d C = M44d.Translation(center), Ci = M44d.Translation(-center); M44d SR = M44d.Rotation(scaleOrientation.XYZ, scaleOrientation.W), SRi = M44d.Rotation(scaleOrientation.XYZ, -scaleOrientation.W); M44d T = M44d.Translation(translation), Ti = M44d.Translation(-translation); //if (m_aveCompatibilityMode) r.W = -r.W; M44d R = M44d.Rotation(rotation.XYZ, rotation.W), Ri = M44d.Rotation(rotation.XYZ, -rotation.W); // in case some axis scales by 0 the best thing for the inverse scale is also 0 var si = new V3d(scale.X.IsTiny() ? 0 : 1 / scale.X, scale.Y.IsTiny() ? 0 : 1 / scale.Y, scale.Z.IsTiny() ? 0 : 1 / scale.Z); M44d S = M44d.Scale(scale), Si = M44d.Scale(si); return(new Trafo3d( T * C * R * SR * S * SRi * Ci, C * SR * Si * SRi * Ri * Ci * Ti)); }
/// <summary>Computes MD5 hash of given data.</summary> public static Guid ComputeMd5Hash(this V4d x) => ComputeMd5Hash(bw => { bw.Write(x.X); bw.Write(x.Y); bw.Write(x.Z); bw.Write(x.W); });
/// <summary> /// Create a plane from coefficients (a, b, c, d) of the normal equation: ax + by + cz + d = 0 /// Normal = [a, b, c]; Distance = -d /// </summary> public Plane3d(V4d coefficients) { Normal = coefficients.XYZ; Distance = -coefficients.W; }
public void Write(V4d x) { Write(x.X); Write(x.Y); Write(x.Z); Write(x.W); }
public QuaternionD(V4d v) { W = v.X; X = v.Y; Y = v.Z; Z = v.W; }