00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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 #if !defined(__sun) && !defined(OSG_NO_INT8_PNT)
00070
00071 if(_filter & Geometry::NormalsFieldMask)
00072 {
00073 GeoNormalsPtr normals = geo->getNormals();
00074 GeoNormals3fPtr normals3f = GeoNormals3fPtr::dcast(normals);
00075 if (normals3f != NullFC)
00076 {
00077 MFVec3f &src = normals3f->getField();
00078
00079 GeoNormals3bPtr normals3b = GeoNormals3b::create();
00080 MFVec3b &dst = normals3b->getField();
00081 dst.reserve(src.size());
00082 beginEditCP(normals3b);
00083 for (UInt32 i = 0; i < src.size(); ++i)
00084 {
00085 Vec3f vec = src[i];
00086 vec *= (0.9f / vec.length());
00087 normals3b->push_back(vec);
00088 }
00089 endEditCP(normals3b);
00090
00091 beginEditCP(geo, Geometry::NormalsFieldMask);
00092 geo->setNormals(normals3b);
00093 endEditCP(geo, Geometry::NormalsFieldMask);
00094 }
00095 }
00096 #endif
00097
00098 GeoColorsPtr colors = geo->getColors();
00099 GeoColorsPtr scolors = geo->getSecondaryColors();
00100
00101 if(_filter & Geometry::LengthsFieldMask)
00102 {
00103
00104 GeoPLengthsUI32Ptr lengthsUI32 = GeoPLengthsUI32Ptr::dcast(geo->getLengths());
00105 if(lengthsUI32 != NullFC)
00106 {
00107 MFUInt32 &src = lengthsUI32->getField();
00108
00109
00110 UInt32 max_length = UInt32(TypeTraits<UInt16>::getMax());
00111 bool max_length_ok = true;
00112 for(UInt32 i=0;i<src.size();++i)
00113 {
00114 if(src[i] > max_length)
00115 {
00116 max_length_ok = false;
00117 break;
00118 }
00119 }
00120
00121 if(max_length_ok)
00122 {
00123 GeoPLengthsUI16Ptr lengthsUI16 = GeoPLengthsUI16::create();
00124 MFUInt16 &dst = lengthsUI16->getField();
00125 dst.reserve(src.size());
00126 beginEditCP(lengthsUI16);
00127 for (UInt32 i = 0; i < src.size(); ++i)
00128 dst.push_back(UInt16(src[i]));
00129 endEditCP(lengthsUI16);
00130
00131 beginEditCP(geo, Geometry::LengthsFieldMask);
00132 geo->setLengths(lengthsUI16);
00133 endEditCP(geo, Geometry::LengthsFieldMask);
00134 }
00135 }
00136 }
00137
00138
00139 if(_filter & Geometry::IndicesFieldMask)
00140 {
00141 GeoIndicesUI32Ptr indicesUI32 = GeoIndicesUI32Ptr::dcast(geo->getIndices());
00142 if(indicesUI32 != NullFC)
00143 {
00144 MFUInt32 &src = indicesUI32->getField();
00145
00146
00147 UInt32 max_index = UInt32(TypeTraits<UInt16>::getMax());
00148 bool max_index_ok = true;
00149 for(UInt32 i=0;i<src.size();++i)
00150 {
00151 if(src[i] > max_index)
00152 {
00153 max_index_ok = false;
00154 break;
00155 }
00156 }
00157
00158 if(max_index_ok)
00159 {
00160 GeoIndicesUI16Ptr indicesUI16 = GeoIndicesUI16::create();
00161 MFUInt16 &dst = indicesUI16->getField();
00162 dst.reserve(src.size());
00163 beginEditCP(indicesUI16);
00164 for (UInt32 i = 0; i < src.size(); ++i)
00165 dst.push_back(src[i]);
00166 endEditCP(indicesUI16);
00167
00168 beginEditCP(geo, Geometry::IndicesFieldMask);
00169 geo->setIndices(indicesUI16);
00170 endEditCP(geo, Geometry::IndicesFieldMask);
00171 }
00172 }
00173 }
00174
00175 return true;
00176 }
00177
00178
00179 bool GeoTypeGraphOp::travNodeLeave(NodePtr)
00180 {
00181 return true;
00182 }
00183
00184 void GeoTypeGraphOp::setParams(const std::string params)
00185 {
00186 ParamSet ps(params);
00187 std::string filter;
00188
00189 if(ps("filter", filter))
00190 {
00191 _filter = 0;
00192 if(filter.find("Nor") != std::string::npos ||
00193 filter.find("nor") != std::string::npos)
00194 {
00195 _filter |= Geometry::NormalsFieldMask;
00196 }
00197 if(filter.find("Ind") != std::string::npos ||
00198 filter.find("ind") != std::string::npos)
00199 {
00200 _filter |= Geometry::IndicesFieldMask;
00201 }
00202 if(filter.find("Len") != std::string::npos ||
00203 filter.find("len") != std::string::npos)
00204 {
00205 _filter |= Geometry::LengthsFieldMask;
00206 }
00207 }
00208
00209 std::string out = ps.getUnusedParams();
00210 if(out.length())
00211 {
00212 FWARNING(("GeoTypeGraphOp doesn't have parameters '%s'.\n",
00213 out.c_str()));
00214 }
00215 }
00216
00217 std::string GeoTypeGraphOp::usage(void)
00218 {
00219 return
00220 "GeoType: convert the types of a Geometry's attributes\n"
00221 " Tries to convert the attributes of a Geometry to smaller/faster\n"
00222 " types. By default only the lengths are changed to 16 bit.\n"
00223 "Params: name (type, default)\n"
00224 " filter (string, \"\"): fields to convert, can be a combination of\n"
00225 " Normals, Indices and Lengths, connected by +.\n"
00226 ;
00227 }
00228
00229 void GeoTypeGraphOp::setFilter(const OSG::BitVector &filter)
00230 {
00231 _filter = filter;
00232 }