protected virtual void buildIndex(Profile profile, osg.Group scene_graph) { //scene_graph = new osg.Group(); if (lod.valid()) { scene_graph.addChild(lod.get()); } }
protected virtual void buildIndex(Profile _profile, osg.Group scene_graph) { QuadTreeProfile profile = (QuadTreeProfile)_profile; if (profile == null) { return; } //osgGIS.notice() << "Rebuilding index..." << std.endl; // first, determine the SRS of the output scene graph so that we can // make pagedlod/lod centroids. SpatialReference output_srs = map_layer.getOutputSRS(getSession(), getTerrainSRS()); // first build the very top level. //scene_graph = new osg.Group(); // the starting LOD is the best fit the the cell size: uint top_lod = getTopLod(profile.getQuadMap(), map_layer); SmartReadCallback reader = new SmartReadCallback(); foreach (MapLayerLevelOfDetail i in map_layer.getLevels()) { MapLayerLevelOfDetail level_def = i; uint lod = top_lod + level_def.getDepth(); MapLayerLevelOfDetail sub_level_def = i + 1 != map_layer.getLevels().end() ? (i + 1).get() : null; float min_range, max_range; if (sub_level_def != null) { min_range = sub_level_def.getMinRange(); max_range = sub_level_def.getMaxRange(); } // get the extent of tiles that we will build based on the AOI: uint cell_xmin, cell_ymin, cell_xmax, cell_ymax; profile.getQuadMap().getCells( map_layer.getAreaOfInterest(), lod, cell_xmin, cell_ymin, cell_xmax, cell_ymax); for (uint y = cell_ymin; y <= cell_ymax; y++) { for (uint x = cell_xmin; x <= cell_xmax; x++) { osg.Node node; QuadKey key = new QuadKey(x, y, lod, profile.getQuadMap()); //osgGIS.notify( osg.NOTICE ) // << "Cell: " << std.endl // << " Quadkey = " << key.toString() << std.endl // << " LOD = " << key.getLOD() << std.endl // << " Extent = " << key.getExtent().toString() << " (w=" << key.getExtent().getWidth() << ", h=" << key.getExtent().getHeight() << ")" << std.endl // << std.endl; node = sub_level_def ? createIntermediateIndexNode(key, min_range, max_range, reader.get()) : createLeafIndexNode(key, reader); if (node.valid()) { string out_file = createAbsPathFromTemplate("i" + key.toString()); if (!osgDB.writeNodeFile(*(node.get()), out_file)) { // osgGIS.warn() << "FAILED to write index file " << out_file << std.endl; } // at the top level, assemble the root node if (i == map_layer.getLevels().begin()) { double top_min_range = sub_level_def != null ? 0 : level_def.getMinRange(); osg.PagedLOD plod = new osg.PagedLOD(); plod.setName(key.toString()); plod.setFileName(0, createRelPathFromTemplate("i" + key.toString())); plod.setRange(0, top_min_range, level_def.getMaxRange()); plod.setPriorityScale(0, MY_PRIORITY_SCALE); setCenterAndRadius(plod, key.getExtent(), reader.get()); //osgGIS.notice() << "QK=" << key.toString() << ", Ex=" << key.getExtent().toString() << ", Cen=" << key.getExtent().getCentroid().toString() << std.endl; scene_graph.addChild(plod); } } } } } }
protected virtual AttributedNodeList process(AttributedNodeList input, FilterEnv env) { Node result; if (input.Count > 1) { result = new osg.Group(); for (AttributedNodeList.iterator i = input.begin(); i != input.end(); i++) { osg.Node node = i.get().getNode(); if (node != null) { if (getEmbedAttributes()) { embedAttributes(node, i.get().getAttributes()); } result.asGroup().addChild(node); } } } else if (input.Count == 1) { result = input[0].getNode(); if (getEmbedAttributes()) { embedAttributes(result.get(), input[0].getAttributes()); } } else { return(new AttributedNodeList()); } // if there are no drawables or external refs, toss it. if (!GeomUtils.hasDrawables(result.get())) { return(AttributedNodeList()); } // NEXT create a XFORM if there's a localization matrix in the SRS. This will // prevent jittering due to loss of precision. SpatialReference input_srs = env.getInputSRS(); if (env.getExtent().getArea() > 0 && !input_srs.getReferenceFrame().isIdentity()) { Vector3D centroid = new Vector3D(0, 0, 0); osg.Matrixd irf = input_srs.getInverseReferenceFrame(); osg.Vec3d centroid_abs = centroid * irf; osg.MatrixTransform xform = new osg.MatrixTransform(irf); xform.addChild(result); result = xform; if (getApplyClusterCulling() && input_srs.isGeocentric()) { Vector3D normal = centroid_abs; normal.normalize(); //osg.BoundingSphere bs = result.computeBound(); // force it // radius = distance from centroid inside which to disable CC altogether: //float radius = bs.radius(); //osg.Vec3d control_point = bs.center(); Vector3D control_point = centroid_abs; GeoPoint env_cen = input_srs.transform(env.getCellExtent().getCentroid()); GeoPoint env_sw = input_srs.transform(env.getCellExtent().getSouthwest()); float radius = (env_cen - env_sw).length(); // dot product: 0 = orthogonal to normal, -1 = equal to normal float deviation = -radius / input_srs.getEllipsoid().getSemiMinorAxis(); osg.ClusterCullingCallback ccc = new osg.ClusterCullingCallback(); ccc.set(control_point, normal, deviation, radius); osg.Group cull_group = new osg.Group(); cull_group.setCullCallback(ccc); cull_group.addChild(xform); result = cull_group; //osgGIS.notify(osg.NOTICE) << "CCC: radius = " << radius << ", deviation = " << deviation << std.endl; //if ( getDrawClusterCullingNormals() == true ) //{ // //DRAW CLUSTER-CULLING NORMALS // osg.Geode* geode = new osg.Geode(); // osg.Geometry* g = new osg.Geometry(); // osg.Vec3Array* v = new osg.Vec3Array(2); // (*v)[0] = control_point; (*v)[1] = control_point + (normal*radius); // g.setVertexArray( v ); // osg.Vec4Array* c = new osg.Vec4Array(1); // (*c)[0] = osg.Vec4f( 0,1,0,1 ); // g.setColorArray( c ); // g.setColorBinding( osg.Geometry.BIND_OVERALL ); // g.addPrimitiveSet( new osg.DrawArrays( osg.PrimitiveSet.LINES, 0, 2 ) ); // geode.addDrawable( g ); // cull_group.addChild( geode ); //} } } if (getCullBackfaces()) { result.getOrCreateStateSet().setAttributeAndModes(new osg.CullFace(), osg.StateAttribute.ON); } if (getDisableLighting()) { result.getOrCreateStateSet().setMode(GL_LIGHTING, osg.StateAttribute.OFF); } if (getLineWidth() > 0.0f) { result.getOrCreateStateSet().setAttribute(new osg.LineWidth(line_width), osg.StateAttribute.ON); } if (getPointSize() > 0.0f) { osg.Point point = new osg.Point(); point.setSize(point_size); result.getOrCreateStateSet().setAttribute(point, osg.StateAttribute.ON); } if (getAlphaBlending()) { osg.BlendFunc blend_func = new osg.BlendFunc(); //blend_func.setFunction( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); result.getOrCreateStateSet().setAttributeAndModes(blend_func, osg.StateAttribute.ON); result.getOrCreateStateSet().setRenderingHint(osg.StateSet.TRANSPARENT_BIN); } if (getRasterOverlayScript()) { ScriptResult r = env.getScriptEngine().run(getRasterOverlayScript(), env); if (r.isValid()) { RasterResource *raster = env.getSession().getResources().getRaster(r.asString()); if (raster) { osg.Image *image = NULL; std.stringstream builder; string cell_id = env.getProperties().getValue("compiler.cell_id", ""); if (cell_id.length() > 0) { builder << "r" << cell_id << ".jpg"; } else { double x = env.getExtent().getCentroid().x(); double y = env.getExtent().getCentroid().y(); builder << std.setprecision(10) << "r" << x << "x" << y << ".jpg"; } if (raster.applyToStateSet(result.getOrCreateStateSet(), env.getExtent(), getRasterOverlayMaxSize(), &image)) { // Add this as a skin resource so the compiler can properly localize and deploy it. image.setFileName(builder.str()); env.getResourceCache().addSkin(result.getOrCreateStateSet()); } } } else { env.getReport().error(r.asString()); } } if (getOptimize()) { //osgGIS.notice() << "[BuildNodes] Optimizing..." << std.endl; osgUtil.Optimizer opt; int opt_mask = osgUtil.Optimizer.DEFAULT_OPTIMIZATIONS | osgUtil.Optimizer.MERGE_GEODES | osgUtil.Optimizer.TRISTRIP_GEOMETRY | osgUtil.Optimizer.SPATIALIZE_GROUPS; // disable texture atlases, since they mess with our shared skin resources and // don't work correctly during multi-threaded building opt_mask &= ~osgUtil.Optimizer.TEXTURE_ATLAS_BUILDER; // I've seen this crash the app when dealing with certain ProxyNodes. // TODO: investigate this later. opt_mask &= ~osgUtil.Optimizer.REMOVE_REDUNDANT_NODES; // integrate the optimizer hints: opt_mask |= env.getOptimizerHints().getIncludedOptions(); opt_mask &= ~(env.getOptimizerHints().getExcludedOptions()); opt.optimize(result.get(), opt_mask); GeometryCleaner cleaner; cleaner.clean(result.get()); } AttributedNodeList output; output.push_back(new AttributedNode(result.get())); return(output); }
protected virtual AttributedNodeList process(AttributedNodeList input, FilterEnv env) { Node result; if ( input.Count > 1 ) { result = new osg.Group(); for( AttributedNodeList.iterator i = input.begin(); i != input.end(); i++ ) { osg.Node node = i.get().getNode(); if ( node != null) { if ( getEmbedAttributes() ) embedAttributes( node, i.get().getAttributes() ); result.asGroup().addChild( node ); } } } else if ( input.Count == 1 ) { result = input[0].getNode(); if ( getEmbedAttributes() ) embedAttributes( result.get(), input[0].getAttributes() ); } else { return new AttributedNodeList(); } // if there are no drawables or external refs, toss it. if ( !GeomUtils.hasDrawables( result.get() ) ) { return AttributedNodeList(); } // NEXT create a XFORM if there's a localization matrix in the SRS. This will // prevent jittering due to loss of precision. SpatialReference input_srs = env.getInputSRS(); if ( env.getExtent().getArea() > 0 && !input_srs.getReferenceFrame().isIdentity() ) { Vector3D centroid = new Vector3D( 0, 0, 0 ); osg.Matrixd irf = input_srs.getInverseReferenceFrame(); osg.Vec3d centroid_abs = centroid * irf; osg.MatrixTransform xform = new osg.MatrixTransform( irf ); xform.addChild( result); result = xform; if ( getApplyClusterCulling() && input_srs.isGeocentric() ) { Vector3D normal = centroid_abs; normal.normalize(); //osg.BoundingSphere bs = result.computeBound(); // force it // radius = distance from centroid inside which to disable CC altogether: //float radius = bs.radius(); //osg.Vec3d control_point = bs.center(); Vector3D control_point = centroid_abs; GeoPoint env_cen = input_srs.transform( env.getCellExtent().getCentroid() ); GeoPoint env_sw = input_srs.transform( env.getCellExtent().getSouthwest() ); float radius = (env_cen-env_sw).length(); // dot product: 0 = orthogonal to normal, -1 = equal to normal float deviation = -radius/input_srs.getEllipsoid().getSemiMinorAxis(); osg.ClusterCullingCallback ccc = new osg.ClusterCullingCallback(); ccc.set( control_point, normal, deviation, radius ); osg.Group cull_group = new osg.Group(); cull_group.setCullCallback( ccc ); cull_group.addChild( xform ); result = cull_group; //osgGIS.notify(osg.NOTICE) << "CCC: radius = " << radius << ", deviation = " << deviation << std.endl; //if ( getDrawClusterCullingNormals() == true ) //{ // //DRAW CLUSTER-CULLING NORMALS // osg.Geode* geode = new osg.Geode(); // osg.Geometry* g = new osg.Geometry(); // osg.Vec3Array* v = new osg.Vec3Array(2); // (*v)[0] = control_point; (*v)[1] = control_point + (normal*radius); // g.setVertexArray( v ); // osg.Vec4Array* c = new osg.Vec4Array(1); // (*c)[0] = osg.Vec4f( 0,1,0,1 ); // g.setColorArray( c ); // g.setColorBinding( osg.Geometry.BIND_OVERALL ); // g.addPrimitiveSet( new osg.DrawArrays( osg.PrimitiveSet.LINES, 0, 2 ) ); // geode.addDrawable( g ); // cull_group.addChild( geode ); //} } } if ( getCullBackfaces() ) { result.getOrCreateStateSet().setAttributeAndModes( new osg.CullFace(), osg.StateAttribute.ON ); } if ( getDisableLighting() ) { result.getOrCreateStateSet().setMode( GL_LIGHTING, osg.StateAttribute.OFF ); } if ( getLineWidth() > 0.0f ) { result.getOrCreateStateSet().setAttribute( new osg.LineWidth( line_width ), osg.StateAttribute.ON ); } if ( getPointSize() > 0.0f ) { osg.Point point = new osg.Point(); point.setSize( point_size ); result.getOrCreateStateSet().setAttribute( point, osg.StateAttribute.ON ); } if ( getAlphaBlending() ) { osg.BlendFunc blend_func = new osg.BlendFunc(); //blend_func.setFunction( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); result.getOrCreateStateSet().setAttributeAndModes( blend_func, osg.StateAttribute.ON ); result.getOrCreateStateSet().setRenderingHint( osg.StateSet.TRANSPARENT_BIN ); } if ( getRasterOverlayScript() ) { ScriptResult r = env.getScriptEngine().run( getRasterOverlayScript(), env ); if ( r.isValid() ) { RasterResource* raster = env.getSession().getResources().getRaster( r.asString() ); if ( raster ) { osg.Image* image = NULL; std.stringstream builder; string cell_id = env.getProperties().getValue( "compiler.cell_id", "" ); if ( cell_id.length() > 0 ) { builder << "r" << cell_id << ".jpg"; } else { double x = env.getExtent().getCentroid().x(); double y = env.getExtent().getCentroid().y(); builder << std.setprecision(10) << "r" << x << "x" << y << ".jpg"; } if ( raster.applyToStateSet( result.getOrCreateStateSet(), env.getExtent(), getRasterOverlayMaxSize(), &image ) ) { // Add this as a skin resource so the compiler can properly localize and deploy it. image.setFileName( builder.str() ); env.getResourceCache().addSkin( result.getOrCreateStateSet() ); } } } else { env.getReport().error( r.asString() ); } } if ( getOptimize() ) { //osgGIS.notice() << "[BuildNodes] Optimizing..." << std.endl; osgUtil.Optimizer opt; int opt_mask = osgUtil.Optimizer.DEFAULT_OPTIMIZATIONS | osgUtil.Optimizer.MERGE_GEODES | osgUtil.Optimizer.TRISTRIP_GEOMETRY | osgUtil.Optimizer.SPATIALIZE_GROUPS; // disable texture atlases, since they mess with our shared skin resources and // don't work correctly during multi-threaded building opt_mask &= ~osgUtil.Optimizer.TEXTURE_ATLAS_BUILDER; // I've seen this crash the app when dealing with certain ProxyNodes. // TODO: investigate this later. opt_mask &= ~osgUtil.Optimizer.REMOVE_REDUNDANT_NODES; // integrate the optimizer hints: opt_mask |= env.getOptimizerHints().getIncludedOptions(); opt_mask &= ~( env.getOptimizerHints().getExcludedOptions() ); opt.optimize( result.get(), opt_mask ); GeometryCleaner cleaner; cleaner.clean( result.get() ); } AttributedNodeList output; output.push_back( new AttributedNode( result.get() ) ); return output; }
/** * Runs the graph to generate a scene graph. * * Executes the graph by passing features to the first filter in the * chain. That filter will process the data, pass the results along to * the next filter, and so on until completion. * * @param cursor * Source cursor for features to process * @param env * Contextual compilation environment * @param output * Group node that, upon success, contains resulting nodes of compiled scene * as its children * @return * A structure describing the result of the compilation. */ public FilterGraphResult computeNodes(FeatureCursor cursor, FilterEnv env, osg.Group output) { FilterStateResult state_result; output = null; NodeFilterState output_state; // first build a new state chain corresponding to our filter prototype chain. FilterState first = null; foreach (Filter i in filter_prototypes) { FilterState next_state = i.newState(); if (first == null) { first = next_state; } else { first.appendState(next_state); } if (next_state is NodeFilterState) { output_state = (NodeFilterState)next_state; } } // now traverse the states. if (first != null) { int count = 0; osg.Timer_t start = osg.Timer.instance().tick(); env.setOutputSRS(env.getInputSRS()); if (first is FeatureFilterState) { FeatureFilterState state = (FeatureFilterState)first; while (state_result.isOK() && cursor.hasNext()) { state.push(wind(cursor.next())); state_result = state.traverse(env); count++; } if (state_result.isOK()) { state_result = state.signalCheckpoint(); } } else if (first is FragmentFilterState) { FragmentFilterState state = (FragmentFilterState)first; while (state_result.isOK() && cursor.hasNext()) { state.push(wind(cursor.next())); state_result = state.traverse(env); count++; } if (state_result.isOK()) { state_result = state.signalCheckpoint(); } } else if (first is CollectionFilterState) { CollectionFilterState state = (CollectionFilterState)first; while (state_result.isOK() && cursor.hasNext()) { state.push(wind(cursor.next())); state_result = state.traverse(env); count++; } if (state_result.isOK()) { state_result = state.signalCheckpoint(); } } osg.Timer_t end = osg.Timer.instance().tick(); double dur = osg.Timer.instance().delta_s(start, end); //osgGIS.notify( osg.ALWAYS ) << std.endl << "Time = " << dur << " s; Per Feature Avg = " << (dur/(double)count) << " s" << std.endl; } else { state_result.set(FilterStateResult.Status.STATUS_NODATA); } if (output_state != null && state_result.hasData()) { output = new osg.Group(); foreach (AttributedNode i in output_state.getOutput()) { output.addChild(i.getNode()); } } if (state_result.isOK()) { return(FilterGraphResult.ok(output_state.getLastKnownFilterEnv())); } else { return(FilterGraphResult.error("Filter graph failed to compute any nodes")); } }