1 // This is core/vcsl/examples/vcsl_tutor.cxx
2
3 //:
4 // \file
5 // \brief Demonstrates basic use of vcsl coordinate system library
6 // \author Rob.Campbell@att.net
7 //
8 // Abbreviations used in comments:
9 // - CS coordinate system
10 //
11 // Required libraries: vcl, vbl, vnl, vcsl
12
13 #include <iostream>
14 #include "vcsl/vcsl_cartesian_3d.h"
15 #include "vcsl/vcsl_rotation.h"
16 #include "vnl/vnl_math.h"
17 #include "vcsl/vcsl_graph.h"
18 #include <cassert>
19 #ifdef _MSC_VER
20 # include "vcl_msvc_warnings.h"
21 #endif
22
23 int
main()24 main()
25 {
26 //: Convenient indices for vectors representing points, etc.
27 enum AXES
28 {
29 X,
30 Y,
31 Z,
32 T
33 };
34
35 //: Graph of defined CS
36 // All CS must be added to this graph as they are created.
37 vcsl_graph_sptr graphCS = new vcsl_graph;
38
39 //: World Coordinate System
40 // Equivalent to the Top view, by definition.
41 //
42 // A CS for which no parent is defined is absolute. Logically,
43 // there must be one absolute CS which all other directly or
44 // indirectly relate to.
45 vcsl_spatial_sptr WCS = new vcsl_cartesian_3d;
46
47 // Add WCS to the global map of coordinate systems.
48 WCS->set_graph(graphCS);
49
50 // New CS can be defined relative to any point (translation) or
51 // axis (rotation) in the parent CS. But it is so common to do
52 // so relative to the X, Y, or Z axes, that it is convenient to
53 // define the corresponding vectors:
54
55 //: x-axis vector
56 vnl_vector_fixed<double, 3> xA;
57 xA[X] = 1;
58 xA[Y] = 0;
59 xA[Z] = 0;
60
61 //: y-axis vector
62 vnl_vector_fixed<double, 3> yA;
63 yA[X] = 0;
64 yA[Y] = 1;
65 yA[Z] = 0;
66
67 //: z-axis vector
68 vnl_vector_fixed<double, 3> zA;
69 zA[X] = 0;
70 zA[Y] = 0;
71 zA[Z] = 1;
72
73 //: 90 degree rotation about WCS y-axis
74 // Transforms from WCS to right CS
75 vcsl_rotation rightXF;
76 rightXF.set_static(vnl_math::pi_over_2, yA.as_ref());
77
78 //: WCS rotated 90 degrees about the y-axis to produce right hand view/CS
79 vcsl_spatial_sptr right = new vcsl_cartesian_3d;
80 right->set_graph(graphCS);
81 right->set_unique(WCS, &rightXF);
82
83 //: Corner of a box with opposite corner at origin of WCS.
84 vnl_vector_fixed<double, 3> corner;
85 corner[X] = 1;
86 corner[Y] = 2;
87 corner[Z] = 3;
88
89 // By inspection, corner should be (-3,2,1) in 'right' CS
90 vnl_vector<double> cornerXF = WCS->from_local_to_cs(corner.as_ref(), right, 0);
91
92 std::cout << cornerXF[0] << ", " << cornerXF[1] << ", " << cornerXF[2] << '\n';
93 assert(std::abs(cornerXF[0] + 3) < 1e-6);
94 assert(std::abs(cornerXF[1] - 2) < 1e-6);
95 assert(std::abs(cornerXF[2] - 1) < 1e-6);
96
97 // Note that at this point, none of the smart pointers should be deleted
98 // since smart pointers, by definition, clean up themselves:
99 // delete right->parent(); delete right; // don't !!
100
101 return 0;
102 }
103