int maxProfit(vector<int> &prices) { if(prices.size() < 2) return 0; vector<int> cVec(prices.size() , 0); int minV = prices[0]; cVec[0] = prices[0]; for (int i = 1 ; i < prices.size() ; i++) { if(prices[i] < minV) { minV = prices[i]; } cVec[i] = minV; } vector<int> eVec(prices.size(),0); int maxV = prices[prices.size()-1]; eVec[eVec.size()-1] = prices[prices.size()-1]; for(int i = prices.size() - 2 ; i >= 0 ; i--) { if(prices[i] > maxV) { maxV = prices[i]; } eVec[i] = maxV; } int sum = 0; for(int i = 0 ; i < prices.size() - 1 ; i++) { if(eVec[i+1] - cVec[i] > sum) sum = eVec[i+1] - cVec[i]; } return sum; }
void ElementInterface::Bind(HSQUIRRELVM vm) { sq_pushroottable(vm); NamespaceHelper::switchTo(vm, "Rocket"); ElementStyleProxy::Bind(vm); //ElementList sqb::ClassDefinition<VectorInterface<ElementWrapperList>> cVec(vm, -1, _SC("ElementList")); cVec.ClassFunction(&VectorInterface<ElementWrapperList>::Contains, _SC("Contains")); cVec.ClassFunction(&VectorInterface<ElementWrapperList>::SetItem, _SC("_set")); cVec.NativeClassFunction(&VectorInterface<ElementWrapperList>::GetItem, _SC("_get"), sqb::FunctionOptions().ParamCheckCount(-2).TypeMask(_SC("xi"))); cVec.ClassFunction(&VectorInterface<ElementWrapperList>::PushBack, _SC("append")); cVec.ClassFunction(&VectorInterface<ElementWrapperList>::PushBack, _SC("push")); cVec.ClassFunction(&VectorInterface<ElementWrapperList>::Size, _SC("len")); cVec.ClassFunction(&VectorInterface<ElementWrapperList>::DelItem, _SC("remove")); sq_poptop(vm); ElementWrapper::Bind(vm); ElementDocumentWrapper::Bind(vm); ElementTextWrapper::Bind(vm); //hmm can't access ElementTextDefault, ElementImage and ElementHandle }
bool CCylinder::Intersects(Real& f_t_on_ray, const CRay3& c_ray) { /* * This algorithm was adapted from * http://www.realtimerendering.com/resources/GraphicsGems/gemsiv/ray_cyl.c */ /* Vector from cylinder base to ray start */ CVector3 cCylBase2RayStart(c_ray.GetStart()); cCylBase2RayStart -= m_cBasePos; /* Ray direction and length */ CVector3 cRayDir; c_ray.GetDirection(cRayDir); Real fRayLen = c_ray.GetLength(); /* Vector normal to cylinder axis and ray direction */ CVector3 cNormal(cRayDir); cNormal.CrossProduct(m_cAxis); Real fNormalLen = cNormal.Length(); /* Are cylinder axis and ray parallel? */ if(fNormalLen > 0) { /* No, they aren't parallel */ /* Make normal have length 1 */ cNormal /= fNormalLen; /* Calculate shortest distance between axis and ray * by projecting cCylBase2RayStart onto cNormal */ Real fDist = Abs(cCylBase2RayStart.DotProduct(cNormal)); /* Is fDist smaller than the cylinder radius? */ if(fDist > m_fRadius) { /* No, it's not, so there can't be any intersection */ return false; } /* If we get here, it's because the ray intersects the infinite cylinder */ /* Create a buffer for the 4 potential intersection points (two on the sides, two on the bases) */ Real fPotentialT[4]; /* First, calculate the intersection points with the sides */ /* Calculate the midpoint between the two intersection points */ CVector3 cVec(cCylBase2RayStart); cVec.CrossProduct(m_cAxis); Real fMidPointDist = -cVec.DotProduct(cNormal) / fNormalLen; /* Calculate the distance between the midpoint and the potential t's */ cVec = cNormal; cVec.CrossProduct(m_cAxis); cVec.Normalize(); Real fDeltaToMidPoint = Abs(Sqrt(Square(m_fRadius) - Square(fDist)) / cRayDir.DotProduct(cVec)); /* Calculate the potential t's on the infinite surface */ fPotentialT[0] = (fMidPointDist - fDeltaToMidPoint) / fRayLen; fPotentialT[1] = (fMidPointDist + fDeltaToMidPoint) / fRayLen; /* Make sure these t's correspond to points within the cylinder bases */ CVector3 cPoint; c_ray.GetPoint(cPoint, fPotentialT[0]); if((cPoint - m_cBasePos).DotProduct(m_cAxis) < 0 || (cPoint - (m_cBasePos + m_fHeight * m_cAxis)).DotProduct(m_cAxis) > 0) { fPotentialT[0] = -1; } c_ray.GetPoint(cPoint, fPotentialT[1]); if((cPoint - m_cBasePos).DotProduct(m_cAxis) < 0 || (cPoint - (m_cBasePos + m_fHeight * m_cAxis)).DotProduct(m_cAxis) > 0) { fPotentialT[1] = -1; } /* Check whether the ray is contained within the cylinder bases */ Real fDenominator = cRayDir.DotProduct(m_cAxis); /* Is ray parallel to plane? */ if(Abs(fDenominator) > 1e-6) { /* No, it's not parallel */ fDenominator *= fRayLen; /* Bottom base */ fPotentialT[2] = (m_cBasePos - c_ray.GetStart()).DotProduct(m_cAxis) / fDenominator; /* Top base */ fPotentialT[3] = (m_cBasePos + m_fHeight * m_cAxis - c_ray.GetStart()).DotProduct(m_cAxis) / fDenominator; /* Make sure these t's are within the cylinder surface */ c_ray.GetPoint(cPoint, fPotentialT[2]); CVector3 cDiff = cPoint - m_cBasePos; if((cDiff - cDiff.DotProduct(m_cAxis) * m_cAxis).SquareLength() > Square(m_fRadius)) fPotentialT[2] = -1; c_ray.GetPoint(cPoint, fPotentialT[3]); cDiff = cPoint - m_cBasePos; if((cDiff - cDiff.DotProduct(m_cAxis) * m_cAxis).SquareLength() > Square(m_fRadius)) fPotentialT[3] = -1; } else { /* Yes, it's parallel - discard the intersections */ fPotentialT[2] = -1.0; fPotentialT[3] = -1.0; } /* Go through all the potential t's and get the best */ f_t_on_ray = 2.0; for(UInt32 i = 0; i < 4; ++i) { if(fPotentialT[i] > 0.0f) { f_t_on_ray = Min(f_t_on_ray, fPotentialT[i]); } } /* Return true only if the intersection point is within the ray limits */ return (f_t_on_ray < 1.0f); } else { /* Yes, ray and axis are parallel */ /* Projection of cCylBase2RayStart onto the axis */ Real fProj = cCylBase2RayStart.DotProduct(m_cAxis); /* Radial vector */ CVector3 cRadial(cCylBase2RayStart); cRadial -= fProj * m_cAxis; Real fDist = cRadial.Length(); /* Is ray within the cylinder radius? */ if(fDist > m_fRadius) { /* No, it's not */ return false; } /* If we get here, it's because the ray might intersect the cylinder bases */ Real fDenominator = cRayDir.DotProduct(m_cAxis) * fRayLen; /* Create a buffer for the 2 potential intersection points */ Real fPotentialT[2]; /* Bottom base */ fPotentialT[0] = (m_cBasePos-c_ray.GetStart()).DotProduct(m_cAxis) / fDenominator; /* Top base */ fPotentialT[1] = (m_cBasePos + m_fHeight * m_cAxis - c_ray.GetStart()).DotProduct(m_cAxis) / fDenominator; /* Make sure these t's are within the cylinder surface */ CVector3 cPoint; c_ray.GetPoint(cPoint, fPotentialT[0]); CVector3 cDiff = cPoint - m_cBasePos; if((cDiff - cDiff.DotProduct(m_cAxis) * m_cAxis).SquareLength() > Square(m_fRadius)) fPotentialT[0] = -1; c_ray.GetPoint(cPoint, fPotentialT[1]); cDiff = cPoint - m_cBasePos; if((cDiff - cDiff.DotProduct(m_cAxis) * m_cAxis).SquareLength() > Square(m_fRadius)) fPotentialT[1] = -1; /* Go through all the potential t's and get the best */ f_t_on_ray = 2.0; for(UInt32 i = 0; i < 2; ++i) { if(fPotentialT[i] > 0.0f) { f_t_on_ray = Min(f_t_on_ray, fPotentialT[i]); } } /* Return true only if the intersection point is within the ray limits */ return (f_t_on_ray < 1.0f); } }
void Solver::solve() { // Utility constants const int sIFreq = 20; const double sIF = 4.0; const double sDF = 2.0; const int eIFreq = 2; const double eIF = 2.0; // Loop over all valid frames for (int f = 0; f < mMaxFrames; f++) { // Build C[] buildConstraintMat(f); //Objective function value double o = 0.0; Markers &handles = mModel->mHandleList; for (int i = 0; i < constraintIDs.size(); i++) { // Get handle and target position for the given constraint Marker *h = handles[constraintIDs[i]]; Vec3d cPos = mModel->mOpenedC3dFile->GetMarkerPos(f, constraintIDs[i]); // Calculate values and store Vec3d cVal = h->mGlobalPos - cPos; double cLength = sqrlen(cVal); //Store results constraintVals.push_back(cVal); constraintLengths.push_back(cLength); //Update objective function o += cLength; } // Main loop double e = mEps; double s = mStep; int iterCount = 0; int stepCount = 0; while (o > e) //while Objective Function > Epsilon { // Compute gradient -- TODO: Move this somewhere else for the sake of keeping this compact! Vecd gradient; gradient.SetSize(mModel->GetDofCount()); gradient.MakeZero(); for (int i = 0; i < constraintIDs.size(); i++) { //Get Constraint Vec4d cVec(constraintVals[i], 1.0); //Get Jacobian Matd J = computeJ(mModel->mHandleList, constraintIDs[i]); //Calculate current and add to gradient matrix Vecd currentVal = J * cVec; gradient = gradient + currentVal; } // Finally, double it gradient = 2.0 * gradient; // Get previous DOFs Vecd prevDofs; prevDofs.SetSize(mModel->GetDofCount()); mModel->mDofList.GetDofs(&prevDofs); // Update DOFs Vecd nextDofs = prevDofs - s * gradient; mModel->SetDofs(nextDofs); // Recalculate constraints and objective function double newO = 0.0; Markers &handles = mModel->mHandleList; for (int i = 0; i < constraintIDs.size(); i++) { // Get handle and target position for the given constraint Marker *h = handles[constraintIDs[i]]; Vec3d cPos = mModel->mOpenedC3dFile->GetMarkerPos(f, constraintIDs[i]); // Calculate values and store Vec3d cVal = h->mGlobalPos - cPos; double cLength = sqrlen(cVal); //Store results constraintVals.push_back(cVal); constraintLengths.push_back(cLength); //Update objective function newO += cLength; } // Attempting to make sure we don't go infinite... if (newO < o) { if (newO > mEps && iterCount > 0 && iterCount % 20 == 0) { s *= 3.0; } o = newO; } else { stepCount++; // Decrease step size s = s / 2.0; // based on how many times we've had to decrease the step size, choose different options if (stepCount < mMaxIters) { // Reset to the previous dofs and try again mModel->SetDofs(prevDofs); } else if (stepCount < eIFreq*mMaxIters) { // Try increasing epsilon e *= 3.0; // Reset to the previous dofs and try again mModel->SetDofs(prevDofs); } else { // Otherwise, halt or we will never finish o = 0.0; } } iterCount++; } // Update the frame counter UI->mFrameCounter_cou->value(f); //Refresh the screen UI->mGLWindow->flush(); } }