Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGGeoTypeGraphOp.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #include <OSGBaseTypes.h>
00040 #include <OSGGeoTypeGraphOp.h>
00041 #include <OSGLog.h>
00042 
00043 OSG_USING_NAMESPACE
00044 
00045     
00046 GeoTypeGraphOp::GeoTypeGraphOp(const char* name)
00047     : SingleTypeGraphOp<Geometry>(name),
00048     _filter(TypeTraits<OSG::BitVector>::getMax())
00049 {
00050 }
00051 
00052 GraphOp* GeoTypeGraphOp::create()
00053 {
00054     return new GeoTypeGraphOp();
00055 }
00056 
00057 
00058 bool GeoTypeGraphOp::travNodeEnter(NodePtr node)
00059 {
00060     GeometryPtr geo = GeometryPtr::dcast(node->getCore());
00061 
00062     if(geo == NullFC)
00063     {
00064         return true;
00065     }
00066 
00067     GeoPositionsPtr positions = geo->getPositions();
00068 
00069     // normals
00070     if(_filter & Geometry::NormalsFieldMask)
00071     {
00072         GeoNormalsPtr   normals   = geo->getNormals();
00073         GeoNormals3fPtr normals3f = GeoNormals3fPtr::dcast(normals);
00074         if (normals3f != NullFC)
00075         {
00076             MFVec3f &src = normals3f->getField();
00077             
00078             GeoNormals3bPtr normals3b = GeoNormals3b::create();
00079             MFVec3b &dst = normals3b->getField();
00080             dst.reserve(src.size());
00081             beginEditCP(normals3b);
00082                 for (UInt32 i = 0; i < src.size(); ++i)
00083                 {
00084                     Vec3f vec = src[i];
00085                     vec *= (0.9f / vec.length());
00086                     normals3b->push_back(vec);
00087                 }
00088             endEditCP(normals3b);
00089     
00090             beginEditCP(geo, Geometry::NormalsFieldMask);
00091                 geo->setNormals(normals3b);
00092             endEditCP(geo, Geometry::NormalsFieldMask);
00093         }
00094     }
00095 
00096     GeoColorsPtr    colors    = geo->getColors();
00097     GeoColorsPtr    scolors   = geo->getSecondaryColors();
00098 
00099     if(_filter & Geometry::LengthsFieldMask)
00100     {
00101         // lengths
00102         GeoPLengthsUI32Ptr lengthsUI32 = GeoPLengthsUI32Ptr::dcast(geo->getLengths());
00103         if(lengthsUI32 != NullFC)
00104         {
00105             MFUInt32 &src = lengthsUI32->getField();
00106     
00107             // now check if maximum length is greater than 65535
00108             UInt32 max_length = UInt32(TypeTraits<UInt16>::getMax());
00109             bool max_length_ok = true;
00110             for(UInt32 i=0;i<src.size();++i)
00111             {
00112                 if(src[i] > max_length)
00113                 {
00114                     max_length_ok = false;
00115                     break;
00116                 }
00117             }
00118     
00119             if(max_length_ok)
00120             {
00121                 GeoPLengthsUI16Ptr lengthsUI16 = GeoPLengthsUI16::create();
00122                 MFUInt16 &dst = lengthsUI16->getField();
00123                 dst.reserve(src.size());
00124                 beginEditCP(lengthsUI16);
00125                     for (UInt32 i = 0; i < src.size(); ++i)
00126                         dst.push_back(UInt16(src[i]));
00127                 endEditCP(lengthsUI16);
00128         
00129                 beginEditCP(geo, Geometry::LengthsFieldMask);
00130                     geo->setLengths(lengthsUI16);
00131                 endEditCP(geo, Geometry::LengthsFieldMask);
00132             }
00133         }
00134     }
00135 
00136     // indices
00137     if(_filter & Geometry::IndicesFieldMask)
00138     {
00139         GeoIndicesUI32Ptr indicesUI32 = GeoIndicesUI32Ptr::dcast(geo->getIndices());
00140         if(indicesUI32 != NullFC)
00141         {
00142             MFUInt32 &src = indicesUI32->getField();
00143     
00144             // now check if maximum index is greater than 65535
00145             UInt32 max_index = UInt32(TypeTraits<UInt16>::getMax());
00146             bool max_index_ok = true;
00147             for(UInt32 i=0;i<src.size();++i)
00148             {
00149                 if(src[i] > max_index)
00150                 {
00151                     max_index_ok = false;
00152                     break;
00153                 }
00154             }
00155     
00156             if(max_index_ok)
00157             {
00158                 GeoIndicesUI16Ptr indicesUI16 = GeoIndicesUI16::create();
00159                 MFUInt16 &dst = indicesUI16->getField();
00160                 dst.reserve(src.size());
00161                 beginEditCP(indicesUI16);
00162                     for (UInt32 i = 0; i < src.size(); ++i)
00163                         dst.push_back(src[i]);
00164                 endEditCP(indicesUI16);
00165         
00166                 beginEditCP(geo, Geometry::IndicesFieldMask);
00167                     geo->setIndices(indicesUI16);
00168                 endEditCP(geo, Geometry::IndicesFieldMask);
00169             }
00170         }
00171     }
00172 
00173     return true;
00174 }
00175 
00176 
00177 bool GeoTypeGraphOp::travNodeLeave(NodePtr)
00178 {
00179     return true;
00180 }
00181 
00182 void GeoTypeGraphOp::setParams(const std::string params)
00183 {
00184     ParamSet ps(params);   
00185     std::string filter;
00186     
00187     if(ps("filter", filter))
00188     {
00189         _filter = 0;
00190         if(filter.find("Nor") != std::string::npos ||
00191            filter.find("nor") != std::string::npos)
00192         {
00193             _filter |= Geometry::NormalsFieldMask;
00194         }
00195         if(filter.find("Ind") != std::string::npos ||
00196            filter.find("ind") != std::string::npos)
00197         {
00198             _filter |= Geometry::IndicesFieldMask;
00199         }
00200         if(filter.find("Len") != std::string::npos ||
00201            filter.find("len") != std::string::npos)
00202         {
00203             _filter |= Geometry::LengthsFieldMask;
00204         }
00205     }
00206     
00207     std::string out = ps.getUnusedParams();
00208     if(out.length())
00209     {
00210         FWARNING(("GeoTypeGraphOp doesn't have parameters '%s'.\n",
00211                 out.c_str()));
00212     }
00213 }
00214 
00215 std::string GeoTypeGraphOp::usage(void)
00216 {
00217     return 
00218     "GeoType: convert the types of a Geometry's attributes\n"
00219     "  Tries to convert the attributes of a Geometry to smaller/faster\n"
00220     "  types. By default only the lengths are changed to 16 bit.\n"
00221     "Params: name (type, default)\n"
00222     "  filter  (string, \"\"): fields to convert, can be a combination of\n"
00223     "                        Normals, Indices and Lengths, connected by +.\n"
00224     ;
00225 }
00226 
00227 void GeoTypeGraphOp::setFilter(const OSG::BitVector &filter)
00228 {
00229     _filter = filter;
00230 }

Generated on Thu Aug 25 04:05:25 2005 for OpenSG by  doxygen 1.4.3