1 // Created on: 2003-12-17
2 // Created by: Alexander SOLOVYOV
3 // Copyright (c) 2003-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <MeshVS_Tool.ixx>
18 #include <MeshVS_DrawerAttribute.hxx>
19 #include <Precision.hxx>
20 #include <Font_NameOfFont.hxx>
22 //================================================================
23 // Function : CreateAspectFillArea3d
25 //================================================================
26 Handle( Graphic3d_AspectFillArea3d ) MeshVS_Tool::CreateAspectFillArea3d
27 ( const Handle(MeshVS_Drawer)& theDr,
28 const Graphic3d_MaterialAspect& Mat,
29 const Standard_Boolean UseDefaults )
31 Handle( Graphic3d_AspectFillArea3d ) anAsp;
35 Aspect_InteriorStyle anIntStyle = Aspect_IS_EMPTY;
36 Quantity_Color anIntColor = Quantity_NOC_CYAN1,
37 anEdgeColor = Quantity_NOC_WHITE;
38 Aspect_TypeOfLine anEdgeType = Aspect_TOL_SOLID;
39 Standard_Real anEdgeWidth = 1.0;
40 Aspect_HatchStyle aHStyle = Aspect_HS_HORIZONTAL;
41 Graphic3d_MaterialAspect aFrMat = Mat,
44 Standard_Integer anIntStyleI = (Standard_Integer)Aspect_IS_EMPTY;
45 Standard_Integer anEdgeTypeI = (Standard_Integer)Aspect_TOL_SOLID;
46 Standard_Integer aHStyleI = (Standard_Integer)Aspect_HS_HORIZONTAL;
48 if ( !theDr->GetColor ( MeshVS_DA_InteriorColor, anIntColor ) && !UseDefaults )
51 Quantity_Color aBackIntColor = anIntColor;
52 if ( !theDr->GetColor ( MeshVS_DA_BackInteriorColor, aBackIntColor ) && !UseDefaults )
55 if ( !theDr->GetColor ( MeshVS_DA_EdgeColor, anEdgeColor ) && !UseDefaults )
58 if ( !theDr->GetDouble ( MeshVS_DA_EdgeWidth, anEdgeWidth ) && !UseDefaults )
61 if ( !theDr->GetInteger ( MeshVS_DA_InteriorStyle, anIntStyleI ) && !UseDefaults )
64 anIntStyle = (Aspect_InteriorStyle) anIntStyleI;
66 if ( !theDr->GetInteger ( MeshVS_DA_EdgeType, anEdgeTypeI ) && !UseDefaults )
69 anEdgeType = (Aspect_TypeOfLine) anEdgeTypeI;
71 if ( !theDr->GetInteger ( MeshVS_DA_HatchStyle, aHStyleI ) && !UseDefaults )
74 aHStyle = (Aspect_HatchStyle) aHStyleI;
76 anAsp = new Graphic3d_AspectFillArea3d ( anIntStyle, anIntColor, anEdgeColor, anEdgeType,
77 anEdgeWidth, aFrMat, aBackMat );
78 anAsp->SetBackInteriorColor ( aBackIntColor );
79 anAsp->SetHatchStyle ( aHStyle );
84 //================================================================
85 // Function : CreateAspectFillArea3d
87 //================================================================
88 Handle( Graphic3d_AspectFillArea3d ) MeshVS_Tool::CreateAspectFillArea3d
89 ( const Handle(MeshVS_Drawer)& theDr,
90 const Standard_Boolean UseDefaults )
92 Graphic3d_MaterialAspect aFrMat = Graphic3d_NOM_BRASS;
93 Graphic3d_MaterialAspect aBackMat = Graphic3d_NOM_BRASS;
94 Standard_Integer aFrMatI = (Standard_Integer)Graphic3d_NOM_BRASS;
95 Standard_Integer aBackMatI = (Standard_Integer)Graphic3d_NOM_BRASS;
97 if ( !theDr->GetInteger ( MeshVS_DA_FrontMaterial, aFrMatI ) && !UseDefaults )
100 aFrMat = (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)aFrMatI;
102 if ( !theDr->GetInteger ( MeshVS_DA_BackMaterial, aBackMatI ) && !UseDefaults )
105 aBackMat = (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)aBackMatI;
107 Handle( Graphic3d_AspectFillArea3d ) aFill =
108 CreateAspectFillArea3d ( theDr, aFrMat, UseDefaults );
109 aFill->SetBackMaterial ( aBackMat );
113 //================================================================
114 // Function : CreateAspectLine3d
116 //================================================================
117 Handle( Graphic3d_AspectLine3d ) MeshVS_Tool::CreateAspectLine3d
118 ( const Handle(MeshVS_Drawer)& theDr,
119 const Standard_Boolean UseDefaults )
121 Handle( Graphic3d_AspectLine3d ) anAsp;
122 if ( theDr.IsNull() )
125 Quantity_Color aBeamColor = Quantity_NOC_YELLOW;
126 Aspect_TypeOfLine aBeamType = Aspect_TOL_SOLID;
127 Standard_Real aBeamWidth = 1.0;
128 Standard_Integer aBeamTypeI = (Standard_Integer)Aspect_TOL_SOLID;
130 if ( !theDr->GetColor ( MeshVS_DA_BeamColor, aBeamColor ) && !UseDefaults )
133 if ( !theDr->GetDouble ( MeshVS_DA_BeamWidth, aBeamWidth ) && !UseDefaults )
136 if ( !theDr->GetInteger ( MeshVS_DA_BeamType, aBeamTypeI ) && !UseDefaults )
139 aBeamType = (Aspect_TypeOfLine) aBeamTypeI;
141 anAsp = new Graphic3d_AspectLine3d ( aBeamColor, aBeamType, aBeamWidth );
146 //================================================================
147 // Function : CreateAspectMarker3d
149 //================================================================
150 Handle( Graphic3d_AspectMarker3d ) MeshVS_Tool::CreateAspectMarker3d
151 ( const Handle(MeshVS_Drawer)& theDr,
152 const Standard_Boolean UseDefaults )
154 Handle( Graphic3d_AspectMarker3d ) anAsp;
155 if ( theDr.IsNull() )
158 Quantity_Color aMColor = Quantity_NOC_YELLOW;
159 Aspect_TypeOfMarker aMType = Aspect_TOM_X;
160 Standard_Real aMScale = 1.0;
161 Standard_Integer aMTypeI = (Standard_Integer)Aspect_TOM_X;
163 if ( !theDr->GetColor ( MeshVS_DA_MarkerColor, aMColor ) && !UseDefaults )
166 if ( !theDr->GetDouble ( MeshVS_DA_MarkerScale, aMScale ) && !UseDefaults )
169 if ( !theDr->GetInteger ( MeshVS_DA_MarkerType, aMTypeI ) && !UseDefaults )
172 aMType = (Aspect_TypeOfMarker) aMTypeI;
174 anAsp = new Graphic3d_AspectMarker3d ( aMType, aMColor, aMScale );
179 //================================================================
180 // Function : CreateAspectText3d
182 //================================================================
183 Handle( Graphic3d_AspectText3d ) MeshVS_Tool::CreateAspectText3d
184 ( const Handle(MeshVS_Drawer)& theDr,
185 const Standard_Boolean UseDefaults )
187 Handle( Graphic3d_AspectText3d ) anAsp;
188 if ( theDr.IsNull() )
191 Quantity_Color aTColor = Quantity_NOC_YELLOW;
192 Standard_Real anExpFactor = 1.0,
194 Standard_CString aFont = Font_NOF_ASCII_MONO;
195 Aspect_TypeOfStyleText aStyle = Aspect_TOST_NORMAL;
196 Aspect_TypeOfDisplayText aDispText = Aspect_TODT_NORMAL;
197 TCollection_AsciiString aFontString = Font_NOF_ASCII_MONO;
198 Font_FontAspect aFontAspect = Font_FA_Regular;
199 Standard_Integer aStyleI = (Standard_Integer)Aspect_TOST_NORMAL;
200 Standard_Integer aDispTextI = (Standard_Integer)Aspect_TODT_NORMAL;
201 // Bold font is used by default for better text readability
202 Standard_Integer aFontAspectI = (Standard_Integer)Font_FA_Bold;
204 if ( !theDr->GetColor ( MeshVS_DA_TextColor, aTColor ) && !UseDefaults )
207 if ( !theDr->GetDouble ( MeshVS_DA_TextExpansionFactor, anExpFactor ) && !UseDefaults )
210 if ( !theDr->GetDouble ( MeshVS_DA_TextSpace, aSpace ) && !UseDefaults )
213 if ( !theDr->GetAsciiString ( MeshVS_DA_TextFont, aFontString ) && !UseDefaults )
216 aFont = aFontString.ToCString();
218 if ( !theDr->GetInteger ( MeshVS_DA_TextStyle, aStyleI ) && !UseDefaults )
221 aStyle = (Aspect_TypeOfStyleText) aStyleI;
223 if ( !theDr->GetInteger ( MeshVS_DA_TextDisplayType, aDispTextI ) && !UseDefaults )
226 aDispText = (Aspect_TypeOfDisplayText) aDispTextI;
228 if ( !theDr->GetInteger ( MeshVS_DA_TextFontAspect, aFontAspectI ) && !UseDefaults )
231 aFontAspect = (Font_FontAspect) aFontAspectI;
233 anAsp = new Graphic3d_AspectText3d ( aTColor, aFont, anExpFactor, aSpace, aStyle, aDispText );
234 anAsp->SetTextFontAspect( aFontAspect );
238 //================================================================
239 // Function : GetNormal
241 //================================================================
242 Standard_Boolean MeshVS_Tool::GetNormal( const TColStd_Array1OfReal& Nodes,
245 Standard_Integer first = Nodes.Lower(),
246 last = Nodes.Upper(),
247 count = (last-first+1)/3, i, j;
251 count = Standard_Integer( Nodes.Value( 0 ) );
255 return Standard_False;
257 Standard_Boolean res = Standard_True;
260 Standard_Real normal[3], first_vec[3], cur_vec[3], xx, yy, zz,
261 conf = Precision::Confusion();
266 first_vec[i] = Nodes.Value( first+3+i ) - Nodes.Value( first+i );
269 for( i=2; i<count; i++ )
272 cur_vec[j] = Nodes.Value( first+3*i+j ) - Nodes.Value( first+j );
274 xx = first_vec[1] * cur_vec[2] - first_vec[2] * cur_vec[1];
275 yy = first_vec[2] * cur_vec[0] - first_vec[0] * cur_vec[2];
276 zz = first_vec[0] * cur_vec[1] - first_vec[1] * cur_vec[0];
282 if( fabs( cur_vec[0] ) > conf ||
283 fabs( cur_vec[1] ) > conf ||
284 fabs( cur_vec[2] ) > conf )
286 Standard_Real cur = Sqrt( cur_vec[0]*cur_vec[0] + cur_vec[1]*cur_vec[1] + cur_vec[2]*cur_vec[2] );
287 for( Standard_Integer k=0; k<3; k++ )
291 if( fabs( normal[0] ) <= conf &&
292 fabs( normal[1] ) <= conf &&
293 fabs( normal[2] ) <= conf )
294 for( Standard_Integer k=0; k<3; k++ )
295 normal[k] = cur_vec[k];
297 if( fabs( normal[0]-cur_vec[0] ) > conf ||
298 fabs( normal[1]-cur_vec[1] ) > conf ||
299 fabs( normal[2]-cur_vec[2] ) > conf )
301 res = Standard_False;
307 Norm.SetCoord( normal[0], normal[1], normal[2] );
313 //================================================================
314 // Function : GetAverageNormal
316 //================================================================
317 Standard_Boolean MeshVS_Tool::GetAverageNormal( const TColStd_Array1OfReal& Nodes,
320 Standard_Integer first = Nodes.Lower(),
321 last = Nodes.Upper(),
322 count = (last-first+1)/3, i, j;
326 count = Standard_Integer( Nodes.Value( 0 ) );
330 return Standard_False;
332 Standard_Boolean res = Standard_True;
335 Standard_Real normal[3], first_vec[3], cur_vec[3], xx, yy, zz,
336 conf = Precision::Confusion();
341 first_vec[i] = Nodes.Value( first+3+i ) - Nodes.Value( first+i );
344 gp_XYZ* norm_vec = new gp_XYZ[count-2];
345 for ( i = 0; i < count-2; i++ )
346 norm_vec[i].SetCoord(0, 0, 0);
348 for( i=2; i<count; i++ )
351 cur_vec[j] = Nodes.Value( first+3*i+j ) - Nodes.Value( first+j );
353 xx = first_vec[1] * cur_vec[2] - first_vec[2] * cur_vec[1];
354 yy = first_vec[2] * cur_vec[0] - first_vec[0] * cur_vec[2];
355 zz = first_vec[0] * cur_vec[1] - first_vec[1] * cur_vec[0];
361 if( fabs( cur_vec[0] ) > conf ||
362 fabs( cur_vec[1] ) > conf ||
363 fabs( cur_vec[2] ) > conf )
365 Standard_Real cur = Sqrt( cur_vec[0]*cur_vec[0] + cur_vec[1]*cur_vec[1] + cur_vec[2]*cur_vec[2] );
366 for( Standard_Integer k=0; k<3; k++ )
370 norm_vec[i-2].SetCoord(cur_vec[0], cur_vec[1], cur_vec[2]);
372 if( fabs( normal[0] ) <= conf &&
373 fabs( normal[1] ) <= conf &&
374 fabs( normal[2] ) <= conf )
375 for( Standard_Integer k=0; k<3; k++ )
376 normal[k] = cur_vec[k];
378 if( fabs( normal[0]-cur_vec[0] ) > conf ||
379 fabs( normal[1]-cur_vec[1] ) > conf ||
380 fabs( normal[2]-cur_vec[2] ) > conf )
382 res = Standard_False;
387 for( j=0; j<3; j++ ) {
389 for (i = 0; i < count-2; i++)
390 normal[j] += norm_vec[i].Coord(j+1);
391 normal[j] /= (count-2);
396 Norm.SetCoord( normal[0], normal[1], normal[2] );