Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_hatcher 5 #define tools_hatcher 6 7 /** 8 * 9 * hatcher is a class to draw Hatch in a 3D polyline plane 10 * A hatch is caracterise by a direction (dirAngle), a spacing to get 11 * second hatch, 12 * an offset, and a stripWidth : 13 * - offset value : between 0-1, This value set the offset of 14 * the hatch.0 meen that the hatch will touch the first point 15 * of the polyline, and 1 meen that first hatch will be draw 16 * at a 'spacing' distance to first point 17 * - offsetVec : the 3D point from where a hatch had to pass. This is 18 * very usefull to have hach continuing in different polygones 19 * 20 * The compute_polyline() method<br> 21 * By default: 22 * - spacing = .1; 23 * - dirAngle = PI/4; 24 * - offsetValue = 0; 25 * - stripWidth=0.0; 26 * 27 * A way to get all points and vertices and to draw them can be : 28 * <pre> 29 * iindex =0; 30 * icoord =0; 31 * for (unsigned int a=0;a<sbHatch.number_of_vertices();a++) { 32 * for (unsigned int b=0;b<sbHatch.number_of_vertices()[a];b++) { 33 * coordinate3->point.set1Value(icoord,sbHatch.get_points()[icoord]); 34 * indexedFaceSet->coordIndex.set1Value(iindex,icoord); 35 * iindex++; 36 * icoord ++; 37 * } 38 * indexedFaceSet->coordIndex.set1Value(iindex,SO_END_LINE_INDEX); 39 * iindex++; 40 * } 41 * </pre> 42 * 43 * @author Laurent Garnier 44 * Creation : on Fri Jan 05 2004 45 * Last update : 9 April 2004 46 * 47 */ 48 49 #include "lina/vec3f" 50 #include "lina/vec2f" 51 #include "mathf" 52 #include <cfloat> // for FLT_MAX 53 54 namespace tools { 55 56 class hatcher { 57 public: 58 hatcher() 59 :fShift(.1f) 60 ,fDirAngle(fpi()/4) 61 ,fOffsetValue(.0f) 62 ,fOffset(vec3f(FLT_MAX,FLT_MAX,FLT_MAX)) 63 ,fPrecisionFactor (.0001f) // good value to get rid of some errors 64 ,fStripWidth(0.0) 65 ,fFirstNumHatch(0) 66 ,fNumberHatchToDraw(0) 67 ,fFirstPolyline(true) 68 ,fResolveResult(RESOLVE_UNDEFINED) 69 {} 70 virtual ~hatcher() {} 71 protected: 72 hatcher(const hatcher& a_from) 73 :fNormal(a_from.fNormal) 74 ,fShift(a_from.fShift) 75 ,fDirAngle(a_from.fDirAngle) 76 ,fOffsetValue(a_from.fOffsetValue) 77 ,fOffset(a_from.fOffset) 78 ,fShiftVec(a_from.fShiftVec) 79 ,fPrecisionFactor(a_from.fPrecisionFactor) 80 ,fDirVec(a_from.fDirVec) 81 ,fStripWidth(a_from.fStripWidth) 82 ,fPoints(a_from.fPoints) 83 ,fVertices(a_from.fVertices) 84 ,fConflictNumHatchLineTab(a_from.fConflictNumHatchLineTab) 85 ,fHatchShiftToMatchPointVec(a_from.fHatchShiftToMatchPointVec) 86 ,fFirstNumHatch(a_from.fFirstNumHatch) 87 ,fNumberHatchToDraw(a_from.fNumberHatchToDraw) 88 ,fFirstPolyline(a_from.fFirstPolyline) 89 ,fResolveResult(a_from.fResolveResult) 90 {} 91 hatcher& operator=(const hatcher& a_from){ 92 fNormal = a_from.fNormal; 93 fShift = a_from.fShift; 94 fDirAngle = a_from.fDirAngle; 95 fOffsetValue = a_from.fOffsetValue; 96 fOffset = a_from.fOffset; 97 fShiftVec = a_from.fShiftVec; 98 fPrecisionFactor = a_from.fPrecisionFactor; 99 fDirVec = a_from.fDirVec; 100 fStripWidth = a_from.fStripWidth; 101 fPoints = a_from.fPoints; 102 fVertices = a_from.fVertices; 103 fConflictNumHatchLineTab = a_from.fConflictNumHatchLineTab; 104 fHatchShiftToMatchPointVec = a_from.fHatchShiftToMatchPointVec; 105 fFirstNumHatch = a_from.fFirstNumHatch; 106 fNumberHatchToDraw = a_from.fNumberHatchToDraw; 107 fFirstPolyline = a_from.fFirstPolyline; 108 fResolveResult = a_from.fResolveResult; 109 return *this; 110 } 111 public: 112 /** 113 * draw the hatch into the polyline bounding box given in argument 114 * You have to get all compute points by the get_points() method 115 * Number of points can be get by number_of_points() 116 * The number of vertices in the return polyline can be get by number_of_vertices() 117 * and vertice table by get_vertices 118 * @return FALSE if : 119 * - All points are not in the same plan 120 * - There is a precision error on one or more point 121 */ 122 bool compute_polyline (vec3f* listPoints,unsigned int number); 123 124 125 /** 126 * test if the polygone given is correct for hatching 127 * @return FALSE if : 128 * - All points are not in the same plan 129 * - Number of points <3 130 * - Offset point is not in the same plan 131 * - There is less than three different points 132 * - The vector from point[0],point[1] is colinear to point[0],lastPoint 133 */ 134 bool check_polyline(vec3f* listPoints,unsigned int number); 135 136 void set_spacing(float a) {fShift = a;} //set the spacing for this hatch. 137 void set_angle(float a) {fDirAngle = a;} //set the Direction angle for the hatch in radians. 138 void set_offset(float a) {fOffsetValue = a;} //set the offset value for this hatch. 139 void set_offset_point(vec3f a) {fOffset = a;} //set the offset Point for this hatch. 140 void set_precision_factor(float a) {fPrecisionFactor = a;} //set the precision factor for computing (0.0001 is default). 141 142 bool set_strip_width(float a) { 143 // set the strip width value(0 is default and means no strip). 144 if (a<0 || a>1) { 145 fStripWidth = 0; 146 return false; 147 } 148 fStripWidth = a; 149 return true; 150 } 151 152 float get_spacing()const {return fShift;} 153 float get_angle()const {return fDirAngle;} 154 float get_offset()const {return fOffsetValue;} 155 const vec3f& get_offset_point() {return fOffset;} 156 float get_precision_factor()const {return fPrecisionFactor;} 157 float get_strip_width()const {return fStripWidth;} 158 const vec3f& get_normal() {return fNormal;} 159 160 //size_t number_of_points() const {return fPoints.size();} //get the number of points compute for this hatch. 161 /** vector of compute points<br> 162 * Be careful with this function because it can return a set of non convex 163 * polygone if you have a non convex polygone at beginning !So, when you want 164 * to draw it, you have to use a tesselisation algorithm first 165 */ 166 const std::vector<vec3f>& points() {return fPoints;} 167 168 //size_t number_of_vertices() const {return fVertices.size();} //get the number of vertices compute for this hatch. 169 /** vector of numbers of vertices */ 170 const std::vector<unsigned int>& vertices() {return fVertices;} 171 172 protected: 173 174 /** 175 * draw the hatch into the polyline bounding box given in argument 176 * @return FALSE if : 177 * - All points are not in the same plan 178 * - There is a precision error on one or more point 179 */ 180 bool compute_single_polyline (vec3f* listPoints,unsigned int number); 181 182 183 /** 184 * Compute a vector system equation aA+bB=C 185 * return SbVec2f(0,0) if there is an error 186 * set the resolveResult variable to the error code : 187 * COLINEAR if A and B are 188 * PRECISION_ERROR if there is a lack of precision in computing 189 * Z_ERROR if there s no solution for Z 190 * UNDEFINED never throw 191 * return a SbVec2f for result. a is 'x' value and b is 'y' if it is correct 192 */ 193 vec2f resolve_system(const vec3f& A,const vec3f& B,const vec3f& C); 194 195 196 protected: 197 /** normal vector for the current polyline */ 198 vec3f fNormal; 199 200 /** Spacing vector between two hatch */ 201 float fShift; // Absloute distance between two hatch in the polyline plan */ 202 203 /** The angle (given in radians) is the one between the first 204 * line (point 1-point0) and the hatch lines, in the polyline plan. 205 * Given in the direct axis ((point1-point0),(lastPoint-point0), 206 * normalPlanVec). The angle in compute only one time for the first polyline. 207 * Changes on angle value for others polyline will not take effect.This is to 208 * perform correct hatching between the polylines 209 */ 210 float fDirAngle; 211 212 /** between 0-1. This value set the offset of the hatch.0 meen 213 * that the hatch will touch the first point of the polyline, 214 * and 1 meen that first hatch will be draw 215 * at a 'spacing' distance to first point 216 */ 217 float fOffsetValue; 218 219 /** first point of the hatch. 220 * offset = firstPolylinePoint+ShiftVec*offsetValue 221 */ 222 vec3f fOffset; 223 224 /** Orientation vector for the hatch */ 225 vec3f fShiftVec; 226 227 /** factor for compute error between two points */ 228 float fPrecisionFactor; 229 230 /** hatch direction Vector */ 231 vec3f fDirVec; 232 233 /** strip with size : set to 0 by default 234 * between 0 and 1.0 means no strip, 0.5 means strip size is 235 * half of distance between two hatches 236 */ 237 float fStripWidth; 238 239 /** vector list of points */ 240 std::vector<vec3f> fPoints; 241 242 /** vector vertices number */ 243 std::vector<unsigned int> fVertices; 244 245 /** conflict line table */ 246 std::vector< std::vector<int> > fConflictNumHatchLineTab; 247 248 /** hatchShiftToMatchPointVec tab*/ 249 std::vector<float> fHatchShiftToMatchPointVec; 250 251 /** first hatch number to draw */ 252 int fFirstNumHatch; 253 254 /**number of hatch to draw */ 255 unsigned int fNumberHatchToDraw; 256 257 bool fFirstPolyline; 258 enum ResolveErrors{ 259 RESOLVE_OK = 0, 260 RESOLVE_COLINEAR, 261 RESOLVE_Z_ERROR, 262 RESOLVE_PRECISION_ERROR, 263 RESOLVE_UNDEFINED 264 }; 265 ResolveErrors fResolveResult; 266 }; 267 268 } 269 270 #include "hatcher.icc" 271 272 #endif