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