14 |
24 |
15 return mid; |
25 return mid; |
16 } |
26 } |
17 |
27 |
18 // ============================================================================= |
28 // ============================================================================= |
19 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
20 // ============================================================================= |
|
21 str vertex::stringRep (const bool mangled) { |
29 str vertex::stringRep (const bool mangled) { |
22 return fmt (mangled ? "(%s, %s, %s)" : "%s %s %s", |
30 return fmt (mangled ? "(%s, %s, %s)" : "%s %s %s", |
23 ftoa (coord (X)).chars(), |
31 ftoa (coord (X)).chars(), |
24 ftoa (coord (Y)).chars(), |
32 ftoa (coord (Y)).chars(), |
25 ftoa (coord (Z)).chars()); |
33 ftoa (coord (Z)).chars()); |
26 } |
34 } |
27 |
35 |
28 // ============================================================================= |
36 // ============================================================================= |
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
37 void vertex::transform (matrix matr, vertex pos) { |
30 // ============================================================================= |
|
31 void vertex::transform (matrix<3> matr, vertex pos) { |
|
32 double x2 = (matr[0] * x ()) + (matr[1] * y ()) + (matr[2] * z ()) + pos[X]; |
38 double x2 = (matr[0] * x ()) + (matr[1] * y ()) + (matr[2] * z ()) + pos[X]; |
33 double y2 = (matr[3] * x ()) + (matr[4] * y ()) + (matr[5] * z ()) + pos[Y]; |
39 double y2 = (matr[3] * x ()) + (matr[4] * y ()) + (matr[5] * z ()) + pos[Y]; |
34 double z2 = (matr[6] * x ()) + (matr[7] * y ()) + (matr[8] * z ()) + pos[Z]; |
40 double z2 = (matr[6] * x ()) + (matr[7] * y ()) + (matr[8] * z ()) + pos[Z]; |
35 |
41 |
36 x () = x2; |
42 x () = x2; |
37 y () = y2; |
43 y () = y2; |
38 z () = z2; |
44 z () = z2; |
39 } |
45 } |
40 |
46 |
|
47 vertex vertex::operator-() const { |
|
48 return vertex (-m_coords[X], -m_coords[Y], -m_coords[Z]); |
|
49 } |
|
50 |
|
51 bool vertex::operator!= (const vertex& other) const { |
|
52 return !operator== (other); |
|
53 } |
|
54 |
|
55 double& vertex::operator[] (const Axis ax) { |
|
56 return coord ((ushort) ax); |
|
57 } |
|
58 |
|
59 const double& vertex::operator[] (const Axis ax) const { |
|
60 return coord ((ushort) ax); |
|
61 } |
|
62 |
|
63 bool vertex::operator== (const vertex& other) const { |
|
64 return coord (X) == other[X] && |
|
65 coord (Y) == other[Y] && |
|
66 coord (Z) == other[Z]; |
|
67 } |
|
68 |
|
69 vertex& vertex::operator/= (const double d) { |
|
70 for (const Axis ax : g_Axes) |
|
71 m_coords[ax] /= d; |
|
72 |
|
73 return *this; |
|
74 } |
|
75 |
|
76 vertex vertex::operator/ (const double d) const { |
|
77 vertex other (*this); |
|
78 return other /= d; |
|
79 } |
|
80 |
|
81 vertex& vertex::operator+= (vertex other) { |
|
82 move (other); |
|
83 return *this; |
|
84 } |
|
85 |
|
86 matrix::matrix (double vals[]) { |
|
87 for (short i = 0; i < 9; ++i) |
|
88 m_vals[i] = vals[i]; |
|
89 } |
|
90 |
|
91 matrix::matrix (double fillval) { |
|
92 for (short i = 0; i < 9; ++i) |
|
93 m_vals[i] = fillval; |
|
94 } |
|
95 |
|
96 matrix::matrix (initlist<double> vals) { |
|
97 assert (vals.size() == 9); |
|
98 memcpy (&m_vals[0], &(*vals.begin ()), sizeof m_vals); |
|
99 } |
|
100 |
41 // ============================================================================= |
101 // ============================================================================= |
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
102 void matrix::puts () const { |
|
103 for (short i = 0; i < 3; ++i) { |
|
104 for (short j = 0; j < 3; ++j) |
|
105 printf ("%*f\t", 10, m_vals[(i * 3) + j]); |
|
106 |
|
107 printf ("\n"); |
|
108 } |
|
109 } |
|
110 |
43 // ============================================================================= |
111 // ============================================================================= |
44 void vertex::transform (matrix<4> matr) { |
112 str matrix::stringRep () const { |
45 double x2 = (matr[0] * x ()) + (matr[4] + y ()) + (matr[8] + z ()) + matr[12]; |
113 str val; |
46 double y2 = (matr[1] * x ()) + (matr[5] + y ()) + (matr[9] + z ()) + matr[13]; |
114 for (short i = 0; i < 9; ++i) { |
47 double z2 = (matr[2] * x ()) + (matr[6] + y ()) + (matr[10] + z ()) + matr[14]; |
115 if (i > 0) |
48 double w2 = (matr[3] * x ()) + (matr[7] + y ()) + (matr[11] + z ()) + matr[15]; |
116 val += ' '; |
|
117 |
|
118 val += fmt ("%s", ftoa (m_vals[i]).chars()); |
|
119 } |
49 |
120 |
50 x () = x2 / w2; |
121 return val; |
51 y () = y2 / w2; |
|
52 z () = z2 / w2; |
|
53 } |
122 } |
|
123 |
|
124 // ============================================================================= |
|
125 void matrix::zero () { |
|
126 memset (&m_vals[0], 0, sizeof (double) * 9); |
|
127 } |
|
128 |
|
129 // ============================================================================= |
|
130 matrix matrix::mult (matrix other) { |
|
131 matrix val; |
|
132 val.zero (); |
|
133 |
|
134 for (short i = 0; i < 3; ++i) |
|
135 for (short j = 0; j < 3; ++j) |
|
136 for (short k = 0; k < 3; ++k) |
|
137 val[(i * 3) + j] += m_vals[(i * 3) + k] * other[(k * 3) + j]; |
|
138 |
|
139 return val; |
|
140 } |
|
141 |
|
142 // ============================================================================= |
|
143 matrix& matrix::operator= (matrix other) { |
|
144 memcpy (&m_vals[0], &other.m_vals[0], sizeof (double) * 9); |
|
145 return *this; |
|
146 } |
|
147 |
|
148 // ============================================================================= |
|
149 double matrix::determinant () const { |
|
150 return |
|
151 (val (0) * val (4) * val (8)) + |
|
152 (val (1) * val (5) * val (6)) + |
|
153 (val (2) * val (3) * val (7)) - |
|
154 (val (2) * val (4) * val (6)) - |
|
155 (val (1) * val (3) * val (8)) - |
|
156 (val (0) * val (5) * val (7)); |
|
157 } |