Point Cloud Library (PCL)  1.9.1-dev
opennurbs_hatch.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 #pragma once
18 
19 /*
20  class ON_HatchLoop
21  /////////////////////////////////////////////////////////////////
22  Represents a 3d boundary loop curve
23 */
24 class ON_CLASS ON_HatchLoop
25 {
26 public:
27 #if defined(ON_DLL_EXPORTS) || defined(ON_DLL_IMPORTS)
28  // When the Microsoft CRT(s) is/are used, this is the best
29  // way to prevent crashes that happen when a hatch loop is
30  // allocated with new in one DLL and deallocated with
31  // delete in another DLL.
32 
33  // new/delete
34  void* operator new(size_t);
35  void operator delete(void*);
36 
37  // array new/delete
38  void* operator new[] (size_t);
39  void operator delete[] (void*);
40 
41  // in place new/delete
42  void* operator new(size_t,void*);
43  void operator delete(void*,void*);
44 #endif
45 
46  enum eLoopType
47  {
48  ltOuter = 0,
49  ltInner = 1,
50  };
51 
52  ON_HatchLoop();
53  ON_HatchLoop( ON_Curve* pCurve2d, eLoopType type = ltOuter);
54  ON_HatchLoop( const ON_HatchLoop& src);
55  ~ON_HatchLoop();
56 
57  ON_HatchLoop& operator=( const ON_HatchLoop& src);
58 
59  ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
60  void Dump( ON_TextLog& ) const; // for debugging
61  ON_BOOL32 Write( ON_BinaryArchive&) const;
62  ON_BOOL32 Read( ON_BinaryArchive&);
63 
64  // Interface
65  /////////////////////////////////////////////////////////////////
66 
67  /*
68  Description:
69  Get a closed 2d curve boundary loop
70  Parameters:
71  Return:
72  Pointer to loop's 2d curve
73  */
74  const ON_Curve* Curve() const;
75 
76  /*
77  Description:
78  Specify the 2d loop curve in the hatch's plane coordinates
79  Parameters:
80  curve - [in] 2d input curve
81  Return:
82  true: success, false, curve couldn't be duplicated
83  Remarks:
84  The curve is copied
85  */
86  bool SetCurve( const ON_Curve& curve);
87 
88  /*
89  Description:
90  Get the type flag of the loop
91  Returns:
92  eLoopType::ltInner or eLoopType::ltOuter
93  */
94  eLoopType Type() const;
95 
96  /*
97  Description:
98  Specify the type flag of the loop
99  Parameters:
100  type - [in] ltInner or ltOuter
101  */
102  void SetType( eLoopType type);
103 
104 protected:
105  friend class ON_Hatch;
106  eLoopType m_type; // loop type flag - inner or outer
107  ON_Curve* m_p2dCurve; // 2d closed curve bounding the hatch
108  // This is really a 3d curve with z coordinates = 0
109 };
110 
111 
112 /*
113  class ON_HatchLine
114  /////////////////////////////////////////////////////////////////
115  Represents one line of a hatch pattern
116  Similar to AutoCAD's .pat file definition
117  ON_HatchLine's are used by ON_HatchPattern
118  to specify the dashes and offset patterns of the lines.
119 
120  Each line has the following information:
121  Angle is the direction of the line CCW from the x axis
122  The first line origin is at base
123  Each line repetition is offset by offset from the previous line
124  offset.x is parallel to the line and
125  offset.y is perpendicular to the line
126  The base and offset values are rotated by the line's angle to
127  produce a location in the hatch pattern's coordinate system
128  There can be gaps and dashes specified for drawing the line
129 
130  If there are no dashes, the line is solid
131  Negative length dashes are gaps
132  Positive length dashes are drawn as line segments
133 */
134 
135 class ON_CLASS ON_HatchLine
136 {
137 public:
138  ON_HatchLine();
139  // C++ default copy construction and operator= work fine.
140 
141  ON_HatchLine(
142  double angle,
143  const ON_2dPoint& base,
144  const ON_2dVector& offset,
145  const ON_SimpleArray<double> dashes);
146 
147  bool operator==( const ON_HatchLine&) const;
148  bool operator!=( const ON_HatchLine&) const;
149 
150  ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
151  void Dump( ON_TextLog& ) const; // for debugging
152  ON_BOOL32 Write( ON_BinaryArchive&) const; // serialize definition to binary archive
153  ON_BOOL32 Read( ON_BinaryArchive&); // restore definition from binary archive
154 
155  // Interface
156  /////////////////////////////////////////////////////////////////
157 
158  /*
159  Description:
160  Get angle of the hatch line.
161  CCW from x-axis
162  Parameters:
163  Return:
164  The angle in radians
165  */
166  double Angle() const;
167 
168  /*
169  Description:
170  Set angle of the hatch line.
171  CCW from x-axis
172  Parameters:
173  angle - [in] angle in radians
174  Return:
175  */
176  void SetAngle( double angle);
177 
178  /*
179  Description:
180  Get this line's 2d basepoint
181  Parameters:
182  Return:
183  the base point
184  */
185  ON_2dPoint Base() const;
186  /*
187  Description:
188  Set this line's 2d basepoint
189  Parameters:
190  base - [in] the basepoint
191  Return:
192  */
193  void SetBase( const ON_2dPoint& base);
194 
195  /*
196  Description:
197  Get this line's 2d offset for line repetitions
198  Offset().x is shift parallel to line
199  Offset().y is spacing perpendicular to line
200  Parameters:
201  Return:
202  the offset
203  */
204  ON_2dVector Offset() const;
205 
206  /*
207  Description:
208  Get this line's 2d offset for line repetitions
209  Offset().x is shift parallel to line
210  Offset().y is spacing perpendicular to line
211  Parameters:
212  offset - [in] the shift,spacing for repeated lines
213  Return:
214  */
215  void SetOffset( const ON_2dVector& offset);
216 
217  /*
218  Description:
219  Get the number of gaps + dashes in the line
220  Parameters:
221  Return:
222  nummber of dashes in the line
223  */
224  int DashCount() const;
225 
226  /*
227  Description:
228  Get the dash length at index
229  Parameters:
230  index - [in] the dash to get
231  Return:
232  the length of the dash ( gap if negative)
233  */
234  double Dash( int) const;
235 
236  /*
237  Description:
238  Add a dash to the pattern
239  Parameters:
240  dash - [in] length to append - < 0 for a gap
241  */
242  void AppendDash( double dash);
243 
244  /*
245  Description:
246  Specify a new dash array
247  Parameters:
248  dashes - [in] array of dash lengths
249  */
250  void SetPattern( const ON_SimpleArray<double>& dashes);
251 
252  /*
253  Description:
254  Get the line's angle, base, offset and dashes
255  in one function call
256  Parameters:
257  angle - [out] angle in radians CCW from x-axis
258  base - [out] origin of the master line
259  offset - [out] offset for line replications
260  dashes - [out] the dash array for the line
261  Return:
262  */
263  void GetLineData(
264  double& angle,
265  ON_2dPoint& base,
266  ON_2dVector& offset,
267  ON_SimpleArray<double>& dashes) const;
268 
269  /*
270  Description:
271  Get the total length of a pattern repeat
272  Parameters:
273  Return:
274  Pattern length
275  */
276  double GetPatternLength() const;
277 
278 public:
279  double m_angle;
283 };
284 
285 
286 
287 
288 #if defined(ON_DLL_TEMPLATE)
289 // This stuff is here because of a limitation in the way Microsoft
290 // handles templates and DLLs. See Microsoft's knowledge base
291 // article ID Q168958 for details.
292 #pragma warning( push )
293 #pragma warning( disable : 4231 )
294 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_HatchLoop*>;
295 ON_DLL_TEMPLATE template class ON_CLASS ON_ClassArray<ON_HatchLine>;
296 #pragma warning( pop )
297 #endif
298 
299 
300 /*
301  class ON_HatchPattern
302  /////////////////////////////////////////////////////////////////
303  Fill definition for a hatch
304 
305  The hatch will be one of
306  ON_Hatch::ftLines - pat file style definition
307  ON_Hatch::ftGradient - uses a color function
308  ON_Hatch::ftSolid - uses entity color
309 
310 */
311 class ON_CLASS ON_HatchPattern : public ON_Object
312 {
313  ON_OBJECT_DECLARE( ON_HatchPattern);
314 
315 public:
316 
318  {
319  ftSolid = 0, // uses entity color
320  ftLines = 1, // pat file definition
321  ftGradient = 2, // uses a fill color function
322  ftLast = 3
323  };
324 
325  ON_HatchPattern();
326  ~ON_HatchPattern();
327  // C++ default copy construction and operator= work fine.
328 
329  // ON_Object overrides
330  /////////////////////////////////////////////////////////////////
331  ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
332  void Dump( ON_TextLog& ) const; // for debugging
333  ON_BOOL32 Write( ON_BinaryArchive&) const;
334  ON_BOOL32 Read( ON_BinaryArchive&);
335 
336  // virtual
337  ON_UUID ModelObjectId() const;
338 
339 
340  //////////////////////////////////////////////////////////////////////
341  // Interface
342 
343  /*
344  Description:
345  Return the pattern's fill type
346  Parameters:
347  */
348  eFillType FillType() const;
349 
350  /*
351  Description:
352  Set the pattern's fill type
353  Parameters:
354  type - [in] the new filltype
355  */
356  void SetFillType( eFillType type);
357 
358  /*
359  Description:
360  Set the name of the pattern
361  Parameters:
362  pName - [in] the new name
363  Returns:
364  */
365  void SetName( const wchar_t* pName);
366  void SetName( const char* pName);
367 
368  /*
369  Description:
370  Get the name of the pattern
371  Parameters:
372  string - [out] The name is returned here
373  */
374  void GetName( ON_wString& string) const;
375 
376  /*
377  Description:
378  Get the name of the pattern
379  Returns:
380  The name string
381  */
382  const wchar_t* Name() const;
383 
384  /*
385  Description:
386  Set the name of the pattern
387  Parameters:
388  pDescription - [in] the new description
389  Returns:
390  */
391  void SetDescription( const wchar_t* pDescription);
392  void SetDescription( const char* pDescription);
393 
394  /*
395  Description:
396  Get a short description of the pattern
397  Parameters:
398  string - [out] The string is returned here
399  */
400  void GetDescription( ON_wString& string) const;
401 
402  /*
403  Description:
404  Return a short text description of the pattern type
405  Parameters:
406  Returns:
407  The description string
408  */
409  const wchar_t* Description() const;
410 
411  /*
412  Description:
413  Set the table index of the pattern
414  Parameters:
415  index - [in] the new index
416  Returns:
417  */
418  void SetIndex( int index);
419 
420  /*
421  Description:
422  Return the table index of the pattern
423  Parameters:
424  Returns:
425  The table index
426  */
427  int Index() const;
428 
429  // Interface functions for line hatches
430  /////////////////////////////////////////////////////////////////
431  /*
432  Description:
433  Get the number of ON_HatchLines in the pattern
434  Parameters:
435  Return:
436  number of lines
437  */
438  int HatchLineCount() const;
439 
440  /*
441  Description:
442  Add an ON_HatchLine to the pattern
443  Parameters:
444  line - [in] the line to add
445  Return:
446  >= 0 index of the new line
447  -1 on failure
448  */
449  int AddHatchLine( const ON_HatchLine& line);
450 
451  /*
452  Description:
453  Get the ON_HatchLine at index
454  Parameters:
455  index - [in] Index of the line to get
456  Return:
457  the hatch line
458  NULL if index is out of range
459  */
460  const ON_HatchLine* HatchLine( int index) const;
461 
462  /*
463  Description:
464  Remove a hatch line from the pattern
465  Parameters:
466  index - [in] Index of the line to remove
467  Return:
468  true - success
469  false - index out of range
470  */
471  bool RemoveHatchLine( int index);
472 
473  /*
474  Description:
475  Remove all of the hatch line from the pattern
476  Parameters:
477 
478  Return:
479  true - success
480  false - index out of range
481  */
482  void RemoveAllHatchLines();
483 
484  /*
485  Description:
486  Set all of the hatch lines at once.
487  Existing hatchlines are deleted.
488  Parameters:
489  lines - [in] Array of lines to add. Lines are copied
490  Return:
491  number of lines added
492  */
493  int SetHatchLines( const ON_ClassArray<ON_HatchLine> lines);
494 
495 public:
496  int m_hatchpattern_index; // Index in the hatch pattern table
497  ON_wString m_hatchpattern_name; // String name of the pattern
499 
501 
502  ON_wString m_description; // String description of the pattern
503 
504  // Represents a collection of ON_HatchLine's to make a complete pattern
505  // This is the definition of a hatch pattern.
506  // Simple solid line hatches with fixed angle and spacing are also
507  // represented with this type of hatch
508  ON_ClassArray<ON_HatchLine> m_lines; // used by line hatches
509 };
510 
511 /*
512  class ON_Hatch
513  /////////////////////////////////////////////////////////////////
514  Represents a hatch in planar boundary loop or loops
515  This is a 2d entity with a plane defining a local coordinate system
516  The loops, patterns, angles, etc are all in this local coordinate system
517 
518  The ON_Hatch object manages the plane and loop array
519  Fill definitions are in the ON_HatchPattern or class derived from ON_HatchPattern
520  ON_Hatch has an index to get the pattern definition from the pattern table
521 
522 */
523 class ON_CLASS ON_Hatch : public ON_Geometry
524 {
525  ON_OBJECT_DECLARE( ON_Hatch);
526 
527 public:
528  // Default constructor
529  ON_Hatch();
530  ON_Hatch( const ON_Hatch&);
531  ON_Hatch& operator=(const ON_Hatch&);
532  ~ON_Hatch();
533 
534  virtual ON_Hatch* DuplicateHatch() const;
535 
536  // ON_Object overrides
537  /////////////////////////////////////////////////////////////////
538  ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
539  void Dump( ON_TextLog& ) const; // for debugging
540  ON_BOOL32 Write( ON_BinaryArchive&) const;
541  ON_BOOL32 Read( ON_BinaryArchive&);
542  ON::object_type ObjectType() const;
543 
544  // ON_Geometry overrides
545  /////////////////////////////////////////////////////////////////
546  /*
547  Returns the geometric dimension of the object ( usually 3)
548  */
549  int Dimension() const;
550 
551  /*
552  Description:
553  Get a bounding 3d WCS box of the object
554  This is a bounding box of the boundary loops
555  Parameters:
556  [in/out] double* boxmin - pointer to dim doubles for min box corner
557  [in/out] double* boxmax - pointer to dim doubles for max box corner
558  [in] ON_BOOL32 growbox - true to grow the existing box,
559  false ( the default) to reset the box
560  Returns:
561  true = Success
562  false = Failure
563  Remarks:
564  */
565  ON_BOOL32 GetBBox( double*, double*, ON_BOOL32 = false) const;
566 
567  /*
568  Description:
569  Get tight bounding box of the hatch.
570  Parameters:
571  tight_bbox - [in/out] tight bounding box
572  bGrowBox -[in] (default=false)
573  If true and the input tight_bbox is valid, then returned
574  tight_bbox is the union of the input tight_bbox and the
575  tight bounding box of the hatch.
576  xform -[in] (default=NULL)
577  If not NULL, the tight bounding box of the transformed
578  hatch is calculated. The hatch is not modified.
579  Returns:
580  True if the returned tight_bbox is set to a valid
581  bounding box.
582  */
583  bool GetTightBoundingBox(
584  ON_BoundingBox& tight_bbox,
585  int bGrowBox = false,
586  const ON_Xform* xform = 0
587  ) const;
588 
589 
590  /*
591  Description:
592  Transform the object by a 4x4 xform matrix
593 
594  Parameters:
595  [in] xform - An ON_Xform with the transformation information
596  Returns:
597  true = Success
598  false = Failure
599  Remarks:
600  The object has been transformed when the function returns.
601  */
602  ON_BOOL32 Transform( const ON_Xform&);
603 
604  // Interface
605  /////////////////////////////////////////////////////////////////
606 
607  /*
608  Description:
609  Create a hatch from input geometry and parameters
610  Parameters:
611  plane [I] - ON_Plane to make the hatch on
612  loops [I] - Array of boundary loops with the outer one first
613  pattern_index [I] - Index into the hatch table
614  pattern_rotation [I] - ccw in radians about plane origin
615  pattern_scale [I] - Scale factor for pattern definition
616  Returns:
617  true = success, false = failure
618  */
619  bool Create( const ON_Plane& plane,
620  const ON_SimpleArray<const ON_Curve*> loops,
621  int pattern_index,
622  double pattern_rotation,
623  double pattern_scale);
624 
625  /*
626  Description:
627  Get the plane defining the hatch's coordinate system
628  Parameters:
629  Returns:
630  the plane
631  */
632  const ON_Plane& Plane() const;
633 
634  /*
635  Description:
636  Set the plane defining the hatch's coordinate system
637  Parameters:
638  plane - [in] the plane to set
639  Returns:
640  */
641  void SetPlane( const ON_Plane& plane);
642 
643  /*
644  Description:
645  Gets the rotation applied to the hatch pattern
646  when it is mapped to the hatch's plane
647  Returns:
648  The rotation in radians
649  Remarks:
650  The pattern is rotated counter-clockwise around
651  the hatch's plane origin by this value
652  */
653  double PatternRotation() const;
654 
655 /*
656  Description:
657  Sets the rotation applied to the hatch pattern
658  when it is mapped to the hatch's plane
659  Parameters:
660  rotation - [in] The rotation in radians
661  Remarks:
662  The pattern is rotated counter-clockwise around
663  the hatch's plane origin by this value
664  */
665  void SetPatternRotation( double rotation);
666 
667  /*
668  Description:
669  Gets the scale applied to the hatch pattern
670  when it is mapped to the hatch's plane
671  Returns:
672  The scale
673  Remarks:
674  The pattern is scaled around
675  the hatch's plane origin by this value
676  */
677  double PatternScale() const;
678 
679 /*
680  Description:
681  Sets the scale applied to the hatch pattern
682  when it is mapped to the hatch's plane
683  Parameters:
684  scale - [in] The scale
685  Remarks:
686  The pattern is scaled around
687  the hatch's plane origin by this value
688  */
689  void SetPatternScale( double scale);
690 
691  /*
692  Description:
693  Get the number of loops used by this hatch
694  Parameters:
695  Returns:
696  the number of loops
697  */
698  int LoopCount() const;
699 
700  /*
701  Description:
702  Add a loop to the hatch
703  Parameters:
704  loop - [in] the loop to add. Memory management for the loop is managed
705  by this class.
706  Returns:
707  */
708  void AddLoop( ON_HatchLoop* loop);
709 
710  /*
711  Description:
712  Insert a loop to the hatch at the specified index
713  Parameters:
714  index - [in] zero based index of the position where insert the loop to.
715  loop - [in] the loop to insert. Memory management for the loop is managed
716  by this class on success.
717  Returns:
718  true if success
719  false if index is lower than 0 or greater than current loop count.
720  */
721  bool InsertLoop( int index,
722  ON_HatchLoop* loop);
723 
724  /*
725  Description:
726  Remove a loop in the hatch
727  Parameters:
728  loop - [in] zero based index of the loop to remove.
729  Returns:
730  true if success
731  */
732  bool RemoveLoop( int index);
733 
734  /*
735  Description:
736  Get the loop at index
737  Parameters:
738  index - [in] which loop to get
739  Returns:
740  pointer to loop at index
741  NULL if index is out of range
742  */
743  const ON_HatchLoop* Loop( int index) const;
744 
745  /*
746  Description:
747  Get the 3d curve corresponding to loop[index]
748  Parameters:
749  index - [in] which loop to get
750  Returns:
751  pointer to 3d curve of loop at index
752  NULL if index is out of range or curve can't be made
753  Caller deletes the returned curve
754  */
755  ON_Curve* LoopCurve3d( int index) const;
756 
757  /*
758  Description:
759  Get the index of the hatch's pattern
760  Parameters:
761  Returns:
762  index of the pattern
763  */
764  int PatternIndex() const;
765 
766 /*
767  Description:
768  Set the index of the hatch's pattern
769  Parameters:
770  index - [in] pattern index to set
771  Returns:
772  */
773  void SetPatternIndex( int index);
774 
775  // Basepoint functions added March 23, 2008 -LW
776  /*
777  Description:
778  Set 2d Base point for hatch pattern alignment.
779  Parameters:
780  basepoint - 2d point in hatch's ECS
781  */
782  void SetBasePoint(ON_2dPoint basepoint);
783 
784  /*
785  Description:
786  Set 3d Base point for hatch pattern alignment.
787  Parameters:
788  point - 3d WCS point
789  Remarks:
790  Projects point to hatch's plane and sets 2d point
791  */
792  void SetBasePoint(ON_3dPoint point);
793 
794  /*
795  Description:
796  Return 3d WCS point that lies on hatch's plane used for pattern origin.
797  */
798  ON_3dPoint BasePoint() const;
799 
800  /*
801  Description:
802  Return 2d ECS point used for pattern origin.
803  */
804  ON_2dPoint BasePoint2d() const;
805 
806  /*
807  Function added June 12 2008 LW
808  Description:
809  Remove all of the loops on the hatch and add the curves in 'loops' as new loops
810  Parameters:
811  loops - [in] An array of pointers to 2d or 3d curves
812  If the curves are 2d, add them to the hatch directly
813  If they are 3d, project them to the hatch's plane first
814  Returns:
815  true - success
816  false - no loops in input array or an error adding them
817  */
818  bool ReplaceLoops(ON_SimpleArray<const ON_Curve*> loops);
819 
820 protected:
826 
827  // This function is temporary and will be removed next time the SDK can be modified.
828  class ON_HatchExtra* HatchExtension();
829 
830 };
double m_pattern_scale
ON_SimpleArray< double > m_dashes
int m_pattern_index
ON_UUID m_hatchpattern_id
eLoopType m_type
ON_wString m_hatchpattern_name
double m_pattern_rotation
ON_wString m_description
ON_ClassArray< ON_HatchLine > m_lines
ON_2dPoint m_base
ON_Plane m_plane
ON_Curve * m_p2dCurve
ON_2dVector m_offset
ON_SimpleArray< ON_HatchLoop * > m_loops