bool MgArc::_setHandlePoint(int index, const Point2d& pt, float) { if (index == 1) { return setStartMidEnd(pt, getMidPoint(), getEndPoint()); } if (index == 2) { return setStartMidEnd(getStartPoint(), getMidPoint(), pt); } if (index == 3) { return setStartMidEnd(getStartPoint(), pt, getEndPoint()); } if (index == 4) { float a = (pt - getCenter()).angle2(); return setCenterRadius(getCenter(), getRadius(), a, getEndAngle() - a); } if (index == 5) { float a = (pt - getCenter()).angle2(); return setCenterRadius(getCenter(), getRadius(), getStartAngle(), a - getStartAngle()); } if (index == 6) { return setTanStartEnd(pt - getStartPoint(), getStartPoint(), getEndPoint()); } if (index == 7) { return (setTanStartEnd(getEndPoint() - pt, getEndPoint(), getStartPoint()) && _reverse()); } return setCenterStartEnd(pt, getStartPoint(), getEndPoint()); }
QPainterPath CurveGraphicsItem::obtainCurvePath() const { if (useMidPoint_) { return obtainCurvePath(getStartPoint(), getMidPoint()) + obtainCurvePath(getMidPoint(), getEndPoint()); } else { return obtainCurvePath(getStartPoint(), getEndPoint()); } }
float MgArc::getSweepAngle() const { if (!mgIsZero(_sweepAngle)) { return _sweepAngle; } const float midAngle = (getMidPoint() - getCenter()).angle2(); const float startAngle = getStartAngle(); const float endAngle = getEndAngle(); if (mgEquals(midAngle, startAngle) && mgEquals(startAngle, endAngle)) { return endAngle - startAngle; } Tol tol(getRadius() * 1e-3f, 1e-4f); if (getStartPoint().isEqualTo(getEndPoint(), tol) && (getMidPoint() + (getStartPoint() + getEndPoint()) / 2).isEqualTo(2 * getCenter(), tol)) { return _M_2PI; } float startAngle2 = startAngle; float midAngle2 = midAngle; float endAngle2 = endAngle; // 先尝试看是否为逆时针方向:endAngle2 > midAngle2 > startAngle2 >= 0 if (startAngle2 < 0) startAngle2 += _M_2PI; while (midAngle2 < startAngle2) midAngle2 += _M_2PI; while (endAngle2 < midAngle2) endAngle2 += _M_2PI; if (fabsf(startAngle2 + endAngle2 - 2 * midAngle2) < _M_PI_6 && endAngle2 - startAngle2 < _M_2PI) { return endAngle2 - startAngle2; } // 再尝试看是否为顺时针方向:endAngle2 < midAngle2 < startAngle2 <= 0 startAngle2 = startAngle; midAngle2 = midAngle; endAngle2 = endAngle; if (startAngle2 > 0) startAngle2 -= _M_2PI; while (midAngle2 > startAngle2) midAngle2 -= _M_2PI; while (endAngle2 > midAngle2) endAngle2 -= _M_2PI; if (fabsf(startAngle2 + endAngle2 - 2 * midAngle2) < _M_PI_6) { if (endAngle2 - startAngle2 > -_M_2PI) return endAngle2 - startAngle2; return mgbase::toRange(endAngle2 - startAngle2, -_M_2PI, 0); } return endAngle - startAngle; // error }
/** * gets a scaling of a quad */ quadri getQuadScale(quadri base, vector scaleAction) { vector c; /* center of quad */ quadri s; /* new quad */ double imW = length(base.v4,base.v3); double imH = length(base.v4,base.v1); double sx = 1 + scaleAction.x/imW; double sy = 1 + scaleAction.y/imH; /* get the params */ c = getMidPoint(base); /* new vertices are scaled from the center of the current loaction */ s.v1.x = c.x + sx*(base.v1.x-c.x); s.v1.y = c.y + sy*(base.v1.y-c.y); s.v2.x = c.x + sx*(base.v2.x-c.x); s.v2.y = c.y + sy*(base.v2.y-c.y); s.v3.x = c.x + sx*(base.v3.x-c.x); s.v3.y = c.y + sy*(base.v3.y-c.y); s.v4.x = c.x + sx*(base.v4.x-c.x); s.v4.y = c.y + sy*(base.v4.y-c.y); s.v1.z = s.v2.z = s.v3.z = s.v4.z = 0.0; return s; }
FboMaskSelection FboMaskShape::getNearestPoint(ofxVec2f& target){ FboMaskSelection nearest; float bestDistance = -1; int max = points.size(); //check points for(int i = 0; i < max; ++i){ ofxVec2f& point = points[i]; float distance = point.distance(target); if(distance < clickDistance){ if(!nearest.valid || distance < bestDistance){ nearest.setPoint(point, i); bestDistance = distance; } } } //check midpoints if(max > 1){ for(int i = 0; i < max; ++i){ ofxVec2f point = getMidPoint(i); float distance = point.distance(target); if(distance < clickDistance){ if(!nearest.valid || distance < bestDistance){ nearest.setPoint(point, i); nearest.midPoint = true; bestDistance = distance; } } } } return nearest; }
void helix::drawCentered( bool bDrawOuter, bool bDrawInner ){ ofPoint midPt = getMidPoint(); ofPushMatrix(); ofTranslate(-midPt.x, -midPt.y, -midPt.z); if (bDrawOuter){ helix0.draw(); helix1.draw(); } if (bDrawInner) { for (int i = 0; i < lines.size(); i++){ float div = 20; for (int j = 0; j < (int)(div-1); j++){ ofPoint a = lines[i].a; ofPoint diff = (lines[i].b - lines[i].a) / (div-1.f); ofLine(a + diff * j, a + diff * (j+1)); } } } ofPopMatrix(); }
Point2d MgArc::_getHandlePoint(int index) const { if (index == 1) { return getStartPoint(); } if (index == 2) { return getEndPoint(); } if (index == 3) { return getMidPoint(); } if (index == 4) { return (getStartPoint() * 2 + getCenter()) / 3; } if (index == 5) { return (getEndPoint() * 2 + getCenter()) / 3; } if (index == 6) { return getStartPoint() + getStartTangent(); } if (index == 7) { return getEndPoint() + getEndTangent(); } return getCenter(); }
void FboMaskShape::drawPoints(){ int max = points.size(); for(int i = 0; i < max; ++i){ ofxVec2f& point = points[i]; drawPoint(point, false, false, false); } if(max > 1){ for(int i = 0; i < max; ++i){ ofxVec2f point = getMidPoint(i); drawPoint(point, false, false, true); } } }
ossim_float64 ossimHistogramRemapper::getMidPoint() const { if (theMidPoint.size() == 0) { return ossim::nan(); } ossim_float64 d = 0.0; const ossim_uint32 BANDS = (ossim_uint32)theMidPoint.size(); for (ossim_uint32 band = 0; band < BANDS; ++band) { d += getMidPoint(band); } return (d / BANDS); }
size_t SpatialEdge::newEdge(size_t emindex, size_t index, int k) { Edge *en, *em; size_t swap; em = &edges_[emindex]; switch (k) { case 0: em->start_ = IV(1); em->end_ = IV(2); break; case 1: em->start_ = IV(0); em->end_ = IV(2); break; case 2: em->start_ = IV(0); em->end_ = IV(1); break; } // sort the vertices by increasing index if(em->start_ > em->end_) { swap = em->start_; em->start_ = em->end_; em->end_ = swap; } // check all previous edges for a match, return pointer if // already present, log the midpoint with the new face as well if( (en=edgeMatch(em)) != NULL){ IW(k) = en->mid_; return emindex; } // this is a new edge, immediately process the midpoint, // and save it with the nodes and the edge as well insertLookup(em); IW(k) = getMidPoint(em); em->mid_ = IW(k); return ++emindex; }
float MgArc::getSweepAngle() const { const float midAngle = (getMidPoint() - getCenter()).angle2(); const float startAngle = getStartAngle(); const float endAngle = getEndAngle(); if (mgEquals(midAngle, startAngle) && mgEquals(startAngle, endAngle)) { return endAngle - startAngle; } float startAngle2 = startAngle; float midAngle2 = midAngle; float endAngle2 = endAngle; // 先尝试看是否为逆时针方向:endAngle2 > midAngle2 > startAngle2 >= 0 if (startAngle2 < 0) startAngle2 += _M_2PI; while (midAngle2 < startAngle2) midAngle2 += _M_2PI; while (endAngle2 < midAngle2) endAngle2 += _M_2PI; if (fabsf(startAngle2 + endAngle2 - 2 * midAngle2) < _M_PI_6 && endAngle2 - startAngle2 < _M_2PI) { return endAngle2 - startAngle2; } // 再尝试看是否为顺时针方向:endAngle2 < midAngle2 < startAngle2 <= 0 startAngle2 = startAngle; midAngle2 = midAngle; endAngle2 = endAngle; if (startAngle2 > 0) startAngle2 -= _M_2PI; while (midAngle2 > startAngle2) midAngle2 -= _M_2PI; while (endAngle2 > midAngle2) endAngle2 -= _M_2PI; if (fabsf(startAngle2 + endAngle2 - 2 * midAngle2) < _M_PI_6) { if (endAngle2 - startAngle2 > -_M_2PI) return endAngle2 - startAngle2; return mgToRange(endAngle2 - startAngle2, -_M_2PI, 0); } return endAngle - startAngle; // error }
void SphericalPlanet::beginDoubleDrag(const Vector3D& origin, const Vector3D& centerRay, const Vector3D& initialRay0, const Vector3D& initialRay1) const { _origin = origin.asMutableVector3D(); _centerRay = centerRay.asMutableVector3D(); _initialPoint0 = closestIntersection(origin, initialRay0).asMutableVector3D(); _initialPoint1 = closestIntersection(origin, initialRay1).asMutableVector3D(); _angleBetweenInitialPoints = _initialPoint0.angleBetween(_initialPoint1)._degrees; _centerPoint = closestIntersection(origin, centerRay).asMutableVector3D(); _angleBetweenInitialRays = initialRay0.angleBetween(initialRay1)._degrees; // middle point in 3D Geodetic2D g0 = toGeodetic2D(_initialPoint0.asVector3D()); Geodetic2D g1 = toGeodetic2D(_initialPoint1.asVector3D()); Geodetic2D g = getMidPoint(g0, g1); _initialPoint = toCartesian(g).asMutableVector3D(); }
sphere::sphere(glm::vec3 _origin, glm::vec3 _color, float radius) { isSelected = false; origin = glm::vec4(_origin,1.0); color = _color; glm::mat4 transOrigin = glm::translate(_origin); glm::mat4 scaleSphere = glm::scale(radius,radius,radius); float t = (1.f + glm::sqrt(5.0)) / 2.f; icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3(-1.f, t,0.f)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3( 1.f, t,0.f)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3(-1.f, -t,0.f)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3( 1.f, -t,0.f)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3(0.f, -1.f, t)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3(0.f, 1.f, t)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3(0.f, -1.f, -t)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3(0.f, 1.f, -t)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3( t, 0.f, -1)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3( t, 0.f, 1)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3(-t, 0.f, -1)),1.f)); icoPoints.push_back(glm::vec4(glm::normalize(glm::vec3(-t, 0.f, 1)),1.f)); // faces around point 0 vector<face> faces; faces.push_back(face(0,11,5)); faces.push_back(face(0,5,1)); faces.push_back(face(0,1,7)); faces.push_back(face(0,7,10)); faces.push_back(face(0,10,11)); faces.push_back(face(1,5,9)); faces.push_back(face(5,11,4)); faces.push_back(face(11,10,2)); faces.push_back(face(10,7,6)); faces.push_back(face(7,1,8)); faces.push_back(face(3,9,4)); faces.push_back(face(3,4,2)); faces.push_back(face(3,2,6)); faces.push_back(face(3,6,8)); faces.push_back(face(3,8,9)); faces.push_back(face(4,9,5)); faces.push_back(face(2,4,11)); faces.push_back(face(6,2,10)); faces.push_back(face(8,6,7)); faces.push_back(face(9,8,1)); for (int i = 0; i < 2; i++) { for (int i = 0; i < faces.size(); i++) { int a = getMidPoint(faces[i].ind1,faces[i].ind2); int b = getMidPoint(faces[i].ind2,faces[i].ind3); int c = getMidPoint(faces[i].ind3,faces[i].ind1); faces2.push_back(face(faces[i].ind1,a,c)); faces2.push_back(face(faces[i].ind2,b,a)); faces2.push_back(face(faces[i].ind3,c,b)); faces2.push_back(face(a,b,c)); } } for(int i = 0; i < icoPoints.size(); i++) { icoPoints[i] = transOrigin * scaleSphere * icoPoints[i]; } for(int i = 0; i < icoPoints.size(); i++) { points.push_back(icoPoints[i]); } constructBuffers(); }
MutableMatrix44D SphericalPlanet::doubleDrag(const Vector3D& finalRay0, const Vector3D& finalRay1) const { // test if initialPoints are valid if (_initialPoint0.isNan() || _initialPoint1.isNan()) return MutableMatrix44D::invalid(); // init params const IMathUtils* mu = IMathUtils::instance(); MutableVector3D positionCamera = _origin; const double finalRaysAngle = finalRay0.angleBetween(finalRay1)._degrees; const double factor = finalRaysAngle / _angleBetweenInitialRays; double dAccum=0, angle0, angle1; double distance = _origin.sub(_centerPoint).length(); // compute estimated camera translation: step 0 double d = distance*(factor-1)/factor; MutableMatrix44D translation = MutableMatrix44D::createTranslationMatrix(_centerRay.asVector3D().normalized().times(d)); positionCamera = positionCamera.transformedBy(translation, 1.0); dAccum += d; { const Vector3D point0 = closestIntersection(positionCamera.asVector3D(), finalRay0); const Vector3D point1 = closestIntersection(positionCamera.asVector3D(), finalRay1); angle0 = point0.angleBetween(point1)._degrees; if (ISNAN(angle0)) return MutableMatrix44D::invalid(); } // compute estimated camera translation: step 1 d = mu->abs((distance-d)*0.3); if (angle0 < _angleBetweenInitialPoints) d*=-1; translation = MutableMatrix44D::createTranslationMatrix(_centerRay.asVector3D().normalized().times(d)); positionCamera = positionCamera.transformedBy(translation, 1.0); dAccum += d; { const Vector3D point0 = closestIntersection(positionCamera.asVector3D(), finalRay0); const Vector3D point1 = closestIntersection(positionCamera.asVector3D(), finalRay1); angle1 = point0.angleBetween(point1)._degrees; if (ISNAN(angle1)) return MutableMatrix44D::invalid(); } // compute estimated camera translation: steps 2..n until convergence //int iter=0; double precision = mu->pow(10, mu->log10(distance)-8.0); double angle_n1=angle0, angle_n=angle1; while (mu->abs(angle_n-_angleBetweenInitialPoints) > precision) { // iter++; if ((angle_n1-angle_n)/(angle_n-_angleBetweenInitialPoints) < 0) d*=-0.5; translation = MutableMatrix44D::createTranslationMatrix(_centerRay.asVector3D().normalized().times(d)); positionCamera = positionCamera.transformedBy(translation, 1.0); dAccum += d; angle_n1 = angle_n; { const Vector3D point0 = closestIntersection(positionCamera.asVector3D(), finalRay0); const Vector3D point1 = closestIntersection(positionCamera.asVector3D(), finalRay1); angle_n = point0.angleBetween(point1)._degrees; if (ISNAN(angle_n)) return MutableMatrix44D::invalid(); } } //if (iter>2) printf("----------- iteraciones=%d precision=%f angulo final=%.4f distancia final=%.1f\n", iter, precision, angle_n, dAccum); // start to compound matrix MutableMatrix44D matrix = MutableMatrix44D::identity(); positionCamera = _origin; MutableVector3D viewDirection = _centerRay; MutableVector3D ray0 = finalRay0.asMutableVector3D(); MutableVector3D ray1 = finalRay1.asMutableVector3D(); // drag from initialPoint to centerPoint { Vector3D initialPoint = _initialPoint.asVector3D(); const Vector3D rotationAxis = initialPoint.cross(_centerPoint.asVector3D()); const Angle rotationDelta = Angle::fromRadians( - mu->acos(_initialPoint.normalized().dot(_centerPoint.normalized())) ); if (rotationDelta.isNan()) return MutableMatrix44D::invalid(); MutableMatrix44D rotation = MutableMatrix44D::createRotationMatrix(rotationDelta, rotationAxis); positionCamera = positionCamera.transformedBy(rotation, 1.0); viewDirection = viewDirection.transformedBy(rotation, 0.0); ray0 = ray0.transformedBy(rotation, 0.0); ray1 = ray1.transformedBy(rotation, 0.0); matrix = rotation.multiply(matrix); } // move the camera forward { MutableMatrix44D translation2 = MutableMatrix44D::createTranslationMatrix(viewDirection.asVector3D().normalized().times(dAccum)); positionCamera = positionCamera.transformedBy(translation2, 1.0); matrix = translation2.multiply(matrix); } // compute 3D point of view center Vector3D centerPoint2 = closestIntersection(positionCamera.asVector3D(), viewDirection.asVector3D()); // compute middle point in 3D Vector3D P0 = closestIntersection(positionCamera.asVector3D(), ray0.asVector3D()); Vector3D P1 = closestIntersection(positionCamera.asVector3D(), ray1.asVector3D()); Geodetic2D g = getMidPoint(toGeodetic2D(P0), toGeodetic2D(P1)); Vector3D finalPoint = toCartesian(g); // drag globe from centerPoint to finalPoint { const Vector3D rotationAxis = centerPoint2.cross(finalPoint); const Angle rotationDelta = Angle::fromRadians( - mu->acos(centerPoint2.normalized().dot(finalPoint.normalized())) ); if (rotationDelta.isNan()) return MutableMatrix44D::invalid(); MutableMatrix44D rotation = MutableMatrix44D::createRotationMatrix(rotationDelta, rotationAxis); positionCamera = positionCamera.transformedBy(rotation, 1.0); viewDirection = viewDirection.transformedBy(rotation, 0.0); ray0 = ray0.transformedBy(rotation, 0.0); ray1 = ray1.transformedBy(rotation, 0.0); matrix = rotation.multiply(matrix); } // camera rotation { Vector3D normal = geodeticSurfaceNormal(centerPoint2); Vector3D v0 = _initialPoint0.asVector3D().sub(centerPoint2).projectionInPlane(normal); Vector3D p0 = closestIntersection(positionCamera.asVector3D(), ray0.asVector3D()); Vector3D v1 = p0.sub(centerPoint2).projectionInPlane(normal); double angle = v0.angleBetween(v1)._degrees; double sign = v1.cross(v0).dot(normal); if (sign<0) angle = -angle; MutableMatrix44D rotation = MutableMatrix44D::createGeneralRotationMatrix(Angle::fromDegrees(angle), normal, centerPoint2); matrix = rotation.multiply(matrix); } return matrix; }