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