├── Circle.cpp ├── Circle.hpp ├── Header.cpp ├── Header.hpp ├── LinearEqu.cpp ├── LinearEqu.hpp ├── README.md ├── cppransac.xcodeproj.zip ├── main.cpp ├── mypoint2f.cpp ├── mypoint2f.hpp └── pts_to_ransac.txt /Circle.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Circle.cpp 3 | // cppransac 4 | // 5 | // Created by ललित सिंह on 09/11/2016. 6 | // Copyright © 2016 ललित सिंह. All rights reserved. 7 | // 8 | 9 | #include "Circle.hpp" 10 | 11 | Circle::Circle(){ 12 | this-> radius = 0; 13 | this-> center = mypoint2f(); 14 | this-> isSingular = true; 15 | } 16 | Circle::Circle(double radius, mypoint2f center){ 17 | this-> radius = radius; 18 | this->center = center; 19 | this-> isSingular = false; 20 | } 21 | Circle::Circle(mypoint2f a, mypoint2f b, mypoint2f c){ 22 | LinearEqu equ1 = LinearEqu(a,b); 23 | LinearEqu equ2 = LinearEqu(b,c); 24 | internsectionPoint intesecPoint = solveLinearEquation(equ1, equ2); 25 | 26 | double radius = distance(a, intesecPoint.point); 27 | this-> center = intesecPoint.point; 28 | this->radius = radius; 29 | this->isSingular = intesecPoint.doesIntersect; 30 | } 31 | bool Circle::getSingular(){ 32 | return this->isSingular; 33 | } 34 | mypoint2f Circle::getCenter(){ 35 | return this->center; 36 | } 37 | double Circle::getRadius(){ 38 | return this->radius; 39 | } 40 | std::ostream& operator <<(std::ostream& out, Circle& c){ 41 | out<<"radius:"< 13 | #include "mypoint2f.hpp" 14 | #include "LinearEqu.hpp" 15 | #include "Header.hpp" 16 | #include 17 | 18 | class Circle{ 19 | private: 20 | double radius; 21 | mypoint2f center; 22 | bool isSingular; 23 | public: 24 | Circle(); 25 | Circle(double, mypoint2f); 26 | Circle( mypoint2f, mypoint2f, mypoint2f); 27 | Circle (std::vector); 28 | bool getSingular(); 29 | mypoint2f getCenter(); 30 | double getRadius(); 31 | friend std::ostream& operator<<(std::ostream&, Circle&); 32 | }; 33 | #endif /* Circle_hpp */ 34 | -------------------------------------------------------------------------------- /Header.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Header.cpp 3 | // cppransac 4 | // 5 | // Created by ललित सिंह on 09/11/2016. 6 | // Copyright © 2016 ललित सिंह. All rights reserved. 7 | // 8 | 9 | #include "Header.hpp" 10 | #include 11 | double sqr(double a){ 12 | return a*a; 13 | } 14 | double abs(double a){ 15 | if (a<0) 16 | return -1* a; 17 | else 18 | return a; 19 | } 20 | double distance(mypoint2f a, mypoint2f b){ 21 | return sqrt( sqr(a.getx()- b.getx()) + sqr(a.gety()- b.gety())); 22 | } 23 | double determinant(double a11, double a12, double a21, double a22){ 24 | return a11 * a22 - a12 * a21; 25 | } 26 | internsectionPoint solveLinearEquation(LinearEqu a, LinearEqu b){ 27 | double den = determinant(a.getA(), a.getB(), b.getA(), b.getB()); 28 | internsectionPoint result; 29 | if ( den == 0){ 30 | result.doesIntersect = true; 31 | return result; 32 | } 33 | double x = determinant(a.getC(), a.getB(), b.getC(), b.getB()); 34 | double y = determinant(a.getA(), a.getC(), b.getA(), b.getC()); 35 | result.point = mypoint2f(x/den, y/den); 36 | result.doesIntersect = false; 37 | return result; 38 | } 39 | std::vector compliment(std::vector set, std::vector subset ){ 40 | std::vector complimentset; 41 | for(int i = 0; i 13 | #include "mypoint2f.hpp" 14 | #include "LinearEqu.hpp" 15 | #include 16 | 17 | struct internsectionPoint{ 18 | bool doesIntersect; 19 | mypoint2f point; 20 | }; 21 | std::vector compliment(std::vector, std::vector); 22 | double abs(double); 23 | double sqr(double sqr); 24 | double distance(mypoint2f , mypoint2f); 25 | double determinant(double, double, double, double); 26 | internsectionPoint solveLinearEquation(LinearEqu, LinearEqu); 27 | 28 | 29 | #endif /* Header_hpp */ 30 | -------------------------------------------------------------------------------- /LinearEqu.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // LinearEqu.cpp 3 | // cppransac 4 | // 5 | // Created by ललित सिंह on 09/11/2016. 6 | // Copyright © 2016 ललित सिंह. All rights reserved. 7 | // 8 | 9 | #include "LinearEqu.hpp" 10 | #include "Header.hpp" 11 | LinearEqu::LinearEqu(){ 12 | this -> A = 0; 13 | this -> B = 0; 14 | this -> C = 0; 15 | } 16 | LinearEqu::LinearEqu(double A, double B, double C){ 17 | this-> A = A; 18 | this-> B = B; 19 | this-> C = C; 20 | } 21 | LinearEqu:: LinearEqu( mypoint2f a, mypoint2f b ){ 22 | this-> A = 2* (b.getx() - a.getx()); 23 | this-> B = 2 * ( b.gety() - a.gety()); 24 | this-> C = sqr(b.getx()) + sqr(b.gety()) - sqr(a.getx()) - sqr(a.gety()); 25 | } 26 | double LinearEqu::getA(){ 27 | return this->A; 28 | } 29 | double LinearEqu::getB(){ 30 | return this->B; 31 | } 32 | double LinearEqu::getC(){ 33 | return this->C; 34 | } 35 | -------------------------------------------------------------------------------- /LinearEqu.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // LinearEqu.hpp 3 | // cppransac 4 | // 5 | // Created by ललित सिंह on 09/11/2016. 6 | // Copyright © 2016 ललित सिंह. All rights reserved. 7 | // 8 | 9 | #ifndef LinearEqu_hpp 10 | #define LinearEqu_hpp 11 | 12 | #include 13 | #include "mypoint2f.hpp" 14 | //This class represents a linear equation in the form Ax + By = C where 15 | // x and y are the variables and A, B, C are the constants. 16 | 17 | class LinearEqu{ 18 | private: 19 | double A; 20 | double B; 21 | double C; 22 | public: 23 | LinearEqu(); 24 | LinearEqu(double, double , double); // when A, B, C are given 25 | LinearEqu(mypoint2f, mypoint2f); // two independent on circle 26 | double getA(); 27 | double getB(); 28 | double getC(); 29 | }; 30 | #endif /* LinearEqu_hpp */ 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RANSAC-Algorithm 2 | This c++ implementation RANSAC algorithm finds the n best fitting circles out of the given points. 3 | -------------------------------------------------------------------------------- /cppransac.xcodeproj.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aerolalit/RANSAC-Algorithm/0d662b1ffd3e87b3f470407369f02bde026684b9/cppransac.xcodeproj.zip -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // cppransac 4 | // 5 | // Created by ललित सिंह on 09/11/2016. 6 | // Copyright © 2016 ललित सिंह. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "Header.hpp" 11 | #include "LinearEqu.hpp" 12 | #include "mypoint2f.hpp" 13 | #include "Circle.hpp" 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | double error(Circle C, mypoint2f p){ 19 | return abs(distance(C.getCenter(), p) - C.getRadius()); 20 | } 21 | 22 | vector rnsac( vector points, int Nmin,int imax,float tau,int d, int num){ 23 | 24 | time_t seconds; 25 | time(&seconds); 26 | srand((unsigned int) seconds); 27 | vector selectedCircles; 28 | vector qualifiedPoints; 29 | for(int m = 0; m errors; 35 | int i = 0; 36 | while(i< imax){ 37 | vector rands;//random numbers 38 | 39 | //cout<<"just entered"< tempQualifiedPoints; 41 | //vector tempErrors; 42 | for(int j = 0 ; j< Nmin; j++){ 43 | rands.push_back(rand() % size); 44 | //cout< d){ 58 | // cout<<"tempvote "< points; 85 | int res=1; 86 | while(res != EOF){ 87 | float x,y; 88 | res = fscanf(file, " %f %f\n",&x,&y); 89 | mypoint2f point = mypoint2f((double)x,(double)y); 90 | //cout< Circles = rnsac(points, 3, 1000, 0.05, 0,3); 94 | 95 | cout<<"Result"<x = 0; 12 | this->y = 0 ; 13 | 14 | } 15 | mypoint2f::mypoint2f(double x, double y ){ 16 | this->x = x; 17 | this->y = y; 18 | } 19 | double mypoint2f::getx(){ 20 | return this->x; 21 | } 22 | double mypoint2f::gety(){ 23 | return this->y; 24 | } 25 | 26 | 27 | mypoint2f operator-(mypoint2f& a, mypoint2f& b){ 28 | mypoint2f result = mypoint2f(a.x - b.x, a.y -b.y); 29 | return result ; 30 | } 31 | mypoint2f operator *(mypoint2f& a, mypoint2f& b){ 32 | mypoint2f result = mypoint2f(a.x * b.x, a.y *b.y); 33 | return result ; 34 | } 35 | mypoint2f operator *(double& k, mypoint2f& a){ 36 | mypoint2f result = mypoint2f(a.x * k, a.y * k); 37 | return result; 38 | } 39 | 40 | mypoint2f operator *(mypoint2f& a, double& k){ 41 | mypoint2f result = mypoint2f(a.x * k, a.y * k); 42 | return result; 43 | } 44 | mypoint2f operator + (mypoint2f& a, mypoint2f& b){ 45 | mypoint2f result = mypoint2f(a.x + b.x, a.y + b.y); 46 | return result; 47 | } 48 | 49 | std::ostream& operator <<(std::ostream& out, mypoint2f& a){ 50 | out<<"x:"< 13 | #include 14 | //#include "vect.hpp" 15 | class mypoint2f{ 16 | private: 17 | double x; 18 | double y; 19 | 20 | 21 | public: 22 | mypoint2f(); 23 | mypoint2f(double, double); 24 | double getx(); 25 | double gety(); 26 | friend mypoint2f operator -(mypoint2f&, mypoint2f&); 27 | friend mypoint2f operator *( mypoint2f&, mypoint2f&); 28 | friend mypoint2f operator +(mypoint2f&, mypoint2f&); 29 | friend mypoint2f operator *(double&, mypoint2f&); 30 | friend mypoint2f operator *( mypoint2f&, double&); 31 | friend std::ostream& operator <<(std::ostream&, mypoint2f&); 32 | friend bool operator ==(mypoint2f&, mypoint2f&); 33 | }; 34 | #endif /* mypoint2f_hpp */ 35 | -------------------------------------------------------------------------------- /pts_to_ransac.txt: -------------------------------------------------------------------------------- 1 | 1.8736 1.1525 2 | 0.1604 0.8276 3 | 0.3617 1.9913 4 | 1.0408 1.7706 5 | 1.2965 0.9325 6 | 0.1906 1.9356 7 | 1.2402 0.3120 8 | 0.7967 1.7650 9 | 1.0780 1.0874 10 | 0.8849 0.3675 11 | 0.4985 0.5702 12 | 1.0590 1.1197 13 | 0.8303 1.8114 14 | 0.6103 1.2995 15 | 0.5776 0.5105 16 | 0.7164 1.6118 17 | 1.0779 1.1801 18 | 0.4623 0.2039 19 | 1.2893 1.9603 20 | 0.2034 0.3757 21 | 0.0188 1.6766 22 | 0.9627 0.9371 23 | 1.8171 0.8343 24 | 1.0883 1.3980 25 | 0.1581 1.0188 26 | 0.9738 1.7118 27 | 1.2288 0.2348 28 | 1.2137 0.3284 29 | 0.7981 1.0647 30 | 1.7504 1.3181 31 | 1.5753 0.2521 32 | 1.0499 1.7990 33 | 0.2182 1.2691 34 | 0.1617 0.8225 35 | 1.4253 0.2008 36 | 1.6199 1.2756 37 | 1.7961 1.2436 38 | 0.8292 1.2951 39 | 0.9785 0.1875 40 | 1.2746 1.9005 41 | 0.9528 1.2056 42 | 1.1830 0.4505 43 | 1.3369 0.3131 44 | 1.5485 0.4261 45 | 0.3382 1.4515 46 | 0.5124 0.3255 47 | 1.2516 0.5015 48 | 0.5260 1.6879 49 | 0.7948 0.2092 50 | 0.3877 0.7261 51 | 1.7491 1.1995 52 | 0.5162 0.7169 53 | 1.7751 1.8011 54 | 0.8959 0.5377 55 | 1.1076 0.3577 56 | 1.7194 0.4640 57 | 0.3362 0.0534 58 | 0.6449 1.1103 59 | 1.6491 1.6085 60 | 0.0488 0.7430 61 | 0.9837 0.9321 62 | 0.0834 1.2340 63 | 1.1561 0.5976 64 | 0.8713 0.2733 65 | 0.5994 1.5227 66 | 0.0706 0.5390 67 | 1.9925 0.8939 68 | 0.3056 1.7725 69 | 0.0628 0.2321 70 | 0.5017 1.5193 71 | 1.7966 0.4468 72 | 1.3466 1.6376 73 | 1.8979 1.7486 74 | 0.7875 1.8739 75 | 0.8738 0.3250 76 | 0.6196 1.3622 77 | 1.8681 1.8948 78 | 1.1982 1.8977 79 | 0.8080 0.0821 80 | 0.5877 0.0639 81 | 1.7290 0.8651 82 | 0.1856 0.2756 83 | 0.4839 0.4460 84 | 1.7355 1.5284 85 | 0.6894 0.7696 86 | 1.1897 1.0702 87 | 0.6671 1.7094 88 | 0.5313 1.8679 89 | 0.7797 1.3661 90 | 0.5500 0.0559 91 | 1.8812 1.0679 92 | 1.3423 1.2151 93 | 1.5017 1.9626 94 | 1.4553 1.7146 95 | 1.9837 1.5189 96 | 0.2921 0.6526 97 | 0.0576 1.3891 98 | 1.9175 1.4581 99 | 1.4735 0.3492 100 | 0.7108 1.1493 101 | 1.1125 1.4985 102 | -0.3770 0.6742 103 | 1.3632 0.7521 104 | 1.3532 0.6576 105 | 1.2115 0.3472 106 | 0.8780 1.6579 107 | -0.0479 1.5864 108 | 0.1411 1.7165 109 | 1.2470 1.3392 110 | 0.9285 0.0619 111 | 1.3777 1.0813 112 | 1.3100 1.2096 113 | -0.3523 1.2551 114 | 1.0925 1.5398 115 | -0.1751 0.2231 116 | -0.0526 0.1476 117 | 1.3388 0.6191 118 | 1.4014 0.9242 119 | -0.3723 0.4744 120 | 1.3756 0.8120 121 | -0.0998 1.5352 122 | 0.6598 -0.0417 123 | -0.0875 1.5605 124 | 0.2068 -0.0271 125 | 1.2812 1.3048 126 | 0.8612 0.0229 127 | 1.1627 0.2475 128 | 0.3684 -0.0197 129 | 0.6031 -0.0421 130 | 1.3877 0.6329 131 | 0.8409 -0.1829 132 | -0.4456 0.7288 133 | 0.6820 -0.2928 134 | 0.7943 -0.2432 135 | 0.7648 1.2264 136 | 1.1612 0.2744 137 | 0.0049 -0.2608 138 | -0.0197 1.2337 139 | 1.0768 0.9269 140 | -0.4515 0.2314 141 | 0.5927 -0.3131 142 | -0.4445 0.1946 143 | 1.1840 0.4530 144 | -0.4691 0.5771 145 | 0.3368 1.3341 146 | 0.8795 -0.1601 147 | 1.0129 1.0150 148 | 0.8072 -0.2210 149 | 0.9492 1.0824 150 | 1.1805 0.5282 151 | 0.5448 -0.3264 152 | 0.8787 1.1498 153 | -0.4192 0.8390 154 | 1.1842 0.4672 155 | -0.3021 0.0038 156 | -0.1350 -0.1843 157 | 1.1449 0.8251 158 | 0.0553 -0.3084 159 | 1.0927 0.8779 160 | 0.2043 -0.3582 161 | 0.1171 0.2985 162 | 1.1073 -0.0659 163 | 0.8978 0.5939 164 | 0.4488 -0.3293 165 | 0.3370 0.5744 166 | 0.1253 0.0297 167 | 0.5396 -0.3325 168 | 1.1208 0.1052 169 | 1.0443 -0.1698 170 | 0.2869 0.5601 171 | 0.9482 0.5625 172 | 0.5958 0.6941 173 | 0.2049 -0.1565 174 | 0.5755 -0.3512 175 | 0.1987 -0.1339 176 | 0.8339 -0.2904 177 | 0.2110 0.4303 178 | 0.1197 0.1130 179 | 1.0828 0.3523 180 | 0.6262 -0.3297 181 | 0.7086 0.6564 182 | 1.1063 0.0914 183 | 0.1278 0.0180 184 | 0.1264 0.0415 185 | 0.3168 0.5533 186 | 0.6925 -0.3612 187 | 0.8969 -0.3029 188 | 0.2248 -0.1634 189 | 0.1266 0.1994 190 | 0.1892 -0.1405 --------------------------------------------------------------------------------