7fd59977 |
1 | // File: MeshVS_Tool.cxx |
2 | // Created: Wed Dec 17 2003 |
3 | // Author: Alexander SOLOVYOV |
4 | // Copyright: Open CASCADE 2003 |
5 | |
6 | #include <MeshVS_Tool.ixx> |
7 | |
8 | #include <MeshVS_DrawerAttribute.hxx> |
9 | #include <Precision.hxx> |
10 | #include <Graphic3d_NameOfFont.hxx> |
11 | |
12 | //================================================================ |
13 | // Function : CreateAspectFillArea3d |
14 | // Purpose : |
15 | //================================================================ |
16 | Handle( Graphic3d_AspectFillArea3d ) MeshVS_Tool::CreateAspectFillArea3d |
17 | ( const Handle(MeshVS_Drawer)& theDr, |
18 | const Graphic3d_MaterialAspect& Mat, |
19 | const Standard_Boolean UseDefaults ) |
20 | { |
21 | Handle( Graphic3d_AspectFillArea3d ) anAsp; |
22 | if ( theDr.IsNull() ) |
23 | return anAsp; |
24 | |
25 | Aspect_InteriorStyle anIntStyle = Aspect_IS_EMPTY; |
26 | Quantity_Color anIntColor = Quantity_NOC_CYAN1, |
27 | anEdgeColor = Quantity_NOC_WHITE; |
28 | Aspect_TypeOfLine anEdgeType = Aspect_TOL_SOLID; |
29 | Standard_Real anEdgeWidth = 1.0; |
30 | Aspect_HatchStyle aHStyle = Aspect_HS_HORIZONTAL; |
31 | Graphic3d_MaterialAspect aFrMat = Mat, |
32 | aBackMat = Mat; |
33 | |
34 | Standard_Integer anIntStyleI = (Standard_Integer)Aspect_IS_EMPTY; |
35 | Standard_Integer anEdgeTypeI = (Standard_Integer)Aspect_TOL_SOLID; |
36 | Standard_Integer aHStyleI = (Standard_Integer)Aspect_HS_HORIZONTAL; |
37 | |
38 | if ( !theDr->GetColor ( MeshVS_DA_InteriorColor, anIntColor ) && !UseDefaults ) |
39 | return anAsp; |
40 | |
41 | Quantity_Color aBackIntColor = anIntColor; |
42 | if ( !theDr->GetColor ( MeshVS_DA_BackInteriorColor, aBackIntColor ) && !UseDefaults ) |
43 | return anAsp; |
44 | |
45 | if ( !theDr->GetColor ( MeshVS_DA_EdgeColor, anEdgeColor ) && !UseDefaults ) |
46 | return anAsp; |
47 | |
48 | if ( !theDr->GetDouble ( MeshVS_DA_EdgeWidth, anEdgeWidth ) && !UseDefaults ) |
49 | return anAsp; |
50 | |
51 | if ( !theDr->GetInteger ( MeshVS_DA_InteriorStyle, anIntStyleI ) && !UseDefaults ) |
52 | return anAsp; |
53 | else |
54 | anIntStyle = (Aspect_InteriorStyle) anIntStyleI; |
55 | |
56 | if ( !theDr->GetInteger ( MeshVS_DA_EdgeType, anEdgeTypeI ) && !UseDefaults ) |
57 | return anAsp; |
58 | else |
59 | anEdgeType = (Aspect_TypeOfLine) anEdgeTypeI; |
60 | |
61 | if ( !theDr->GetInteger ( MeshVS_DA_HatchStyle, aHStyleI ) && !UseDefaults ) |
62 | return anAsp; |
63 | else |
64 | aHStyle = (Aspect_HatchStyle) aHStyleI; |
65 | |
66 | anAsp = new Graphic3d_AspectFillArea3d ( anIntStyle, anIntColor, anEdgeColor, anEdgeType, |
67 | anEdgeWidth, aFrMat, aBackMat ); |
68 | anAsp->SetBackInteriorColor ( aBackIntColor ); |
69 | anAsp->SetHatchStyle ( aHStyle ); |
70 | |
71 | return anAsp; |
72 | } |
73 | |
74 | //================================================================ |
75 | // Function : CreateAspectFillArea3d |
76 | // Purpose : |
77 | //================================================================ |
78 | Handle( Graphic3d_AspectFillArea3d ) MeshVS_Tool::CreateAspectFillArea3d |
79 | ( const Handle(MeshVS_Drawer)& theDr, |
80 | const Standard_Boolean UseDefaults ) |
81 | { |
82 | Graphic3d_MaterialAspect aFrMat = Graphic3d_NOM_BRASS; |
83 | Graphic3d_MaterialAspect aBackMat = Graphic3d_NOM_BRASS; |
84 | Standard_Integer aFrMatI = (Standard_Integer)Graphic3d_NOM_BRASS; |
85 | Standard_Integer aBackMatI = (Standard_Integer)Graphic3d_NOM_BRASS; |
86 | |
87 | if ( !theDr->GetInteger ( MeshVS_DA_FrontMaterial, aFrMatI ) && !UseDefaults ) |
88 | return 0; |
89 | else |
90 | aFrMat = (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)aFrMatI; |
91 | |
92 | if ( !theDr->GetInteger ( MeshVS_DA_BackMaterial, aBackMatI ) && !UseDefaults ) |
93 | return 0; |
94 | else |
95 | aBackMat = (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)aBackMatI; |
96 | |
97 | Handle( Graphic3d_AspectFillArea3d ) aFill = |
98 | CreateAspectFillArea3d ( theDr, aFrMat, UseDefaults ); |
99 | aFill->SetBackMaterial ( aBackMat ); |
100 | |
101 | return aFill; |
102 | } |
103 | //================================================================ |
104 | // Function : CreateAspectLine3d |
105 | // Purpose : |
106 | //================================================================ |
107 | Handle( Graphic3d_AspectLine3d ) MeshVS_Tool::CreateAspectLine3d |
108 | ( const Handle(MeshVS_Drawer)& theDr, |
109 | const Standard_Boolean UseDefaults ) |
110 | { |
111 | Handle( Graphic3d_AspectLine3d ) anAsp; |
112 | if ( theDr.IsNull() ) |
113 | return anAsp; |
114 | |
115 | Quantity_Color aBeamColor = Quantity_NOC_YELLOW; |
116 | Aspect_TypeOfLine aBeamType = Aspect_TOL_SOLID; |
117 | Standard_Real aBeamWidth = 1.0; |
118 | Standard_Integer aBeamTypeI = (Standard_Integer)Aspect_TOL_SOLID; |
119 | |
120 | if ( !theDr->GetColor ( MeshVS_DA_BeamColor, aBeamColor ) && !UseDefaults ) |
121 | return anAsp; |
122 | |
123 | if ( !theDr->GetDouble ( MeshVS_DA_BeamWidth, aBeamWidth ) && !UseDefaults ) |
124 | return anAsp; |
125 | |
126 | if ( !theDr->GetInteger ( MeshVS_DA_BeamType, aBeamTypeI ) && !UseDefaults ) |
127 | return anAsp; |
128 | else |
129 | aBeamType = (Aspect_TypeOfLine) aBeamTypeI; |
130 | |
131 | anAsp = new Graphic3d_AspectLine3d ( aBeamColor, aBeamType, aBeamWidth ); |
132 | |
133 | return anAsp; |
134 | } |
135 | |
136 | //================================================================ |
137 | // Function : CreateAspectMarker3d |
138 | // Purpose : |
139 | //================================================================ |
140 | Handle( Graphic3d_AspectMarker3d ) MeshVS_Tool::CreateAspectMarker3d |
141 | ( const Handle(MeshVS_Drawer)& theDr, |
142 | const Standard_Boolean UseDefaults ) |
143 | { |
144 | Handle( Graphic3d_AspectMarker3d ) anAsp; |
145 | if ( theDr.IsNull() ) |
146 | return anAsp; |
147 | |
148 | Quantity_Color aMColor = Quantity_NOC_YELLOW; |
149 | Aspect_TypeOfMarker aMType = Aspect_TOM_X; |
150 | Standard_Real aMScale = 1.0; |
151 | Standard_Integer aMTypeI = (Standard_Integer)Aspect_TOM_X; |
152 | |
153 | if ( !theDr->GetColor ( MeshVS_DA_MarkerColor, aMColor ) && !UseDefaults ) |
154 | return anAsp; |
155 | |
156 | if ( !theDr->GetDouble ( MeshVS_DA_MarkerScale, aMScale ) && !UseDefaults ) |
157 | return anAsp; |
158 | |
159 | if ( !theDr->GetInteger ( MeshVS_DA_MarkerType, aMTypeI ) && !UseDefaults ) |
160 | return anAsp; |
161 | else |
162 | aMType = (Aspect_TypeOfMarker) aMTypeI; |
163 | |
164 | anAsp = new Graphic3d_AspectMarker3d ( aMType, aMColor, aMScale ); |
165 | |
166 | return anAsp; |
167 | } |
168 | |
169 | //================================================================ |
170 | // Function : CreateAspectText3d |
171 | // Purpose : |
172 | //================================================================ |
173 | Handle( Graphic3d_AspectText3d ) MeshVS_Tool::CreateAspectText3d |
174 | ( const Handle(MeshVS_Drawer)& theDr, |
175 | const Standard_Boolean UseDefaults ) |
176 | { |
177 | Handle( Graphic3d_AspectText3d ) anAsp; |
178 | if ( theDr.IsNull() ) |
179 | return anAsp; |
180 | |
181 | Quantity_Color aTColor = Quantity_NOC_YELLOW; |
182 | Standard_Real anExpFactor = 1.0, |
183 | aSpace = 0.0; |
184 | Standard_CString aFont = Graphic3d_NOF_ASCII_MONO; |
185 | Aspect_TypeOfStyleText aStyle = Aspect_TOST_NORMAL; |
186 | Aspect_TypeOfDisplayText aDispText = Aspect_TODT_NORMAL; |
187 | TCollection_AsciiString aFontString = Graphic3d_NOF_ASCII_MONO; |
188 | OSD_FontAspect aFontAspect = OSD_FA_Regular; |
189 | Standard_Integer aStyleI = (Standard_Integer)Aspect_TOST_NORMAL; |
190 | Standard_Integer aDispTextI = (Standard_Integer)Aspect_TODT_NORMAL; |
191 | // Bold font is used by default for better text readability |
192 | Standard_Integer aFontAspectI = (Standard_Integer)OSD_FA_Bold; |
193 | |
194 | if ( !theDr->GetColor ( MeshVS_DA_TextColor, aTColor ) && !UseDefaults ) |
195 | return anAsp; |
196 | |
197 | if ( !theDr->GetDouble ( MeshVS_DA_TextExpansionFactor, anExpFactor ) && !UseDefaults ) |
198 | return anAsp; |
199 | |
200 | if ( !theDr->GetDouble ( MeshVS_DA_TextSpace, aSpace ) && !UseDefaults ) |
201 | return anAsp; |
202 | |
203 | if ( !theDr->GetAsciiString ( MeshVS_DA_TextFont, aFontString ) && !UseDefaults ) |
204 | return anAsp; |
205 | else |
206 | aFont = aFontString.ToCString(); |
207 | |
208 | if ( !theDr->GetInteger ( MeshVS_DA_TextStyle, aStyleI ) && !UseDefaults ) |
209 | return anAsp; |
210 | else |
211 | aStyle = (Aspect_TypeOfStyleText) aStyleI; |
212 | |
213 | if ( !theDr->GetInteger ( MeshVS_DA_TextDisplayType, aDispTextI ) && !UseDefaults ) |
214 | return anAsp; |
215 | else |
216 | aDispText = (Aspect_TypeOfDisplayText) aDispTextI; |
217 | |
218 | if ( !theDr->GetInteger ( MeshVS_DA_TextFontAspect, aFontAspectI ) && !UseDefaults ) |
219 | return anAsp; |
220 | else |
221 | aFontAspect = (OSD_FontAspect) aFontAspectI; |
222 | |
223 | anAsp = new Graphic3d_AspectText3d ( aTColor, aFont, anExpFactor, aSpace, aStyle, aDispText ); |
224 | anAsp->SetTextFontAspect( aFontAspect ); |
225 | return anAsp; |
226 | } |
227 | |
228 | //================================================================ |
229 | // Function : GetNormal |
230 | // Purpose : |
231 | //================================================================ |
232 | Standard_Boolean MeshVS_Tool::GetNormal( const TColStd_Array1OfReal& Nodes, |
233 | gp_Vec& Norm ) |
234 | { |
235 | Standard_Integer first = Nodes.Lower(), |
236 | last = Nodes.Upper(), |
237 | count = (last-first+1)/3, i, j; |
238 | if( first==0 ) |
239 | { |
240 | first = 1; |
241 | count = Standard_Integer( Nodes.Value( 0 ) ); |
242 | } |
243 | |
244 | if( count<3 ) |
245 | return Standard_False; |
246 | |
247 | Standard_Boolean res = Standard_True; |
248 | |
249 | |
250 | Standard_Real normal[3], first_vec[3], cur_vec[3], xx, yy, zz, |
251 | conf = Precision::Confusion(); |
252 | |
253 | for( i=0; i<3; i++ ) |
254 | { |
255 | normal[i] = 0.0; |
256 | first_vec[i] = Nodes.Value( first+3+i ) - Nodes.Value( first+i ); |
257 | } |
258 | |
259 | for( i=2; i<count; i++ ) |
260 | { |
261 | for( j=0; j<3; j++ ) |
262 | cur_vec[j] = Nodes.Value( first+3*i+j ) - Nodes.Value( first+j ); |
263 | |
264 | xx = first_vec[1] * cur_vec[2] - first_vec[2] * cur_vec[1]; |
265 | yy = first_vec[2] * cur_vec[0] - first_vec[0] * cur_vec[2]; |
266 | zz = first_vec[0] * cur_vec[1] - first_vec[1] * cur_vec[0]; |
267 | |
268 | cur_vec[0] = xx; |
269 | cur_vec[1] = yy; |
270 | cur_vec[2] = zz; |
271 | |
272 | if( fabs( cur_vec[0] ) > conf || |
273 | fabs( cur_vec[1] ) > conf || |
274 | fabs( cur_vec[2] ) > conf ) |
275 | { |
276 | Standard_Real cur = Sqrt( cur_vec[0]*cur_vec[0] + cur_vec[1]*cur_vec[1] + cur_vec[2]*cur_vec[2] ); |
277 | for( Standard_Integer k=0; k<3; k++ ) |
278 | cur_vec[k] /= cur; |
279 | } |
280 | |
281 | if( fabs( normal[0] ) <= conf && |
282 | fabs( normal[1] ) <= conf && |
283 | fabs( normal[2] ) <= conf ) |
284 | for( Standard_Integer k=0; k<3; k++ ) |
285 | normal[k] = cur_vec[k]; |
286 | |
287 | if( fabs( normal[0]-cur_vec[0] ) > conf || |
288 | fabs( normal[1]-cur_vec[1] ) > conf || |
289 | fabs( normal[2]-cur_vec[2] ) > conf ) |
290 | { |
291 | res = Standard_False; |
292 | break; |
293 | } |
294 | } |
295 | |
296 | if( res ) |
297 | Norm.SetCoord( normal[0], normal[1], normal[2] ); |
298 | |
299 | return res; |
300 | } |
301 | |
302 | |
303 | //================================================================ |
304 | // Function : GetAverageNormal |
305 | // Purpose : |
306 | //================================================================ |
307 | Standard_Boolean MeshVS_Tool::GetAverageNormal( const TColStd_Array1OfReal& Nodes, |
308 | gp_Vec& Norm ) |
309 | { |
310 | Standard_Integer first = Nodes.Lower(), |
311 | last = Nodes.Upper(), |
312 | count = (last-first+1)/3, i, j; |
313 | if( first==0 ) |
314 | { |
315 | first = 1; |
316 | count = Standard_Integer( Nodes.Value( 0 ) ); |
317 | } |
318 | |
319 | if( count<3 ) |
320 | return Standard_False; |
321 | |
322 | Standard_Boolean res = Standard_True; |
323 | |
324 | |
325 | Standard_Real normal[3], first_vec[3], cur_vec[3], xx, yy, zz, |
326 | conf = Precision::Confusion(); |
327 | |
328 | for( i=0; i<3; i++ ) |
329 | { |
330 | normal[i] = 0.0; |
331 | first_vec[i] = Nodes.Value( first+3+i ) - Nodes.Value( first+i ); |
332 | } |
333 | |
334 | gp_XYZ* norm_vec = new gp_XYZ[count-2]; |
335 | for ( i = 0; i < count-2; i++ ) |
336 | norm_vec[i].SetCoord(0, 0, 0); |
337 | |
338 | for( i=2; i<count; i++ ) |
339 | { |
340 | for( j=0; j<3; j++ ) |
341 | cur_vec[j] = Nodes.Value( first+3*i+j ) - Nodes.Value( first+j ); |
342 | |
343 | xx = first_vec[1] * cur_vec[2] - first_vec[2] * cur_vec[1]; |
344 | yy = first_vec[2] * cur_vec[0] - first_vec[0] * cur_vec[2]; |
345 | zz = first_vec[0] * cur_vec[1] - first_vec[1] * cur_vec[0]; |
346 | |
347 | cur_vec[0] = xx; |
348 | cur_vec[1] = yy; |
349 | cur_vec[2] = zz; |
350 | |
351 | if( fabs( cur_vec[0] ) > conf || |
352 | fabs( cur_vec[1] ) > conf || |
353 | fabs( cur_vec[2] ) > conf ) |
354 | { |
355 | Standard_Real cur = Sqrt( cur_vec[0]*cur_vec[0] + cur_vec[1]*cur_vec[1] + cur_vec[2]*cur_vec[2] ); |
356 | for( Standard_Integer k=0; k<3; k++ ) |
357 | cur_vec[k] /= cur; |
358 | } |
359 | |
360 | norm_vec[i-2].SetCoord(cur_vec[0], cur_vec[1], cur_vec[2]); |
361 | |
362 | if( fabs( normal[0] ) <= conf && |
363 | fabs( normal[1] ) <= conf && |
364 | fabs( normal[2] ) <= conf ) |
365 | for( Standard_Integer k=0; k<3; k++ ) |
366 | normal[k] = cur_vec[k]; |
367 | |
368 | if( fabs( normal[0]-cur_vec[0] ) > conf || |
369 | fabs( normal[1]-cur_vec[1] ) > conf || |
370 | fabs( normal[2]-cur_vec[2] ) > conf ) |
371 | { |
372 | res = Standard_False; |
373 | } |
374 | } |
375 | |
376 | if( !res ) { |
377 | for( j=0; j<3; j++ ) { |
378 | normal[j] = 0.0; |
379 | for (i = 0; i < count-2; i++) |
380 | normal[j] += norm_vec[i].Coord(j+1); |
381 | normal[j] /= (count-2); |
382 | } |
383 | } |
384 | delete [] norm_vec; |
385 | |
386 | Norm.SetCoord( normal[0], normal[1], normal[2] ); |
387 | |
388 | return res; |
389 | } |