bool Bface::view_intersect( CNDCpt& p, // Screen point at which to do intersect Wpt& nearpt, // Point on face visually nearest to p double& dist, // Distance from nearpt to ray from camera double& d2d, // Distance in pixels nearpt to p Wvec& n // "normal" at nearpt in world coordinates ) const { // (Bsimplex virtual method): // Intersection w/ ray from given screen point -- returns the point // on the Bface that is nearest to the given screen space point. // Note: the returned "near point" and "normal" are both // transformed to world space. // Get "eye point" for computing distance. // Not sure if this is the same as cam->from(), // but it seems to be the way it's done in other // intersection code. Wpt eye = XYpt(p); // Make object-space ray: Wline ray = mesh()->inv_xform()*Wline(p); // ray in object space Wpt hit; // Try for exact intersection: double d; if (ray_intersect(ray, hit, d)) { // Direct hit nearpt = mesh()->xform()*hit; dist = nearpt.dist(eye); d2d = PIXEL(nearpt).dist(PIXEL(p)); n = (mesh()->inv_xform().transpose()*norm()).normalized(); return true; } Wpt hit1, hit2, hit3; double d1 = DBL_MAX, d2 = DBL_MAX, d3 = DBL_MAX; Wvec n1, n2, n3; _e1->view_intersect(p, hit1, d, d1, n1); _e2->view_intersect(p, hit2, d, d2, n2); _e3->view_intersect(p, hit3, d, d3, n3); // Rename so d1 represents closest hit if (d1 > d2) { swap(d1, d2); swap(hit1, hit2); swap(n1, n2); } if (d1 > d3) { swap(d1, d3); swap(hit1, hit3); swap(n1, n3); } nearpt = mesh()->xform()*hit1; dist = nearpt.dist(eye); d2d = PIXEL(nearpt).dist(PIXEL(p)); n = n1; return true; }
Wpt QuadtreeNode::farthest_pt(Wpt& p) { Wpt ret = _v1; double max_dist = ret.dist(p); if (_v2.dist(p) > max_dist) { ret = _v2; max_dist = ret.dist(p); } if (_v3.dist(p) > max_dist) { ret = _v3; } return ret; }
bool Bedge::view_intersect( CNDCpt& p, // Screen point at which to do intersect Wpt& nearpt, // Point on edge visually nearest to p double& dist, // Distance from nearpt to ray from camera double& d2d, // Distance in pixels nearpt to p Wvec& n // "normal" at nearpt in world space ) const { // (Bsimplex virtual method): // Intersection w/ ray from given screen point -- returns the point // on the Bedge that is nearest to the given screen space point. // Note: the returned "near point" and "normal" are both // transformed to world space. // Find nearest point on the edge in screen-space, and make a 3D // ray out of it (world space, not object space): Wline ray(NDCline(_v1->ndc(), _v2->ndc()).project_to_seg(p)); // Working in world space (applying mesh xf to verts), find // nearest point on the edge to the ray: nearpt = Wline(_v1->wloc(), _v2->wloc()).project_to_seg(ray); // Compute world and screen distances dist = nearpt.dist(ray.point()); d2d = PIXEL(nearpt).dist(PIXEL(ray.point())); // Return a "normal" vector: Wvec n1; if (nfaces() == 2) n1 = norm(); else if (nfaces() == 1) n1 = get_face()->norm(); else n1 = (ray.point() - nearpt).normalized(); // Transform the normal properly: n = (_mesh->inv_xform().transpose()*n1).normalized(); return 1; }
//! Given an initial slash gesture (or delayed slash) near the //! center of an existing straight Bcurve, set up the widget to //! do a sweep cross-ways to the Bcurve: bool SWEEP_LINE::setup(CGESTUREptr& slash, double dur) { static bool debug = Config::get_var_bool("DEBUG_SWEEP_SETUP",false) || debug_all; err_adv(debug, "SWEEP_LINE::setup"); // check the gesture if (!(slash && slash->straightness() > 0.99)) { err_adv(debug, "SWEEP_LINE::setup: gesture is bad"); return false; } // find the (straight) Bcurve near slash start _curve = Bcurve::hit_ctrl_curve(slash->start()); if (!(_curve && _curve->is_straight())) { err_adv(debug, "SWEEP_LINE::setup: no straight curve at start"); return false; } // find endpoints Bpoint *b1 = _curve->b1(), *b2 = _curve->b2(); assert(b1 && b2); // straight curve must have endpoints // curve cannot be connected to other curves if (b1->vert()->degree() != 1 || b2->vert()->degree() != 1) { err_adv(debug, "SWEEP_LINE::setup: curve is not isolated"); return false; } // ensure the gesture starts near the center of the straight line Bcurve: { PIXEL a = b1->vert()->pix(); PIXEL b = b2->vert()->pix(); double t = (slash->start() - a).tlen(b-a); if (t < 0.35 || t > 0.65) { err_adv(debug, "SWEEP_LINE::setup: gesture not near center of line"); return false; } } // find the plane to work in _plane = check_plane(shared_plane(b1, b2)); if (!_plane.is_valid()) { err_adv(debug, "SWEEP_LINE::setup: no valid plane"); return false; } // check that slash is perpendicular to line Wpt a = b1->loc(); // endpoint at b1 Wpt b = b2->loc(); // endpoint at b2 Wvec t = b - a; // vector from endpt a to endpt b Wpt o = a + t/2; // center of straight line curve Wvec n = cross(_plane.normal(), t); // direction across line ab Wvec slash_vec = endpt_vec(slash, _plane); const double ALIGN_ANGLE_THRESH = 15; double angle = rad2deg(slash_vec.angle(n)); if (angle > 90) { angle = 180 - angle; n = -n; } if (angle > ALIGN_ANGLE_THRESH) { err_adv(debug, "SWEEP_LINE::setup: slash is not perpendicular to line"); err_adv(debug, " angle: %f", angle); return false; } // compute guideline endpoint: Wpt endpt = o + n.normalized()*a.dist(b); return SWEEP_BASE::setup(_curve->mesh(), o, endpt, dur); }