Overture  Version 25
NurbsMapping.h
Go to the documentation of this file.
1 #ifndef NURBS_MAPPING_H
2 #define NURBS_MAPPING_H
3 
4 #include "Mapping.h"
5 #include <vector>
6 
7 class IgesReader; // forward declaration
10 
11 //-------------------------------------------------------------
12 // Define a Nurbs Curve or Surface
13 //-------------------------------------------------------------
14 class NurbsMapping : public Mapping
15 {
16 
17 // AP: this doesn't seem necessary anymore: friend class PlotStuff;
18 
19 public:
20 
21  enum ParameterizationTypeEnum // these are used by the interpolate functions
22  {
25  };
26 
28  {
29  maximumOrder=15 // maximum order for nurbs (p1+1) <= maximumOrder
30  };
31 
33  {
34  xxww, // ascii file format is x1,x2,... y1,y2,... z1,z2,..., w1,w2,...
35  xwxw, // ascii file format is x1,y1,z1,w1, x2,y2,z2,w2,...
36  cheryl // cheryl's format
37  };
38 
39  NurbsMapping();
40  NurbsMapping(const int & domainDimension , const int & rangeDimension_); // make a default NURBS
41 
42  // Copy constructor is deep by default
43  NurbsMapping( const NurbsMapping &, const CopyType copyType=DEEP );
44 
45  ~NurbsMapping();
46 
47  NurbsMapping & operator =( const NurbsMapping & X0 );
48 
49  int addSubCurve(NurbsMapping &nurbs);
50 
53 
54  virtual void basicInverseS(const RealArray & x,
55  RealArray & r,
58 
59  int binomial(const int m, const int n); // compute m! /( (m-n)! n! )
60 
61  // split a curve at corners into sub-curves.
62  int buildSubCurves( real angle = 60. );
63 
64  // build a new Nurbs curve that matches a coordinate line on the surface
65  int buildCurveOnSurface( NurbsMapping & curve, real r0, real r1=-1. );
66 
67  // Build a new curve (e.g. x(s)) that represents one component of an existing curve (x(s),y(s),z(s))
68  int buildComponentCurve(NurbsMapping & curve, int component =0 );
69 
70  // build a circle with centre o, radius r, in the plane defined by the orthogonal unit vectors x,y
71  int circle(RealArray & o,
72  RealArray & x,
73  RealArray & y,
74  real r,
75  real startAngle=0.,
76  real endAngle=1. );
77 
78  // Build a NURBS for a conic defined by end points, two tangents, and an additional point
79  int conic( const RealArray &p0, const RealArray &t0, const RealArray &p2, const RealArray &t2,
80  const RealArray &p );
81 
82  // Build a NURBS for a conic defined by an implicit formula and two points
83  int conic( const real a, const real b, const real c, const real d, const real e, const real f,
84  const real z, const real x1, const real y1, const real x2, const real y2 );
85 
86  int deleteSubCurve(int sc);
87 
88  virtual void display( const aString & label=blankString) const;
89 
90  int elevateDegree(const int increment);
91 
92  // merge two nurbs, add a straight line segment if they do not match up closely
93  int forcedMerge(NurbsMapping & nurbs );
94 
95  // force a nurbs mapping to be periodic, moving the last control point if neccessary...
96  int forcePeriodic();
97 
98  // Build a general cylinder (tabulated cylnder) by extruding a curve along a direction vector
99  int generalCylinder( const Mapping & curve, real d[3] );
100 
101  // Build a general cylinder (tabulated cylnder) by interpolating two curves
102  int generalCylinder( const Mapping & curve1, const Mapping & curve2 );
103 
104  // build a lofted surface by interpolation from a list of mappings
105  int interpolateLoftedSurface(std::vector<Mapping *>&, int degree1=3,
106  int degree2=3,
108  int numberOfGhostPoints=0 );
109 
110  const RealArray & getControlPoints() const;
111 
112  // get uKnot or vKnot, the knots in the first or second direction.
113  const RealArray & getKnots( int direction=0 ) const;
114 
115  virtual int get( const GenericDataBase & dir, const aString & name); // get from a database file
116  int get( FILE *file, const FileFormat & fileFormat=xxww ); // read from an ascii file.
117  int get(const aString & fileName, const FileFormat & fileFormat=xxww);
118  aString getClassName() const { return NurbsMapping::className; }
119 
121  bool includeGhost=false);
122 
123 
124  int getNumberOfControlPoints( int axis=0 ) const; // n+1
125 
126  int getNumberOfKnots( int axis=0 ) const; // m+1
127 
128  int getOrder( int axis=0 ) const; // get order p
129 
130  real getOriginalDomainBound(int side, int axis);
131 
132  int getParameterBounds( int axis, real & rStart, real & rEnd ) const;
133 
134  // insert a knot
135  int insertKnot(const real & uBar, const int & numberOfTimesToInsert=1 );
136 
137  // construct a NURBS by interpolating another mapping.
138  void interpolate(Mapping & map,
139  int degree=3,
141  int numberOfGhostPoints=0,
142  int *numPointsToInterpolate=NULL );
143 
144  // make a nurb that passes through given points, optionally pass parameterization, optionally get parameterization
145  void interpolate(const RealArray & x,
146  const int & option = 0 ,
147  RealArray & parameterization =Overture::nullRealArray(),
148  int degree = 3,
150  int numberOfGhostPoints=0 );
151 
152  #ifdef USE_PPP
153  // obsolete method -- keep for now for backward compatibility
154  void interpolate(const realArray & x,
155  const int & option = 0 ,
156  realArray & parameterization =Overture::nullRealDistributedArray(),
157  int degree = 3,
159  int numberOfGhostPoints=0 );
160  #endif
161 
162  // make a nurb surface that passes through given points. Accessed through the above routine
163  void interpolateSurface(const RealArray & x,
164  int degree = 3,
166  int numberOfGhostPoints=0,
167  int degree2 = 3);
168 
169  void interpolateVolume(const RealArray & x,
170  int degree = 3,
172  int numberOfGhostPoints=0 );
173 
174  bool isInitialized(){return initialized;};
175 
176  bool isSubCurveHidden(int sc); // is subCurve sc hidden?
177 
178  bool isSubCurveOriginal(int sc); // is the original marker of subCurve sc set?
179 
180  // join two consequtive sub-curves and modify the subCurves and subCurveState arrays
181  int joinSubCurves( int subCurveNumber );
182 
183  // join two sub-curves (merge into one)
184  int joinSubCurves( int subCurve1, int subCurve2 );
185 
186  int line( const RealArray &p1, const RealArray &p2 );
187 
188  void lowerRangeDimension();
189 
190  Mapping *make( const aString & mappingClassName );
191 
192  void map( const realArray & r, realArray & x, realArray & xr = Overture::nullRealDistributedArray(),
194 
195  virtual void mapS( const RealArray & r, RealArray & x, RealArray &xr = Overture::nullRealArray(),
197 
198  // transform using a 3x3 matrix
199  int matrixTransform( const RealArray & r );
200 
201  // merge (join) two nurbs's
202  int merge(NurbsMapping & nurbs, bool keepFailed = true, real eps=-1, bool attemptPeriodic=true );
203 
204  int moveEndpoint( int end, const RealArray &endPoint, real tol=-1. );
205 
206  // if the Nurb is formed by merging a sequence of Nurbs then function will return that number.
207  int numberOfSubCurves() const; // all visible subcurves
208  int numberOfSubCurvesInList() const; // all subcurves
209 
210  // Indicate that this nurbs is a parametric curve on another nurbs.
211  int parametricCurve(const NurbsMapping & nurbs,
212  const bool & scaleParameterSpace=TRUE );
213 
214  int parametricSplineSurface(int mu, int mv, RealArray & u, RealArray & v, RealArray & poly );
215 
216  // build a plane that passes through three points
217  int plane( real pt1[3], real pt2[3], real pt3[3] );
218 
219  int plot(GenericGraphicsInterface & gi, GraphicsParameters & parameters, bool plotControlPoints = FALSE );
220 
221  virtual int put( GenericDataBase & dir, const aString & name) const; // put to a database file
222  int put( FILE *file, const FileFormat & fileFormat=xxww ); // save basic NURBS data to a readable ascii file.
223  int put(const aString & fileName, const FileFormat & fileFormat=xxww);
224 
225  int readFromIgesFile( IgesReader & iges, const int & item, bool normKnots=true );
226 
227  int removeKnot(const int & index,
228  const int & numberOfTimesToRemove,
229  int & numberRemoved,
230  const real &tol = 100.*FLT_EPSILON);
231 
232  // rescale u and v
233  int reparameterize(const real & uMin,
234  const real & uMax,
235  const real & vMin=0.,
236  const real & vMax=1.);
237 
238  // rotate about a given axis
239  int rotate( const int & axis, const real & theta );
240 
241  // scale the NURBS
242  int scale(const real & scalex=1.,
243  const real & scaley=1.,
244  const real & scalez=1. );
245 
246 
247  int setDomainInterval(const real & r1Start =0.,
248  const real & r1End =1.,
249  const real & r2Start =0.,
250  const real & r2End =1.,
251  const real & r3Start =0.,
252  const real & r3End =1. );
253 
254  // shift in space
255  int shift(const real & shiftx=0.,
256  const real & shifty=0.,
257  const real & shiftz=0. );
258 
259  // specify a curve in 2D or 3D
260  int specify(const int & m,
261  const int & n,
262  const int & p,
263  const RealArray & uKnot,
264  const RealArray & cPoint,
265  const int & rangeDimension=3,
266  bool normalizeTheKnots=true );
267  // specify a NURBS with domain dimension = 2
268  int specify(const int & n1,
269  const int & n2,
270  const int & p1,
271  const int & p2,
272  const RealArray & uKnot,
273  const RealArray & vKnot,
274  const RealArray & controlPoint,
275  const int & rangeDimension =3,
276  bool normalizeTheKnots=true );
277 
278  // split the nurb into two, return the pieces but do not alter the original nurb
279  int split(real uSplit, NurbsMapping &c1, NurbsMapping&c2, bool normalizePieces =true);
280 
281  // split a sub-curve at a given position
282  int splitSubCurve( int subCurveNumber, real rSplit );
283 
284  // Here is the sub curve.
285  NurbsMapping& subCurve(int subCurveNumber); // only visible subcurves (Make arg non-ref)
286 
287  NurbsMapping& subCurveFromList(int subCurveNumber); // access all subcurves (Make arg non-ref)
288 
289  int toggleSubCurveVisibility(int sc);
290  void toggleSubCurveOriginal(int sc);
291 
292  // apply a scaling and shift to the knots
293  int transformKnots(const real & uScale,
294  const real & uShift,
295  const real & vScale =1.,
296  const real & vShift =0. );
297 
298  // apply a scaling and shift to the control points (scales and shifts the NURBS)
300  const RealArray & shift);
301 
302  // clip the nurb to the domain bounds ( ie, get rid of any extra control points and knots )
304 
305  virtual int update( MappingInformation & mapInfo );
306 
307 
308  protected:
309 
310  real angle( const RealArray &p0, const RealArray &p1, const RealArray &p2 ) const;
311 
312  void initialize( bool setMappingHasChanged=true );
313 
314  int normalizeKnots();
315 
316  int makeOneArc( const RealArray &p0, const RealArray &t0, const RealArray &p2, const RealArray &t2,
317  const RealArray &p, RealArray &p1, real&w1 ) const;
318 
319  int splitArc(const RealArray &p0, const RealArray &p1, const real & w1, const RealArray &p2,
320  RealArray &q1, RealArray &s, RealArray &r1, real &wqr) const;
321 
322  int intersect3DLines( const RealArray & p0, const RealArray & t0,
323  const RealArray & p1, const RealArray & t1,
324  real & alpha0, real & alpha1,
325  RealArray & p2) const;
326 
327  real distance4D( const RealArray & x, const RealArray & y );
328 
329  // vectorized version of map -- not normally used --
330  virtual void mapVector(const RealArray & r, RealArray & x, RealArray & xr = Overture::nullRealArray(),
332 
333  void setBounds(); // set bounds for plotting etc.
334 
335  void privateInterpolate( const RealArray & x, const RealArray *uBar );
336 
337  private:
338 
339  aString className;
340  int n1,m1,p1;
341  int n2,m2,p2;
342  int n3,m3,p3; // kkc 051031
343  RealArray uKnot,vKnot,wKnot,cPoint; // kkc added wKnot 051031
344  bool initialized;
345  bool nonUniformWeights; // true if the weights are not all constant
346  real uMin,uMax,vMin,vMax,wMin,wMax; // original knot extent (they are scaled to [0,1])
347  int nurbsIsPeriodic[3]; // for reparameterization, remember original periodicity.
348  real rStart[3], rEnd[3]; // for reparameterization
349 
350  bool mappingNeedsToBeReinitialized;
351 
352  // if the Nurb is formed by merging a sequence of Nurbs then we may keep the
353  // Nurbs that were merged for later use.
354 
355  int numberOfCurves;
356  NurbsMapping **subCurves;
357  char *subCurveState;
358  int lastVisible;
359 
360  public:
362 
363  static bool useScalarEvaluation;
364 
365  private:
366 
367  //
368  // Virtual member functions used only through class ReferenceCounting:
369  //
371  { return operator=((NurbsMapping &)x); }
372  virtual void reference( const ReferenceCounting& x)
373  { reference((NurbsMapping &)x); } // *** Conversion to this class for the virtual = ****
374  virtual ReferenceCounting* virtualConstructor( const CopyType ct = DEEP ) const
375  { return ::new NurbsMapping(*this, ct); }
376 
377 
378 
379 
380  int findSpan(const int & n ,
381  const int & p,
382  const real & u,
383  const RealArray & uKnot );
384  void basisFuns(const int & i,
385  const real & u,
386  const int & p,
387  const RealArray & uKnot,
388  RealArray & basis );
389  void dersBasisFuns(const int & i,
390  const real & u,
391  const int & p,
392  const int & order,
393  const RealArray & uKnot,
394  real *ders );
395 
396  // vectorized versions
397  void findSpan(const int & n ,
398  const int & p,
399  const Index & I,
400  const RealArray & u,
401  const RealArray & knot,
402  IntegerArray & span );
403 
404  void dersBasisFuns(const Index & I,
405  const IntegerArray & ia,
406  const RealArray & u,
407  const int & p,
408  const int & order,
409  const RealArray & knot,
410  RealArray & ders );
411 };
412 
413 
414 #endif