00001
00023 #ifndef ELEMENT_H
00024 #define ELEMENT_H
00025
00026 #include "Tuple.h"
00027 #include <vector>
00028
00029
00030
00031
00032 typedef std::vector<Tuple> TupleList;
00033
00034 class Element {
00035 Tuple coords[3];
00036 bool bDim;
00037 TupleList tuples;
00038 public:
00039 const Tuple& getPoint(int i) const { return coords[i]; }
00040 bool getBDim(){ return bDim; }
00041 int getDim() const { return bDim ? 3 : 2; }
00042
00043 void addTuple(Tuple& newTuple) {
00044 tuples.push_back(newTuple);
00045 }
00046
00047 TupleList& getTuples() { return tuples; }
00048
00049 explicit Element(const Tuple a, const Tuple b, const Tuple c): bDim(true) {
00050 coords[0] = a;
00051 coords[1] = b;
00052 coords[2] = c;
00053 }
00054
00055 explicit Element(const Tuple a, const Tuple b): bDim(false) {
00056 coords[0] = a;
00057 coords[1] = b;
00058 }
00059
00063 bool elementContains(Tuple p) {
00064 Tuple p1 = coords[0];
00065 Tuple p2 = coords[1];
00066 Tuple p3 = coords[2];
00067
00068 if ((p1 == p) || (p2 == p) || (p3 == p)) {
00069 return false;
00070 }
00071
00072 int count = 0;
00073 double px = p.x();
00074 double py = p.y();
00075 double p1x = p1.x();
00076 double p1y = p1.y();
00077 double p2x = p2.x();
00078 double p2y = p2.y();
00079 double p3x = p3.x();
00080 double p3y = p3.y();
00081
00082 if (p2x < p1x) {
00083 if ((p2x < px) && (p1x >= px)) {
00084 if (((py - p2y) * (p1x - p2x)) < ((px - p2x) * (p1y - p2y))) {
00085 count = 1;
00086 }
00087 }
00088 } else {
00089 if ((p1x < px) && (p2x >= px)) {
00090 if (((py - p1y) * (p2x - p1x)) < ((px - p1x) * (p2y - p1y))) {
00091 count = 1;
00092 }
00093 }
00094 }
00095
00096 if (p3x < p2x) {
00097 if ((p3x < px) && (p2x >= px)) {
00098 if (((py - p3y) * (p2x - p3x)) < ((px - p3x) * (p2y - p3y))) {
00099 if (count == 1) {
00100 return false;
00101 }
00102 count++;
00103 }
00104 }
00105 } else {
00106 if ((p2x < px) && (p3x >= px)) {
00107 if (((py - p2y) * (p3x - p2x)) < ((px - p2x) * (p3y - p2y))) {
00108 if (count == 1) {
00109 return false;
00110 }
00111 count++;
00112 }
00113 }
00114 }
00115
00116 if (p1x < p3x) {
00117 if ((p1x < px) && (p3x >= px)) {
00118 if (((py - p1y) * (p3x - p1x)) < ((px - p1x) * (p3y - p1y))) {
00119 if (count == 1) {
00120 return false;
00121 }
00122 count++;
00123 }
00124 }
00125 } else {
00126 if ((p3x < px) && (p1x >= px)) {
00127 if (((py - p3y) * (p1x - p3x)) < ((px - p3x) * (p1y - p3y))) {
00128 if (count == 1) {
00129 return false;
00130 }
00131 count++;
00132 }
00133 }
00134 }
00135
00136 return count == 1;
00137 }
00138
00139 bool clockwise() {
00140 double t1_x = coords[0].x();
00141 double t1_y = coords[0].y();
00142
00143 double t2_x = coords[1].x();
00144 double t2_y = coords[1].y();
00145
00146 double t3_x = coords[2].x();
00147 double t3_y = coords[2].y();
00148
00149 double counter_clockwise = (t2_x - t1_x) * (t3_y - t1_y) - (t3_x - t1_x) * (t2_y - t1_y);
00150
00151 return counter_clockwise < 0;
00152 }
00153
00157 bool inCircle(const Tuple& p) {
00158
00159
00160
00161 double t1_x = coords[0].x();
00162 double t1_y = coords[0].y();
00163
00164 double t2_x = coords[1].x();
00165 double t2_y = coords[1].y();
00166
00167 double t3_x = coords[2].x();
00168 double t3_y = coords[2].y();
00169
00170 double p_x = p.x();
00171 double p_y = p.y();
00172
00173
00174
00175
00176
00177
00178 double counter_clockwise = (t2_x - t1_x) * (t3_y - t1_y) - (t3_x - t1_x) * (t2_y - t1_y);
00179
00180
00181 if (counter_clockwise == 0.0) {
00182 return true;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 double t1_p_x, t1_p_y, t2_p_x, t2_p_y, t3_p_x, t3_p_y;
00195
00196
00197 double det_t1_t2, det_t2_t3, det_t3_t1_m;
00198
00199 double t1_col3, t2_col3, t3_col3;
00200
00201 t1_p_x = t1_x - p_x;
00202 t1_p_y = t1_y - p_y;
00203 t2_p_x = t2_x - p_x;
00204 t2_p_y = t2_y - p_y;
00205 t3_p_x = t3_x - p_x;
00206 t3_p_y = t3_y - p_y;
00207
00208 det_t1_t2 = t1_p_x * t2_p_y - t2_p_x * t1_p_y;
00209 det_t2_t3 = t2_p_x * t3_p_y - t3_p_x * t2_p_y;
00210 det_t3_t1_m = t3_p_x * t1_p_y - t1_p_x * t3_p_y;
00211 t1_col3 = t1_p_x * t1_p_x + t1_p_y * t1_p_y;
00212 t2_col3 = t2_p_x * t2_p_x + t2_p_y * t2_p_y;
00213 t3_col3 = t3_p_x * t3_p_x + t3_p_y * t3_p_y;
00214
00215 double det = t1_col3 * det_t2_t3 + t2_col3 * det_t3_t1_m + t3_col3 * det_t1_t2;
00216
00217
00218 if (counter_clockwise < 0) {
00219 return det < 0;
00220 }
00221 return det > 0;
00222 }
00223
00224 std::ostream& print(std::ostream& s) const {
00225 s << '[';
00226 for (int i = 0; i < getDim(); ++i)
00227 s << coords[i] << (i < (getDim() - 1) ? ", " : "");
00228 s << ']';
00229 return s;
00230 }
00231 };
00232
00233 static std::ostream& operator<<(std::ostream& s, const Element& E) {
00234 return E.print(s);
00235 }
00236
00237 #endif