00001
00002 #ifndef _LINEDATA_H_
00003 #define _LINEDATA_H_
00004
00005 #include <vector>
00006 #include <iostream>
00007
00008 #include "BaseData.h"
00009 #include "EndPoint.h"
00010 #include "ShapeFuns.h"
00011 #include "ShapePoint.h"
00012 #include "SketchTypes.h"
00013 #include "Shared/mathutils.h"
00014
00015 #define TSIZE 180
00016 #define RSIZE 1600
00017
00018 namespace DualCoding {
00019
00020 class Region;
00021
00022
00023 class LineData : public BaseData {
00024 private:
00025 EndPoint end1_pt;
00026 EndPoint end2_pt;
00027 float rho_norm;
00028 AngTwoPi theta_norm;
00029 AngPi orientation;
00030 float length;
00031
00032 static const Point origin_pt;
00033
00034 friend class Shape<LineData>;
00035 friend class PolygonData;
00036 friend class BlobData;
00037
00038 public:
00039 static const float DEFAULT_MIN_LENGTH;
00040
00041
00042 LineData(ShapeSpace& _space, const EndPoint &p1, const EndPoint &p2)
00043 : BaseData(_space,getStaticType()),
00044 end1_pt(p1), end2_pt(p2), rho_norm(0), theta_norm(0), orientation(0), length(0)
00045 { update_derived_properties(); }
00046
00047
00048 LineData(ShapeSpace& _space, const Point &pt, orientation_t orient);
00049
00050
00051 LineData(const LineData& other);
00052
00053 static ShapeType_t getStaticType() { return lineDataType; }
00054
00055 DATASTUFF_H(LineData);
00056
00057
00058 void update_derived_properties();
00059
00060
00061 virtual Point getCentroid() const;
00062
00063
00064 void setInfinite(bool value=true);
00065
00066 BoundingBox2D getBoundingBox() const {
00067 BoundingBox2D b(end1_pt.coords);
00068 b.expand(end2_pt.coords);
00069 return b;
00070 }
00071
00072
00073 virtual bool isMatchFor(const ShapeRoot& other) const;
00074 bool isMatchFor(const LineData& other) const;
00075
00076
00077 virtual bool isAdmissible() const ;
00078
00079 virtual bool updateParams(const ShapeRoot& other, bool force=false);
00080 static void updateLinePt(EndPoint& localPt, coordinate_t local_coord,
00081 const EndPoint& groundPt, coordinate_t ground_coord,
00082 int sign);
00083 virtual void mergeWith(const ShapeRoot& other);
00084
00085 LineData& operator=(const LineData&);
00086
00087
00088 bool isValidUpdate(coordinate_t p1, coordinate_t p2, coordinate_t p3, coordinate_t p4);
00089
00090
00091 virtual void printParams() const;
00092
00093
00094 virtual void applyTransform(const fmat::Transform& Tmat, const ReferenceFrameType_t newref=unspecified);
00095
00096
00097 virtual void projectToGround(const fmat::Transform& camToBase,
00098 const PlaneEquation& groundplane);
00099
00100 virtual unsigned short getDimension() const { return 1; }
00101
00102 void printEnds() const;
00103 void setEndPts(const EndPoint& _end1_pt, const EndPoint& _end2_pt);
00104
00105
00106
00107
00108
00109
00110
00111 EndPoint& end1Pt() { return end1_pt; }
00112 EndPoint& end2Pt() { return end2_pt; }
00113 const EndPoint& end1Pt() const { return end1_pt; }
00114 const EndPoint& end2Pt() const { return end2_pt; }
00115
00116 EndPoint& leftPt();
00117 const EndPoint& leftPt() const;
00118 EndPoint& rightPt();
00119 const EndPoint& rightPt() const;
00120 EndPoint& topPt();
00121 const EndPoint& topPt() const;
00122 EndPoint& bottomPt();
00123 const EndPoint& bottomPt() const;
00124
00125 Shape<PointData> leftPtShape();
00126 Shape<PointData> rightPtShape();
00127 Shape<PointData> topPtShape();
00128 Shape<PointData> bottomPtShape();
00129
00130 EndPoint& firstPt();
00131 const EndPoint& firstPt() const;
00132 const EndPoint& firstPt(const Shape<LineData> &otherline) const;
00133 EndPoint& secondPt();
00134 const EndPoint& secondPt() const;
00135 const EndPoint& secondPt(const Shape<LineData> &otherline) const;
00136
00137 Shape<PointData> firstPtShape();
00138 Shape<PointData> secondPtShape();
00139
00140 coordinate_t firstPtCoord() const;
00141 coordinate_t firstPtCoord(const Shape<LineData> &otherline) const;
00142 coordinate_t secondPtCoord() const;
00143 coordinate_t secondPtCoord(const Shape<LineData> &otherline) const;
00144
00145
00146
00147
00148 float getRhoNorm() const { return rho_norm; }
00149 AngTwoPi getThetaNorm() const { return theta_norm; }
00150 AngPi getOrientation() const { return orientation; }
00151 float getLength() const { return length; }
00152 std::pair<float,float> lineEquation_mb() const;
00153 std::vector<float> lineEquation_abc() const;
00154 std::vector<float> lineEquation_abc_xz() const;
00155
00156
00157
00158
00159 bool isNotVertical() const;
00160 bool isOverlappedWith(const LineData& otherline, int amount=0) const;
00161
00162
00163
00164
00165
00166
00167
00168 bool pointIsLeftOf(const Point& pt) const;
00169 bool pointIsRightOf(const Point& pt) const;
00170 bool pointIsAbove(const Point& pt) const;
00171 bool pointIsBelow(const Point& pt) const;
00172
00173
00174
00175 bool isLongerThan(const Shape<LineData>& other) const;
00176 bool isLongerThan(float ref_length) const;
00177 bool isShorterThan(const Shape<LineData>& other) const;
00178 bool isShorterThan(float ref_length) const;
00179
00180
00181
00182 bool isBetween(const Point &p, const LineData &other) const;
00183
00184
00185
00186 bool intersectsLine(const Shape<LineData>& other) const;
00187 bool intersectsLine(const LineData& other) const;
00188
00189 Point intersectionWithLine(const Shape<LineData>& other) const
00190 { bool b; return intersectionWithLine(other,b,b); };
00191 Point intersectionWithLine(const Shape<LineData>& other,
00192 bool& intersection_on_this,
00193 bool& intersection_on_other) const;
00194 Point intersectionWithLine(const LineData& other) const
00195 { bool b; return intersectionWithLine(other, b,b); };
00196 Point intersectionWithLine(const LineData& other,
00197 bool& intersection_on_this,
00198 bool& intersection_on_other) const;
00199
00200
00201
00202 bool pointsOnSameSide(const Point& p1, const Point& p2);
00203 bool pointOnLine(const Point& p);
00204
00205
00206 float perpendicularDistanceFrom(const Point& other) const;
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 static Shape<LineData> splitLine(ShapeSpace &ShS, Region &skelchunk,
00233 Sketch<bool> &skeleton, const Sketch<bool> &occlusions);
00234
00235
00236
00237
00238 void clearLine(Sketch<bool>& sketch);
00239
00240 void scanHorizForEndPts(const Sketch<uint>& skelDist, const Sketch<bool>& occlusions,
00241 float m, float b);
00242 void scanVertForEndPts(const Sketch<uint>& skelDist, const Sketch<bool>& occlusions,
00243 float m, float b);
00244
00245 void balanceEndPointHoriz(EndPoint &pt, Sketch<bool> const &occluders, float m, float b);
00246 void balanceEndPointVert(EndPoint &pt, Sketch<bool> const &occluders, float m, float b);
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 static bool pointsOnPerimeterOfWindow(Sketch<bool> const& sketch, double t, double r, Point& pt1, Point& pt2);
00257
00258 static double getScore(int t, int r, int height, short hough[TSIZE][RSIZE], float ptsArray[TSIZE][RSIZE]);
00259
00260 static std::vector<Shape<LineData> > houghExtractLines(Sketch<bool> const& sketch,
00261 Sketch<bool> const& occluders,
00262 const int num_lines);
00263
00264 static Shape<LineData> extractLine(Sketch<bool>& sketch);
00265
00266 static Shape<LineData> extractLine(Sketch<bool>& skelsketch,
00267 const Sketch<bool>& occlusions);
00268
00269 static std::vector<Shape<LineData> > extractLines(Sketch<bool> const& sketch, int const num_lines=20);
00270
00271 static std::vector<Shape<LineData> > extractLines(Sketch<bool> const& skel,
00272 Sketch<bool> const& occluders,
00273 int const num_lines=20);
00274
00275 static std::vector<Shape<LineData> > houghTransform(const Sketch<bool>& fat,
00276 const Sketch<bool>& skinny,
00277 const Sketch<bool>& occlusions,
00278 const size_t num_lines,
00279 int minLength=-1);
00280
00281 static bool linesParallel(Shape<LineData> l1, Shape<LineData>l2);
00282
00283
00284
00285
00286 class LineDataLengthLessThan : public std::binary_function<const LineData, const LineData, bool> {
00287 public:
00288 bool operator() (const LineData &ln1, const LineData &ln2) const;
00289 };
00290
00291
00292
00293
00294
00295 class LengthLessThan : public BinaryShapePred<LineData> {
00296 public:
00297 bool operator() (const Shape<LineData> &ln1, const Shape<LineData> &ln2) const;
00298 };
00299
00300
00301 class ParallelTest : public BinaryShapePred<LineData> {
00302 public:
00303 AngPi tolerance;
00304 ParallelTest(AngPi _tol=mathutils::deg2rad((orientation_t)20)) : tolerance(_tol) {}
00305 bool operator() (const Shape<LineData> &line1, const Shape<LineData> &line2) const;
00306 };
00307
00308
00309
00310 class PerpendicularTest : public BinaryShapePred<LineData> {
00311 public:
00312 AngPi tolerance;
00313 PerpendicularTest(AngPi _tol=mathutils::deg2rad((orientation_t)20)) : tolerance(_tol) {}
00314 bool operator() (const Shape<LineData> &line1, const Shape<LineData> &line2) const;
00315 };
00316
00317
00318
00319 class ColinearTest : public BinaryShapePred<LineData> {
00320 public:
00321 AngPi ang_tol;
00322 coordinate_t dist_tol;
00323 ColinearTest(AngPi _ang_tol=mathutils::deg2rad((orientation_t)20), coordinate_t _dist_tol=10) :
00324 ang_tol(_ang_tol), dist_tol(_dist_tol) {}
00325 bool operator() (const Shape<LineData> &line1, const Shape<LineData> &ln2) const;
00326 };
00327
00328
00329 class IsHorizontal : public UnaryShapePred<LineData> {
00330 public:
00331 IsHorizontal(AngPi thresh=(orientation_t)M_PI/6) : UnaryShapePred<LineData>(), threshold(thresh) {}
00332 bool operator() (const Shape<LineData> &line) const;
00333 private:
00334 AngPi threshold;
00335 };
00336
00337
00338 class IsVertical : public UnaryShapePred<LineData> {
00339 public:
00340 IsVertical(AngPi thresh=(orientation_t)M_PI/3) : UnaryShapePred<LineData>(), threshold(thresh) {}
00341 bool operator() (const Shape<LineData> &line) const;
00342 private:
00343 AngPi threshold;
00344 };
00345
00346
00347
00348
00349 virtual Sketch<bool>& getRendering();
00350
00351 private:
00352 static const int extractorGapTolerance = 15;
00353
00354
00355
00356
00357
00358 Sketch<bool>* render() const;
00359
00360
00361
00362
00363
00364 void setDrawCoords(float& x1,float& y1, float& x2, float& y2, const int width, const int height) const;
00365 static void drawline2d(Sketch<bool>& canvas, int x0, int y0, int x1, int y1);
00366
00367 };
00368
00369 }
00370
00371 #endif
00372