bool hitTriang(Photon *photon, Triang *triang, const float distance, const Material *materials) { Shader shader = DIFFUSE; // Better idea? if (fDot(triang->direct, photon->direct) > -EPSILON) triang->direct = vNeg(triang->direct); // Update ray's position to the point where the ray hits the sphere photon->origin = fFMA(photon->direct, distance - UM(10), photon->origin); photon->lambda = materials[triang->mID].lambda; if (shader == CRAZY) { photon->direct = vNorm(vSub(newVec(0.99, 0, 0), photon->origin)); return REFLECTED; } else if (shader == SPECULAR) { // Angle between new and old vector is twice the angle between old vector and normal Vec reflect = vDot(photon->direct, triang->direct); photon->direct = vSub(photon->direct, vMul(triang->direct, vAdd(reflect, reflect))); return REFLECTED; } else if (shader == DIFFUSE) { photon->direct = randDir(); if (fDot(photon->direct, triang->direct) < EPSILON) photon->direct = vNeg(photon->direct); return REFLECTED; } else return ABSORBED; }
vector<uint64_t> Prime::getLower(uint64_t max){ vector<bool> siev(max,true); siev[0] = false; for(uint64_t i=1;i*i<max;i++ ){ if (siev[i]){ uint64_t product = (i+1)*(i+1); while (product < max) { siev[product-1]=false; product += (i+1); } } } uint64_t lastPrime = *Primes.end(); for(uint64_t i=lastPrime;i<max;i++ ){ if (siev[i] == true) { Primes.push_back(i+1); } } long size = getSize(); while (Primes[size - 1] > max) { size--; } vector<uint64_t>::const_iterator first = Primes.begin(); vector<uint64_t>::const_iterator end = Primes.begin() + size; vector<uint64_t> newVec(first, end); return newVec; }
int GCF::ActionContainerWidgetLayout::indexOf(int row, int col) const { if(row < 0 || row >= d->rowCount) return -1; if(col < 0 || col >= d->colCount) { QVector<bool> newVec(d->rowCount*(col+1)); for(int i=0; i<newVec.count(); i++) newVec[i] = false; for(int i=0; i<d->rowCount; i++) { for(int j=0; j<d->colCount; j++) { int oldIndex = i*d->colCount + j; int newIndex = i*(col+1) + j; newVec[newIndex] = d->gridMatrix[oldIndex]; } } d->colCount = col+1; d->gridMatrix = newVec; } return row*d->colCount + col; }
//get average motion double VideoShot::averageMotion(vector<double> &frameDifferenceScore, int startIndex, int endIndex) { if(startIndex==endIndex) { return 0; } //copy vector vector<double>::const_iterator first = frameDifferenceScore.begin() + startIndex; vector<double>::const_iterator last = frameDifferenceScore.begin() + endIndex; vector<double> newVec(first, last); double median; size_t size = newVec.size(); sort(newVec.begin(), newVec.end()); if (size % 2 == 0) { median = (newVec[size / 2 - 1] + newVec[size / 2]) / 2; } else { median = newVec[size / 2]; } return median; }
std::vector<V> Vector::getSubVector(const std::vector<V>& vector, int start, int end) { auto first = vector.begin() + start; auto last = vector.end() - end; std::vector<V> newVec(first, last); return newVec; }
Vector3 utils3D::randomizeVec(const Vector3& vec, float fudge) { Vector3 newVec(randomizeValue( vec.x, fudge ), randomizeValue( vec.y, fudge ), randomizeValue( vec.z, fudge )); return newVec; }
Vector3 Vector3::operator*(const float& f1)const { Vector3 newVec( x * f1, y * f1, z * f1); return newVec; //return Vector3(x * v3, y * v3, z * v3); }
Vector3 Vector3::operator*(const Vector3& v3)const { Vector3 newVec(x * v3.x, y * v3.y, z * v3.z); return newVec; //return Vector3(x * v3, y * v3, z * v3); }
vector<int> FuzzyVariable::setFireFlag(const float& a) const { vector<int> newVec(setSize); for (int i=0;i<setSize;i++) newVec[i]=this->fuzzySet[i].member_flag(a); return newVec; }
Vector<T> Diagonal<T>::operator* (const Vector<T>& rhs) const { if(MatrixBase<T>::size() != rhs.size()) throw std::length_error("The Diagonal and Vector must be of the same size"); Vector<T> newVec(MatrixBase<T>::size()); for(unsigned int i=0; i < MatrixBase<T>::size(); i++) newVec[i] = m_matrix[i] * rhs[i]; return newVec; }
Vector3 utils3D::randomizeUnitVec(const Vector3& vec, float fudge) { assert(vec.isNormalised()); Vector3 newVec(randomizeValue( vec.x, fudge ), randomizeValue( vec.y, fudge ), randomizeValue( vec.z, fudge )); newVec.normalise(); return newVec; }
vector<uint64_t> Prime::getN(unsigned long n){ long size = getSize(); if (size >= n){ vector<uint64_t>::const_iterator first = Primes.begin(); vector<uint64_t>::const_iterator last = Primes.begin() + n; vector<uint64_t> newVec(first, last); return newVec; } int k1 = 6*num-1; int k2 = 6*num+1; while (size < n) { long newSize = size + 1; while (size < newSize){ bool isPrime=true; for (std::vector<uint64_t>::iterator it = Primes.begin()+2; it != Primes.end() && isPrime; ++it){ isPrime = (k1 % (*it) != 0); if (isPrime && (*it)*(*it) > k1 ) { it = Primes.end()-1; } } if (isPrime){ Primes.push_back(k1); size++; } isPrime=true; for (std::vector<uint64_t>::iterator it = Primes.begin()+2; it != Primes.end() && isPrime; ++it){ isPrime = (k2 % (*it) != 0); if (isPrime && (*it)*(*it) > k2 ) { it = Primes.end()-1; } } if (isPrime){ Primes.push_back(k2); size++; } num++; k1 = 6*num-1; k2 = 6*num+1; } } if (size == n) { return Primes; } else return getN(n); }
Vec Vec::Cross(const Vec & v) const{ if( _dim != v._dim ) throw Exception( "Vec::Cross(): dimension not match" ); if( _dim != 3 ) throw Exception( "Vec::Cross(): dimension should be 3" ); Vec newVec(_dim); newVec._vec[0] = _vec[1]*v._vec[2] - _vec[2]*v._vec[1]; newVec._vec[1] = -1.0f * (_vec[0]*v._vec[2] - _vec[2]*v._vec[0]); newVec._vec[2] = _vec[0]*v._vec[1] - _vec[1]*v._vec[0]; return newVec; }
Vec Vec::operator - (const Vec & v) const{ if( _dim != v._dim ) throw Exception( "Vec::operator-(): dimension not match" ); Vec newVec( _dim ); float * a = _vec; float * b = v._vec; float * c = newVec._vec; for( int i = 0; i < _dim; i++ ){ *c = *a - *b; a++; b++; c++; } return newVec; }
Vector Vector::makeDim(int dim, const Vector& vec) const { Vector newVec(dim); if( vec.d > dim ) { for( int i = 0; i < dim; i++ ) newVec.v[i] = vec.v[i]; } else // vec.d <= dim { int i; for( i = 0; i < vec.d; i++ ) newVec.v[i] = vec.v[i]; for( ; i < dim; i++ ) newVec.v[i] = 0.0; } return newVec; }
namespace trait1 { struct Vec { int size; void** data; }; Vec* newVec() { Vec* v = new Vec(); v->size = 0; v->data = new void*[v->size]; return v; } void setVec(Vec* v, int idx, void* d) { if (idx > v->size) { // resize void** data = new void*[idx+1]; memcpy(v->data, data, sizeof(void*)*v->size); v->size = idx+1; delete[] v->data; v->data = data; } v->data[idx] = d; } struct Class { int id; }; int Class_genId() { static int classId = -1; classId++; return classId; } // struct Int int Int_classId = Class_genId(); struct Int { int id; int x; Int(int x):id(Int_classId),x(x){} }; struct Fib2 { int (*fib)(Class*); }; Vec* Fib2_v = newVec(); int Fib2_Int_fib(Class* self) { Int* p = (Int*)self; if(p-> x < 2) return 1; Int p1(p->x - 2); Int p2(p->x - 1); return ((Fib2*)Fib2_v->data[p->id])->fib((Class*)&p1) + ((Fib2*)Fib2_v->data[p->id])->fib((Class*)&p2); } Fib2* newFib2_Int() { Fib2 *impl = new Fib2(); setVec(Fib2_v, Int_classId, (void*)impl); impl->fib = &Fib2_Int_fib; return impl; } Fib2* Fib2_Int_ = newFib2_Int(); int Int3_classId = Class_genId(); struct Int3 { int id; int x; }; struct Fib3 { int (*fib)(Class*); }; Vec* Fib3_v = newVec(); int Fib3_Int3_fib(Class* self) { Int3* p = (Int3*)self; if(p-> x < 2) return 1; Int3 p1 ={Int3_classId, p->x - 2}; Int3 p2 ={Int3_classId, p->x - 1}; return Fib3_Int3_fib((Class*)&p1) + Fib3_Int3_fib((Class*)&p2); } Fib3* newFib3_Int3() { Fib3 *impl = new Fib3(); setVec(Fib3_v, Int3_classId, (void*)impl); impl->fib = &Fib3_Int3_fib; return impl; } Fib3* Fib3_Int3_ = newFib3_Int3(); void bench() { long start; start = gett(); Int p(40); printf("%d\n", ((Fib2*)Fib2_v->data[p.id])->fib((Class*)&p)); printf("%ld\n", gett() - start); start = gett(); Int3 p3 = {Int3_classId, 40}; printf("%d\n", ((Fib2*)Fib3_v->data[p3.id])->fib((Class*)&p3)); printf("%ld\n", gett() - start); } };