Sirikata
libcore/include/sirikata/core/prox/QueryHandlerFactory.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2010 Sirikata Authors. All rights reserved.
00002 // Use of this source code is governed by a BSD-style license that can
00003 // be found in the LICENSE file.
00004 
00005 // NOTE: This isn't really ideal since we shouldn't really depend on libprox
00006 // within libcore. However, this works fine since it's just a header and makes
00007 // it available to libspace plugins, liboh plugins, and pinto. DO NOT add
00008 // anything which requires exporting symbols or an implementation file.
00009 
00010 #include <sirikata/core/util/Platform.hpp>
00011 #include <sirikata/core/options/Options.hpp>
00012 
00013 #include <prox/geom/BruteForceQueryHandler.hpp>
00014 #include <prox/geom/RTreeAngleQueryHandler.hpp>
00015 #include <prox/geom/RTreeDistanceQueryHandler.hpp>
00016 #include <prox/geom/RTreeCutQueryHandler.hpp>
00017 #include <prox/geom/LevelQueryHandler.hpp>
00018 #include <prox/geom/RebuildingQueryHandler.hpp>
00019 
00020 namespace Sirikata {
00021 
00025 template<typename SimulationTraits>
00026 Prox::QueryHandler<SimulationTraits>* QueryHandlerFactory(const String& type, const String& args, bool rebuilding = true) {
00027     static OptionValue* branching = NULL;
00028     static OptionValue* rebuild_batch_size = NULL;
00029     if (branching == NULL) {
00030         branching = new OptionValue("branching", "10", Sirikata::OptionValueType<uint32>(), "Number of children each node should have.");
00031         rebuild_batch_size = new OptionValue("rebuild-batch-size", "10", Sirikata::OptionValueType<uint32>(), "Number of queries to transition on each iteration when rebuilding. Keep this small to avoid long latencies between updates.");
00032         Sirikata::InitializeClassOptions ico("query_handler", NULL,
00033             branching,
00034             rebuild_batch_size,
00035             NULL);
00036     }
00037 
00038     assert(branching != NULL);
00039 
00040     // Since these options end up being shared if you instantiate multiple
00041     // QueryHandlers, reset them each time.
00042     branching->unsafeAs<uint32>() = 10;
00043 
00044     OptionSet* optionsSet = OptionSet::getOptions("query_handler", NULL);
00045     optionsSet->parse(args);
00046 
00047     if (type == "brute") {
00048         if (rebuilding)
00049             return new Prox::RebuildingQueryHandler<SimulationTraits>(
00050                 Prox::BruteForceQueryHandler<SimulationTraits>::Constructor(), rebuild_batch_size->unsafeAs<uint32>()
00051             );
00052         else
00053             return new Prox::BruteForceQueryHandler<SimulationTraits>();
00054 
00055     }
00056     else if (type == "rtree") {
00057         if (rebuilding)
00058             return new Prox::RebuildingQueryHandler<SimulationTraits>(
00059                 Prox::RTreeAngleQueryHandler<SimulationTraits>::Constructor(branching->unsafeAs<uint32>()), rebuild_batch_size->unsafeAs<uint32>()
00060             );
00061         else
00062             return new Prox::RTreeAngleQueryHandler<SimulationTraits>(branching->unsafeAs<uint32>());
00063     }
00064     else if (type == "rtreedist" || type == "dist") {
00065         if (rebuilding)
00066             return new Prox::RebuildingQueryHandler<SimulationTraits>(
00067                 Prox::RTreeDistanceQueryHandler<SimulationTraits>::Constructor(branching->unsafeAs<uint32>()), rebuild_batch_size->unsafeAs<uint32>()
00068             );
00069         else
00070             return new Prox::RTreeDistanceQueryHandler<SimulationTraits>(branching->unsafeAs<uint32>());
00071     }
00072     else if (type == "rtreecut") {
00073         if (rebuilding)
00074             return new Prox::RebuildingQueryHandler<SimulationTraits>(
00075                 Prox::RTreeCutQueryHandler<SimulationTraits>::Constructor(branching->unsafeAs<uint32>(), false), rebuild_batch_size->unsafeAs<uint32>()
00076             );
00077         else
00078             return new Prox::RTreeCutQueryHandler<SimulationTraits>(branching->unsafeAs<uint32>(), false);
00079     }
00080     else if (type == "rtreecutagg") {
00081         if (rebuilding)
00082             return new Prox::RebuildingQueryHandler<SimulationTraits>(
00083                 Prox::RTreeCutQueryHandler<SimulationTraits>::Constructor(branching->unsafeAs<uint32>(), true), rebuild_batch_size->unsafeAs<uint32>()
00084             );
00085         else
00086             return new Prox::RTreeCutQueryHandler<SimulationTraits>(branching->unsafeAs<uint32>(), true);
00087     }
00088     else if (type == "level") {
00089         if (rebuilding)
00090             return new Prox::RebuildingQueryHandler<SimulationTraits>(
00091                 Prox::LevelQueryHandler<SimulationTraits>::Constructor(branching->unsafeAs<uint32>()), rebuild_batch_size->unsafeAs<uint32>()
00092             );
00093         else
00094             return new Prox::LevelQueryHandler<SimulationTraits>(branching->unsafeAs<uint32>());
00095     }
00096     else {
00097         return NULL;
00098     }
00099 }
00100 
00101 } // namespace Sirikata