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
00040
00041
00042
00043
00044 #include <OSGPruneGraphOp.h>
00045
00046 OSG_USING_NAMESPACE
00047
00048
00049
00050
00051
00058 PruneGraphOp::PruneGraphOp(float size, Method method, const char* name)
00059 : GraphOp(name)
00060 , _size(size)
00061 , _method(method)
00062 {
00063 }
00064
00065 GraphOp* PruneGraphOp::create()
00066 {
00067 return new PruneGraphOp(_size, _method);
00068 }
00069
00070 void PruneGraphOp::setParams(const std::string params)
00071 {
00072 ParamSet ps(params);
00073
00074 ps("size", _size);
00075
00076 std::string m;
00077 ps("method", m);
00078
00079 if(m.length())
00080 {
00081 if(m.find("volume") || m.find("VOLUME"))
00082 {
00083 _method = VOLUME;
00084 }
00085 else if(m.find("sum_of_dimensions") || m.find("SUM_OF_DIMENSIONS") ||
00086 m.find("sum"))
00087 {
00088 _method = SUM_OF_DIMENSIONS;
00089 }
00090 else
00091 {
00092 FWARNING(("GeoTypeGraphOp: method '%s' unknown.\n", m.c_str()));
00093 }
00094 }
00095
00096 std::string out = ps.getUnusedParams();
00097 if(out.length())
00098 {
00099 FWARNING(("PruneGraphOp doesn't have parameters '%s'.\n",
00100 out.c_str()));
00101 }
00102 }
00103
00104 std::string PruneGraphOp::usage(void)
00105 {
00106 return
00107 "Prune: Remove small objects\n"
00108 " Removes nodes of size smaller than a given threshold from the scene\n"
00109 "Params: name (type, default)\n"
00110 " method (string, SUM_OF_DIMENSIONS): \n"
00111 " VOLUME: measure volume of Node\n"
00112 " SUM_OF_DIMENSIONS: add upp the individual dims\n"
00113 " size (Real32, 1.0f): \n"
00114 " threshold value. Nodes smaller than this will be\n"
00115 " removed\n";
00116 }
00117
00118 Action::ResultE PruneGraphOp::traverseEnter(NodePtr& node)
00119 {
00120 return isTooSmall(node) ? Action::Skip : Action::Continue;
00121 }
00122
00123 Action::ResultE PruneGraphOp::traverseLeave(NodePtr& node, Action::ResultE res)
00124 {
00125 beginEditCP(node);
00126 for (UInt32 i = 0; i < node->getNChildren(); ++i) {
00127 if (isTooSmall(node->getChild(i))) {
00128 node->subChild(i);
00129 --i;
00130 }
00131 }
00132 endEditCP(node);
00133
00134 return res;
00135 }
00136
00137 bool PruneGraphOp::isTooSmall(const NodePtr& node)
00138 {
00139 return getSize(node) < _size;
00140 }
00141
00142 float PruneGraphOp::getSize(const NodePtr& node)
00143 {
00144 #ifndef OSG_2_PREP
00145 const DynamicVolume &vol = node->editVolume(true);
00146 #else
00147 const BoxVolume &vol = node->editVolume(true);
00148 #endif
00149 if (_method == VOLUME)
00150 {
00151 return vol.getScalarVolume();
00152 }
00153 else if (_method == SUM_OF_DIMENSIONS)
00154 {
00155 Pnt3f min, max;
00156 vol.getBounds(min, max);
00157 Vec3f diff = max - min;
00158 return diff[0] + diff[1] + diff[2];
00159 }
00160 else
00161 {
00162 SWARNING << "Unknown size calculation method" << std::endl;
00163 return 0;
00164 }
00165 }