1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include <math.h>// consider <cmath> instead
#include <iostream>
#include <stdio.h>// not used
using namespace std;
struct v3d
{
double X, Y, Z, mag;// data members
// function members
// no-arg constructor to declare arrays
v3d() { X = Y = Z = mag = 0.0; }
// this constructor assigns values to X, Y, Z then calculates the magnitude
v3d(double x, double y, double z): X(x), Y(y), Z(z)
{
mag = sqrt(X*X + Y*Y + Z*Z);
}
};
// Function to calculate dot product
double dotproduct(v3d a, v3d b)
{
return a.X*b.X + a.Y*b.Y + a.Z*b.Z;
}
// Function to calcuate cross product
void crossproduct(v3d A, v3d B, v3d &C)
{
C.X = A.Y * B.Z - A.Z * B.Y;
C.Y = A.Z * B.X - A.X * B.Z;
C.Z = A.X * B.Y - A.Y * B.X;
}
// To calculate the magnitude of a vector
void magnitude(v3d &a)
{
a.mag = sqrt(dotproduct(a,a));
}
// To create a normalised vector given two other vectors
void normalise (v3d &v, v3d a, v3d b)
{
v3d temp( b.X - a.X, b.Y - a.Y, b.Z - a.Z );
v.X = temp.X/temp.mag;//
v.Y = temp.Y/temp.mag;// function for v3d * scalar
v.Z = temp.Z/temp.mag;
magnitude(v);
}
void pprint(v3d A)
{
cout<<A.X<<"\t"<<A.Y<<"\t"<<A.Z<<"\t"<<A.mag<<"\n";
}
//creating the function
void Bezier(v3d P1,v3d P2,v3d P3,v3d P4, v3d Pa[9])
{
// all components and magnitudes of P1, P2, P3 and P4 were assigned before this function was called
// assign values to u and v array
double u[3] = {0.0, 0.5, 1.0 }, v[3] = {0.0, 0.5, 1.0 };
for(int i=0; i<3; ++i)
for(int j=0; j<3; ++j)
{
Pa[3*i+j].X = ( P1.X*(1-u[i] ) + P2.X*u[i] )*( 1 - v[j] ) + ( P3.X*(1-u[i] ) + P4.X*u[i] )* v[j];
Pa[3*i+j].Y = ( P1.Y*(1-u[i] ) + P2.Y*u[i] )*( 1 - v[j] ) + ( P3.Y*(1-u[i] ) + P4.Y*u[i] )* v[j];
Pa[3*i+j].Z = ( P1.Z*(1-u[i] ) + P2.Z*u[i] )*( 1 - v[j] ) + ( P3.Z*(1-u[i] ) + P4.Z*u[i] )* v[j];
magnitude(Pa[3*i+j]);
pprint(Pa[3*i+j]);
}
}
int main ()
{
// using the new constructor. mag is also calculated
v3d P1(2,2,2), P2(12,2,2), P3(3,16,6), P4(13,16,6);
v3d Pb[9];// uses the no-arg constructor
Bezier (P1,P2,P3,P4,Pb);
//Triangle 1
v3d temp1, temp2, temp3, uhat;
//directional vector to create cross product
d(Pb[0], Pb[3], Pb[1], temp1,temp2);
//cross product of two 3d vectors
crossproduct (temp1,temp2, temp3);
magnitude(temp3);
uhat.X = temp3.X / temp3.mag;
uhat.Y = temp3.Y / temp3.mag;
uhat.Z = temp3.Z / temp3.mag;
magnitude(uhat);
//uhat is the orthogonal vector to the triangular surface.
cout<<"uhat\n";
pprint(uhat);
//Triangle 2
v3d temp4, temp5, temp6, vhat;
d(S[3], S[4], S[1], temp4,temp5);
crossproduct (temp4,temp5, temp6);
magnitude(temp6);
vhat.X = temp6.X / temp6.mag;
vhat.Y = temp6.Y / temp6.mag;
vhat.Z = temp6.Z / temp6.mag;
magnitude(vhat);
cout<<"vhat\n";
pprint(vhat);
Triangle 3
v3d temp7, temp8, temp9, what;
d(S[3], S[6], S[4], temp7,temp8);
crossproduct (temp7,temp8, temp9);
magnitude(temp9);
what.X = temp9.X / temp9.mag;
what.Y = temp9.Y / temp9.mag;
what.Z = temp9.Z / temp9.mag;
magnitude(what);
cout<<"what\n";
pprint(what);
Triangle 4
v3d temp10, temp11, temp12, ahat;
d(S[6], S[7], S[4], temp10,temp11);
crossproduct (temp10,temp11, temp12);
magnitude(temp12);
ahat.X = temp12.X / temp12.mag;
ahat.Y = temp12.Y / temp12.mag;
ahat.Z = temp12.Z / temp12.mag;
magnitude(ahat);
cout<<"ahat\n";
pprint(ahat);
}
}
|