Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

LineData.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef _LINEDATA_H_
00003 #define _LINEDATA_H_
00004 
00005 #include <vector>
00006 #include <iostream>
00007 
00008 #include "BaseData.h"    // superclass
00009 #include "EndPoint.h"    // EndPoint data member
00010 #include "ShapeFuns.h"
00011 #include "ShapePoint.h"
00012 #include "SketchTypes.h" // uint
00013 #include "Shared/mathutils.h"   // deg2rad
00014 
00015 #define TSIZE 180
00016 #define RSIZE 1600
00017 
00018 namespace DualCoding {
00019 
00020 class Region;
00021 
00022 //! A line shape, with two endpoints, a length, orientation, etc.
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>;   // grant access to line endpoints
00035   friend class PolygonData;
00036   friend class BlobData;
00037 
00038 public:
00039   static const float DEFAULT_MIN_LENGTH; //!< Min line length as a fraction of sketch width
00040   
00041   //! Constructor
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   //! Constructor
00048   LineData(ShapeSpace& _space, const Point &pt, orientation_t orient);
00049 
00050   //! Copy constructor
00051   LineData(const LineData& other);
00052 
00053   static ShapeType_t getStaticType() { return lineDataType; }
00054   
00055   DATASTUFF_H(LineData);
00056   
00057   //! Updates norm parameters (rho and theta)
00058   void update_derived_properties();
00059   
00060   //! Centroid. (Virtual in BaseData.)
00061   virtual Point getCentroid() const;  
00062   
00063   //! Makes endpoints inactive if value = true
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   //! Match lines based on their parameters.  (Virtual in BaseData.)
00073   virtual bool isMatchFor(const ShapeRoot& other) const;
00074   bool isMatchFor(const LineData& other) const;
00075 
00076   //! Lines are admissible to the local map if they're long enough to not be noise.
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   //checks if update of endpoints from (p1,p2) to (p3,p4) is acceptable
00088   bool isValidUpdate(coordinate_t p1, coordinate_t p2, coordinate_t p3, coordinate_t p4);
00089 
00090   //! Print information about this shape. (Virtual in BaseData.)
00091   virtual void printParams() const;
00092   
00093   //! Transformations. (Virtual in BaseData.)
00094   virtual void applyTransform(const fmat::Transform& Tmat, const ReferenceFrameType_t newref=unspecified);
00095   
00096   //! Project to ground
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   //!@name Point access functions.
00106   /*! The first point of a line is the leftmost point if the line is horizontal, 
00107   else the topmost point.  With an optional Shape<LineData> argument, uses the current
00108   line's orientation to pick the appropriate point of the other line.
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   //!@name Properties functions
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   //!@name Orientation functions
00158   //@{
00159   bool isNotVertical() const; //!< True if line orientation is far enough from vertical
00160   bool isOverlappedWith(const LineData& otherline, int amount=0) const;
00161   //  bool isRoughlyPerpendicularTo(Shape<LineData> &other);
00162   //  bool isExactlyPerpendicularTo(Shape<LineData> &other);
00163   //@}
00164   
00165   
00166   //!@name Left/Right predicates
00167   //@{
00168   bool pointIsLeftOf(const Point& pt) const; //!< Defined on Point, but will also work on PointData or Shape<PointData> due to type coercion
00169   bool pointIsRightOf(const Point& pt) const; //!< Defined on Point, but will also work on PointData or Shape<PointData> due to type coercion
00170   bool pointIsAbove(const Point& pt) const; //!< Defined on Point, but will also work on PointData or Shape<PointData> due to type coercion
00171   bool pointIsBelow(const Point& pt) const; //!< Defined on Point, but will also work on PointData or Shape<PointData> due to type coercion
00172 
00173   //!@name Predicates based on line length
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   //! Check if point falls between the two lines
00182   bool isBetween(const Point &p, const LineData &other) const;
00183 
00184   //!@name Check line intersection
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   //! Distance.
00206   float perpendicularDistanceFrom(const Point& other) const;
00207   
00208   
00209   // ==================================================
00210   // BEGIN SKETCH MANIPULATION AND LINE EXTRACTION CODE
00211   // ==================================================
00212   
00213   //!@name Line extraction
00214   //@{
00215 
00216   //! Extracts most prominent line from a skeletonized image.
00217   //static Shape<LineData> extractLine(Sketch<bool>& sketch);
00218 
00219   //static Shape<LineData> oldExtractLine(Sketch<bool>& sketch);
00220 
00221   //! Extracts most prominent line from a skeletonized image.
00222   //! It's often useful to use the original sketch as an occluder
00223   //static Shape<LineData> extractLine(Sketch<bool>& skelsketch, 
00224   //             const Sketch<bool>& occlusions);
00225   //static Shape<LineData> oldExtractLine(Sketch<bool>& skelsketch, 
00226   //             const Sketch<bool>& occlusions);
00227 
00228   //@}
00229   
00230   //! Helper functions used by extractLine().
00231   //@{
00232   static Shape<LineData> splitLine(ShapeSpace &ShS, Region &skelchunk,
00233            Sketch<bool> &skeleton, const Sketch<bool> &occlusions);
00234   //static Shape<LineData> oldSplitLine(ShapeSpace &ShS, Region &skelchunk,
00235   //           Sketch<bool> &skeleton, const Sketch<bool> &occlusions);
00236 
00237   //! Clears a line from a sketch.
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   //static std::vector<Shape<LineData> > oldExtractLines(Sketch<bool> const& sketch, int const num_lines=20);
00249   
00250   //static std::vector<Shape<LineData> > extractLines(Sketch<bool> const& sketch, int const num_lines=20);
00251   
00252   //static std::vector<Shape<LineData> > oldExtractLines(Sketch<bool> const& skel,
00253   //                 Sketch<bool> const& occluders,
00254   //                 int const num_lines=20);
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   // for internal use by extract line functions
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   //!@name Comparison predicates used by shape functions
00292   //@{
00293 
00294   //! True if line1 shorter than line2
00295   class LengthLessThan : public BinaryShapePred<LineData> {
00296   public:
00297     bool operator() (const Shape<LineData> &ln1, const Shape<LineData> &ln2) const;
00298   };
00299 
00300   //! True if difference in line orientations is <= tolerance (default 20 deg)
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   //! True if difference in line orientations is 90 deg +/- tolerance (default 20 deg)
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   //! True if line orientations are within @a ang_tol (default 20 deg) and normpoints are within @a dist_tol (default 10 units)
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   //! Predicate returns true if line orientation is within @a threshold of horizontal
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   //! Predicate returns true if line orientation is within threshold of vertical
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   //!@name  Rendering.
00355   //@{
00356   
00357   //! Render into a sketch space and return reference.
00358   Sketch<bool>* render() const;
00359   
00360   //! returns a Sketch which is true where the specified line is
00361   //! end0_stop and end1_stop specify whether rendering should stop at endpoints
00362   //  Sketch<bool>& drawline2d(SketchSpace &renderspace, 
00363   //         int x0, int y0, int x1, int y1) const;
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 } // namespace
00370 
00371 #endif
00372 

DualCoding 5.1CVS
Generated Sat May 4 06:29:26 2013 by Doxygen 1.6.3