spline3d.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**********************************************************************
  2. *<
  3. FILE: spline3d.cpp
  4. DESCRIPTION: General-purpose 3D spline class
  5. CREATED BY: Tom Hudson & Dan Silva
  6. HISTORY: created 2/23/95
  7. *> Copyright (c) 1995, All Rights Reserved.
  8. **********************************************************************/
  9. #ifndef __SPLINE3D_H__
  10. #define __SPLINE3D_H__
  11. #include "polyshp.h" // Need this for PolyLine class
  12. // Point flags for PolyShape representation
  13. #define BEZ_SHAPE_KNOT (1<<0) // It's a knot point
  14. #define BEZ_SHAPE_INTERPOLATED (1<<1) // It's an interpolated point between two knots
  15. // Line types:
  16. #define LTYPE_CURVE 0
  17. #define LTYPE_LINE 1
  18. // Compound line types
  19. #define CURVE_CURVE (LTYPE_CURVE | (LTYPE_CURVE<<2))
  20. #define LINE_CURVE (LTYPE_LINE | (LTYPE_CURVE<<2))
  21. #define CURVE_LINE (LTYPE_CURVE | (LTYPE_LINE<<2))
  22. #define LINE_LINE (LTYPE_LINE | (LTYPE_LINE<<2))
  23. // Knot types
  24. #define KTYPE_AUTO 0
  25. #define KTYPE_CORNER 1
  26. #define KTYPE_BEZIER 2
  27. #define KTYPE_BEZIER_CORNER (KTYPE_BEZIER | KTYPE_CORNER)
  28. // Parameter types
  29. #define PARM_UNIFORM 0
  30. #define PARM_ARCLENGTH 1
  31. #define PARM_CENTRIPETAL 2
  32. #define PARM_CUSTOM 3
  33. class Spline3D;
  34. class SplineKnot {
  35. int ktype;
  36. int ltype;
  37. Point3 point;
  38. Point3 inVec;
  39. Point3 outVec;
  40. public:
  41. SplineKnot(int k, int l, Point3 p, Point3 in, Point3 out) { ktype=k; ltype=l; point=p; inVec=in; outVec=out; }
  42. inline int Ktype() { return ktype; }
  43. inline int Ltype() { return ltype; }
  44. friend class Spline3D;
  45. };
  46. typedef struct {
  47. int ktype; // Knot type
  48. int ltype; // Line type
  49. float du; // Parameter value
  50. int aux; // Used in capping
  51. } Knot;
  52. // Private spline flags
  53. #define SPLINE_CLOSED (1<<0)
  54. class Spline3D {
  55. private:
  56. static int splineCount; // Number of splines in the system
  57. int parmType; // Interpolation parameter type (needed?)
  58. int knotCount; // Number of points in spline
  59. int flags; // Private flags
  60. // HMENU hMenuPType;
  61. int iCur; // Current editing point
  62. int Interact(ViewExp *vpt,int msg, int point, int flags, IPoint2 m, Matrix3* mat ); // Handle mouse interaction
  63. float cachedLength;
  64. BOOL cacheValid;
  65. public:
  66. // Should consolidate all of these into one knot structure -- Was this way for simple Windows GDI version
  67. Knot * knots; // Knot attributes array
  68. Point3 * bezp; // Bezier point array
  69. int drawPhase; // Drawing phase
  70. int editMode; // 1 if editing, 0 otherwise
  71. // Creation settings
  72. int initialType; // Knot type at initial click
  73. int dragType; // Knot type at drag
  74. CoreExport Spline3D(int itype = KTYPE_CORNER,int dtype = KTYPE_BEZIER,int ptype = PARM_UNIFORM); // Constructor
  75. CoreExport ~Spline3D(); // Destructor
  76. CoreExport Spline3D& operator=(Spline3D& fromSpline);
  77. CoreExport Spline3D& operator=(PolyLine& fromLine);
  78. CoreExport void NewSpline();
  79. CoreExport void Allocate(int count);
  80. inline int ParmType() { return parmType; };
  81. inline int KnotCount() { return knotCount; } // Point (knot) count
  82. inline int Flags() { return flags; }
  83. inline int Segments() { return knotCount + Closed() - 1; } // Segment count
  84. inline int Closed() { return (flags & SPLINE_CLOSED) ? 1:0; } // Returns closed status
  85. CoreExport int ShiftKnot(int where,int direction); // Shove array left or right 1,
  86. // starting at given point
  87. CoreExport int AddKnot(SplineKnot &k,int where = -1); // Add a knot to the spline
  88. CoreExport int DeleteKnot(int where); // Delete the specified knot
  89. CoreExport int Create(ViewExp *vpt,int msg, int point, int flags, IPoint2 m, Matrix3* mat); // Create the spline
  90. CoreExport int StartInsert(ViewExp *vpt,int msg, int point, int flags, IPoint2 theP, Matrix3* mat, int where ); // Start an insertion operation on the spline
  91. CoreExport void ChordParams(); // Compute chord length params
  92. CoreExport void UniformParams(); // Compute uniform params
  93. CoreExport void CentripetalParams(); // Compute centripetal params
  94. CoreExport int SetParam(int index,float param); // Set custom param value
  95. CoreExport float GetParam(int index); // Get param value
  96. inline int GetKnotType(int index) { return knots[index].ktype; }
  97. CoreExport int SetKnotType(int index,int type); // Set the knot type
  98. inline int GetLineType(int index) { return knots[index].ltype; }
  99. CoreExport int SetLineType(int index,int type); // Set the line type
  100. virtual void CustomParams() { UniformParams(); } // Replace this as needed
  101. CoreExport void CompParams(); // Compute param values
  102. CoreExport void ComputeBezPoints();
  103. CoreExport void LinearFwd(int i);
  104. CoreExport void LinearBack(int i);
  105. CoreExport void ContinFwd(int i);
  106. CoreExport void ContinBack(int i);
  107. CoreExport void HybridPoint(int i);
  108. CoreExport void CompCornerBezPoints(int n);
  109. CoreExport void CompAdjBesselBezPoints(int i);
  110. CoreExport void BesselStart(int i);
  111. CoreExport void BesselEnd(int i);
  112. CoreExport void NaturalFwd(int i);
  113. CoreExport void NaturalBack(int i);
  114. CoreExport Point2 InterpBezier(IPoint2 *bez, float t);
  115. CoreExport Point3 InterpBezier3D(int segment, float t);
  116. CoreExport Point3 InterpCurve3D(float u);
  117. CoreExport Point3 TangentBezier3D(int segment, float t);
  118. CoreExport Point3 TangentCurve3D(float u);
  119. CoreExport void BoundingRect(RECT *r, IPoint2 *p, int npoints);
  120. CoreExport Point3 AverageTangent(int i);
  121. CoreExport void MakeBezCont(int i);
  122. CoreExport void RedistTangents(int i, Point3 d);
  123. CoreExport void FixAdjBezTangents(int i);
  124. CoreExport void DrawCurve(GraphicsWindow *gw, Material *mtl);
  125. inline void SetEditMode(int mode) { editMode = mode ? 1:0; }
  126. CoreExport int IsAuto(int i);
  127. CoreExport int IsBezierPt(int i);
  128. CoreExport int IsCorner(int i);
  129. CoreExport Point3 GetDragVector(ViewExp *vpt,IPoint2 p,int i,Matrix3* mat);
  130. CoreExport int InsertPoint(ViewExp *vpt,int where, IPoint2& p, float t);
  131. CoreExport int AppendPoint(ViewExp *vpt,const Point3& p, int where = -1);
  132. CoreExport void ComputeAdjBezPts(int n);
  133. CoreExport int DrawPhase() { return drawPhase; }
  134. CoreExport int GetiCur() { return iCur; }
  135. CoreExport void CheckMenu(HMENU hMenu, int idfirst, int idlast, int idcheck);
  136. CoreExport void RedrawCurves();
  137. CoreExport void GetBBox(TimeValue t, Matrix3& tm, Box3& box);
  138. CoreExport IPoint2 ProjectPoint(ViewExp *vpt, Point3 fp, Matrix3 *mat);
  139. CoreExport Point3 UnProjectPoint(ViewExp *vpt, IPoint2 p, Matrix3 *mat);
  140. CoreExport void Snap(GraphicsWindow *gw, SnapInfo *snap, IPoint2 *p, Matrix3 &tm);
  141. CoreExport IOResult Save(ISave *isave);
  142. CoreExport IOResult Load(ILoad *iload);
  143. CoreExport int SetClosed(int flag = 1);
  144. CoreExport int SetOpen();
  145. CoreExport void Dump(int where);
  146. inline Point3& InVec(int i) { return bezp[i*3]; }
  147. inline Point3& KnotPoint(int i) { return bezp[i*3+1]; }
  148. inline Point3& OutVec(int i) { return bezp[i*3+2]; }
  149. inline Point3& GetVert(int i) { return bezp[i]; }
  150. inline void SetVert(int i, const Point3& p) { bezp[i] = p; }
  151. inline int Verts() { return knotCount*3; }
  152. CoreExport float SplineLength();
  153. CoreExport void Transform(Matrix3 *tm);
  154. CoreExport void Reverse();
  155. CoreExport void Append(Spline3D *spline);
  156. CoreExport void Prepend(Spline3D *spline);
  157. CoreExport BOOL IsClockWise(); // 2D!
  158. CoreExport BOOL SelfIntersects(); // 2D!
  159. CoreExport BOOL IntersectsSpline(Spline3D *spline); // 2D!
  160. CoreExport BOOL SurroundsPoint(Point2 p); // 2D!
  161. CoreExport void MakePolyLine(PolyLine &line, int steps = -1, BOOL optimize = FALSE);
  162. CoreExport void InvalidateGeomCache();
  163. };
  164. #endif // __SPLINE3D_H__