double Parameters::pixelDiff(Shot &test, CMeshO &mesh) { double maxdist = 0.0; double avedist = 0.0; int count = 0; CMeshO::VertexIterator vi; for(vi = pointSamples->vert.begin(); vi != pointSamples->vert.end(); ++vi ) { vcg::Point3f c; c.Import((*vi).P()); Point2f diff = pixelDiff(test, c); double dd = diff.Norm(); if(dd <= 0) continue; //outside of viewpoint if(dd > maxdist) maxdist = dd; avedist += dd*dd; count++; } //for(int i = 0; i < nsamples; i++) //{ // double r = (double)rand() / ((double)(RAND_MAX)+(double)(1)); // double g = (double)rand() / ((double)(RAND_MAX)+(double)(1)); // r = r*16000*16000+g*16000; // int v = ((int)r)%(mesh.vert.size()); // vcg::Point3d c; // c.Import(mesh.vert[v].P()); // Point2d diff = pixelDiff(test, c); // double dd = diff.Norm(); // if(dd <= 0) continue; //outside of viewpoint // if(dd > maxdist) maxdist = dd; // avedist += dd*dd; // count++; //} if(max_norm) return maxdist; return sqrt(avedist/count); }
bool SplineControl::isClose(const Point2f &p) { float val = getValue(p.getX()); float distance = fabs((p.getY() / height) - (1.0f - val)); return distance < (1.0f * FINGER_RANGE_FACTOR); }
Eigen::Matrix4d View::calcProjectionMatrix(Point2f frameBufferSize,float margin) { float near=0,far=0; Point2d frustLL,frustUR; frustLL.x() = -imagePlaneSize * (1.0 + margin); frustUR.x() = imagePlaneSize * (1.0 + margin); double ratio = ((double)frameBufferSize.y() / (double)frameBufferSize.x()); frustLL.y() = -imagePlaneSize * ratio * (1.0 + margin); frustUR.y() = imagePlaneSize * ratio * (1.0 + margin); near = nearPlane; far = farPlane; // Borrowed from the "OpenGL ES 2.0 Programming" book Eigen::Matrix4d projMat; Point3d delta(frustUR.x()-frustLL.x(),frustUR.y()-frustLL.y(),far-near); projMat.setIdentity(); projMat(0,0) = 2.0f * near / delta.x(); projMat(1,0) = projMat(2,0) = projMat(3,0) = 0.0f; projMat(1,1) = 2.0f * near / delta.y(); projMat(0,1) = projMat(2,1) = projMat(3,1) = 0.0f; projMat(0,2) = (frustUR.x()+frustLL.x()) / delta.x(); projMat(1,2) = (frustUR.y()+frustLL.y()) / delta.y(); projMat(2,2) = -(near + far ) / delta.z(); projMat(3,2) = -1.0f; projMat(2,3) = -2.0f * near * far / delta.z(); projMat(0,3) = projMat(1,3) = projMat(3,3) = 0.0f; return projMat; }
/// Sample the BRDF Color3f sample(BSDFQueryRecord &bRec, const Point2f &sample) const { if (Frame::cosTheta(bRec.wi) <= 0) return Color3f(0.0f); bRec.measure = ESolidAngle; /* Warp a uniformly distributed sample on [0,1]^2 to a direction on a cosine-weighted hemisphere */ if (sample.x() < m_ks) { float x = sample.x()/m_ks; Vector3f n = Warp::squareToBeckmann(Point2f(x, sample.y()), m_alpha); bRec.wo = (2.0f * bRec.wi.dot(n) * n - bRec.wi ); } else { float x = (sample.x() - m_ks) / (1.0f - m_ks); bRec.wo = Warp::squareToCosineHemisphere(Point2f(x,sample.y())); } if(Frame::cosTheta(bRec.wo) <= 0) { return Color3f(0.0f); } /* Relative index of refraction: no change */ bRec.eta = 1.0f; float pdfVal = pdf(bRec); return (pdfVal != 0.0f) ? Color3f(eval(bRec) * Frame::cosTheta(bRec.wo) / pdfVal) : Color3f(0.0f); }
void Body::Update(float dt) { //------------------------------------------------------- // Integrate position (verlet integration) //------------------------------------------------------- Point2f xAccel = netForce * invMass; pos += velocity * dt;// + 0.5f * xAccel * dt*dt; orientation += angVelocity * dt; assert(xAccel.Length() < 1e3); //------------------------------------------------------- // Integrate velocity (implicit linear velocity) //------------------------------------------------------- velocity += xAccel * dt; angVelocity += netTorque * (invInertia * dt); //------------------------------------------------------- // clear forces //------------------------------------------------------- netForce = Point2f(0.0f, 0.0f); netTorque = 0.0f; if (shape) shape->update(); }
void Bullet::step(float gameTime, float deltaTime) { Point2f delta = dest_pos - pos; float len; if (!bExplode) { len = delta.DotProduct(); if (len < 1) { bExplode = false; explodePos = pos; Explode(gameTime); return; } } else { bExplode = false; Explode(gameTime); return; } Point2f vel = delta.Normalize(min(sqrtf(len) / deltaTime, speed));// 可能引发爆发事件 collisionEntity.getBody().setVelocity(vel); collisionEntity.getBody().Update(deltaTime); }
bool Rect::contains(Point2f const & p) const { float left = m_corner.x(); float right = left + width(); float bottom = m_corner.y(); float top = bottom + height(); return p.x() >= left && p.x() <= right && p.y() >= bottom && p.y() <= top; }
Vector3f Warp::squareToCosineHemisphere(const Point2f &sample) { //create a sample which is cosine weighted Point2f ptOnDisk = squareToUniformDisk(sample); //compute the Z value return Vector3f(ptOnDisk(0), ptOnDisk(1), sqrt(1.0 - ptOnDisk.squaredNorm())); }
void Player::step(float gameTime, float deltaTime) { gameTime, deltaTime; Point2f dir = Point2f(400, 300) - pos; float speed = 1 * MainGameState::getSingleton().getTimeScale(); dir.Normalize(speed); pos += dir; }
void WMTouchEventSource::touchUp(TOUCHINPUT *ti) { assert(ti); if(eventCallback != NULL) { Point2f p = convert(ti); eventCallback(ti->dwID, p.getX(),p.getY(), FingerEventArgs::EventType::FINGER_REMOVED); } }
void Mesh::samplePosition(const Point2f &_sample, Point3f &p, Normal3f &n) const { Point2f sample(_sample); /* First, sample a triangle with respect to surface area */ size_t index = m_distr.sampleReuse(sample.x()); /* Lookup vertex positions for the chosen triangle */ int i0 = m_indices[3*index], i1 = m_indices[3*index+1], i2 = m_indices[3*index+2]; const Point3f &p0 = m_vertexPositions[i0], &p1 = m_vertexPositions[i1], &p2 = m_vertexPositions[i2]; /* Sample a position in barycentric coordinates */ Point2f b = squareToUniformTriangle(sample); p = p0 * (1.0f - b.x() - b.y()) + p1 * b.x() + p2 * b.y(); /* Also provide a normal (interpolated if vertex normals are provided) */ if (m_vertexNormals) { const Normal3f &n0 = m_vertexNormals[i0], &n1 = m_vertexNormals[i1], &n2 = m_vertexNormals[i2]; n = (n0 * (1.0f - b.x() - b.y()) + n1 * b.x() + n2 * b.y()).normalized(); } else { n = (p1-p0).cross(p2-p0).normalized(); } }
Point2d View::screenSizeInDisplayCoords(Point2f &frameSize) { Point2d screenSize(0,0); if (frameSize.x() == 0.0 || frameSize.y() == 0.0) return screenSize; screenSize.x() = tan(fieldOfView/2.0) * heightAboveSurface() * 2.0; screenSize.y() = screenSize.x() / frameSize.x() * frameSize.y(); return screenSize; }
void TargetIndicator::GraphicEntity::render(float gameTime, float deltaTime) { int frameTime = int(gameTime * 60); const Rectf viewerRange(-379, 379, -279, 279); // slightly smaller than windowViewRange const Point2f &windowSize = iRenderQueue::getSingleton().getWindowSize(); const Rectf windowRange(0, windowSize.x, 0, windowSize.y); const Rectf windowViewRange(windowRange.leftTop.x + 20, windowRange.rightBottom.x - 20, windowRange.leftTop.y + 20, windowRange.rightBottom.y - 20); const Point2f posOffset(windowRange.rightBottom / 2); const Point2f viewerPos = iRenderQueue::getSingleton().getViewerPos() + posOffset; Point2f pos; deltaTime; for (vector<iAttacheeEntity*>::const_iterator ie = getEntity().entities.begin(); ie != getEntity().entities.end(); ++ie) { if (((iTargetEntity*)*ie)->getEntity().getStatus(ESI_Position, &pos)) { Point2f dir = pos - viewerPos; if (viewerRange & dir) continue; if (dir.Length() > 500) continue; float direction = -atan2(dir.y, dir.x); if (fabs(dir.x) > 1e-5) { Point2f pos = dir * (viewerRange.rightBottom.x / fabs(dir.x)) + posOffset; if (windowViewRange & pos) iRenderQueue::getSingleton().render(pos, direction, frameTime, anim, GLI_UI, true); else { pos = dir * (viewerRange.rightBottom.y / fabs(dir.y)) + posOffset; //if (windowViewRange & pos) iRenderQueue::getSingleton().render(pos, direction, frameTime, anim, GLI_UI, true); } } else { assert(fabs(dir.y) > 1e-5); Point2f pos = dir * (viewerRange.rightBottom.y / fabs(dir.y)) + posOffset; if (windowViewRange & pos) iRenderQueue::getSingleton().render(pos, direction, frameTime, anim, GLI_UI, true); else { pos = dir * (viewerRange.rightBottom.x / fabs(dir.x)) + posOffset; //if (windowViewRange & pos) iRenderQueue::getSingleton().render(pos, direction, frameTime, anim, GLI_UI, true); } } } } }
void Polygon::buildQPolygon() { int const n = numPoints(); m_qpolygon = QPolygonF(n); for (int i = 0; i < n; ++i) { Point2f p = m_points[i]; m_qpolygon[i] = QPointF(p.x(), p.y()); } }
void PressAndHold::updateFingerPosition(const Point2f &newPosition) { float dx = position.getX() - newPosition.getX(); float dy = position.getY() - newPosition.getY(); float d = sqrt((dx * dx) + (dy * dy)); if(d >= THRESHOLD) { markedForDelete = true; } }
/* PUBLIC MEMBER FUNCTIONS */ bool ControlPoint::contains(const FingerEventArgs &e) const { Point2f p = e.getPosition(); assert(modelview != NULL); modelview->unTranslatePoint(p); modelview->unScalePoint(p); float distance = sqrt((p.getX() * p.getX()) + (p.getY() * p.getY())); return distance <= (width * BOUNDING_SPHERE); }
void AiTankController::stepMoveTo() { if (!isAttached()) { bCheckMoveTo = false; return; } Point2f posTank; float orientionTank; Entity &e = entity->getEntity(); if (e.getStatus(Entity::ESI_Position, &posTank) && e.getStatus(Entity::ESI_Orientation, &orientionTank)) { //#ifndef NDEBUG iRenderQueue::getSingleton().render(moveToPos, posTank, ARGB(255, 0, 0, 0)); //#endif Point2f dir = moveToPos - posTank; float len = dir.Length(); float len2 = (nextMoveToPos - posTank).Length(); if (len < 10 || len > len2) { bCheckMoveTo = false; bForward = false; sendCommand(Tank::TCI_Forward, (void *)bForward); return; } dir.Normalize(); if (fabs(dir ^ Point2f(cos(orientionTank), sin(orientionTank))) < 0.3f) { if (!bForward) { bForward = true; sendCommand(Tank::TCI_Forward, (void *)bForward); } } else { float lerpValue = (float)rand() / RAND_MAX; lerpValue *= lerpValue; turnTo(moveToPos);//lerp(moveToPos, nextMoveToPos, lerpValue)); if (bForward) { bForward = false; sendCommand(Tank::TCI_Forward, (void *)bForward); } } } else { bCheckMoveTo = false; } }
Vector3f Warp::squareToSchlick(const Point2f &sample, float k) { //use the inverse methode if(k == 0.0){ return Warp::squareToUniformSphere(sample); } float cosTheta = ((k + 2.0f * sample.x() - 1.0f ) / ( k * (2.0f * sample.x() - 1.0f ) + 1.0f)); float acostheta = acos(cosTheta); float phi = 2.0f * M_PI * sample.y(); return Vector3f(sin(acostheta) * cos(phi), sin(acostheta) * sin(phi), cos(acostheta)); }
/* PUBLIC MEMBER FUNCTIONS */ bool SplineControl::contains(const FingerEventArgs &e) const { assert(modelview != NULL); Point2f p = e.getPosition(); modelview->unTranslatePoint(p); float legendDim = (height * LEGEND_DIM) * modelview->getYScale(); return p.getX() >= 0.0f && p.getX() <= (width * modelview->getXScale()) && p.getY() >= legendDim && p.getY() <= (height * modelview->getYScale()) - legendDim; }
bool SplineControl::controlPointContains(const FingerEventArgs &e) const { assert(modelview != NULL); Point2f p = e.getPosition(); modelview->unTranslatePoint(p); // use legend dim on left and right as a buffer to prevent deletion near edge float legendDim = (height * LEGEND_DIM) * modelview->getYScale(); return p.getX() >= -legendDim && p.getX() <= (width * modelview->getXScale()) + legendDim && p.getY() >= 0.0f && p.getY() <= height * modelview->getYScale(); }
bool physicsAabb::overlaps(const physicsAabb& aabb) { Point2f centerA = m_max.add(m_min); centerA.setMul(0.5f); Point2f centerB = aabb.m_max.add(aabb.m_min); centerB.setMul(0.5f); float dx = abs(centerA.x - centerB.x); float dy = abs(centerA.y - centerB.y); float sumX = m_max.x - centerA.x + aabb.m_max.x - centerB.x; float sumY = m_max.y - centerA.y + aabb.m_max.y - centerB.y; return (dx < sumX && dy < sumY); }
size_t collide(CircleShape &circle, BoxShape &box, float dt, Contact *pxContacts, size_t numMaxContacts) { dt, pxContacts, numMaxContacts; const Point2f &p1 = circle.body.getPosition(); const Point2f &p2 = box.body.getPosition(); // work in the box's coordinate system const Point2f kDiff = p1 - p2; // compute squared distance and closest point on box float fSqrDistance = 0.0f, fDelta; Point2f kClosest(kDiff * box.GetDir(0), kDiff * box.GetDir(1)); const Point2f &extents = box.GetExtents(); if (kClosest.x < -extents.x) { fDelta = kClosest.x + extents.x; fSqrDistance += fDelta*fDelta; kClosest.x = -extents.x; } else if (kClosest.x > extents.x) { fDelta = kClosest.x - extents.x; fSqrDistance += fDelta*fDelta; kClosest.x = extents.x; } if (kClosest.y < -extents.y) { fDelta = kClosest.y + extents.y; fSqrDistance += fDelta*fDelta; kClosest.y = -extents.y; } else if (kClosest.y > extents.y) { fDelta = kClosest.y - extents.y; fSqrDistance += fDelta*fDelta; kClosest.y = extents.y; } if (fSqrDistance > circle.GetRadius() * circle.GetRadius()) { return 0; } Point2f d = p2 + kClosest - p1; d.Normalize(); pxContacts[0] = Contact(p1 + d * circle.GetRadius(), p2 + kClosest, &circle.body, &box.body); return 1; }
Spectrum PerspectiveSensor::sampleRay(Ray3f& ray, const Point2f& samplePosition, const Point2f& apertureSample) const { Point3f nearPoint = m_sampleToCamera * Point3f( samplePosition.x() * m_invImageSize.x(), samplePosition.y() * m_invImageSize.y(), 0.f); Vector3f dir = nearPoint.normalized(); float invZ = 1.f / dir.z(); ray.o = m_cameraToWorld * Point3f(0.f, 0.f, 0.f); ray.d = m_cameraToWorld * dir; ray.minT = m_nearClip * invZ; ray.maxT = m_farClip * invZ; ray.update(); return Spectrum(1.f); }
void Bullet::Explode(float gameTime) { Explosive *e = new Explosive(graphicEntity.animResManager); Point2f delta = dest_pos - pos; float len = delta.DotProduct(); if (len > 1) { delta = delta.Normalize(speed); } e->orientation = orientation; e->init("data/tank/bulletexplode.xml", gameTime, gun->explosive); e->setPostion(explodePos, explodePos + delta); EntityManager::getSingleton().attach(e); active = false; }
bool StopLineDetector::EstimateVerticalLine(Mat &input, Mat &debugImage, Rect roi, Point2i searchedSlope, CVMath::Line *outputLine) { vector<Vec4i> foundLines; Mat localCopy; input(roi).copyTo(localCopy); Canny(localCopy, localCopy, 50, 200, 3); HoughLinesP(localCopy, foundLines, 1, CV_PI / 180 , 10, 15, 4); const float COS_ANGLE_LIMIT = cos(20.0 / 180 * CV_PI); int resultListSize = 0; Point2f resultTangent; Point2f resultPoint; size_t listSize = foundLines.size(); for (size_t i = 0; i < listSize; i++) { Vec4i l = foundLines[i]; Point2f p0 = Point2f(l[0], l[1]) + Point2f(roi.x, roi.y); Point2f p1 = Point2f(l[2], l[3]) + Point2f(roi.x, roi.y); Point2f tangent = p0 - p1; tangent /= cv::norm(tangent); float dotProduct = tangent.dot(searchedSlope); if (dotProduct > COS_ANGLE_LIMIT || -dotProduct > COS_ANGLE_LIMIT ) { if(debugEnabled) { line(debugImage, p0, p1, Scalar(255, 0, 0), 2); } resultPoint += p0; resultTangent += tangent; resultListSize++; } } if(resultListSize > 0) { *outputLine = CVMath::Line (resultPoint / resultListSize, resultTangent / resultListSize); if(debugEnabled) { line(debugImage, outputLine->pointOnLine - outputLine->direction*20, outputLine->pointOnLine + outputLine->direction*20, Scalar(0, 100, 100), 2); } return true; } return false; }
void LightCategory::doCopyComponent( Point2f const & component , uint32_t index , uint32_t & offset , PxBufferBase & data )const { uint8_t * dst = &( *data.getAt( index * shader::MaxLightComponentsCount + offset++, 0u ) ); std::memcpy( dst, component.constPtr(), 2 * sizeof( float ) ); }
size_t collide(CircleShape &circle1, CircleShape &circle2, float dt, Contact *pxContacts, size_t numMaxContacts) { dt, numMaxContacts; float rR = circle1.GetRadius() + circle2.GetRadius(); const Point2f &p1 = circle1.body.getPosition(); const Point2f &p2 = circle2.body.getPosition(); Point2f d = p2 - p1; float len = d.Length(); if (len > rR) { return 0; } d.Normalize(); pxContacts[0] = Contact(p1 + d * circle1.GetRadius(), p1 + d * (len - circle1.GetRadius()), &circle1.body, &circle2.body); return 1; }
int getDir(const Position& newPos) { Point2f center = { BOARD_WIDTH / 2, BOARD_HEIGHT / 2 }; Point2f centeredOld = center - coordinate; Point2f centeredNew = center - newPos.coordinate; float z = centeredOld.cross(centeredNew); if (abs(z) > 7.5) { if (z < 0) return DIR_CCW; else if (z > 0) return DIR_CW; } return DIR_NONE; }
static Mat endpointError( const Mat_<Point2f>& flow1, const Mat_<Point2f>& flow2 ) { Mat result(flow1.size(), CV_32FC1); for ( int i = 0; i < flow1.rows; ++i ) { for ( int j = 0; j < flow1.cols; ++j ) { const Point2f u1 = flow1(i, j); const Point2f u2 = flow2(i, j); if ( isFlowCorrect(u1) && isFlowCorrect(u2) ) { const Point2f diff = u1 - u2; result.at<float>(i, j) = sqrt((float)diff.ddot(diff)); //distance } else result.at<float>(i, j) = std::numeric_limits<float>::quiet_NaN(); } } return result; }
std::vector<int> ptsubsetBetween2Line( std::vector<Point2f> points2d, Point2f p0, Point2f p1, Point2f t0){ t0.Normalize() ; std::vector<int> res ; for( int i=0; i<points2d.size(); ++i ){ Point2f p0p1 = p1-p0 ; if( p0p1 * (points2d[i]-p1) <= 0 && !ReconstructorUtility::vectorIntersect(p0-t0*100, p0+t0*100, points2d[i], p1 ) ) res.push_back( i ) ; } return res ; }