math primer for cisc/cmpe 330 part 1: vector geometry · 2018-09-12 · defined by orthonormal...
TRANSCRIPT
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Math primer for CISC/CMPE 330
part 1: Vector geometry
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Examples
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Location/position vector in 2D
x
yp=P(x,y) =
x = | p | *cos(a)
y= | p | *sin(a)
x
y
length (p) = | p | = sqrt(x2 + y2)
a
P
p
| p |
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Location/position vector in 3D
x
y
zp =P(x,y,z) =
x
yx
y
length (p) = | p | = sqrt(x2 + y2 + z2)
_
P
z
zp
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Unit vector
P(x,y,z)
x
y
length(p) = 1
sqrt(x2 + y2+ z2) = 1
p
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Scaling up/down a vector
P(x,y,z)
x
y Q = s * P = (s*x,s*y,s*z)
s> 1 stretch
s< 1 shrink
s= 1 leave it alone
s= 0 probably wrong…
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Unit vector
a
|v| =1
Normalize a by scaling down the vector by its own length
v = a / |a| or v = a /length(a)
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Create unit vector from a vector (Example)
Q(3,4,0)
x
y
length(Q) =sqrt(3*3 + 4*4+0*0) = sqrt(25) = 5
v = (3/5, 4/5, 0)
v so that |v| =1
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Sum of vectors
p1 (x1,y1,z1)
x
y
Concatenate the vectors
p2 (x2,y2,z2)
p1 + p2 = p3 (x1+x2, y1+y2, z1+z2)
p2p3
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Subtraction of vectors
p1 (x1,y1,z1)
x
y
Reverse p2 and concatenate to p1
p3= p1 - p2 = p1 + (- p2)
p3 (x1-x2, y1-y2, z1-z2)
p2(x2,y2,z2)
-p2
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Vector from A to B
x
y
AB = B – A = (x1-x2, y1-y2, z1-z2)
Subtract A from B
A (x2,y2,z2)
B (x1,y1,z1)
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Distance between A and B
x
y
Distance = length( AB) = length(B – A) =
sqrt( (x1-x2)2 + (y1-y2)
2 + (z1-z2) 2 )
A (x2,y2,z2)
B (x1,y1,z1)
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Vector equation of a line
v
P
L = P + t*vP is called holding point or fixed point
L is called the running point along the line
Equation broken to x,y,z components
Lx = Px + t*vx
Ly = Py + t*vy
Lz = Pz + t*vz
Infinite line:
t=(-inf,inf)
Ray:
t=(0,inf)
Line segment:
t=(tmin, tmax)
L
x
x
y
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Unit direction vector of a line
v
P
L
x
Obtain the unit direction vector:
AB = B – A Subtract
v= AB / |AB| Normalize
x
x
A
B
Line defined by two points
v
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Vector equation of a line
v
P
L = P + t*v
then
L - P = t*v
then
|L - P | = t|v |
Now if |v |=1 (v was normalized)
then
|L - P | = t
‘t’ measures the distance between the
holding point (P) and running point (L)
L
x
x
y
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Cross product (or vector product) of u and v is
denoted as q = u x v, where u,v,q are 3D vectors
denoted as u(x1,y1,z1), v(x2,y2,z2), q(x3,y3,z3),
|q | = |u| * |v |* sin(a)
q is perpendicular to both u and v, and
x3=y1*z2-y2*z1
y3=x2*z1-x1*z2
z3=x1*y2-x2*y1
Cross product
v
u
a
Cross product = 0 if and only if
sin(a)=0
i.e. u and v are parallel
u
v
q
NOT COMMUTATIVE! ORDER MATTERS !
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Area of a triangle
A= ½ |b|* h --- area of triangle
h=|c|*sin(a)
A= ½ |b|* |c| *sin(a)
A= ½ | b x c |
c
b
a
For non-zero b and c, the area is 0 if and only if sin(a) =0
i.e. c and b are parallel, so a=0
a
h
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Is the point P on the line?
x
yP
S
v
P-S=c
P is on the line if and
only if
a=0
|v x c| =0
(if v and c are parallel
L
a
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Ortho-Normal vector base from 3 points
A
C
B
x = AB
z
z = x x AC
y = z x x
x
y
Step 1 Ortho: Use cross products
to create three pairwise orthogonal
vectors, while keeping the right hand rule
Step 2 Normal: Normalize x, y, z
to make them unit length
The simplest test case is
A(0,0,0), B(1,0,0), C(0,1,0).
Yielding x(1,0,0), y(0,1,0) z(0,0,1)
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Cartesian coordinate system
defined by orthonormal vector base and center
O
C
B
z
x
y
A
To fix a coordinate system, to the
ABC triangle, we create an
orthonormal vector base and
select a center.
The center can be anywhere, but
we must be consistent with the
choice. Commonly, we place it in
the Center of Gravity (COG),
which is the same as the
unweighted average of the points:
O= (A+B+C)/ 3
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Dot product (or scalar product) = dot(u,v) = u*v
u(x1,y1,z1) v(x2,y2,z2)
dot product = u * v = |u| * |v| * cos(a) =(x1x2 + y1y2 + z1z2)
Dot product
u
v
a
Dot product = 0 if and only if cos(a)=0,
i.e. u and v are perpendicular u
v90
It is commutative, so order does not matter
Result is a scalar number, not a vector
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Dot product and the length of a vector
Length = square root of the dot product with itself
v=(x,y,z)
length(v) = sqrt(x2 + y2+ z2) = sqrt( dot(v,v) )
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Some More Dot Product Facts
ab = ba commutative
(ab)c != a (bc) not associative
(a + b )c = ac + bc distributive with addition
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Angle between vectors
u
v
a
dot(u,v)=length(u) * length(v) * cos(a)
If u and v are unit vectors:
dot(u,v) =cos(a)
a = ...by inverse…
(length(cross(u,v)) =length(u) * length(v) * sin (a)
If u and v are unit vectors:
(length(cross(u,v)) = sin (a)
a = ...by inverse…
OR
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Distance of a point from a line
v is a known unit vector, so length(v) =1
c=P-A -- this is a known vector
dot(v,c) = vc = |v| * |c |* cos(a) = |c|*cos(a) = |a|
a= v * |a| = v(vc)
d = c – a = c- v(vc)
dist = |d| = |c- v(vc)| or d2 = c2- (vc)2
P
A
_
v
a
d
c
a
Distance=|d|
90
P
A
_
v
d= ?
90
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Intersection of two lines in 3D ?
v1_
P1
L1 = P1 + t1*v1
L2 = P2 + t2 *v2
----------------------
L1x = P1x + t1 *v1x
L1y = P1y + t1 *v1y
L1z = P1z + t1 *v1z
L2x = P2x + t2 *v2x
L2y = P2y + t2 *v2y
L2z = P2z + t2 *v2z
where
t=(-inf,inf)
u=(-inf,inf)
_
L1
x
v2
_
P2
_
L2
x
x
x
Write up the equations of the 2 lines
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Intersection of two lines in 3D ?
In the intersection point :
L1=L2
or by components
L1x = L2x
L1y = L2y
L1z = L2z
v1
_
P1
_
L1
xv2
_
P2
_
L2
x
x
x
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Intersection of two lines in 3D – IMPOSSIBLE
Generally, two lines avoid each other in 3D space.
In the intersection point L1 equals L2:
L1x –L2x= 0 = P1x - P2x + t1*v1x - t2 *v2x
L1y –L2y= 0 = P1y - P2y + t1*v1y - t2 *v2y
L1z –L2z= 0 = P1z - P2z + t1*v1z - t2 *v2z
where
t=(-inf,inf)
u=(-inf,inf)
Trouble: 3 eqs, 2 unknowns there is no guaranteed solution
The lines might intersect, but they do not have to. When they intersect, one of the
three eqs cancels out. In other words: a linear combination of any two yields the
third one. (We have to be extremely lucky for this to happen.)
We can approximate the solution: find the shortest distance between the
two lines and find the point in midway.
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Intuition: Find the line (L3) that is perpendicular to both lines
L1L2
9090d/2
d/2
P2
ML3
x
P1
x
v1
v2
xv3
v1 x v2 = v3
REMINDER: If v1(x1,y1,z1), v2(x2,y2,z2),
and v3(x3,y3,z3), then the cross product is
x3=y1*z2-y2*z1
y3=x2*z1-x1*z2
z3=x1*y2-x2*y1
Approximate intersection of two lines in 3D
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Write up the equation of each line
L1L2
9090
P2
L3x
P1
x
v1
v2
x
v3
L1 = P1 + t1*v1
L2 = P2 + t2 *v2
L3 = P3 + t3 *v3
P3
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Derive conditions, make an equation system
=L1 L29090
P2
L3
x
P1x
v1
v2
v3
P3 = L2
L3 = L1
v1 x v2 = v3
[1] L1 = P1 + t1*v1
[2] L2 = P2 + t2 *v2
[3] L3 = P3 + t3 *v3
=P3
Conditions of
intersection
Equations
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Solve the vector equation system
P3 = L2 -- replace P3 in [3]
L3 = L1 -- replace L3 in [3]
[1] L1 = P1 + t1*v1
[2] L2 = P2 + t2 *v2
[3] L3 = P3 + t3 *v3
where
[1] L1 = P1 + t1*v1 -- plug [1] in [3]
[2] L2 = P2 + t2 *v2 -- plug [1] in [3]
[3] L1 = L2 + t3 *v3
P1 + t1*v1 = P2 + t2 *v2 + t3 *v3 – arrange
P1 - P2 = -t1*v1 + t2 *v2 + t3 *v3 – break it up
P1x - P2x = -t1*v1x + t2 *v2x + t3 *v3x
P1y - P2y = -t1*v1y + t2 *v2y + t3 *v3y
P1z - P2z = -t1*v1z + t2 *v2z + t3 *v3z
• 3 linear equations
• 3 unknowns
• SOLVABLE !!!
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Solve the linear equation system
P1x - P2x -v1x v2x v3x t1
P1y - P2y = -v1y v2y v3y t2
P1z - P2z -v1z v2z v3z t3
P1x - P2x t1
P1y - P2y = t2
P1z - P2z t3
-v1x v2x v3x
-v1y v2y v3y
-v1z v2z v3z
-1
M =( L1 + L2 ) /2
Error metric: distance of M from
either of the lines
L1 = P1 + t1*v1
L2 = P2 + t2 *v2
Plug t1 and t2 back
into L1 and L2 line
equations
Use any of the three methods:
1. Gaussian elimination
2. Substitution
3. Matrix inversion (shown below)
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Intersection of N lines in 3D – proper way
v1_
P1
L1 = P1 + t1*v1
L2 = P2 + t2 *v2
…
Ln = Pn + tn *vn
_
L1
x
v2
_
P2
_
L2
x
x
Write up the equations of all lines
Pose the problem as a least square
optimization:
S = d12 + d2
2 + … dn2
Compute M where S is at the minimum
Mx
d2
d1
_
Ln
_
L2
dn
vn
_
Pnx
Always compute error metric:
RMS(d1 d2… dn)
Avg (d1 d2… dn)
STD(d1 d2… dn)
Look for outliers that “stand out”,
such as di >2*STD
Remove these “bad” lines and
recompute M and the error
metrics
Keep repeating until no outlier is
left (or you run out of lines :-)
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Intersection of N lines in 3D – cheap but not too dirty
v1_
P1
L1 = P1 + t1*v1
L2 = P2 + t2 *v2
…
Ln = Pn + tn *vn
_
L1
x
v2
_
P2
_
L2
x
x
Compute pairwise approx. intersections for all
pairs (M12, M13… M1n, M2n...) then compute
M as the average of those. (i.e. Center Of
Gravity.)
If the input is “decent” (no outliers and all lines
come very close) simple average is almost as
good as the proper least square minimum.
M x
M2n
_
Ln
_
L2
vn
_
Pnx
M12
M1n
xx
x
d2
d1
dn
Always compute error metric:
RMS(d1 d2… dn)
Avg (d1 d2… dn)
STD(d1 d2… dn)
Look for outliers, such as
|Avgd-di| > 2*STD
Remove these “bad apples” and
recompute M and the error
metrics
Keep repeating until no outlier is
left (or you run out of input :-)
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Plane representation
Normal vector and one point in plane (n,P)
x P
n
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Plane normal from 3 points in the plane
x P
x
R
x
Q
___ __
n = PQ x PRn
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Is point in the plane?
n
x
P
Q
Q is in the plane if and only if PQ and n are orthogonal:
dot(PQ, n) =0
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Distance of point from plane
n
x
P
x Q
d (distance)
d = PQ * n
If d>0 Q is above
If d<0 Q is below
If d=0 Q is in plane
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Intersection of 3 planes
n1
x
P1
Q
Q lays in each of the 3 planes
dot(P1Q, n1) = 0
dot(P2Q, n2) = 0
dot(P3Q, n3) = 0
These yield a linear equation system for Q, which is always solvable if the 3 planes are non-parallel
n2
x
P2
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Intersection of line and plane
n
x
P
Q
Q is in the plane if and only if PQ and n are orthogonal:
dot(PQ, n) = 0 i.e. (A+vt-P)n= 0
Yields a linear equation for t, which is always solvable unless v and n
are orthogonal (i.e. line and plane is parallel)
vAx Q=A+vt
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Equations of sphere & ellipsoid
(x-xo)2 + (y-yo)2 + (z-zo) 2 = R2 Sphere
x [xo,yo,zo]
R
(x-xo)2 (y-yo)2 (z-zo) 2
-------- + -------- + --------- = 1 Ellipsoid
a2 b2 c2
where a,b,c are principal axes.
Use case: represent anatomical organs like the orbit, cranium, etc.
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Intersection of line and sphere
(x-xo)2 + (y-yo)2 + (z-zo) 2 = R2
Leads to a quadratic equation in “t”
There are 0,1, or 2 intersections
Check the determinant to find out how many exist
Follow the same method for ellipsoid
v
Px
LL = P+ vt
or
Lx = Px + t*vx
Ly = Py + t*vy
Lz = Pz + t*vzx=Lx
y=Ly
z=Lz
Plug into the sphere’s equation
Use cases: compute where a linear surgical tool penetrates the eye, cranium, etc.
x [xo,yo,zo]
R
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Fitting points to line in 3D – by least square
Concept: Pose the problem as a
least square optimization. Write up
the cost function as sum of square
distances of all points from the object
S = d12 + d2
2 + … dn2
Minimize the cost function to find the
optimal (P,v)
v_
P
P1, P2…Pi… points observed
_ _
L = P + t*v eq. of the line
di2 = ci
2- (vci)2 sq. distance of Pi from line
x Pi
P1
xdix
P4x
P2
x
P3
x
Always compute error metric:
RMS(d1 d2… dn)
Avg (d1 d2… dn)
STD(d1 d2… dn)
Look for outliers, such as
|Avg-di| > 2*STD or
|ARMS-di| > 2*STD or
Remove these “bad apples” and
recompute the optimization and
error metrics.
Keep repeating until no outlier is
left (or you run out of input :-)
Use case: reconstruct the path of a
straight surgical tool from points observed
ci
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Fitting points to line in 3D – by average
Concept: if the input is “decent” (lots
of points and no outliers, average is
as good as least square minimum.
Compute both average P and
average v.
P=Avg (P1 P2… Pn)
v=Avg (c1 c2… cn)
Note: normalize all c1 c2.. and flip
them in one direction!
v_
P
P1, P2…Pi… points observed
_ _
L = P + t*v eq. of the line
di2 = ci
2- (vci)2 sq. distance of Pi from line
x Pi
P1
xdix
P4x
P2
x
P3
x
ci
Use case: reconstruct the path of a
straight surgical tool from points observed
Always compute error metric:
RMS(d1 d2… dn)
Avg (d1 d2… dn)
STD(d1 d2… dn)
Look for outliers, such as
|Avg-di| > 2*STD or
|ARMS-di| > 2*STD or
Remove these “bad apples” and
recompute the optimization and
error metrics.
Keep repeating until no outlier is
left (or you run out of input :-)
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Fitting points to sphere in 3D – by least square
Concept: Pose the problem as a
least square optimization. Write up
the cost function as sum of square
distances of all points from the object
S = d12 + d2
2 + … dn2
Minimize the cost function to find the
optimal (R,xo,yo,zo)
Pi
P1
xdix
P4x
P2
x
P3
x
(x-xo)2 + (y-yo)2 + (z-zo) 2 = R2 Sphere
di = |R - length(Pi,O)|
R
O[xo,yo,zo]x
Use case: reconstruct the path of a pivoting
surgical tool from points observed.
Always compute error metric:
RMS(d1 d2… dn)
Avg (d1 d2… dn)
STD(d1 d2… dn)
Look for outliers, such as
|Avg-di| > 2*STD or
|ARMS-di| > 2*STD or
Remove these “bad apples” and
recompute the optimization and
error metrics.
Keep repeating until no outlier is
left (or you run out of input :-)
Copyright © Gabor Fichtinger, 2018Laboratory for Percutaneous Surgery – The Perk Lab
Fitting points to sphere in 3D – by average
Concept: Compute an “average O”.
Ideally, any two (Pi,Pj) pair are on
the surface of the sphere. Place a
normal plane in the midpoint
between (Pi,Pj) then compute O as
the average intersection of all such
planes. Then compute average R as
R =avg ( length(PiO))
Pi
P1
xdix
P4x
P2
x
P3
x
(x-xo)2 + (y-yo)2 + (z-zo) 2 = R2 Sphere
di = |R - length(Pi,O)|
R
O[xo,yo,zo]x
Use case: reconstruct the path of a pivoting
surgical tool from points observed.
Always compute error metric:
RMS(d1 d2… dn)
Avg (d1 d2… dn)
STD(d1 d2… dn)
Look for outliers, such as
|avg-di| > 2*STD or
|RMS-di| > 2*STD or
Remove these “bad apples” and
recompute the optimization and
error metrics.
Keep repeating until no outlier is
left (or you run out of input :-)