1 // Created on: 2003-12-17
2 // Created by: Alexander SOLOVYOV
3 // Copyright (c) 2003-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <MeshVS_Tool.ixx>
23 #include <MeshVS_DrawerAttribute.hxx>
24 #include <Precision.hxx>
25 #include <Graphic3d_NameOfFont.hxx>
27 //================================================================
28 // Function : CreateAspectFillArea3d
30 //================================================================
31 Handle( Graphic3d_AspectFillArea3d ) MeshVS_Tool::CreateAspectFillArea3d
32 ( const Handle(MeshVS_Drawer)& theDr,
33 const Graphic3d_MaterialAspect& Mat,
34 const Standard_Boolean UseDefaults )
36 Handle( Graphic3d_AspectFillArea3d ) anAsp;
40 Aspect_InteriorStyle anIntStyle = Aspect_IS_EMPTY;
41 Quantity_Color anIntColor = Quantity_NOC_CYAN1,
42 anEdgeColor = Quantity_NOC_WHITE;
43 Aspect_TypeOfLine anEdgeType = Aspect_TOL_SOLID;
44 Standard_Real anEdgeWidth = 1.0;
45 Aspect_HatchStyle aHStyle = Aspect_HS_HORIZONTAL;
46 Graphic3d_MaterialAspect aFrMat = Mat,
49 Standard_Integer anIntStyleI = (Standard_Integer)Aspect_IS_EMPTY;
50 Standard_Integer anEdgeTypeI = (Standard_Integer)Aspect_TOL_SOLID;
51 Standard_Integer aHStyleI = (Standard_Integer)Aspect_HS_HORIZONTAL;
53 if ( !theDr->GetColor ( MeshVS_DA_InteriorColor, anIntColor ) && !UseDefaults )
56 Quantity_Color aBackIntColor = anIntColor;
57 if ( !theDr->GetColor ( MeshVS_DA_BackInteriorColor, aBackIntColor ) && !UseDefaults )
60 if ( !theDr->GetColor ( MeshVS_DA_EdgeColor, anEdgeColor ) && !UseDefaults )
63 if ( !theDr->GetDouble ( MeshVS_DA_EdgeWidth, anEdgeWidth ) && !UseDefaults )
66 if ( !theDr->GetInteger ( MeshVS_DA_InteriorStyle, anIntStyleI ) && !UseDefaults )
69 anIntStyle = (Aspect_InteriorStyle) anIntStyleI;
71 if ( !theDr->GetInteger ( MeshVS_DA_EdgeType, anEdgeTypeI ) && !UseDefaults )
74 anEdgeType = (Aspect_TypeOfLine) anEdgeTypeI;
76 if ( !theDr->GetInteger ( MeshVS_DA_HatchStyle, aHStyleI ) && !UseDefaults )
79 aHStyle = (Aspect_HatchStyle) aHStyleI;
81 anAsp = new Graphic3d_AspectFillArea3d ( anIntStyle, anIntColor, anEdgeColor, anEdgeType,
82 anEdgeWidth, aFrMat, aBackMat );
83 anAsp->SetBackInteriorColor ( aBackIntColor );
84 anAsp->SetHatchStyle ( aHStyle );
89 //================================================================
90 // Function : CreateAspectFillArea3d
92 //================================================================
93 Handle( Graphic3d_AspectFillArea3d ) MeshVS_Tool::CreateAspectFillArea3d
94 ( const Handle(MeshVS_Drawer)& theDr,
95 const Standard_Boolean UseDefaults )
97 Graphic3d_MaterialAspect aFrMat = Graphic3d_NOM_BRASS;
98 Graphic3d_MaterialAspect aBackMat = Graphic3d_NOM_BRASS;
99 Standard_Integer aFrMatI = (Standard_Integer)Graphic3d_NOM_BRASS;
100 Standard_Integer aBackMatI = (Standard_Integer)Graphic3d_NOM_BRASS;
102 if ( !theDr->GetInteger ( MeshVS_DA_FrontMaterial, aFrMatI ) && !UseDefaults )
105 aFrMat = (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)aFrMatI;
107 if ( !theDr->GetInteger ( MeshVS_DA_BackMaterial, aBackMatI ) && !UseDefaults )
110 aBackMat = (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)aBackMatI;
112 Handle( Graphic3d_AspectFillArea3d ) aFill =
113 CreateAspectFillArea3d ( theDr, aFrMat, UseDefaults );
114 aFill->SetBackMaterial ( aBackMat );
118 //================================================================
119 // Function : CreateAspectLine3d
121 //================================================================
122 Handle( Graphic3d_AspectLine3d ) MeshVS_Tool::CreateAspectLine3d
123 ( const Handle(MeshVS_Drawer)& theDr,
124 const Standard_Boolean UseDefaults )
126 Handle( Graphic3d_AspectLine3d ) anAsp;
127 if ( theDr.IsNull() )
130 Quantity_Color aBeamColor = Quantity_NOC_YELLOW;
131 Aspect_TypeOfLine aBeamType = Aspect_TOL_SOLID;
132 Standard_Real aBeamWidth = 1.0;
133 Standard_Integer aBeamTypeI = (Standard_Integer)Aspect_TOL_SOLID;
135 if ( !theDr->GetColor ( MeshVS_DA_BeamColor, aBeamColor ) && !UseDefaults )
138 if ( !theDr->GetDouble ( MeshVS_DA_BeamWidth, aBeamWidth ) && !UseDefaults )
141 if ( !theDr->GetInteger ( MeshVS_DA_BeamType, aBeamTypeI ) && !UseDefaults )
144 aBeamType = (Aspect_TypeOfLine) aBeamTypeI;
146 anAsp = new Graphic3d_AspectLine3d ( aBeamColor, aBeamType, aBeamWidth );
151 //================================================================
152 // Function : CreateAspectMarker3d
154 //================================================================
155 Handle( Graphic3d_AspectMarker3d ) MeshVS_Tool::CreateAspectMarker3d
156 ( const Handle(MeshVS_Drawer)& theDr,
157 const Standard_Boolean UseDefaults )
159 Handle( Graphic3d_AspectMarker3d ) anAsp;
160 if ( theDr.IsNull() )
163 Quantity_Color aMColor = Quantity_NOC_YELLOW;
164 Aspect_TypeOfMarker aMType = Aspect_TOM_X;
165 Standard_Real aMScale = 1.0;
166 Standard_Integer aMTypeI = (Standard_Integer)Aspect_TOM_X;
168 if ( !theDr->GetColor ( MeshVS_DA_MarkerColor, aMColor ) && !UseDefaults )
171 if ( !theDr->GetDouble ( MeshVS_DA_MarkerScale, aMScale ) && !UseDefaults )
174 if ( !theDr->GetInteger ( MeshVS_DA_MarkerType, aMTypeI ) && !UseDefaults )
177 aMType = (Aspect_TypeOfMarker) aMTypeI;
179 anAsp = new Graphic3d_AspectMarker3d ( aMType, aMColor, aMScale );
184 //================================================================
185 // Function : CreateAspectText3d
187 //================================================================
188 Handle( Graphic3d_AspectText3d ) MeshVS_Tool::CreateAspectText3d
189 ( const Handle(MeshVS_Drawer)& theDr,
190 const Standard_Boolean UseDefaults )
192 Handle( Graphic3d_AspectText3d ) anAsp;
193 if ( theDr.IsNull() )
196 Quantity_Color aTColor = Quantity_NOC_YELLOW;
197 Standard_Real anExpFactor = 1.0,
199 Standard_CString aFont = Graphic3d_NOF_ASCII_MONO;
200 Aspect_TypeOfStyleText aStyle = Aspect_TOST_NORMAL;
201 Aspect_TypeOfDisplayText aDispText = Aspect_TODT_NORMAL;
202 TCollection_AsciiString aFontString = Graphic3d_NOF_ASCII_MONO;
203 Font_FontAspect aFontAspect = Font_FA_Regular;
204 Standard_Integer aStyleI = (Standard_Integer)Aspect_TOST_NORMAL;
205 Standard_Integer aDispTextI = (Standard_Integer)Aspect_TODT_NORMAL;
206 // Bold font is used by default for better text readability
207 Standard_Integer aFontAspectI = (Standard_Integer)Font_FA_Bold;
209 if ( !theDr->GetColor ( MeshVS_DA_TextColor, aTColor ) && !UseDefaults )
212 if ( !theDr->GetDouble ( MeshVS_DA_TextExpansionFactor, anExpFactor ) && !UseDefaults )
215 if ( !theDr->GetDouble ( MeshVS_DA_TextSpace, aSpace ) && !UseDefaults )
218 if ( !theDr->GetAsciiString ( MeshVS_DA_TextFont, aFontString ) && !UseDefaults )
221 aFont = aFontString.ToCString();
223 if ( !theDr->GetInteger ( MeshVS_DA_TextStyle, aStyleI ) && !UseDefaults )
226 aStyle = (Aspect_TypeOfStyleText) aStyleI;
228 if ( !theDr->GetInteger ( MeshVS_DA_TextDisplayType, aDispTextI ) && !UseDefaults )
231 aDispText = (Aspect_TypeOfDisplayText) aDispTextI;
233 if ( !theDr->GetInteger ( MeshVS_DA_TextFontAspect, aFontAspectI ) && !UseDefaults )
236 aFontAspect = (Font_FontAspect) aFontAspectI;
238 anAsp = new Graphic3d_AspectText3d ( aTColor, aFont, anExpFactor, aSpace, aStyle, aDispText );
239 anAsp->SetTextFontAspect( aFontAspect );
243 //================================================================
244 // Function : GetNormal
246 //================================================================
247 Standard_Boolean MeshVS_Tool::GetNormal( const TColStd_Array1OfReal& Nodes,
250 Standard_Integer first = Nodes.Lower(),
251 last = Nodes.Upper(),
252 count = (last-first+1)/3, i, j;
256 count = Standard_Integer( Nodes.Value( 0 ) );
260 return Standard_False;
262 Standard_Boolean res = Standard_True;
265 Standard_Real normal[3], first_vec[3], cur_vec[3], xx, yy, zz,
266 conf = Precision::Confusion();
271 first_vec[i] = Nodes.Value( first+3+i ) - Nodes.Value( first+i );
274 for( i=2; i<count; i++ )
277 cur_vec[j] = Nodes.Value( first+3*i+j ) - Nodes.Value( first+j );
279 xx = first_vec[1] * cur_vec[2] - first_vec[2] * cur_vec[1];
280 yy = first_vec[2] * cur_vec[0] - first_vec[0] * cur_vec[2];
281 zz = first_vec[0] * cur_vec[1] - first_vec[1] * cur_vec[0];
287 if( fabs( cur_vec[0] ) > conf ||
288 fabs( cur_vec[1] ) > conf ||
289 fabs( cur_vec[2] ) > conf )
291 Standard_Real cur = Sqrt( cur_vec[0]*cur_vec[0] + cur_vec[1]*cur_vec[1] + cur_vec[2]*cur_vec[2] );
292 for( Standard_Integer k=0; k<3; k++ )
296 if( fabs( normal[0] ) <= conf &&
297 fabs( normal[1] ) <= conf &&
298 fabs( normal[2] ) <= conf )
299 for( Standard_Integer k=0; k<3; k++ )
300 normal[k] = cur_vec[k];
302 if( fabs( normal[0]-cur_vec[0] ) > conf ||
303 fabs( normal[1]-cur_vec[1] ) > conf ||
304 fabs( normal[2]-cur_vec[2] ) > conf )
306 res = Standard_False;
312 Norm.SetCoord( normal[0], normal[1], normal[2] );
318 //================================================================
319 // Function : GetAverageNormal
321 //================================================================
322 Standard_Boolean MeshVS_Tool::GetAverageNormal( const TColStd_Array1OfReal& Nodes,
325 Standard_Integer first = Nodes.Lower(),
326 last = Nodes.Upper(),
327 count = (last-first+1)/3, i, j;
331 count = Standard_Integer( Nodes.Value( 0 ) );
335 return Standard_False;
337 Standard_Boolean res = Standard_True;
340 Standard_Real normal[3], first_vec[3], cur_vec[3], xx, yy, zz,
341 conf = Precision::Confusion();
346 first_vec[i] = Nodes.Value( first+3+i ) - Nodes.Value( first+i );
349 gp_XYZ* norm_vec = new gp_XYZ[count-2];
350 for ( i = 0; i < count-2; i++ )
351 norm_vec[i].SetCoord(0, 0, 0);
353 for( i=2; i<count; i++ )
356 cur_vec[j] = Nodes.Value( first+3*i+j ) - Nodes.Value( first+j );
358 xx = first_vec[1] * cur_vec[2] - first_vec[2] * cur_vec[1];
359 yy = first_vec[2] * cur_vec[0] - first_vec[0] * cur_vec[2];
360 zz = first_vec[0] * cur_vec[1] - first_vec[1] * cur_vec[0];
366 if( fabs( cur_vec[0] ) > conf ||
367 fabs( cur_vec[1] ) > conf ||
368 fabs( cur_vec[2] ) > conf )
370 Standard_Real cur = Sqrt( cur_vec[0]*cur_vec[0] + cur_vec[1]*cur_vec[1] + cur_vec[2]*cur_vec[2] );
371 for( Standard_Integer k=0; k<3; k++ )
375 norm_vec[i-2].SetCoord(cur_vec[0], cur_vec[1], cur_vec[2]);
377 if( fabs( normal[0] ) <= conf &&
378 fabs( normal[1] ) <= conf &&
379 fabs( normal[2] ) <= conf )
380 for( Standard_Integer k=0; k<3; k++ )
381 normal[k] = cur_vec[k];
383 if( fabs( normal[0]-cur_vec[0] ) > conf ||
384 fabs( normal[1]-cur_vec[1] ) > conf ||
385 fabs( normal[2]-cur_vec[2] ) > conf )
387 res = Standard_False;
392 for( j=0; j<3; j++ ) {
394 for (i = 0; i < count-2; i++)
395 normal[j] += norm_vec[i].Coord(j+1);
396 normal[j] /= (count-2);
401 Norm.SetCoord( normal[0], normal[1], normal[2] );