Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/hatcher is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_hatcher
0005 #define tools_hatcher
0006 
0007 /**
0008  *
0009  * hatcher is a class to draw Hatch in a 3D polyline plane
0010  * A hatch is caracterise by a direction (dirAngle), a spacing to get
0011  * second hatch,
0012  * an offset, and a stripWidth :
0013  * - offset value : between 0-1, This value set the offset of
0014  *   the hatch.0 meen that the hatch  will touch the first point
0015  *   of the polyline, and 1 meen that first hatch will be draw
0016  *   at a 'spacing' distance to first point
0017  * - offsetVec : the 3D point from where a hatch had to pass. This is
0018  *   very usefull to have hach continuing in different polygones
0019  *
0020  * The compute_polyline() method<br>
0021  * By default:
0022  * - spacing = .1;
0023  * - dirAngle = PI/4;
0024  * - offsetValue = 0;
0025  * - stripWidth=0.0;
0026  *
0027  * A way to get all points and vertices and to draw them can be :
0028  * <pre>
0029  *  iindex =0;
0030  *  icoord =0;
0031  *  for (unsigned int a=0;a<sbHatch.number_of_vertices();a++) {
0032  *    for (unsigned int b=0;b<sbHatch.number_of_vertices()[a];b++) {
0033  *      coordinate3->point.set1Value(icoord,sbHatch.get_points()[icoord]);
0034  *      indexedFaceSet->coordIndex.set1Value(iindex,icoord);
0035  *      iindex++;
0036  *      icoord ++;
0037  *    }
0038  *    indexedFaceSet->coordIndex.set1Value(iindex,SO_END_LINE_INDEX);
0039  *    iindex++;
0040  *  }
0041  * </pre>
0042  *
0043  * @author Laurent Garnier
0044  * Creation : on Fri Jan 05 2004
0045  * Last update : 9 April 2004
0046  *
0047  */
0048 
0049 #include "lina/vec3f"
0050 #include "lina/vec2f"
0051 #include "mathf"
0052 #include <cfloat> // for FLT_MAX
0053 
0054 namespace tools {
0055 
0056 class hatcher {
0057 public:
0058   hatcher()
0059   :fShift(.1f)
0060   ,fDirAngle(fpi()/4)
0061   ,fOffsetValue(.0f)
0062   ,fOffset(vec3f(FLT_MAX,FLT_MAX,FLT_MAX))
0063   ,fPrecisionFactor (.0001f) // good value to get rid of some errors
0064   ,fStripWidth(0.0)
0065   ,fFirstNumHatch(0)
0066   ,fNumberHatchToDraw(0)
0067   ,fFirstPolyline(true)
0068   ,fResolveResult(RESOLVE_UNDEFINED)
0069   {}
0070   virtual ~hatcher() {}
0071 protected:
0072   hatcher(const hatcher& a_from)
0073   :fNormal(a_from.fNormal)
0074   ,fShift(a_from.fShift)
0075   ,fDirAngle(a_from.fDirAngle)
0076   ,fOffsetValue(a_from.fOffsetValue)
0077   ,fOffset(a_from.fOffset)
0078   ,fShiftVec(a_from.fShiftVec)
0079   ,fPrecisionFactor(a_from.fPrecisionFactor)
0080   ,fDirVec(a_from.fDirVec)
0081   ,fStripWidth(a_from.fStripWidth)
0082   ,fPoints(a_from.fPoints)
0083   ,fVertices(a_from.fVertices)
0084   ,fConflictNumHatchLineTab(a_from.fConflictNumHatchLineTab)
0085   ,fHatchShiftToMatchPointVec(a_from.fHatchShiftToMatchPointVec)
0086   ,fFirstNumHatch(a_from.fFirstNumHatch)
0087   ,fNumberHatchToDraw(a_from.fNumberHatchToDraw)
0088   ,fFirstPolyline(a_from.fFirstPolyline)
0089   ,fResolveResult(a_from.fResolveResult)
0090   {}
0091   hatcher& operator=(const hatcher& a_from){
0092     fNormal = a_from.fNormal;
0093     fShift = a_from.fShift;
0094     fDirAngle = a_from.fDirAngle;
0095     fOffsetValue = a_from.fOffsetValue;
0096     fOffset = a_from.fOffset;
0097     fShiftVec = a_from.fShiftVec;
0098     fPrecisionFactor = a_from.fPrecisionFactor;
0099     fDirVec = a_from.fDirVec;
0100     fStripWidth = a_from.fStripWidth;
0101     fPoints = a_from.fPoints;
0102     fVertices = a_from.fVertices;
0103     fConflictNumHatchLineTab = a_from.fConflictNumHatchLineTab;
0104     fHatchShiftToMatchPointVec = a_from.fHatchShiftToMatchPointVec;
0105     fFirstNumHatch = a_from.fFirstNumHatch;
0106     fNumberHatchToDraw = a_from.fNumberHatchToDraw;
0107     fFirstPolyline = a_from.fFirstPolyline;
0108     fResolveResult = a_from.fResolveResult;
0109     return *this;
0110   }
0111 public:
0112   /**
0113   * draw the hatch into the polyline bounding box given in argument
0114   * You have to get all compute points by the get_points() method
0115   * Number of points can be get by number_of_points()
0116   * The  number of vertices in the return polyline can be get by number_of_vertices()
0117   * and vertice table by get_vertices
0118   * @return FALSE if :
0119   *    - All points are not in the same plan
0120   *    - There is a precision error on one or more point
0121   */
0122   bool compute_polyline (vec3f* listPoints,unsigned int number);
0123 
0124 
0125   /**
0126   * test if the polygone given is correct for hatching
0127   * @return FALSE if :
0128   *    - All points are not in the same plan
0129   *    - Number of points <3
0130   *    - Offset point is not in the same plan
0131   *    - There is less than three different points
0132   *    - The vector from point[0],point[1] is colinear to point[0],lastPoint
0133   */
0134   bool check_polyline(vec3f* listPoints,unsigned int number);
0135 
0136   void set_spacing(float a) {fShift = a;}       //set the spacing for this hatch.
0137   void set_angle(float a) {fDirAngle = a;}      //set the Direction angle for the hatch in radians.
0138   void set_offset(float a) {fOffsetValue = a;}  //set the offset value for this hatch.
0139   void set_offset_point(vec3f a) {fOffset = a;}  //set the offset Point for this hatch.
0140   void set_precision_factor(float a) {fPrecisionFactor = a;} //set the precision factor for computing (0.0001 is default).
0141 
0142   bool set_strip_width(float a) {
0143     // set the strip width value(0 is default and means no strip).
0144     if (a<0 || a>1) {
0145       fStripWidth = 0;
0146       return false;
0147     }
0148     fStripWidth = a;
0149     return true;
0150   }
0151 
0152   float get_spacing()const {return fShift;}
0153   float get_angle()const {return fDirAngle;}
0154   float get_offset()const {return fOffsetValue;}
0155   const vec3f& get_offset_point() {return fOffset;}
0156   float get_precision_factor()const {return fPrecisionFactor;}
0157   float get_strip_width()const {return fStripWidth;}
0158   const vec3f& get_normal() {return fNormal;}
0159 
0160   //size_t number_of_points() const {return fPoints.size();} //get the number of points compute for this hatch.
0161   /** vector of compute points<br>
0162    * Be careful with this function because it can return a set of non convex
0163    * polygone if you have a non convex polygone at beginning !So, when you want
0164    * to draw it, you have to use a tesselisation algorithm first
0165    */
0166   const std::vector<vec3f>& points() {return fPoints;}
0167 
0168   //size_t number_of_vertices() const {return fVertices.size();} //get the number of vertices compute for this hatch.
0169   /** vector of numbers of vertices */
0170   const std::vector<unsigned int>& vertices() {return fVertices;}
0171 
0172 protected:
0173 
0174   /**
0175   * draw the hatch into the polyline bounding box given in argument
0176   * @return FALSE if :
0177   *    - All points are not in the same plan
0178   *    - There is a precision error on one or more point
0179   */
0180   bool compute_single_polyline (vec3f* listPoints,unsigned int number);
0181 
0182 
0183   /**
0184   * Compute a vector system equation aA+bB=C
0185   * return SbVec2f(0,0) if there is an error
0186   * set the resolveResult variable to the error code :
0187   * COLINEAR if A and B are
0188   * PRECISION_ERROR if there is a lack of precision in computing
0189   * Z_ERROR if there s no solution for Z
0190   * UNDEFINED never throw
0191   * return a SbVec2f  for result. a is 'x' value and b is 'y' if it is correct
0192   */
0193   vec2f resolve_system(const vec3f& A,const vec3f& B,const vec3f& C);
0194 
0195 
0196 protected:
0197   /** normal vector for the current polyline */
0198   vec3f fNormal;
0199 
0200   /** Spacing vector between two hatch */
0201   float fShift; // Absloute distance between two hatch in the polyline plan  */
0202 
0203   /** The angle (given in radians) is the one between the first
0204    * line (point 1-point0) and the hatch lines, in the polyline plan.
0205    * Given in the direct axis ((point1-point0),(lastPoint-point0),
0206    * normalPlanVec). The angle in compute only one time for the first polyline.
0207    * Changes on angle value for others polyline will not take effect.This is to
0208    * perform correct hatching between the polylines
0209    */
0210   float fDirAngle;
0211 
0212   /** between 0-1. This value set the offset of the hatch.0 meen
0213   * that the hatch will touch the first point of the polyline,
0214   * and 1 meen that first hatch will be draw
0215   * at a 'spacing' distance to first point
0216   */
0217   float fOffsetValue;
0218 
0219   /** first point of the hatch.
0220    * offset = firstPolylinePoint+ShiftVec*offsetValue
0221    */
0222   vec3f fOffset;
0223 
0224   /** Orientation vector for the hatch  */
0225   vec3f fShiftVec;
0226 
0227   /** factor for compute error between two points */
0228   float fPrecisionFactor;
0229 
0230   /** hatch direction Vector */
0231   vec3f  fDirVec;
0232 
0233   /** strip with size : set to 0 by default
0234    * between 0 and 1.0 means no strip, 0.5 means strip size is
0235    * half of distance between two hatches
0236    */
0237   float fStripWidth;
0238 
0239   /** vector list of points */
0240   std::vector<vec3f> fPoints;
0241 
0242   /** vector vertices number */
0243   std::vector<unsigned int> fVertices;
0244 
0245   /** conflict line table */
0246   std::vector< std::vector<int> > fConflictNumHatchLineTab;
0247 
0248   /** hatchShiftToMatchPointVec tab*/
0249   std::vector<float> fHatchShiftToMatchPointVec;
0250 
0251   /** first hatch number to draw */
0252   int fFirstNumHatch;
0253 
0254   /**number of hatch to draw */
0255   unsigned int fNumberHatchToDraw;
0256 
0257   bool fFirstPolyline;
0258   enum ResolveErrors{
0259     RESOLVE_OK = 0,
0260     RESOLVE_COLINEAR,
0261     RESOLVE_Z_ERROR,
0262     RESOLVE_PRECISION_ERROR,
0263     RESOLVE_UNDEFINED
0264   };
0265   ResolveErrors fResolveResult;
0266 };
0267 
0268 }
0269 
0270 #include "hatcher.icc"
0271 
0272 #endif