Revision: 47090
          http://brlcad.svn.sourceforge.net/brlcad/?rev=47090&view=rev
Author:   abhi2011
Date:     2011-10-05 12:24:15 +0000 (Wed, 05 Oct 2011)
Log Message:
-----------
Number of functions hitting the roof, moving utility stuff out to seperate file

Added Paths:
-----------
    brlcad/trunk/src/libged/simulate/simutils.c

Added: brlcad/trunk/src/libged/simulate/simutils.c
===================================================================
--- brlcad/trunk/src/libged/simulate/simutils.c                         (rev 0)
+++ brlcad/trunk/src/libged/simulate/simutils.c 2011-10-05 12:24:15 UTC (rev 
47090)
@@ -0,0 +1,726 @@
+/*                         S I M U T I L S . C
+ * BRL-CAD
+ *
+ * Copyright (c) 2011 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+/*
+ * Utilities for the simulate command.
+ *
+ * Used for creating bounding boxes, applying materials, arrows etc.
+ *
+ */
+
+/* Private Header */
+#include "simutils.h"
+
+#define NORMAL_SCALE_FACTOR 1
+#define MAX_FLOATING_POINT_STRLEN 20
+#define ARROW_BASE_RADIUS 0.02
+#define ARROW_TIP_RADIUS  0.001
+
+
+void
+print_usage(struct bu_vls *str)
+{
+    bu_vls_printf(str, "Usage: simulate <steps>\n\n");
+    bu_vls_printf(str, "Currently this command adds all regions in the model 
database to a \n\
+    simulation having only gravity as a force. The objects should fall towards 
the ground plane XY.\n");
+    bu_vls_printf(str, "The positions of the regions are set after <steps> 
number of simulation steps.\n");
+    bu_vls_printf(str, "-f <n> <x> <y> <z>\t- Specifies frequency of update(eg 
1/60 Hz)(WIP)\n");
+    bu_vls_printf(str, "-t <x> <y> <z>\t\t  - Specifies time for which to 
run(alternative to -n)(WIP)\n");
+    return;
+}
+
+
+void
+print_matrix(char *rb_namep, mat_t t)
+{
+    int i, j;
+    struct bu_vls buffer = BU_VLS_INIT_ZERO;
+
+    bu_vls_sprintf(&buffer, "------------Transformation 
matrix(%s)--------------\n",
+                  rb_namep);
+
+    for (i=0 ; i<4 ; i++) {
+       for (j=0 ; j<4 ; j++) {
+           bu_vls_sprintf(&buffer, "t[%d]: %f\t", (i*4 + j), t[i*4 + j]);
+       }
+       bu_vls_strcat(&buffer, "\n");
+    }
+
+    bu_vls_strcat(&buffer, 
"-------------------------------------------------------\n");
+    bu_log("%V", &buffer);
+    bu_vls_free(&buffer);
+}
+
+
+void
+print_rigid_body(struct rigid_body *rb)
+{
+    bu_log("print_rigid_body : \"%s\", state = %d\n", rb->rb_namep, rb->state);
+}
+
+
+void
+print_manifold_list(struct rigid_body *rb)
+{
+    struct sim_manifold *current_manifold;
+    int i;
+
+    bu_log("print_manifold_list: %s\n", rb->rb_namep);
+
+    for (current_manifold = rb->first_manifold; current_manifold != NULL;
+        current_manifold = current_manifold->next) {
+
+       bu_log("--Manifold between %s & %s has %d contacts--\n",
+              current_manifold->rbA->rb_namep,
+              current_manifold->rbB->rb_namep,
+              current_manifold->num_contacts);
+
+       for (i=0; i<current_manifold->num_contacts; i++) {
+           bu_log("%d, (%f, %f, %f):(%f, %f, %f), n(%f, %f, %f)\n",
+                  i+1,
+                  V3ARGS(current_manifold->rb_contacts[i].ptA),
+                  V3ARGS(current_manifold->rb_contacts[i].ptB),
+                  V3ARGS(current_manifold->rb_contacts[i].normalWorldOnB));
+       }
+    }
+}
+
+
+void
+print_command(char* cmd_args[], int num_args)
+{
+    int i;
+    char buffer[500] = "";
+    for (i=0; i<num_args; i++) {
+       sprintf(buffer, "%s %s", buffer, cmd_args[i]);
+    }
+
+    bu_log(buffer);
+}
+
+
+char*
+prefix_name(char *prefix, char *original)
+{
+    /* Prepare prefixed bounding box primitive name */
+    size_t prefix_len, prefixed_name_len;
+    char *prefixed_name;
+
+    prefix_len = strlen(prefix);
+    prefixed_name_len = strlen(prefix)+strlen(original) + 1;
+    prefixed_name = (char *)bu_malloc(prefixed_name_len, "Adding prefix");
+    bu_strlcpy(prefixed_name, prefix, prefix_len + 1);
+    bu_strlcat(prefixed_name + prefix_len, original,
+              prefixed_name_len - prefix_len);
+    return prefixed_name;
+}
+
+
+int
+kill(struct ged *gedp, char *name)
+{
+    char *cmd_args[5];
+
+    /* Check if the duplicate already exists, and kill it if so */
+    if (db_lookup(gedp->ged_wdbp->dbip, name, LOOKUP_QUIET) != RT_DIR_NULL) {
+       bu_log("kill: WARNING \"%s\" exists, deleting it\n", name);
+       cmd_args[0] = "kill";
+       cmd_args[1] = name;
+       cmd_args[2] = (char *)0;
+
+       if (ged_kill(gedp, 2, (const char **)cmd_args) != GED_OK) {
+           bu_log("kill: ERROR Could not delete existing \"%s\"\n", name);
+           return GED_ERROR;
+       }
+    }
+
+    return GED_OK;
+}
+
+
+int
+kill_copy(struct ged *gedp, struct directory *dp, char* new_name)
+{
+    char *cmd_args[5];
+    int rv;
+
+    if (kill(gedp, new_name) != GED_OK) {
+       bu_log("kill_copy: ERROR Could not delete existing \"%s\"\n", new_name);
+       return GED_ERROR;
+    }
+
+    /* Copy the passed prim/comb */
+    cmd_args[0] = "copy";
+    cmd_args[1] = dp->d_namep;
+    cmd_args[2] = new_name;
+    cmd_args[3] = (char *)0;
+    rv = ged_copy(gedp, 3, (const char **)cmd_args);
+    if (rv != GED_OK) {
+       bu_log("kill_copy: ERROR Could not copy \"%s\" to \"%s\"\n", 
dp->d_namep,
+              new_name);
+       return GED_ERROR;
+    }
+
+    return GED_OK;
+}
+
+
+int
+add_to_comb(struct ged *gedp, char *target, char *add)
+{
+    char *cmd_args[5];
+    int rv;
+
+    cmd_args[0] = "comb";
+    cmd_args[1] = target;
+    cmd_args[2] = "u";
+    cmd_args[3] = add;
+    cmd_args[4] = (char *)0;
+    rv = ged_comb(gedp, 4, (const char **)cmd_args);
+    if (rv != GED_OK) {
+        bu_log("add_to_comb: ERROR Could not add \"%s\" to the combination 
\"%s\"\n",
+              add, target);
+        return GED_ERROR;
+    }
+
+    return GED_OK;
+}
+
+
+int
+arrow(struct ged *gedp, char* name, point_t from, point_t to)
+{
+    char *cmd_args[20];
+    int rv;
+    char buffer_str[MAX_FLOATING_POINT_STRLEN];
+    char *prefix_arrow_line = "arrow_line_";
+    char *prefix_arrow_head = "arrow_head_";
+    struct bu_vls arrow_line_vls = BU_VLS_INIT_ZERO, arrow_head_vls = 
BU_VLS_INIT_ZERO;
+    char *prefixed_arrow_line, *prefixed_arrow_head;
+    vect_t v;
+
+    /* Arrow line primitive name */
+    bu_vls_sprintf(&arrow_line_vls, "%s%s", prefix_arrow_line, name);
+    prefixed_arrow_line = bu_vls_addr(&arrow_line_vls);
+
+    /* Arrow line primitive name */
+    bu_vls_sprintf(&arrow_head_vls, "%s%s", prefix_arrow_head, name);
+    prefixed_arrow_head = bu_vls_addr(&arrow_head_vls);
+
+    if (kill(gedp, prefixed_arrow_line) != GED_OK) {
+       bu_log("line: ERROR Could not delete existing \"%s\"\n", 
prefixed_arrow_line);
+       return GED_ERROR;
+    }
+
+    if (kill(gedp, prefixed_arrow_head) != GED_OK) {
+       bu_log("line: ERROR Could not delete existing \"%s\"\n", 
prefixed_arrow_head);
+       return GED_ERROR;
+    }
+
+    cmd_args[0] = "in";
+    cmd_args[1] = prefixed_arrow_line;
+    cmd_args[2] = "bot";
+    cmd_args[3] = "3";
+    cmd_args[4] = "1";
+    cmd_args[5] = "1";
+    cmd_args[6] = "1";
+
+    sprintf(buffer_str, "%f", from[0]); cmd_args[7] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", from[1]); cmd_args[8] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", from[2]); cmd_args[9] = bu_strdup(buffer_str);
+
+    sprintf(buffer_str, "%f", to[0]); cmd_args[10] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", to[1]); cmd_args[11] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", to[2]); cmd_args[12] = bu_strdup(buffer_str);
+
+    sprintf(buffer_str, "%f", from[0]); cmd_args[13] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", from[1]); cmd_args[14] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", from[2]); cmd_args[15] = bu_strdup(buffer_str);
+
+    cmd_args[16] = "0";
+    cmd_args[17] = "1";
+    cmd_args[18] = "2";
+
+    cmd_args[19] = (char *)0;
+
+    print_command(cmd_args, 19);
+
+    rv = ged_in(gedp, 19, (const char **)cmd_args);
+    if (rv != GED_OK) {
+       bu_log("line: ERROR Could not draw arrow line \"%s\" 
(%f,%f,%f)-(%f,%f,%f) \n",
+              prefixed_arrow_line, V3ARGS(from), V3ARGS(to));
+       return GED_ERROR;
+    }
+
+    add_to_comb(gedp, name, prefixed_arrow_line);
+
+    VSUB2(v, to, from);
+    VUNITIZE(v);
+    VSCALE(v, v, 0.1);
+    bu_log("line: Unit vector (%f,%f,%f)\n", V3ARGS(v));
+
+    cmd_args[0] = "in";
+    cmd_args[1] = prefixed_arrow_head;
+    cmd_args[2] = "trc";
+
+    sprintf(buffer_str, "%f", to[0]); cmd_args[3] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", to[1]); cmd_args[4] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", to[2]); cmd_args[5] = bu_strdup(buffer_str);
+
+    sprintf(buffer_str, "%f", v[0]); cmd_args[6] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", v[1]); cmd_args[7] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", v[2]); cmd_args[8] = bu_strdup(buffer_str);
+
+
+    sprintf(buffer_str, "%f", ARROW_BASE_RADIUS); cmd_args[9] = 
bu_strdup(buffer_str);
+    sprintf(buffer_str, "%f", ARROW_TIP_RADIUS);  cmd_args[10] = 
bu_strdup(buffer_str);
+
+    cmd_args[11] = (char *)0;
+
+    print_command(cmd_args, 11);
+
+    rv = ged_in(gedp, 11, (const char **)cmd_args);
+    if (rv != GED_OK) {
+       bu_log("line: ERROR Could not draw arrow head \"%s\" 
(%f,%f,%f)-(%f,%f,%f) \n",
+              prefixed_arrow_head, V3ARGS(from), V3ARGS(to));
+       return GED_ERROR;
+    }
+
+    add_to_comb(gedp, name, prefixed_arrow_head);
+
+    return GED_OK;
+}
+
+
+int
+apply_material(struct ged *gedp,
+              char* comb,
+              char* material,
+              unsigned char r,
+              unsigned char g,
+              unsigned char b)
+{
+    int rv;
+    char buffer_str[MAX_FLOATING_POINT_STRLEN];
+    char* cmd_args[28];
+
+    cmd_args[0] = "mater";
+    cmd_args[1] = comb;
+    cmd_args[2] = material;
+
+    sprintf(buffer_str, "%d", r); cmd_args[3] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%d", g); cmd_args[4] = bu_strdup(buffer_str);
+    sprintf(buffer_str, "%d", b); cmd_args[5] = bu_strdup(buffer_str);
+
+    cmd_args[6] = "0";
+    cmd_args[7] = (char *)0;
+
+    rv = ged_mater(gedp, 7, (const char **)cmd_args);
+    if (rv != GED_OK) {
+       bu_log("apply_material: WARNING Could not adjust the material to %s(%d, 
%d, %d) for \"%s\"\n",
+              material, r, g, b, comb);
+       return GED_ERROR;
+    }
+
+    return GED_OK;
+}
+
+
+int
+apply_color(struct ged *gedp,
+           char* name,
+           unsigned char r,
+           unsigned char g,
+           unsigned char b)
+{
+    struct directory *dp = NULL;
+    struct rt_comb_internal *comb = NULL;
+    struct rt_db_internal intern;
+    struct bu_attribute_value_set avs;
+
+    /* Look up directory pointer for the passed comb name */
+    GED_DB_LOOKUP(gedp, dp, name, LOOKUP_NOISY, GED_ERROR);
+    GED_CHECK_COMB(gedp, dp, GED_ERROR);
+    GED_DB_GET_INTERNAL(gedp, &intern, dp, (fastf_t *)NULL, &rt_uniresource, 
GED_ERROR);
+
+    /* Get a comb from the internal format */
+    comb = (struct rt_comb_internal *)intern.idb_ptr;
+    RT_CK_COMB(comb);
+
+    /* Set the color related members */
+    comb->rgb[0] = r;
+    comb->rgb[1] = g;
+    comb->rgb[2] = b;
+    comb->rgb_valid = 1;
+    comb->inherit = 0;
+
+    /* Get the current attribute set of the comb from the db */
+    bu_avs_init_empty(&avs);
+    if (db5_get_attributes(gedp->ged_wdbp->dbip, &avs, dp)) {
+       bu_vls_printf(gedp->ged_result_str, "apply_transforms: ERROR Cannot get 
attributes for object %s\n", dp->d_namep);
+       bu_avs_free(&avs);
+       return GED_ERROR;
+    }
+
+    /* Sync the changed attributes with the old ones */
+    db5_standardize_avs(&avs);
+    db5_sync_comb_to_attr(&avs, comb);
+    db5_standardize_avs(&avs);
+
+    /* Put back in db to allow drawing */
+    GED_DB_PUT_INTERNAL(gedp, dp, &intern, &rt_uniresource, GED_ERROR);
+    if (db5_update_attributes(dp, &avs, gedp->ged_wdbp->dbip)) {
+       bu_vls_printf(gedp->ged_result_str, "apply_transforms: ERROR failed to 
update attributes\n");
+       bu_avs_free(&avs);
+       return GED_ERROR;
+    }
+
+    bu_avs_free(&avs);
+    return GED_OK;
+}
+
+
+int
+insert_AABB(struct ged *gedp, struct simulation_params *sim_params, struct 
rigid_body *current_node)
+{
+    char* cmd_args[28];
+    char buffer[MAX_FLOATING_POINT_STRLEN];
+    int rv;
+    char *prefix = "bb_";
+    char *prefix_reg = "bb_reg_";
+    char *prefixed_name, *prefixed_reg_name;
+    point_t v;
+
+    /* Prepare prefixed bounding box primitive name */
+    prefixed_name = prefix_name(prefix, current_node->rb_namep);
+
+    /* Prepare prefixed bounding box region name */
+    prefixed_reg_name = prefix_name(prefix_reg, current_node->rb_namep);
+
+    /* Delete existing bb prim and region */
+    rv = kill(gedp, prefixed_name);
+    if (rv != GED_OK) {
+       bu_log("insertAABB: ERROR Could not delete existing bounding box arb8 : 
%s \
+               so NOT attempting to add new bounding box\n", prefixed_name);
+       return GED_ERROR;
+    }
+
+    rv = kill(gedp, prefixed_reg_name);
+    if (rv != GED_OK) {
+       bu_log("insertAABB: ERROR Could not delete existing bounding box region 
: %s \
+               so NOT attempting to add new region\n", prefixed_reg_name);
+       return GED_ERROR;
+    }
+
+    /* Setup the simulation result group union-ing the new objects */
+    cmd_args[0] = "in";
+    cmd_args[1] = bu_strdup(prefixed_name);
+    cmd_args[2] = "arb8";
+
+    /* Front face vertices */
+    /* v1 */
+    v[0] = current_node->btbb_center[0] + current_node->btbb_dims[0]/2;
+    v[1] = current_node->btbb_center[1] + current_node->btbb_dims[1]/2;
+    v[2] = current_node->btbb_center[2] - current_node->btbb_dims[2]/2;
+    sprintf(buffer, "%f", v[0]); cmd_args[3] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[1]); cmd_args[4] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[2]); cmd_args[5] = bu_strdup(buffer);
+
+    /* v2 */
+    v[0] = current_node->btbb_center[0] + current_node->btbb_dims[0]/2;
+    v[1] = current_node->btbb_center[1] + current_node->btbb_dims[1]/2;
+    v[2] = current_node->btbb_center[2] + current_node->btbb_dims[2]/2;
+    sprintf(buffer, "%f", v[0]); cmd_args[6] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[1]); cmd_args[7] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[2]); cmd_args[8] = bu_strdup(buffer);
+
+    /* v3 */
+    v[0] = current_node->btbb_center[0] + current_node->btbb_dims[0]/2;
+    v[1] = current_node->btbb_center[1] - current_node->btbb_dims[1]/2;
+    v[2] = current_node->btbb_center[2] + current_node->btbb_dims[2]/2;
+    sprintf(buffer, "%f", v[0]); cmd_args[9]  = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[1]); cmd_args[10] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[2]); cmd_args[11] = bu_strdup(buffer);
+
+    /* v4 */
+    v[0] = current_node->btbb_center[0] + current_node->btbb_dims[0]/2;
+    v[1] = current_node->btbb_center[1] - current_node->btbb_dims[1]/2;
+    v[2] = current_node->btbb_center[2] - current_node->btbb_dims[2]/2;
+    sprintf(buffer, "%f", v[0]); cmd_args[12] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[1]); cmd_args[13] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[2]); cmd_args[14] = bu_strdup(buffer);
+
+    /* Back face vertices */
+    /* v5 */
+    v[0] = current_node->btbb_center[0] - current_node->btbb_dims[0]/2;
+    v[1] = current_node->btbb_center[1] + current_node->btbb_dims[1]/2;
+    v[2] = current_node->btbb_center[2] - current_node->btbb_dims[2]/2;
+    sprintf(buffer, "%f", v[0]); cmd_args[15] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[1]); cmd_args[16] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[2]); cmd_args[17] = bu_strdup(buffer);
+
+    /* v6 */
+    v[0] = current_node->btbb_center[0] - current_node->btbb_dims[0]/2;
+    v[1] = current_node->btbb_center[1] + current_node->btbb_dims[1]/2;
+    v[2] = current_node->btbb_center[2] + current_node->btbb_dims[2]/2;
+    sprintf(buffer, "%f", v[0]); cmd_args[18] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[1]); cmd_args[19] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[2]); cmd_args[20] = bu_strdup(buffer);
+
+    /* v7 */
+    v[0] = current_node->btbb_center[0] - current_node->btbb_dims[0]/2;
+    v[1] = current_node->btbb_center[1] - current_node->btbb_dims[1]/2;
+    v[2] = current_node->btbb_center[2] + current_node->btbb_dims[2]/2;
+    sprintf(buffer, "%f", v[0]); cmd_args[21] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[1]); cmd_args[22] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[2]); cmd_args[23] = bu_strdup(buffer);
+
+    /* v8 */
+    v[0] = current_node->btbb_center[0] - current_node->btbb_dims[0]/2;
+    v[1] = current_node->btbb_center[1] - current_node->btbb_dims[1]/2;
+    v[2] = current_node->btbb_center[2] - current_node->btbb_dims[2]/2;
+    sprintf(buffer, "%f", v[0]); cmd_args[24] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[1]); cmd_args[25] = bu_strdup(buffer);
+    sprintf(buffer, "%f", v[2]); cmd_args[26] = bu_strdup(buffer);
+
+    /* Finally make the bb primitive, phew ! */
+    cmd_args[27] = (char *)0;
+    rv = ged_in(gedp, 27, (const char **)cmd_args);
+    if (rv != GED_OK) {
+       bu_log("insertAABB: WARNING Could not draw bounding box for \"%s\"\n",
+              current_node->rb_namep);
+    }
+
+    /* Make the region for the bb primitive */
+    add_to_comb(gedp, prefixed_reg_name, prefixed_name);
+
+    /* Adjust the material for region to be almost transparent */
+    apply_material(gedp, prefixed_reg_name, "plastic tr 0.9", 210, 0, 100);
+
+
+    /* Add the region to the result of the sim so it will be drawn too */
+    add_to_comb(gedp, sim_params->sim_comb_name, prefixed_reg_name);
+
+    bu_free(prefixed_name, "simulate : prefixed_name");
+    bu_free(prefixed_reg_name, "simulate : prefixed_reg_name");
+
+    return GED_OK;
+
+}
+
+
+int
+insert_manifolds(struct ged *gedp, struct simulation_params *sim_params, 
struct rigid_body *rb)
+{
+    char* cmd_args[28];
+    char buffer[20];
+    int rv, num_args;
+    char *prefixed_name, *prefixed_reg_name, *prefixed_normal_name;
+    char *prefix = "mf_";
+    char *prefix_reg = "mf_reg_";
+    char *prefix_normal = "normal_";
+    struct bu_vls buffer_vls = BU_VLS_INIT_ZERO;
+    char *name;
+    vect_t scaled_normal;
+    point_t from, to;
+    struct sim_manifold *current_manifold;
+    int i;
+
+    for (current_manifold = rb->first_manifold; current_manifold != NULL;
+        current_manifold = current_manifold->next) {
+
+
+       if(current_manifold->num_contacts > 0){
+
+           /* Prepare prefixed bounding box primitive name */
+           bu_vls_sprintf(&buffer_vls, "%s_%s", 
current_manifold->rbA->rb_namep,
+                          current_manifold->rbB->rb_namep);
+           name = bu_vls_addr(&buffer_vls);
+
+           /* Prepare the manifold shape name */
+           prefixed_name = prefix_name(prefix, name);
+
+           /* Prepare prefixed manifold region name */
+           prefixed_reg_name = prefix_name(prefix_reg, name);
+
+           /* Prepare prefixed manifold region name */
+           prefixed_normal_name = prefix_name(prefix_normal, name);
+
+           /* Delete existing manifold prim and region */
+           rv = kill(gedp, prefixed_name);
+           if (rv != GED_OK) {
+               bu_log("insert_manifolds: ERROR Could not delete existing 
bounding box arb8 : %s \
+                                       so NOT attempting to add new bounding 
box\n", prefixed_name);
+               return GED_ERROR;
+           }
+
+           rv = kill(gedp, prefixed_reg_name);
+           if (rv != GED_OK) {
+               bu_log("insert_manifolds: ERROR Could not delete existing 
bounding box region : %s \
+                                       so NOT attempting to add new region\n", 
prefixed_reg_name);
+               return GED_ERROR;
+           }
+
+           /* Setup the simulation result group union-ing the new objects */
+           cmd_args[0] = "in";
+           cmd_args[1] = bu_strdup(prefixed_name);
+           cmd_args[2] = (char *)0;
+           num_args = 2;
+
+           switch(current_manifold->num_contacts) {
+               case 1:
+                   bu_log("1 contact got, no manifold drawn");
+                   break;
+
+               case 2:
+                   cmd_args[2] = "arb4";
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[0].ptA[0]);
+                   cmd_args[3] = bu_strdup(buffer);
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[0].ptA[1]);
+                   cmd_args[4] = bu_strdup(buffer);
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[0].ptA[2]);
+                   cmd_args[5] = bu_strdup(buffer);
+
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[1].ptA[0]);
+                   cmd_args[6] = bu_strdup(buffer);
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[1].ptA[1]);
+                   cmd_args[7] = bu_strdup(buffer);
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[1].ptA[2]);
+                   cmd_args[8] = bu_strdup(buffer);
+
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[1].ptB[0]);
+                   cmd_args[9] = bu_strdup(buffer);
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[1].ptB[1]);
+                   cmd_args[10] = bu_strdup(buffer);
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[1].ptB[2]);
+                   cmd_args[11] = bu_strdup(buffer);
+
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[0].ptB[0]);
+                   cmd_args[12] = bu_strdup(buffer);
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[0].ptB[1]);
+                   cmd_args[13] = bu_strdup(buffer);
+                   sprintf(buffer, "%f", 
current_manifold->rb_contacts[0].ptB[2]);
+                   cmd_args[14] = bu_strdup(buffer);
+
+                   cmd_args[15] = (char *)0;
+                   num_args = 15;
+
+                   VADD2SCALE(from, current_manifold->rb_contacts[0].ptA,
+                              current_manifold->rb_contacts[1].ptB, 0.5);
+                   break;
+
+               case 3:
+                   bu_log("3 contacts got, no manifold drawn");
+                   break;
+
+               case 4:
+                   cmd_args[2] = "arb8";
+                   for (i=0; i<4; i++) {
+                       sprintf(buffer, "%f", 
current_manifold->rb_contacts[i].ptA[0]);
+                       cmd_args[3+i*3] = bu_strdup(buffer);
+                       sprintf(buffer, "%f", 
current_manifold->rb_contacts[i].ptA[1]);
+                       cmd_args[4+i*3] = bu_strdup(buffer);
+                       sprintf(buffer, "%f", 
current_manifold->rb_contacts[i].ptA[2]);
+                       cmd_args[5+i*3] = bu_strdup(buffer);
+
+                       sprintf(buffer, "%f", 
current_manifold->rb_contacts[i].ptB[0]);
+                       cmd_args[15+i*3] = bu_strdup(buffer);
+                       sprintf(buffer, "%f", 
current_manifold->rb_contacts[i].ptB[1]);
+                       cmd_args[16+i*3] = bu_strdup(buffer);
+                       sprintf(buffer, "%f", 
current_manifold->rb_contacts[i].ptB[2]);
+                       cmd_args[17+i*3] = bu_strdup(buffer);
+
+                       /* current_manifold->rb_contacts[i].ptA[0],
+                          current_manifold->rb_contacts[i].ptA[1],
+                          current_manifold->rb_contacts[i].ptA[2],
+                          current_manifold->rb_contacts[i].ptB[0],
+                          current_manifold->rb_contacts[i].ptB[1],
+                          current_manifold->rb_contacts[i].ptB[2],
+                          current_manifold->rb_contacts[i].normalWorldOnB[0],
+                          current_manifold->rb_contacts[i].normalWorldOnB[1],
+                          current 
cmd_argsrent_manifold->rb_contacts[i].normalWorldOnB[2]);*/
+                   }
+                   cmd_args[27] = (char *)0;
+                   num_args = 27;
+
+                   VADD2SCALE(from, current_manifold->rb_contacts[0].ptA,
+                              current_manifold->rb_contacts[2].ptB, 0.5);
+                   break;
+
+               default:
+                   bu_log("%d contacts got, no manifold drawn", 
current_manifold->num_contacts);
+                   cmd_args[2] = (char *)0;
+                   num_args = 2;
+           }
+
+           print_command(cmd_args, num_args);
+
+           /* Finally make the manifold primitive, if proper command generated 
*/
+           if (num_args > 2) {
+               rv = ged_in(gedp, num_args, (const char **)cmd_args);
+               if (rv != GED_OK) {
+                   bu_log("insert_manifolds: WARNING Could not draw manifold 
for \"%s\"\n", rb->rb_namep);
+               }
+
+               /* Make the region for the manifold primitive */
+               add_to_comb(gedp, prefixed_reg_name, prefixed_name);
+
+               /* Adjust the material for region to be visible */
+               apply_material(gedp, prefixed_reg_name, "plastic tr 0.9", 210, 
210, 0);
+
+               /* Add the region to the result of the sim so it will be drawn 
too */
+               add_to_comb(gedp, sim_params->sim_comb_name, prefixed_reg_name);
+
+               /* Finally draw the normal */
+               VSCALE(scaled_normal, 
current_manifold->rb_contacts[0].normalWorldOnB, NORMAL_SCALE_FACTOR);
+               VADD2(to, scaled_normal, from);
+
+               bu_log("insert_manifolds: line (%f,%f,%f)-> (%f,%f,%f)-> 
(%f,%f,%f) \n",
+                      V3ARGS(current_manifold->rb_contacts[0].normalWorldOnB),
+                      V3ARGS(to),
+                      V3ARGS(scaled_normal));
+
+               arrow(gedp, prefixed_normal_name, from, to);
+               add_to_comb(gedp, sim_params->sim_comb_name, 
prefixed_normal_name);
+
+           }/*  if-num_args */
+
+           bu_free(prefixed_name, "simulate : prefixed_name");
+           bu_free(prefixed_reg_name, "simulate : prefixed_reg_name");
+
+       }/* if-num_contacts */
+
+    } /* end for-manifold */
+
+
+    return GED_OK;
+
+}
+
+
+/*
+ * Local Variables:
+ * tab-width: 8
+ * mode: C
+ * c-basic-offset: 4
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */


Property changes on: brlcad/trunk/src/libged/simulate/simutils.c
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
BRL-CAD Source Commits mailing list
brlcad-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to