bsurface.h

00001 #ifndef __BSURFACE_H__
00002 #define __BSURFACE_H__
00003 
00004 #include <config.h>
00005 #include <vector>
00006 #include <string>
00007 
00008 #include <util/vector.h>
00009 #include <util/matrix.h>
00010 #include <geometry/geometry.h>
00011 
00012 namespace util
00013 {
00014   //#define Debug false
00015 
00016   using std::vector;
00017 
00018   using geometry::Matrix3d;
00019   using geometry::Point3d;
00020   using geometry::Point2d;
00021 
00029   namespace Shapes
00030   {
00031 
00037     class BSplineSurface
00038     {
00039     public:
00040 
00042 
00043 
00051       void Load(const char* fname, double scale, int TextureId, int divU, int divV);
00053       void Reread();
00055       void BoundingBox(double &x1,double &y1,double &z1,double &x2,double &y2,double &z2);
00057       void Transform(Matrix3d M, double x, double y, double z);
00059       bool IsTextured() const
00060       { return _TextureId >= 0; }
00061 
00063       int TextureId() const
00064       { return _TextureId; }
00066 
00068       void Draw(int uDiv, int vDiv);
00069 
00071       void Recompute()
00072       {precomputed=false;}
00073 
00074       void GetControlMesh(int i, int j, double &x,double &y,double &z)
00075       {x=Control_points[0][i][j][0];y=Control_points[0][i][j][1];z=Control_points[0][i][j][2];}
00076 
00077       void SetControlMesh(int i, int j,double x, double y, double z)
00078       {Control_points[0][i][j][0]=x;Control_points[0][i][j][1]=y;Control_points[0][i][j][2]=z;}
00079 
00080       //Functions used in splineEdit and frameEdit
00081 
00082       //Constructors and Destructors
00083       BSplineSurface();
00084       ~BSplineSurface();
00085       BSplineSurface(const BSplineSurface &B);
00086 
00087       //Save/Load the surface in fname
00088       bool  LoadPatch(const char *fname);
00089       bool  SavePatch(const char *fname);
00090       void Reset(int u,int v);  
00091 
00092       //Resizes the patch to have num_rows rows and num_cols columns using forward and reverse subdivision
00093       //It is assumed that the dimensions passed in can be created simply by using forward and reverse subdivision
00094       void Resize(unsigned int num_rows,unsigned int num_cols);
00095 
00096       //Returns first direvative of change in area at u,v
00097       float HasContracted(BSplineSurface *b, float u, float v);
00098       //Provides previous b-spline surface to perform the above comparison on when rendering
00099       void SetPrevious(BSplineSurface *b)
00100       {psurf = b;};
00101       void VisualizeContraction(bool c)
00102       {contraction=c;}
00103 
00104 
00105       //Applys forward subdivision along the U-curves of the control points
00106       void DoubleU();
00107       //Applys reverse subdivision along the U-curves of the control points
00108       void HalveU();
00109       //Applys forward subdivision along the V-curves of the control points
00110       void DoubleV();
00111       //Applys reverse subdivision along the V-curves of the control points
00112       void HalveV();
00113 
00114       //Toggles between wireframe and shaded renderings
00115       void IsShaded(bool shaded){wireframe=!shaded;};
00116       //Sets the degree of the surface, if degree!=3 then forward and reverse subdivision will not work
00117       //  void setdegree(int i){degree=i;};
00118       //Toggles between a shaded wireframe mesh and shaded polygons
00119       void ToggleShadedMesh(){s_mesh=false;};
00120       void TogglePolygonMesh(){s_mesh=true;};
00121       void ToggleNormals(){disp_normal=!disp_normal;};
00122       void ToggleControlMesh(){disp_c_mesh=!disp_c_mesh;};
00123       //Changes the colors used when rendering
00124       void SetColors(double c1[3],double c2[3],double c3[3]);
00125 
00126       //Specifies which resolution of control points is being edited
00127       //returns false if level specified does not exist
00128       bool EditLevel(unsigned int i);
00129 
00130       //Specifies the length of u and v steps to be used when rendering
00131       //the surface
00132       void SetPrecision(double u, double v){u_prec=u;v_prec=v;};
00133 
00134       //Select a point for editing
00135       void SelectPoint(Point3d n, Matrix3d A, Point3d t);
00136       //Move the selected point
00137       void MovePoint(Point3d m, Matrix3d A, Point3d t);
00138 
00139       //Select a region for editing
00140       void SelectRegion(vector<Point3d > s_reg, Matrix3d A, Point3d t);
00141       //Determine whether to_check fall in the contour defined by  shape
00142       bool InsideContour(vector<Point3d > shape, Point3d to_check);
00143 
00144       int SelectedLevel()
00145       {return selected_level;}
00146       vector<int*> SelectedIndicies()
00147       {return selected_region;}
00148       vector<vector<vector<Point3d > > >* ControlPoints()
00149       {return &Control_points;}
00150 
00151       //Move the selected region
00152       void  MoveRegion(Point3d dis);
00153       //Apply the bending tool to the selected region
00154       //void RotateRegion(Point2d c_rot,double theta,double r_dis, Matrix3d A);
00155 
00156       //renders the control points as circles
00157       void DisplayControlPoints(Matrix3d A, Point3d t);
00158       //renders the surface based on options contained in the class
00159       void DisplaySurface();
00160       //draws a chircel at x,y,z with radius w
00161       void DrawCircle(double x, double y, double z, double w);
00162 
00163       //Functions working on the parameter range (1.0,n_u+degree-1.0)x(1.0,n_v+degree-1.0)
00164       //evaluates the b-spline surface at (u,v)
00165       Point3d Eval(double u, double v) const;
00166       //evaluates the u-curve at u
00167       Point3d EvalU(double u, int m) const;
00168       //evaluates the normal at u,v
00169       Point3d NormalEval(double u, double v) const;
00170       //evaluates F(u,v)/du
00171       Point3d PartialU(double u, double v) const;
00172       //evaluates F(u,v)/dv
00173       Point3d PartialV(double u, double v) const;
00174       //Locates the closest point to q on the surface using p0 as
00175       //an initial guess
00176       bool PointInversion(Point2d &surf_p, Point2d p0, Point3d q) const;
00177 
00178       //The same functionality as above, but for the parameter range (0,1)x(0,1)
00179       Point3d EvalN(double u, double v) const;
00180       Point3d NormalEvalN(double u, double v) const; 
00181       Point3d PartialUN(double u, double v) const;
00182       Point3d PartialVN(double u, double v) const;
00183 
00184       bool PointInversionN(Point2d &surf_p, Point2d p0, Point3d q) const;
00185       inline double ConvertU(double u) const;
00186       inline double ConvertV(double v) const;
00187 
00188       //Parameterized representation of the curve along the lines
00189       //(0,0)(0,1),(0,1)(1,1),(1,1)(1,0),(1,0)(0,0) in the order given
00190       //this traces out the boundary of the b-spline patch, u varies between
00191       //0 and 1
00192       Point3d ContourEval(double u) const;
00193 
00194       Point2d ContourInverse(double u) const;
00195 
00196       BSplineSurface Interpolate(const BSplineSurface &BS1, const BSplineSurface &BS2, double alpha, int num_rows, int num_cols) const;
00197       unsigned int LowestResDim(int i);
00198       BSplineSurface LowestRes();
00199 
00200       void UpdateFootprint();
00201       void RenderSupport(unsigned int i,unsigned int j,unsigned int level);
00202       void RenderFootprint();
00203       void CalcFootprint(unsigned int i,unsigned  int j,unsigned int level);
00204       void CalcPreImage(int i, int j, unsigned int level);
00205 
00206       void  RenderLine(int i1, int j1, int level1, int i2, int j2, int level2);
00207 
00208       BSplineSurface& operator=(const BSplineSurface& B);
00209       BSplineSurface operator+(const BSplineSurface& B);
00210       BSplineSurface operator-(const BSplineSurface& B);
00211       BSplineSurface operator*(const double c);
00212 
00213       int DimU(){return n_u;};
00214       int DimV(){return n_v;};
00215 
00216     private:
00217       //        UVPrecision _uvPrecision;
00218       inline void Set(int r, int c, Point3d v)
00219       { Control_points[0][r][c] = v; }
00220       inline Point3d Get(int r, int c) const
00221       { return Control_points[0][r][c]; }
00222 
00223       std::string _fname;
00224       double _scale;
00225 
00226       int _TextureId;
00227 
00228       int PtId(int s, int t) const
00229       {
00230         return s+(_divU+1)*t;
00231       }
00232 
00233       int _divU, _divV;
00234       vector<Point3d > _vrtx;
00235       vector<Point3d > _nrml;
00236       vector<float > _ctract; 
00237       float c_min,c_max;
00238 
00239       void Precompute(int, int);
00240       bool precomputed;
00241       std::string _f_name;
00242 
00243       bool s_mesh;
00244       bool wireframe;
00245       bool lines;
00246       bool contraction;
00247       bool disp_normal;
00248       bool disp_c_mesh;
00249       bool periodic;
00250 
00251       unsigned int selected[2];
00252       int selected_level;
00253       int n_u,n_v;
00254 
00255       double sel_point[3];
00256       double unsel_point[3];
00257       double poly_line[3];
00258       int degree;
00259 
00260       vector< vector< vector< Point3d > > > Control_points;
00261       vector< vector< vector< Point3d > > > Details;
00262 
00263       vector< int* > footprint;
00264       vector< int* > selected_region;
00265       int selected_bb[4];
00266 
00267       double u_prec,v_prec;  
00268 
00269       BSplineSurface *psurf;
00270       /*
00271          void Update_Mres();
00272          */
00273 
00274     };
00275   } // namespace Shapes
00276 
00277   using Shapes::BSplineSurface;
00278 }
00279 #endif
00280 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Fri May 31 15:37:52 2013 for VVE by  doxygen 1.6.3