1 
2 // J.L. Mundy August, 2015
3 #include <iostream>
4 #include <fstream>
5 #include <cmath>
6 #include "testlib/testlib_test.h"
7 #include "vgl/vgl_pointset_3d.h"
8 #include "vgl/vgl_point_3d.h"
9 #include "vgl/vgl_bounding_box.h"
10 #include "vgl/vgl_box_3d.h"
11 #ifdef _MSC_VER
12 #  include "vcl_msvc_warnings.h"
13 #endif
14 #include "vpl/vpl.h"
15 
16 static void
test_pointset()17 test_pointset()
18 {
19   vgl_point_3d<double> p0(1.0, 2.0, 3.0);
20   vgl_point_3d<double> p1(4.0, 5.0, 6.0);
21   vgl_point_3d<double> p2(7.0, 8.0, 9.0);
22   vgl_vector_3d<double> n0(1.0, 0.0, 0.0);
23   vgl_vector_3d<double> n1(0.0, 1.0, 0.0);
24   vgl_vector_3d<double> n2(0.0, 0.0, 1.0);
25 
26   std::vector<vgl_point_3d<double>> pts;
27   pts.push_back(p0);
28   pts.push_back(p1);
29   pts.push_back(p2);
30   std::vector<vgl_vector_3d<double>> normals;
31   normals.push_back(n0);
32   normals.push_back(n1);
33   normals.push_back(n2);
34   vgl_pointset_3d<double> ptset(pts, normals);
35 
36   // test accessors
37   bool good = ptset.npts() == 3;
38   good = good && ptset.has_normals();
39   good = good && (ptset.p(1) == p1) && (ptset.n(1) == n1);
40   good = good && ptset.set_point(2, vgl_point_3d<double>(1.0, 1.0, 1.0));
41   good = good && ptset.set_normal(2, vgl_vector_3d<double>(0.577, 0.577, 0.577));
42   good = good && ptset.p(2).x() == 1.0 && ptset.n(2).x() == 0.577;
43   TEST(" Accessors ", good, true);
44 
45   // test bounding box
46   vgl_box_3d<double> box = vgl_bounding_box(ptset);
47   vgl_point_3d<double> min_pt = box.min_point();
48   vgl_point_3d<double> max_pt = box.max_point();
49   good = (min_pt == vgl_point_3d<double>(1.0, 1.0, 1.0)) && (max_pt == p1);
50   TEST(" bounding box ", good, true);
51   // test IO
52   std::string path = "./test_ptset_io.txt";
53   std::ofstream ostr(path.c_str());
54   ostr << ptset;
55   ostr.close();
56   std::ifstream istr(path.c_str());
57   vgl_pointset_3d<double> io_ptset;
58   istr >> io_ptset;
59   good = io_ptset == ptset;
60   TEST("pointset_ I/O", good, true);
61   istr.close();
62   vpl_unlink(path.c_str());
63   // test space separated pointset IO
64   std::ofstream sostr(path.c_str());
65   sostr << p0.x() << ' ' << p0.y() << ' ' << p0.z() << std::endl;
66   sostr << p1.x() << ' ' << p1.y() << ' ' << p1.z() << std::endl;
67   sostr << p2.x() << ' ' << p2.y() << ' ' << p2.z() << std::endl;
68   sostr.close();
69   vgl_pointset_3d<double> spset;
70   std::ifstream sistr(path.c_str());
71   sistr >> spset;
72   good = spset.npts() == 3;
73   good = good && spset.p(0) == p0;
74   TEST("spaced pointset_ I/O", good, true);
75   sistr.close();
76   vpl_unlink(path.c_str());
77   // test poinset with scalars
78   std::vector<double> sclrs;
79   sclrs.push_back(1.0);
80   sclrs.push_back(1.5);
81   sclrs.push_back(2.0);
82   vgl_pointset_3d<double> ptset_sc(pts, sclrs), ptset_sc_in;
83   good = ptset_sc.has_scalars() && !ptset_sc.has_normals();
84   good = good && ptset_sc.sc(1) == 1.5;
85   std::ofstream scostr(path.c_str());
86   scostr << ptset_sc;
87   scostr.close();
88   std::ifstream scistr(path.c_str());
89   scistr >> ptset_sc_in;
90   good = good && (ptset_sc_in == ptset_sc);
91   TEST("pointset with scalars", good, true);
92   scistr.close();
93   vpl_unlink(path.c_str());
94   vgl_pointset_3d<double> ptset_nsc(pts, normals, sclrs), ptset_nsc_in;
95   good = ptset_nsc.has_scalars() && ptset_nsc.has_normals();
96   good = good && ptset_nsc.sc(2) == 2.0;
97   std::ofstream nscostr(path.c_str());
98   nscostr << ptset_nsc;
99   nscostr.close();
100   std::ifstream nscistr(path.c_str());
101   nscistr >> ptset_nsc_in;
102   good = good && (ptset_nsc_in == ptset_nsc);
103   nscistr.close();
104   TEST("pointset with scalars and normals", good, true);
105   vpl_unlink(path.c_str());
106 }
107 
108 TESTMAIN(test_pointset);
109