Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

exotkMaterial.cxx

Go to the documentation of this file.
00001 
00002 //   exotkMaterial.cxx
00004 //    Copyright (C) 2001  Stephane Routelous
00005 //
00006 //    This file is part of exoTK.
00007 //
00008 //    exoTK is free software; you can redistribute it and/or modify
00009 //    it under the terms of the GNU General Public License as published by
00010 //    the Free Software Foundation; either version 2 of the License, or
00011 //    (at your option) any later version.
00012 //
00013 //    exoTK is distributed in the hope that it will be useful,
00014 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 //    GNU General Public License for more details.
00017 //
00018 //    You should have received a copy of the GNU General Public License
00019 //    along with exoTK; if not, write to the Free Software
00020 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00029 #include <exotkMaterial.hxx>
00030 
00031 #ifndef  _Graphic3d_MaterialAspect_HeaderFile
00032 #include <Graphic3d_MaterialAspect.hxx>
00033 #endif
00034 #ifndef  _exotkMaterial_MapOfMaterial_HeaderFile
00035 #include <exotkMaterial_MapOfMaterial.hxx>
00036 #endif
00037 #ifndef  _TCollection_AsciiString_HeaderFile
00038 #include <TCollection_AsciiString.hxx>
00039 #endif
00040 #ifndef  _exotkMaterial_Material_HeaderFile
00041 #include <exotkMaterial_Material.hxx>
00042 #endif
00043 #ifndef  _exotkMaterial_MapIteratorOfMapOfMaterial_HeaderFile
00044 #include <exotkMaterial_MapIteratorOfMapOfMaterial.hxx>
00045 #endif
00046 
00052 namespace exotkMaterial_Datas
00053 {
00055         exotkMaterial_MapOfMaterial theMap;
00057         exotkMaterial_MapIteratorOfMapOfMaterial theIterator;
00059         Standard_Boolean theInitIsDone = Standard_False;
00060 }
00061 
00062 
00063 
00071 Standard_Boolean IsSameMaterial(const Graphic3d_MaterialAspect& mat1,const Graphic3d_MaterialAspect& mat2)
00072 {
00073         Quantity_Parameter theOldColorEpsilon = Quantity_Color::Epsilon();
00074         Quantity_Color::SetEpsilon(0.03);
00075 
00076         Standard_Boolean theIsEqual =  (Abs(mat1.Ambient()- mat2.Ambient())<=0.03 &&
00077                 Abs(mat1.Diffuse()- mat2.Diffuse())<=0.03 &&
00078                 Abs(mat1.Specular()- mat2.Specular())<=0.03 &&
00079                 Abs(mat1.Emissive()- mat2.Emissive())<=0.03 &&
00080                 Abs(mat1.Transparency()- mat2.Transparency())<=0.03 &&
00081                 Abs(mat1.Shininess ()- mat2.Shininess())<=0.03 &&
00082                 Abs(mat1.EnvReflexion()- mat2.EnvReflexion())<=0.03 &&
00083                 mat1.AmbientColor() == mat2.AmbientColor() &&
00084                 mat1.DiffuseColor() == mat2.DiffuseColor() &&
00085                 mat1.SpecularColor() == mat2.SpecularColor() &&
00086                 mat1.EmissiveColor() == mat2.EmissiveColor() &&
00087                 mat1.ReflectionMode(Graphic3d_TOR_AMBIENT) == mat2.ReflectionMode(Graphic3d_TOR_AMBIENT) &&
00088                 mat1.ReflectionMode(Graphic3d_TOR_DIFFUSE) == mat2.ReflectionMode(Graphic3d_TOR_DIFFUSE) &&
00089                 mat1.ReflectionMode(Graphic3d_TOR_SPECULAR) == mat2.ReflectionMode(Graphic3d_TOR_SPECULAR) &&
00090                 mat1.ReflectionMode(Graphic3d_TOR_EMISSION) == mat2.ReflectionMode(Graphic3d_TOR_EMISSION));
00091 
00092         Quantity_Color::SetEpsilon(theOldColorEpsilon);
00093         return theIsEqual;
00094 }
00095 
00096 
00097 //==================================================================================
00098 // Function name        : exotkMaterial::InitStandardMaterials
00099 //==================================================================================
00100 // Written by       : Stephane Routelous - 2001-12-29 00:45:21
00101 // Description      : 
00102 // Return type          : void 
00103 //==================================================================================
00104 void exotkMaterial::InitStandardMaterials()
00105 {
00106         if (!exotkMaterial_Datas::theInitIsDone)
00107         {
00108                 exotkMaterial_Datas::theInitIsDone = Standard_True;
00109                 for (Standard_Integer iter = 0; iter <(Standard_Integer)Graphic3d_NOM_DEFAULT; iter++)
00110                 {
00111                         try
00112                         {
00113                                 Graphic3d_MaterialAspect theMat((Graphic3d_NameOfMaterial)iter);
00114                                 TCollection_AsciiString theName = Graphic3d_MaterialAspect::MaterialName(iter + 1);
00115                                 theName.Capitalize();
00116                                 Handle_exotkMaterial_Material theMaterial = new exotkMaterial_Material(theMat,theName);
00117                                 theMaterial->SetModifiable(Standard_False);
00118                                 exotkMaterial_Datas::theMap.Add(theMaterial);
00119                         }
00120                         catch (Standard_Failure)
00121                         {
00122                         }
00123                         catch (...)
00124                         {
00125                         }
00126                 }
00127         }
00128 }
00129 
00130 //==================================================================================
00131 // Function name        : exotkMaterial::CurrentMaterial
00132 //==================================================================================
00133 // Written by       : Stephane Routelous - 2001-12-29 00:45:24
00134 // Description      : 
00135 // Return type          : Graphic3d_MaterialAspect 
00136 //==================================================================================
00137 Standard_Boolean exotkMaterial::CurrentMaterial(Graphic3d_MaterialAspect& aMaterial)
00138 {
00139         Standard_Boolean success = Standard_False;
00140         Handle_exotkMaterial_Material theMaterial = exotkMaterial_Datas::theIterator.Key();
00141         if ( !theMaterial.IsNull() )
00142         {
00143                 aMaterial = theMaterial->Material();
00144                 success =  Standard_True;
00145         }
00146         return success;
00147 }
00148 
00149 //==================================================================================
00150 // Function name        : exotkMaterial::CurrentName
00151 //==================================================================================
00152 // Written by       : Stephane Routelous - 2001-12-29 00:45:26
00153 // Description      : 
00154 // Return type          : TCollection_AsciiString 
00155 //==================================================================================
00156 Standard_Boolean exotkMaterial::CurrentName(TCollection_AsciiString& aName)
00157 {
00158         Standard_Boolean success = Standard_False;
00159         Handle_exotkMaterial_Material theMaterial = exotkMaterial_Datas::theIterator.Key();
00160         if ( !theMaterial.IsNull() )
00161         {
00162                 aName = theMaterial->Name();
00163                 success =  Standard_True;
00164         }
00165         return success;
00166 }
00167 
00168 //==================================================================================
00169 // Function name        : exotkMaterial::NextMaterialIterator
00170 //==================================================================================
00171 // Written by       : Stephane Routelous - 2001-12-29 00:45:28
00172 // Description      : 
00173 // Return type          : void 
00174 //==================================================================================
00175 void exotkMaterial::NextMaterialIterator()
00176 {
00177         exotkMaterial_Datas::theIterator.Next();
00178 }
00179 
00180 //==================================================================================
00181 // Function name        : exotkMaterial::MoreMaterialIterator
00182 //==================================================================================
00183 // Written by       : Stephane Routelous - 2001-12-29 00:45:29
00184 // Description      : 
00185 // Return type          : Standard_Boolean 
00186 //==================================================================================
00187 Standard_Boolean exotkMaterial::MoreMaterialIterator()
00188 {
00189         return exotkMaterial_Datas::theIterator.More();
00190 }
00191 
00192 //==================================================================================
00193 // Function name        : exotkMaterial::InitMaterialIterator
00194 //==================================================================================
00195 // Written by       : Stephane Routelous - 2001-12-29 00:45:31
00196 // Description      : 
00197 // Return type          : void 
00198 //==================================================================================
00199 void exotkMaterial::InitMaterialIterator()
00200 {
00201         InitStandardMaterials();
00202         exotkMaterial_Datas::theIterator.Initialize(exotkMaterial_Datas::theMap);
00203 }
00204 
00205 
00206 //==================================================================================
00207 // Function name        : exotkMaterial::IsModifiable
00208 //==================================================================================
00209 // Written by       : Stephane Routelous - 2001-12-29 00:45:33
00210 // Description      : 
00211 // Return type          : Standard_Boolean 
00212 //==================================================================================
00213 // Argument         : const TCollection_AsciiString &aName
00214 // Argument         : Standard_Boolean& aIsModifiable
00215 Standard_Boolean exotkMaterial::IsModifiable(const TCollection_AsciiString &aName,Standard_Boolean& aIsModifiable)
00216 {
00217         InitStandardMaterials();
00218         Standard_Boolean theMaterialIsFound = Standard_False;
00219         Handle_exotkMaterial_Material theMaterial = FindMaterial(aName);
00220         if ( !theMaterial.IsNull())
00221         {
00222                 theMaterialIsFound = Standard_True;
00223                 aIsModifiable = theMaterial->IsModifiable();
00224         }
00225         return theMaterialIsFound;
00226 }
00227 
00228 //==================================================================================
00229 // Function name        : exotkMaterial::IsModifiable
00230 //==================================================================================
00231 // Written by       : Stephane Routelous - 2001-12-29 00:45:35
00232 // Description      : 
00233 // Return type          : Standard_Boolean 
00234 //==================================================================================
00235 // Argument         : const Graphic3d_MaterialAspect &aMaterial
00236 // Argument         : Standard_Boolean& aIsModifiable
00237 Standard_Boolean exotkMaterial::IsModifiable(const Graphic3d_MaterialAspect &aMaterial,Standard_Boolean& aIsModifiable)
00238 {
00239         InitStandardMaterials();
00240         Standard_Boolean theMaterialIsFound = Standard_False;
00241         Handle_exotkMaterial_Material theMaterial = FindMaterial(aMaterial);
00242         if ( !theMaterial.IsNull())
00243         {
00244                 theMaterialIsFound = Standard_True;
00245                 aIsModifiable = theMaterial->IsModifiable();
00246         }
00247         return theMaterialIsFound;
00248 }
00249 
00250 //==================================================================================
00251 // Function name        : exotkMaterial::FindMaterial
00252 //==================================================================================
00253 // Written by       : Stephane Routelous - 2001-12-29 00:45:37
00254 // Description      : 
00255 // Return type          : Handle_exotkMaterial_Material 
00256 //==================================================================================
00257 // Argument         : const Graphic3d_MaterialAspect& aMaterial
00258 Handle_exotkMaterial_Material exotkMaterial::FindMaterial(const Graphic3d_MaterialAspect& aMaterial)
00259 {
00260         InitStandardMaterials();
00261         exotkMaterial_MapIteratorOfMapOfMaterial theMapIterator;
00262         Standard_Boolean theIsFound = Standard_False;
00263         Handle_exotkMaterial_Material theFoundMaterial;
00264         for ( theMapIterator.Initialize(exotkMaterial_Datas::theMap) ; !theIsFound && theMapIterator.More() ; theMapIterator.Next() )
00265         {
00266                 Handle_exotkMaterial_Material theMaterial = theMapIterator.Key();
00267                 theIsFound = IsSameMaterial(theMaterial->Material(),aMaterial);
00268                 if ( theIsFound )
00269                         theFoundMaterial = theMaterial;
00270         }
00271         return theFoundMaterial;
00272 }
00273 
00274 //==================================================================================
00275 // Function name        : exotkMaterial::FindMaterial
00276 //==================================================================================
00277 // Written by       : Stephane Routelous - 2001-12-29 00:45:39
00278 // Description      : 
00279 // Return type          : Handle_exotkMaterial_Material 
00280 //==================================================================================
00281 // Argument         : const TCollection_AsciiString& aName
00282 Handle_exotkMaterial_Material exotkMaterial::FindMaterial(const TCollection_AsciiString& aName)
00283 {
00284         InitStandardMaterials();
00285         exotkMaterial_MapIteratorOfMapOfMaterial theMapIterator;
00286         Standard_Boolean theIsFound = Standard_False;
00287         Handle_exotkMaterial_Material theFoundMaterial;
00288         for ( theMapIterator.Initialize(exotkMaterial_Datas::theMap) ; !theIsFound && theMapIterator.More() ; theMapIterator.Next() )
00289         {
00290                 Handle_exotkMaterial_Material theMaterial = theMapIterator.Key();
00291                 theIsFound = (theMaterial->Name()==aName);
00292                 if ( theIsFound )
00293                         theFoundMaterial = theMaterial;
00294         }
00295         return theFoundMaterial;
00296 }
00297 
00298 //==================================================================================
00299 // Function name        : exotkMaterial::FindName
00300 //==================================================================================
00301 // Written by       : Stephane Routelous - 2001-12-29 00:45:41
00302 // Description      : 
00303 // Return type          : Standard_Boolean 
00304 //==================================================================================
00305 // Argument         : const Graphic3d_MaterialAspect& aMaterial
00306 // Argument         : TCollection_AsciiString& aFoundName
00307 Standard_Boolean exotkMaterial::FindName(const Graphic3d_MaterialAspect& aMaterial,TCollection_AsciiString& aFoundName)
00308 {
00309         Standard_Boolean theSuccess = Standard_False;
00310         InitStandardMaterials();
00311         Handle_exotkMaterial_Material theInternMaterial = FindMaterial(aMaterial);
00312         if ( !theInternMaterial.IsNull() )
00313         {
00314                 aFoundName = theInternMaterial->Name();
00315                 theSuccess = Standard_True;
00316         }
00317         return theSuccess;
00318 }
00319 
00320 //==================================================================================
00321 // Function name        : exotkMaterial::FindMaterial
00322 //==================================================================================
00323 // Written by       : Stephane Routelous - 2001-12-29 00:45:43
00324 // Description      : 
00325 // Return type          : Standard_Boolean 
00326 //==================================================================================
00327 // Argument         : const TCollection_AsciiString& aName
00328 // Argument         : Graphic3d_MaterialAspect& aFoundMaterial
00329 Standard_Boolean exotkMaterial::FindMaterial(const TCollection_AsciiString& aName,Graphic3d_MaterialAspect& aFoundMaterial)
00330 {
00331         Standard_Boolean theSuccess = Standard_False;
00332         InitStandardMaterials();
00333         Handle_exotkMaterial_Material theInternMaterial = FindMaterial(aName);
00334         if ( !theInternMaterial.IsNull() )
00335         {
00336                 aFoundMaterial = theInternMaterial->Material();
00337                 theSuccess = Standard_True;
00338         }
00339         return theSuccess;
00340 }
00341 
00342 
00343 //==================================================================================
00344 // Function name        : exotkMaterial::NameExists
00345 //==================================================================================
00346 // Written by       : Stephane Routelous - 2001-12-29 00:45:45
00347 // Description      : 
00348 // Return type          : Standard_Boolean 
00349 //==================================================================================
00350 // Argument         : const TCollection_AsciiString &aName
00351 Standard_Boolean exotkMaterial::NameExists(const TCollection_AsciiString &aName)
00352 {
00353         return (!FindMaterial(aName).IsNull());
00354 }
00355 
00356 //==================================================================================
00357 // Function name        : exotkMaterial::ReplaceMaterial
00358 //==================================================================================
00359 // Written by       : Stephane Routelous - 2001-12-29 00:45:47
00360 // Description      : 
00361 // Return type          : Standard_Boolean 
00362 //==================================================================================
00363 // Argument         : const TCollection_AsciiString& aName
00364 // Argument         : const Graphic3d_MaterialAspect& aNewMaterial
00365 Standard_Boolean exotkMaterial::ReplaceMaterial(const TCollection_AsciiString& aName,const Graphic3d_MaterialAspect& aNewMaterial)
00366 {
00367         InitStandardMaterials();
00368         Standard_Boolean theSuccess = Standard_False;
00369         Handle_exotkMaterial_Material theInternMaterial = FindMaterial(aName);
00370         if ( !theInternMaterial.IsNull() )
00371         {
00372                 if ( theInternMaterial->IsModifiable() )
00373                 {
00374                         theInternMaterial->SetMaterial(aNewMaterial);
00375                         theSuccess = Standard_True;
00376                 }
00377         }
00378         return theSuccess;
00379 }
00380 
00381 
00382 //==================================================================================
00383 // Function name        : exotkMaterial::AddMaterial
00384 //==================================================================================
00385 // Written by       : Stephane Routelous - 2001-12-29 00:45:49
00386 // Description      : 
00387 // Return type          : Standard_Boolean 
00388 //==================================================================================
00389 // Argument         : const TCollection_AsciiString &aName
00390 // Argument         : const Graphic3d_MaterialAspect &aMaterial
00391 // Argument         : const Standard_Boolean aIsModifiable
00392 Standard_Boolean exotkMaterial::AddMaterial(const TCollection_AsciiString &aName, const Graphic3d_MaterialAspect &aMaterial,const Standard_Boolean aIsModifiable)
00393 {
00394         InitStandardMaterials();
00395         Handle_exotkMaterial_Material theInternMaterial = new exotkMaterial_Material(aMaterial,aName);
00396         theInternMaterial->SetModifiable(aIsModifiable);
00397         return exotkMaterial_Datas::theMap.Add(theInternMaterial);
00398 }

Generated on Wed Jan 23 12:16:42 2002 for exotk by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001