void Sphere::sub(int n, Point3 p1, Point3 p2, Point3 p3) { Vector3 pv1(p1.x,p1.y,p1.z); Vector3 pv2(p2.x,p2.y,p2.z); Vector3 pv3(p3.x,p3.y,p3.z); if (n == 1) { pv1.normalize(); pv2.normalize(); pv3.normalize(); pv1 *= 0.5; pv2 *= 0.5; pv3 *= 0.5; p1.x = pv1.x; p1.y = pv1.y; p1.z = pv1.z; p2.x = pv2.x; p2.y = pv2.y; p2.z = pv2.z; p3.x = pv3.x; p3.y = pv3.y; p3.z = pv3.z; addTriangle(p1,p2,p3); } else { Vector3 av = pv1+pv2; Vector3 bv = pv2+pv3; Vector3 cv = pv3+pv1; av.normalize(); bv.normalize(); cv.normalize(); Point3 v12(av.x,av.y,av.z); Point3 v23(bv.x,bv.y,bv.z); Point3 v31(cv.x,cv.y,cv.z); sub(n-1,p1,v12,v31); sub(n-1,p2,v23,v12); sub(n-1,p3,v31,v23); sub(n-1,v12,v23,v31); } }
template <typename PointInT, typename PointOutT> void pcl::ESFEstimation<PointInT, PointOutT>::computeESF ( PointCloudIn &pc, std::vector<float> &hist) { const int binsize = 64; unsigned int sample_size = 20000; srand (static_cast<unsigned int> (time (0))); int maxindex = static_cast<int> (pc.points.size ()); int index1, index2, index3; std::vector<float> d2v, d1v, d3v, wt_d3; std::vector<int> wt_d2; d1v.reserve (sample_size); d2v.reserve (sample_size * 3); d3v.reserve (sample_size); wt_d2.reserve (sample_size * 3); wt_d3.reserve (sample_size); float h_in[binsize] = {0}; float h_out[binsize] = {0}; float h_mix[binsize] = {0}; float h_mix_ratio[binsize] = {0}; float h_a3_in[binsize] = {0}; float h_a3_out[binsize] = {0}; float h_a3_mix[binsize] = {0}; float h_d1[binsize] = {0}; float h_d3_in[binsize] = {0}; float h_d3_out[binsize] = {0}; float h_d3_mix[binsize] = {0}; float ratio=0.0; float pih = static_cast<float>(M_PI) / 2.0f; float a,b,c,s; int th1,th2,th3; int vxlcnt = 0; int pcnt1,pcnt2,pcnt3; for (size_t nn_idx = 0; nn_idx < sample_size; ++nn_idx) { // get a new random point index1 = rand()%maxindex; index2 = rand()%maxindex; index3 = rand()%maxindex; if (index1==index2 || index1 == index3 || index2 == index3) { nn_idx--; continue; } Eigen::Vector4f p1 = pc.points[index1].getVector4fMap (); Eigen::Vector4f p2 = pc.points[index2].getVector4fMap (); Eigen::Vector4f p3 = pc.points[index3].getVector4fMap (); // A3 Eigen::Vector4f v21 (p2 - p1); Eigen::Vector4f v31 (p3 - p1); Eigen::Vector4f v23 (p2 - p3); a = v21.norm (); b = v31.norm (); c = v23.norm (); s = (a+b+c) * 0.5f; if (s * (s-a) * (s-b) * (s-c) <= 0.001f) continue; v21.normalize (); v31.normalize (); v23.normalize (); //TODO: .dot gives nan's th1 = static_cast<int> (pcl_round (acos (fabs (v21.dot (v31))) / pih * (binsize-1))); th2 = static_cast<int> (pcl_round (acos (fabs (v23.dot (v31))) / pih * (binsize-1))); th3 = static_cast<int> (pcl_round (acos (fabs (v23.dot (v21))) / pih * (binsize-1))); if (th1 < 0 || th1 >= binsize) { nn_idx--; continue; } if (th2 < 0 || th2 >= binsize) { nn_idx--; continue; } if (th3 < 0 || th3 >= binsize) { nn_idx--; continue; } //pcl::PointXYZ cog(((rand()%100)-50.0f) / 100.0f,((rand()%100)-50.0f) / 100.0f,((rand()%100)-50.0f) / 100.0f); // D1 // d1v.push_back( pcl::euclideanDistance(cog, pc.points[index1]) ); // D2 d2v.push_back (pcl::euclideanDistance (pc.points[index1], pc.points[index2])); d2v.push_back (pcl::euclideanDistance (pc.points[index1], pc.points[index3])); d2v.push_back (pcl::euclideanDistance (pc.points[index2], pc.points[index3])); int vxlcnt_sum = 0; int p_cnt = 0; // IN, OUT, MIXED, Ratio line tracing, index1->index2 { const int xs = p1[0] < 0.0? static_cast<int>(floor(p1[0])+GRIDSIZE_H): static_cast<int>(ceil(p1[0])+GRIDSIZE_H-1); const int ys = p1[1] < 0.0? static_cast<int>(floor(p1[1])+GRIDSIZE_H): static_cast<int>(ceil(p1[1])+GRIDSIZE_H-1); const int zs = p1[2] < 0.0? static_cast<int>(floor(p1[2])+GRIDSIZE_H): static_cast<int>(ceil(p1[2])+GRIDSIZE_H-1); const int xt = p2[0] < 0.0? static_cast<int>(floor(p2[0])+GRIDSIZE_H): static_cast<int>(ceil(p2[0])+GRIDSIZE_H-1); const int yt = p2[1] < 0.0? static_cast<int>(floor(p2[1])+GRIDSIZE_H): static_cast<int>(ceil(p2[1])+GRIDSIZE_H-1); const int zt = p2[2] < 0.0? static_cast<int>(floor(p2[2])+GRIDSIZE_H): static_cast<int>(ceil(p2[2])+GRIDSIZE_H-1); wt_d2.push_back (this->lci (xs, ys, zs, xt, yt, zt, ratio, vxlcnt, pcnt1)); if (wt_d2.back () == 2) h_mix_ratio[static_cast<int> (pcl_round (ratio * (binsize-1)))]++; vxlcnt_sum += vxlcnt; p_cnt += pcnt1; } // IN, OUT, MIXED, Ratio line tracing, index1->index3 { const int xs = p1[0] < 0.0? static_cast<int>(floor(p1[0])+GRIDSIZE_H): static_cast<int>(ceil(p1[0])+GRIDSIZE_H-1); const int ys = p1[1] < 0.0? static_cast<int>(floor(p1[1])+GRIDSIZE_H): static_cast<int>(ceil(p1[1])+GRIDSIZE_H-1); const int zs = p1[2] < 0.0? static_cast<int>(floor(p1[2])+GRIDSIZE_H): static_cast<int>(ceil(p1[2])+GRIDSIZE_H-1); const int xt = p3[0] < 0.0? static_cast<int>(floor(p3[0])+GRIDSIZE_H): static_cast<int>(ceil(p3[0])+GRIDSIZE_H-1); const int yt = p3[1] < 0.0? static_cast<int>(floor(p3[1])+GRIDSIZE_H): static_cast<int>(ceil(p3[1])+GRIDSIZE_H-1); const int zt = p3[2] < 0.0? static_cast<int>(floor(p3[2])+GRIDSIZE_H): static_cast<int>(ceil(p3[2])+GRIDSIZE_H-1); wt_d2.push_back (this->lci (xs, ys, zs, xt, yt, zt, ratio, vxlcnt, pcnt2)); if (wt_d2.back () == 2) h_mix_ratio[static_cast<int>(pcl_round (ratio * (binsize-1)))]++; vxlcnt_sum += vxlcnt; p_cnt += pcnt2; } // IN, OUT, MIXED, Ratio line tracing, index2->index3 { const int xs = p2[0] < 0.0? static_cast<int>(floor(p2[0])+GRIDSIZE_H): static_cast<int>(ceil(p2[0])+GRIDSIZE_H-1); const int ys = p2[1] < 0.0? static_cast<int>(floor(p2[1])+GRIDSIZE_H): static_cast<int>(ceil(p2[1])+GRIDSIZE_H-1); const int zs = p2[2] < 0.0? static_cast<int>(floor(p2[2])+GRIDSIZE_H): static_cast<int>(ceil(p2[2])+GRIDSIZE_H-1); const int xt = p3[0] < 0.0? static_cast<int>(floor(p3[0])+GRIDSIZE_H): static_cast<int>(ceil(p3[0])+GRIDSIZE_H-1); const int yt = p3[1] < 0.0? static_cast<int>(floor(p3[1])+GRIDSIZE_H): static_cast<int>(ceil(p3[1])+GRIDSIZE_H-1); const int zt = p3[2] < 0.0? static_cast<int>(floor(p3[2])+GRIDSIZE_H): static_cast<int>(ceil(p3[2])+GRIDSIZE_H-1); wt_d2.push_back (this->lci (xs,ys,zs,xt,yt,zt,ratio,vxlcnt,pcnt3)); if (wt_d2.back () == 2) h_mix_ratio[static_cast<int>(pcl_round(ratio * (binsize-1)))]++; vxlcnt_sum += vxlcnt; p_cnt += pcnt3; } // D3 ( herons formula ) d3v.push_back (sqrt (sqrt (s * (s-a) * (s-b) * (s-c)))); if (vxlcnt_sum <= 21) { wt_d3.push_back (0); h_a3_out[th1] += static_cast<float> (pcnt3) / 32.0f; h_a3_out[th2] += static_cast<float> (pcnt1) / 32.0f; h_a3_out[th3] += static_cast<float> (pcnt2) / 32.0f; } else if (p_cnt - vxlcnt_sum < 4) { h_a3_in[th1] += static_cast<float> (pcnt3) / 32.0f; h_a3_in[th2] += static_cast<float> (pcnt1) / 32.0f; h_a3_in[th3] += static_cast<float> (pcnt2) / 32.0f; wt_d3.push_back (1); } else { h_a3_mix[th1] += static_cast<float> (pcnt3) / 32.0f; h_a3_mix[th2] += static_cast<float> (pcnt1) / 32.0f; h_a3_mix[th3] += static_cast<float> (pcnt2) / 32.0f; wt_d3.push_back (static_cast<float> (vxlcnt_sum) / static_cast<float> (p_cnt)); } } // Normalizing, get max float maxd1 = 0; float maxd2 = 0; float maxd3 = 0; for (size_t nn_idx = 0; nn_idx < sample_size; ++nn_idx) { // get max of Dx if (d1v[nn_idx] > maxd1) maxd1 = d1v[nn_idx]; if (d2v[nn_idx] > maxd2) maxd2 = d2v[nn_idx]; if (d2v[sample_size + nn_idx] > maxd2) maxd2 = d2v[sample_size + nn_idx]; if (d2v[sample_size*2 +nn_idx] > maxd2) maxd2 = d2v[sample_size*2 +nn_idx]; if (d3v[nn_idx] > maxd3) maxd3 = d3v[nn_idx]; } // Normalize and create histogram int index; for (size_t nn_idx = 0; nn_idx < sample_size; ++nn_idx) { h_d1[static_cast<int>(pcl_round (d1v[nn_idx] / maxd1 * (binsize-1)))]++ ; if (wt_d3[nn_idx] >= 0.999) // IN { index = static_cast<int>(pcl_round (d3v[nn_idx] / maxd3 * (binsize-1))); if (index >= 0 && index < binsize) h_d3_in[index]++; } else { if (wt_d3[nn_idx] <= 0.001) // OUT { index = static_cast<int>(pcl_round (d3v[nn_idx] / maxd3 * (binsize-1))); if (index >= 0 && index < binsize) h_d3_out[index]++ ; } else { index = static_cast<int>(pcl_round (d3v[nn_idx] / maxd3 * (binsize-1))); if (index >= 0 && index < binsize) h_d3_mix[index]++; } } } //normalize and create histogram for (size_t nn_idx = 0; nn_idx < d2v.size(); ++nn_idx ) { if (wt_d2[nn_idx] == 0) h_in[static_cast<int>(pcl_round (d2v[nn_idx] / maxd2 * (binsize-1)))]++ ; if (wt_d2[nn_idx] == 1) h_out[static_cast<int>(pcl_round (d2v[nn_idx] / maxd2 * (binsize-1)))]++; if (wt_d2[nn_idx] == 2) h_mix[static_cast<int>(pcl_round (d2v[nn_idx] / maxd2 * (binsize-1)))]++ ; } //float weights[10] = {1, 1, 1, 1, 1, 1, 1, 1 , 1 , 1}; float weights[10] = {0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f}; hist.reserve (binsize * 10); for (int i = 0; i < binsize; i++) hist.push_back (h_a3_in[i] * weights[0]); for (int i = 0; i < binsize; i++) hist.push_back (h_a3_out[i] * weights[1]); for (int i = 0; i < binsize; i++) hist.push_back (h_a3_mix[i] * weights[2]); for (int i = 0; i < binsize; i++) hist.push_back (h_d3_in[i] * weights[3]); for (int i = 0; i < binsize; i++) hist.push_back (h_d3_out[i] * weights[4]); for (int i = 0; i < binsize; i++) hist.push_back (h_d3_mix[i] * weights[5]); for (int i = 0; i < binsize; i++) hist.push_back (h_in[i]*0.5f * weights[6]); for (int i = 0; i < binsize; i++) hist.push_back (h_out[i] * weights[7]); for (int i = 0; i < binsize; i++) hist.push_back (h_mix[i] * weights[8]); for (int i = 0; i < binsize; i++) hist.push_back (h_mix_ratio[i]*0.5f * weights[9]); float sm = 0; for (size_t i = 0; i < hist.size (); i++) sm += hist[i]; for (size_t i = 0; i < hist.size (); i++) hist[i] /= sm; }
void Scene::init(){ primitives = new Primitive*[25]; nrOfPrimitives = 0; //wall at0,0,1 Vec3 v121( 0,0, 1 ); primitives[nrOfPrimitives] = new PlanePrim( v121, -13.0f ); primitives[nrOfPrimitives]->getMaterial()->setReflection( 1 ); primitives[nrOfPrimitives]->getMaterial()->setDiffuse( 0.0f ); primitives[nrOfPrimitives]->getMaterial()->setSpecular(1.0f); Color c121( 1,1,1 ); primitives[nrOfPrimitives++]->getMaterial()->setColor(c121); //wall at 0 0 -1 Vec3 v12( 0,0, -1 ); primitives[nrOfPrimitives] = new PlanePrim( v12, 13.0f ); primitives[nrOfPrimitives]->getMaterial()->setReflection( 1.0f ); primitives[nrOfPrimitives]->getMaterial()->setDiffuse( 0.0f ); primitives[nrOfPrimitives]->getMaterial()->setSpecular(1.0f); Color c12( 0.3f,0.3f,0.7f ); primitives[nrOfPrimitives++]->getMaterial()->setColor(c12); //- //the ground Vec3 v1( 0,1, 0 ); primitives[nrOfPrimitives] = new CheckerBoard( v1, 3.4f ); primitives[nrOfPrimitives]->getMaterial()->setReflection( 0.7f ); primitives[nrOfPrimitives]->getMaterial()->setDiffuse( 1.0f ); primitives[nrOfPrimitives]->getMaterial()->setSpecular(0.0f); Color c1( 0.3f, 0.3f, 0.7f ); primitives[nrOfPrimitives++]->getMaterial()->setColor(c1); // // 0 -1 0 Vec3 v13( 0,-1, 0 ); primitives[nrOfPrimitives] = new PlanePrim( v13, -3.4f ); primitives[nrOfPrimitives]->getMaterial()->setReflection( 1 ); primitives[nrOfPrimitives]->getMaterial()->setDiffuse( 1.0f ); primitives[nrOfPrimitives]->getMaterial()->setSpecular(1.0f - primitives[nrOfPrimitives]->getMaterial()->getDiffuse()); Color c13( 1,1,1 ); primitives[nrOfPrimitives++]->getMaterial()->setColor(c13); // //middle sphere Vec3 v2( 0, 1, 8 ); primitives[nrOfPrimitives] = new Sphere( v2, 1.0f ); primitives[nrOfPrimitives]->getMaterial()->setReflection( 1 ); primitives[nrOfPrimitives]->getMaterial()->setDiffuse( 0.2 ); primitives[nrOfPrimitives]->getMaterial()->setSpecular(1.0f - primitives[nrOfPrimitives]->getMaterial()->getDiffuse()); Color c2( 1,1,1); primitives[nrOfPrimitives++]->getMaterial()->setColor( c2 ); //- //left sphere Vec3 v3( -5.5f, -0.5, 7 ); Color c3( 0.7f, 0.7f, 0 ); primitives[nrOfPrimitives] = new Sphere( v3, 2 ); primitives[nrOfPrimitives]->getMaterial()->setReflection( 0.6f ); primitives[nrOfPrimitives]->getMaterial()->setDiffuse( 0.9f ); primitives[nrOfPrimitives]->getMaterial()->setSpecular(1.0f - primitives[nrOfPrimitives]->getMaterial()->getDiffuse()); primitives[nrOfPrimitives++]->getMaterial()->setColor( c3 ); //- //right sphere Vec3 v31( 5.5f, -0.5, 7 ); Color c31( 0.7f, 0.0f, 0 ); primitives[nrOfPrimitives] = new Sphere( v31, 2 ); primitives[nrOfPrimitives]->getMaterial()->setReflection( 0.6f ); primitives[nrOfPrimitives]->getMaterial()->setDiffuse( 0.9f ); primitives[nrOfPrimitives]->getMaterial()->setSpecular(1.0f); primitives[nrOfPrimitives++]->getMaterial()->setColor( c31 ); //- //a light Vec3 v4( 3, 2.5f, 3 ); Color c4(0.7f,0.7f,0.7f); primitives[nrOfPrimitives] = new Sphere( v4, 0.5f ); primitives[nrOfPrimitives]->isLight = true; primitives[nrOfPrimitives++]->getMaterial()->setColor( c4 ); //-- //another light Vec3 v5( 0, 5, 10 ); Color c5( 0.6f, 0.6f, 0.8f ); primitives[nrOfPrimitives] = new Sphere( v5, 0.5f ); primitives[nrOfPrimitives]->isLight = true; primitives[nrOfPrimitives++]->getMaterial()->setColor( c5 ); //-- }
int main(int argc, char *argv[]) { Pooma::initialize(argc,argv); Pooma::Tester tester(argc, argv); // -------------------------------------------------------------------------- // 3D // -------------------------------------------------------------------------- Tensor<3,double,Full> t3f1(0.0, 3.0, 6.0, 1.0, 4.0, 7.0, 2.0, 5.0, 8.0); tester.out() << "t3f1: " << t3f1 << std::endl; Tensor<3,double,Full> t3f2 = -t3f1; tester.out() << "t3f2: " << t3f2 << std::endl; Tensor<3,double,Symmetric> t3s1(1.0, 2.0, 3.0, 4.0, 5.0, 6.0); tester.out() << "t3s1: " << t3s1 << std::endl; Tensor<3,double,Symmetric> t3s2(-1.0, -2.0, -3.0, -4.0, -5.0, -6.0); tester.out() << "t3s2: " << t3s2 << std::endl; Tensor<3,double,Full> t3s1AsFull(1.0,2.0,4.0, 2.0,3.0,5.0, 4.0,5.0,6.0); tester.out() << "t3s1AsFull: " << t3s1AsFull << std::endl; Tensor<3,double,Full> t3s2AsFull = -t3s1AsFull; tester.out() << "t3s2AsFull: " << t3s2AsFull << std::endl; Tensor<3,double,Symmetric> t3s3(9.0, 9.0, 9.0, 9.0, 9.0, 9.0), t3s4(9.0, 9.0, 9.0, 9.0, 9.0, 9.0); t3s3 = t3s1 + t3s2; tester.out() << "t3s3 = t3s1 + t3s2: " << t3s3 << std::endl; tester.check("t3s3", t3s3, Tensor<3,double,Symmetric>(0.0)); tester.check("t3s3 against Full", (t3s3 == Tensor<3,double,Symmetric>(0.0))); Tensor<3,double,Full> t3f3(99.9), t3f4(99.9), t3f5(99.9), t3f6(99.9); t3f3 = t3f1 + t3f2; // No need to check results here; done in TestTensors t3f4 = t3s1 + t3s2; tester.out() << "t3f4 = t3s1 + t3s2: " << t3f4 << std::endl; tester.check("t3f4", (t3f4 == t3s3)); t3f5 = t3f1 + t3s2; tester.out() << "t3f5 = t3f1 + t3s2: " << t3f5 << std::endl; tester.check("t3f5", t3f5, t3f1 + t3s2AsFull); t3f6 = t3s2 + t3f1; tester.out() << "t3f6 = t3s2 + t3f1: " << t3f6 << std::endl; tester.check("t3f6", t3f6, t3f1 + t3s2AsFull); t3f6 -= t3f1; tester.out() << "t3f6 -= t3f1: " << t3f6 << std::endl; tester.check("t3f6", t3f6, t3s2AsFull); t3s4 = t3s3 - t3f1; tester.out() << "t3s4 = t3s3 - t3f1: " << t3s4 << std::endl; tester.check("t3s4", (t3s4 == Tensor<3,double,Symmetric>(0,-3,-4,-6,-7,-8))); // Test Tensor dot Tensor: // Full: double sum = 0.0; int i, j, k; t3f3 = dot(t3f1, t3f2); for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { for (k = 0; k < 3; ++k) { t3f3(i,k) -= t3f1(i,j)*t3f2(j,k); } } } t3f3 = t3f3*t3f3; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { sum += t3f3(i,j); } } tester.check("dot(t3f1, t3f2)", (sum == 0)); // Symmetric: sum = 0.0; t3f3 = dot(t3s1, t3s2); for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { for (k = 0; k < 3; ++k) { t3f3(i,k) -= t3s1(i,j)*t3s2(j,k); } } } t3f3 = t3f3*t3f3; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { sum += t3f3(i,j); } } tester.check("dot(t3s1, t3s2)", (sum == 0)); // Test Tensor dot Vector, and vice-versa: // Full: // Vector dot Tensor Vector<3> v31(1.0, 2.0, 3.0); tester.out() << "v31: " << v31 << std::endl; Vector<3> v32(9.0); v32 = dot(v31, t3f2); tester.out() << "v32 = dot(v31, t3f2): " << v32 << std::endl; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { v32(j) -= v31(i)*t3f2(i,j); } } v32 = v32*v32; sum = 0.0; for (i = 0; i < 3; ++i) { sum += v32(i); } tester.check("dot(v31, t3f2)", (sum == 0)); // Tensor dot Vector v32 = dot(t3f2, v31); tester.out() << "v32 = dot(t3f2, v31): " << v32 << std::endl; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { v32(i) -= t3f2(i,j)*v31(j); } } v32 = v32*v32; sum = 0.0; for (i = 0; i < 3; ++i) { sum += v32(i); } tester.check("dot(t3f2, v31)", (sum == 0)); // Symmetric: // Vector dot Tensor v32 = dot(v31, t3s2); tester.out() << "v32 = dot(v31, t3s2): " << v32 << std::endl; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { v32(j) -= v31(i)*t3s2(i,j); } } v32 = v32*v32; sum = 0.0; for (i = 0; i < 3; ++i) { sum += v32(i); } tester.check("dot(v31, t3s2)", (sum == 0)); // Tensor dot Vector v32 = dot(t3s2, v31); tester.out() << "v32 = dot(t3s2, v31): " << v32 << std::endl; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { v32(i) -= t3s2(i,j)*v31(j); } } v32 = v32*v32; sum = 0.0; for (i = 0; i < 3; ++i) { sum += v32(i); } tester.check("dot(t3s2, v31)", (sum == 0)); // -------------------------------------------------------------------------- // 2D // -------------------------------------------------------------------------- Tensor<2,double,Full> t2f1(0.0, 2.0, 1.0, 3.0); tester.out() << "t2f1: " << t2f1 << std::endl; Tensor<2,double,Full> t2f2 = -t2f1; tester.out() << "t2f2: " << t2f2 << std::endl; Tensor<2,double,Symmetric> t2s1(1.0, 2.0, 3.0); tester.out() << "t2s1: " << t2s1 << std::endl; Tensor<2,double,Symmetric> t2s2(-1.0, -2.0, -3.0); tester.out() << "t2s2: " << t2s2 << std::endl; Tensor<2,double,Full> t2s1AsFull(1.0,2.0, 2.0,3.0); tester.out() << "t2s1AsFull: " << t2s1AsFull << std::endl; Tensor<2,double,Full> t2s2AsFull = -t2s1AsFull; tester.out() << "t2s2AsFull: " << t2s2AsFull << std::endl; Tensor<2,double,Symmetric> t2s3(9.0, 9.0, 9.0), t2s4(9.0, 9.0, 9.0); t2s3 = t2s1 + t2s2; tester.out() << "t2s3 = t2s1 + t2s2: " << t2s3 << std::endl; tester.check("t2s3", t2s3, Tensor<2,double,Symmetric>(0.0)); tester.check("t2s3 against Full", (t2s3 == Tensor<2,double,Symmetric>(0.0))); Tensor<2,double,Full> t2f3(99.9), t2f4(99.9), t2f5(99.9), t2f6(99.9), t2f7(99.9); t2f3 = t2f1 + t2f2; tester.out() << "t2f3 = t2f1 + t2f2: " << t2f3 << std::endl; tester.check("t2f3", t2f3, Tensor<2,double,Full>(0.0)); t2f4 = t2s1 + t2s2; tester.out() << "t2f4 = t2s1 + t2s2: " << t2f4 << std::endl; tester.check("t2f4", (t2f4 == t2s3)); t2f5 = t2f1 + t2s2; tester.out() << "t2f5 = t2f1 + t2s2: " << t2f5 << std::endl; tester.check("t2f5", t2f5, t2f1 + t2s2AsFull); t2f6 = t2s2 + t2f1; tester.out() << "t2f6 = t2s2 + t2f1: " << t2f6 << std::endl; tester.check("t2f6", t2f6, t2f1 + t2s2AsFull); t2f6 -= t2f1; tester.out() << "t2f6 -= t2f1: " << t2f6 << std::endl; tester.check("t2f6", t2f6, t2s2AsFull); t2s4 = t2s3 - t2f1; tester.out() << "t2s4 = t2s3 - t2f1: " << t2s4 << std::endl; tester.check("t2s4", (t2s4 == Tensor<2,double,Symmetric>(-0, -2, -3))); // Test Tensor dot Tensor: // Full: sum = 0.0; t2f3 = dot(t2f1, t2f2); for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { for (k = 0; k < 2; ++k) { t2f3(i,k) -= t2f1(i,j)*t2f2(j,k); } } } t2f3 = t2f3*t2f3; for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { sum += t2f3(i,j); } } tester.check("dot(t2f1, t2f2)", (sum == 0)); // Symmetric: sum = 0.0; t2f3 = dot(t2s1, t2s2); for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { for (k = 0; k < 2; ++k) { t2f3(i,k) -= t2s1(i,j)*t2s2(j,k); } } } t2f3 = t2f3*t2f3; for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { sum += t2f3(i,j); } } tester.check("dot(t2s1, t2s2)", (sum == 0)); // Test Tensor dot Vector, and vice-versa: // Full: // Vector dot Tensor Vector<2> v21(1.0, 2.0); tester.out() << "v21: " << v21 << std::endl; Vector<2> v22(9.0); v22 = dot(v21, t2f2); tester.out() << "v22 = dot(v21, t2f2): " << v22 << std::endl; for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { v22(j) -= v21(i)*t2f2(i,j); } } v22 = v22*v22; sum = 0.0; for (i = 0; i < 2; ++i) { sum += v22(i); } tester.check("dot(v21, t2f2)", (sum == 0)); // Tensor dot Vector v22 = dot(t2f2, v21); tester.out() << "v22 = dot(t2f2, v21): " << v22 << std::endl; for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { v22(i) -= t2f2(i,j)*v21(j); } } v22 = v22*v22; sum = 0.0; for (i = 0; i < 2; ++i) { sum += v22(i); } tester.check("dot(t2f2, v21)", (sum == 0)); // Symmetric: // Vector dot Tensor v22 = dot(v21, t2s2); tester.out() << "v22 = dot(v21, t2s2): " << v22 << std::endl; for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { v22(j) -= v21(i)*t2s2(i,j); } } v22 = v22*v22; sum = 0.0; for (i = 0; i < 2; ++i) { sum += v22(i); } tester.check("dot(v21, t2s2)", (sum == 0)); // Tensor dot Vector v22 = dot(t2s2, v21); tester.out() << "v22 = dot(t2s2, v21): " << v22 << std::endl; for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { v22(i) -= t2s2(i,j)*v21(j); } } v22 = v22*v22; sum = 0.0; for (i = 0; i < 2; ++i) { sum += v22(i); } tester.check("dot(t2s2, v21)", (sum == 0)); // -------------------------------------------------------------------------- // 1D // -------------------------------------------------------------------------- Tensor<1,double,Full> t1f1(1.0); tester.out() << "t1f1: " << t1f1 << std::endl; Tensor<1,double,Full> t1f2 = -t1f1; tester.out() << "t1f2: " << t1f2 << std::endl; Tensor<1,double,Symmetric> t1s1(1.0); tester.out() << "t1s1: " << t1s1 << std::endl; Tensor<1,double,Symmetric> t1s2(-1.0); tester.out() << "t1s2: " << t1s2 << std::endl; Tensor<1,double,Full> t1s1AsFull(1.0); tester.out() << "t1s1AsFull: " << t1s1AsFull << std::endl; Tensor<1,double,Full> t1s2AsFull = -t1s1AsFull; tester.out() << "t1s2AsFull: " << t1s2AsFull << std::endl; Tensor<1,double,Symmetric> t1s3(9.0), t1s4(9.0); t1s3 = t1s1 + t1s2; tester.out() << "t1s3 = t1s1 + t1s2: " << t1s3 << std::endl; tester.check("t1s3", t1s3, Tensor<1,double,Symmetric>(0.0)); tester.check("t1s3 against Full", (t1s3 == Tensor<1,double,Symmetric>(0.0))); Tensor<1,double,Full> t1f3(99.9), t1f4(99.9), t1f5(99.9), t1f6(99.9), t1f7(99.9); t1f3 = t1f1 + t1f2; tester.out() << "t1f3 = t1f1 + t1f2: " << t1f3 << std::endl; tester.check("t1f3", t1f3, Tensor<1,double,Full>(0.0)); t1f4 = t1s1 + t1s2; tester.out() << "t1f4 = t1s1 + t1s2: " << t1f4 << std::endl; tester.check("t1f4", (t1f4 == t1s3)); t1f5 = t1f1 + t1s2; tester.out() << "t1f5 = t1f1 + t1s2: " << t1f5 << std::endl; tester.check("t1f5", t1f5, t1f1 + t1s2AsFull); t1f6 = t1s2 + t1f1; tester.out() << "t1f6 = t1s2 + t1f1: " << t1f6 << std::endl; tester.check("t1f6", t1f6, t1f1 + t1s2AsFull); t1f6 -= t1f1; tester.out() << "t1f6 -= t1f1: " << t1f6 << std::endl; tester.check("t1f6", t1f6, t1s2AsFull); t1s4 = t1s3 - t1f1; tester.out() << "t1s4 = t1s3 - t1f1: " << t1s4 << std::endl; tester.check("t1s4", (t1s4 == Tensor<1,double,Symmetric>(-1))); // Test Tensor dot Tensor: // Full: sum = 0.0; t1f3 = dot(t1f1, t1f2); for (i = 0; i < 1; ++i) { for (j = 0; j < 1; ++j) { for (k = 0; k < 1; ++k) { t1f3(i,k) -= t1f1(i,j)*t1f2(j,k); } } } t1f3 = t1f3*t1f3; for (i = 0; i < 1; ++i) { for (j = 0; j < 1; ++j) { sum += t1f3(i,j); } } tester.check("dot(t1f1, t1f2)", (sum == 0)); // Symmetric: sum = 0.0; t1f3 = dot(t1s1, t1s2); for (i = 0; i < 1; ++i) { for (j = 0; j < 1; ++j) { for (k = 0; k < 1; ++k) { t1f3(i,k) -= t1s1(i,j)*t1s2(j,k); } } } t1f3 = t1f3*t1f3; for (i = 0; i < 1; ++i) { for (j = 0; j < 1; ++j) { sum += t1f3(i,j); } } tester.check("dot(t1s1, t1s2)", (sum == 0)); // Test Tensor dot Vector, and vice-versa: // Full: // Vector dot Tensor Vector<1> v11(1.0); tester.out() << "v11: " << v11 << std::endl; Vector<1> v12(9.0); v12 = dot(v11, t1f2); tester.out() << "v12 = dot(v11, t1f2): " << v12 << std::endl; for (i = 0; i < 1; ++i) { for (j = 0; j < 1; ++j) { v12(j) -= v11(i)*t1f2(i,j); } } v12 = v12*v12; sum = 0.0; for (i = 0; i < 1; ++i) { sum += v12(i); } tester.check("dot(v11, t1f2)", (sum == 0)); // Tensor dot Vector v12 = dot(t1f2, v11); tester.out() << "v12 = dot(t1f2, v11): " << v12 << std::endl; for (i = 0; i < 1; ++i) { for (j = 0; j < 1; ++j) { v12(i) -= t1f2(i,j)*v11(j); } } v12 = v12*v12; sum = 0.0; for (i = 0; i < 1; ++i) { sum += v12(i); } tester.check("dot(t1f2, v11)", (sum == 0)); // Symmetric: // Vector dot Tensor v12 = dot(v11, t1s2); tester.out() << "v12 = dot(v11, t1s2): " << v12 << std::endl; for (i = 0; i < 1; ++i) { for (j = 0; j < 1; ++j) { v12(j) -= v11(i)*t1s2(i,j); } } v12 = v12*v12; sum = 0.0; for (i = 0; i < 1; ++i) { sum += v12(i); } tester.check("dot(v11, t1s2)", (sum == 0)); // Tensor dot Vector v12 = dot(t1s2, v11); tester.out() << "v12 = dot(t1s2, v11): " << v12 << std::endl; for (i = 0; i < 1; ++i) { for (j = 0; j < 1; ++j) { v12(i) -= t1s2(i,j)*v11(j); } } v12 = v12*v12; sum = 0.0; for (i = 0; i < 1; ++i) { sum += v12(i); } tester.check("dot(t1s2, v11)", (sum == 0)); int ret = tester.results("TestSymmetricTensors"); Pooma::finalize(); return ret; }