| |
1 #include <assert.h> |
| |
2 #include "common.h" |
| |
3 #include "types.h" |
| |
4 #include "misc.h" |
| |
5 |
| |
6 // ============================================================================= |
| |
7 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| |
8 // ============================================================================= |
| |
9 vertex vertex::midpoint (vertex& other) { |
| |
10 vertex mid; |
| |
11 mid.x = (x + other.x); |
| |
12 mid.y = (y + other.y); |
| |
13 mid.z = (z + other.z); |
| |
14 return mid; |
| |
15 } |
| |
16 |
| |
17 // ============================================================================= |
| |
18 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| |
19 // ============================================================================= |
| |
20 str vertex::getStringRep (const bool bMangled) { |
| |
21 const char* sFormat = (bMangled) ? "(%s, %s, %s)" : "%s %s %s"; |
| |
22 |
| |
23 return str::mkfmt (sFormat, |
| |
24 ftoa (x).chars(), |
| |
25 ftoa (y).chars(), |
| |
26 ftoa (z).chars()); |
| |
27 } |
| |
28 |
| |
29 // ============================================================================= |
| |
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| |
31 // ============================================================================= |
| |
32 void vertex::transform (matrix mMatrix, vertex pos) { |
| |
33 double x2, y2, z2; |
| |
34 x2 = (mMatrix[0] * x) + (mMatrix[1] * y) + (mMatrix[2] * z) + pos.x; |
| |
35 y2 = (mMatrix[3] * x) + (mMatrix[4] * y) + (mMatrix[5] * z) + pos.y; |
| |
36 z2 = (mMatrix[6] * x) + (mMatrix[7] * y) + (mMatrix[8] * z) + pos.z; |
| |
37 |
| |
38 x = x2; |
| |
39 y = y2; |
| |
40 z = z2; |
| |
41 } |
| |
42 |
| |
43 // ============================================================================= |
| |
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| |
45 // ============================================================================= |
| |
46 matrix::matrix (vector<double> vals) { |
| |
47 assert (vals.size() == (sizeof faValues / sizeof *faValues)); |
| |
48 memcpy (&faValues[0], &(*vals.begin ()), sizeof faValues); |
| |
49 } |
| |
50 |
| |
51 // ----------------------------------------------------------------------------- |
| |
52 matrix::matrix (double fVal) { |
| |
53 for (short i = 0; i < 9; ++i) |
| |
54 faValues[i] = fVal; |
| |
55 } |
| |
56 |
| |
57 // ----------------------------------------------------------------------------- |
| |
58 matrix::matrix (double a, double b, double c, double d, double e, double f, |
| |
59 double g, double h, double i) |
| |
60 { |
| |
61 faValues[0] = a; faValues[1] = b; faValues[2] = c; |
| |
62 faValues[3] = d; faValues[4] = e; faValues[5] = f; |
| |
63 faValues[6] = g; faValues[7] = h; faValues[8] = i; |
| |
64 } |
| |
65 |
| |
66 // ============================================================================= |
| |
67 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| |
68 // ============================================================================= |
| |
69 void matrix::zero () { |
| |
70 memset (&faValues[0], 0, sizeof faValues); |
| |
71 } |
| |
72 |
| |
73 // ============================================================================= |
| |
74 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| |
75 // ============================================================================= |
| |
76 matrix matrix::mult (matrix mOther) { |
| |
77 matrix mVal; |
| |
78 matrix& mThis = *this; |
| |
79 |
| |
80 mVal.zero (); |
| |
81 |
| |
82 // arrrrrrrrrrrgh |
| |
83 for (short i = 0; i < 3; ++i) |
| |
84 for (short j = 0; j < 3; ++j) |
| |
85 for (short k = 0; k < 3; ++k) |
| |
86 mVal[(i * 3) + j] += mThis[(i * 3) + k] * mOther[(k * 3) + j]; |
| |
87 |
| |
88 return mVal; |
| |
89 } |
| |
90 |
| |
91 // ============================================================================= |
| |
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| |
93 // ============================================================================= |
| |
94 void matrix::testOutput () { |
| |
95 for (short i = 0; i < 3; ++i) { |
| |
96 for (short j = 0; j < 3; ++j) |
| |
97 printf ("%*f\t", 10, faValues[(i * 3) + j]); |
| |
98 |
| |
99 printf ("\n"); |
| |
100 } |
| |
101 } |