Revision: 47160 http://brlcad.svn.sourceforge.net/brlcad/?rev=47160&view=rev Author: abhi2011 Date: 2011-10-07 00:43:50 +0000 (Fri, 07 Oct 2011) Log Message: ----------- Began to actually shoot rays towards simulation geometry, needs some debugging(issues during memory cleanup)
Modified Paths: -------------- brlcad/trunk/src/libged/simulate/simrt.c brlcad/trunk/src/libged/simulate/simrt.h brlcad/trunk/src/libged/simulate/simutils.c Modified: brlcad/trunk/src/libged/simulate/simrt.c =================================================================== --- brlcad/trunk/src/libged/simulate/simrt.c 2011-10-06 21:11:04 UTC (rev 47159) +++ brlcad/trunk/src/libged/simulate/simrt.c 2011-10-07 00:43:50 UTC (rev 47160) @@ -27,8 +27,43 @@ /* Private Header */ #include "simrt.h" +/* + * Global lists filled up while raytracing + */ +struct overlap overlap_list; +struct hit_reg hit_list; + int +cleanup_lists(void) +{ + struct overlap *ovp; + struct hit_reg *hrp; + + /* Free all nodes of overlap circularly linked list */ + if (overlap_list.forw != &overlap_list) { + ovp = overlap_list.forw; + while (ovp != &overlap_list) { + bu_free(ovp, "overlap_list"); + ovp = ovp->forw; + } + } + + /* Free all nodes of hit region circularly linked list */ + if (hit_list.forw != &hit_list) { + hrp = hit_list.forw; + while (hrp != &hit_list) { + bu_free(hrp, "hit_list"); + hrp = hrp->forw; + } + } + + return GED_OK; +} + + + +int get_overlap(struct rigid_body *rbA, struct rigid_body *rbB, vect_t overlap_min, vect_t overlap_max) { bu_log("Calculating overlap between BB of %s(%f, %f, %f):(%f,%f,%f) \ @@ -56,10 +91,17 @@ return GED_OK; } - +/* + * Handles hits, records then in a global list + * TODO : Stop the ray after it's left the overlap region which is being currently + * queried. + */ int if_hit(struct application *ap, struct partition *part_headp, struct seg *UNUSED(segs)) { + + struct hit_reg *new_hit_regp; + /* iterating over partitions, this will keep track of the current * partition we're working on. */ @@ -75,7 +117,7 @@ struct curvature cur; /* will contain our hit point coordinate */ - point_t pt; + point_t in_pt, out_pt; /* will contain normal vector where ray enters geometry */ vect_t inormal; @@ -83,94 +125,127 @@ /* will contain normal vector where ray exits geometry */ vect_t onormal; - /* iterate over each partition until we get back to the head. - * each partition corresponds to a specific homogeneous region of - * material. - */ + new_hit_regp = (struct hit_reg *) bu_malloc(sizeof(struct hit_reg), "new_ovlp"); + if(new_hit_regp){ - /* iterate over each partition until we get back to the head. - * each partition corresponds to a specific homogeneous region of - * material. - */ - for (pp=part_headp->pt_forw; pp != part_headp; pp = pp->pt_forw) { + /* iterate over each partition until we get back to the head. + * each partition corresponds to a specific homogeneous region of + * material. + */ - /* print the name of the region we hit as well as the name of - * the primitives encountered on entry and exit. - */ - bu_log("\n--- Hit region %s (in %s, out %s)\n", - pp->pt_regionp->reg_name, - pp->pt_inseg->seg_stp->st_name, - pp->pt_outseg->seg_stp->st_name ); + /* iterate over each partition until we get back to the head. + * each partition corresponds to a specific homogeneous region of + * material. + */ + for (pp=part_headp->pt_forw; pp != part_headp; pp = pp->pt_forw) { - /* entry hit point, so we type less */ - hitp = pp->pt_inhit; + /* print the name of the region we hit as well as the name of + * the primitives encountered on entry and exit. + */ + bu_log("\n--- Hit region %s (in %s, out %s)\n", + pp->pt_regionp->reg_name, + pp->pt_inseg->seg_stp->st_name, + pp->pt_outseg->seg_stp->st_name ); - /* construct the actual (entry) hit-point from the ray and the - * distance to the intersection point (i.e., the 't' value). - */ - VJOIN1(pt, ap->a_ray.r_pt, hitp->hit_dist, ap->a_ray.r_dir); + /* Insert solid data into list node */ + new_hit_regp->reg_name = pp->pt_regionp->reg_name; + new_hit_regp->in_stp = pp->pt_inseg->seg_stp; + new_hit_regp->out_stp = pp->pt_outseg->seg_stp; - /* primitive we encountered on entry */ - stp = pp->pt_inseg->seg_stp; + /* entry hit point, so we type less */ + hitp = pp->pt_inhit; - /* compute the normal vector at the entry point, flipping the - * normal if necessary. - */ - RT_HIT_NORMAL(inormal, hitp, stp, &(ap->a_ray), pp->pt_inflip); + /* construct the actual (entry) hit-point from the ray and the + * distance to the intersection point (i.e., the 't' value). + */ + VJOIN1(in_pt, ap->a_ray.r_pt, hitp->hit_dist, ap->a_ray.r_dir); - /* print the entry hit point info */ - rt_pr_hit(" In", hitp); - VPRINT( " Ipoint", pt); - VPRINT( " Inormal", inormal); + /* primitive we encountered on entry */ + stp = pp->pt_inseg->seg_stp; - if (pp->pt_overlap_reg) { - struct region *pp_reg; - int j = -1; + /* compute the normal vector at the entry point, flipping the + * normal if necessary. + */ + RT_HIT_NORMAL(inormal, hitp, stp, &(ap->a_ray), pp->pt_inflip); - bu_log(" Claiming regions:\n"); - while ((pp_reg = pp->pt_overlap_reg[++j])) - bu_log(" %s\n", pp_reg->reg_name); - } + /* print the entry hit point info */ + rt_pr_hit(" In", hitp); + VPRINT( " Ipoint", in_pt); + VPRINT( " Inormal", inormal); - /* This next macro fills in the curvature information which - * consists on a principle direction vector, and the inverse - * radii of curvature along that direction and perpendicular - * to it. Positive curvature bends toward the outward - * pointing normal. - */ - RT_CURVATURE(&cur, hitp, pp->pt_inflip, stp); - /* print the entry curvature information */ - VPRINT("PDir", cur.crv_pdir); - bu_log(" c1=%g\n", cur.crv_c1); - bu_log(" c2=%g\n", cur.crv_c2); + if (pp->pt_overlap_reg) { + struct region *pp_reg; + int j = -1; - /* exit point, so we type less */ - hitp = pp->pt_outhit; + bu_log(" Claiming regions:\n"); + while ((pp_reg = pp->pt_overlap_reg[++j])) + bu_log(" %s\n", pp_reg->reg_name); + } - /* construct the actual (exit) hit-point from the ray and the - * distance to the intersection point (i.e., the 't' value). - */ - VJOIN1(pt, ap->a_ray.r_pt, hitp->hit_dist, ap->a_ray.r_dir); + /* This next macro fills in the curvature information which + * consists of a principle direction vector, and the inverse + * radii of curvature along that direction and perpendicular + * to it. Positive curvature bends toward the outward + * pointing normal. + */ + RT_CURVATURE(&cur, hitp, pp->pt_inflip, stp); - /* primitive we exited from */ - stp = pp->pt_outseg->seg_stp; + /* print the entry curvature information */ + VPRINT("PDir", cur.crv_pdir); + bu_log(" c1=%g\n", cur.crv_c1); + bu_log(" c2=%g\n", cur.crv_c2); - /* compute the normal vector at the exit point, flipping the - * normal if necessary. - */ - RT_HIT_NORMAL(onormal, hitp, stp, &(ap->a_ray), pp->pt_outflip); + /* Insert the data about the input point into the list */ + VMOVE(new_hit_regp->in_point, in_pt); + VMOVE(new_hit_regp->in_normal, inormal); + new_hit_regp->in_dist = hitp->hit_dist; + new_hit_regp->cur = cur; - /* print the exit hit point info */ - rt_pr_hit(" Out", hitp); - VPRINT( " Opoint", pt); - VPRINT( " Onormal", onormal); + /* exit point, so we type less */ + hitp = pp->pt_outhit; + + /* construct the actual (exit) hit-point from the ray and the + * distance to the intersection point (i.e., the 't' value). + */ + VJOIN1(out_pt, ap->a_ray.r_pt, hitp->hit_dist, ap->a_ray.r_dir); + + /* primitive we exited from */ + stp = pp->pt_outseg->seg_stp; + + /* compute the normal vector at the exit point, flipping the + * normal if necessary. + */ + RT_HIT_NORMAL(onormal, hitp, stp, &(ap->a_ray), pp->pt_outflip); + + /* print the exit hit point info */ + rt_pr_hit(" Out", hitp); + VPRINT( " Opoint", out_pt); + VPRINT( " Onormal", onormal); + + /* Insert the data about the input point into the list */ + VMOVE(new_hit_regp->out_point, out_pt); + VMOVE(new_hit_regp->out_normal, onormal); + new_hit_regp->out_dist = hitp->hit_dist; + + /* Insert the new hit region into the list of hit regions */ + new_hit_regp->forw = hit_list.forw; + new_hit_regp->backw = &hit_list; + new_hit_regp->forw->backw = new_hit_regp; + hit_list.forw = new_hit_regp; + + } } return HIT; } +/** + * + * Handles misses while shooting manifold rays, + * not interested in misses. + */ int if_miss(struct application *UNUSED(ap)) { @@ -180,78 +255,58 @@ /** - * I F _ O V E R L A P * - * Default handler for overlaps in rt_boolfinal(). - * Returns - - * 0 to eliminate partition with overlap entirely - * !0 to retain partition in output list + * Handles overlaps while shooting manifold rays, + * records the overlap regions in a global list * */ int if_overlap(struct application *ap, struct partition *pp, struct region *reg1, struct region *reg2, struct partition *InputHdp) { + struct overlap *new_ovlp; + bu_log("if_overlap: OVERLAP between %s and %s", reg1->reg_name, reg2->reg_name); - fastf_t in_dist; - fastf_t out_dist; - point_t in_point; - point_t out_point; - in_dist = pp->pt_inhit->hit_dist; - out_dist = pp->pt_outhit->hit_dist; - VJOIN1(in_point, ap->a_ray.r_pt, pp->pt_inhit->hit_dist, - ap->a_ray.r_dir); - VJOIN1(out_point, ap->a_ray.r_pt, pp->pt_outhit->hit_dist, - ap->a_ray.r_dir); + new_ovlp = (struct overlap *) bu_malloc(sizeof(struct overlap), "new_ovlp"); + if(new_ovlp){ + new_ovlp->ap = ap; + new_ovlp->pp = pp; + new_ovlp->reg1 = reg1; + new_ovlp->reg2 = reg2; + new_ovlp->in_dist = pp->pt_inhit->hit_dist; + new_ovlp->out_dist = pp->pt_outhit->hit_dist; + VJOIN1(new_ovlp->in_point, ap->a_ray.r_pt, pp->pt_inhit->hit_dist, + ap->a_ray.r_dir); + VJOIN1(new_ovlp->out_point, ap->a_ray.r_pt, pp->pt_outhit->hit_dist, + ap->a_ray.r_dir); - bu_log("if_overlap: Entering at (%f,%f,%f) at distance of %f", V3ARGS(in_point), in_dist); - bu_log("if_overlap: Exiting at (%f,%f,%f) at distance of %f", V3ARGS(out_point), out_dist); + /* Insert the new overlap into the list of overlaps */ + new_ovlp->forw = overlap_list.forw; + new_ovlp->backw = &overlap_list; + new_ovlp->forw->backw = new_ovlp; + overlap_list.forw = new_ovlp; + } + bu_log("if_overlap: Entering at (%f,%f,%f) at distance of %f", + V3ARGS(new_ovlp->in_point), new_ovlp->in_dist); + bu_log("if_overlap: Exiting at (%f,%f,%f) at distance of %f", + V3ARGS(new_ovlp->out_point), new_ovlp->out_dist); + + return rt_defoverlap (ap, pp, reg1, reg2, InputHdp); } - +/* + * Shoots a ray at the simulation geometry and fills up the hit & + * overlap global list + */ int -generate_manifolds(struct ged *gedp, struct simulation_params *sim_params) +shoot_ray(struct rt_i *rtip, point_t pt, point_t dir) { - struct sim_manifold *current_manifold; - struct rigid_body *rb; - vect_t overlap_min, overlap_max; - char *prefix_overlap = "overlap_"; - struct bu_vls overlap_name = BU_VLS_INIT_ZERO; - - /* Raytrace related stuff */ - struct rt_i *rtip; struct application ap; - /* Add all sim objects to raytrace instance */ - /* Make a new rt_i instance from the existing db_i sructure */ - if ((rtip=rt_new_rti(gedp->ged_wdbp->dbip)) == RTI_NULL) { - bu_log("generate_manifolds: rt_new_rti failed while getting new rt instance\n"); - return GED_ERROR; - } - - rtip->useair = 1; - - /* Add all the sim objects to the rt_i - */ - for (rb = sim_params->head_node; rb != NULL; rb = rb->next) { - if (rt_gettree(rtip, rb->rb_namep) < 0) - bu_log("generate_manifolds: Failed to load geometry for [%s]\n", rb->rb_namep); - else - bu_log("generate_manifolds: Added [%s] to raytracer\n", rb->rb_namep); - } - - /* This next call causes some values to be pre-computed, sets up space - * partitioning, computes bounding volumes, etc. - */ - rt_prep_parallel(rtip, 1); - - bu_log("Simulation objects bounding box (%f, %f, %f):(%f,%f,%f)", - V3ARGS(rtip->mdl_min), V3ARGS(rtip->mdl_max)); - /* Initialize the table of resource structures */ /* rt_init_resource(&res_tab, 0, rtip); */ @@ -276,8 +331,8 @@ * set during editing. There are libbu routines for performing * unit conversions if desired. */ - VSET(ap.a_ray.r_pt, 1.0, 1.0, 0.0); - VSET(ap.a_ray.r_dir, -1.0, 0.0, 0.0); + VMOVE(ap.a_ray.r_pt, pt); + VMOVE(ap.a_ray.r_dir, dir); /* Simple debug printing */ VPRINT("Pnt", ap.a_ray.r_pt); @@ -286,7 +341,78 @@ /* Shoot the ray. */ (void)rt_shootray(&ap); + return GED_OK; +} + +/* + * Initialize the simulation scene for raytracing + */ +int +init_raytrace(struct simulation_params *sim_params, struct rt_i *rtip) +{ + struct rigid_body *rb; + + /* Add all sim objects to raytrace instance */ + + /* Add all the sim objects to the rt_i */ + for (rb = sim_params->head_node; rb != NULL; rb = rb->next) { + if (rt_gettree(rtip, rb->rb_namep) < 0) + bu_log("generate_manifolds: Failed to load geometry for [%s]\n", rb->rb_namep); + else + bu_log("generate_manifolds: Added [%s] to raytracer\n", rb->rb_namep); + } + + /* This next call causes some values to be pre-computed, sets up space + * partitioning, computes bounding volumes, etc. + */ + rt_prep_parallel(rtip, 1); + + bu_log("Simulation objects bounding box (%f, %f, %f):(%f,%f,%f)", + V3ARGS(rtip->mdl_min), V3ARGS(rtip->mdl_max)); + + overlap_list.forw = overlap_list.backw = &overlap_list; + hit_list.forw = hit_list.backw = &hit_list; + + return GED_OK; +} + + +int +generate_manifolds(struct ged *gedp, struct simulation_params *sim_params) +{ + struct sim_manifold *current_manifold; + struct rigid_body *rb; + vect_t overlap_min, overlap_max; + char *prefix_overlap = "overlap_"; + struct bu_vls overlap_name = BU_VLS_INIT_ZERO; + + /* Raytrace related stuff */ + struct rt_i *rtip; + point_t r_pt, r_dir; + + /* Make a new rt_i instance from the existing db_i structure */ + if ((rtip=rt_new_rti(gedp->ged_wdbp->dbip)) == RTI_NULL) { + bu_log("generate_manifolds: rt_new_rti failed while getting new rt instance\n"); + return GED_ERROR; + } + rtip->useair = 1; + + init_raytrace(sim_params, rtip); + + VSET(r_pt, 1.0, 1.0, 0.0); + VSET(r_dir, -1.0, 0.0, 0.0); + + shoot_ray(rtip, r_pt, r_dir); + + + /* Traverse the hit list and overlap list */ + /* line(gedp, "ray_test", in_pt, out_pt, 0, 210, 0); */ + + /* Cleanup the overlap and hit lists and free memory */ + cleanup_lists(); + + /* Check all rigid bodies for overlaps using their manifold lists */ for (rb = sim_params->head_node; rb != NULL; rb = rb->next) { Modified: brlcad/trunk/src/libged/simulate/simrt.h =================================================================== --- brlcad/trunk/src/libged/simulate/simrt.h 2011-10-06 21:11:04 UTC (rev 47159) +++ brlcad/trunk/src/libged/simulate/simrt.h 2011-10-07 00:43:50 UTC (rev 47160) @@ -51,9 +51,48 @@ */ #define GRID_GRANULARITY 0.04 +/* + * This structure is a single node of a circularly linked list + * of overlap regions: similar to the one in nirt/usrfrmt.h + */ +struct overlap { + struct application *ap; + struct partition *pp; + struct region *reg1; + struct region *reg2; + fastf_t in_dist; + fastf_t out_dist; + point_t in_point; + point_t out_point; + struct overlap *forw; + struct overlap *backw; +}; + +/* + * This structure is a single node of a circularly linked list + * of hit regions, similar to struct hit from raytrace.h + */ +struct hit_reg { + struct application *ap; + struct partition *pp; + const char *reg_name; + struct soltab *in_stp; + struct soltab *out_stp; + fastf_t in_dist; + fastf_t out_dist; + point_t in_point; + point_t out_point; + vect_t in_normal; + vect_t out_normal; + struct curvature cur; + int hit_surfno; /**< @brief solid-specific surface indicator */ + struct hit_reg *forw; + struct hit_reg *backw; +}; + /** - * Shoots rays within the AABB overlap regions only to allow more rays to be shot + * Shoots rays within the AABB overlap regions only, to allow more rays to be shot * in a grid of finer granularity and to increase performance. */ int Modified: brlcad/trunk/src/libged/simulate/simutils.c =================================================================== --- brlcad/trunk/src/libged/simulate/simutils.c 2011-10-06 21:11:04 UTC (rev 47159) +++ brlcad/trunk/src/libged/simulate/simutils.c 2011-10-07 00:43:50 UTC (rev 47160) @@ -195,32 +195,25 @@ 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; + char *suffix_reg = "_reg"; + struct bu_vls reg_vls = BU_VLS_INIT_ZERO; 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); + bu_vls_sprintf(®_vls, "%s%s", name, suffix_reg); - /* 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); + if (kill(gedp, name) != GED_OK) { + bu_log("line: ERROR Could not delete existing \"%s\"\n", name); return GED_ERROR; } - if (kill(gedp, prefixed_arrow_head) != GED_OK) { - bu_log("line: ERROR Could not delete existing \"%s\"\n", prefixed_arrow_head); + if (kill(gedp, bu_vls_addr(®_vls)) != GED_OK) { + bu_log("line: ERROR Could not delete existing \"%s\"\n", bu_vls_addr(®_vls)); return GED_ERROR; } cmd_args[0] = "in"; - cmd_args[1] = prefixed_arrow_line; + cmd_args[1] = name; cmd_args[2] = "bot"; cmd_args[3] = "3"; cmd_args[4] = "1"; @@ -249,47 +242,14 @@ 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)); + bu_log("line: ERROR Could not draw line \"%s\" (%f,%f,%f)-(%f,%f,%f) \n", + name, V3ARGS(from), V3ARGS(to)); return GED_ERROR; } - add_to_comb(gedp, name, prefixed_arrow_line); + add_to_comb(gedp, bu_vls_addr(®_vls), name); + apply_material(gedp, bu_vls_addr(®_vls), "plastic tr 0.9", r, g, b); - 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; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2dcopy2 _______________________________________________ BRL-CAD Source Commits mailing list brlcad-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/brlcad-commits