pwu::Vector3

Utility functions for three dimensional vectors, which are represented as a list of three real values.

Summary
pwu::Vector3Utility functions for three dimensional vectors, which are represented as a list of three real values.
Static Actions
zeroReturn the 3 dimensional zero vector
setReturn a vector with the given x, y and z components
indexReturn the value of the vector at the given index
xReturn the x component of the vector
yReturn the y component of the vector
zReturn the z component of the vector
equalCheck if two vectors are equal
notEqualCheck if two vectors are not equal
addAdd two vectors together and return the result
subtractSubtract one vector from another and return the result
negateReturn the negation of a vector
scaleScale a vector by a single scalar value
divideScale a vector by inverse of a single scalar value
multiplyReturn a vector that is the components of two vectors multiplied together
crossReturn the cross product of two vectors
dotCalculate the dot product of two vectors
normalizeReturn the normalization of a vector
lengthReturn the length of the vector
distanceToLineReturn the distance of the vector to a line
minimumReturn a vector that has the minimum components of two vectors
maximumReturn a vector that has the maximum components of two vectors
affineReturn a vector that is the affine combination of two vectors
barycentricReturn a vector that has the barycentric coordinates of the given point in the frame of the given three vectors
intersectReturn a vector that is the intersection of the two lines specified as two point and direction pairs.

Static Actions

zero

pwu::Vector3 zero

Return the 3 dimensional zero vector

Parameters

none

Returns

a vector with the given x and y

set

pwu::Vector3 set x y z

Return a vector with the given x, y and z components

Parameters

xthe x component
ythe y component
zthe z component

Returns

a vector with the given x and y

index

pwu::Vector3 index vec i

Return the value of the vector at the given index

Parameters

vecthe vector
ithe index to get; [0, 2]

Returns

the value of the vector at the given index

x

pwu::Vector3 x vec

Return the x component of the vector

Parameters

vecthe vector

Returns

the x component of the vector

y

pwu::Vector3 y vec

Return the y component of the vector

Parameters

vecthe vector

Returns

the y component of the vector

z

pwu::Vector3 z vec

Return the z component of the vector

Parameters

vecthe vector

Returns

the z component of the vector

equal

pwu::Vector3 equal ?-tolerance tol? vec1 vec2

Check if two vectors are equal

Parameters

tolthe tolerance to check equality, 0.0 is used if not given
vec1the first vector
vec2the second vector

Returns

true if the vectors are equal within tolerance

notEqual

pwu::Vector3 notEqual ?-tolerance tol? vec1 vec2

Check if two vectors are not equal

Parameters

tolthe tolerance to check equality, 0.0 is used if not given
vec1the first vector
vec2the second vector

Returns

true if the vectors are not equal within tolerance

add

pwu::Vector3 add vec1 vec2

Add two vectors together and return the result

Parameters

vec1the first vector
vec2the second vector

Returns

the sum of the two vectors

subtract

pwu::Vector3 subtract vec1 vec2

Subtract one vector from another and return the result

Parameters

vec1the first vector
vec2the second vector

Returns

the difference of the two vectors

negate

pwu::Vector3 negate vec

Return the negation of a vector

Parameters

vecthe vector

Returns

the negated vector

scale

pwu::Vector3 scale vec scalar

Scale a vector by a single scalar value

Parameters

vecthe vector
scalara scalar value

Returns

a vector scaled by the scalar

divide

pwu::Vector3 divide vec scalar

Scale a vector by inverse of a single scalar value

Parameters

vecthe vector
scalara scalar value

Returns

a vector divided by the scalar

multiply

pwu::Vector3 multiply vec1 vec2

Return a vector that is the components of two vectors multiplied together

Parameters

vec1the first vector
vec2the second vector

Returns

the component multiply of the two vectors

cross

pwu::Vector3 cross vec1 vec2

Return the cross product of two vectors

Parameters

vec1the first vector
vec2the second vector

Returns

the cross product of the two vectors

dot

pwu::Vector3 dot vec1 vec2

Calculate the dot product of two vectors

Parameters

vec1the first vector
vec2the second vector

Returns

the dot product of the two vectors

normalize

pwu::Vector3 normalize ?-length lenVar? vec

Return the normalization of a vector

Parameters

vecthe vector
lenVara variable name to set the length of the vector before being normalized

Returns

the normalized vector

length

pwu::Vector3 length vec

Return the length of the vector

Parameters

vecthe vector

Returns

the length of the vector

distanceToLine

pwu::Vector3 distanceToLine vec pt dir

Return the distance of the vector to a line

Parameters

vecthe vector
ptthe origin point of the line
dirthe direction vector of the line

Returns

the perpendicular distance of the vector to the line

minimum

pwu::Vector3 minimum vec1 vec2

Return a vector that has the minimum components of two vectors

Parameters

vec1the first vector
vec2the second vector

Returns

the minimum vector

maximum

pwu::Vector3 maximum vec1 vec2

Return a vector that has the maximum components of two vectors

Parameters

vec1the first vector
vec2the second vector

Returns

the maximum vector

affine

pwu::Vector3 affine s vec1 vec2

Return a vector that is the affine combination of two vectors

Parameters

sthe affine scalar
vec1the first vector
vec2the second vector

Returns

A linearly interpolated point along the directed line from vec1 to vec2.  An s of 0.0 returns vec1 and and s of 1.0 returns vec2.  Values of s less than 0.0 and greater than 1.0 are valid.  When s is less than 0.0, the point returned would be before vec1.  When s is greater than 1.0, the point returned would be after vec2.

Example

Code

# Capture extents of all database entities
lassign [pw::Database getExtents] minExt maxExt

# Compute extents centroid
set centroidPt [pwu::Vector3 affine 0.5 $minExt $maxExt]

# Enlarge extents diagonal by 10%
set minExt10 [pwu::Vector3 affine -0.1 $minExt $maxExt]
set maxExt10 [pwu::Vector3 affine 1.1 $minExt $maxExt]

puts "minExt     : [list $minExt]"
puts "maxExt     : [list $maxExt]"
puts "centroidPt : [list $centroidPt]"
puts "minExt10   : [list $minExt10]"
puts "maxExt10   : [list $maxExt10]"

Output

minExt     : {10.000170704427509 -14.405848937908303 -9.049999999999983}
maxExt     : {40.0 65.59615106209186 50.95000000000002}
centroidPt : {25.000085352213755 25.59515106209178 20.950000000000017}
minExt10   : {7.000187774870261 -22.40604893790832 -15.049999999999985}
maxExt10   : {42.99998292955725 73.59635106209188 56.950000000000024}

barycentric

pwu::Vector3 barycentric pt vec1 vec2 vec3

Return a vector that has the barycentric coordinates of the given point in the frame of the given three vectors

Parameters

ptthe point to find the barycentric coordinates of
vec1the first vector
vec2the second vector
vec3the third vector

Returns

The barycentric coordinates vector.

intersect

pwu::Vector3 intersect ?-tolerance tol? anchor1 dir1 anchor2 dir2

Return a vector that is the intersection of the two lines specified as two point and direction pairs.

Parameters

toleranceThis parameter specifies the maximum distance allowed between the closest points of approach on each line.  If the tolerance is exceeded, an error is raised.  A value of 0.0 (the default) or less disables this check.
anchor1This parameter is the start point of the first line.
dir1This value is the direction of the first line from anchor1.
anchor2This parameter is the start point of the second line.
dir2This value is the direction of the second line from anchor2.

Returns

The return value is a vector representing the intersection of the two lines.  If the lines do not intersect, the behavior differs based on whether a tolerance is specified.  If no tolerance is given, the return value represents the midpoint of the points on each line closest to the other line.  If the two lines are parallel, the return value will be the midpoint of the two anchor points.  If a tolerance is given and the two lines do not intersect or cross within the given tolerance, an error is raised.  Otherwise, the return value is the same as if no tolerance was specified.

Example

Code

# Intersect two lines at 90 degrees
set iPt [pwu::Vector3 intersect {0 0 0} {1 0 0} {1 -1 0} {0 1 0}]
puts $iPt
# Intersect two lines at 90 degrees in different planes
set iPt [pwu::Vector3 intersect {0 0 0} {1 0 0} {1 -1 1} {0 1 0}]
puts $iPt
# Fail to intersect two lines within a given tolerance
catch {pwu::Vector3 intersect -tolerance 0.1 {0 0 0} {1 0 0} \
   {1 -1 1} {0 1 0}} msg
puts $msg
# Try to intersect two parallel lines with no tolerance
set iPt [pwu::Vector3 intersect {0 0 0} {1 0 0} {0 -1 0} {1 0 0}]
puts $iPt
# Try to intersect two parallel lines with a tolerance
catch {pwu::Vector3 intersect -tolerance 0.1 {0 0 0} {1 0 0} \
   {0 -1 0} {1 0 0}} msg
puts $msg

Output

1.0 0.0 0.0
1.0 0.0 0.5
ERROR: Lines do not intersect within the given tolerance.

0.0 -0.5 0.0
ERROR: Lines are parallel.
pwu::Vector3 zero
Return the 3 dimensional zero vector
pwu::Vector3 set x y z
Return a vector with the given x, y and z components
pwu::Vector3 index vec i
Return the value of the vector at the given index
pwu::Vector3 x vec
Return the x component of the vector
pwu::Vector3 y vec
Return the y component of the vector
pwu::Vector3 z vec
Return the z component of the vector
pwu::Vector3 equal ?-tolerance tol? vec1 vec2
Check if two vectors are equal
pwu::Vector3 notEqual ?-tolerance tol? vec1 vec2
Check if two vectors are not equal
pwu::Vector3 add vec1 vec2
Add two vectors together and return the result
pwu::Vector3 subtract vec1 vec2
Subtract one vector from another and return the result
pwu::Vector3 negate vec
Return the negation of a vector
pwu::Vector3 scale vec scalar
Scale a vector by a single scalar value
pwu::Vector3 divide vec scalar
Scale a vector by inverse of a single scalar value
pwu::Vector3 multiply vec1 vec2
Return a vector that is the components of two vectors multiplied together
pwu::Vector3 cross vec1 vec2
Return the cross product of two vectors
pwu::Vector3 dot vec1 vec2
Calculate the dot product of two vectors
pwu::Vector3 normalize ?-length lenVar? vec
Return the normalization of a vector
pwu::Vector3 length vec
Return the length of the vector
pwu::Vector3 distanceToLine vec pt dir
Return the distance of the vector to a line
pwu::Vector3 minimum vec1 vec2
Return a vector that has the minimum components of two vectors
pwu::Vector3 maximum vec1 vec2
Return a vector that has the maximum components of two vectors
pwu::Vector3 affine s vec1 vec2
Return a vector that is the affine combination of two vectors
pwu::Vector3 barycentric pt vec1 vec2 vec3
Return a vector that has the barycentric coordinates of the given point in the frame of the given three vectors
pwu::Vector3 intersect ?-tolerance tol? anchor1 dir1 anchor2 dir2
Return a vector that is the intersection of the two lines specified as two point and direction pairs.
Close