int main() { #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, test_allocator<int> > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), test_allocator<int>(10) ); C c = std::move(c0); assert(c.bucket_count() == 7); assert(c.size() == 0); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == test_allocator<int>(10)); assert(c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(c.load_factor() == 0); assert(c.max_load_factor() == 1); assert(c0.empty()); } { typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, test_allocator<int> > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), test_allocator<int>(10) ); C c = std::move(c0); assert(c.bucket_count() == 7); assert(c.size() == 6); assert(c.count(1) == 2); assert(c.count(2) == 2); assert(c.count(3) == 1); assert(c.count(4) == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == test_allocator<int>(10)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(c.load_factor() == (float)c.size()/c.bucket_count()); assert(c.max_load_factor() == 1); assert(c0.empty()); } #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES }
// assumes cv::Mats are of type <float> int PoseEstimator::estimate(const matches_t &matches, const cv::Mat &train_kpts, const cv::Mat &query_kpts, const cv::Mat &train_pts, const cv::Mat &query_pts, const cv::Mat &K, const double baseline) { // make sure we're using floats if (train_kpts.depth() != CV_32F || query_kpts.depth() != CV_32F || train_pts.depth() != CV_32F || query_pts.depth() != CV_32F) throw std::runtime_error("Expected input to be of floating point (CV_32F)"); int nmatch = matches.size(); cout << endl << "[pe3d] Matches: " << nmatch << endl; // set up data structures for fast processing // indices to good matches std::vector<Eigen::Vector3f> t3d, q3d; std::vector<Eigen::Vector2f> t2d, q2d; std::vector<int> m; // keep track of matches for (int i=0; i<nmatch; i++) { const float* ti = train_pts.ptr<float>(matches[i].trainIdx); const float* qi = query_pts.ptr<float>(matches[i].queryIdx); const float* ti2 = train_kpts.ptr<float>(matches[i].trainIdx); const float* qi2 = query_kpts.ptr<float>(matches[i].queryIdx); // make sure we have points within range; eliminates NaNs as well if (matches[i].distance < 60 && // TODO: get this out of the estimator ti[2] < maxMatchRange && qi[2] < maxMatchRange) { m.push_back(i); t2d.push_back(Eigen::Vector2f(ti2[0],ti2[1])); q2d.push_back(Eigen::Vector2f(qi2[0],qi2[1])); t3d.push_back(Eigen::Vector3f(ti[0],ti[1],ti[2])); q3d.push_back(Eigen::Vector3f(qi[0],qi[1],qi[2])); } } nmatch = m.size(); cout << "[pe3d] Filtered matches: " << nmatch << endl; if (nmatch < 3) return 0; // can't do it... int bestinl = 0; Matrix3f Kf; cv::cv2eigen(K,Kf); // camera matrix float fb = Kf(0,0)*baseline; // focal length times baseline float maxInlierXDist2 = maxInlierXDist*maxInlierXDist; float maxInlierDDist2 = maxInlierDDist*maxInlierDDist; // set up minimum hyp pixel distance float minpdist = minPDist * Kf(0,2) * 2.0; // RANSAC loop int numchoices = 1 + numRansac / 10; for (int ii=0; ii<numRansac; ii++) { // find a candidate int k; int a=rand()%nmatch; int b; for (k=0; k<numchoices; k++) { b=rand()%nmatch; if (a!=b && (t2d[a]-t2d[b]).norm() > minpdist && (q2d[a]-q2d[b]).norm() > minpdist) // TODO: add distance check break; } if (k >= numchoices) continue; int c; for (k=0; k<numchoices+numchoices; k++) { c=rand()%nmatch; if (c!=b && c!=a && (t2d[a]-t2d[c]).norm() > minpdist && (t2d[b]-t2d[c]).norm() > minpdist && (q2d[a]-q2d[c]).norm() > minpdist && (q2d[b]-q2d[c]).norm() > minpdist) // TODO: add distance check break; } if (k >= numchoices+numchoices) continue; // get centroids Vector3f p0a = t3d[a]; Vector3f p0b = t3d[b]; Vector3f p0c = t3d[c]; Vector3f p1a = q3d[a]; Vector3f p1b = q3d[b]; Vector3f p1c = q3d[c]; Vector3f c0 = (p0a+p0b+p0c)*(1.0/3.0); Vector3f c1 = (p1a+p1b+p1c)*(1.0/3.0); // subtract out p0a -= c0; p0b -= c0; p0c -= c0; p1a -= c1; p1b -= c1; p1c -= c1; Matrix3f Hf = p1a*p0a.transpose() + p1b*p0b.transpose() + p1c*p0c.transpose(); Matrix3d H = Hf.cast<double>(); #if 0 cout << p0a.transpose() << endl; cout << p0b.transpose() << endl; cout << p0c.transpose() << endl; #endif // do the SVD thang JacobiSVD<Matrix3d> svd(H, ComputeFullU | ComputeFullV); Matrix3d V = svd.matrixV(); Matrix3d R = V * svd.matrixU().transpose(); double det = R.determinant(); //ntot++; if (det < 0.0) { //nneg++; V.col(2) = V.col(2)*-1.0; R = V * svd.matrixU().transpose(); } Vector3d cd0 = c0.cast<double>(); Vector3d cd1 = c1.cast<double>(); Vector3d tr = cd0-R*cd1; // translation // cout << "[PE test] R: " << endl << R << endl; // cout << "[PE test] t: " << tr.transpose() << endl; Vector3f trf = tr.cast<float>(); // convert to floats Matrix3f Rf = R.cast<float>(); Rf = Kf*Rf; trf = Kf*trf; // find inliers, based on image reprojection int inl = 0; for (int i=0; i<nmatch; i++) { const Vector3f &pt1 = q3d[i]; Vector3f ipt = Rf*pt1+trf; // transform query point float iz = 1.0/ipt.z(); Vector2f &kp = t2d[i]; // cout << kp.transpose() << " " << pt1.transpose() << " " << ipt.transpose() << endl; float dx = kp.x() - ipt.x()*iz; float dy = kp.y() - ipt.y()*iz; float dd = fb/t3d[i].z() - fb/ipt.z(); // diff of disparities, could pre-compute t3d if (dx*dx < maxInlierXDist2 && dy*dy < maxInlierXDist2 && dd*dd < maxInlierDDist2) // inl+=(int)fsqrt(ipt.z()); // clever way to weight closer points inl++; } if (inl > bestinl) { bestinl = inl; trans = tr.cast<float>(); // convert to floats rot = R.cast<float>(); } } cout << "[pe3d] Best inliers: " << bestinl << endl; // printf("Total ransac: %d Neg det: %d\n", ntot, nneg); // reduce matches to inliers matches_t inls; // temporary for current inliers inliers.clear(); Matrix3f Rf = Kf*rot; Vector3f trf = Kf*trans; // cout << "[pe3e] R: " << endl << rot << endl; cout << "[pe3d] t: " << trans.transpose() << endl; AngleAxisf aa; aa.fromRotationMatrix(rot); cout << "[pe3d] AA: " << aa.angle()*180.0/M_PI << " " << aa.axis().transpose() << endl; for (int i=0; i<nmatch; i++) { Vector3f &pt1 = q3d[i]; Vector3f ipt = Rf*pt1+trf; // transform query point float iz = 1.0/ipt.z(); Vector2f &kp = t2d[i]; // cout << kp.transpose() << " " << pt1.transpose() << " " << ipt.transpose() << endl; float dx = kp.x() - ipt.x()*iz; float dy = kp.y() - ipt.y()*iz; float dd = fb/t3d[i].z() - fb/ipt.z(); // diff of disparities, could pre-compute t3d if (dx*dx < maxInlierXDist2 && dy*dy < maxInlierXDist2 && dd*dd < maxInlierDDist2) inls.push_back(matches[m[i]]); } cout << "[pe3d] Final inliers: " << inls.size() << endl; // polish with SVD if (polish) { Matrix3d Rd = rot.cast<double>(); Vector3d trd = trans.cast<double>(); StereoPolish pol(5,false); pol.polish(inls,train_kpts,query_kpts,train_pts,query_pts,K,baseline, Rd,trd); AngleAxisd aa; aa.fromRotationMatrix(Rd); cout << "[pe3d] Polished t: " << trd.transpose() << endl; cout << "[pe3d] Polished AA: " << aa.angle()*180.0/M_PI << " " << aa.axis().transpose() << endl; int num = inls.size(); // get centroids Vector3f c0(0,0,0); Vector3f c1(0,0,0); for (int i=0; i<num; i++) { c0 += t3d[i]; c1 += q3d[i]; } c0 = c0 / (float)num; c1 = c1 / (float)num; // subtract out and create H matrix Matrix3f Hf; Hf.setZero(); for (int i=0; i<num; i++) { Vector3f p0 = t3d[i]-c0; Vector3f p1 = q3d[i]-c1; Hf += p1*p0.transpose(); } Matrix3d H = Hf.cast<double>(); // do the SVD thang JacobiSVD<Matrix3d> svd(H, ComputeFullU | ComputeFullV); Matrix3d V = svd.matrixV(); Matrix3d R = V * svd.matrixU().transpose(); double det = R.determinant(); //ntot++; if (det < 0.0) { //nneg++; V.col(2) = V.col(2)*-1.0; R = V * svd.matrixU().transpose(); } Vector3d cd0 = c0.cast<double>(); Vector3d cd1 = c1.cast<double>(); Vector3d tr = cd0-R*cd1; // translation aa.fromRotationMatrix(R); cout << "[pe3d] t: " << tr.transpose() << endl; cout << "[pe3d] AA: " << aa.angle()*180.0/M_PI << " " << aa.axis().transpose() << endl; #if 0 // system SysSBA sba; sba.verbose = 0; // set up nodes // should have a frame => node function Vector4d v0 = Vector4d(0,0,0,1); Quaterniond q0 = Quaternion<double>(Vector4d(0,0,0,1)); sba.addNode(v0, q0, f0.cam, true); Quaterniond qr1(rot); // from rotation matrix Vector4d temptrans = Vector4d(trans(0), trans(1), trans(2), 1.0); // sba.addNode(temptrans, qr1.normalized(), f1.cam, false); qr1.normalize(); sba.addNode(temptrans, qr1, f1.cam, false); int in = 3; if (in > (int)inls.size()) in = inls.size(); // set up projections for (int i=0; i<(int)inls.size(); i++) { // add point int i0 = inls[i].queryIdx; int i1 = inls[i].trainIdx; Vector4d pt = query_pts[i0]; sba.addPoint(pt); // projected point, ul,vl,ur Vector3d ipt; ipt(0) = f0.kpts[i0].pt.x; ipt(1) = f0.kpts[i0].pt.y; ipt(2) = ipt(0)-f0.disps[i0]; sba.addStereoProj(0, i, ipt); // projected point, ul,vl,ur ipt(0) = f1.kpts[i1].pt.x; ipt(1) = f1.kpts[i1].pt.y; ipt(2) = ipt(0)-f1.disps[i1]; sba.addStereoProj(1, i, ipt); } sba.huber = 2.0; sba.doSBA(5,10e-4,SBA_DENSE_CHOLESKY); int nbad = sba.removeBad(2.0); // cout << endl << "Removed " << nbad << " projections > 2 pixels error" << endl; sba.doSBA(5,10e-5,SBA_DENSE_CHOLESKY); // cout << endl << sba.nodes[1].trans.transpose().head(3) << endl; // get the updated transform trans = sba.nodes[1].trans.head(3); Quaterniond q1; q1 = sba.nodes[1].qrot; rot = q1.toRotationMatrix(); // set up inliers inliers.clear(); for (int i=0; i<(int)inls.size(); i++) { ProjMap &prjs = sba.tracks[i].projections; if (prjs[0].isValid && prjs[1].isValid) // valid track inliers.push_back(inls[i]); } #if 0 printf("Inliers: %d After polish: %d\n", (int)inls.size(), (int)inliers.size()); #endif #endif } inliers = inls; return (int)inls.size(); }
int test9 (void) { int ret = 0; try { printf ("TEST: (double), (float), (int) CBString operators\n"); CBString c0 ("1.2e3"), c1("100"), c2("100.55"); printf ("\t(double) \"%s\"\n", (const char *) c0); ret += 1.2e3 != (double) c0; printf ("\t(float) \"%s\"\n", (const char *) c0); ret += 1.2e3 != (float) c0; printf ("\t(int) \"%s\"\n", (const char *) c1); ret += 100 != (float) c1; printf ("\t(int) \"%s\"\n", (const char *) c2); ret += 100 != (int) c2; printf ("\t(unsigned int) \"%s\"\n", (const char *) c2); ret += 100 != (unsigned int) c2; } catch (struct CBStringException err) { printf ("Exception thrown [%d]: %s\n", __LINE__, err.what()); ret ++; } try { CBString c0 ("xxxxx"); printf ("\t(double) \"%s\"\n", (const char *) c0); ret += -1.2e3 != (double) c0; } catch (struct CBStringException err) { printf ("\tException (%s) correctly thrown\n", err.what()); } try { CBString c0 ("xxxxx"); printf ("\t(float) \"%s\"\n", (const char *) c0); ret += -1.2e3 != (float) c0; } catch (struct CBStringException err) { printf ("\tException (%s) correctly thrown\n", err.what()); } try { CBString c0 ("xxxxx"); printf ("\t(int) \"%s\"\n", (const char *) c0); ret += -100 != (int) c0; } catch (struct CBStringException err) { printf ("\tException (%s) correctly thrown\n", err.what()); } try { CBString c0 ("xxxxx"); printf ("\t(unsigned int) \"%s\"\n", (const char *) c0); ret += 1000 != (unsigned int) c0; } catch (struct CBStringException err) { printf ("\tException (%s) correctly thrown\n", err.what()); } printf ("\t# failures: %d\n", ret); return ret; }
void level_two() { vector<DPipe> DPIPES(44); vector<DoublePipe> DOUBPIPES(18); vector<CrossPipe> CROSSPIPES(3); DPipe background(600,400,1200,800); DPipe a0(50,750,100,40); DPipe a1(150,650,100,40); DPipe a2(150,550,100,40); DPipe a3(650,450,100,40); DPipe a4(550,550,100,40); DPipe a5(450,350,100,40); DPipe a6(550,250,100,40); DPipe a7(650,250,100,40); DPipe a8(750,350,100,40); DPipe a9(750,450,100,40); DPipe a10(750,550,100,40); DPipe a11(650,650,100,40); DPipe a12(550,650,100,40); DPipe a13(450,650,100,40); DPipe a14(350,550,100,40); DPipe a15(350,350,100,40); DPipe a16(350,250,100,40); DPipe a17(450,150,100,40); DPipe a18(550,150,100,40); DPipe a19(650,150,100,40); DPipe a20(750,150,100,40); DPipe a21(850,250,100,40); DPipe a22(850,350,100,40); DPipe a23(850,450,100,40); DPipe a24(850,550,100,40); DPipe a25(850,650,100,40); DPipe a26(750,750,100,40); DPipe a27(650,750,100,40); DPipe a28(550,750,100,40); DPipe a29(450,750,100,40); DPipe a30(350,750,100,40); DPipe a31(250,650,100,40); DPipe a32(250,550,100,40); DPipe a33(250,350,100,40); DPipe a34(250,250,100,40); DPipe a35(250,150,100,40); DPipe a36(350,50,100,40); DPipe a37(450,50,100,40); DPipe a38(550,50,100,40); DPipe a39(650,50,100,40); DPipe a40(750,50,100,40); DPipe a41(850,50,100,40); DPipe a42(950,150,100,40); DPipe a43(950,250,100,40); DoublePipe b0(150,750,70,40); DoublePipe b1(150,450,70,40); DoublePipe b2(550,450,70,40); DoublePipe b3(550,350,70,40); DoublePipe b4(650,350,70,40); DoublePipe b5(650,550,70,40); DoublePipe b6(450,550,70,40); DoublePipe b7(450,250,70,40); DoublePipe b8(750,250,70,40); DoublePipe b9(750,650,70,40); DoublePipe b10(350,650,70,40); DoublePipe b11(350,150,70,40); DoublePipe b12(850,150,70,40); DoublePipe b13(850,750,70,40); DoublePipe b14(250,750,70,40); DoublePipe b15(250,50,70,40); DoublePipe b16(950,50,70,40); DoublePipe b17(950,350,70,40); CrossPipe c0(250,450,100,40); CrossPipe c1(350,450,100,40); CrossPipe c2(450,450,100,40); DPIPES[0]=a0; DPIPES[1]=a1; DPIPES[2]=a2; DPIPES[3]=a3; DPIPES[4]=a4; DPIPES[5]=a5; DPIPES[6]=a6; DPIPES[7]=a7; DPIPES[8]=a8; DPIPES[9]=a9; DPIPES[10]=a10; DPIPES[11]=a11; DPIPES[12]=a12; DPIPES[13]=a13; DPIPES[14]=a14; DPIPES[15]=a15; DPIPES[16]=a16; DPIPES[17]=a17; DPIPES[18]=a18; DPIPES[19]=a19; DPIPES[20]=a20; DPIPES[21]=a21; DPIPES[22]=a22; DPIPES[23]=a23; DPIPES[24]=a24; DPIPES[25]=a25; DPIPES[26]=a26; DPIPES[27]=a27; DPIPES[28]=a28; DPIPES[29]=a29; DPIPES[30]=a30; DPIPES[31]=a31; DPIPES[32]=a32; DPIPES[33]=a33; DPIPES[34]=a34; DPIPES[35]=a35; DPIPES[36]=a36; DPIPES[37]=a37; DPIPES[38]=a38; DPIPES[39]=a39; DPIPES[40]=a40; DPIPES[41]=a41; DPIPES[42]=a42; DPIPES[43]=a43; DOUBPIPES[0]=b0; DOUBPIPES[1]=b1; DOUBPIPES[2]=b2; DOUBPIPES[3]=b3; DOUBPIPES[4]=b4; DOUBPIPES[5]=b5; DOUBPIPES[6]=b6; DOUBPIPES[7]=b7; DOUBPIPES[8]=b8; DOUBPIPES[9]=b9; DOUBPIPES[10]=b10; DOUBPIPES[11]=b11; DOUBPIPES[12]=b12; DOUBPIPES[13]=b13; DOUBPIPES[14]=b14; DOUBPIPES[15]=b15; DOUBPIPES[16]=b16; DOUBPIPES[17]=b17; CROSSPIPES[0]=c0; CROSSPIPES[1]=c1; CROSSPIPES[2]=c2; }
int main() { #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { typedef test_allocator<int> A; typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A(4) ); c = std::move(c0); assert(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(*i == 1); ++i; assert(*i == 1); ++i; assert(*i == 2); ++i; assert(*i == 2); ++i; assert(*i == 3); ++i; assert(*i == 4); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(4)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } { typedef test_allocator<int> A; typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A(10) ); c = std::move(c0); assert(c.bucket_count() == 7); assert(c.size() == 6); assert(c.count(1) == 2); assert(c.count(2) == 2); assert(c.count(3) == 1); assert(c.count(4) == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(10)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } { typedef other_allocator<int> A; typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A(4) ); c = std::move(c0); assert(c.bucket_count() == 7); assert(c.size() == 6); assert(c.count(1) == 2); assert(c.count(2) == 2); assert(c.count(3) == 1); assert(c.count(4) == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(10)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES }
int main(int, char**) { { typedef std::pair<int, std::string> P; typedef test_allocator<std::pair<const int, std::string>> A; typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(std::move(c0), A(12)); assert(c.bucket_count() >= 5); assert(c.size() == 4); assert(c.at(1) == "one"); assert(c.at(2) == "two"); assert(c.at(3) == "three"); assert(c.at(4) == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(12)); assert(!c.empty()); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c0.empty()); } { typedef std::pair<int, std::string> P; typedef test_allocator<std::pair<const int, std::string>> A; typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(std::move(c0), A(10)); LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 4); assert(c.at(1) == "one"); assert(c.at(2) == "two"); assert(c.at(3) == "three"); assert(c.at(4) == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(10)); assert(!c.empty()); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c0.empty()); } { typedef std::pair<int, std::string> P; typedef min_allocator<std::pair<const int, std::string>> A; typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A() ); C c(std::move(c0), A()); LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 4); assert(c.at(1) == "one"); assert(c.at(2) == "two"); assert(c.at(3) == "three"); assert(c.at(4) == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A()); assert(!c.empty()); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c0.empty()); } { typedef std::pair<int, std::string> P; typedef explicit_allocator<std::pair<const int, std::string>> A; typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A{} ); C c(std::move(c0), A{}); LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 4); assert(c.at(1) == "one"); assert(c.at(2) == "two"); assert(c.at(3) == "three"); assert(c.at(4) == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A{}); assert(!c.empty()); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c0.empty()); } return 0; }
//------------------------------------------------------------------------------ void TileRenderer::RenderTile(std::string tile_uri, mapnik::Map m, int x, int y, int zoom, GoogleProjection tileproj, mapnik::projection prj, bool verbose, bool overrideTile, bool lockEnabled, std::string compositionLayerPath, std::string compositionMode, double compositionAlpha) { if(!overrideTile && FileSystem::FileExists(tile_uri)) { return; } else { // Calculate pixel positions of bottom-left & top-right ituple p0(x * 256, (y + 1) * 256); ituple p1((x + 1) * 256, y * 256); // Convert to LatLong (EPSG:4326) dtuple l0 = tileproj.pixel2GeoCoord(p0, zoom); dtuple l1 = tileproj.pixel2GeoCoord(p1, zoom); // Convert to map projection (e.g. mercator co-ords EPSG:900913) dtuple c0(l0.a,l0.b); dtuple c1(l1.a,l1.b); prj.forward(c0.a, c0.b); prj.forward(c1.a, c1.b); // Bounding box for the tile #ifndef MAPNIK_2 mapnik::Envelope<double> bbox = mapnik::Envelope<double>(c0.a,c0.b,c1.a,c1.b); m.resize(256,256); m.zoomToBox(bbox); #else mapnik::box2d<double> bbox(c0.a,c0.b,c1.a,c1.b); m.resize(256,256); m.zoom_to_box(bbox); #endif m.set_buffer_size(128); // Render image with default Agg renderer #ifndef MAPNIK_2 mapnik::Image32 buf(m.getWidth(),m.getHeight()); mapnik::agg_renderer<mapnik::Image32> ren(m,buf); #else mapnik::image_32 buf(m.width(), m.height()); mapnik::agg_renderer<mapnik::image_32> ren(m,buf); #endif ren.apply(); if(lockEnabled) { int lockhandle = FileSystem::Lock(tile_uri); #ifndef MAPNIK_2 Compose(compositionLayerPath, compositionMode, compositionAlpha, m.getWidth(),m.getHeight(),&buf, zoom, x,y); mapnik::save_to_file<mapnik::ImageData32>(buf.data(),tile_uri,"png"); #else Compose(compositionLayerPath, compositionMode, compositionAlpha, m.width(),m.height(),&buf, zoom, x,y); mapnik::save_to_file<mapnik::image_data_32>(buf.data(),tile_uri,"png"); #endif FileSystem::Unlock(tile_uri, lockhandle); } else { #ifndef MAPNIK_2 Compose(compositionLayerPath, compositionMode, compositionAlpha, m.getWidth(),m.getHeight(),&buf, zoom, x,y); mapnik::save_to_file<mapnik::ImageData32>(buf.data(),tile_uri,"png"); #else Compose(compositionLayerPath, compositionMode, compositionAlpha, m.width(),m.height(),&buf, zoom, x,y); mapnik::save_to_file<mapnik::image_data_32>(buf.data(),tile_uri,"png"); #endif } } }
int main() { // check that it'll find nodes exactly MAX away { tree_type exact_dist(std::ptr_fun(tac)); triplet c0(5, 4, 0); exact_dist.insert(c0); triplet target(7,4,0); std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target,2); assert(found.first != exact_dist.end()); assert(found.second == 2); std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << std::endl; } // do the same test, except use alternate_triplet as the search key { // NOTE: stores triplet, but we search with alternate_triplet typedef KDTree::KDTree<3, triplet, alternate_tac> alt_tree; triplet actual_target(7,0,0); alt_tree tree; tree.insert( triplet(0, 0, 7) ); tree.insert( triplet(0, 0, 7) ); tree.insert( triplet(0, 0, 7) ); tree.insert( triplet(3, 0, 0) ); tree.insert( actual_target ); tree.optimise(); alternate_triplet target( actual_target ); std::pair<alt_tree::const_iterator,double> found = tree.find_nearest(target); assert(found.first != tree.end()); std::cout << "Test with alternate search type, found: " << *found.first << ", wanted " << actual_target << std::endl; assert(found.second == 0); assert(*found.first == actual_target); } { tree_type exact_dist(std::ptr_fun(tac)); triplet c0(5, 2, 0); exact_dist.insert(c0); triplet target(7,4,0); // call find_nearest without a range value - it found a compile error earlier. std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target); assert(found.first != exact_dist.end()); std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << " @ " << found.second << " should be " << std::sqrt(8.0) << std::endl; assert(found.second == std::sqrt(8.0)); } { tree_type exact_dist(std::ptr_fun(tac)); triplet c0(5, 2, 0); exact_dist.insert(c0); triplet target(7,4,0); std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target,std::sqrt(8.0)); assert(found.first != exact_dist.end()); std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << " @ " << found.second << " should be " << std::sqrt(8.0) << std::endl; assert(found.second == std::sqrt(8.0)); } tree_type src(std::ptr_fun(tac)); triplet c0(5, 4, 0); src.insert(c0); triplet c1(4, 2, 1); src.insert(c1); triplet c2(7, 6, 9); src.insert(c2); triplet c3(2, 2, 1); src.insert(c3); triplet c4(8, 0, 5); src.insert(c4); triplet c5(5, 7, 0); src.insert(c5); triplet c6(3, 3, 8); src.insert(c6); triplet c7(9, 7, 3); src.insert(c7); triplet c8(2, 2, 6); src.insert(c8); triplet c9(2, 0, 6); src.insert(c9); std::cout << src << std::endl; src.erase(c0); src.erase(c1); src.erase(c3); src.erase(c5); src.optimise(); // test the efficient_replace_and_optimise() tree_type eff_repl = src; { std::vector<triplet> vec; // erased above as part of test vec.push_back(triplet(5, 4, 0)); // erased above as part of test vec.push_back(triplet(4, 2, 1)); vec.push_back(triplet(7, 6, 9)); // erased above as part of test vec.push_back(triplet(2, 2, 1)); vec.push_back(triplet(8, 0, 5)); // erased above as part of test vec.push_back(triplet(5, 7, 0)); vec.push_back(triplet(3, 3, 8)); vec.push_back(triplet(9, 7, 3)); vec.push_back(triplet(2, 2, 6)); vec.push_back(triplet(2, 0, 6)); eff_repl.clear(); eff_repl.efficient_replace_and_optimise(vec); } std::cout << std::endl << src << std::endl; tree_type copied(src); std::cout << copied << std::endl; tree_type assigned(std::ptr_fun(tac)); assigned = src; std::cout << assigned << std::endl; for (int loop = 0; loop != 4; ++loop) { tree_type * target; switch (loop) { case 0: std::cout << "Testing plain construction" << std::endl; target = &src; break; case 1: std::cout << "Testing copy-construction" << std::endl; target = &copied; break; case 2: std::cout << "Testing assign-construction" << std::endl; target = &assigned; break; default: case 4: std::cout << "Testing efficient-replace-and-optimise" << std::endl; target = &eff_repl; break; } tree_type & t = *target; int i=0; for (tree_type::const_iterator iter=t.begin(); iter!=t.end(); ++iter, ++i); std::cout << "iterator walked through " << i << " nodes in total" << std::endl; if (i!=6) { std::cerr << "Error: does not tally with the expected number of nodes (6)" << std::endl; return 1; } i=0; for (tree_type::const_reverse_iterator iter=t.rbegin(); iter!=t.rend(); ++iter, ++i); std::cout << "reverse_iterator walked through " << i << " nodes in total" << std::endl; if (i!=6) { std::cerr << "Error: does not tally with the expected number of nodes (6)" << std::endl; return 1; } triplet s(5, 4, 3); std::vector<triplet> v; unsigned int const RANGE = 3; size_t count = t.count_within_range(s, RANGE); std::cout << "counted " << count << " nodes within range " << RANGE << " of " << s << ".\n"; t.find_within_range(s, RANGE, std::back_inserter(v)); std::cout << "found " << v.size() << " nodes within range " << RANGE << " of " << s << ":\n"; std::vector<triplet>::const_iterator ci = v.begin(); for (; ci != v.end(); ++ci) std::cout << *ci << " "; std::cout << "\n" << std::endl; std::cout << std::endl << t << std::endl; // search for all the nodes at exactly 0 dist away for (tree_type::const_iterator target = t.begin(); target != t.end(); ++target) { std::pair<tree_type::const_iterator,double> found = t.find_nearest(*target,0); assert(found.first != t.end()); assert(*found.first == *target); std::cout << "Test find_nearest(), found at exact distance away from " << *target << ", found " << *found.first << std::endl; } { const double small_dist = 0.0001; std::pair<tree_type::const_iterator,double> notfound = t.find_nearest(s,small_dist); std::cout << "Test find_nearest(), nearest to " << s << " within " << small_dist << " should not be found" << std::endl; if (notfound.first != t.end()) { std::cout << "ERROR found a node at dist " << notfound.second << " : " << *notfound.first << std::endl; std::cout << "Actual distance = " << s.distance_to(*notfound.first) << std::endl; } assert(notfound.first == t.end()); } { std::pair<tree_type::const_iterator,double> nif = t.find_nearest_if(s,std::numeric_limits<double>::max(),Predicate()); std::cout << "Test find_nearest_if(), nearest to " << s << " @ " << nif.second << ": " << *nif.first << std::endl; std::pair<tree_type::const_iterator,double> cantfind = t.find_nearest_if(s,std::numeric_limits<double>::max(),FalsePredicate()); std::cout << "Test find_nearest_if(), nearest to " << s << " should never be found (predicate too strong)" << std::endl; assert(cantfind.first == t.end()); } { std::pair<tree_type::const_iterator,double> found = t.find_nearest(s,std::numeric_limits<double>::max()); std::cout << "Nearest to " << s << " @ " << found.second << " " << *found.first << std::endl; std::cout << "Should be " << found.first->distance_to(s) << std::endl; // NOTE: the assert does not check for an exact match, as it is not exact when -O2 or -O3 is // switched on. Some sort of optimisation makes the math inexact. assert( fabs(found.second - found.first->distance_to(s)) < std::numeric_limits<double>::epsilon() ); } { triplet s2(10, 10, 2); std::pair<tree_type::const_iterator,double> found = t.find_nearest(s2,std::numeric_limits<double>::max()); std::cout << "Nearest to " << s2 << " @ " << found.second << " " << *found.first << std::endl; std::cout << "Should be " << found.first->distance_to(s2) << std::endl; // NOTE: the assert does not check for an exact match, as it is not exact when -O2 or -O3 is // switched on. Some sort of optimisation makes the math inexact. assert( fabs(found.second - found.first->distance_to(s2)) < std::numeric_limits<double>::epsilon() ); } std::cout << std::endl; std::cout << t << std::endl; // Testing iterators { std::cout << "Testing iterators" << std::endl; t.erase(c2); t.erase(c4); t.erase(c6); t.erase(c7); t.erase(c8); // t.erase(c9); std::cout << std::endl << t << std::endl; std::cout << "Forward iterator test..." << std::endl; std::vector<triplet> forwards; for (tree_type::iterator i = t.begin(); i != t.end(); ++i) { std::cout << *i << " " << std::flush; forwards.push_back(*i); } std::cout << std::endl; std::cout << "Reverse iterator test..." << std::endl; std::vector<triplet> backwards; for (tree_type::reverse_iterator i = t.rbegin(); i != t.rend(); ++i) { std::cout << *i << " " << std::flush; backwards.push_back(*i); } std::cout << std::endl; std::reverse(backwards.begin(),backwards.end()); assert(backwards == forwards); } } // Walter reported that the find_within_range() wasn't giving results that were within // the specified range... this is the test. { // note: we need to store doubles in this tree. typedef KDTree::KDTree<3, triplet_t<double>, std::pointer_to_binary_function<triplet_t<double>,size_t,double> > dbl_tree_type; dbl_tree_type tree(std::ptr_fun(tac_dbl)); tree.insert( triplet_t<double>(28.771200,16.921600,-2.665970) ); tree.insert( triplet_t<double>(28.553101,18.649700,-2.155560) ); tree.insert( triplet_t<double>(28.107500,20.341400,-1.188940) ); tree.optimise(); std::deque< triplet_t<double> > vectors; triplet_t<double> sv(18.892500,20.341400,-1.188940); tree.find_within_range(sv, 10.0f, std::back_inserter(vectors)); std::cout << std::endl << "Test find_with_range( " << sv << ", 10.0f) found " << vectors.size() << " candidates." << std::endl; // double-check the ranges for (std::deque<triplet_t<double> >::iterator v = vectors.begin(); v != vectors.end(); ++v) { double dist = sv.distance_to(*v); std::cout << " " << *v << " dist=" << dist << std::endl; if (dist > 10.0f) std::cout << " This point is too far! But that is by design, its within a 'box' with a 'radius' of 10, not a sphere with a radius of 10" << std::endl; // Not a valid test, it can be greater than 10 if the point is in the corners of the box. // assert(dist <= 10.0f); } } return 0; }
void GSVertexTrace::FindMinMax(const void* vertex, const uint32* index, int count) { const GSDrawingContext* context = m_state->m_context; int n = 1; switch(primclass) { case GS_POINT_CLASS: n = 1; break; case GS_LINE_CLASS: case GS_SPRITE_CLASS: n = 2; break; case GS_TRIANGLE_CLASS: n = 3; break; } GSVector4 tmin = s_minmax.xxxx(); GSVector4 tmax = s_minmax.yyyy(); GSVector4i cmin = GSVector4i::xffffffff(); GSVector4i cmax = GSVector4i::zero(); #if _M_SSE >= 0x401 GSVector4i pmin = GSVector4i::xffffffff(); GSVector4i pmax = GSVector4i::zero(); #else GSVector4 pmin = s_minmax.xxxx(); GSVector4 pmax = s_minmax.yyyy(); #endif const GSVertex* RESTRICT v = (GSVertex*)vertex; for(int i = 0; i < count; i += n) { if(primclass == GS_POINT_CLASS) { GSVector4i c(v[index[i]].m[0]); if(color) { cmin = cmin.min_u8(c); cmax = cmax.max_u8(c); } if(tme) { if(!fst) { GSVector4 stq = GSVector4::cast(c); GSVector4 q = stq.wwww(); stq = (stq.xyww() * q.rcpnr()).xyww(q); tmin = tmin.min(stq); tmax = tmax.max(stq); } else { GSVector4i uv(v[index[i]].m[1]); GSVector4 st = GSVector4(uv.uph16()).xyxy(); tmin = tmin.min(st); tmax = tmax.max(st); } } GSVector4i xyzf(v[index[i]].m[1]); GSVector4i xy = xyzf.upl16(); GSVector4i z = xyzf.yyyy(); #if _M_SSE >= 0x401 GSVector4i p = xy.blend16<0xf0>(z.uph32(xyzf)); pmin = pmin.min_u32(p); pmax = pmax.max_u32(p); #else GSVector4 p = GSVector4(xy.upl64(z.srl32(1).upl32(xyzf.wwww()))); pmin = pmin.min(p); pmax = pmax.max(p); #endif } else if(primclass == GS_LINE_CLASS) { GSVector4i c0(v[index[i + 0]].m[0]); GSVector4i c1(v[index[i + 1]].m[0]); if(color) { if(iip) { cmin = cmin.min_u8(c0.min_u8(c1)); cmax = cmax.max_u8(c0.max_u8(c1)); } else { cmin = cmin.min_u8(c1); cmax = cmax.max_u8(c1); } } if(tme) { if(!fst) { GSVector4 stq0 = GSVector4::cast(c0); GSVector4 stq1 = GSVector4::cast(c1); GSVector4 q = stq0.wwww(stq1).rcpnr(); stq0 = (stq0.xyww() * q.xxxx()).xyww(stq0); stq1 = (stq1.xyww() * q.zzzz()).xyww(stq1); tmin = tmin.min(stq0.min(stq1)); tmax = tmax.max(stq0.max(stq1)); } else { GSVector4i uv0(v[index[i + 0]].m[1]); GSVector4i uv1(v[index[i + 1]].m[1]); GSVector4 st0 = GSVector4(uv0.uph16()).xyxy(); GSVector4 st1 = GSVector4(uv1.uph16()).xyxy(); tmin = tmin.min(st0.min(st1)); tmax = tmax.max(st0.max(st1)); } } GSVector4i xyzf0(v[index[i + 0]].m[1]); GSVector4i xyzf1(v[index[i + 1]].m[1]); GSVector4i xy0 = xyzf0.upl16(); GSVector4i z0 = xyzf0.yyyy(); GSVector4i xy1 = xyzf1.upl16(); GSVector4i z1 = xyzf1.yyyy(); #if _M_SSE >= 0x401 GSVector4i p0 = xy0.blend16<0xf0>(z0.uph32(xyzf0)); GSVector4i p1 = xy1.blend16<0xf0>(z1.uph32(xyzf1)); pmin = pmin.min_u32(p0.min_u32(p1)); pmax = pmax.max_u32(p0.max_u32(p1)); #else GSVector4 p0 = GSVector4(xy0.upl64(z0.srl32(1).upl32(xyzf0.wwww()))); GSVector4 p1 = GSVector4(xy1.upl64(z1.srl32(1).upl32(xyzf1.wwww()))); pmin = pmin.min(p0.min(p1)); pmax = pmax.max(p0.max(p1)); #endif } else if(primclass == GS_TRIANGLE_CLASS) { GSVector4i c0(v[index[i + 0]].m[0]); GSVector4i c1(v[index[i + 1]].m[0]); GSVector4i c2(v[index[i + 2]].m[0]); if(color) { if(iip) { cmin = cmin.min_u8(c2).min_u8(c0.min_u8(c1)); cmax = cmax.max_u8(c2).max_u8(c0.max_u8(c1)); } else { cmin = cmin.min_u8(c2); cmax = cmax.max_u8(c2); } } if(tme) { if(!fst) { GSVector4 stq0 = GSVector4::cast(c0); GSVector4 stq1 = GSVector4::cast(c1); GSVector4 stq2 = GSVector4::cast(c2); GSVector4 q = stq0.wwww(stq1).xzww(stq2).rcpnr(); stq0 = (stq0.xyww() * q.xxxx()).xyww(stq0); stq1 = (stq1.xyww() * q.yyyy()).xyww(stq1); stq2 = (stq2.xyww() * q.zzzz()).xyww(stq2); tmin = tmin.min(stq2).min(stq0.min(stq1)); tmax = tmax.max(stq2).max(stq0.max(stq1)); } else { GSVector4i uv0(v[index[i + 0]].m[1]); GSVector4i uv1(v[index[i + 1]].m[1]); GSVector4i uv2(v[index[i + 2]].m[1]); GSVector4 st0 = GSVector4(uv0.uph16()).xyxy(); GSVector4 st1 = GSVector4(uv1.uph16()).xyxy(); GSVector4 st2 = GSVector4(uv2.uph16()).xyxy(); tmin = tmin.min(st2).min(st0.min(st1)); tmax = tmax.max(st2).max(st0.max(st1)); } } GSVector4i xyzf0(v[index[i + 0]].m[1]); GSVector4i xyzf1(v[index[i + 1]].m[1]); GSVector4i xyzf2(v[index[i + 2]].m[1]); GSVector4i xy0 = xyzf0.upl16(); GSVector4i z0 = xyzf0.yyyy(); GSVector4i xy1 = xyzf1.upl16(); GSVector4i z1 = xyzf1.yyyy(); GSVector4i xy2 = xyzf2.upl16(); GSVector4i z2 = xyzf2.yyyy(); #if _M_SSE >= 0x401 GSVector4i p0 = xy0.blend16<0xf0>(z0.uph32(xyzf0)); GSVector4i p1 = xy1.blend16<0xf0>(z1.uph32(xyzf1)); GSVector4i p2 = xy2.blend16<0xf0>(z2.uph32(xyzf2)); pmin = pmin.min_u32(p2).min_u32(p0.min_u32(p1)); pmax = pmax.max_u32(p2).max_u32(p0.max_u32(p1)); #else GSVector4 p0 = GSVector4(xy0.upl64(z0.srl32(1).upl32(xyzf0.wwww()))); GSVector4 p1 = GSVector4(xy1.upl64(z1.srl32(1).upl32(xyzf1.wwww()))); GSVector4 p2 = GSVector4(xy2.upl64(z2.srl32(1).upl32(xyzf2.wwww()))); pmin = pmin.min(p2).min(p0.min(p1)); pmax = pmax.max(p2).max(p0.max(p1)); #endif } else if(primclass == GS_SPRITE_CLASS) { GSVector4i c0(v[index[i + 0]].m[0]); GSVector4i c1(v[index[i + 1]].m[0]); if(color) { if(iip) { cmin = cmin.min_u8(c0.min_u8(c1)); cmax = cmax.max_u8(c0.max_u8(c1)); } else { cmin = cmin.min_u8(c1); cmax = cmax.max_u8(c1); } } if(tme) { if(!fst) { GSVector4 stq0 = GSVector4::cast(c0); GSVector4 stq1 = GSVector4::cast(c1); GSVector4 q = stq1.wwww().rcpnr(); stq0 = (stq0.xyww() * q).xyww(stq1); stq1 = (stq1.xyww() * q).xyww(stq1); tmin = tmin.min(stq0.min(stq1)); tmax = tmax.max(stq0.max(stq1)); } else { GSVector4i uv0(v[index[i + 0]].m[1]); GSVector4i uv1(v[index[i + 1]].m[1]); GSVector4 st0 = GSVector4(uv0.uph16()).xyxy(); GSVector4 st1 = GSVector4(uv1.uph16()).xyxy(); tmin = tmin.min(st0.min(st1)); tmax = tmax.max(st0.max(st1)); } } GSVector4i xyzf0(v[index[i + 0]].m[1]); GSVector4i xyzf1(v[index[i + 1]].m[1]); GSVector4i xy0 = xyzf0.upl16(); GSVector4i z0 = xyzf0.yyyy(); GSVector4i xy1 = xyzf1.upl16(); GSVector4i z1 = xyzf1.yyyy(); #if _M_SSE >= 0x401 GSVector4i p0 = xy0.blend16<0xf0>(z0.uph32(xyzf1)); GSVector4i p1 = xy1.blend16<0xf0>(z1.uph32(xyzf1)); pmin = pmin.min_u32(p0.min_u32(p1)); pmax = pmax.max_u32(p0.max_u32(p1)); #else GSVector4 p0 = GSVector4(xy0.upl64(z0.srl32(1).upl32(xyzf1.wwww()))); GSVector4 p1 = GSVector4(xy1.upl64(z1.srl32(1).upl32(xyzf1.wwww()))); pmin = pmin.min(p0.min(p1)); pmax = pmax.max(p0.max(p1)); #endif } } #if _M_SSE >= 0x401 pmin = pmin.blend16<0x30>(pmin.srl32(1)); pmax = pmax.blend16<0x30>(pmax.srl32(1)); #endif GSVector4 o(context->XYOFFSET); GSVector4 s(1.0f / 16, 1.0f / 16, 2.0f, 1.0f); m_min.p = (GSVector4(pmin) - o) * s; m_max.p = (GSVector4(pmax) - o) * s; if(tme) { if(fst) { s = GSVector4(1.0f / 16, 1.0f).xxyy(); } else { s = GSVector4(1 << context->TEX0.TW, 1 << context->TEX0.TH, 1, 1); } m_min.t = tmin * s; m_max.t = tmax * s; } else { m_min.t = GSVector4::zero(); m_max.t = GSVector4::zero(); } if(color) { m_min.c = cmin.zzzz().u8to32(); m_max.c = cmax.zzzz().u8to32(); } else { m_min.c = GSVector4i::zero(); m_max.c = GSVector4i::zero(); } }
int main() { #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { typedef test_allocator<int> A; typedef std::unordered_set<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A(4) ); c = std::move(c0); assert(c.bucket_count() == 7); assert(c.size() == 4); assert(c.count(1) == 1); assert(c.count(2) == 1); assert(c.count(3) == 1); assert(c.count(4) == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(4)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } { typedef test_allocator<int> A; typedef std::unordered_set<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A(10) ); c = std::move(c0); assert(c.bucket_count() == 7); assert(c.size() == 4); assert(c.count(1) == 1); assert(c.count(2) == 1); assert(c.count(3) == 1); assert(c.count(4) == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(10)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } { typedef other_allocator<int> A; typedef std::unordered_set<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A(4) ); c = std::move(c0); assert(c.bucket_count() == 7); assert(c.size() == 4); assert(c.count(1) == 1); assert(c.count(2) == 1); assert(c.count(3) == 1); assert(c.count(4) == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(10)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #if TEST_STD_VER >= 11 { typedef min_allocator<int> A; typedef std::unordered_set<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A() ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A() ); c = std::move(c0); assert(c.bucket_count() == 7); assert(c.size() == 4); assert(c.count(1) == 1); assert(c.count(2) == 1); assert(c.count(3) == 1); assert(c.count(4) == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A()); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #endif #if _LIBCPP_DEBUG >= 1 { std::unordered_set<int> s1 = {1, 2, 3}; std::unordered_set<int>::iterator i = s1.begin(); int k = *i; std::unordered_set<int> s2; s2 = std::move(s1); assert(*i == k); s2.erase(i); assert(s2.size() == 2); } #endif #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES }
static void installMinEQ3asShomateThermoFromXML(std::string speciesName, ThermoPhase *th_ptr, SpeciesThermo& sp, int k, const XML_Node* MinEQ3node) { array_fp coef(15), c0(7, 0.0); std::string astring = (*MinEQ3node)["Tmin"]; doublereal tmin0 = strSItoDbl(astring); astring = (*MinEQ3node)["Tmax"]; doublereal tmax0 = strSItoDbl(astring); astring = (*MinEQ3node)["Pref"]; doublereal p0 = strSItoDbl(astring); doublereal deltaG_formation_pr_tr = getFloatDefaultUnits(*MinEQ3node, "DG0_f_Pr_Tr", "cal/gmol", "actEnergy"); doublereal deltaH_formation_pr_tr = getFloatDefaultUnits(*MinEQ3node, "DH0_f_Pr_Tr", "cal/gmol", "actEnergy"); doublereal Entrop_pr_tr = getFloatDefaultUnits(*MinEQ3node, "S0_Pr_Tr", "cal/gmol/K"); doublereal a = getFloatDefaultUnits(*MinEQ3node, "a", "cal/gmol/K"); doublereal b = getFloatDefaultUnits(*MinEQ3node, "b", "cal/gmol/K2"); doublereal c = getFloatDefaultUnits(*MinEQ3node, "c", "cal-K/gmol"); doublereal dg = deltaG_formation_pr_tr * 4.184 * 1.0E3; doublereal fac = convertDGFormation(k, th_ptr); doublereal Mu0_tr_pr = fac + dg; doublereal e = Entrop_pr_tr * 1.0E3 * 4.184; doublereal Hcalc = Mu0_tr_pr + 298.15 * e; doublereal DHjmol = deltaH_formation_pr_tr * 1.0E3 * 4.184; // If the discrepency is greater than 100 cal gmol-1, print // an error and exit. if (fabs(Hcalc -DHjmol) > 10.* 1.0E6 * 4.184) { throw CanteraError("installMinEQ3asShomateThermoFromXML()", "DHjmol is not consistent with G and S" + fp2str(Hcalc) + " vs " + fp2str(DHjmol)); } /* * Now calculate the shomate polynomials * * Cp first * * Shomate: (Joules / gmol / K) * Cp = As + Bs * t + Cs * t*t + Ds * t*t*t + Es / (t*t) * where * t = temperature(Kelvin) / 1000 */ double As = a * 4.184; double Bs = b * 4.184 * 1000.; double Cs = 0.0; double Ds = 0.0; double Es = c * 4.184 / (1.0E6); double t = 298.15 / 1000.; double H298smFs = As * t + Bs * t * t / 2.0 - Es / t; double HcalcS = Hcalc / 1.0E6; double Fs = HcalcS - H298smFs; double S298smGs = As * log(t) + Bs * t - Es/(2.0*t*t); double ScalcS = e / 1.0E3; double Gs = ScalcS - S298smGs; c0[0] = As; c0[1] = Bs; c0[2] = Cs; c0[3] = Ds; c0[4] = Es; c0[5] = Fs; c0[6] = Gs; coef[0] = tmax0 - 0.001; copy(c0.begin(), c0.begin()+7, coef.begin() + 1); copy(c0.begin(), c0.begin()+7, coef.begin() + 8); sp.install(speciesName, k, SHOMATE, &coef[0], tmin0, tmax0, p0); }
/************************************************************************* Weighted constained linear least squares fitting. This is variation of LSFitLinearW(), which searchs for min|A*x=b| given that K additional constaints C*x=bc are satisfied. It reduces original task to modified one: min|B*y-d| WITHOUT constraints, then LSFitLinearW() is called. INPUT PARAMETERS: Y - array[0..N-1] Function values in N points. W - array[0..N-1] Weights corresponding to function values. Each summand in square sum of approximation deviations from given values is multiplied by the square of corresponding weight. FMatrix - a table of basis functions values, array[0..N-1, 0..M-1]. FMatrix[I,J] - value of J-th basis function in I-th point. CMatrix - a table of constaints, array[0..K-1,0..M]. I-th row of CMatrix corresponds to I-th linear constraint: CMatrix[I,0]*C[0] + ... + CMatrix[I,M-1]*C[M-1] = CMatrix[I,M] N - number of points used. N>=1. M - number of basis functions, M>=1. K - number of constraints, 0 <= K < M K=0 corresponds to absence of constraints. OUTPUT PARAMETERS: Info - error code: * -4 internal SVD decomposition subroutine failed (very rare and for degenerate systems only) * -3 either too many constraints (M or more), degenerate constraints (some constraints are repetead twice) or inconsistent constraints were specified. * -1 incorrect N/M/K were specified * 1 task is solved C - decomposition coefficients, array[0..M-1] Rep - fitting report. Following fields are set: * RMSError rms error on the (X,Y). * AvgError average error on the (X,Y). * AvgRelError average relative error on the non-zero Y * MaxError maximum error NON-WEIGHTED ERRORS ARE CALCULATED IMPORTANT: this subroitine doesn't calculate task's condition number for K<>0. SEE ALSO LSFitLinear LSFitLinearC LSFitLinearWC -- ALGLIB -- Copyright 07.09.2009 by Bochkanov Sergey *************************************************************************/ void lsfitlinearwc(ap::real_1d_array y, const ap::real_1d_array& w, const ap::real_2d_array& fmatrix, ap::real_2d_array cmatrix, int n, int m, int k, int& info, ap::real_1d_array& c, lsfitreport& rep) { int i; int j; ap::real_1d_array tau; ap::real_2d_array q; ap::real_2d_array f2; ap::real_1d_array tmp; ap::real_1d_array c0; double v; if( n<1||m<1||k<0 ) { info = -1; return; } if( k>=m ) { info = -3; return; } // // Solve // if( k==0 ) { // // no constraints // lsfitlinearinternal(y, w, fmatrix, n, m, info, c, rep); } else { // // First, find general form solution of constraints system: // * factorize C = L*Q // * unpack Q // * fill upper part of C with zeros (for RCond) // // We got C=C0+Q2'*y where Q2 is lower M-K rows of Q. // rmatrixlq(cmatrix, k, m, tau); rmatrixlqunpackq(cmatrix, k, m, tau, m, q); for(i = 0; i <= k-1; i++) { for(j = i+1; j <= m-1; j++) { cmatrix(i,j) = 0.0; } } if( ap::fp_less(rmatrixlurcondinf(cmatrix, k),1000*ap::machineepsilon) ) { info = -3; return; } tmp.setlength(k); for(i = 0; i <= k-1; i++) { if( i>0 ) { v = ap::vdotproduct(&cmatrix(i, 0), 1, &tmp(0), 1, ap::vlen(0,i-1)); } else { v = 0; } tmp(i) = (cmatrix(i,m)-v)/cmatrix(i,i); } c0.setlength(m); for(i = 0; i <= m-1; i++) { c0(i) = 0; } for(i = 0; i <= k-1; i++) { v = tmp(i); ap::vadd(&c0(0), 1, &q(i, 0), 1, ap::vlen(0,m-1), v); } // // Second, prepare modified matrix F2 = F*Q2' and solve modified task // tmp.setlength(ap::maxint(n, m)+1); f2.setlength(n, m-k); matrixvectormultiply(fmatrix, 0, n-1, 0, m-1, false, c0, 0, m-1, -1.0, y, 0, n-1, 1.0); matrixmatrixmultiply(fmatrix, 0, n-1, 0, m-1, false, q, k, m-1, 0, m-1, true, 1.0, f2, 0, n-1, 0, m-k-1, 0.0, tmp); lsfitlinearinternal(y, w, f2, n, m-k, info, tmp, rep); rep.taskrcond = -1; if( info<=0 ) { return; } // // then, convert back to original answer: C = C0 + Q2'*Y0 // c.setlength(m); ap::vmove(&c(0), 1, &c0(0), 1, ap::vlen(0,m-1)); matrixvectormultiply(q, k, m-1, 0, m-1, true, tmp, 0, m-k-1, 1.0, c, 0, m-1, 1.0); } }
int tc_libcxx_containers_unord_map_cnstr_move(void) { { typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, test_allocator<std::pair<const int, std::string> > > C; C c0(7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), test_allocator<std::pair<const int, std::string> >(10) ); C c = std::move(c0); LIBCPP_ASSERT(c.bucket_count() == 7); TC_ASSERT_EXPR(c.size() == 0); TC_ASSERT_EXPR(c.hash_function() == test_hash<std::hash<int> >(8)); TC_ASSERT_EXPR(c.key_eq() == test_compare<std::equal_to<int> >(9)); TC_ASSERT_EXPR(c.get_allocator() == (test_allocator<std::pair<const int, std::string> >(10))); TC_ASSERT_EXPR(c.empty()); TC_ASSERT_EXPR(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); TC_ASSERT_EXPR(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); TC_ASSERT_EXPR(c.load_factor() == 0); TC_ASSERT_EXPR(c.max_load_factor() == 1); TC_ASSERT_EXPR(c0.empty()); } { typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, test_allocator<std::pair<const int, std::string> > > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), test_allocator<std::pair<const int, std::string> >(10) ); C c = std::move(c0); LIBCPP_ASSERT(c.bucket_count() == 7); TC_ASSERT_EXPR(c.size() == 4); TC_ASSERT_EXPR(c.at(1) == "one"); TC_ASSERT_EXPR(c.at(2) == "two"); TC_ASSERT_EXPR(c.at(3) == "three"); TC_ASSERT_EXPR(c.at(4) == "four"); TC_ASSERT_EXPR(c.hash_function() == test_hash<std::hash<int> >(8)); TC_ASSERT_EXPR(c.key_eq() == test_compare<std::equal_to<int> >(9)); TC_ASSERT_EXPR(c.get_allocator() == (test_allocator<std::pair<const int, std::string> >(10))); TC_ASSERT_EXPR(!c.empty()); TC_ASSERT_EXPR(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); TC_ASSERT_EXPR(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); TC_ASSERT_EXPR(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); TC_ASSERT_EXPR(c.max_load_factor() == 1); TC_ASSERT_EXPR(c0.empty()); } TC_SUCCESS_RESULT(); return 0; }
int main() { { typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, test_allocator<int> > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), test_allocator<int>(10) ); C c = c0; assert(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(*i == 1); ++i; assert(*i == 1); ++i; assert(*i == 2); ++i; assert(*i == 2); ++i; assert(*i == 3); ++i; assert(*i == 4); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == test_allocator<int>(10)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE { typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, other_allocator<int> > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), other_allocator<int>(10) ); C c = c0; assert(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(*i == 1); ++i; assert(*i == 1); ++i; assert(*i == 2); ++i; assert(*i == 2); ++i; assert(*i == 3); ++i; assert(*i == 4); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == other_allocator<int>(-2)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE #if __cplusplus >= 201103L || defined(_LIBCPP_MSVC) { typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, min_allocator<int> > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), min_allocator<int>() ); C c = c0; assert(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(*i == 1); ++i; assert(*i == 1); ++i; assert(*i == 2); ++i; assert(*i == 2); ++i; assert(*i == 3); ++i; assert(*i == 4); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == min_allocator<int>()); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #endif }
int main() { #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { typedef std::pair<int, std::string> P; typedef test_allocator<std::pair<const int, std::string>> A; typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(std::move(c0), A(12)); assert(c.bucket_count() >= 7); assert(c.size() == 6); typedef std::pair<C::const_iterator, C::const_iterator> Eq; Eq eq = c.equal_range(1); assert(std::distance(eq.first, eq.second) == 2); C::const_iterator i = eq.first; assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); eq = c.equal_range(2); assert(std::distance(eq.first, eq.second) == 2); i = eq.first; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); eq = c.equal_range(3); assert(std::distance(eq.first, eq.second) == 1); i = eq.first; assert(i->first == 3); assert(i->second == "three"); eq = c.equal_range(4); assert(std::distance(eq.first, eq.second) == 1); i = eq.first; assert(i->first == 4); assert(i->second == "four"); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >(12))); assert(c0.empty()); } { typedef std::pair<int, std::string> P; typedef test_allocator<std::pair<const int, std::string>> A; typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(std::move(c0), A(10)); assert(c.bucket_count() == 7); assert(c.size() == 6); typedef std::pair<C::const_iterator, C::const_iterator> Eq; Eq eq = c.equal_range(1); assert(std::distance(eq.first, eq.second) == 2); C::const_iterator i = eq.first; assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); eq = c.equal_range(2); assert(std::distance(eq.first, eq.second) == 2); i = eq.first; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); eq = c.equal_range(3); assert(std::distance(eq.first, eq.second) == 1); i = eq.first; assert(i->first == 3); assert(i->second == "three"); eq = c.equal_range(4); assert(std::distance(eq.first, eq.second) == 1); i = eq.first; assert(i->first == 4); assert(i->second == "four"); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >(10))); assert(c0.empty()); } #if __cplusplus >= 201103L { typedef std::pair<int, std::string> P; typedef min_allocator<std::pair<const int, std::string>> A; typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A() ); C c(std::move(c0), A()); assert(c.bucket_count() == 7); assert(c.size() == 6); typedef std::pair<C::const_iterator, C::const_iterator> Eq; Eq eq = c.equal_range(1); assert(std::distance(eq.first, eq.second) == 2); C::const_iterator i = eq.first; assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); eq = c.equal_range(2); assert(std::distance(eq.first, eq.second) == 2); i = eq.first; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); eq = c.equal_range(3); assert(std::distance(eq.first, eq.second) == 1); i = eq.first; assert(i->first == 3); assert(i->second == "three"); eq = c.equal_range(4); assert(std::distance(eq.first, eq.second) == 1); i = eq.first; assert(i->first == 4); assert(i->second == "four"); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >())); assert(c0.empty()); } #endif #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES }
int main() { { typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, test_allocator<std::pair<const int, std::string> > > C; C c0(7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), test_allocator<std::pair<const int, std::string> >(10) ); C c = std::move(c0); LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 0); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == (test_allocator<std::pair<const int, std::string> >(10))); assert(c.empty()); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(c.load_factor() == 0); assert(c.max_load_factor() == 1); assert(c0.empty()); } { typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, test_allocator<std::pair<const int, std::string> > > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), test_allocator<std::pair<const int, std::string> >(10) ); C c = std::move(c0); LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 6); typedef std::pair<C::const_iterator, C::const_iterator> Eq; Eq eq = c.equal_range(1); assert(std::distance(eq.first, eq.second) == 2); C::const_iterator i = eq.first; assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); eq = c.equal_range(2); assert(std::distance(eq.first, eq.second) == 2); i = eq.first; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); eq = c.equal_range(3); assert(std::distance(eq.first, eq.second) == 1); i = eq.first; assert(i->first == 3); assert(i->second == "three"); eq = c.equal_range(4); assert(std::distance(eq.first, eq.second) == 1); i = eq.first; assert(i->first == 4); assert(i->second == "four"); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >(10))); assert(c0.empty()); } { typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, min_allocator<std::pair<const int, std::string> > > C; C c0(7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), min_allocator<std::pair<const int, std::string> >() ); C c = std::move(c0); LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 0); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == (min_allocator<std::pair<const int, std::string> >())); assert(c.empty()); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(c.load_factor() == 0); assert(c.max_load_factor() == 1); assert(c0.empty()); } { typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, min_allocator<std::pair<const int, std::string> > > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), min_allocator<std::pair<const int, std::string> >() ); C c = std::move(c0); LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 6); typedef std::pair<C::const_iterator, C::const_iterator> Eq; Eq eq = c.equal_range(1); assert(std::distance(eq.first, eq.second) == 2); C::const_iterator i = eq.first; assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); eq = c.equal_range(2); assert(std::distance(eq.first, eq.second) == 2); i = eq.first; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); eq = c.equal_range(3); assert(std::distance(eq.first, eq.second) == 1); i = eq.first; assert(i->first == 3); assert(i->second == "three"); eq = c.equal_range(4); assert(std::distance(eq.first, eq.second) == 1); i = eq.first; assert(i->first == 4); assert(i->second == "four"); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >())); assert(c0.empty()); } #if _LIBCPP_DEBUG >= 1 { std::unordered_multimap<int, int> s1 = {{1, 1}, {2, 2}, {3, 3}}; std::unordered_multimap<int, int>::iterator i = s1.begin(); std::pair<const int, int> k = *i; std::unordered_multimap<int, int> s2 = std::move(s1); assert(*i == k); s2.erase(i); assert(s2.size() == 2); } #endif }
void thixotropicViscosity::correct ( const volScalarField& p, const volScalarField& T ) { const kinematicSingleLayer& film = filmType<kinematicSingleLayer>(); // references to film fields const volVectorField& U = film.U(); const volVectorField& Uw = film.Uw(); const volScalarField& delta = film.delta(); const volScalarField& deltaRho = film.deltaRho(); const surfaceScalarField& phi = film.phi(); const volScalarField& alpha = film.alpha(); const Time& runTime = this->owner().regionMesh().time(); // Shear rate volScalarField gDot("gDot", alpha*mag(U - Uw)/(delta + film.deltaSmall())); if (debug && runTime.outputTime()) { gDot.write(); } dimensionedScalar deltaRho0("deltaRho0", deltaRho.dimensions(), ROOTVSMALL); surfaceScalarField phiU(phi/fvc::interpolate(deltaRho + deltaRho0)); dimensionedScalar c0("c0", dimless/dimTime, ROOTVSMALL); volScalarField coeff("coeff", -c_*pow(gDot, d_) + c0); // Limit the filmMass and deltaMass to calculate the effect of the added // droplets with lambda = 0 to the film const volScalarField filmMass ( "thixotropicViscosity:filmMass", film.netMass() + dimensionedScalar("SMALL", dimMass, ROOTVSMALL) ); const volScalarField deltaMass ( "thixotropicViscosity:deltaMass", max(dimensionedScalar("zero", dimMass, 0), film.deltaMass()) ); fvScalarMatrix lambdaEqn ( fvm::ddt(lambda_) + fvm::div(phiU, lambda_) - fvm::Sp(fvc::div(phiU), lambda_) == a_*pow((1.0 - lambda_), b_) + fvm::SuSp(coeff, lambda_) - fvm::Sp(deltaMass/(runTime.deltaT()*filmMass), lambda_) ); lambdaEqn.relax(); lambdaEqn.solve(); lambda_.min(1.0); lambda_.max(0.0); mu_ = muInf_/(sqr(1.0 - K_*lambda_) + ROOTVSMALL); mu_.correctBoundaryConditions(); }
void Test3::rgbimage( const char* Directory) { printf( "Start RGBImage Test\n"); // create 3 images RGBImage Img1( 800, 600); RGBImage Img2( 800, 600); RGBImage Img3( 800, 600); TEST( "Invalid Image Width\n", Img1.width()==800); TEST( "Invalid Image Height\n", Img1.height()==600); /* Img1.setPixelColor( 800, 100, Color()); Color c = Img1.getPixelColor(800, 100); Img2.setPixelColor( 100, 600, Color()); c = Img1.getPixelColor(100, 600); */ Color c0(0,0,0); Color c1(1,0,0); Color c2(0,1,0); Color c3(0,0,1); for( unsigned int i=0; i<Img1.height(); i++ ) { for( unsigned int j=0; j<Img1.width(); j++ ) { float u = (float)j/(float)Img1.width(); float v = (float)i/(float)Img1.height(); Color cu0 = c0 *(1.0f-u) + c1*u; Color cu1 = c2 *(1.0f-u) + c3*u; Color co = cu0 *(1.0f-v) + cu1*v; if ((j == 0 || j==400) && i%50 == 0) { std::cout << " : i =" << i*j << "\n" << "Floats RGB:( " << co.R << " | " << co.G << " | " << co.B << " )" << std::endl; } Img1.setPixelColor(j, i, co); TEST( "Different colors for identical get/setPixelColor calls\n", equals(co, Img1.getPixelColor(j,i))); } } char Path[512]; char CompletePath[512]; size_t len = strlen( Directory ); assert(len<=256); strcpy(Path, Directory); if(len>0 && Path[len-1] != '/' && Path[len-1] != '\\') strcat( Path, "/"); strcpy( CompletePath, Path); strcat( CompletePath, "rgbtestimage1.bmp"); TEST( "Unable to save Image!\n", Img1.saveToDisk(CompletePath)); for( unsigned int i=0; i<Img1.height(); i++ ) { for( unsigned int j=0; j<Img1.width(); j++ ) { float cx = ((float)j-400.0f); float cy = ((float)i-300.0f); float r = sqrt(cx*cx + cy*cy); if(r<300) { r = 300-r; float rcx = cx * cos( r * 0.015f) - cy * sin(r* 0.015f); float rcy = cx * sin( r * 0.015f) + cy * cos(r* 0.015f); Color co = Img1.getPixelColor(400 + (int)rcx, 300 + (int)rcy); Img2.setPixelColor(j,i, co); } else { Color co = Img1.getPixelColor(j, i ); Img2.setPixelColor(j,i, co); } } } strcpy( CompletePath, Path); strcat( CompletePath, "rgbtestimage2.bmp"); TEST( "Unable to save Image!\n", Img2.saveToDisk(CompletePath)); for( unsigned int i=0; i<Img2.height(); i++ ) { for( unsigned int j=0; j<Img2.width(); j++ ) { if( ((j/40)%2)==(i/40)%2 ) { Color c = Color(1,1,1); c.R = c.R - Img2.getPixelColor(j,i).R; c.G = c.G - Img2.getPixelColor(j,i).G; c.B = c.B - Img2.getPixelColor(j,i).B; Img3.setPixelColor(j,i, c); } else { Img3.setPixelColor(j,i, Img2.getPixelColor(j,i)); } } } strcpy( CompletePath, Path); strcat( CompletePath, "rgbtestimage3.bmp"); TEST( "Unable to save Image!\n", Img3.saveToDisk(CompletePath)); printf( "RGBImage Test succeeded if the saved bmp-images are equal to the reference images!\n"); }
void perform_test_trivial() { { // running independent counters sequentially (???) typedef Counter<0> counter0; typedef typename counter0::uint_type uint_type_0; const uint_type_0 count0 = 10000000U; { const counter0 c0(count0); OKLIB_TEST_EQUAL(counter0::counter(), count0); c0(); OKLIB_TEST_EQUAL(counter0::counter(), 0); } const counter0 c0(count0); OKLIB_TEST_EQUAL(counter0::counter(), count0); typedef Counter<1> counter1; typedef typename counter1::uint_type uint_type_1; const uint_type_1 count1 = 10000001U; const counter1 c1(count1); OKLIB_TEST_EQUAL(counter1::counter(), count1); //independent counter c0, c1 initialised boost::thread thread0(c0); boost::thread thread1(c1); // now both counters are counting down in parallel threads OKLIB_TEST_NOTEQUAL(thread0, thread1); { boost::thread thread; OKLIB_TEST_NOTEQUAL(thread0, thread); OKLIB_TEST_NOTEQUAL(thread1, thread); } thread0.join(); thread1.join(); OKLIB_TEST_EQUAL(counter0::counter(), 0); OKLIB_TEST_EQUAL(counter1::counter(), 0); } { boost::mutex mutex; typedef CounterWithMutex<0> counter0; typedef typename counter0::uint_type uint_type_0; const uint_type_0 count0 = 10000000U; const counter0 c0(count0, mutex); OKLIB_TEST_EQUAL(counter0::counter(), count0); typedef CounterWithMutex<1> counter1; typedef typename counter1::uint_type uint_type_1; const uint_type_1 count1 = 10000001U; const counter1 c1(count1, mutex); OKLIB_TEST_EQUAL(counter1::counter(), count1); boost::thread thread0(c0); boost::thread thread1(c1); OKLIB_TEST_NOTEQUAL(thread0, thread1); { boost::mutex::scoped_lock lock(mutex); } OKLIB_TEST_EQUAL(counter0::counter(), 0); OKLIB_TEST_EQUAL(counter1::counter(), 0); } { // NOW parallelism boost::mutex mutex0; typedef CounterWithMutex<0> counter0; typedef typename counter0::uint_type uint_type_0; const uint_type_0 count0 = 10000000U; const counter0 c0(count0, mutex0); boost::mutex mutex1; typedef CounterWithMutex<1> counter1; typedef typename counter1::uint_type uint_type_1; const uint_type_1 count1 = 10000001U; const counter1 c1(count1, mutex1); { boost::thread thread0(c0); boost::thread thread1(c1); boost::mutex::scoped_lock lock0(mutex0); boost::mutex::scoped_lock lock1(mutex1); } OKLIB_TEST_EQUAL(counter0::counter(), 0); OKLIB_TEST_EQUAL(counter1::counter(), 0); } }
int main() { { typedef test_allocator<std::pair<const int, std::string> > A; typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A(4) ); c = c0; assert(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); ++i; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); ++i; assert(i->first == 3); assert(i->second == "three"); ++i; assert(i->first == 4); assert(i->second == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(4)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } { typedef std::unordered_multimap<int, std::string> C; typedef std::pair<const int, std::string> P; const P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c(a, a+sizeof(a)/sizeof(a[0])); C *p = &c; c = *p; assert(c.size() == 6); assert(std::is_permutation(c.begin(), c.end(), a)); } { typedef other_allocator<std::pair<const int, std::string> > A; typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A(4) ); c = c0; assert(c.bucket_count() >= 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); ++i; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); ++i; assert(i->first == 3); assert(i->second == "three"); ++i; assert(i->first == 4); assert(i->second == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(10)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #if TEST_STD_VER >= 11 { typedef min_allocator<std::pair<const int, std::string> > A; typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A() ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A() ); c = c0; assert(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); ++i; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); ++i; assert(i->first == 3); assert(i->second == "three"); ++i; assert(i->first == 4); assert(i->second == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A()); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #endif }
int main() { // setup random number generator gRandomGenerator = gsl_rng_alloc(gsl_rng_taus); gsl_rng_set (gRandomGenerator, 0.0); // created a rapidity cut named "eta", which returns true if the passed track has a // pseudo-rapidity greater than 0.1 Cut c0("eta", new eta_greator(0.1)); // add some other cuts acting on different ranges c0.AddCut("zab>2", new eta_greator(2.0))(new eta_greator(5.0))(new eta_greator(8.0)); // Create a pt cut Cut pt_cut("pt>3", new pt_greator(3.0)); // Create a pt cut Cut pt_cut("pt>3.0", new pt_greator(3.0)); // add some more cuts to the pt-cut group // (Cut::AddCut returns an inserter with operator() which // continues to insert if given a name + function pair) pt_cut.AddCut("pt>4.0", new pt_greator(4.0)) ("pt>6.0", new pt_greator(6.0)) ("pt>2.0", new pt_greator(2.0)) ("pt>1.0", new pt_greator(1.0)); // create a cutlist CutList cuts; cuts.AddCut(pt_cut); // cuts.AddAction("eta", add_to_histogram_eta_1); // cuts.AddAction("pt>3 zab>2", add_to_histogram_1); // cuts.AddAction("pt>3 eta", add_to_histogram_4); cuts.AddAction("pt>3.0", action_pt_3_0); cuts.AddAction("pt>4.0", action_pt_4_0); cuts.AddAction("pt>1.0", action_pt_1_0); cuts.AddAction("pt>2.0", action_pt_2_0); cuts.AddAction("pt>6.0", action_pt_6_0); cuts.AddAction("pt>4.0 pt>2.0", action_pt_4_AND_2); // cuts.Print(); // generate a random cut Track track = Generate(); track.print(); std::cout << "Testing Random : " << cuts.Run(track) << std::endl; for (int i = 0; i < 50; i++) { Track t = Generate(); // std::cout << "Mass : " << t.m << std::endl; cuts.Run(t); } // std::cout << c0.Run(1.0f) << ' ' << c0.Run(9.0f) << std::endl; puts(""); std::cout << "Pt > 1.0 Count : " << pt_1_count << std::endl << "Pt > 2.0 Count : " << pt_2_count << std::endl << "Pt > 3.0 Count : " << pt_3_count << std::endl << "Pt > 4.0 Count : " << pt_4_count << std::endl << "Pt > 6.0 Count : " << pt_6_count << std::endl; std::cout << "It works!" << std::endl; return 0; }
int main() { #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { typedef std::pair<int, std::string> P; typedef test_allocator<std::pair<const int, std::string>> A; typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(std::move(c0), A(12)); assert(c.bucket_count() >= 5); assert(c.size() == 4); assert(c.at(1) == "one"); assert(c.at(2) == "two"); assert(c.at(3) == "three"); assert(c.at(4) == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(12)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c0.empty()); } { typedef std::pair<int, std::string> P; typedef test_allocator<std::pair<const int, std::string>> A; typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(std::move(c0), A(10)); LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 4); assert(c.at(1) == "one"); assert(c.at(2) == "two"); assert(c.at(3) == "three"); assert(c.at(4) == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(10)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c0.empty()); } #if TEST_STD_VER >= 11 { typedef std::pair<int, std::string> P; typedef min_allocator<std::pair<const int, std::string>> A; typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A() ); C c(std::move(c0), A()); LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 4); assert(c.at(1) == "one"); assert(c.at(2) == "two"); assert(c.at(3) == "three"); assert(c.at(4) == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A()); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); assert(c0.empty()); } #endif #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES }
int main() { { typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, test_allocator<std::pair<const int, std::string> > > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), test_allocator<std::pair<const int, std::string> >(10) ); C c = c0; assert(c.bucket_count() == 7); assert(c.size() == 4); assert(c.at(1) == "one"); assert(c.at(2) == "two"); assert(c.at(3) == "three"); assert(c.at(4) == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == (test_allocator<std::pair<const int, std::string> >(10))); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(c.load_factor() == (float)c.size()/c.bucket_count()); assert(c.max_load_factor() == 1); } #ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE { typedef std::unordered_map<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, other_allocator<std::pair<const int, std::string> > > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), other_allocator<std::pair<const int, std::string> >(10) ); C c = c0; assert(c.bucket_count() == 7); assert(c.size() == 4); assert(c.at(1) == "one"); assert(c.at(2) == "two"); assert(c.at(3) == "three"); assert(c.at(4) == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == (other_allocator<std::pair<const int, std::string> >(-2))); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(c.load_factor() == (float)c.size()/c.bucket_count()); assert(c.max_load_factor() == 1); } #endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE }
int main() { // check that it'll find nodes exactly MAX away { tree_type exact_dist(std::ptr_fun(tac)); triplet c0(5, 4, 0); exact_dist.insert(c0); triplet target(7,4,0); std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target,2); assert(found.first != exact_dist.end()); assert(found.second == 2); std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << std::endl; } { tree_type exact_dist(std::ptr_fun(tac)); triplet c0(5, 2, 0); exact_dist.insert(c0); triplet target(7,4,0); // call find_nearest without a range value - it found a compile error earlier. std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target); assert(found.first != exact_dist.end()); std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << " @ " << found.second << " should be " << sqrt(8) << std::endl; assert(found.second == sqrt(8)); } { tree_type exact_dist(std::ptr_fun(tac)); triplet c0(5, 2, 0); exact_dist.insert(c0); triplet target(7,4,0); std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target,sqrt(8)); assert(found.first != exact_dist.end()); std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << " @ " << found.second << " should be " << sqrt(8) << std::endl; assert(found.second == sqrt(8)); } tree_type src(std::ptr_fun(tac)); triplet c0(5, 4, 0); src.insert(c0); triplet c1(4, 2, 1); src.insert(c1); triplet c2(7, 6, 9); src.insert(c2); triplet c3(2, 2, 1); src.insert(c3); triplet c4(8, 0, 5); src.insert(c4); triplet c5(5, 7, 0); src.insert(c5); triplet c6(3, 3, 8); src.insert(c6); triplet c7(9, 7, 3); src.insert(c7); triplet c8(2, 2, 6); src.insert(c8); triplet c9(2, 0, 6); src.insert(c9); std::cout << src << std::endl; src.erase(c0); src.erase(c1); src.erase(c3); src.erase(c5); src.optimise(); // test the efficient_replace_and_optimise() tree_type eff_repl = src; { std::vector<triplet> vec; // erased above as part of test vec.push_back(triplet(5, 4, 0)); // erased above as part of test vec.push_back(triplet(4, 2, 1)); vec.push_back(triplet(7, 6, 9)); // erased above as part of test vec.push_back(triplet(2, 2, 1)); vec.push_back(triplet(8, 0, 5)); // erased above as part of test vec.push_back(triplet(5, 7, 0)); vec.push_back(triplet(3, 3, 8)); vec.push_back(triplet(9, 7, 3)); vec.push_back(triplet(2, 2, 6)); vec.push_back(triplet(2, 0, 6)); eff_repl.clear(); eff_repl.efficient_replace_and_optimise(vec); } std::cout << std::endl << src << std::endl; tree_type copied(src); std::cout << copied << std::endl; tree_type assigned; assigned = src; std::cout << assigned << std::endl; for (int loop = 0; loop != 4; ++loop) { tree_type * target; switch (loop) { case 0: std::cout << "Testing plain construction" << std::endl; target = &src; break; case 1: std::cout << "Testing copy-construction" << std::endl; target = &copied; break; case 2: std::cout << "Testing assign-construction" << std::endl; target = &assigned; break; default: case 4: std::cout << "Testing efficient-replace-and-optimise" << std::endl; target = &eff_repl; break; } tree_type & t = *target; int i=0; for (tree_type::const_iterator iter=t.begin(); iter!=t.end(); ++iter, ++i); std::cout << "iterator walked through " << i << " nodes in total" << std::endl; if (i!=6) { std::cerr << "Error: does not tally with the expected number of nodes (6)" << std::endl; return 1; } i=0; for (tree_type::const_reverse_iterator iter=t.rbegin(); iter!=t.rend(); ++iter, ++i); std::cout << "reverse_iterator walked through " << i << " nodes in total" << std::endl; if (i!=6) { std::cerr << "Error: does not tally with the expected number of nodes (6)" << std::endl; return 1; } triplet s(5, 4, 3); std::vector<triplet> v; unsigned int const RANGE = 3; size_t count = t.count_within_range(s, RANGE); std::cout << "counted " << count << " nodes within range " << RANGE << " of " << s << ".\n"; t.find_within_range(s, RANGE, std::back_inserter(v)); std::cout << "found " << v.size() << " nodes within range " << RANGE << " of " << s << ":\n"; std::vector<triplet>::const_iterator ci = v.begin(); for (; ci != v.end(); ++ci) std::cout << *ci << " "; std::cout << "\n" << std::endl; std::cout << std::endl << t << std::endl; // search for all the nodes at exactly 0 dist away for (tree_type::const_iterator target = t.begin(); target != t.end(); ++target) { std::pair<tree_type::const_iterator,double> found = t.find_nearest(*target,0); assert(found.first != t.end()); assert(*found.first == *target); std::cout << "Test find_nearest(), found at exact distance away from " << *target << ", found " << *found.first << std::endl; } { const double small_dist = 0.0001; std::pair<tree_type::const_iterator,double> notfound = t.find_nearest(s,small_dist); std::cout << "Test find_nearest(), nearest to " << s << " within " << small_dist << " should not be found" << std::endl; if (notfound.first != t.end()) { std::cout << "ERROR found a node at dist " << notfound.second << " : " << *notfound.first << std::endl; std::cout << "Actual distance = " << s.distance_to(*notfound.first) << std::endl; } assert(notfound.first == t.end()); } { std::pair<tree_type::const_iterator,double> nif = t.find_nearest_if(s,std::numeric_limits<double>::max(),Predicate()); std::cout << "Test find_nearest_if(), nearest to " << s << " @ " << nif.second << ": " << *nif.first << std::endl; std::pair<tree_type::const_iterator,double> cantfind = t.find_nearest_if(s,std::numeric_limits<double>::max(),FalsePredicate()); std::cout << "Test find_nearest_if(), nearest to " << s << " should never be found (predicate too strong)" << std::endl; assert(cantfind.first == t.end()); } { std::pair<tree_type::const_iterator,double> found = t.find_nearest(s,std::numeric_limits<double>::max()); std::cout << "Nearest to " << s << " @ " << found.second << " " << *found.first << std::endl; std::cout << "Should be " << found.first->distance_to(s) << std::endl; // NOTE: the assert does not check for an exact match, as it is not exact when -O2 or -O3 is // switched on. Some sort of optimisation makes the math inexact. assert( fabs(found.second - found.first->distance_to(s)) < std::numeric_limits<double>::epsilon() ); } { triplet s2(10, 10, 2); std::pair<tree_type::const_iterator,double> found = t.find_nearest(s2,std::numeric_limits<double>::max()); std::cout << "Nearest to " << s2 << " @ " << found.second << " " << *found.first << std::endl; std::cout << "Should be " << found.first->distance_to(s2) << std::endl; // NOTE: the assert does not check for an exact match, as it is not exact when -O2 or -O3 is // switched on. Some sort of optimisation makes the math inexact. assert( fabs(found.second - found.first->distance_to(s2)) < std::numeric_limits<double>::epsilon() ); } std::cout << std::endl; std::cout << t << std::endl; // Testing iterators { std::cout << "Testing iterators" << std::endl; t.erase(c2); t.erase(c4); t.erase(c6); t.erase(c7); t.erase(c8); // t.erase(c9); std::cout << std::endl << t << std::endl; std::cout << "Forward iterator test..." << std::endl; std::vector<triplet> forwards; for (tree_type::iterator i = t.begin(); i != t.end(); ++i) { std::cout << *i << " " << std::flush; forwards.push_back(*i); } std::cout << std::endl; std::cout << "Reverse iterator test..." << std::endl; std::vector<triplet> backwards; for (tree_type::reverse_iterator i = t.rbegin(); i != t.rend(); ++i) { std::cout << *i << " " << std::flush; backwards.push_back(*i); } std::cout << std::endl; std::reverse(backwards.begin(),backwards.end()); assert(backwards == forwards); } } return 0; }
// Test the DynSysModel class void test_DynSysModel() { // Make a 4 variable model GAParams::SetNumVars( 4 ); DynSysModel m; // Create monomial x1, x2, x3, x4, x1*x2, x3*x4, x1*x2*x3*x4 Monomial c0( "0000" ); c0.mCoeff = false; Monomial c1( "0000" ); c1.mCoeff = true; Monomial x4( "0001" ); Monomial x3( "0010" ); Monomial x2( "0100" ); Monomial x1( "1000" ); Monomial x34( "0011" ); Monomial x12( "1100" ); Monomial x14( "1001" ); Monomial x1234("1111" ); Polynomial f; f.AddTerm( c0 ); std::cout << "f = " << f.ToString( true ) << std::endl; Polynomial g; g.AddTerm( c1 ); std::cout << "g = " << g.ToString( true ) << std::endl; // f1(x) = x1 + x2 + x4; Polynomial f1; f1.AddTerm( x1 ); f1.AddTerm( x2 ); f1.AddTerm( x4 ); std::cout << "f1 = " << f1.ToString( true ) << std::endl; Polynomial::mMaxSupport = 2; m.SetFunction( 1, f ); m.SetFunction( 2, g ); m.SetFunction( 3, f ); m.SetFunction( 4, f1 ); ComplexityMatrix cmplx_mat; ComplexityMatrixRow row; cmplx_mat.assign( 4, row ); m.SetPolyComplexities( ); double s; s = m[1].mComplexityScore; s = m[2].mComplexityScore; s = m[3].mComplexityScore; s = m[4].mComplexityScore; /* // f2(x) = x1*x2*x3*x4; Polynomial f2; f2.AddTerm( x1234 ); std::cout << "f2 = " << f2.ToString( true ) << std::endl; // f3(x) = x1*x2 + x3*x4; Polynomial f3; f3.AddTerm( x12 ); f3.AddTerm( x34 ); std::cout << "f3 = " << f3.ToString( true ) << std::endl; // f3'(x) = x1*x4; Polynomial f3p; f3p.AddTerm( x14 ); std::cout << "f3p = " << f3p.ToString( true ) << std::endl; // f4(x) = x3 + x4; Polynomial f4; f4.AddTerm( x3 ); f4.AddTerm( x4 ); std::cout << "f4 = " << f4.ToString( true ) << std::endl; // Assign the functions to the model m.SetFunction( 1, f1 ); m.SetFunction( 2, f2 ); m.SetFunction( 3, f3p ); m.SetFunction( 4, f4 ); */ TimeSeries t2; // t2.push_back( NTuple("1101" ) ); // t2.push_back( NTuple("1011" ) ); t2.push_back( NTuple("1100" ) ); t2.push_back( NTuple("0010" ) ); t2.push_back( NTuple("0001" ) ); t2.push_back( NTuple("1001" ) ); t2.push_back( NTuple("0001" ) ); // Better to pass a reference to the time series for the result // TimeSeries t3 = m.Iterate( NTuple( "1111" ), 6 ); // Test iteration for only the k'th variable size_t k = 3; TimeSeries t4; size_t h = m.Iterate( t2, k, t4 ); TimeSeriesIter iter = t4.begin(); while( iter != t4.end() ) { std::cout << *iter++ << std::endl; } }
bool CollisionObject::contact(CollisionObject *other, // the other object CollisionType ct, CollisionPair& cr) { // This object moved from p0 to p0+v0, the other from p1 to p1+v1. Vector3 p0(old_matrix[0][3], old_matrix[1][3], old_matrix[2][3]); Vector3 p1(other->old_matrix[0][3], other->old_matrix[1][3], other->old_matrix[2][3]); Vector3 v0(new_matrix[0][3], new_matrix[1][3], new_matrix[2][3]); //Vector3 p0 = old_pos; //Vector3 p1 = other->old_pos; //Vector3 v0 = new_pos; v0 -= p0; Vector3 v1(Vector3(other->new_matrix[0][3], other->new_matrix[1][3], other->new_matrix[2][3]) - p1); //Vector3 v1(other->new_pos - p1); bool has_contact = false; switch (ct) { case COLLTYPE_QUICK: { // do a contact check between 'moving spheres' sphere s0(p0,mRadius); sphere s1(p1,other->mRadius); Real u0,u1; if (s0.intersect_sweep(v0,s1,v1,u0,u1)) { // there may be a valid contact somewhere along the path if ((u0>=0.0f) && (u0<1.0f)) { // compute the 2 midpoints at the time of collision Vector3 c0(p0 + v0*u0); Vector3 c1(p1 + v1*u0); // compute the collide normal Vector3 d(c1-c0); if (d.length() > TINY) { d.normalise(); } else { d = Vector3(0.0f, 1.0f, 0.0f); } // compute the contact point CollisionInfo collInfo; collInfo.contact = (d*mRadius) + c0; // compute the collide normals collInfo.this_normal = d; collInfo.other_normal = -d; cr.collInfos.push_back(collInfo); // compute the timestamp where the collision happened cr.tstamp = m_tdelta*u0; has_contact = true; } } } break; case COLLTYPE_EXACT: case COLLTYPE_CONTACT: { // If distance traveled is more then 1/8 then each of the object's // radii, then we do several tests along the line // of movements. // WVB: // This isn't likely to work particularly well for objects // with large rotational motion. For that it would be better // to blend the transformations along the path as well, // and to also use amount of rotation in determining how many // steps are necessary. That would bring this another step closer // to being a true continuous collision detection system. Real rq0 = mRadius * 0.125f; Real rq1 = other->mRadius * 0.125f; Real v0_len = v0.length(); Real v1_len = v1.length(); int num = (int) n_max((v0_len/rq0), (v1_len/rq1)); const int maxChecks = 16; if (num == 0) { num = 1; } else if (num > maxChecks) { num = maxChecks; } Vector3 d0(v0 / float(num)); Vector3 d1(v1 / float(num)); Matrix4 self_matrix = old_matrix; Matrix4 other_matrix = other->old_matrix; int i; for (i=0; i<num; i++) { p0 += d0; p1 += d1; self_matrix[0][3] = p0.x; self_matrix[1][3] = p0.y; self_matrix[2][3] = p0.z; other_matrix[0][3] = p1.x; other_matrix[1][3] = p1.y; other_matrix[2][3] = p1.z; if (mShape->collide(ct, self_matrix, other->getShape(), other_matrix, cr)) { // CONTACT!!! double dt = (m_tdelta) / num; cr.tstamp = dt*i; return true; } } } break; default: break; } return has_contact; }
int main(int, char**) { { typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, test_allocator<std::pair<const int, std::string> > > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), test_allocator<std::pair<const int, std::string> >(10) ); C c = c0; LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); ++i; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); ++i; assert(i->first == 3); assert(i->second == "three"); ++i; assert(i->first == 4); assert(i->second == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == (test_allocator<std::pair<const int, std::string> >(10))); assert(!c.empty()); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #if TEST_STD_VER >= 11 { typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, other_allocator<std::pair<const int, std::string> > > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), other_allocator<std::pair<const int, std::string> >(10) ); C c = c0; LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); ++i; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); ++i; assert(i->first == 3); assert(i->second == "three"); ++i; assert(i->first == 4); assert(i->second == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == (other_allocator<std::pair<const int, std::string> >(-2))); assert(!c.empty()); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } { typedef std::unordered_multimap<int, std::string, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, min_allocator<std::pair<const int, std::string> > > C; typedef std::pair<int, std::string> P; P a[] = { P(1, "one"), P(2, "two"), P(3, "three"), P(4, "four"), P(1, "four"), P(2, "four"), }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), min_allocator<std::pair<const int, std::string> >() ); C c = c0; LIBCPP_ASSERT(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(i->first == 1); assert(i->second == "one"); ++i; assert(i->first == 1); assert(i->second == "four"); ++i; assert(i->first == 2); assert(i->second == "two"); ++i; assert(i->first == 2); assert(i->second == "four"); ++i; assert(i->first == 3); assert(i->second == "three"); ++i; assert(i->first == 4); assert(i->second == "four"); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == (min_allocator<std::pair<const int, std::string> >())); assert(!c.empty()); assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #endif return 0; }
void ModelMesher::generateRegularSurface(double offset) { if(m->activeNode == nullptr) return; auto n = m->activeNode; n->vis_property["isSmoothShading"].setValue(true); Structure::Curve* curve = dynamic_cast<Structure::Curve*>(n); Structure::Sheet* sheet = dynamic_cast<Structure::Sheet*>(n); auto addTriangle = [&](SurfaceMeshModel * newMesh, Vector3 a, Vector3 b, Vector3 c){ typedef SurfaceMeshModel::Vertex Vert; int v = newMesh->n_vertices(); newMesh->add_vertex(a); newMesh->add_vertex(b); newMesh->add_vertex(c); newMesh->add_triangle(Vert(v + 0), Vert(v + 1), Vert(v + 2)); }; QSharedPointer<SurfaceMeshModel> newMesh = QSharedPointer<SurfaceMeshModel>(new SurfaceMeshModel()); // Options bool isFlat = m->QObject::property("meshingIsFlat").toBool(); bool isSquare = m->QObject::property("meshingIsSquare").toBool(); switch(m->QObject::property("meshingIsThick").toInt()){ case 0: break; case 1: offset *= 2; break; case 2: offset *= 8; break; } if(isFlat) n->vis_property["isSmoothShading"].setValue(false); if(curve) { int numSegments = 20; int radialSegments = isSquare ? 4 : 20; std::vector< std::vector<Vector3> > grid; RMF rmf(curve->discretizedAsCurve(curve->length() / numSegments)); // Compute frames & construct grid for (int i = 0; i < numSegments; i++) { // Frames auto point = rmf.point[i]; auto normal = rmf.U[i].r; auto binormal = rmf.U[i].s; auto tangent = rmf.U[i].t; // Construct grid double r = offset; // Start cap if (i == 0) { double d = M_PI * 2 / radialSegments; for (double phi = M_PI * 0.5; phi >= d; phi -= d){ grid.push_back(std::vector<Vector3>()); for (double theta = 0; theta < M_PI * 2; theta += d){ Eigen::AngleAxisd q1(theta + M_PI, -tangent); Eigen::AngleAxisd q2(phi, binormal); if(isFlat) grid.back().push_back((q1 * normal * cos(phi)) * r + point); else grid.back().push_back((q1 * (q2 * normal)) * r + point); } } } // Middle segments grid.push_back(std::vector<Vector3>()); for (int j = 0; j < radialSegments; j++){ double v = double(j) / radialSegments * 2.0 * M_PI; double cx = -r * std::cos(v); double cy = r * std::sin(v); auto pos2 = point; pos2.x() += cx * normal.x() + cy * binormal.x(); pos2.y() += cx * normal.y() + cy * binormal.y(); pos2.z() += cx * normal.z() + cy * binormal.z(); grid.back().push_back(Vector3(pos2.x(), pos2.y(), pos2.z())); } // End cap if (i == numSegments - 1) { double d = M_PI * 2 / radialSegments; for (double phi = d; phi <= M_PI * 0.5; phi += d){ grid.push_back(std::vector<Vector3>()); for (double theta = 0; theta < M_PI * 2; theta += d){ Eigen::AngleAxisd q1(theta + M_PI, -tangent); Eigen::AngleAxisd q2(phi, -binormal); if(isFlat) grid.back().push_back((q1 * normal * cos(phi)) * r + point); else grid.back().push_back((q1 * (q2 * normal)) * r + point); } } } } // Construct the mesh for (size_t i = 0; i < grid.size() - 1; i++) { for (size_t j = 0; j < grid.front().size(); j++) { auto ip = i + 1; auto jp = (j + 1) % grid.front().size(); auto a = grid[i][j]; auto b = grid[ip][j]; auto c = grid[ip][jp]; auto d = grid[i][jp]; if (i != 0) addTriangle(newMesh.data(), a, b, d); if (i != grid.size() - 2) addTriangle(newMesh.data(), b, c, d); } } } if(sheet) { if(isFlat) { Eigen::Vector4d c0(0,0,0,0), c1(1,1,0,0); Vector3 pos(0,0,0); std::vector<Vector3> frame(3,Vector3::Zero()); // First corner sheet->get(c0,pos,frame); Vector3 corner0 = pos - (offset * (frame[0] + frame[1] + frame[2])); // Second corner sheet->get(c1,pos,frame); Vector3 corner1 = pos + (offset * (frame[0] + frame[1] + frame[2])); Eigen::AlignedBox3d bbox(corner0, corner1); QVector<Vector3> corn; for(int i = 0; i < 8 ; i++){ corn << bbox.corner(Eigen::AlignedBox3d::CornerType(Eigen::AlignedBox3d::CornerType::BottomLeftFloor + i)); } addTriangle(newMesh.data(), corn[0], corn[1], corn[2]); addTriangle(newMesh.data(), corn[1], corn[3], corn[2]); addTriangle(newMesh.data(), corn[6], corn[5], corn[4]); addTriangle(newMesh.data(), corn[6], corn[7], corn[5]); addTriangle(newMesh.data(), corn[1], corn[0], corn[4]); addTriangle(newMesh.data(), corn[1], corn[4], corn[5]); addTriangle(newMesh.data(), corn[2], corn[3], corn[7]); addTriangle(newMesh.data(), corn[2], corn[7], corn[6]); addTriangle(newMesh.data(), corn[3], corn[1], corn[5]); addTriangle(newMesh.data(), corn[3], corn[5], corn[7]); addTriangle(newMesh.data(), corn[0], corn[2], corn[4]); addTriangle(newMesh.data(), corn[4], corn[2], corn[6]); } else { generateOffsetSurface(offset); return; } } GeometryHelper::meregeVertices<Vector3>(newMesh.data()); newMesh->updateBoundingBox(); newMesh->update_face_normals(); newMesh->update_vertex_normals(); n->property["mesh"].setValue(newMesh); n->property["mesh_filename"].setValue(QString("meshes/%1.obj").arg(n->id)); }
int main() { { typedef test_allocator<int> A; typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A(4) ); c = c0; assert(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(*i == 1); ++i; assert(*i == 1); ++i; assert(*i == 2); ++i; assert(*i == 2); ++i; assert(*i == 3); ++i; assert(*i == 4); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(4)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } { typedef other_allocator<int> A; typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A(10) ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A(4) ); c = c0; assert(c.bucket_count() >= 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(*i == 1); ++i; assert(*i == 1); ++i; assert(*i == 2); ++i; assert(*i == 2); ++i; assert(*i == 3); ++i; assert(*i == 4); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A(10)); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #if __cplusplus >= 201103L { typedef min_allocator<int> A; typedef std::unordered_multiset<int, test_hash<std::hash<int> >, test_compare<std::equal_to<int> >, A > C; typedef int P; P a[] = { P(1), P(2), P(3), P(4), P(1), P(2) }; C c0(a, a + sizeof(a)/sizeof(a[0]), 7, test_hash<std::hash<int> >(8), test_compare<std::equal_to<int> >(9), A() ); C c(a, a + 2, 7, test_hash<std::hash<int> >(2), test_compare<std::equal_to<int> >(3), A() ); c = c0; assert(c.bucket_count() == 7); assert(c.size() == 6); C::const_iterator i = c.cbegin(); assert(*i == 1); ++i; assert(*i == 1); ++i; assert(*i == 2); ++i; assert(*i == 2); ++i; assert(*i == 3); ++i; assert(*i == 4); assert(c.hash_function() == test_hash<std::hash<int> >(8)); assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); assert(c.get_allocator() == A()); assert(!c.empty()); assert(std::distance(c.begin(), c.end()) == c.size()); assert(std::distance(c.cbegin(), c.cend()) == c.size()); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(c.max_load_factor() == 1); } #endif }
// add a pad hole or slot bool PCBMODEL::AddPadHole( KICADPAD* aPad ) { if( NULL == aPad || !aPad->IsThruHole() ) return false; if( !aPad->m_drill.oval ) { TopoDS_Shape s = BRepPrimAPI_MakeCylinder( aPad->m_drill.size.x * 0.5, m_thickness * 2.0 ).Shape(); gp_Trsf shift; shift.SetTranslation( gp_Vec( aPad->m_position.x, aPad->m_position.y, -m_thickness * 0.5 ) ); BRepBuilderAPI_Transform hole( s, shift ); m_cutouts.push_back( hole.Shape() ); return true; } // slotted hole double angle_offset = 0.0; double rad; // radius of the slot double hlen; // half length of the slot if( aPad->m_drill.size.x < aPad->m_drill.size.y ) { angle_offset = M_PI_2; rad = aPad->m_drill.size.x * 0.5; hlen = aPad->m_drill.size.y * 0.5 - rad; } else { rad = aPad->m_drill.size.y * 0.5; hlen = aPad->m_drill.size.x * 0.5 - rad; } DOUBLET c0( -hlen, 0.0 ); DOUBLET c1( hlen, 0.0 ); DOUBLET p0( -hlen, rad ); DOUBLET p1( -hlen, -rad ); DOUBLET p2( hlen, -rad ); DOUBLET p3( hlen, rad ); angle_offset += aPad->m_rotation; double dlim = (double)std::numeric_limits< float >::epsilon(); if( angle_offset < -dlim || angle_offset > dlim ) { double vsin = sin( angle_offset ); double vcos = cos( angle_offset ); double x = c0.x * vcos - c0.y * vsin; double y = c0.x * vsin + c0.y * vcos; c0.x = x; c0.y = y; x = c1.x * vcos - c1.y * vsin; y = c1.x * vsin + c1.y * vcos; c1.x = x; c1.y = y; x = p0.x * vcos - p0.y * vsin; y = p0.x * vsin + p0.y * vcos; p0.x = x; p0.y = y; x = p1.x * vcos - p1.y * vsin; y = p1.x * vsin + p1.y * vcos; p1.x = x; p1.y = y; x = p2.x * vcos - p2.y * vsin; y = p2.x * vsin + p2.y * vcos; p2.x = x; p2.y = y; x = p3.x * vcos - p3.y * vsin; y = p3.x * vsin + p3.y * vcos; p3.x = x; p3.y = y; } c0.x += aPad->m_position.x; c0.y += aPad->m_position.y; c1.x += aPad->m_position.x; c1.y += aPad->m_position.y; p0.x += aPad->m_position.x; p0.y += aPad->m_position.y; p1.x += aPad->m_position.x; p1.y += aPad->m_position.y; p2.x += aPad->m_position.x; p2.y += aPad->m_position.y; p3.x += aPad->m_position.x; p3.y += aPad->m_position.y; OUTLINE oln; oln.SetMinSqDistance( m_minDistance2 ); KICADCURVE crv0, crv1, crv2, crv3; // crv0 = arc crv0.m_start = c0; crv0.m_end = p0; crv0.m_ep = p1; crv0.m_angle = M_PI; crv0.m_radius = rad; crv0.m_form = CURVE_ARC; // crv1 = line crv1.m_start = p1; crv1.m_end = p2; crv1.m_form = CURVE_LINE; // crv2 = arc crv2.m_start = c1; crv2.m_end = p2; crv2.m_ep = p3; crv2.m_angle = M_PI; crv2.m_radius = rad; crv2.m_form = CURVE_ARC; // crv3 = line crv3.m_start = p3; crv3.m_end = p0; crv3.m_form = CURVE_LINE; oln.AddSegment( crv0 ); oln.AddSegment( crv1 ); oln.AddSegment( crv2 ); oln.AddSegment( crv3 ); TopoDS_Shape slot; if( oln.MakeShape( slot, m_thickness ) ) { if( !slot.IsNull() ) m_cutouts.push_back( slot ); return true; } return false; }