00001
00002 #ifndef _LINEDATA_H_
00003 #define _LINEDATA_H_
00004
00005 #include <vector>
00006 #include <iostream>
00007 #include <string>
00008
00009 #include "BaseData.h"
00010 #include "EndPoint.h"
00011 #include "Measures.h"
00012 #include "ShapeFuns.h"
00013 #include "ShapePoint.h"
00014 #include "SketchTypes.h"
00015 #include "Shared/mathutils.h"
00016
00017 namespace DualCoding {
00018
00019 class Region;
00020
00021 #define EXTRACT_LINE_MIN_AREA 20
00022 #define DEFAULT_MIN_LENGTH 4.0
00023
00024 class LineData : public BaseData {
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
00041
00042 LineData(ShapeSpace& _space, const Point &p1, const Point &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 : BaseData(other),
00053 end1_pt(other.end1_pt), end2_pt(other.end2_pt),
00054 rho_norm(other.rho_norm), theta_norm(other.theta_norm),
00055 orientation(other.orientation), length(other.length) {}
00056
00057 static ShapeType_t getStaticType() { return lineDataType; }
00058
00059 DATASTUFF_H(LineData);
00060
00061
00062 void update_derived_properties();
00063
00064
00065 virtual Point getCentroid() const;
00066
00067
00068 void setInfinite(bool value=true);
00069
00070 BoundingBox getBoundingBox() const {
00071 return BoundingBox(min(end1_pt.coordX(),end2_pt.coordX()),
00072 min(end1_pt.coordY(),end2_pt.coordY()),
00073 max(end1_pt.coordX(),end2_pt.coordX()),
00074 max(end1_pt.coordY(),end2_pt.coordY()));
00075 }
00076
00077
00078 virtual bool isMatchFor(const ShapeRoot& other) const;
00079 bool isMatchFor(const LineData& other) const;
00080
00081
00082 virtual bool isAdmissible() const ;
00083
00084 virtual bool updateParams(const ShapeRoot& other, bool force=false);
00085 static void updateLinePt(EndPoint& localPt, coordinate_t local_coord,
00086 const EndPoint& groundPt, coordinate_t ground_coord,
00087 int sign);
00088 virtual void mergeWith(const ShapeRoot& other);
00089
00090 LineData& operator=(const LineData&);
00091
00092
00093 bool isValidUpdate(coordinate_t p1, coordinate_t p2, coordinate_t p3, coordinate_t p4);
00094
00095
00096 virtual void printParams() const;
00097
00098
00099 virtual void applyTransform(const NEWMAT::Matrix& Tmat);
00100
00101
00102 virtual void projectToGround(const NEWMAT::Matrix& camToBase,
00103 const NEWMAT::ColumnVector& groundplane);
00104
00105 virtual unsigned short getDimension() const { return 1; }
00106
00107
00108
00109 EndPoint& end1Pt() { return end1_pt; }
00110 EndPoint& end2Pt() { return end2_pt; }
00111 const EndPoint& end1Pt() const { return end1_pt; }
00112 const EndPoint& end2Pt() const { return end2_pt; }
00113
00114 EndPoint& leftPt();
00115 EndPoint& rightPt();
00116 EndPoint& topPt();
00117 EndPoint& bottomPt();
00118
00119 Shape<PointData> leftPtShape();
00120 Shape<PointData> rightPtShape();
00121 Shape<PointData> topPtShape();
00122 Shape<PointData> bottomPtShape();
00123
00124
00125
00126
00127
00128 EndPoint& firstPt();
00129 EndPoint& firstPt(const Shape<LineData> &otherline) const;
00130 EndPoint& secondPt();
00131 EndPoint& secondPt(const Shape<LineData> &otherline) const;
00132
00133 coordinate_t firstPtCoord() const;
00134 coordinate_t firstPtCoord(const Shape<LineData> &otherline) const;
00135 coordinate_t secondPtCoord() const;
00136 coordinate_t secondPtCoord(const Shape<LineData> &otherline) const;
00137
00138
00139
00140 void printEnds() const;
00141 void setEndPts(const EndPoint& _end1_pt, const EndPoint& _end2_pt);
00142
00143
00144
00145 float getRhoNorm() const { return rho_norm; }
00146 AngTwoPi getThetaNorm() const { return theta_norm; }
00147 AngPi getOrientation() const { return orientation; }
00148 float getLength() const { return length; }
00149 pair<float,float> lineEquation_mb() const;
00150 vector<float> lineEquation_abc() const;
00151 vector<float> lineEquation_abc_xz() const;
00152
00153
00154 public:
00155
00156 bool isNotVertical() const;
00157 bool isOverlappedWith(const LineData& otherline, int amount=0) const;
00158
00159
00160
00161
00162
00163
00164
00165 bool isLongerThan(const Shape<LineData>& other) const;
00166 bool isLongerThan(float ref_length) const;
00167 bool isShorterThan(const Shape<LineData>& other) const;
00168 bool isShorterThan(float ref_length) const;
00169
00170
00171
00172 bool isBetween(const Point &p, const LineData &other) const;
00173
00174
00175
00176 bool intersectsLine(const Shape<LineData>& other) const;
00177 bool intersectsLine(const LineData& other) const;
00178
00179 Point intersectionWithLine(const Shape<LineData>& other) const
00180 { bool b; return intersectionWithLine(other,b,b); };
00181 Point intersectionWithLine(const Shape<LineData>& other,
00182 bool& intersection_on_this,
00183 bool& intersection_on_other) const;
00184 Point intersectionWithLine(const LineData& other) const
00185 { bool b; return intersectionWithLine(other, b,b); };
00186 Point intersectionWithLine(const LineData& other,
00187 bool& intersection_on_this,
00188 bool& intersection_on_other) const;
00189
00190
00191
00192 bool pointsOnSameSide(const Point& p1, const Point& p2);
00193 bool pointOnLine(const Point& p);
00194
00195
00196
00197 float perpendicularDistanceFrom(const Point& other) const;
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 static Shape<LineData> extractLine(Sketch<bool>& sketch);
00210
00211
00212
00213 static Shape<LineData> extractLine(Sketch<bool>& skelsketch,
00214 const Sketch<bool>& occlusions);
00215
00216
00217
00218 static Shape<LineData> splitLine(ShapeSpace &ShS, Region &skelchunk,
00219 Sketch<bool> &skeleton, const Sketch<bool> &occlusions);
00220
00221
00222 void clearLine(Sketch<bool>& sketch);
00223
00224 void scanHorizForEndPts(const Sketch<usint>& skelDist, const Sketch<bool>& occlusions,
00225 float m, float b);
00226 void scanVertForEndPts(const Sketch<usint>& skelDist, const Sketch<bool>& occlusions,
00227 float m, float b);
00228
00229 void balanceEndPointHoriz(EndPoint &pt, Sketch<bool> const &occluders, float m, float b);
00230 void balanceEndPointVert(EndPoint &pt, Sketch<bool> const &occluders, float m, float b);
00231
00232 static vector<Shape<LineData> > extractLines(Sketch<bool> const& sketch, int const num_lines=10);
00233
00234 static vector<Shape<LineData> > extractLines(Sketch<bool> const& skel,
00235 Sketch<bool> const& occluders,
00236 int const num_lines=10);
00237
00238 static std::vector<Shape<LineData> > houghTransform(const Sketch<bool>& fat,
00239 const Sketch<bool>& skinny,
00240 const Sketch<bool>& occlusions,
00241 const size_t num_lines,
00242 int minLength=DEFAULT_MIN_LENGTH);
00243
00244 static bool linesParallel(Shape<LineData> l1, Shape<LineData>l2);
00245
00246
00247
00248
00249
00250
00251 class LengthLessThan : public BinaryShapePred<LineData> {
00252 public:
00253 bool operator() (const Shape<LineData> &ln1, const Shape<LineData> &ln2) const;
00254 };
00255
00256 class ParallelTest : public BinaryShapePred<LineData> {
00257 public:
00258 AngPi tolerance;
00259 ParallelTest(AngPi _tol=mathutils::deg2rad(20.0)) : tolerance(_tol) {}
00260 bool operator() (const Shape<LineData> &line1, const Shape<LineData> &line2) const;
00261 };
00262
00263
00264 class PerpendicularTest : public BinaryShapePred<LineData> {
00265 public:
00266 AngPi tolerance;
00267 PerpendicularTest(AngPi _tol=mathutils::deg2rad(20.0)) : tolerance(_tol) {}
00268 bool operator() (const Shape<LineData> &line1, const Shape<LineData> &line2) const;
00269 };
00270
00271
00272 class ColinearTest : public BinaryShapePred<LineData> {
00273 public:
00274 AngPi ang_tol;
00275 coordinate_t dist_tol;
00276 ColinearTest(AngPi _ang_tol=mathutils::deg2rad(20.0), coordinate_t _dist_tol=10) :
00277 ang_tol(_ang_tol), dist_tol(_dist_tol) {}
00278 bool operator() (const Shape<LineData> &line1, const Shape<LineData> &ln2) const;
00279 };
00280
00281 class IsHorizontal : public UnaryShapePred<LineData> {
00282 public:
00283 IsHorizontal(AngPi thresh=M_PI/6) : UnaryShapePred<LineData>(), threshold(thresh) {}
00284 bool operator() (const Shape<LineData> &line);
00285 private:
00286 AngPi threshold;
00287 };
00288
00289
00290
00291
00292
00293 virtual Sketch<bool>& getRendering();
00294
00295 private:
00296 static const int extractorGapTolerance = 8;
00297
00298
00299
00300
00301
00302 Sketch<bool>* render() const;
00303
00304
00305
00306
00307
00308 void setDrawCoords(float& x1,float& y1, float& x2, float& y2, const int width, const int height) const;
00309 static void drawline2d(Sketch<bool>& canvas, int x0, int y0, int x1, int y1);
00310
00311 };
00312
00313 }
00314
00315 #endif
00316