Class Vec3d
Basic 3D point (or vector).
[BsonIgnoreExtraElements]
public class Vec3d : IEquatable<Vec3d>, IExpandToBox3d, IBinaryIo, IWriteBin, ICsvRowIo, IEqualityOperators<Vec3d, Vec3d, bool>, IAdditionOperators<Vec3d, Vec3d, Vec3d>, ISubtractionOperators<Vec3d, Vec3d, Vec3d>, IMultiplyOperators<Vec3d, double, Vec3d>, IMultiplyOperators<Vec3d, Mat4d, Vec3d>, IDivisionOperators<Vec3d, double, Vec3d>, IVec<double>, IFormattable
- Inheritance
-
Vec3d
- Implements
- Inherited Members
- Extension Methods
Constructors
Vec3d()
Ctor.
public Vec3d()
Vec3d(Polar3d)
Ctor.
public Vec3d(Polar3d src)
Parameters
src
Polar3dsrc
Vec3d(Vec3d)
Copy ctor.
public Vec3d(Vec3d src)
Parameters
src
Vec3dsrc
Vec3d(vec3d)
ctor.
public Vec3d(vec3d src)
Parameters
src
vec3dsrc
Vec3d(vec3f)
ctor.
public Vec3d(vec3f src)
Parameters
src
vec3fsrc
Vec3d(IEnumerable<double>)
Creates a vector from an enumerable collection of three double values.
public Vec3d(IEnumerable<double> src)
Parameters
src
IEnumerable<double>Source collection containing three double values
Vec3d(double, double, double)
Ctor.
public Vec3d(double x, double y, double z)
Parameters
Vec3d(Func<int, double>)
Creates a vector using a function that maps direction index to value.
public Vec3d(Func<int, double> dirToValueFunc)
Parameters
dirToValueFunc
Func<int, double>Function that takes direction index (0=X, 1=Y, 2=Z) and returns the corresponding value
Vec3d(BinaryReader)
Ctor by bytes: x = reader.ReadDouble(); y = reader.ReadDouble(); z = reader.ReadDouble();
public Vec3d(BinaryReader reader)
Parameters
reader
BinaryReaderreader
Vec3d(int, double, double, double)
Ctor by direction offset. Direction 0,1,2 indicate x,y,z respectively.
public Vec3d(int dir, double a, double b, double c)
Parameters
dir
intdirection offset
a
doublevalue at direction (0+dir)%3
b
doublevalue at direction (1+dir)%3
c
doublevalue at direction (2+dir)%3
Vec3d(string)
Ctor by string. The format is (x,y,z).
public Vec3d(string src)
Parameters
src
stringsrc
Vec3d((double, double, double))
Ctor.
public Vec3d((double, double, double) src)
Parameters
Fields
x
Value at x direction.
public double x
Field Value
y
Value at y direction.
public double y
Field Value
z
Value at z direction.
public double z
Field Value
Properties
AbsSum
[BsonIgnore]
public double AbsSum { get; }
Property Value
- double
Sum of the abs {x,y,z}. Which is Math.Abs(x) + Math.Abs(y) + Math.Abs(z).
AllOne
public static Vec3d AllOne { get; }
Property Value
CsvText
Csv text.
[BsonIgnore]
public string CsvText { get; set; }
Property Value
CsvTitleText
Csv titles text.
[BsonIgnore]
public string CsvTitleText { get; }
Property Value
ElementNum
Element number: 3 for (x,y,z).
public static int ElementNum { get; }
Property Value
IsAllFinite
[BsonIgnore]
public bool IsAllFinite { get; }
Property Value
- bool
Is x,y,z all finite.
IsAllNaN
[BsonIgnore]
public bool IsAllNaN { get; }
Property Value
IsAllNegativeInfinity
[BsonIgnore]
public bool IsAllNegativeInfinity { get; }
Property Value
- bool
is x,y,z all NegativeInfinity.
IsAllPositiveInfinity
[BsonIgnore]
public bool IsAllPositiveInfinity { get; }
Property Value
- bool
is x,y,z all PositiveInfinity.
IsAnyFinite
[BsonIgnore]
public bool IsAnyFinite { get; }
Property Value
- bool
Is at least one of x,y,z finite.
IsAnyNaN
[BsonIgnore]
public bool IsAnyNaN { get; }
Property Value
IsZero
[BsonIgnore]
public bool IsZero { get; }
Property Value
- bool
Is zero vector. Which is
x == 0 && y == 0 && z == 0
.
this[int]
Gets or sets the element at the specified index.
public double this[int dir] { get; set; }
Parameters
dir
int
Property Value
- double
The element at the specified index.
Length
[BsonIgnore]
public double Length { get; }
Property Value
- double
Geometry length of this.
LengthSquare
[BsonIgnore]
public double LengthSquare { get; }
Property Value
- double
Geometry length ^ 2. Which is
x * x + y * y + z * z
.
MaxAbsDir
[BsonIgnore]
public int MaxAbsDir { get; }
Property Value
- int
Get the direction index with maximum absolute value. If the absolute of {x,y,z} is the biggest, return {0,1,2}.
MaxDir
[BsonIgnore]
public int MaxDir { get; }
Property Value
- int
Get the direction index with maximum value. If {x,y,z} is the biggest, return {0,1,2}.
MaxValue
[BsonIgnore]
public double MaxValue { get; }
Property Value
- double
The max value selected from {x,y,z}. Which is
Math.Max(Math.Max(x, y), z)
.
MinDir
[BsonIgnore]
public int MinDir { get; }
Property Value
- int
Get the direction index with maximum value. If {x,y,z} is the biggest, return {0,1,2}.
MinValue
[BsonIgnore]
public double MinValue { get; }
Property Value
- double
The min value selected from {x,y,z}. Which is
Math.Min(Math.Min(x, y), z)
.
NaN
public static Vec3d NaN { get; }
Property Value
NativeByteSize
public static int NativeByteSize { get; }
Property Value
- int
Byte size: sizeof(double) * 3.
NegativeInfinity
public static Vec3d NegativeInfinity { get; }
Property Value
PositiveInfinity
public static Vec3d PositiveInfinity { get; }
Property Value
Rank
Dimension (i.e. Size) of the Vector.
public int Rank { get; }
Property Value
Text
Gets or sets the vector as a string representation.
[BsonIgnore]
public string Text { get; set; }
Property Value
UnitX
public static Vec3d UnitX { get; }
Property Value
UnitY
public static Vec3d UnitY { get; }
Property Value
UnitZ
public static Vec3d UnitZ { get; }
Property Value
X
Value at x direction.
[BsonElement]
public double X { get; set; }
Property Value
XY
[BsonIgnore]
public Vec2d XY { get; set; }
Property Value
Y
Value at y direction.
[BsonElement]
public double Y { get; set; }
Property Value
Z
Value at z direction.
[BsonElement]
public double Z { get; set; }
Property Value
Zero
public static Vec3d Zero { get; }
Property Value
Methods
All(double)
Creates a vector with all components set to the specified value.
public static Vec3d All(double v)
Parameters
v
doubleValue to set for all components
Returns
- Vec3d
A new vector with all components set to the specified value
At(int)
Get the value at the dirction. Direction 0,1,2 are x,y,z. If direction index is larger than 2, the return value is at z direction.
public ref double At(int dir)
Parameters
dir
intdirection index
Returns
- double
value at the direction
BilinearInterpolate(Vec3d, Vec3d, Vec3d, Vec3d, double, double)
Performs bilinear interpolation between four points.
public static Vec3d BilinearInterpolate(Vec3d p00, Vec3d p01, Vec3d p10, Vec3d p11, double u, double v)
Parameters
p00
Vec3dPoint at (0,0)
p01
Vec3dPoint at (0,1)
p10
Vec3dPoint at (1,0)
p11
Vec3dPoint at (1,1)
u
doubleInterpolation parameter in first dimension (0.0 to 1.0)
v
doubleInterpolation parameter in second dimension (0.0 to 1.0)
Returns
- Vec3d
The interpolated point
Cross(Vec3d, Vec3d)
Get a cross b.
public static Vec3d Cross(Vec3d a, Vec3d b)
Parameters
Returns
- Vec3d
a x b
Dot(Vec3d)
this dot src.
public double Dot(Vec3d src)
Parameters
src
Vec3dsrc
Returns
- double
dotted value
Enumerate()
Enumerates the components of the vector.
public IEnumerable<double> Enumerate()
Returns
- IEnumerable<double>
An enumerable sequence of the vector's components (X, Y, Z)
Equals(Vec3d)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(Vec3d other)
Parameters
other
Vec3dAn object to compare with this object.
Returns
Equals(Vec3d, double)
check equals for each component with tolerance.
public bool Equals(Vec3d other, double toleranceForEachComponent)
Parameters
Returns
- bool
check equals for each component with tolerance.
Equals(object)
Determines whether the specified object is equal to the current object.
public override bool Equals(object obj)
Parameters
obj
objectThe object to compare with the current object.
Returns
ExpandMax(Vec3d)
Find and expand maximum values from src.
public void ExpandMax(Vec3d src)
Parameters
src
Vec3dsrc
ExpandMin(Vec3d)
Find and expand minimum values from src.
public void ExpandMin(Vec3d src)
Parameters
src
Vec3dsrc
ExpandToBox3d(Box3d)
Expands the destination box. This function is usually used to compute the bounding box of elements.
public void ExpandToBox3d(Box3d dst)
Parameters
dst
Box3dDestination box
GetCosSquareWith(Vec3d)
Get Cos(theta)^2. theta is the angle between this and src
.
This function is faster than GetCosWith(Vec3d) since it lacks one square root operation.
public double GetCosSquareWith(Vec3d src)
Parameters
src
Vec3done of edge vector
Returns
- double
Cos(theta)^2
GetCosWith(Vec3d)
Get Cos(theta). theta is the angle between this and v
.
public double GetCosWith(Vec3d v)
Parameters
v
Vec3da vector
Returns
- double
Cos(theta)
GetCross(Vec3d)
Get this
cross src
.
public Vec3d GetCross(Vec3d src)
Parameters
src
Vec3dsrc
Returns
GetCsvText(string)
Gets the CSV text representation of this vector with the specified format.
public string GetCsvText(string format)
Parameters
format
stringFormat string for the double values
Returns
- string
CSV formatted string
GetEachValueAbs()
Creates a new vector with the absolute value of each component.
public Vec3d GetEachValueAbs()
Returns
- Vec3d
A new vector with absolute values of each component
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
GetMulEach(Vec3d)
Creates a new vector by multiplying each component of this vector with the corresponding component of another vector.
public Vec3d GetMulEach(Vec3d vec)
Parameters
vec
Vec3dThe vector to multiply with
Returns
- Vec3d
A new vector with component-wise multiplication results
GetMulWithoutTrans(Mat4d)
Get a new object by this*mat
without translation part.
public Vec3d GetMulWithoutTrans(Mat4d mat)
Parameters
mat
Mat4dtransform matrix
Returns
- Vec3d
new Vec3d(x * mat.At(0, 0) + y * mat.At(1, 0) + z * mat.At(2, 0) , x* mat.At(0, 1) + y* mat.At(1, 1) + z* mat.At(2, 1) , x* mat.At(0, 2) + y* mat.At(1, 2) + z* mat.At(2, 2))
GetNormalized()
Generate normalized vec.
public Vec3d GetNormalized()
Returns
- Vec3d
Normalized vec
GetRadian(Vec3d)
Get angle between this and v
.
The angle has no sign.
This vector is not required to be an unit vector.
public double GetRadian(Vec3d v)
Parameters
v
Vec3done of the edge vector. Not required to be an unit vector.
Returns
- double
Angle in radian
GetRadian(Vec3d, Vec3d)
Get angle between this and v
.
This function applies normal vector to determine the sign of angle.
This vector is not required to be an unit vector.
public double GetRadian(Vec3d v, Vec3d n)
Parameters
v
Vec3dvector of ending edge. Not required to be an unit vector.
n
Vec3dnormal vector. Not required to be an unit vector.
Returns
- double
angle in radian
GetRadianByUnitVector(Vec3d)
Get angle between this and v
.
The angle has no sign.
This vector is not required to be an unit vector.
Both this and v
should be unit vector.
Much efficient than GetRadian(Vec3d).
public double GetRadianByUnitVector(Vec3d v)
Parameters
v
Vec3done of the edge vector. Not required to be an unit vector.
Returns
- double
Angle in radian
GetTransform(Func<double, double>)
Get the new Vec3d by transforming each element by the function.
public Vec3d GetTransform(Func<double, double> transformingFunc)
Parameters
Returns
GetVec2dByPlaneDir(int)
Gets a 2D vector by projecting the 3D vector onto a plane.
public Vec2d GetVec2dByPlaneDir(int dir)
Parameters
dir
intDirection index: 0 for YZ plane, 1 for ZX plane, 2 for XY plane
Returns
- Vec2d
A 2D vector representing the projection
GetXRotation(double)
public Vec3d GetXRotation(double rad)
Parameters
rad
doublerotation radian
Returns
GetYRotation(double)
public Vec3d GetYRotation(double rad)
Parameters
rad
doublerotation radian
Returns
GetZRotation(double)
public Vec3d GetZRotation(double rad)
Parameters
rad
doublerotation radian
Returns
Interpolate(Vec3d, Vec3d, double)
Interpolate from a to b with ratio alpha:(1-alpha).
public static Vec3d Interpolate(Vec3d a, Vec3d b, double alpha)
Parameters
Returns
- Vec3d
a * (1 - alpha) + b * alpha
IsNormalized(double)
Checks if the vector is normalized (has a length of approximately 1).
public bool IsNormalized(double toleranceSquare = 1E-07)
Parameters
toleranceSquare
doubleSquare of the tolerance value for comparing with 1 (default: 1e-7)
Returns
- bool
True if the vector is normalized within the specified tolerance
MulEach(Vec3d)
Multiplies each component of this vector with the corresponding component of another vector.
public Vec3d MulEach(Vec3d vec)
Parameters
vec
Vec3dThe vector to multiply with
Returns
- Vec3d
This vector after multiplication
MulWithoutTrans(Mat4d)
this*=mat
without translation part.
public Vec3d MulWithoutTrans(Mat4d mat)
Parameters
mat
Mat4dtransformation matrix
Returns
- Vec3d
this
- See Also
Normalize()
Normalize this.
public Vec3d Normalize()
Returns
- Vec3d
this
Parse(string)
If src is not null and not empty string, return Vec3d(string); otherwise return null.
public static Vec3d Parse(string src)
Parameters
src
stringsrc
Returns
ParseByCsv(string)
Parses a vector from a CSV text string.
public static Vec3d ParseByCsv(string csvText)
Parameters
csvText
stringCSV formatted string containing vector components
Returns
- Vec3d
A new vector parsed from the CSV text
ReadBin(BinaryReader)
Reads binary data to initialize the object.
public void ReadBin(BinaryReader reader)
Parameters
reader
BinaryReaderThe binary reader to read from
Set(Vec3d)
Set values by copy.
public Vec3d Set(Vec3d src)
Parameters
src
Vec3dsrc
Returns
- Vec3d
this
Set(vec3d)
Set values by copy.
public Vec3d Set(vec3d src)
Parameters
src
vec3dsrc
Returns
- Vec3d
this
Set(double, double, double)
Set values.
public Vec3d Set(double x, double y, double z)
Parameters
Returns
- Vec3d
this
Set(double[])
Set values by array.
public Vec3d Set(double[] xyz)
Parameters
xyz
double[]double[]{x,y,z}
Returns
- Vec3d
this
Set(Func<int, double, double>)
Sets vector components using a function that maps direction index and current value to new value.
public Vec3d Set(Func<int, double, double> dirToValueFunc)
Parameters
dirToValueFunc
Func<int, double, double>Function that takes direction index (0=X, 1=Y, 2=Z) and current value, and returns the new value
Returns
- Vec3d
This vector after modification
Set(Func<int, double>)
Sets vector components using a function that maps direction index to value.
public Vec3d Set(Func<int, double> dirToValueFunc)
Parameters
dirToValueFunc
Func<int, double>Function that takes direction index (0=X, 1=Y, 2=Z) and returns the corresponding value
Returns
- Vec3d
This vector after modification
Set(int, double, double, double)
Set values by direction offset. Direction 0,1,2 indicate x,y,z respectively.
public Vec3d Set(int dir, double a, double b, double c)
Parameters
dir
intdirection offset
a
doublevalue at direction (0+dir)%3
b
doublevalue at direction (1+dir)%3
c
doublevalue at direction (2+dir)%3
Returns
- Vec3d
this
SetEachNanToZero()
Set NaN to 0 for each value.
public Vec3d SetEachNanToZero()
Returns
- Vec3d
this
SetEachValueAbs()
Set each value to absolute.
public Vec3d SetEachValueAbs()
Returns
- Vec3d
this
ToArray()
return new double[] { x, y, z }
public double[] ToArray()
Returns
- double[]
{ x, y, z }
ToBuf(double[])
Set x,y,z to the dst array.
public void ToBuf(double[] dst)
Parameters
dst
double[]dst
ToBuf(double[], ref int)
Set x,y,z to the dst array from postion p and increase p by the pushed number.
public int ToBuf(double[] dst, ref int p)
Parameters
Returns
- int
- Which is pushed number of double
ToString()
To representative string with format:(x,y,z).
public override string ToString()
Returns
- string
Representative string
ToString(string)
To string with format: (x,y,z)
public string ToString(string format)
Parameters
format
stringformat of ToString(string)
Returns
- string
Representative string
ToString(string, IFormatProvider)
Returns a string representation of the vector formatted according to the specified format.
public string ToString(string format, IFormatProvider formatProvider)
Parameters
format
stringThe format to use for each component
formatProvider
IFormatProviderThe format provider to use
Returns
- string
A formatted string representation of the vector
ToString(string, int)
Converts the vector to a string with the specified format and left padding.
public string ToString(string format, int leftPadding)
Parameters
format
stringFormat string for the double values
leftPadding
intNumber of characters to pad on the left of each value
Returns
- string
Formatted string representation of the vector
Transform(Func<double, double>)
Transform each element by the function.
public Vec3d Transform(Func<double, double> transformingFunc)
Parameters
Returns
TryParse(string, out Vec3d)
Attempts to parse a string into a Vec3d.
public static bool TryParse(string src, out Vec3d dst)
Parameters
src
stringThe string to parse in format “(x,y,z)”
dst
Vec3dWhen this method returns, contains the Vec3d value if parsing succeeded, or null if parsing failed
Returns
- bool
true if parsing succeeded; otherwise, false
WriteBin(BinaryWriter)
Output to bytes: writer.Write(x); writer.Write(y); writer.Write(z);
public void WriteBin(BinaryWriter writer)
Parameters
writer
BinaryWriterwriter
Operators
operator +(Vec3d, Vec3d)
Plus.
public static Vec3d operator +(Vec3d left, Vec3d right)
Parameters
Returns
- Vec3d
new Vec3d(a.x + b.x, a.y + b.y, a.z + b.z)
operator /(Vec3d, double)
Get a new object from a
scaled by 1/d
.
public static Vec3d operator /(Vec3d a, double d)
Parameters
Returns
operator ==(Vec3d, Vec3d)
Equality operator for comparing two Vec3d objects.
public static bool operator ==(Vec3d left, Vec3d right)
Parameters
Returns
- bool
True if the vectors are equal, false otherwise
operator !=(Vec3d, Vec3d)
Inequality operator for comparing two Vec3d objects.
public static bool operator !=(Vec3d left, Vec3d right)
Parameters
Returns
- bool
True if the vectors are not equal, false otherwise
operator *(Vec3d, Mat4d)
Get p*src.
public static Vec3d operator *(Vec3d p, Mat4d src)
Parameters
Returns
- Vec3d
new Vec3d(p.x * src.At(0, 0) + p.y * src.At(1, 0) + p.z * src.At(2, 0) + src.At(3, 0) , p.x* src.At(0, 1) + p.y* src.At(1, 1) + p.z* src.At(2, 1) + src.At(3, 1) , p.x* src.At(0, 2) + p.y* src.At(1, 2) + p.z* src.At(2, 2) + src.At(3, 2))
operator *(Vec3d, double)
Scale a by s.
public static Vec3d operator *(Vec3d a, double s)
Parameters
Returns
- Vec3d
new Vec3d(a.x * s, a.y * s, a.z * s)
operator -(Vec3d, Vec3d)
Minus.
public static Vec3d operator -(Vec3d a, Vec3d b)
Parameters
Returns
- Vec3d
new Vec3d(a.x - b.x, a.y - b.y, a.z - b.z)
operator -(Vec3d)
Get negate vector.
public static Vec3d operator -(Vec3d src)
Parameters
src
Vec3dsrc
Returns
- Vec3d
new Vec3d(-src.x, -src.y, -src.z)