Professional Documents
Culture Documents
Lines in 3D
CM0268
MATLAB
DSP
GRAPHICS
515
x = x0 + f t
y = y0 + gt
z = z0 + ht
JJ
II
J
I
Back
Close
Parametric Lines in 3D
p = (x, y, z)
w
v0 = (x0 , y0 , z0 )
tw
v + tw
v
CM0268
MATLAB
DSP
GRAPHICS
516
x = x0 + f t
y = y0 + gt
z = z0 + ht
1
JJ
II
J
I
Back
Close
x = x0 + f t
y = y0 + gt
z = z0 + ht
CM0268
MATLAB
DSP
GRAPHICS
517
This builds on the 2D example we met earlier, line par point dist 2d.
The 3D form is line par point dist 3d.
dx =
dy =
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
518
JJ
II
J
I
Back
Close
Implicit Surfaces
An implicit surface (just like implicit curves in 2D) of the form
f (x, y, z) = 0
CM0268
MATLAB
DSP
GRAPHICS
519
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
520
d
O
ax + by + cz + d = 0
1
Is normalised if a + b + c = 1.
Like 2D the normal vector the surface normal is given by a
vector n = (a, b, c)
a, b and c are the cosine angles which the normal makes with
the x-,y - and z -axes respectively.
JJ
II
J
I
Back
Close
(f2 , g2 , h2 )
(x0 , y0 , z0 )
(f1 , g1 , h1 )
CM0268
MATLAB
DSP
GRAPHICS
521
x = x0 + f 1 u + f 2 v
y = y0 + g1u + g2v
z = z0 + h1u + h2v
This is an extension of parametric line into 3D where we now
have two variable parameters u and v that vary.
(f1, g1, h1) and (f2, g2, h2) are two different vectors parallel to the
plane.
JJ
II
J
I
Back
Close
(f1 , g1 , h1 )
CM0268
MATLAB
DSP
GRAPHICS
522
O
x = x0 + f 1 u + f 2 v
y = y0 + g1u + g2v
z = z0 + h1u + h2v
A point in the plane is found by adding proportion u of one
vector to a proportion v of the other vector
If the two vectors are have unit length and are perpendicular,
then:
JJ
II
J
I
Back
Close
523
d=
a2 + b2 + c2
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
524
x = x0 + f t
y = y0 + gt
z = z0 + ht
then the angle, between the line and the normal the plane (a, b, c)
is:
= cos1(af + bg + ch)
JJ
II
J
I
Back
Close
= cos1 p
(a2 + b2 + c2 )
(af + bg + ch)
, = cos1 p
(f 2 + g 2 + h2 )
(af + bg + ch)
, = cos1 p
(a2 + b2 + c2 )(f 2 + g 2 + h2 )
CM0268
MATLAB
DSP
GRAPHICS
525
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
526
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
527
norm1 = sqrt ( a1 * a1 + b1 * b1 + c1 * c1 );
if ( norm1 == 0.0 )
angle = Inf;
return
end
1
norm2 = sqrt ( a2 * a2 + b2 * b2 + c2 * c2 );
if ( norm2 == 0.0 )
angle = Inf;
return
end
cosine = ( a1 * a2 + b1 * b2 + c1 * c2 ) / ( norm1 * norm2 );
angle = acos ( cosine );
JJ
II
J
I
Back
Close
P (x, y)
CM0268
MATLAB
DSP
GRAPHICS
528
x = x0 + f t
y = y0 + gt
z = z0 + ht
The the point, P (x, y), where the is given by parameter, t:
t=
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
529
norm2 = sqrt ( f * f + g * g + h * h );
if ( norm2 == 0.0 )
error ( Norm2 = 0 - Fatal error! )
end
denom = a * f + b * g + c * h;
if ( abs ( denom ) < tol * norm1 * norm2 ) % The line and the plane may be parallel.
if ( a * x0 + b * y0 + c * z0 + d == 0.0 )
intersect = 1;
p(1) = x0;
p(2) = y0;
p(3) = z0;
else
intersect = 0;
p(1:dim_num) = 0.0;
end
else
intersect = 1;
t = - ( a * x0 + b * y0 + c * z0 + d ) / denom; % they must intersect.
p(1) = x0 + t * f;
p(2) = y0 + t * g;
p(3) = z0 + t * h;
end
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
530
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
531
detinv = 1/det;
p(1) = (b1*dc - d1*bc - c1*db)*detinv;
p(2) = (d1*ac - a1*dc - c1*ad)*detinv;
p(3) = (b1*ad + a1*db - d1*ab)*detinv;
return;
end;
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
532
Two planes
x = x0 + f t
y = y0 + gt
z = z0 + ht
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
533
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
534
det = f*f + g*g + h*h;
if (abs(det) < tol)
error(planes_3d_2intersect_line: Planes are parallel);
end;
else
dc = d1*c2 - c1*d2;
db = d1*b2 - b1*d2;
ad = a1*d1 - a2*d1;
detinv = 1/det;
x0 = (g*dc - h*db)*detinv;
y0 = -(f*dc +h*ad)*detinv;
z0 = (f*db +g*ab)*detinv;
return;
end;
JJ
II
J
I
Back
Close
Pk
Pj
Pl
535
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
536
JJ
II
J
I
Back
Close
Parametric Surfaces
The general form of a parametric surface is
Parametric Surfaces
CM0268
MATLAB
DSP
GRAPHICS
537
This is justeasy
like a to
parametric
curvepoints
excepton
wesurface.
now have two
Advantage:
enumerate
Su
JJ
II
J
I
Back
Close
x = x0 + r cos u
y = y0 + r sin u
z = z0 + v.
CM0268
MATLAB
DSP
GRAPHICS
538
10
8
6
4
2
0
3
2
1
0
1
2
3
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
539
hold on;
for v = 1:10
for u = 1:360
theta = ( 2.0 * pi * ( u - 1 ) ) / n;
x = p0(1) + r * cos(theta);
y = p0(2) + r * sin(theta);
z = p0(3) + v;
plot3(x,y,z);
end
end
JJ
II
J
I
Back
Close
x = xc + sin(u) sin(v)
y = yc + cos(u) sin(v)
z = zc + cos(v)
CM0268
MATLAB
DSP
GRAPHICS
540
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
541
e3
P2 (x2 , y2 )
P6 (x6 , y6 )
e1
P1 (x1 , y1 )
e6
e7
P4 (x4 , y4 )
e5
e4
P7 (x7 , y7 )
P5 (x5 , y5 )
JJ
II
J
I
Back
Close
Polygons (Cont.)
P3 (x3 , y3 )
e2
e3
P2 (x2 , y2 )
P6 (x6 , y6 )
e1
P1 (x1 , y1 )
e6
e7
P4 (x4 , y4 )
e5
e4
P7 (x7 , y7 )
P5 (x5 , y5 )
CM0268
MATLAB
DSP
GRAPHICS
542
Each line is defined by two vertices the start and end points.
We can define a data structure which stores a list of points
(coordinate positions) and the edges define by indexes to two
points:
Points : {P1 (x1 , y1 ), P2 (x2 , y2 ), P3 (x3 , y3 ), . . .}
Points define the geometry of the polygon.
Edges : Edges : {e1 = (P1 , P2 ), e2 = (P2 , P3 ), . . .
Edges define the topology of the polygon.
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
543
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
544
JJ
II
J
I
Back
Close
Geometric Transformations
Some Common Geometric Transformations:
xk 0 0
xk 0
2D Scaling:
3D Scaling: 0 yk 0
0 yk
2D Rotation:
cos sin
sin cos
545
2D Shear (x axis):
2D Shear (y axis):
0 zk
CM0268
MATLAB
DSP
GRAPHICS
1 k
0 1
1 0
k 1
cos sin 0
sin cos 0
0
0 1
2D Translation
(Homogeneous Coords):
1 0 dx
0 1 dy
0 0 1
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
546
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
547
JJ
II
J
I
Back
Close
548
1 0 dx
T = 0 1 dy
0 0 1
cos sin
sin cos
with a translation?
We need to define Homogeneous Coordinates to deal with this
issue.
JJ
II
J
I
Back
Close
Homogeneous Coordinates
To work in homogeneous coordinates do the following:
CM0268
MATLAB
DSP
GRAPHICS
549
X X 0
X X 0
0 0 1
where
X X
X X
is the regular 2D transformation
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
550
cos sin
sin cos
cos sin 0
sin cos 0
0
0 1
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
551
JJ
II
J
I
Back
Close
3D Transformations
3D Rotation about x axis:
1
0
0
0 cos sin
0 sin cos
3D Rotation about y axis:
cos 0 sin
0
1 0
sin 0 cos
3D Rotation about z axis:
cos sin 0
sin cos 0
0
0 1
3D Scaling:
xk 0 0
0 yk 0
0 0 zk
CM0268
MATLAB
DSP
GRAPHICS
552
3D Translation
(Homogeneous Coords (4D):
1
0
0
0
0
1
0
0
0
0
1
0
dx
dy
dz
1
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
553
554
y = mx + c
We can define an error, i as the distance from the line:
i = yi mxi c
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
555
2 =
i2 =
2
= 0
m
2
= 0
c
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
556
which leads to
X
X
yi m
xi y i m
x2i
xi nc = 0
xi = 0
P
P P
xi yi xi yi
P
P
m =
n x2i ( xi)2
P P 2 P P
yi xi xi xi yi
P 2
P 2
c =
xi (
xi )
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
557
sumx = sum(x);
sumy = sum(y);
sumx2 = sum(x.*x);
sumxy = sum(x.*y);
1
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
558
z = ax + by + c.
1
JJ
II
J
I
Back
Close
i2
and
2
=0
a
(1)
2
=0
b
(2)
2
=0
c
(3)
CM0268
MATLAB
DSP
GRAPHICS
559
JJ
II
J
I
Back
Close
Now
2
a
= 0 gives:
X
and
2
b
CM0268
MATLAB
DSP
GRAPHICS
xizi a
x2i
xi y i c
xi = 0,
yi = 0,
560
= 0 gives:
X
yizi a
xi y i b
yi2
and
2
c
= 0 gives
X
zi a
xi b
yi nc = 0.
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
561
xi =
yi =
zi = 0.
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
Therefore, using the above fact in previous equations and solving for
a, b and c gives:
P
P
P
P
xizi yi2 yizi xiyi
P 2P 2
P
,
(4)
a =
xi yi ( xi yi ) 2
P
P
P
P
yizi x2i xizi xiyi
P 2P 2
P
b =
,
(5)
2
xi
yi (
562
xi yi )
c = 0.
(6)
JJ
II
J
I
All we now need to do is to translate back.
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
563
JJ
II
J
I
Back
Close
CM0268
MATLAB
DSP
GRAPHICS
564
THE
END
JJ
II
J
I
Back
Close