1 // This is brl/bbas/bvgl/bvgl_spline_region_3d.h
2 #ifndef bvgl_spline_region_3d_h_
3 #define bvgl_spline_region_3d_h_
4 //:
5 // \file
6 // \brief A 3-d plane surface bounded by a cubic spline lying in the plane
7 // \author December 20, 2015 J.L. Mundy
8 //
9 
10 #include <vector>
11 #include <iostream>
12 #include <vgl/vgl_cubic_spline_3d.h>
13 #include <vgl/vgl_cubic_spline_2d.h>
14 #include <vgl/vgl_pointset_3d.h>
15 #include <vgl/vgl_plane_3d.h>
16 #include <vgl/vgl_polygon.h>
17 #include <vgl/vgl_box_2d.h>
18 #include <vgl/vgl_box_3d.h>
19 #include <vgl/vgl_point_3d.h>
20 
21 template <class Type>
22 class bvgl_spline_region_3d
23 {
24  public:
25 
26   //: Default constructor (creates empty spline_region_3d)
bvgl_spline_region_3d()27  bvgl_spline_region_3d():tolerance_(Type(0.5)){};
28 
29   //: Construct using spline knots (must be closed curve)
30  bvgl_spline_region_3d(std::vector<vgl_point_3d<Type> > const& knots, Type tolerance);
31 
32   //: Construct using spline knots as a pointset
33  bvgl_spline_region_3d(vgl_pointset_3d<Type> const& ptset, Type tolerance);
34 
35  //: construct from a 2-d spline a 3-d origin and plane normal
36  // the spline in 3-d is a planar curve defined by the 2-d spline and the plane
37  bvgl_spline_region_3d(std::vector<vgl_point_2d<Type> > const& knots, vgl_vector_3d<Type> const& normal,
38                        vgl_point_3d<Type> const& origin, Type tolerance);
39 
40  //: set point positve, the plane is oriented so the specified point has a positive distance.
41   void set_point_positive(vgl_point_3d<Type> const& p_pos);
42 
43   //: mapping from/to the spline plane coordinates
44   void plane_to_world(Type u, Type v, vgl_point_3d<Type>& p3d) const;
45   bool world_to_plane(vgl_point_3d<Type>, Type& u, Type& v, Type tolerance) const;
46 
47   //: plane coordinate vectors
plane_coordinate_vectors(vgl_vector_3d<Type> & uvec,vgl_vector_3d<Type> & vvec)48   void plane_coordinate_vectors(vgl_vector_3d<Type>& uvec, vgl_vector_3d<Type>& vvec) const{
49     uvec = u_vec_; vvec = v_vec_;}
50 
51   //: is a point inside the region (on the plane and within or on the spline boundary
52   bool in(vgl_point_3d<Type> const& p3d) const;
53 
54   //: signed distance. If the closest planar is ::in return true, otherwise false
55   bool signed_distance(vgl_point_3d<Type> const& p, Type& dist) const;
56 
57   //: closest point in the region to p, including on the boundary
58   vgl_point_3d<Type> closest_point(vgl_point_3d<Type> const& p) const;
59 
max_t()60   Type max_t() const {return spline_2d_.max_t();}
61   //: a point on the spline curve at parameter t
62   vgl_point_3d<Type> operator () (Type t) const;
63 
64   //: centroid of the region
65   vgl_point_3d<Type> compute_centroid() const;
66   vgl_point_2d<Type> compute_centroid_2d() const;
67 
centroid()68   vgl_point_3d<Type> centroid() const{return centroid_3d_;}
centroid_2d()69   vgl_point_2d<Type> centroid_2d() const{return centroid_2d_;}
70 
71   //: area of the region
72   Type area() const;
73 
74   //: plane normal
normal()75   vgl_vector_3d<Type> normal() const{ return unit_normal_;}
76 
77   //: plane
plane()78   vgl_plane_3d<Type> plane() const {return plane_;}
79 
80   //: bounding box methods
81   vgl_box_2d<Type> bounding_box_2d() const;
82   vgl_box_3d<Type> bounding_box() const;
83 
84   //: accessors
knots()85   std::vector<vgl_point_3d<Type> > knots() const {return spline_3d_.knots();}
knots_2d()86   std::vector<vgl_point_2d<Type> > knots_2d() const {return spline_2d_.knots();}
origin()87   vgl_point_3d<Type> origin() const {return origin_;}
88 
89   //: set parameters for scaling for efficiency. apply these calls before computing vector field values
90   // if |L1| == 0 then the principal axis is u.
91   void set_principal_eigenvector(vgl_vector_3d<Type> const& L1);
set_principal_offset(Type principal_offset)92   void set_principal_offset(Type principal_offset){principal_offset_ = principal_offset;}
set_deformation_eigenvalues(Type su,Type sv)93   void set_deformation_eigenvalues(Type su, Type sv){su_ = su; sv_ = sv;}
set_offset_vector(vgl_vector_3d<Type> const & tv)94   void set_offset_vector(vgl_vector_3d<Type> const& tv){tv_ = tv;}
95   //: inverse vector  field for the scale transformation
96   bool inverse_vector_field(vgl_point_3d<Type> const& p, vgl_vector_3d<Type>& inv, Type tolerance = Type(-1)) const;
97 
98   //: forward vector field, used to couple the deformation field to adjacent structures
99   bool vector_field(vgl_point_3d<Type> const& p, vgl_vector_3d<Type>& vf, vgl_vector_3d<Type> const& tv) const;
100 
101   //: scale the boundary isotropically around the centroid and translate by vector tv
102   // useful for generalized cylinder applications
103   bvgl_spline_region_3d<Type> scale(Type s,vgl_vector_3d<Type> const& tv) const;
104 
105   //: anisotropically scale the boundary about the centroid with principal axis L1 and translate by vector tv
106   // if |L1| == 0 then the principal axis is u.
107   bvgl_spline_region_3d<Type> scale(Type su, Type sv, vgl_vector_3d<Type> const& tv, vgl_vector_3d<Type> const& L1, bool verbose = false)const;
108 
109   // for debug purposes
110   // generate a random poinset drawn from the region
111   vgl_pointset_3d<Type> random_pointset(unsigned n_pts) const;
112  private:
113   Type tolerance_;
114   vgl_polygon<Type> poly_2d_;  // to test inside
115   vgl_point_2d<Type> centroid_2d_;
116   vgl_point_3d<Type> centroid_3d_;
117   vgl_cubic_spline_2d<Type> spline_2d_;
118   vgl_cubic_spline_3d<Type> spline_3d_;
119   vgl_plane_3d<Type> plane_;
120   // a point on the plane to support some forms of construction
121   vgl_point_3d<Type> origin_;
122   // contained in plane but for efficiency cache unit vector
123   vgl_vector_3d<Type> unit_normal_;
124   //cache plane coordinate vectors for convenience
125   vgl_vector_3d<Type> u_vec_;
126   vgl_vector_3d<Type> v_vec_;
127   Type principal_offset_;
128   // parameters for deformation
129   Type su_; //first principal eigenvalue
130   Type sv_; //second principal eigenvalue
131   vgl_vector_3d<Type> tv_;//translation vector
132   Type sang_; //principal_axis_sine
133   Type cang_; // principal_axis_cosine;
134 };
135 
136 #define BVGL_SPLINE_REGION_3D(T) extern "please include bbas/bvgl/bvgl_spline_region_3d.hxx first"
137 
138 #endif // bvgl_spline_region_3d_h_
139