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 #define EXTRACT_LINE_MIN_AREA 20
00020 #define DEFAULT_MIN_LENGTH 4.0
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
00040
00041 LineData(ShapeSpace& _space, const EndPoint &p1, const EndPoint &p2)
00042 : BaseData(_space,getStaticType()),
00043 end1_pt(p1), end2_pt(p2), rho_norm(0), theta_norm(0), orientation(0), length(0)
00044 { update_derived_properties(); }
00045
00046
00047 LineData(ShapeSpace& _space, const Point &pt, orientation_t orient);
00048
00049
00050 LineData(const LineData& other)
00051 : BaseData(other),
00052 end1_pt(other.end1_pt), end2_pt(other.end2_pt),
00053 rho_norm(other.rho_norm), theta_norm(other.theta_norm),
00054 orientation(other.orientation), length(other.length) {}
00055
00056 static ShapeType_t getStaticType() { return lineDataType; }
00057
00058 DATASTUFF_H(LineData);
00059
00060
00061 void update_derived_properties();
00062
00063
00064 virtual Point getCentroid() const;
00065
00066
00067 void setInfinite(bool value=true);
00068
00069 BoundingBox getBoundingBox() const {
00070 return BoundingBox(std::min(end1_pt.coordX(),end2_pt.coordX()),
00071 std::min(end1_pt.coordY(),end2_pt.coordY()),
00072 std::max(end1_pt.coordX(),end2_pt.coordX()),
00073 std::max(end1_pt.coordY(),end2_pt.coordY()));
00074 }
00075
00076
00077 virtual bool isMatchFor(const ShapeRoot& other) const;
00078 bool isMatchFor(const LineData& other) const;
00079
00080
00081 virtual bool isAdmissible() const ;
00082
00083 virtual bool updateParams(const ShapeRoot& other, bool force=false);
00084 static void updateLinePt(EndPoint& localPt, coordinate_t local_coord,
00085 const EndPoint& groundPt, coordinate_t ground_coord,
00086 int sign);
00087 virtual void mergeWith(const ShapeRoot& other);
00088
00089 LineData& operator=(const LineData&);
00090
00091
00092 bool isValidUpdate(coordinate_t p1, coordinate_t p2, coordinate_t p3, coordinate_t p4);
00093
00094
00095 virtual void printParams() const;
00096
00097
00098 virtual void applyTransform(const NEWMAT::Matrix& Tmat, const ReferenceFrameType_t newref=unspecified);
00099
00100
00101 virtual void projectToGround(const NEWMAT::Matrix& camToBase,
00102 const NEWMAT::ColumnVector& groundplane);
00103
00104 virtual unsigned short getDimension() const { return 1; }
00105
00106 void printEnds() const;
00107 void setEndPts(const EndPoint& _end1_pt, const EndPoint& _end2_pt);
00108
00109
00110
00111
00112
00113
00114
00115 EndPoint& end1Pt() { return end1_pt; }
00116 EndPoint& end2Pt() { return end2_pt; }
00117 const EndPoint& end1Pt() const { return end1_pt; }
00118 const EndPoint& end2Pt() const { return end2_pt; }
00119
00120 EndPoint& leftPt();
00121 EndPoint& rightPt();
00122 EndPoint& topPt();
00123 EndPoint& bottomPt();
00124
00125 Shape<PointData> leftPtShape();
00126 Shape<PointData> rightPtShape();
00127 Shape<PointData> topPtShape();
00128 Shape<PointData> bottomPtShape();
00129
00130 EndPoint& firstPt();
00131 EndPoint& firstPt(const Shape<LineData> &otherline) const;
00132 EndPoint& secondPt();
00133 EndPoint& secondPt(const Shape<LineData> &otherline) const;
00134
00135 Shape<PointData> firstPtShape();
00136 Shape<PointData> secondPtShape();
00137
00138 coordinate_t firstPtCoord() const;
00139 coordinate_t firstPtCoord(const Shape<LineData> &otherline) const;
00140 coordinate_t secondPtCoord() const;
00141 coordinate_t secondPtCoord(const Shape<LineData> &otherline) const;
00142
00143
00144
00145
00146 float getRhoNorm() const { return rho_norm; }
00147 AngTwoPi getThetaNorm() const { return theta_norm; }
00148 AngPi getOrientation() const { return orientation; }
00149 float getLength() const { return length; }
00150 std::pair<float,float> lineEquation_mb() const;
00151 std::vector<float> lineEquation_abc() const;
00152 std::vector<float> lineEquation_abc_xz() const;
00153
00154
00155
00156
00157 bool isNotVertical() const;
00158 bool isOverlappedWith(const LineData& otherline, int amount=0) const;
00159
00160
00161
00162
00163
00164
00165
00166 bool isLongerThan(const Shape<LineData>& other) const;
00167 bool isLongerThan(float ref_length) const;
00168 bool isShorterThan(const Shape<LineData>& other) const;
00169 bool isShorterThan(float ref_length) const;
00170
00171
00172
00173 bool isBetween(const Point &p, const LineData &other) const;
00174
00175
00176
00177 bool intersectsLine(const Shape<LineData>& other) const;
00178 bool intersectsLine(const LineData& other) const;
00179
00180 Point intersectionWithLine(const Shape<LineData>& other) const
00181 { bool b; return intersectionWithLine(other,b,b); };
00182 Point intersectionWithLine(const Shape<LineData>& other,
00183 bool& intersection_on_this,
00184 bool& intersection_on_other) const;
00185 Point intersectionWithLine(const LineData& other) const
00186 { bool b; return intersectionWithLine(other, b,b); };
00187 Point intersectionWithLine(const LineData& other,
00188 bool& intersection_on_this,
00189 bool& intersection_on_other) const;
00190
00191
00192
00193 bool pointsOnSameSide(const Point& p1, const Point& p2);
00194 bool pointOnLine(const Point& p);
00195
00196
00197 float perpendicularDistanceFrom(const Point& other) const;
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 static Shape<LineData> extractLine(Sketch<bool>& sketch);
00209
00210
00211
00212 static Shape<LineData> extractLine(Sketch<bool>& skelsketch,
00213 const Sketch<bool>& occlusions);
00214
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<uint>& skelDist, const Sketch<bool>& occlusions,
00225 float m, float b);
00226 void scanVertForEndPts(const Sketch<uint>& 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 std::vector<Shape<LineData> > extractLines(Sketch<bool> const& sketch, int const num_lines=10);
00233
00234 static std::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
00252 class LengthLessThan : public BinaryShapePred<LineData> {
00253 public:
00254 bool operator() (const Shape<LineData> &ln1, const Shape<LineData> &ln2) const;
00255 };
00256
00257
00258 class ParallelTest : public BinaryShapePred<LineData> {
00259 public:
00260 AngPi tolerance;
00261 ParallelTest(AngPi _tol=mathutils::deg2rad(20.0)) : tolerance(_tol) {}
00262 bool operator() (const Shape<LineData> &line1, const Shape<LineData> &line2) const;
00263 };
00264
00265
00266
00267 class PerpendicularTest : public BinaryShapePred<LineData> {
00268 public:
00269 AngPi tolerance;
00270 PerpendicularTest(AngPi _tol=mathutils::deg2rad(20.0)) : tolerance(_tol) {}
00271 bool operator() (const Shape<LineData> &line1, const Shape<LineData> &line2) const;
00272 };
00273
00274
00275
00276 class ColinearTest : public BinaryShapePred<LineData> {
00277 public:
00278 AngPi ang_tol;
00279 coordinate_t dist_tol;
00280 ColinearTest(AngPi _ang_tol=mathutils::deg2rad(20.0), coordinate_t _dist_tol=10) :
00281 ang_tol(_ang_tol), dist_tol(_dist_tol) {}
00282 bool operator() (const Shape<LineData> &line1, const Shape<LineData> &ln2) const;
00283 };
00284
00285
00286 class IsHorizontal : public UnaryShapePred<LineData> {
00287 public:
00288 IsHorizontal(AngPi thresh=M_PI/6) : UnaryShapePred<LineData>(), threshold(thresh) {}
00289 bool operator() (const Shape<LineData> &line) const;
00290 private:
00291 AngPi threshold;
00292 };
00293
00294
00295 class IsVertical : public UnaryShapePred<LineData> {
00296 public:
00297 IsVertical(AngPi thresh=M_PI/3) : UnaryShapePred<LineData>(), threshold(thresh) {}
00298 bool operator() (const Shape<LineData> &line) const;
00299 private:
00300 AngPi threshold;
00301 };
00302
00303
00304
00305
00306 virtual Sketch<bool>& getRendering();
00307
00308 private:
00309 static const int extractorGapTolerance = 8;
00310
00311
00312
00313
00314
00315 Sketch<bool>* render() const;
00316
00317
00318
00319
00320
00321 void setDrawCoords(float& x1,float& y1, float& x2, float& y2, const int width, const int height) const;
00322 static void drawline2d(Sketch<bool>& canvas, int x0, int y0, int x1, int y1);
00323
00324 };
00325
00326 }
00327
00328 #endif
00329