|
| 1 | +// ConvexHull : Implementation |
| 2 | + |
| 3 | +/*============================================================================== |
| 4 | + Includes |
| 5 | +==============================================================================*/ |
| 6 | +#include "include/convexHull.hpp" |
| 7 | + |
| 8 | +/*============================================================================== |
| 9 | + Stack functions to define the Convex Hull algorithm with Compressed Stack |
| 10 | + * readInput |
| 11 | + * initStack |
| 12 | + * popCondition |
| 13 | + * prePop |
| 14 | + * postPop |
| 15 | + * noPop |
| 16 | + * pushCondition |
| 17 | + * prePush |
| 18 | + * postPush |
| 19 | + * noPush |
| 20 | + * reportStack |
| 21 | +==============================================================================*/ |
| 22 | +Point2D ConvexHull::readInput(std::vector<std::string> line) { |
| 23 | + double x = std::stof(line[0]); |
| 24 | + double y = std::stof(line[1]); |
| 25 | + |
| 26 | + Point2D p(x, y); |
| 27 | + |
| 28 | + std::cout << "I JUST READ " << p << std::endl; |
| 29 | + return p; |
| 30 | +} |
| 31 | + |
| 32 | +std::shared_ptr<emptyContext> ConvexHull::initStack() { |
| 33 | + |
| 34 | + std::cout << "going to read two values " << std::endl; |
| 35 | + |
| 36 | + // first, read and push two values |
| 37 | + readPush(2); |
| 38 | + |
| 39 | + std::cout << "done reading two values " << std::endl; |
| 40 | + |
| 41 | + // then initialize context (which in this case is NULL everything |
| 42 | + std::shared_ptr<emptyContext> context; |
| 43 | + return context; |
| 44 | +} |
| 45 | + |
| 46 | +bool ConvexHull::popCondition(Point2D last) { |
| 47 | + Point2D minus1, minus2; |
| 48 | + |
| 49 | + std::cout << last << " <<<< pop condition enter " << std::endl; |
| 50 | + |
| 51 | + // read the two previous elements |
| 52 | + minus1 = top(1).getData(); |
| 53 | + minus2 = top(2).getData(); |
| 54 | + |
| 55 | + std::cout << last << " <<<< pop condition read two before " << minus1 |
| 56 | + << minus2 << std::endl; |
| 57 | + |
| 58 | + if (Point2D::orientation(minus2, minus1, last) == 2) { |
| 59 | + std::cout << last << " <<<< pop condition returning true " << std::endl; |
| 60 | + |
| 61 | + return true; |
| 62 | + } |
| 63 | + std::cout << last << " <<<< pop condition returning false " << std::endl; |
| 64 | + |
| 65 | + return false; |
| 66 | +} |
| 67 | +void ConvexHull::prePop(Point2D data) {} |
| 68 | +void ConvexHull::postPop(Point2D data, Data<emptyContext, Point2D> elt) { |
| 69 | + std::cout << elt.getData() << " <<<< Pop!" << std::endl; |
| 70 | +} |
| 71 | +void ConvexHull::noPop(Point2D data) {} |
| 72 | + |
| 73 | +bool ConvexHull::pushCondition(Point2D data) { |
| 74 | + std::cout << data << " <<<< push condition returning true " << std::endl; |
| 75 | + return true; |
| 76 | +} |
| 77 | +void ConvexHull::prePush(Data<emptyContext, Point2D> elt) {} |
| 78 | +void ConvexHull::postPush(Data<emptyContext, Point2D> elt) { |
| 79 | + std::cout << "ConvexHullStackAlgo::pushAction Nothing to see here " |
| 80 | + << elt.getData() << std::endl; |
| 81 | +} |
| 82 | +void ConvexHull::noPush(Point2D data) {} |
| 83 | + |
| 84 | +void ConvexHull::reportStack() {} |
| 85 | + |
| 86 | +/*============================================================================== |
| 87 | + Stack functions to define the Convex Hull algorithm with Stack comparison |
| 88 | + * readInput |
| 89 | + * initStack |
| 90 | + * popCondition |
| 91 | + * prePop |
| 92 | + * postPop |
| 93 | + * noPop |
| 94 | + * pushCondition |
| 95 | + * prePush |
| 96 | + * postPush |
| 97 | + * noPush |
| 98 | + * reportStack |
| 99 | +==============================================================================*/ |
| 100 | +Point2D ComparisonConvexHull::readInput(std::vector<std::string> line) { |
| 101 | + double x = std::stof(line[0]); |
| 102 | + double y = std::stof(line[1]); |
| 103 | + |
| 104 | + Point2D p(x, y); |
| 105 | + |
| 106 | + std::cout << "I JUST READ " << p << std::endl; |
| 107 | + |
| 108 | + return p; |
| 109 | +} |
| 110 | + |
| 111 | +std::shared_ptr<emptyContext> ComparisonConvexHull::initStack() { |
| 112 | + std::cout << "going to read two values " << std::endl; |
| 113 | + |
| 114 | + // first, read and push two values |
| 115 | + readPush(2); |
| 116 | + |
| 117 | + printCompare(); |
| 118 | + |
| 119 | + std::cout << "done reading two values " << std::endl; |
| 120 | + // then initialize context (which in this case is NULL everything |
| 121 | + std::shared_ptr<emptyContext> context; |
| 122 | + return context; |
| 123 | +} |
| 124 | + |
| 125 | +bool ComparisonConvexHull::popCondition(Point2D last) { |
| 126 | + Point2D minus1, minus2; |
| 127 | + |
| 128 | + std::cout << last << " <<<< pop condition enter " << std::endl; |
| 129 | + |
| 130 | + // read the two previous elements |
| 131 | + minus1 = top(1).getData(); |
| 132 | + minus2 = top(2).getData(); |
| 133 | + |
| 134 | + std::cout << last << " <<<< pop condition read two before " << minus1 |
| 135 | + << minus2 << std::endl; |
| 136 | + |
| 137 | + if (Point2D::orientation(minus2, minus1, last) == 2) { |
| 138 | + std::cout << last << " <<<< pop condition returning true " << std::endl; |
| 139 | + |
| 140 | + return true; |
| 141 | + } |
| 142 | + std::cout << last << " <<<< pop condition returning false " << std::endl; |
| 143 | + |
| 144 | + return false; |
| 145 | +} |
| 146 | +void ComparisonConvexHull::prePop(Point2D data) {} |
| 147 | +void ComparisonConvexHull::postPop(Point2D data, |
| 148 | + Data<emptyContext, Point2D> elt) { |
| 149 | + std::cout << elt.getData() << " <<<< Pop!" << std::endl; |
| 150 | +} |
| 151 | +void ComparisonConvexHull::noPop(Point2D data) {} |
| 152 | + |
| 153 | +bool ComparisonConvexHull::pushCondition(Point2D data) { |
| 154 | + std::cout << data << " <<<< push condition returning true " << std::endl; |
| 155 | + return true; |
| 156 | +} |
| 157 | +void ComparisonConvexHull::prePush(Data<emptyContext, Point2D> elt) {} |
| 158 | +void ComparisonConvexHull::postPush(Data<emptyContext, Point2D> elt) { |
| 159 | + std::cout << "ConvexHullStackAlgo::pushAction Nothing to see here " |
| 160 | + << elt.getData() << std::endl; |
| 161 | +} |
| 162 | +void ComparisonConvexHull::noPush(Point2D data) {} |
| 163 | + |
| 164 | +void ComparisonConvexHull::reportStack() {} |
| 165 | + |
| 166 | +/*============================================================================== |
| 167 | + How to use |
| 168 | + * argv[1] is the file name |
| 169 | + * argv[2] is the code of what is to be done (is no space provided -> 0) |
| 170 | + * 0 is a convex hull problem with a classical stack |
| 171 | + * 1 is a convex hull problem with a compressed stack |
| 172 | + * 2 is a convex hull problem with both stacks comparison (check errors) |
| 173 | +==============================================================================*/ |
| 174 | +int main(int argc, char *argv[]) { |
| 175 | + // Getting the path of the instance to test |
| 176 | + std::string filepath = argv[1]; |
| 177 | + |
| 178 | + switch (atoi(argv[2])) { |
| 179 | + case 0: { |
| 180 | + ConvexHull stack(filepath, true); |
| 181 | + stack.run(); |
| 182 | + stack.println(); |
| 183 | + break; |
| 184 | + } |
| 185 | + case 1: { |
| 186 | + ConvexHull stack(filepath); |
| 187 | + stack.run(); |
| 188 | + stack.println(); |
| 189 | + break; |
| 190 | + } |
| 191 | + default: { |
| 192 | + ComparisonConvexHull stack(filepath); |
| 193 | + stack.runCompare(); |
| 194 | + stack.println(); |
| 195 | + break; |
| 196 | + } |
| 197 | + } |
| 198 | + return 0; |
| 199 | +} |
0 commit comments