Example usage for com.mongodb BasicDBObject get

List of usage examples for com.mongodb BasicDBObject get

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject get.

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value from this object

Usage

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the Pose of the given collision at the given timepoint (or the most recent one)
 * view results as rviz markers// w  w  w .  j  a  va2 s .c o m
 */
public void ViewCollisionPoseAt(double timestamp, String model_name, String link_name, String collision_name,
        String markerID, String markerType, String color, float scale) {

    // $and list for querying the $match in the aggregation
    BasicDBList time_and_name = new BasicDBList();

    // add the timestamp and the model name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("models.name", model_name));

    // create the pipeline operations, first the $match
    DBObject match_time_and_name = new BasicDBObject("$match", new BasicDBObject("$and", time_and_name));

    // sort the results in descending order on the timestamp (keep most recent result first)
    DBObject sort_desc = new BasicDBObject("$sort", new BasicDBObject("timestamp", -1));

    // $limit the result to 1, we only need one pose
    DBObject limit_result = new BasicDBObject("$limit", 1);

    // $unwind models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_links_fields = new BasicDBObject("_id", 0);
    proj_links_fields.put("timestamp", 1);
    proj_links_fields.put("models.links", 1);
    DBObject project_links = new BasicDBObject("$project", proj_links_fields);

    // $unwind the links
    DBObject unwind_links = new BasicDBObject("$unwind", "$models.links");

    // $match for the given link name from the unwinded links
    DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name));

    // build the final $projection operation
    DBObject proj_collision_fields = new BasicDBObject("timestamp", 1);
    proj_collision_fields.put("models.links.collisions", 1);
    DBObject project_collisions = new BasicDBObject("$project", proj_collision_fields);

    // $unwind the collisions
    DBObject unwind_collisions = new BasicDBObject("$unwind", "$models.links.collisions");

    // $match for the given collision name from the unwinded collisions
    DBObject match_collision = new BasicDBObject("$match",
            new BasicDBObject("models.links.collisions.name", collision_name));

    // build the final $projection operation
    DBObject proj_fields = new BasicDBObject("timestamp", 1);
    proj_fields.put("pos", "$models.links.collisions.pos");
    proj_fields.put("rot", "$models.links.collisions.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_models,
            match_model, project_links, unwind_links, match_link, project_collisions, unwind_collisions,
            match_collision, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions);

    if (cursor.hasNext()) {
        // Traj as dynamic array
        ArrayList<Vector3d> pose = new ArrayList<Vector3d>();

        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        pose.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));

        this.CreateMarkers(pose, markerID, markerType, color, scale);
    }
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the trajectory of the given model with double timestamps
 * view as rviz markers// w  ww  .  j  ava2 s .com
 */
public ArrayList<Vector3d> ViewModelTrajectory(double start_ts, double end_ts, String model_name,
        String markerID) {
    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind the models
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("pos", "$models.pos");
    proj_fields.put("rot", "$models.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_models, match_model, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));

        if (markerID != null) {
            traj.add(new Vector3d(((BasicDBObject) first_doc.get("rot")).getDouble("x"),
                    ((BasicDBObject) first_doc.get("rot")).getDouble("y"),
                    ((BasicDBObject) first_doc.get("rot")).getDouble("z")));
        }

    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewModelPoseAt(start_ts, model_name, markerID);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject first_doc = (BasicDBObject) cursor.next();
        // add position to trajectory
        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));

        if (markerID != null) {
            traj.add(new Vector3d(((BasicDBObject) first_doc.get("rot")).getDouble("x"),
                    ((BasicDBObject) first_doc.get("rot")).getDouble("y"),
                    ((BasicDBObject) first_doc.get("rot")).getDouble("z")));
        }
    }

    // create the markers
    if (markerID != null)
        this.CreateMarkers(traj, markerID);
    return traj;
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the trajectory of the given model with double timestamps
 * view as rviz markers// w w  w  .java2  s.c o  m
 */
public void ViewModelTrajectory(double start_ts, double end_ts, String model_name, String markerID,
        String markerType, String color, float scale, double deltaT) {

    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind the models
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("pos", "$models.pos");
    proj_fields.put("rot", "$models.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_models, match_model, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // timestamp used for deltaT
    double prev_ts = 0;

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
        // set the timestamp
        prev_ts = first_doc.getDouble("timestamp");
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewModelPoseAt(start_ts, model_name, markerID, markerType, color, scale);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            traj.add(new Vector3d(((BasicDBObject) curr_doc.get("pos")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("z")));
            prev_ts = curr_ts;
        }
    }

    // create the markers
    this.CreateMarkers(traj, markerID, markerType, color, scale);
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the trajectory of the given model with double timestamps
 * view as rviz markers//  w ww. j a  v  a 2 s .com
 */
public void ViewModelMeshTrajectory(double start_ts, double end_ts, String model_name, String meshPath,
        String markerID, double deltaT) {

    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind the models
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("pos", "$models.pos");
    proj_fields.put("rot", "$models.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_models, match_model, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions);

    // timestamp used for deltaT
    double prev_ts = 0;

    // marker ID suffix
    int marker_suff = 0;

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        double[] translation = new double[] { ((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z") };
        double[] orientation = this.quatFromEulerRad(((BasicDBObject) first_doc.get("rot")).getDouble("x"),
                ((BasicDBObject) first_doc.get("rot")).getDouble("y"),
                ((BasicDBObject) first_doc.get("rot")).getDouble("z"));
        // create the markers
        this.CreateMeshMarker(translation, orientation, meshPath, markerID);

        // set the timestamp
        prev_ts = first_doc.getDouble("timestamp");
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewModelMeshAt(start_ts, model_name, markerID, meshPath);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            double[] translation = new double[] { ((BasicDBObject) curr_doc.get("pos")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("z") };
            double[] orientation = this.quatFromEulerRad(((BasicDBObject) curr_doc.get("rot")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("rot")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("rot")).getDouble("z"));
            // create the markers
            this.CreateMeshMarker(translation, orientation, meshPath, markerID + marker_suff);

            // update current ts
            prev_ts = curr_ts;

            // append to marker suffix
            marker_suff++;
        }
    }
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the trajectory of the given link of the given model with double timestamps
 * save result in MongoDB/*from  w  w w .ja v  a 2  s.  co  m*/
 */
public void ViewLinkTrajectory(double start_ts, double end_ts, String model_name, String link_name,
        String markerID) {

    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind the models
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_links_fields = new BasicDBObject("_id", 0);
    proj_links_fields.put("timestamp", 1);
    proj_links_fields.put("models.links", 1);
    DBObject project_links = new BasicDBObject("$project", proj_links_fields);

    // $unwind the links
    DBObject unwind_links = new BasicDBObject("$unwind", "$models.links");

    // $match for the given link name from the unwinded links
    DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name));

    // build the final $projection operation
    DBObject proj_fields = new BasicDBObject("timestamp", 1);
    proj_fields.put("pos", "$models.links.pos");
    proj_fields.put("rot", "$models.links.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_models, match_model, project_links, unwind_links,
            match_link, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewLinkPoseAt(start_ts, model_name, link_name, markerID);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject first_doc = (BasicDBObject) cursor.next();
        // add position to trajectory
        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
    }

    // create the markers
    this.CreateMarkers(traj, markerID);
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the trajectory of the given link of the given model with double timestamps
 * save result in MongoDB//from   www  . j a va 2 s. c o  m
 */
public void ViewLinkTrajectory(double start_ts, double end_ts, String model_name, String link_name,
        String markerID, String markerType, String color, float scale, double deltaT) {

    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind the models
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_links_fields = new BasicDBObject("_id", 0);
    proj_links_fields.put("timestamp", 1);
    proj_links_fields.put("models.links", 1);
    DBObject project_links = new BasicDBObject("$project", proj_links_fields);

    // $unwind the links
    DBObject unwind_links = new BasicDBObject("$unwind", "$models.links");

    // $match for the given link name from the unwinded links
    DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name));

    // build the final $projection operation
    DBObject proj_fields = new BasicDBObject("timestamp", 1);
    proj_fields.put("pos", "$models.links.pos");
    proj_fields.put("rot", "$models.links.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_models, match_model, project_links, unwind_links,
            match_link, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // timestamp used for deltaT
    double prev_ts = 0;

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
        // set the timestamp
        prev_ts = first_doc.getDouble("timestamp");
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewLinkPoseAt(start_ts, model_name, link_name, markerID, markerType, color, scale);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            traj.add(new Vector3d(((BasicDBObject) curr_doc.get("pos")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("z")));
            prev_ts = curr_ts;
        }
    }

    // create the markers
    this.CreateMarkers(traj, markerID, markerType, color, scale);
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the trajectory of the given collision of the given link of the given model from double timestamps
 * view results as rviz markers/*from w  ww . j  a  v  a  2s.  co  m*/
 */
public void ViewCollisionTrajectory(double start_ts, double end_ts, String model_name, String link_name,
        String collision_name, String markerID) {

    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind the models
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_links_fields = new BasicDBObject("_id", 0);
    proj_links_fields.put("timestamp", 1);
    proj_links_fields.put("models.links", 1);
    DBObject project_links = new BasicDBObject("$project", proj_links_fields);

    // $unwind the links
    DBObject unwind_links = new BasicDBObject("$unwind", "$models.links");

    // $match for the given link name from the unwinded links
    DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name));

    // build the final $projection operation
    DBObject proj_collision_fields = new BasicDBObject("timestamp", 1);
    proj_collision_fields.put("models.links.collisions", 1);
    DBObject project_collisions = new BasicDBObject("$project", proj_collision_fields);

    // $unwind the collisions
    DBObject unwind_collisions = new BasicDBObject("$unwind", "$models.links.collisions");

    // $match for the given collision name from the unwinded collisions
    DBObject match_collision = new BasicDBObject("$match",
            new BasicDBObject("models.links.collisions.name", collision_name));

    // build the final $projection operation
    DBObject proj_fields = new BasicDBObject("timestamp", 1);
    proj_fields.put("pos", "$models.links.collisions.pos");
    proj_fields.put("rot", "$models.links.collisions.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_models, match_model, project_links, unwind_links,
            match_link, project_collisions, unwind_collisions, match_collision, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewCollisionPoseAt(start_ts, model_name, link_name, collision_name, markerID);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject first_doc = (BasicDBObject) cursor.next();
        // add position to trajectory
        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
    }

    // create the markers
    this.CreateMarkers(traj, markerID);
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the trajectory of the given collision of the given link of the given model from double timestamps
 * view results as rviz markers/*from   ww  w . j  av  a 2s .  c om*/
 */
public void ViewCollisionTrajectory(double start_ts, double end_ts, String model_name, String link_name,
        String collision_name, String markerID, String markerType, String color, float scale, double deltaT) {

    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind the models
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_links_fields = new BasicDBObject("_id", 0);
    proj_links_fields.put("timestamp", 1);
    proj_links_fields.put("models.links", 1);
    DBObject project_links = new BasicDBObject("$project", proj_links_fields);

    // $unwind the links
    DBObject unwind_links = new BasicDBObject("$unwind", "$models.links");

    // $match for the given link name from the unwinded links
    DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name));

    // build the final $projection operation
    DBObject proj_collision_fields = new BasicDBObject("timestamp", 1);
    proj_collision_fields.put("models.links.collisions", 1);
    DBObject project_collisions = new BasicDBObject("$project", proj_collision_fields);

    // $unwind the collisions
    DBObject unwind_collisions = new BasicDBObject("$unwind", "$models.links.collisions");

    // $match for the given collision name from the unwinded collisions
    DBObject match_collision = new BasicDBObject("$match",
            new BasicDBObject("models.links.collisions.name", collision_name));

    // build the final $projection operation
    DBObject proj_fields = new BasicDBObject("timestamp", 1);
    proj_fields.put("pos", "$models.links.collisions.pos");
    proj_fields.put("rot", "$models.links.collisions.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_models, match_model, project_links, unwind_links,
            match_link, project_collisions, unwind_collisions, match_collision, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // timestamp used for deltaT
    double prev_ts = 0;

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
        // set the timestamp
        prev_ts = first_doc.getDouble("timestamp");
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewCollisionPoseAt(start_ts, model_name, link_name, collision_name, markerID, markerType, color,
                scale);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            traj.add(new Vector3d(((BasicDBObject) curr_doc.get("pos")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("z")));
            prev_ts = curr_ts;
        }
    }

    // create the markers
    this.CreateMarkers(traj, markerID, markerType, color, scale);
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Get the positions of the model links at the given timestamp
 * view results as rviz markers/* w w w  . j  a  v  a  2  s  .c  om*/
 */
public void ViewLinksPositionsAt(String ts_str, String model_name, String markerID, String markerType,
        String color, float scale) {

    // transform the knowrob time to double with 3 decimal precision
    double timestamp = (double) Math.round((parseTime_d(ts_str) - TIME_OFFSET) * 1000) / 1000;

    // $and list for querying the $match in the aggregation
    BasicDBList time_and_name = new BasicDBList();

    // add the timestamp and the model name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("models.name", model_name));

    // create the pipeline operations, first the $match
    DBObject match_time_and_name = new BasicDBObject("$match", new BasicDBObject("$and", time_and_name));

    // sort the results in descending order on the timestamp (keep most recent result first)
    DBObject sort_desc = new BasicDBObject("$sort", new BasicDBObject("timestamp", -1));

    // $limit the result to 1, we only need one pose
    DBObject limit_result = new BasicDBObject("$limit", 1);

    // $unwind models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("links_pos", "$models.links.pos");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_models,
            match_model, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions);

    // Traj as dynamic array
    ArrayList<Vector3d> positions = new ArrayList<Vector3d>();

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        // get the list of links pos
        BasicDBList pos_list = (BasicDBList) first_doc.get("links_pos");

        // pos_list and rot_list length should be always the same
        for (int i = 0; i < pos_list.size(); ++i) {
            positions.add(new Vector3d(((BasicDBObject) pos_list.get(i)).getDouble("x"),
                    ((BasicDBObject) pos_list.get(i)).getDouble("y"),
                    ((BasicDBObject) pos_list.get(i)).getDouble("z")));
        }
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        System.out.println("Java  - ViewLinksPositionsAt Query returned no results!'");
    }

    // create the markers
    this.CreateMarkers(positions, markerID, markerType, color, scale);
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Get the positions of the model links at the given timestamp
 * view as rviz markers/*  www  .j  ava2s .  c o m*/
 */
public void ViewLinksTrajs(String start_str, String end_str, String model_name, String markerID) {

    // transform the knowrob time to double with 3 decimal precision
    double start_ts = (double) Math.round((parseTime_d(start_str) - TIME_OFFSET) * 1000) / 1000;
    double end_ts = (double) Math.round((parseTime_d(end_str) - TIME_OFFSET) * 1000) / 1000;

    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("links_pos", "$models.links.pos");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_models, match_model, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions);

    // Traj as dynamic array
    ArrayList<Vector3d> trajs = new ArrayList<Vector3d>();

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        // get the list of links pos
        BasicDBList pos_list = (BasicDBList) first_doc.get("links_pos");

        // pos_list and rot_list length should be always the same
        for (int i = 0; i < pos_list.size(); ++i) {
            trajs.add(new Vector3d(((BasicDBObject) pos_list.get(i)).getDouble("x"),
                    ((BasicDBObject) pos_list.get(i)).getDouble("y"),
                    ((BasicDBObject) pos_list.get(i)).getDouble("z")));
        }
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        this.ViewLinksPositionsAt(start_str, model_name, markerID);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        // get the list of links pos
        BasicDBList pos_list = (BasicDBList) curr_doc.get("links_pos");

        // pos_list and rot_list length should be always the same
        for (int i = 0; i < pos_list.size(); ++i) {
            trajs.add(new Vector3d(((BasicDBObject) pos_list.get(i)).getDouble("x"),
                    ((BasicDBObject) pos_list.get(i)).getDouble("y"),
                    ((BasicDBObject) pos_list.get(i)).getDouble("z")));
        }
    }

    // create the markers
    this.CreateMarkers(trajs, markerID);
}