UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
base_splines.hpp
1 /* base_splines.hpp */
2 /* $Id: base_splines.hpp,v 1.3 2007/09/10 08:42:07 guser1 Exp $ */
3 #ifndef __base_splines_hpp
4 #define __base_splines_hpp
5 
6 #include <vector>
7 #include <math.h>
8 
9 #include "parspline.hpp"
10 
11 using namespace std;
12 
13 #define U3D_EPSILON 1.e-6
14 #define EQUAL_REALS(a,b) ( (fabs((a)-(b)) < U3D_EPSILON ? true : false) )
15 
16 //------------------------------------------------------------------------------------------
18 #define U3D_BASE_MSG_LEN 2048
19 
22 {
23 public:
25  static void info(const char* format, ...);
26 
28  static void warning(const char* format, ...);
29 
31  static void error(const char* format, ...);
32 
34  static void debug(const char* format, ...);
35 };
36 
37 
38 //------------------------------------------------------------------------------------------
41 {
42 public:
43 
45  Spl_U3dPoint ();
46 
47  virtual ~Spl_U3dPoint(){};
48 
50  Spl_U3dPoint (const Spl_U3dPoint& p);
51 
53  Spl_U3dPoint (double x, double y, double z);
54 
55  /* Create point with given point coordinates. */
56  //Spl_U3dPoint (Spl_U3dVertex vertex);
57 
59  Spl_U3dPoint& operator= (const Spl_U3dPoint& p);
60 
62  int operator==(const Spl_U3dPoint&) const;
63 
65  int operator!=(const Spl_U3dPoint&) const;
66 
67  // Point and Vector Operations (always valid)
68  Spl_U3dPoint operator+(Spl_U3dPoint); // +translate
69  Spl_U3dPoint operator-(Spl_U3dPoint); // -translate
70  //Spl_U3dPoint& operator+=(Spl_U3dVector); // inc translate
71  //Spl_U3dPoint& operator-=(Spl_U3dVector); // dec translate
72 
73  // Scalar Multiplication
74  friend Spl_U3dPoint operator*(int, Spl_U3dPoint);
75  friend Spl_U3dPoint operator*(Spl_U3dPoint, int);
76  friend Spl_U3dPoint operator*(double, Spl_U3dPoint);
77  friend Spl_U3dPoint operator*(Spl_U3dPoint, double);
78 
79  // Scalar Division
80  friend Spl_U3dPoint operator/(Spl_U3dPoint, int);
81  friend Spl_U3dPoint operator/(Spl_U3dPoint, double);
82 
84  double getX () const;
85 
87  double getY () const;
88 
90  double getZ () const;
91 
92  /* Check the point is inside given box. */
93  //virtual bool isInside (const Spl_U3dBox& mbox) const;
94 
96  void setXYZ (double x, double y, double z);
97 
99  void setX (double x);
100 
102  void setY (double y);
103 
105  void setZ (double z);
106 
107  //Spl_U3dVertex getVertex();
108 
109 protected:
110 
112  double fX, fY, fZ;
113 
114 };
115 
116 
117 /* INLINE implementation */
118 
120  : fX(0.0), fY(0.0), fZ(0.0)
121 {
122  /* dummy */
123 }
124 
126  : fX(p.getX()), fY(p.getY()), fZ(p.getZ())
127 {
128  /* dummy */
129 }
130 
131 inline Spl_U3dPoint::Spl_U3dPoint (double x, double y, double z)
132  : fX(x), fY(y), fZ(z)
133 {
134  /* dummy */
135 }
136 /*
137  inline Spl_U3dPoint::Spl_U3dPoint (Spl_U3dVertex vertex)
138  : fX(vertex.x), fY(vertex.y), fZ(vertex.z)
139  {
140 
141  }*/
142 
143 inline Spl_U3dPoint&
145 {
146  fX = p.getX();
147  fY = p.getY();
148  fZ = p.getZ();
149  return *this;
150 }
151 
152 inline double
154 {
155  return fX;
156 }
157 
158 inline double
160 {
161  return fY;
162 }
163 
164 inline double
166 {
167  return fZ;
168 }
169 
170 inline void
171 Spl_U3dPoint::setXYZ (double x, double y, double z)
172 {
173  fX = x;
174  fY = y;
175  fZ = z;
176 }
177 
178 inline void
180 {
181  fX = x;
182 }
183 
184 inline void
186 {
187  fY = y;
188 }
189 
190 inline void
192 {
193  fZ = z;
194 }
195 
196 typedef vector<Spl_U3dPoint*> Spl_U3dPointsAr;
197 typedef vector<Spl_U3dPointsAr> Spl_U3dPointsMatrix;
198 
199 
200 /*=========================== GLOBAL FUNCTIONS ========================*/
201 
203 inline double pDistance(Spl_U3dPoint p0, Spl_U3dPoint p1)
204 {
205  return sqrt ( (p0.getX()-p1.getX())*(p0.getX()-p1.getX()) +
206  (p0.getY()-p1.getY())*(p0.getY()-p1.getY()) +
207  (p0.getZ()-p1.getZ())*(p0.getZ()-p1.getZ()) );
208 };
209 
210 //------------------------------------------------------------------------------------------
212 typedef std::vector<double> Spl_U3dMathDArray;
213 
215 typedef std::vector<Spl_U3dMathDArray> Spl_U3dMathDMatrix;
216 
219 {
220 public:
223  static double compositionMul(const Spl_U3dPoint &p1, const Spl_U3dPoint &p2, const Spl_U3dPoint &p3);
224 
226  static double scalarMul(const Spl_U3dPoint &p1, const Spl_U3dPoint &p2);
227 
230  static void matrix4x4Mul(const double (&m1)[4][4], const double (&m2)[4][4], double (&m3)[4][4],
231  bool tr1 = false, bool tr2 = false);
232 
234  static void matrix2vec4x4Mul(const double (&m)[4][4], const double (&u)[4], double (&v)[4],
235  bool tr = false);
236 
238  static void vec2matrix4x4Mul(const double (&u)[4], const double (&m)[4][4], double (&v)[4],
239  bool tr = false);
240 
243  static void getLineCoeffs(double x1, double y1, double x2, double y2,
244  double &k, double &b) {
245  k = (y1 - y2) / (x1 - x2);
246  b = y1 - k*x1;
247  }
249  static int solveSquareEq(double a, double b, double c, double &x1, double &x2);
250 
252  static double m_d_prec;
253 };
254 
255 //------------------------------------------------------------------------------------------
256 using namespace std;
257 
259 #define U3D_MATH_BSPL_PARAM_PREC 1.e-10
260 
261 //------------------------------------------------------------------------------------------
266 {
267 public:
268 
270  Spl_U3dBspl3AbstractMatrix(){m_is_constructed = false;};
271 
274 
277 
280  virtual double getB(int i, double t) const;
281 
283  virtual void info() const;
284 
286  bool isConstructed() const
287  {return m_is_constructed;}
288 
290  const double (&get() const)[4][4];
291 
292 protected:
293 
296 
298  double m_bm[4][4];
299 };
300 
301 //------------------------------------------------------------------------------------------
304 {
305 public:
306 
308  Spl_U3dBspl3BetaMatrix(double beta1, double beta2);
309 
312 
315 
316 protected:
317 
319  void reconstruct(double beta1, double beta2);
320 };
321 
322 //------------------------------------------------------------------------------------------
325 {
326 public:
327 
329  Spl_U3dBspl3CRomMatrix(double t);
330 
333 
336 
337 protected:
338 
340  void reconstruct(double t);
341 };
342 
343 //------------------------------------------------------------------------------------------
346 {
347 public:
348 
351 
354 
357 
358 protected:
359 
361  void reconstruct();
362 };
363 
364 //------------------------------------------------------------------------------------------
367 {
368 public:
369 
372 
375 
378 
379 protected:
380 
382  void reconstruct();
383 };
384 
385 
386 //------------------------------------------------------------------------------------------
389 {
390 public:
391 
395  Spl_U3dSimpleBspl3Curve(const Spl_U3dBspl3AbstractMatrix *bm, const double (&cp)[4]);
396 
399 
401  double get(double p) const;
402 
404  double getSp(double p) const;
405 
407  double getSpp(double p) const;
408 
410  void getCoeffs(double (&c)[4]) const {
411  c[0] = m_rv[0];
412  c[1] = m_rv[1];
413  c[2] = m_rv[2];
414  c[3] = m_rv[3];
415  }
416 
419  double getMaxLineDeviation(double k, double b, double p1, double p2);
420 
422  double getLineDeviation(double k, double b, double p);
423 
424 protected:
425 
428 
430  double m_rv[4];
431 
432 private:
433 
435  void cp2rv(const Spl_U3dBspl3AbstractMatrix *bm, const double (&cp)[4]);
436 };
437 
439 typedef vector<Spl_U3dSimpleBspl3Curve*> Spl_U3dSimpleBspl3CurveAr;
440 
441 //------------------------------------------------------------------------------------------
445 {
446 public:
447 
451  Spl_U3dSimpleBspl3Surface(const Spl_U3dBspl3AbstractMatrix *bm, const double (&cp)[4][4]);
452 
455 
457  double get(double p, double q) const;
458 
460  double getSp(double p, double q) const;
461 
463  double getSq(double p, double q) const;
464 
466  double getSpp(double p, double q) const;
467 
469  double getSqq(double p, double q) const;
470 
472  double getSpq(double p, double q) const;
473 
474 protected:
475 
478 
480  double m_rm[4][4];
481 
483  double calc(double *pp, double *qq) const;
484 
485 private:
486 
488  void cp2rm(const Spl_U3dBspl3AbstractMatrix *bm, const double (&cp)[4][4]);
489 };
490 
491 
492 //------------------------------------------------------------------------------------------
496 {
497 public:
498 
502  Spl_U3dSimpleBspl3Surf3d(const Spl_U3dBspl3AbstractMatrix *bm, const double (&cp)[4][4][4]);
503 
506 
508  double get(double p, double q, double r) const;
509 
510 protected:
511 
513  double m_rt[4][4][4];
514 
517 
519  void make_tensor(const Spl_U3dBspl3AbstractMatrix *bm, const double (&cp)[4][4][4]);
520 
522  double calc(double *pp, double *qq, double *rr) const;
523 };
524 
525 
526 //------------------------------------------------------------------------------------------
530 {
531 public:
533  Spl_U3dBspl3Curve (const Spl_U3dBspl3AbstractMatrix *bm, const std::vector<double> &ps);
534 
536  virtual ~Spl_U3dBspl3Curve();
537 
539  double get(double p) const;
540 
541 protected:
542 
544  Spl_U3dSimpleBspl3CurveAr m_spline;
545 
548 
549 private:
550 
552  bool ips2spline(const Spl_U3dBspl3AbstractMatrix *bm, const std::vector<double> &ps);
553 };
554 
555 //------------------------------------------------------------------------------------------
556 
561 {
562 public:
565  Spl_U3dBspl3Curve3d(const Spl_U3dBspl3AbstractMatrix &bm, const Spl_U3dPointsAr &ps);
566 
569 
570 
573  bool getPoint(double p, Spl_U3dPoint &point) const;
574 
577  bool getXYZp(double p, Spl_U3dPoint &point) const;
578 
581  bool getXYZpp(double p, Spl_U3dPoint &point) const;
582 
584  bool getCurvature(double p, double &c);
585 
587  int getControlPointsNum() const;
588 
591  bool getControlPoint(int ind, Spl_U3dPoint &point) const;
592 
593 
597  bool add(Spl_U3dPoint &point, int ind = -1);
598 
601  bool remove(int ind);
602 
605  bool move(int ind);
606 
607 
609  bool isReady() const
610  {return m_is_ready;}
611 
612 
616  bool getLineSegments(double ds, std::vector<double> &segs);
617 
619  bool getLineDeviations(double p1, double p2, double p, Spl_U3dPoint &dev);
620 
623  bool getMaxLineDeviation(double p1, double p2, double &md);
624 
625 
627  void info();
628 
629 protected:
630 
632  struct Bspl3dCurve
633  {
636  };
637 
639  std::vector<Bspl3dCurve> m_bspls;
640 
643 
645  Spl_U3dPointsAr m_ps;
646 
649 
651  void reconstruct();
652 
654  void clear();
655 
659  bool check_add_bps(const Spl_U3dPointsAr &ps);
660 
662  bool find_element(double p, int &nn, double &pp) const;
663 
665  bool get_line_distance(double p, Spl_U3dPoint kP, Spl_U3dPoint bP, Spl_U3dPoint &dist);
666 
668  bool get_line_distance(double pp, int nn, Spl_U3dPoint kP, Spl_U3dPoint bP, Spl_U3dPoint &dist);
669 
671  bool construct_isect_line(double p1, double p2, Spl_U3dPoint &kP, Spl_U3dPoint &bP);
672 
674  void devs_update(double p, int n, Spl_U3dPoint &kP, Spl_U3dPoint &bP,
675  Spl_U3dPoint &dev, double &smx, double &smy, double &smz) {
676  get_line_distance(p, n, kP, bP, dev);
677  if (dev.getX() > smx)
678  smx = dev.getX();
679  if (dev.getY() > smy)
680  smy = dev.getY();
681  if (dev.getZ() > smz)
682  smz = dev.getZ();
683  }
684 
687  void next_segment (double p1, double p2, std::vector<double> &segs);
688 };
689 
690 //------------------------------------------------------------------------------------------
691 
693 class BaseSpline2D : public ParSpline
694 {
695 public:
696 
698  BaseSpline2D();
699 
701  BaseSpline2D(double *xx, double *zz, double *ddx, double *ddz, double *ccurv,bool *flag, int nn);
702 
704  virtual ~BaseSpline2D();
705 
706 protected:
707 
709  virtual int IntervalSmoothParSpline(int N1,double *x1,double *z1,bool *flag,int N2, double *xf,double *zf,
710  double rc, double sp, double_ar *xx1, double_ar *zz1, double_ar *dx, double_ar *dz,
711  double_ar *curvity,int_ar *NumNodeBreak);
712 };
713 
714 #endif /* base_splines.hpp */
void devs_update(double p, int n, Spl_U3dPoint &kP, Spl_U3dPoint &bP, Spl_U3dPoint &dev, double &smx, double &smy, double &smz)
Definition: base_splines.hpp:674
virtual ~Spl_U3dSimpleBspl3Surface()
Definition: base_splines.hpp:454
const Spl_U3dBspl3AbstractMatrix * m_bm
Definition: base_splines.hpp:477
static int solveSquareEq(double a, double b, double c, double &x1, double &x2)
Definition: base_splines.hpp:265
Definition: base_splines.hpp:529
Spl_U3dPoint()
Definition: base_splines.hpp:119
Spl_U3dBspl3AbstractMatrix()
Definition: base_splines.hpp:270
Spl_U3dPointsAr m_ps
Definition: base_splines.hpp:645
static void matrix2vec4x4Mul(const double(&m)[4][4], const double(&u)[4], double(&v)[4], bool tr=false)
Spl_U3dBspl3AbstractMatrix * m_bm
Definition: base_splines.hpp:642
Definition: base_splines.hpp:632
static void vec2matrix4x4Mul(const double(&u)[4], const double(&m)[4][4], double(&v)[4], bool tr=false)
Definition: base_splines.hpp:693
bool isReady() const
Definition: base_splines.hpp:609
const Spl_U3dBspl3AbstractMatrix * m_bm
Definition: base_splines.hpp:516
Definition: base_splines.hpp:218
Definition: base_splines.hpp:324
virtual ~Spl_U3dSimpleBspl3Surf3d()
Definition: base_splines.hpp:505
virtual ~Spl_U3dBspl3BezierMatrix()
Definition: base_splines.hpp:377
void getCoeffs(double(&c)[4]) const
Definition: base_splines.hpp:410
virtual ~Spl_U3dBspl3AbstractMatrix()
Definition: base_splines.hpp:276
double fX
Definition: base_splines.hpp:112
double getZ() const
Definition: base_splines.hpp:165
Definition: base_splines.hpp:495
static double scalarMul(const Spl_U3dPoint &p1, const Spl_U3dPoint &p2)
double getX() const
Definition: base_splines.hpp:153
Definition: geometry.H:16
static double m_d_prec
Definition: base_splines.hpp:252
Definition: base_splines.hpp:444
void setY(double y)
Definition: base_splines.hpp:185
bool isConstructed() const
Definition: base_splines.hpp:286
Definition: base_splines.hpp:388
static void getLineCoeffs(double x1, double y1, double x2, double y2, double &k, double &b)
Definition: base_splines.hpp:243
Definition: objectar.hpp:57
Definition: base_splines.hpp:40
bool m_is_ready
Definition: base_splines.hpp:648
double getY() const
Definition: base_splines.hpp:159
bool m_is_constructed
Definition: base_splines.hpp:295
virtual ~Spl_U3dSimpleBspl3Curve()
Definition: base_splines.hpp:398
void setZ(double z)
Definition: base_splines.hpp:191
Definition: base_splines.hpp:560
static double compositionMul(const Spl_U3dPoint &p1, const Spl_U3dPoint &p2, const Spl_U3dPoint &p3)
Definition: parspline.hpp:14
Definition: base_splines.hpp:303
const Spl_U3dBspl3AbstractMatrix * m_bm
Definition: base_splines.hpp:427
virtual ~Spl_U3dBspl3BSMatrix()
Definition: base_splines.hpp:356
Definition: base_splines.hpp:345
virtual ~Spl_U3dBspl3BetaMatrix()
Definition: base_splines.hpp:314
virtual ~Spl_U3dBspl3CRomMatrix()
Definition: base_splines.hpp:335
bool m_is_interpolated
Definition: base_splines.hpp:547
Definition: objectar.hpp:55
std::vector< Bspl3dCurve > m_bspls
Definition: base_splines.hpp:639
void setXYZ(double x, double y, double z)
Definition: base_splines.hpp:171
Definition: base_splines.hpp:21
Spl_U3dPoint & operator=(const Spl_U3dPoint &p)
Definition: base_splines.hpp:144
Spl_U3dSimpleBspl3CurveAr m_spline
Definition: base_splines.hpp:544
Spl_U3dSimpleBspl3Curve * x
Definition: base_splines.hpp:635
static void matrix4x4Mul(const double(&m1)[4][4], const double(&m2)[4][4], double(&m3)[4][4], bool tr1=false, bool tr2=false)
void setX(double x)
Definition: base_splines.hpp:179
Definition: base_splines.hpp:366