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
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
00081
00082
00083 BSplineSurface();
00084 ~BSplineSurface();
00085 BSplineSurface(const BSplineSurface &B);
00086
00087
00088 bool LoadPatch(const char *fname);
00089 bool SavePatch(const char *fname);
00090 void Reset(int u,int v);
00091
00092
00093
00094 void Resize(unsigned int num_rows,unsigned int num_cols);
00095
00096
00097 float HasContracted(BSplineSurface *b, float u, float v);
00098
00099 void SetPrevious(BSplineSurface *b)
00100 {psurf = b;};
00101 void VisualizeContraction(bool c)
00102 {contraction=c;}
00103
00104
00105
00106 void DoubleU();
00107
00108 void HalveU();
00109
00110 void DoubleV();
00111
00112 void HalveV();
00113
00114
00115 void IsShaded(bool shaded){wireframe=!shaded;};
00116
00117
00118
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
00124 void SetColors(double c1[3],double c2[3],double c3[3]);
00125
00126
00127
00128 bool EditLevel(unsigned int i);
00129
00130
00131
00132 void SetPrecision(double u, double v){u_prec=u;v_prec=v;};
00133
00134
00135 void SelectPoint(Point3d n, Matrix3d A, Point3d t);
00136
00137 void MovePoint(Point3d m, Matrix3d A, Point3d t);
00138
00139
00140 void SelectRegion(vector<Point3d > s_reg, Matrix3d A, Point3d t);
00141
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
00152 void MoveRegion(Point3d dis);
00153
00154
00155
00156
00157 void DisplayControlPoints(Matrix3d A, Point3d t);
00158
00159 void DisplaySurface();
00160
00161 void DrawCircle(double x, double y, double z, double w);
00162
00163
00164
00165 Point3d Eval(double u, double v) const;
00166
00167 Point3d EvalU(double u, int m) const;
00168
00169 Point3d NormalEval(double u, double v) const;
00170
00171 Point3d PartialU(double u, double v) const;
00172
00173 Point3d PartialV(double u, double v) const;
00174
00175
00176 bool PointInversion(Point2d &surf_p, Point2d p0, Point3d q) const;
00177
00178
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
00189
00190
00191
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
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
00272
00273
00274 };
00275 }
00276
00277 using Shapes::BSplineSurface;
00278 }
00279 #endif
00280