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 namespace DualCoding {
00016 
00017 class Region;
00018 
00019 //! A line shape, with two endpoints, a length, orientation, etc.
00020 class LineData : public BaseData {
00021 public:
00022   static const int TSIZE = 180; //!< number of theta entries in Hough table
00023   static const int RSIZE = 3000; //!< number of distance entries in Hough table; was 1600
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>;   // grant access to line endpoints
00036   friend class PolygonData;
00037   friend class BlobData;
00038 
00039 public:
00040   static const float DEFAULT_MIN_LENGTH; //!< Min line length as a fraction of sketch width
00041   
00042   //! Constructor
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   //! Constructor
00049   LineData(ShapeSpace& _space, const Point &pt, orientation_t orient);
00050   
00051   //! Constructor
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   //! Copy constructor
00059   LineData(const LineData& other);
00060 
00061   static ShapeType_t getStaticType() { return lineDataType; }
00062   
00063   DATASTUFF_H(LineData);
00064   
00065   //! Updates norm parameters (rho and theta)
00066   void update_derived_properties();
00067   
00068   //! Centroid. (Virtual in BaseData.)
00069   virtual Point getCentroid() const;  
00070   
00071   //! Makes endpoints inactive if value = true
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   //! Match lines based on their parameters.  (Virtual in BaseData.)
00081   virtual bool isMatchFor(const ShapeRoot& other) const;
00082   bool isMatchFor(const LineData& other) const;
00083 
00084   //! Lines are admissible to the local map if they're long enough to not be noise.
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   //checks if update of endpoints from (p1,p2) to (p3,p4) is acceptable
00097   bool isValidUpdate(coordinate_t p1, coordinate_t p2, coordinate_t p3, coordinate_t p4);
00098 
00099   //! Print information about this shape. (Virtual in BaseData.)
00100   virtual void printParams() const;
00101   
00102   //! Transformations. (Virtual in BaseData.)
00103   virtual void applyTransform(const fmat::Transform& Tmat, const ReferenceFrameType_t newref=unspecified);
00104   
00105   //! Project to ground
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   //!@name Point access functions.
00115   /*! The first point of a line is the leftmost point if the line is horizontal, 
00116   else the topmost point.  With an optional Shape<LineData> argument, uses the current
00117   line's orientation to pick the appropriate point of the other line.
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   //!@name Properties functions
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   //!@name Orientation functions
00167   //@{
00168   bool isNotVertical() const; //!< True if line orientation is far enough from vertical
00169   bool isOverlappedWith(const LineData& otherline, int amount=0) const;
00170   //  bool isRoughlyPerpendicularTo(Shape<LineData> &other);
00171   //  bool isExactlyPerpendicularTo(Shape<LineData> &other);
00172   //@}
00173   
00174   
00175   //!@name Left/Right predicates
00176   //@{
00177   bool pointIsLeftOf(const Point& pt) const; //!< Defined on Point, but will also work on PointData or Shape<PointData> due to type coercion
00178   bool pointIsRightOf(const Point& pt) const; //!< Defined on Point, but will also work on PointData or Shape<PointData> due to type coercion
00179   bool pointIsAbove(const Point& pt) const; //!< Defined on Point, but will also work on PointData or Shape<PointData> due to type coercion
00180   bool pointIsBelow(const Point& pt) const; //!< Defined on Point, but will also work on PointData or Shape<PointData> due to type coercion
00181 
00182   //!@name Predicates based on line length
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   //! Check if point falls between the two lines
00191   bool isBetween(const Point &p, const LineData &other) const;
00192 
00193   //!@name Check line intersection
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   //! Distance.
00215   float perpendicularDistanceFrom(const Point& other) const;
00216   
00217   
00218   // ==================================================
00219   // BEGIN SKETCH MANIPULATION AND LINE EXTRACTION CODE
00220   // ==================================================
00221   
00222   //!@name Line extraction
00223   //@{
00224 
00225   //! Extracts most prominent line from a skeletonized image.
00226   //static Shape<LineData> extractLine(Sketch<bool>& sketch);
00227 
00228   //static Shape<LineData> oldExtractLine(Sketch<bool>& sketch);
00229 
00230   //! Extracts most prominent line from a skeletonized image.
00231   //! It's often useful to use the original sketch as an occluder
00232   //static Shape<LineData> extractLine(Sketch<bool>& skelsketch, 
00233   //             const Sketch<bool>& occlusions);
00234   //static Shape<LineData> oldExtractLine(Sketch<bool>& skelsketch, 
00235   //             const Sketch<bool>& occlusions);
00236 
00237   //@}
00238   
00239   //! Helper functions used by extractLine().
00240   //@{
00241   static Shape<LineData> splitLine(ShapeSpace &ShS, Region &skelchunk,
00242            Sketch<bool> &skeleton, const Sketch<bool> &occlusions);
00243   //static Shape<LineData> oldSplitLine(ShapeSpace &ShS, Region &skelchunk,
00244   //           Sketch<bool> &skeleton, const Sketch<bool> &occlusions);
00245 
00246   //! Clears a line from a sketch.
00247   void clearLine(Sketch<bool>& sketch);
00248   
00249   static int const beg_dist_thresh = 2; //!< skel has to be this close to start a segment
00250   static int const end_dist_thresh = 2; //!< how far line can extend past skel before giving up
00251   static int const line_min_length = 15; //!< minimum x or y length in pixels
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   //static std::vector<Shape<LineData> > oldExtractLines(Sketch<bool> const& sketch, int const num_lines=20);
00259   
00260   //static std::vector<Shape<LineData> > extractLines(Sketch<bool> const& sketch, int const num_lines=20);
00261   
00262   //static std::vector<Shape<LineData> > oldExtractLines(Sketch<bool> const& skel,
00263   //                 Sketch<bool> const& occluders,
00264   //                 int const num_lines=20);
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   // for internal use by extract line functions
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   //!@name Comparison predicates used by shape functions
00302   //@{
00303 
00304   //! True if line1 shorter than line2
00305   class LengthLessThan : public BinaryShapePred<LineData> {
00306   public:
00307     bool operator() (const Shape<LineData> &ln1, const Shape<LineData> &ln2) const;
00308   };
00309 
00310   //! True if difference in line orientations is <= tolerance (default 20 deg)
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   //! True if difference in line orientations is 90 deg +/- tolerance (default 20 deg)
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   //! True if line orientations are within @a ang_tol (default 20 deg) and normpoints are within @a dist_tol (default 10 units)
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   //! Predicate returns true if line orientation is within @a threshold of horizontal
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   //! Predicate returns true if line orientation is within threshold of vertical
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   //!@name  Rendering.
00366   //@{
00367   
00368   //! Render into a sketch space and return reference.
00369   Sketch<bool>* render() const;
00370   
00371   //! returns a Sketch which is true where the specified line is
00372   //! end0_stop and end1_stop specify whether rendering should stop at endpoints
00373   //  Sketch<bool>& drawline2d(SketchSpace &renderspace, 
00374   //         int x0, int y0, int x1, int y1) const;
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 } // namespace
00381 
00382 #endif
00383 

DualCoding 5.1CVS
Generated Mon May 9 04:56:26 2016 by Doxygen 1.6.3