void ColorPalette::identify(const sf::VertexArray& canvas, double x, double y) { sf::Uint8* comps[3]; int i; comps[0] = &color.r; comps[1] = &color.g; comps[2] = &color.b; for(i=0;i<3;++i) { double x1 = canvas[i].position.x, y1 = canvas[i].position.y; double a = (y1-y)/(x1-x); double b = y1-x1*((y1-y)/(x1-x)); double c = angular[(i+1)%3]; double d = linear[(i+1)%3]; double x2 = (d-b)/(a-c); double y2 = a*x2+b; double AP = distSq(x1, y1, x, y); double PQ = distSq(x, y, x2, y2); *comps[i] = 255-(255*AP/(AP+PQ)); } }
/// square distance between point and line, ray or segment double ptLineDistSq(point s1, point s2, point p, int type){ double pd2 = distSq(s1, s2); point r; if(pd2 == 0) r = s1; else{ double u = dot(p-s1, s2-s1) / pd2; r = s1 + (s2 - s1)*u; if(type != LINE && u < 0.0) r = s1; if(type == SEGMENT && u > 1.0) r = s2; } return distSq(r, p); }
inline const double diffDist(const NumericVector& iv, const NumericVector& gprime){ NumericVector distSq(gprime.size()); for(int i=0;i<gprime.size();i++){ distSq[i]=pow(iv[i]-gprime[i],2); } double sm=std::accumulate(distSq.begin(),distSq.end(),0.0); return sqrt(sm); }
bool minLineBetweenLineSegments( ofPoint pt1, ofPoint pt2, ofPoint pt3, ofPoint pt4, ofPoint * pA, ofPoint * pB) { // check if they intersect if( intersectionTwoLines(pt1, pt2, pt3, pt4, pA) ) { pB->x = pA->x; pB->y = pA->y; return true; }else{ vector<float>distSq(4); ofPoint s[4]; ofPoint e[4]; e[0] = pt3; e[1] = pt4; e[2] = pt1; e[3] = pt2; s[0] = distanceToSegment(pt1, pt2, pt3); s[1] = distanceToSegment(pt1, pt2, pt4); s[2] = distanceToSegment(pt3, pt4, pt1); s[3] = distanceToSegment(pt3, pt4, pt2); distSq[0] = (s[0].x-pt3.x)*(s[0].x-pt3.x)+(s[0].y-pt3.y)*(s[0].y-pt3.y); distSq[1] = (s[1].x-pt4.x)*(s[1].x-pt4.x)+(s[1].y-pt4.y)*(s[1].y-pt4.y); distSq[2] = (s[2].x-pt1.x)*(s[2].x-pt1.x)+(s[2].y-pt1.y)*(s[2].y-pt1.y); distSq[3] = (s[3].x-pt2.x)*(s[3].x-pt2.x)+(s[3].y-pt2.y)*(s[3].y-pt2.y); int close = 0; float closeD = 0; for( int i = 0; i < 4; i++) { if( i == 0 || distSq[i] < closeD) { close = i; closeD = distSq[i]; } } pA->x = s[close].x; pA->y = s[close].y; pB->x = e[close].x; pB->y = e[close].y; return false; } }
// returns the index of the glyph closest to the right of the given coordinates // (i.e. when over the right half of a glyph, the returned index will be for the // glyph following it, which will be the first glyph (not) to be selected) int TextSelection::FindClosestGlyph(int pageNo, double x, double y) { int textLen; RectI* coords; textCache->GetData(pageNo, &textLen, &coords); PointD pt = PointD(x, y); unsigned int maxDist = UINT_MAX; PointI pti = pt.ToInt(); bool overGlyph = false; int result = -1; for (int i = 0; i < textLen; i++) { if (!coords[i].x && !coords[i].dx) continue; if (overGlyph && !coords[i].Contains(pti)) continue; unsigned int dist = distSq((int)x - coords[i].x - coords[i].dx / 2, (int)y - coords[i].y - coords[i].dy / 2); if (dist < maxDist) { result = i; maxDist = dist; } // prefer glyphs the cursor is actually over if (!overGlyph && coords[i].Contains(pti)) { overGlyph = true; result = i; maxDist = dist; } } if (-1 == result) return 0; CrashIf(result < 0 || result >= textLen); // the result indexes the first glyph to be selected in a forward selection RectD bbox = engine->Transform(coords[result].Convert<double>(), pageNo, 1.0, 0); pt = engine->Transform(pt, pageNo, 1.0, 0); if (pt.x > bbox.x + 0.5 * bbox.dx) { result++; // for some (DjVu) documents, all glyphs of a word share the same bbox while (result < textLen && coords[result - 1] == coords[result]) result++; } CrashIf(result > 0 && result < textLen && coords[result] == coords[result - 1]); return result; }
mat4f Synthesizer::makeModelToWorld(const DisembodiedObject &object, const PlacementLocation &location, float rotation) const { if (object.contactType == ContactUpright) { float elevation = 0.003f; // // Plates need to be a bit higher to ensure that collisions occur... // if (object.model->categoryName == "Plate") elevation = 0.005f; return mat4f::translation(location.sample.pos) * mat4f::rotationZ(rotation) * object.makeBaseTransform(elevation); } else { const vec3f orientPoint = scene.bbox.getCenter(); vec3f normal = location.sample.normal; if (((orientPoint - location.sample.pos) | normal) < 0.0f) normal = -normal; mat4f faceNormal = mat4f::face(vec3f::eZ, normal); float bestRotation = 0.0f; float bestRotationDist = std::numeric_limits<float>::max(); for (UINT rotationIndex = 0; rotationIndex < 4; rotationIndex++) { float curRotation = rotationIndex * 90.0f; mat4f transform = mat4f::rotation(normal, curRotation) * faceNormal * object.makeBaseTransformCentered(); vec3f downDir = transform.transformAffine(object.model->bbox.getCenter() + object.modelDown).getNormalized(); float curRotationDist = distSq(downDir, -vec3f::eZ); if (curRotationDist < bestRotationDist) { bestRotationDist = curRotationDist; bestRotation = curRotation; } } return mat4f::translation(location.sample.pos) * mat4f::rotation(normal, bestRotation) * faceNormal * object.makeBaseTransform(); } }
float Vec3f::dist(const Vec3f& v) const { return std::sqrt(distSq(v)); }
void MapperGLCanvas::mousePressEvent(QMouseEvent* event) { bool mousePressedOnSomething = false; _mousePressedPosition = event->pos(); QPointF pos = mapToScene(event->pos()); // Drag the scene with middle button. if (event->buttons() & Qt::MiddleButton) { // NOTE: This is a trick code to implement scroll hand drag using the middle button. QMouseEvent releaseEvent(QEvent::MouseButtonRelease, event->pos(), event->globalPos(), Qt::LeftButton, 0, event->modifiers()); QGraphicsView::mouseReleaseEvent(&releaseEvent); setDragMode(QGraphicsView::ScrollHandDrag); // We need to pretend it is actually the left button that was pressed! QMouseEvent fakeEvent(event->type(), event->pos(), event->globalPos(), Qt::LeftButton, event->buttons() | Qt::LeftButton, event->modifiers()); QGraphicsView::mousePressEvent(&fakeEvent); } // Check for vertex selection first. else if (event->buttons() & Qt::LeftButton) { MShape* shape = getCurrentShape(); if (shape) { // Note: we compare with the square value for fastest computation of the distance int minDistance = sq(MM::VERTEX_SELECT_RADIUS); // Find the ID of the nearest vertex (from currently selected shape) for (int i = 0; i < shape->nVertices(); i++) { int dist = distSq(_mousePressedPosition, mapFromScene(shape->getVertex(i))); // squared distance if (dist < minDistance) { _activeVertex = i; minDistance = dist; _mousePressedOnVertex = true; mousePressedOnSomething = true; _grabbedObjectStartPosition = shape->getVertex(i); } } } } if (mousePressedOnSomething) return; // Check for shape selection. if (event->buttons() & (Qt::LeftButton | Qt::RightButton)) // Add Right click for context menu { MShape* selectedShape = getCurrentShape(); // Possibility of changing shape in output by clicking on it. MappingManager manager = getMainWindow()->getMappingManager(); QVector<Mapping::ptr> mappings = manager.getVisibleMappings(); for (QVector<Mapping::ptr>::const_iterator it = mappings.end() - 1; it >= mappings.begin(); --it) { MShape *shape = getShapeFromMappingId((*it)->getId()); // Check if mouse was pressed on that shape. if (shape && shape->includesPoint(pos)) { mousePressedOnSomething = true; // Deselect vertices. deselectVertices(); // Change mapping (only available in destination). if (isOutput() && shape != selectedShape) { // Change current mapping. getMainWindow()->setCurrentMapping((*it)->getId()); // Reset selected shape to new one. selectedShape = getCurrentShape(); } break; } } // Grab the shape. if (event->buttons() & Qt::LeftButton) // This preserve me from duplicate code above { if (selectedShape && selectedShape->includesPoint(pos)) { _shapeGrabbed = true; _shapeFirstGrab = true; _grabbedObjectStartPosition = pos; } } } if (mousePressedOnSomething) return; // Deactivate. deselectAll(); }
T pt4<T>::dist(const pt4& other) const { return T(sqrt(distSq(other))); }
inline double ColorPalette::dist(double x1, double y1, double x2, double y2) { return sqrt(distSq(x1, y1, x2, y2)); }
double dist(const Point3D &p) const { return sqrt(distSq(p)); }