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