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 #define EXTRACT_LINE_MIN_AREA 20
00020 #define DEFAULT_MIN_LENGTH 4.0
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   
00040   //! Constructor
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   //! Constructor
00047   LineData(ShapeSpace& _space, const Point &pt, orientation_t orient);
00048 
00049   //! Copy constructor
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   //! Updates norm parameters (rho and theta)
00061   void update_derived_properties();
00062   
00063   //! Centroid. (Virtual in BaseData.)
00064   virtual Point getCentroid() const;  
00065   
00066   //! Makes endpoints inactive if value = true
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   //! Match lines based on their parameters.  (Virtual in BaseData.)
00077   virtual bool isMatchFor(const ShapeRoot& other) const;
00078   bool isMatchFor(const LineData& other) const;
00079 
00080   //! Lines are admissible to the local map if they're long enough to not be noise.
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   //checks if update of endpoints from (p1,p2) to (p3,p4) is acceptable
00092   bool isValidUpdate(coordinate_t p1, coordinate_t p2, coordinate_t p3, coordinate_t p4);
00093 
00094   //! Print information about this shape. (Virtual in BaseData.)
00095   virtual void printParams() const;
00096   
00097   //! Transformations. (Virtual in BaseData.)
00098   virtual void applyTransform(const NEWMAT::Matrix& Tmat, const ReferenceFrameType_t newref=unspecified);
00099   
00100   //! Project to ground
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   //!@name Point access functions.
00110   /*! The first point of a line is the leftmost point if the line is horizontal, 
00111   else the topmost point.  With an optional Shape<LineData> argument, uses the current
00112   line's orientation to pick the appropriate point of the other line.
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   //!@name Properties functions
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   //!@name Orientation functions
00156   //@{
00157   bool isNotVertical() const; //!< True if line orientation is far enough from vertical
00158   bool isOverlappedWith(const LineData& otherline, int amount=0) const;
00159   //  bool isRoughlyPerpendicularTo(Shape<LineData> &other);
00160   //  bool isExactlyPerpendicularTo(Shape<LineData> &other);
00161   //@}
00162   
00163   
00164   //!@name Predicates based on line length
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   //! Check if point falls between the two lines
00173   bool isBetween(const Point &p, const LineData &other) const;
00174 
00175   //!@name Check line intersection
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   //! Distance.
00197   float perpendicularDistanceFrom(const Point& other) const;
00198   
00199   
00200   // ==================================================
00201   // BEGIN SKETCH MANIPULATION AND LINE EXTRACTION CODE
00202   // ==================================================
00203   
00204   //!@name Line extraction
00205   //@{
00206 
00207   //! Extracts most prominent line from a skeletonized image.
00208   static Shape<LineData> extractLine(Sketch<bool>& sketch);
00209 
00210   //! Extracts most prominent line from a skeletonized image.
00211   //! It's often useful to use the original sketch as an occluder
00212   static Shape<LineData> extractLine(Sketch<bool>& skelsketch, 
00213              const Sketch<bool>& occlusions);
00214   //@}
00215   
00216   //! Helper functions used by extractLine().
00217   //@{
00218   static Shape<LineData> splitLine(ShapeSpace &ShS, Region &skelchunk,
00219            Sketch<bool> &skeleton, const Sketch<bool> &occlusions);
00220 
00221   //! Clears a line from a sketch.
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   //!@name Comparison predicates used by shape functions
00249   //@{
00250 
00251   //! True if line1 shorter than line2
00252   class LengthLessThan : public BinaryShapePred<LineData> {
00253   public:
00254     bool operator() (const Shape<LineData> &ln1, const Shape<LineData> &ln2) const;
00255   };
00256 
00257   //! True if difference in line orientations is <= tolerance (default 20 deg)
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   //! True if difference in line orientations is 90 deg +/- tolerance (default 20 deg)
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   //! True if line orientations are within @a ang_tol (default 20 deg) and normpoints are within @a dist_tol (default 10 units)
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   //! Predicate returns true if line orientation is within @a threshold of horizontal
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   //! Predicate returns true if line orientation is within threshold of vertical
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   //!@name  Rendering.
00312   //@{
00313   
00314   //! Render into a sketch space and return reference.
00315   Sketch<bool>* render() const;
00316   
00317   //! returns a Sketch which is true where the specified line is
00318   //! end0_stop and end1_stop specify whether rendering should stop at endpoints
00319   //  Sketch<bool>& drawline2d(SketchSpace &renderspace, 
00320   //         int x0, int y0, int x1, int y1) const;
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 } // namespace
00327 
00328 #endif
00329 

DualCoding 4.0
Generated Thu Nov 22 00:52:36 2007 by Doxygen 1.5.4